businessApply.js 7.5 KB

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