businessInfo.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. console.log(shop_info)
  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. } else {
  127. wx.showToast({
  128. title: msg,
  129. icon: 'none'
  130. })
  131. }
  132. } catch (err) {}
  133. },
  134. ...uploadJS,
  135. uploadCallBack(res) {
  136. const temp = res.map(item => {
  137. return {
  138. 'url': item.url,
  139. 'formkey': item.formkey
  140. }
  141. })
  142. let tempForm = {}
  143. let formkey = ''
  144. if (temp.length > 0) {
  145. formkey = temp[0].formkey
  146. }
  147. switch (formkey) {
  148. case 'user_code_url':
  149. case 'shop_img_url':
  150. case 'shop_shangbiao':
  151. tempForm[`form.${formkey}[0]`] = temp[0]
  152. break
  153. default:
  154. }
  155. if (Object.keys(tempForm).length > 0) {
  156. this.setData(tempForm)
  157. }
  158. },
  159. setFormValue(event) {
  160. const { value } = event.detail
  161. const { formkey } = event.target.dataset
  162. let tempForm = {}
  163. switch (formkey) {
  164. case 'user_name':
  165. case 'shop_name':
  166. case 'user_card':
  167. case 'user_wechat_code':
  168. case 'company_name':
  169. case 'company_hangye':
  170. case 'company_jingying_fanwei':
  171. case 'shop_jidi_guimo':
  172. case 'shop_product_time':
  173. case 'shop_remark':
  174. tempForm[`form.${formkey}`] = value
  175. break
  176. case 'shop_phone':
  177. tempForm[`form.${formkey}`] = value.replace(/[^\d]$/, '').replace(/^0/, '').replace(/(\d{11}(.*))/, '$1')
  178. break
  179. default:
  180. }
  181. this.setData(tempForm)
  182. },
  183. // 获取定位权限、定位权限授权
  184. async handleGetSetting() {
  185. const that = this
  186. try {
  187. const { errMsg, authSetting } = await wx.getSetting()
  188. if (errMsg === 'getSetting:ok') {
  189. // 有定位授权
  190. if (authSetting['scope.userLocation']) {
  191. if (that.tempFormKey === '') {
  192. } else {
  193. await that.chooseLocationBridge()
  194. }
  195. return
  196. }
  197. }
  198. } catch (err) {}
  199. try {
  200. await wx.authorize({ scope: 'scope.userLocation' })
  201. if (that.tempFormKey === '') {
  202. } else {
  203. await that.chooseLocationBridge()
  204. }
  205. } catch (err) {
  206. wx.showModal({
  207. title: '提示',
  208. content: '未开启获取地理位置权限,去设置中打开',
  209. success(res) {
  210. if (res.confirm) {
  211. that.openSetting()
  212. }
  213. }
  214. })
  215. }
  216. },
  217. // 去小程序自带设置页:返回
  218. async openSetting() {
  219. try {
  220. const openSettingData = await wx.openSetting()
  221. // 开启了定位权限
  222. if (openSettingData.authSetting['scope.userLocation']) {
  223. if (this.tempFormKey === '') {
  224. } else {
  225. await this.chooseLocationBridge()
  226. }
  227. }
  228. } catch (err) {}
  229. },
  230. async handleChooseLocation(e) {
  231. const { lat, lon, formkey } = e.currentTarget.dataset
  232. this.tempPostData = lat && lon ? {
  233. latitude: lat,
  234. longitude: lon
  235. } : {}
  236. this.tempFormKey = 'form.' + formkey
  237. await this.handleGetSetting()
  238. },
  239. async chooseLocationBridge() {
  240. if (!this.tempFormKey) {
  241. return
  242. }
  243. try {
  244. const { errMsg, address, latitude, longitude, name } = await wx.chooseLocation(this.tempPostData)
  245. if (errMsg === 'chooseLocation:ok' && address && name) {
  246. const pointInfo = {
  247. address_name: address,
  248. name,
  249. latitude,
  250. longitude
  251. }
  252. this.setData({
  253. [this.tempFormKey]: pointInfo
  254. })
  255. }
  256. } catch (err) {
  257. // 点击了取消按钮
  258. if (err.errMsg === 'chooseLocation:fail cancel') {
  259. }
  260. // 未开启授权
  261. if (err.errMsg === 'chooseLocation:fail auth deny') {
  262. }
  263. }
  264. },
  265. getForm() {
  266. const {
  267. shop_name,
  268. shop_img_url,
  269. shop_address,
  270. user_name,
  271. user_card,
  272. shop_phone,
  273. country_msg,
  274. user_wechat_code,
  275. user_code_url,
  276. company_name,
  277. company_created_at,
  278. company_hangye,
  279. company_jingying_fanwei,
  280. shop_jidi_guimo,
  281. shop_product_time,
  282. shop_shangbiao,
  283. shop_remark
  284. } = this.data.form
  285. return {
  286. user_name,
  287. shop_name,
  288. user_card,
  289. shop_phone,
  290. country_msg,
  291. user_code_url: user_code_url.map(item => item.url).join(''),
  292. user_wechat_code,
  293. shop_img_url: shop_img_url.map(item => item.url).join(''),
  294. shop_address,
  295. shop_remark,
  296. company_name,
  297. company_created_at,
  298. company_hangye,
  299. company_jingying_fanwei,
  300. shop_jidi_guimo,
  301. shop_product_time,
  302. shop_shangbiao: shop_shangbiao.map(item => item.url).join('')
  303. }
  304. },
  305. verify() {
  306. let errorList = []
  307. const {
  308. user_name,
  309. user_card,
  310. shop_phone,
  311. country_msg
  312. } = this.getForm()
  313. if (!user_name) {
  314. errorList.push('请输入姓名')
  315. }
  316. if (!user_card) {
  317. errorList.push('请输入身份证号码')
  318. }
  319. if (!isMobile(shop_phone)) {
  320. errorList.push('请输入手机号')
  321. }
  322. if (!country_msg) {
  323. errorList.push('请选择村信息')
  324. }
  325. return errorList
  326. },
  327. async onSubmit() {
  328. const temp = this.getForm()
  329. const verifyList = this.verify()
  330. if (verifyList.length) {
  331. wx.showToast({
  332. title: verifyList[0],
  333. icon: 'none'
  334. })
  335. return
  336. }
  337. this.setData({
  338. booLock: true
  339. })
  340. try {
  341. const { status, msg } = await modifyShopInfo(temp)
  342. if (status) {
  343. wx.redirectTo({
  344. url: '/pages/businessHome/businessHome'
  345. })
  346. } else {
  347. console.log(msg)
  348. // wx.showToast({
  349. // title: msg,
  350. // icon: 'none'
  351. // })
  352. }
  353. } catch (err) {}
  354. this.setData({
  355. booLock: false
  356. })
  357. },
  358. setCountryMsgList() {
  359. const { country_msg_list } = app.globalData.objSystemConfig
  360. this.setData({
  361. countryMsgList: Array.isArray(country_msg_list) && country_msg_list.length > 0 ? country_msg_list : []
  362. })
  363. },
  364. showCountryMsg() {
  365. this.setData({
  366. booCountryMsg: true
  367. })
  368. },
  369. hideCountryMsg() {
  370. if (this.data.countryMsgList.length > 0) {
  371. this.selectComponent('#picker-country-msg').setIndexes([this.data.countryMsgInDefaultIndex])
  372. }
  373. this.setData({
  374. booCountryMsg: false
  375. })
  376. },
  377. confirmCountryMsg(event) {
  378. const { value, index } = event.detail
  379. this.setData({
  380. 'form.country_msg': value,
  381. countryMsgInDefaultIndex: index
  382. })
  383. this.hideCountryMsg()
  384. },
  385. showCompanyCreatedAt() {
  386. this.setData({
  387. booCompanyCreatedAt: true
  388. })
  389. },
  390. hideCompanyCreatedAt() {
  391. this.setData({
  392. booCompanyCreatedAt: false
  393. })
  394. },
  395. inputCompanyCreatedAt(event) {
  396. this.setData({
  397. currentDate: event.detail
  398. })
  399. },
  400. confirmCompanyCreatedAt() {
  401. this.setData({
  402. 'form.company_created_at': this.data.currentDate
  403. })
  404. this.hideCompanyCreatedAt()
  405. }
  406. })