const uploadJS = require('../../mixin/upload.js') const { postAddProduct, postModifyProduct } = require('./api/index') const { getProductCategoryList, getProductBrandList } = require('../../api/common') const { formatTs, yuan2Fen } = require('../../utils/util') const minDate = new Date().getTime() const objMinDate = formatTs(minDate) Page({ /** * 页面的初始数据 */ data: { // * 商品品牌:下拉选择 // * 单位:下拉选择 // * 商品货号:系统自动生成(只读) // * 批发价 // * 零售价 // * 库存 // * 状态:现售产品有上架、下架状态 // * 发售时间:不能低于当前创建时间 form: { 'product_img_url': [ { 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffe57fa.jpg', 'formkey': 'product_rotation_img_list' } ], // 商品主图 'product_rotation_img_list': [ { 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffe57fa.jpg', 'formkey': 'product_rotation_img_list' }, { 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffe51c4.jpg', 'formkey': 'product_rotation_img_list' }, { 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffefa54.jpg', 'formkey': 'product_rotation_img_list' }, { 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi6323230067e26.png', 'formkey': 'product_rotation_img_list' } ], // 商品轮播图 'product_detail_img_list': [ { 'url': 'https://tuotuoyinfu-oss.oss-cn-beijing.aliyuncs.com/images/user/bashi632322ffe57fa.jpg', 'formkey': 'product_rotation_img_list' } ], // 详情图 'product_title': '商品标题', // 商品标题 'product_desc': '商品简介', // 商品简介 'product_category_id': { 'category_name': '', 'id': '1' }, // 分类ID 'product_brand_id': { 'brand_name': '', 'id': '1' }, // 品牌ID 'product_spec': '10个没件', // 规格 'product_unit': '个', // 单位 'product_all_price': '100', // 批发价 'product_price': '88', // 零售价 'product_count': '100', // 库存 '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() }, /** * 生命周期函数--监听页面加载 */ async onLoad(options) { const { id } = options await this.fetchProductCategoryList() await this.fetchProductBrandList() if (id) { this.setData({ 'form.id': id }, () => { }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, 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() { 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' }) } }, ...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}`] = temp break case 'product_rotation_img_list': case 'product_detail_img_list': tempForm[`form.${formkey}`] = this.data.form[formkey].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) }, 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_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 : this.data.currentDate } }, setForm() {}, 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.length) { errorList.push('请上传商品轮播图') } if (!product_detail_img_list.length) { 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 = await postAddProduct(temp) if (this.data.form.id) { res = await postModifyProduct({ ...temp, id: this.data.form.id }) } const { status, data, msg } = res } catch (err) {} } })