businessApply.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. const uploadJS = require('../../mixin/upload.js')
  2. const { userApplyShop } = require('./api/index')
  3. const { isMobile } = require('../../utils/validate')
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. form: {
  10. 'user_name': '', // 用户姓名
  11. 'shop_name': '', // 店铺名称
  12. 'user_card': '', // 身份证
  13. 'shop_phone': '', // 手机号码
  14. 'country_msg': '', // 乡村信息
  15. 'user_code_url': [], // 微信二维码
  16. 'user_wechat_code': '', // 微信号
  17. 'shop_img_url': [], // 店铺图片地址
  18. 'shop_address': {
  19. 'longitude': 0, // 经度
  20. 'latitude': 0, // 纬度
  21. 'address_name': '', // 起点位置名称
  22. 'name': ''
  23. }, // 店铺地址
  24. 'shop_remark': '' // 备注
  25. },
  26. booLock: false
  27. },
  28. tempFormKey: '',
  29. tempPostData: {},
  30. /**
  31. * 生命周期函数--监听页面加载
  32. */
  33. onLoad(options) {
  34. },
  35. /**
  36. * 生命周期函数--监听页面初次渲染完成
  37. */
  38. onReady() {
  39. },
  40. /**
  41. * 生命周期函数--监听页面显示
  42. */
  43. onShow() {
  44. },
  45. /**
  46. * 生命周期函数--监听页面隐藏
  47. */
  48. onHide() {
  49. },
  50. /**
  51. * 生命周期函数--监听页面卸载
  52. */
  53. onUnload() {
  54. },
  55. ...uploadJS,
  56. uploadCallBack(res) {
  57. const temp = res.map(item => {
  58. return {
  59. 'url': item.url,
  60. 'formkey': item.formkey
  61. }
  62. })
  63. let tempForm = {}
  64. let formkey = ''
  65. if (temp.length > 0) {
  66. formkey = temp[0].formkey
  67. }
  68. switch (formkey) {
  69. case 'user_code_url':
  70. case 'shop_img_url':
  71. tempForm[`form.${formkey}[0]`] = temp[0]
  72. break
  73. default:
  74. }
  75. if (Object.keys(tempForm).length > 0) {
  76. this.setData(tempForm)
  77. }
  78. },
  79. setFormValue(event) {
  80. const { value } = event.detail
  81. const { formkey } = event.target.dataset
  82. let tempForm = {}
  83. switch (formkey) {
  84. case 'user_name':
  85. case 'shop_name':
  86. case 'user_card':
  87. case 'country_msg':
  88. case 'user_wechat_code':
  89. case 'shop_remark':
  90. tempForm[`form.${formkey}`] = value
  91. break
  92. case 'shop_phone':
  93. tempForm[`form.${formkey}`] = value.replace(/[^\d]$/, '').replace(/^0/, '').replace(/(\d{11}(.*))/, '$1')
  94. break
  95. default:
  96. }
  97. this.setData(tempForm)
  98. },
  99. // 获取定位权限、定位权限授权
  100. async handleGetSetting() {
  101. const that = this
  102. try {
  103. const { errMsg, authSetting } = await wx.getSetting()
  104. if (errMsg === 'getSetting:ok') {
  105. // 有定位授权
  106. if (authSetting['scope.userLocation']) {
  107. if (that.tempFormKey === '') {
  108. } else {
  109. await that.chooseLocationBridge()
  110. }
  111. return
  112. }
  113. }
  114. } catch (err) {}
  115. try {
  116. await wx.authorize({ scope: 'scope.userLocation' })
  117. if (that.tempFormKey === '') {
  118. } else {
  119. await that.chooseLocationBridge()
  120. }
  121. } catch (err) {
  122. wx.showModal({
  123. title: '提示',
  124. content: '未开启获取地理位置权限,去设置中打开',
  125. success(res) {
  126. if (res.confirm) {
  127. that.openSetting()
  128. }
  129. }
  130. })
  131. }
  132. },
  133. // 去小程序自带设置页:返回
  134. async openSetting() {
  135. try {
  136. const openSettingData = await wx.openSetting()
  137. // 开启了定位权限
  138. if (openSettingData.authSetting['scope.userLocation']) {
  139. if (this.tempFormKey === '') {
  140. } else {
  141. await this.chooseLocationBridge()
  142. }
  143. }
  144. } catch (err) {}
  145. },
  146. async handleChooseLocation(e) {
  147. const { lat, lon, formkey } = e.currentTarget.dataset
  148. this.tempPostData = lat && lon ? {
  149. latitude: lat,
  150. longitude: lon
  151. } : {}
  152. this.tempFormKey = 'form.' + formkey
  153. await this.handleGetSetting()
  154. },
  155. async chooseLocationBridge() {
  156. if (!this.tempFormKey) {
  157. return
  158. }
  159. try {
  160. const { errMsg, address, latitude, longitude, name } = await wx.chooseLocation(this.tempPostData)
  161. if (errMsg === 'chooseLocation:ok' && address && name) {
  162. const pointInfo = {
  163. address_name: address,
  164. name,
  165. latitude,
  166. longitude
  167. }
  168. this.setData({
  169. [this.tempFormKey]: pointInfo
  170. })
  171. }
  172. } catch (err) {
  173. // 点击了取消按钮
  174. if (err.errMsg === 'chooseLocation:fail cancel') {
  175. }
  176. // 未开启授权
  177. if (err.errMsg === 'chooseLocation:fail auth deny') {
  178. }
  179. }
  180. },
  181. getForm() {
  182. const {
  183. user_name,
  184. shop_name,
  185. user_card,
  186. shop_phone,
  187. country_msg,
  188. user_code_url,
  189. user_wechat_code,
  190. shop_img_url,
  191. shop_address,
  192. shop_remark
  193. } = this.data.form
  194. return {
  195. user_name,
  196. shop_name,
  197. user_card,
  198. shop_phone,
  199. country_msg,
  200. user_code_url: user_code_url.map(item => item.url).join(''),
  201. user_wechat_code,
  202. shop_img_url: shop_img_url.map(item => item.url).join(''),
  203. shop_address,
  204. shop_remark
  205. }
  206. },
  207. verify() {
  208. let errorList = []
  209. const {
  210. user_name,
  211. shop_name,
  212. user_card,
  213. shop_phone,
  214. country_msg,
  215. user_code_url,
  216. user_wechat_code,
  217. shop_img_url,
  218. shop_address,
  219. shop_remark
  220. } = this.getForm()
  221. if (!user_name) {
  222. errorList.push('请输入姓名')
  223. }
  224. if (!user_card) {
  225. errorList.push('请输入身份证号码')
  226. }
  227. if (!isMobile(shop_phone)) {
  228. errorList.push('请输入手机号')
  229. }
  230. if (!country_msg) {
  231. errorList.push('请输入村信息')
  232. }
  233. if (!user_code_url) {
  234. errorList.push('请上传个人二维码')
  235. }
  236. if (!user_wechat_code) {
  237. errorList.push('请输入微信号')
  238. }
  239. if (!shop_name) {
  240. errorList.push('请输入店铺名称')
  241. }
  242. if (!shop_img_url) {
  243. errorList.push('请上传店铺图片')
  244. }
  245. if (!shop_address.address_name) {
  246. errorList.push('请选择详细地址')
  247. }
  248. if (!shop_remark) {
  249. errorList.push('请输入备注')
  250. }
  251. return errorList
  252. },
  253. async onSubmit() {
  254. const temp = this.getForm()
  255. const verifyList = this.verify()
  256. if (verifyList.length) {
  257. wx.showToast({
  258. title: verifyList[0],
  259. icon: 'none'
  260. })
  261. return
  262. }
  263. this.setData({
  264. booLock: true
  265. })
  266. try {
  267. const { status, data, msg } = await userApplyShop(temp)
  268. if (status) {
  269. wx.switchTab({
  270. url: '/pages/mine/mine'
  271. })
  272. } else {
  273. wx.showToast({
  274. title: msg,
  275. icon: 'none'
  276. })
  277. }
  278. } catch (err) {}
  279. this.setData({
  280. booLock: false
  281. })
  282. }
  283. })