const uploadJS = require('../../mixin/upload.js') const { postAddProduct, postModifyProduct, getProductDetail } = require('./api/index') const { getProductCategoryList, getProductBrandList } = require('../../api/common') const { formatTs, yuan2Fen, fen2Yuan, getTs } = require('../../utils/util') const minDate = new Date().getTime() const objMinDate = formatTs(minDate) const app = getApp() Page({ /** * 页面的初始数据 */ data: { form: { 'product_img_url': [], // 商品主图 'product_rotation_img_list': [], // 商品轮播图 'product_detail_img_list': [], // 详情图 'product_title': '', // 商品标题 'product_desc': '', // 商品简介 'product_category_id': { 'category_name': '', 'id': '' }, // 分类ID 'product_brand_id': { 'brand_name': '', 'id': '' }, // 品牌ID 'product_spec': '', // 规格 'product_unit': '', // 单位 'product_all_price': '', // 批发价 'product_price': '', // 零售价 'product_count': '', // 库存 'product_sale_at': '现货' // 预售时间(0代表预售) }, product_img_url_max: 1, product_rotation_img_list_max: 5, product_detail_img_list_max: 5, booCategory: false, categoryInDefaultIndex: 0, categoryList: [], booBrand: false, brandInDefaultIndex: 0, brandList: [], booUnit: false, unitInDefaultIndex: 0, unitInColumns: [], booSaleAtType: false, saleAtType: '0', booSaleAt: false, minDate: minDate, maxDate: new Date(objMinDate.YYYY + 5, 11, 31).getTime(), currentDate: new Date().getTime(), productStatus: [ { name: '下架', value: 0 }, { name: '上架', value: 1 } ], productCheckStatus: 0 // 0未审核 1审核成功 2审核失败 }, /** * 生命周期函数--监听页面加载 */ async onLoad(options) { const { product_id } = options await this.fetchProductCategoryList() await this.fetchProductBrandList() if (product_id) { this.setData({ 'form.id': product_id }, () => { this.fetchProductDetail() }) wx.setNavigationBarTitle({ title: '产品编辑' }) } else { wx.setNavigationBarTitle({ title: '产品新增' }) try { const response = await wx.getStorage({ key: app.globalData.businessGoodsEditsStorageKey }) if (response.errMsg === 'getStorage:ok' && /^{/.test(response.data)) { this.setForm(JSON.parse(response.data)) } } catch (err) {} } app.fetchSystemConfigCallback = () => { this.setUnitInColumns() } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.setUnitInColumns() }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, // 获取商品分类 async fetchProductCategoryList() { try { const { status, data, msg } = await getProductCategoryList() if (status) { this.setData({ categoryList: data.map(item => { return { ...item, text: item.category_name } }) }) } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (err) {} }, // 获取品牌 async fetchProductBrandList() { try { const { status, data, msg } = await getProductBrandList() if (status) { this.setData({ brandList: data.map(item => { return { ...item, text: item.brand_name } }) }) } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (err) {} }, async fetchProductDetail() { try { const { status, data, msg } = await getProductDetail(this.data.form.id) if (status) { this.setForm(data) } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (err) {} }, ...uploadJS, uploadCallBack(res) { const that = this 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 'product_img_url': tempForm[`form.${formkey}[0]`] = temp[0] break case 'product_rotation_img_list': case 'product_detail_img_list': const formkeyData = this.data.form[formkey] tempForm[`form.${formkey}`] = formkeyData.concat(...temp) break default: } if (Object.keys(tempForm).length > 0) { this.setData(tempForm, () => { that.updateBusinessGoodsEditsDataOfStorage(formkey, that.data.form[formkey].map(item => item.url)) }) } }, categoryShow() { this.setData({ booCategory: true }) }, categoryHide() { if (this.data.categoryList.length > 0) { this.selectComponent('#picker-category').setIndexes([this.data.categoryInDefaultIndex]) } this.setData({ booCategory: false }) }, categoryConfirm(event) { const { value, index } = event.detail this.setData({ 'form.product_category_id': value, categoryInDefaultIndex: index }, () => { this.updateBusinessGoodsEditsDataOfStorage('product_category_id', value.id) }) this.categoryHide() }, brandShow() { this.setData({ booBrand: true }) }, brandHide() { if (this.data.brandList.length > 0) { this.selectComponent('#picker-brand').setIndexes([this.data.brandInDefaultIndex]) } this.setData({ booBrand: false }) }, brandConfirm(event) { const { value, index } = event.detail this.setData({ 'form.product_brand_id': value, brandInDefaultIndex: index }, () => { this.updateBusinessGoodsEditsDataOfStorage('product_brand_id', value.id) }) this.brandHide() }, unitShow() { this.setData({ booUnit: true }) }, unitHide() { if (this.data.unitInColumns.length > 0) { this.selectComponent('#picker-unit').setIndexes([this.data.unitInDefaultIndex]) } this.setData({ booUnit: false }) }, unitConfirm(event) { const { value, index } = event.detail this.setData({ 'form.product_unit': value, unitInDefaultIndex: index }, () => { this.updateBusinessGoodsEditsDataOfStorage('product_unit', value) }) this.unitHide() }, setFormValue(event) { const { value } = event.detail const { formkey } = event.target.dataset let tempForm = {} switch (formkey) { case 'product_title': case 'product_desc': case 'product_spec': tempForm[`form.${formkey}`] = value break case 'product_all_price': case 'product_price': tempForm[`form.${formkey}`] = value.replace(/^0[0-9]*/, '0').replace(/^\D*(\d*(?:\.\d{0,2})?).*$/g, '$1') break case 'product_count': tempForm[`form.${formkey}`] = value.replace(/^0[0-9]*/, '0') break default: } this.setData(tempForm, () => { this.updateBusinessGoodsEditsDataOfStorage(formkey, this.data.form[formkey]) }) }, saleAtTypeShow() { this.setData({ booSaleAtType: true }) }, saleAtTypeHide() { let tempForm = {} tempForm['booSaleAtType'] = false if (this.data.saleAtType === '0') { tempForm['form.product_sale_at'] = '现货' } else { tempForm['form.product_sale_at'] = '' tempForm['booSaleAt'] = true } this.setData(tempForm, () => { this.updateBusinessGoodsEditsDataOfStorage('product_sale_at', 0) }) }, handleSaleAtType(e) { const { type } = e.currentTarget.dataset this.setData({ saleAtType: type }, () => { this.saleAtTypeHide() }) }, saleAtHide() { this.setData({ booSaleAt: false }) }, saleAtConfirm(event) { const { YYYY, MM, DD } = formatTs(event.detail) this.setData({ currentDate: event.detail, 'form.product_sale_at': `${YYYY}年${MM}月${DD}日` }, () => { this.updateBusinessGoodsEditsDataOfStorage('product_sale_at', getTs(this.data.form.product_sale_at, 'YYYY年MM月DD日', 's')) }) this.saleAtHide() }, getForm() { const { product_img_url, product_rotation_img_list, product_detail_img_list, product_title, product_desc, product_category_id, product_brand_id, product_spec, product_unit, product_all_price, product_price, product_count, product_sale_at } = this.data.form return { product_img_url: product_img_url.map(item => item.url).join(''), product_rotation_img_list: product_rotation_img_list.map(item => item.url), product_detail_img_list: product_detail_img_list.map(item => item.url), product_title, product_desc, product_category_id: product_category_id.id, product_brand_id: product_brand_id.id, product_spec, product_unit, product_all_price: yuan2Fen(product_all_price), product_price: yuan2Fen(product_price), product_count, product_sale_at: product_sale_at === '现货' ? 0 : (product_sale_at ? getTs(product_sale_at, 'YYYY年MM月DD日', 's') : '') } }, verify() { let errorList = [] const { product_img_url, product_rotation_img_list, product_detail_img_list, product_title, product_desc, product_category_id, product_brand_id, product_spec, product_unit, product_count, product_sale_at } = this.getForm() if (!product_img_url) { errorList.push('请上传商品主图') } if (!product_rotation_img_list) { errorList.push('请上传商品轮播图') } if (!product_detail_img_list) { errorList.push('请上传商品详情图') } if (!product_title) { errorList.push('请输入商品标题') } if (!product_desc) { errorList.push('请输入商品简介') } if (!product_category_id) { errorList.push('请选择商品分类') } if (!product_brand_id) { errorList.push('请选择商品品牌') } if (!product_spec) { errorList.push('请输入商品规格') } if (!product_unit) { errorList.push('请选择商品单位') } if (!(this.data.form.product_all_price)) { errorList.push('请输入批发价') } if (!(this.data.form.product_price)) { errorList.push('请输入零售价') } if (!product_count) { errorList.push('请输入库存') } if (product_sale_at !== 0 && !product_sale_at) { errorList.push('请选择预售时间') } return errorList }, async onSubmit(e) { const temp = this.getForm() const verifyList = this.verify() if (verifyList.length) { wx.showToast({ title: verifyList[0], icon: 'none' }) return } try { let res = {} if (this.data.form.id) { res = await postModifyProduct({ ...temp, id: this.data.form.id }) } else { res = await postAddProduct(temp) } const { status, msg } = res if (status) { wx.navigateBack({ delta: 1 }) } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (err) {} }, setUnitInColumns() { const { product_unit_list } = app.globalData.objSystemConfig this.setData({ unitInColumns: Array.isArray(product_unit_list) && product_unit_list.length > 0 ? product_unit_list : [] }) }, setForm(data) { let temp = {} for (let key in data) { let value = data[key] if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') { if (key === 'product_img_url') { value = [ { 'formkey': key, 'url': Array.isArray(value) ? value.join('') : value } ] } if (key === 'product_rotation_img_list' || key === 'product_detail_img_list') { value = (Array.isArray(value) ? value : JSON.parse(value)).map(item => { return { 'formkey': key, 'url': item } }) } if (key === 'product_category_id') { const category = this.data.categoryList.filter(item => item.id === value) if (category.length > 0) { value = category[0] temp['categoryInDefaultIndex'] = this.data.categoryList.findIndex(item => item.id === value.id) } } if (key === 'product_brand_id') { const brand = this.data.brandList.filter(item => item.id === value) if (brand.length > 0) { value = brand[0] temp['brandInDefaultIndex'] = this.data.brandList.findIndex(item => item.id === value.id) } } if (key === 'product_unit') { const unit = this.data.unitInColumns.filter(item => item === value) if (unit.length > 0) { temp['unitInDefaultIndex'] = this.data.unitInColumns.findIndex(item => item === value) } } if (key === 'product_all_price' || key === 'product_price') { value = fen2Yuan(value) } if (key === 'product_sale_at') { if (value === 0) { value = '现货' } else { const { YYYY, MM, DD } = formatTs(value * 1000) temp['saleAtType'] = '1' temp['currentDate'] = value * 1000 value = `${YYYY}年${MM}月${DD}日` } } if (key === 'product_check_status') { temp['productCheckStatus'] = value } if (this.data.form.hasOwnProperty(key)) { temp[`form.${key}`] = value } } } this.setData(temp) }, async updateBusinessGoodsEditsDataOfStorage(formKey, value) { const key = app.globalData.businessGoodsEditsStorageKey let temp = {} if (this.data.form.id) { return } try { const { data, errMsg } = await wx.getStorage({ key }) if (errMsg === 'getStorage:ok' && /^{/.test(data)) { temp = JSON.parse(data) } } catch (e) { temp = {} } wx.setStorage({ key, data: JSON.stringify({ ...temp, [formKey]: value }) }) } })