// pages/addtogood/addtogood.js const app = getApp(); const api = require('../../api/api'); const storageManager = require("../../utils/storage-manager.js"); Page({ /** * 页面的初始数据 */ data: { navbarData: { showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示 title: '添加商品', //导航栏 中间的标题 capsuleMode: 'navBack', //显示模式(navBack:返回上一页;navHome:返回首页) }, enterType: '', //进入方式 arealength: 0, showLadderPrice: false, //阶梯价弹出层显示 showCategory: false, //分类弹出层显示 showSpecification: false, //规格弹出层显示 specificationText: '统一规格', // /* 表单 */ goodName: '', //商品名称 goodIntro: '', //商品简介 imageList: [], //图片列表 scribePrice: '', //划线价格 specifications: [], //规格数组 price: '', //售价 stock: '', //库存 categorys: [], //分类数组 categoryIndex: 0, //分类索引 isAddToGoodsBank: 1, //是否添加到商品库 startingPurchase: '', //起购数量 purchaseRestrictionsNumber: '', //限购数量 purchaseRestrictionsSecond: '', //限购次数 startingPurchaseNumber: '', //起购团数 prices: [], //阶梯价格数组 }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { let that = this; that.setData({ enterType: options.enterType }) // 获取分类列表 wx.showLoading({ title: '加载中', mask: true }) wx.request({ url: api.Classification_list, header: { 'Authorization': wx.getStorageSync('token') }, method: 'POST', success(res) { console.log(res); if (res.data.code === 1) { that.setData({ categorys: res.data.data }) } else { wx.showToast({ title: res.data.msg, mask: true, icon: 'none' }) } }, fail(err) { wx.showToast({ title: '发起网络请求失败', icon: 'none', mask: true }) }, complete() { wx.hideLoading() } }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { }, // 输入商品名称 inputGoodName(e) { this.setData({ goodName: e.detail.value }) }, // 输入商品简介 inputGoodIntro(e) { this.setData({ goodIntro: e.detail.value, arealength: e.detail.value.length }) }, // 添加商品图片 uploadSmallImage() { let that = this; if (that.data.imageList.length < 5) { // wx.showLoading({ // title: '文件上传中', // mask: true // }) wx.request({ url: api.getSignedUrl, data: { type: 'image' }, success: (oss) => { console.log(oss); if (oss.statusCode === 200) { wx.chooseMedia({ count: 1, mediaType: ['image'], success: (img) => { console.log(img); let key = "image/" + new Date().getTime() + Math.floor(Math.random() * 150) + '.png'; wx.showLoading({ title: '文件上传中', mask: true }) wx.uploadFile({ filePath: img.tempFiles[0].tempFilePath, name: 'file', url: 'https://' + oss.data.data.host, //输入你的bucketname.endpoint formData: { key: key, policy: oss.data.data.policy, // 输入你获取的的policy OSSAccessKeyId: oss.data.data.OSSAccessKeyId, // 输入你的AccessKeyId success_action_status: '200', // 让服务端返回200,不然,默认会返回204 signature: oss.data.data.Signature, // 输入你获取的的signature }, success: (res) => { if (res.statusCode == 200) { let src = 'https://' + oss.data.data.host + '/' + key; console.log(src); let imageList = that.data.imageList;; imageList.push(src); that.setData({ imageList }) } }, fail: (err) => { console.log(err); }, complete: () => { wx.hideLoading() } }) }, fail: (err) => { wx.hideLoading() } }) } else { wx.showToast({ title: '服务器出现错误', icon: 'none' }) } }, fail: (err) => { console.log(err); } }) } }, // 删除某个小图 deleteSmallImage(e) { let that = this; let index = e.currentTarget.dataset.index; let imageList = that.data.imageList; imageList.splice(index, 1); this.setData({ imageList }) }, // 输入商品划线价格 inputScribePrice(e) { this.setData({ scribePrice: e.detail.value }) }, // 打开规格弹出层 showSpecificationPopup() { let that = this; if (that.data.prices.length == 0) { if (that.data.specifications.length == 0) { that.setData({ showSpecification: true, specifications: [{ title: '', price: '' }, { title: '', price: '' } ] }) } else { that.setData({ showSpecification: true }) } } else { wx.showModal({ title: '提示', content: '规格和阶梯价不可同时设置,如需设置规格,则会清除阶梯价内所有设置', success(res) { if (res.confirm) { if (that.data.specifications.length == 0) { that.setData({ prices: [], showSpecification: true, specifications: [{ title: '', price: '' }, { title: '', price: '' } ] }) } else { that.setData({ prices: [], showSpecification: true }) } } else if (res.cancel) { console.log('用户点击取消') } } }) } }, // 关闭规格弹出层 closeSpecificationPopup() { let that = this; let isOK = that.judgeNoyNullSpecifications(that.data.specifications); if (isOK) { that.setData({ specificationText: that.data.specifications[0].title + '/' + that.data.specifications[0].price + '加币', price: Number(that.data.specifications[0].price), showSpecification: false }) } else { wx.showToast({ title: '您未完整录入数据', icon: 'none', mask: true, success() { setTimeout(() => { that.setData({ specificationText: '统一规格', specifications: [], showSpecification: false }) }, 1500) } }) } }, // 规格输入 specificationsInput(e) { let index = e.currentTarget.dataset.index; let value = e.detail.value; let list = this.data.specifications; list[index].title = value; this.setData({ specifications: list }) }, // 规格金额输入 specificationsPriceInput(e) { let index = e.currentTarget.dataset.index; let value = e.detail.value; let list = this.data.specifications; list[index].price = value; this.setData({ specifications: list }) }, // 添加规格 addSpecification() { let bulk = { title: '', price: '' } let specifications = this.data.specifications; specifications.push(bulk) this.setData({ specifications: specifications }) }, // 删除规格 delSpecification(e) { let index = e.currentTarget.dataset.index let list = this.data.specifications; list.splice(index, 1); this.setData({ specifications: list }) }, // 输入售价 inputPrice(e) { this.setData({ price: e.detail.value }) }, // 输入库存 inputStock(e) { this.setData({ stock: e.detail.value }) }, // 打开分类弹出层 showCategoryPopup() { this.setData({ showCategory: true }) }, // 关闭分类弹出层 closeCategoryPopup() { this.setData({ showCategory: false }) }, // 选择分类 selectCategory(e) { this.setData({ categoryIndex: e.currentTarget.dataset.index }) }, // 是否添加到商品库 changeIsAddToGoodsBank(e) { this.setData({ isAddToGoodsBank: e.detail }) }, // 输入起购数量 inputStartingPurchase(e) { this.setData({ startingPurchase: e.detail.value }) }, // 输入限购数量 inputPurchaseRestrictionsNumber(e) { this.setData({ purchaseRestrictionsNumber: e.detail.value }) }, // 输入限购次数 inputPurchaseRestrictionsSecond(e) { this.setData({ purchaseRestrictionsSecond: e.detail.value }) }, // 输入起购团数 inputStartingPurchaseNumber(e) { this.setData({ startingPurchaseNumber: e.detail.value }) }, // 打开阶梯价弹出层 showLadderPricePopup() { let that = this; if (that.data.specifications.length == 0) { if (that.data.prices.length == 0) { that.setData({ showLadderPrice: true, prices: [{ number: '', price: '' }, { number: '', price: '' } ] }) } else { that.setData({ showLadderPrice: true }) } } else { wx.showModal({ title: '提示', content: '规格和阶梯价不可同时设置,如需设置阶梯价,则会清除规格内所有设置', success(res) { if (res.confirm) { if (that.data.prices.length == 0) { that.setData({ specifications: [], showLadderPrice: true, prices: [{ number: '', price: '' }, { number: '', price: '' } ] }) } else { that.setData({ specifications: [], showLadderPrice: true }) } } else if (res.cancel) { console.log('用户点击取消') } } }) } }, // 关闭阶梯价弹出层 closeLadderPricePopup() { let that = this; let isOK = that.judgeNoyNullStepPrice(that.data.prices); if (isOK) { that.setData({ specificationText: '统一规格', price: Number(that.data.prices[0].price), showLadderPrice: false }) } else { wx.showToast({ title: '您未完整录入数据', icon: 'none', mask: true, success() { setTimeout(() => { that.setData({ specificationText: '统一规格', prices: [], showLadderPrice: false }) }, 1500) } }) } }, // 阶梯价个数输入 ladderPriceNumInput(e) { let index = e.currentTarget.dataset.index; let value = e.detail.value; let list = this.data.prices; list[index].number = value; this.setData({ prices: list }) }, // 阶梯价金额输入 ladderPricePriceInput(e) { let index = e.currentTarget.dataset.index; let value = e.detail.value; let list = this.data.prices; list[index].price = value; this.setData({ prices: list }) }, // 添加阶梯 addbulk() { let bulk = { number: '', price: '' } let prices = this.data.prices; prices.push(bulk) this.setData({ prices: prices }) }, // 删除阶梯 delbulk(e) { let index = e.currentTarget.dataset.index let list = this.data.prices; list.splice(index, 1); this.setData({ prices: list }) }, // 确认添加 submit() { let that = this; // 商品名称必录校验 if (that.data.goodName == '') { wx.showToast({ title: '商品名称未录入', icon: 'none', mask: true }) return } // 商品简介必录校验 if (that.data.goodIntro == '') { wx.showToast({ title: '商品简介未录入', icon: 'none', mask: true }) return } // 商品图片必录校验 if (that.data.imageList.length == 0) { wx.showToast({ title: '商品图片未添加', icon: 'none', mask: true }) return } // 划线价格必录校验 if (that.data.scribePrice == '') { wx.showToast({ title: '划线价格未录入', icon: 'none', mask: true }) return } // 售价必录校验 if (that.data.price == '') { wx.showToast({ title: '售价未录入', icon: 'none', mask: true }) return } // 库存 let stock = that.data.stock; if (stock == '') { stock = '99999999'; } // 起购数量必录校验 /* if (that.data.startingPurchase == '') { wx.showToast({ title: '起购数量未录入', icon: 'none', mask: true }) return } */ // 限购数量必录校验 /* if (that.data.purchaseRestrictionsNumber == '') { wx.showToast({ title: '限购数量未录入', icon: 'none', mask: true }) return } */ // 限购次数必录校验 /* if (that.data.purchaseRestrictionsSecond == '') { wx.showToast({ title: '限购次数未录入', icon: 'none', mask: true }) return } */ // 起购团数必录校验 /* if (that.data.startingPurchaseNumber == '') { wx.showToast({ title: '起购团数未录入', icon: 'none', mask: true }) return } */ // 如果不录规格也不录阶梯价-规格为统一规格,价格为售价 if (that.data.specifications.length == 0 && that.data.prices.length == 0) { let list = that.data.specifications; let obj = {}; obj.title = '统一规格'; obj.price = that.data.price; list.push(obj); that.setData({ specifications: list }) } wx.showLoading({ title: '提交中', mask: true }) wx.request({ url: api.Commodity_add, header: { 'Authorization': wx.getStorageSync('token') }, method: 'POST', data: { commodity_name: that.data.goodName, commodity_img: that.data.imageList, commodity_introduction: that.data.goodIntro, scribe_price: that.data.scribePrice, specifications: that.data.specifications, price: that.data.price, stock: stock, classification_id: that.data.categorys[that.data.categoryIndex].id, commodity_warehouse: that.data.isAddToGoodsBank, starting_purchase: that.data.startingPurchase, purchase_restrictions_number: that.data.purchaseRestrictionsNumber, purchase_restrictions_second: that.data.purchaseRestrictionsSecond, starting_purchase_number: that.data.startingPurchaseNumber, step_price: that.data.prices }, success(res) { console.log(res); wx.hideLoading() if (res.data.code === 1) { wx.showToast({ title: '提交成功', icon: 'success', mask: true, success() { if (that.data.enterType == 'announce') { // 获取商品详情 let id = Number(res.data.data); //商品id wx.request({ url: api.Commodity_info, header: { 'Authorization': wx.getStorageSync('token') }, data: { id: id }, success(ress) { console.log(ress); if (ress.data.code === 1) { let arr = []; let obj = ress.data.data; arr.push(obj); storageManager.set('subPageResult', arr); setTimeout(() => { wx.navigateBack({ delta: 1, }) }, 1500) } } }) } else { setTimeout(() => { wx.navigateBack({ delta: 1, }) }, 1500) } } }) } else { wx.showToast({ title: res.data.msg, mask: true, icon: 'none' }) } }, fail(err) { wx.hideLoading() wx.showToast({ title: '发起网络请求失败', icon: 'none', mask: true }) }, complete() { // wx.hideLoading() } }) }, // 阶梯价-判断不为空 judgeNoyNullStepPrice(arr) { let isOK = true; for (let i = 0; i < arr.length; i++) { if (arr[i].title == '' || arr[i].price == '') { isOK = false; } } return isOK; }, // 规格-判断不为空 judgeNoyNullSpecifications(arr) { let isOK = true; for (let i = 0; i < arr.length; i++) { if (arr[i].number == '' || arr[i].price == '') { isOK = false; } } return isOK; }, })