const uploadJS = require('../../mixin/upload.js') const { getProductCategoryList, getProductBrandList } = require('./api/index') Page({ /** * 页面的初始数据 */ data: { // * 商品品牌:下拉选择 // * 单位:下拉选择 // * 商品货号:系统自动生成(只读) // * 批发价 // * 零售价 // * 库存 // * 状态:现售产品有上架、下架状态 // * 发售时间:不能低于当前创建时间 form: { 'product_img_url': [], // 商品主图 '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': [], // 详情图 '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: ['件', '个'] }, /** * 生命周期函数--监听页面加载 */ async onLoad(options) { await this.fetchProductCategoryList() await this.fetchProductBrandList() }, /** * 生命周期函数--监听页面初次渲染完成 */ 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) }, onSubmit(e) { console.log(e.detail) } })