businessApply.js 8.0 KB

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