const uploadJS = require('../../mixin/upload.js') const { postAddProduct, postModifyProduct, getProductDetail } = require('./api/index') const { getProductCategoryList, getProductBrandList } = require('../../api/common') const { formatTs, yuan2Fen, fen2Yuan } = require('../../utils/util') const minDate = new Date().getTime() const objMinDate = formatTs(minDate) 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_status': 0, // 状态 '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: '产品新增' }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ 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() { let temp = {} try { const { status, data, msg } = await getProductDetail(this.data.form.id) if (status) { 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': value } ] } if (key === 'product_rotation_img_list' || key === 'product_detail_img_list') { 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) } } 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) } } 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, HH, mm } = formatTs(value * 1000) temp['saleAtType'] = '1' temp['currentDate'] = value * 1000 value = `${YYYY}年${MM}月${DD}日 ${HH}:${mm}` } } if (key === 'product_check_status') { temp['productCheckStatus'] = 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 '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) } }, 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.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.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.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) }, handleRadio(event) { const { formkey, item } = event.currentTarget.dataset this.setData({ [`form.${formkey}`]: item.value }) }, 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) }, handleSaleAtType(e) { const { type } = e.currentTarget.dataset this.setData({ saleAtType: type }, () => { this.saleAtTypeHide() }) }, saleAtHide() { this.setData({ booSaleAt: false }) }, saleAtConfirm(event) { const { YYYY, MM, DD, HH, mm } = formatTs(event.detail) this.setData({ currentDate: event.detail, 'form.product_sale_at': `${YYYY}年${MM}月${DD}日 ${HH}:${mm}` }) 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_status, 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_status, product_sale_at: product_sale_at === '预售' ? 0 : Math.round(this.data.currentDate / 1000) } }, 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, data, msg } = res if (status) { wx.redirectTo({ url: '/pages/businessGoodsManage/businessGoodsManage' }) } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (err) {} } })