businessApply.js 6.6 KB

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