businessInfo.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. const uploadJS = require('../../mixin/upload.js')
  2. const { isMobile } = require('../../utils/validate')
  3. const { formatTs, getTs } = require('../../utils/util')
  4. const { getShopInfo } = require('../businessHome/api/index')
  5. const { modifyShopInfo } = require('./api/index')
  6. const { YYYY, MM, DD } = formatTs((new Date()).getTime())
  7. const app = getApp()
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. form: {
  14. 'shop_name': '', // 店铺名称
  15. 'shop_img_url': [], // 店铺图片地址
  16. 'shop_address': {
  17. 'longitude': 0, // 经度
  18. 'latitude': 0, // 纬度
  19. 'address_name': '', // 起点位置名称
  20. 'name': ''
  21. }, // 店铺地址
  22. 'user_name': '', // 必填 用户姓名
  23. 'user_card': '', // 必填 身份证
  24. 'shop_phone': '', // 必填 手机号码
  25. 'country_msg': '', // 必填 乡村信息
  26. 'user_wechat_code': '', // 微信号
  27. 'user_code_url': [], // 微信二维码
  28. 'company_name': '', // 企业名称
  29. 'company_created_at': '', // 企业成立日期
  30. 'company_hangye': '', // 所属行业
  31. 'company_jingying_fanwei': '', // 经营范围
  32. 'shop_jidi_guimo': '', // 基地规模
  33. 'shop_product_time': '', // 商品上架时间
  34. 'shop_shangbiao': [], // 注册商标
  35. 'shop_remark': '' // 备注
  36. },
  37. booLock: false,
  38. userInfo: {},
  39. booCountryMsg: false,
  40. countryMsgList: [],
  41. countryMsgInDefaultIndex: 0,
  42. booCompanyCreatedAt: false,
  43. minDate: new Date(1900, 0, 1).getTime(),
  44. maxDate: new Date(YYYY, MM * 1 - 1, DD * 1).getTime(),
  45. currentDate: new Date().getTime()
  46. },
  47. tempFormKey: '',
  48. tempPostData: {},
  49. /**
  50. * 生命周期函数--监听页面加载
  51. */
  52. onLoad(options) {
  53. this.bindCallBack()
  54. app.fetchUserDataCallback = () => {
  55. this.bindCallBack()
  56. }
  57. app.fetchSystemConfigCallback = () => {
  58. this.setCountryMsgList()
  59. }
  60. },
  61. /**
  62. * 生命周期函数--监听页面初次渲染完成
  63. */
  64. onReady() {
  65. },
  66. /**
  67. * 生命周期函数--监听页面显示
  68. */
  69. onShow() {
  70. this.setCountryMsgList()
  71. },
  72. /**
  73. * 生命周期函数--监听页面隐藏
  74. */
  75. onHide() {
  76. },
  77. /**
  78. * 生命周期函数--监听页面卸载
  79. */
  80. onUnload() {
  81. },
  82. bindCallBack() {
  83. this.setData({
  84. userInfo: app.globalData.userInfo
  85. }, () => {
  86. this.fetchShopDetail()
  87. })
  88. },
  89. async fetchShopDetail() {
  90. let temp = {}
  91. try {
  92. const { status, data, msg } = await getShopInfo()
  93. if (status) {
  94. const { shop_info } = data
  95. if (Object.prototype.toString.call(shop_info) === '[object Object]') {
  96. for (let key in shop_info) {
  97. let value = shop_info[key]
  98. if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') {
  99. if (key === 'user_code_url' || key === 'shop_img_url' || key === 'shop_shangbiao') {
  100. value = [
  101. {
  102. 'formkey': key,
  103. 'url': value
  104. }
  105. ]
  106. }
  107. if (key === 'country_msg') {
  108. const index = this.data.countryMsgList.findIndex(item => item === value)
  109. if (index > -1) {
  110. temp['countryMsgInDefaultIndex'] = index
  111. }
  112. }
  113. if (key === 'shop_address') {
  114. value = JSON.parse(value)
  115. }
  116. if (key === 'company_created_at') {
  117. value = getTs(value, 'YYYY-MM-DD')
  118. temp['currentDate'] = value
  119. }
  120. if (this.data.form.hasOwnProperty(key)) {
  121. temp[`form.${key}`] = value
  122. }
  123. }
  124. }
  125. this.setData(temp)
  126. }
  127. } else {
  128. wx.showToast({
  129. title: msg,
  130. icon: 'none'
  131. })
  132. }
  133. } catch (err) {}
  134. },
  135. ...uploadJS,
  136. uploadCallBack(res) {
  137. const temp = res.map(item => {
  138. return {
  139. 'url': item.url,
  140. 'formkey': item.formkey
  141. }
  142. })
  143. let tempForm = {}
  144. let formkey = ''
  145. if (temp.length > 0) {
  146. formkey = temp[0].formkey
  147. }
  148. switch (formkey) {
  149. case 'user_code_url':
  150. case 'shop_img_url':
  151. case 'shop_shangbiao':
  152. tempForm[`form.${formkey}[0]`] = temp[0]
  153. break
  154. default:
  155. }
  156. if (Object.keys(tempForm).length > 0) {
  157. this.setData(tempForm)
  158. }
  159. },
  160. setFormValue(event) {
  161. const { value } = event.detail
  162. const { formkey } = event.target.dataset
  163. let tempForm = {}
  164. switch (formkey) {
  165. case 'user_name':
  166. case 'shop_name':
  167. case 'user_card':
  168. case 'user_wechat_code':
  169. case 'company_name':
  170. case 'company_hangye':
  171. case 'company_jingying_fanwei':
  172. case 'shop_jidi_guimo':
  173. case 'shop_product_time':
  174. case 'shop_remark':
  175. tempForm[`form.${formkey}`] = value
  176. break
  177. case 'shop_phone':
  178. tempForm[`form.${formkey}`] = value.replace(/[^\d]$/, '').replace(/^0/, '').replace(/(\d{11}(.*))/, '$1')
  179. break
  180. default:
  181. }
  182. this.setData(tempForm)
  183. },
  184. // 获取定位权限、定位权限授权
  185. async handleGetSetting() {
  186. const that = this
  187. try {
  188. const { errMsg, authSetting } = await wx.getSetting()
  189. if (errMsg === 'getSetting:ok') {
  190. // 有定位授权
  191. if (authSetting['scope.userLocation']) {
  192. if (that.tempFormKey === '') {
  193. } else {
  194. await that.chooseLocationBridge()
  195. }
  196. return
  197. }
  198. }
  199. } catch (err) {}
  200. try {
  201. await wx.authorize({ scope: 'scope.userLocation' })
  202. if (that.tempFormKey === '') {
  203. } else {
  204. await that.chooseLocationBridge()
  205. }
  206. } catch (err) {
  207. wx.showModal({
  208. title: '提示',
  209. content: '未开启获取地理位置权限,去设置中打开',
  210. success(res) {
  211. if (res.confirm) {
  212. that.openSetting()
  213. }
  214. }
  215. })
  216. }
  217. },
  218. // 去小程序自带设置页:返回
  219. async openSetting() {
  220. try {
  221. const openSettingData = await wx.openSetting()
  222. // 开启了定位权限
  223. if (openSettingData.authSetting['scope.userLocation']) {
  224. if (this.tempFormKey === '') {
  225. } else {
  226. await this.chooseLocationBridge()
  227. }
  228. }
  229. } catch (err) {}
  230. },
  231. async handleChooseLocation(e) {
  232. const { lat, lon, formkey } = e.currentTarget.dataset
  233. this.tempPostData = lat && lon ? {
  234. latitude: lat,
  235. longitude: lon
  236. } : {}
  237. this.tempFormKey = 'form.' + formkey
  238. await this.handleGetSetting()
  239. },
  240. async chooseLocationBridge() {
  241. if (!this.tempFormKey) {
  242. return
  243. }
  244. try {
  245. const { errMsg, address, latitude, longitude, name } = await wx.chooseLocation(this.tempPostData)
  246. if (errMsg === 'chooseLocation:ok' && address && name) {
  247. const pointInfo = {
  248. address_name: address,
  249. name,
  250. latitude,
  251. longitude
  252. }
  253. this.setData({
  254. [this.tempFormKey]: pointInfo
  255. })
  256. }
  257. } catch (err) {
  258. // 点击了取消按钮
  259. if (err.errMsg === 'chooseLocation:fail cancel') {
  260. }
  261. // 未开启授权
  262. if (err.errMsg === 'chooseLocation:fail auth deny') {
  263. }
  264. }
  265. },
  266. getForm() {
  267. let _company_created_at = ''
  268. const {
  269. shop_name,
  270. shop_img_url,
  271. shop_address,
  272. user_name,
  273. user_card,
  274. shop_phone,
  275. country_msg,
  276. user_wechat_code,
  277. user_code_url,
  278. company_name,
  279. company_created_at,
  280. company_hangye,
  281. company_jingying_fanwei,
  282. shop_jidi_guimo,
  283. shop_product_time,
  284. shop_shangbiao,
  285. shop_remark
  286. } = this.data.form
  287. if (company_created_at > 0) {
  288. const { YYYY, MM, DD } = formatTs(company_created_at)
  289. _company_created_at = `${YYYY}-${MM}-${DD}`
  290. }
  291. return {
  292. user_name,
  293. shop_name,
  294. user_card,
  295. shop_phone,
  296. country_msg,
  297. user_code_url: user_code_url.map(item => item.url).join(''),
  298. user_wechat_code,
  299. shop_img_url: shop_img_url.map(item => item.url).join(''),
  300. shop_address,
  301. shop_remark,
  302. company_name,
  303. company_created_at: _company_created_at,
  304. company_hangye,
  305. company_jingying_fanwei,
  306. shop_jidi_guimo,
  307. shop_product_time,
  308. shop_shangbiao: shop_shangbiao.map(item => item.url).join('')
  309. }
  310. },
  311. verify() {
  312. let errorList = []
  313. const {
  314. user_name,
  315. user_card,
  316. shop_phone,
  317. country_msg
  318. } = this.getForm()
  319. if (!user_name) {
  320. errorList.push('请输入姓名')
  321. }
  322. if (!user_card) {
  323. errorList.push('请输入身份证号码')
  324. }
  325. if (!isMobile(shop_phone)) {
  326. errorList.push('请输入手机号')
  327. }
  328. if (!country_msg) {
  329. errorList.push('请选择村信息')
  330. }
  331. return errorList
  332. },
  333. async onSubmit() {
  334. const temp = this.getForm()
  335. const verifyList = this.verify()
  336. if (verifyList.length) {
  337. wx.showToast({
  338. title: verifyList[0],
  339. icon: 'none'
  340. })
  341. return
  342. }
  343. this.setData({
  344. booLock: true
  345. })
  346. try {
  347. const { status, msg } = await modifyShopInfo(temp)
  348. if (status) {
  349. wx.navigateBack({
  350. delta: 1
  351. })
  352. } else {
  353. console.log(msg)
  354. // wx.showToast({
  355. // title: msg,
  356. // icon: 'none'
  357. // })
  358. }
  359. } catch (err) {}
  360. this.setData({
  361. booLock: false
  362. })
  363. },
  364. setCountryMsgList() {
  365. const { country_msg_list } = app.globalData.objSystemConfig
  366. this.setData({
  367. countryMsgList: Array.isArray(country_msg_list) && country_msg_list.length > 0 ? country_msg_list : []
  368. })
  369. },
  370. showCountryMsg() {
  371. this.setData({
  372. booCountryMsg: true
  373. })
  374. },
  375. hideCountryMsg() {
  376. if (this.data.countryMsgList.length > 0) {
  377. this.selectComponent('#picker-country-msg').setIndexes([this.data.countryMsgInDefaultIndex])
  378. }
  379. this.setData({
  380. booCountryMsg: false
  381. })
  382. },
  383. confirmCountryMsg(event) {
  384. const { value, index } = event.detail
  385. this.setData({
  386. 'form.country_msg': value,
  387. countryMsgInDefaultIndex: index
  388. })
  389. this.hideCountryMsg()
  390. },
  391. showCompanyCreatedAt() {
  392. this.setData({
  393. booCompanyCreatedAt: true
  394. })
  395. },
  396. hideCompanyCreatedAt() {
  397. this.setData({
  398. booCompanyCreatedAt: false
  399. })
  400. },
  401. confirmCompanyCreatedAt(event) {
  402. const value = event.detail
  403. this.setData({
  404. currentDate: value,
  405. 'form.company_created_at': value
  406. })
  407. this.hideCompanyCreatedAt()
  408. }
  409. })