const uploadJS = require('../../mixin/upload.js') const { isMobile } = require('../../utils/validate') const { formatTs, getTs } = require('../../utils/util') const { getShopInfo } = require('../businessHome/api/index') const { modifyShopInfo } = require('./api/index') const { YYYY, MM, DD } = formatTs((new Date()).getTime()) const app = getApp() Page({ /** * 页面的初始数据 */ data: { form: { 'shop_name': '', // 店铺名称 'shop_img_url': [], // 店铺图片地址 'shop_address': { 'longitude': 0, // 经度 'latitude': 0, // 纬度 'address_name': '', // 起点位置名称 'name': '' }, // 店铺地址 'user_name': '', // 必填 用户姓名 'user_card': '', // 必填 身份证 'shop_phone': '', // 必填 手机号码 'country_msg': '', // 必填 乡村信息 'user_wechat_code': '', // 微信号 'user_code_url': [], // 微信二维码 'company_name': '', // 企业名称 'company_created_at': '', // 企业成立日期 'company_hangye': '', // 所属行业 'company_jingying_fanwei': '', // 经营范围 'shop_jidi_guimo': '', // 基地规模 'shop_product_time': '', // 商品上架时间 'shop_shangbiao': [], // 注册商标 'shop_remark': '' // 备注 }, booLock: false, userInfo: {}, booCountryMsg: false, countryMsgList: [], countryMsgInDefaultIndex: 0, booCompanyCreatedAt: false, minDate: new Date(1900, 0, 1).getTime(), maxDate: new Date(YYYY, MM * 1 - 1, DD * 1).getTime(), currentDate: new Date().getTime() }, tempFormKey: '', tempPostData: {}, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.bindCallBack() app.fetchUserDataCallback = () => { this.bindCallBack() } app.fetchSystemConfigCallback = () => { this.setCountryMsgList() } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.setCountryMsgList() }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, bindCallBack() { this.setData({ userInfo: app.globalData.userInfo }, () => { this.fetchShopDetail() }) }, async fetchShopDetail() { let temp = {} try { const { status, data, msg } = await getShopInfo() if (status) { const { shop_info } = data if (Object.prototype.toString.call(shop_info) === '[object Object]') { for (let key in shop_info) { let value = shop_info[key] if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') { if (key === 'user_code_url' || key === 'shop_img_url' || key === 'shop_shangbiao') { value = [ { 'formkey': key, 'url': value } ] } if (key === 'country_msg') { const index = this.data.countryMsgList.findIndex(item => item === value) if (index > -1) { temp['countryMsgInDefaultIndex'] = index } } if (key === 'shop_address') { value = JSON.parse(value) } if (key === 'company_created_at') { value = getTs(value, 'YYYY-MM-DD') temp['currentDate'] = value } if (this.data.form.hasOwnProperty(key)) { temp[`form.${key}`] = value } } } this.setData(temp) } } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (err) {} }, ...uploadJS, uploadCallBack(res) { const temp = res.map(item => { return { 'url': item.url, 'formkey': item.formkey } }) let tempForm = {} let formkey = '' if (temp.length > 0) { formkey = temp[0].formkey } switch (formkey) { case 'user_code_url': case 'shop_img_url': case 'shop_shangbiao': tempForm[`form.${formkey}[0]`] = temp[0] break default: } if (Object.keys(tempForm).length > 0) { this.setData(tempForm) } }, setFormValue(event) { const { value } = event.detail const { formkey } = event.target.dataset let tempForm = {} switch (formkey) { case 'user_name': case 'shop_name': case 'user_card': case 'user_wechat_code': case 'company_name': case 'company_hangye': case 'company_jingying_fanwei': case 'shop_jidi_guimo': case 'shop_product_time': case 'shop_remark': tempForm[`form.${formkey}`] = value break case 'shop_phone': tempForm[`form.${formkey}`] = value.replace(/[^\d]$/, '').replace(/^0/, '').replace(/(\d{11}(.*))/, '$1') break default: } this.setData(tempForm) }, // 获取定位权限、定位权限授权 async handleGetSetting() { const that = this try { const { errMsg, authSetting } = await wx.getSetting() if (errMsg === 'getSetting:ok') { // 有定位授权 if (authSetting['scope.userLocation']) { if (that.tempFormKey === '') { } else { await that.chooseLocationBridge() } return } } } catch (err) {} try { await wx.authorize({ scope: 'scope.userLocation' }) if (that.tempFormKey === '') { } else { await that.chooseLocationBridge() } } catch (err) { wx.showModal({ title: '提示', content: '未开启获取地理位置权限,去设置中打开', success(res) { if (res.confirm) { that.openSetting() } } }) } }, // 去小程序自带设置页:返回 async openSetting() { try { const openSettingData = await wx.openSetting() // 开启了定位权限 if (openSettingData.authSetting['scope.userLocation']) { if (this.tempFormKey === '') { } else { await this.chooseLocationBridge() } } } catch (err) {} }, async handleChooseLocation(e) { const { lat, lon, formkey } = e.currentTarget.dataset this.tempPostData = lat && lon ? { latitude: lat, longitude: lon } : {} this.tempFormKey = 'form.' + formkey await this.handleGetSetting() }, async chooseLocationBridge() { if (!this.tempFormKey) { return } try { const { errMsg, address, latitude, longitude, name } = await wx.chooseLocation(this.tempPostData) if (errMsg === 'chooseLocation:ok' && address && name) { const pointInfo = { address_name: address, name, latitude, longitude } this.setData({ [this.tempFormKey]: pointInfo }) } } catch (err) { // 点击了取消按钮 if (err.errMsg === 'chooseLocation:fail cancel') { } // 未开启授权 if (err.errMsg === 'chooseLocation:fail auth deny') { } } }, getForm() { let _company_created_at = '' const { shop_name, shop_img_url, shop_address, user_name, user_card, shop_phone, country_msg, user_wechat_code, user_code_url, company_name, company_created_at, company_hangye, company_jingying_fanwei, shop_jidi_guimo, shop_product_time, shop_shangbiao, shop_remark } = this.data.form if (company_created_at > 0) { const { YYYY, MM, DD } = formatTs(company_created_at) _company_created_at = `${YYYY}-${MM}-${DD}` } return { user_name, shop_name, user_card, shop_phone, country_msg, user_code_url: user_code_url.map(item => item.url).join(''), user_wechat_code, shop_img_url: shop_img_url.map(item => item.url).join(''), shop_address, shop_remark, company_name, company_created_at: _company_created_at, company_hangye, company_jingying_fanwei, shop_jidi_guimo, shop_product_time, shop_shangbiao: shop_shangbiao.map(item => item.url).join('') } }, verify() { let errorList = [] const { user_name, user_card, shop_phone, country_msg } = this.getForm() if (!user_name) { errorList.push('请输入姓名') } if (!user_card) { errorList.push('请输入身份证号码') } if (!isMobile(shop_phone)) { errorList.push('请输入手机号') } if (!country_msg) { errorList.push('请选择村信息') } return errorList }, async onSubmit() { const temp = this.getForm() const verifyList = this.verify() if (verifyList.length) { wx.showToast({ title: verifyList[0], icon: 'none' }) return } this.setData({ booLock: true }) try { const { status, msg } = await modifyShopInfo(temp) if (status) { wx.redirectTo({ url: '/pages/businessHome/businessHome' }) } else { console.log(msg) // wx.showToast({ // title: msg, // icon: 'none' // }) } } catch (err) {} this.setData({ booLock: false }) }, setCountryMsgList() { const { country_msg_list } = app.globalData.objSystemConfig this.setData({ countryMsgList: Array.isArray(country_msg_list) && country_msg_list.length > 0 ? country_msg_list : [] }) }, showCountryMsg() { this.setData({ booCountryMsg: true }) }, hideCountryMsg() { if (this.data.countryMsgList.length > 0) { this.selectComponent('#picker-country-msg').setIndexes([this.data.countryMsgInDefaultIndex]) } this.setData({ booCountryMsg: false }) }, confirmCountryMsg(event) { const { value, index } = event.detail this.setData({ 'form.country_msg': value, countryMsgInDefaultIndex: index }) this.hideCountryMsg() }, showCompanyCreatedAt() { this.setData({ booCompanyCreatedAt: true }) }, hideCompanyCreatedAt() { this.setData({ booCompanyCreatedAt: false }) }, confirmCompanyCreatedAt(event) { const value = event.detail this.setData({ currentDate: value, 'form.company_created_at': value }) this.hideCompanyCreatedAt() } })