businessApply.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 === 'shop_address') {
  97. value = JSON.parse(value)
  98. }
  99. if (this.data.form.hasOwnProperty(key)) {
  100. temp[`form.${key}`] = value
  101. }
  102. }
  103. }
  104. this.setData(temp)
  105. } else {
  106. wx.showToast({
  107. title: msg,
  108. icon: 'none'
  109. })
  110. }
  111. } catch (err) {}
  112. },
  113. ...uploadJS,
  114. uploadCallBack(res) {
  115. const temp = res.map(item => {
  116. return {
  117. 'url': item.url,
  118. 'formkey': item.formkey
  119. }
  120. })
  121. let tempForm = {}
  122. let formkey = ''
  123. if (temp.length > 0) {
  124. formkey = temp[0].formkey
  125. }
  126. switch (formkey) {
  127. case 'user_code_url':
  128. case 'shop_img_url':
  129. tempForm[`form.${formkey}[0]`] = temp[0]
  130. break
  131. default:
  132. }
  133. if (Object.keys(tempForm).length > 0) {
  134. this.setData(tempForm)
  135. }
  136. },
  137. setFormValue(event) {
  138. const { value } = event.detail
  139. const { formkey } = event.target.dataset
  140. let tempForm = {}
  141. switch (formkey) {
  142. case 'user_name':
  143. case 'shop_name':
  144. case 'user_card':
  145. case 'country_msg':
  146. case 'user_wechat_code':
  147. case 'shop_remark':
  148. tempForm[`form.${formkey}`] = value
  149. break
  150. case 'shop_phone':
  151. tempForm[`form.${formkey}`] = value.replace(/[^\d]$/, '').replace(/^0/, '').replace(/(\d{11}(.*))/, '$1')
  152. break
  153. default:
  154. }
  155. this.setData(tempForm)
  156. },
  157. // 获取定位权限、定位权限授权
  158. async handleGetSetting() {
  159. const that = this
  160. try {
  161. const { errMsg, authSetting } = await wx.getSetting()
  162. if (errMsg === 'getSetting:ok') {
  163. // 有定位授权
  164. if (authSetting['scope.userLocation']) {
  165. if (that.tempFormKey === '') {
  166. } else {
  167. await that.chooseLocationBridge()
  168. }
  169. return
  170. }
  171. }
  172. } catch (err) {}
  173. try {
  174. await wx.authorize({ scope: 'scope.userLocation' })
  175. if (that.tempFormKey === '') {
  176. } else {
  177. await that.chooseLocationBridge()
  178. }
  179. } catch (err) {
  180. wx.showModal({
  181. title: '提示',
  182. content: '未开启获取地理位置权限,去设置中打开',
  183. success(res) {
  184. if (res.confirm) {
  185. that.openSetting()
  186. }
  187. }
  188. })
  189. }
  190. },
  191. // 去小程序自带设置页:返回
  192. async openSetting() {
  193. try {
  194. const openSettingData = await wx.openSetting()
  195. // 开启了定位权限
  196. if (openSettingData.authSetting['scope.userLocation']) {
  197. if (this.tempFormKey === '') {
  198. } else {
  199. await this.chooseLocationBridge()
  200. }
  201. }
  202. } catch (err) {}
  203. },
  204. async handleChooseLocation(e) {
  205. const { lat, lon, formkey } = e.currentTarget.dataset
  206. this.tempPostData = lat && lon ? {
  207. latitude: lat,
  208. longitude: lon
  209. } : {}
  210. this.tempFormKey = 'form.' + formkey
  211. await this.handleGetSetting()
  212. },
  213. async chooseLocationBridge() {
  214. if (!this.tempFormKey) {
  215. return
  216. }
  217. try {
  218. const { errMsg, address, latitude, longitude, name } = await wx.chooseLocation(this.tempPostData)
  219. if (errMsg === 'chooseLocation:ok' && address && name) {
  220. const pointInfo = {
  221. address_name: address,
  222. name,
  223. latitude,
  224. longitude
  225. }
  226. this.setData({
  227. [this.tempFormKey]: pointInfo
  228. })
  229. }
  230. } catch (err) {
  231. // 点击了取消按钮
  232. if (err.errMsg === 'chooseLocation:fail cancel') {
  233. }
  234. // 未开启授权
  235. if (err.errMsg === 'chooseLocation:fail auth deny') {
  236. }
  237. }
  238. },
  239. getForm() {
  240. const {
  241. user_name,
  242. shop_name,
  243. user_card,
  244. shop_phone,
  245. country_msg,
  246. user_code_url,
  247. user_wechat_code,
  248. shop_img_url,
  249. shop_address,
  250. shop_remark
  251. } = this.data.form
  252. return {
  253. user_name,
  254. shop_name,
  255. user_card,
  256. shop_phone,
  257. country_msg,
  258. user_code_url: user_code_url.map(item => item.url).join(''),
  259. user_wechat_code,
  260. shop_img_url: shop_img_url.map(item => item.url).join(''),
  261. shop_address,
  262. shop_remark
  263. }
  264. },
  265. verify() {
  266. let errorList = []
  267. const {
  268. user_name,
  269. user_card,
  270. shop_phone,
  271. country_msg,
  272. shop_address
  273. } = this.getForm()
  274. if (!user_name) {
  275. errorList.push('请输入姓名')
  276. }
  277. if (!user_card) {
  278. errorList.push('请输入身份证号码')
  279. }
  280. if (!isMobile(shop_phone)) {
  281. errorList.push('请输入手机号')
  282. }
  283. if (!country_msg) {
  284. errorList.push('请输入村信息')
  285. }
  286. if (!shop_address.address_name) {
  287. errorList.push('请选择详细地址')
  288. }
  289. return errorList
  290. },
  291. async onSubmit() {
  292. const temp = this.getForm()
  293. const verifyList = this.verify()
  294. if (verifyList.length) {
  295. wx.showToast({
  296. title: verifyList[0],
  297. icon: 'none'
  298. })
  299. return
  300. }
  301. this.setData({
  302. booLock: true
  303. })
  304. try {
  305. const { status, data, msg } = await userApplyShop(temp)
  306. if (status) {
  307. wx.switchTab({
  308. url: '/pages/mine/mine'
  309. })
  310. } else {
  311. wx.showToast({
  312. title: msg,
  313. icon: 'none'
  314. })
  315. }
  316. } catch (err) {}
  317. this.setData({
  318. booLock: false
  319. })
  320. },
  321. setCountryMsgList() {
  322. const { country_msg_list } = app.globalData.objSystemConfig
  323. this.setData({
  324. countryMsgList: Array.isArray(country_msg_list) && country_msg_list.length > 0 ? country_msg_list : []
  325. })
  326. },
  327. showCountryMsg() {
  328. this.setData({
  329. booCountryMsg: true
  330. })
  331. },
  332. hideCountryMsg() {
  333. if (this.data.countryMsgList.length > 0) {
  334. this.selectComponent('#picker-country-msg').setIndexes([this.data.countryMsgInDefaultIndex])
  335. }
  336. this.setData({
  337. booCountryMsg: false
  338. })
  339. },
  340. confirmCountryMsg(event) {
  341. const { value, index } = event.detail
  342. this.setData({
  343. 'form.country_msg': value,
  344. countryMsgInDefaultIndex: index
  345. })
  346. this.hideCountryMsg()
  347. }
  348. })