businessApply.js 8.6 KB

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