var app = getApp(); Component({ properties: { DataInfo: { type: null }, store: { type: null }, store_id: { type: null }, goods_id: { type: Number }, isType: { type: null } }, data: { // store: '', uploadUrl: '', store_id: '', sectionOptions: [], objectMultiArray: [], multiIndex: [0, 0, 0], regional: '请选择商品分类', cargon_data: { title: '', sku: '', thumb: '', price: '', market_price: '', cost_price: '', stock: '', thumb_url: '' }, imageUrl: '', fileList2: [], fileList1: [], upNumMax: 0, upNumMaxOther: 0, fileList3: [], fileList4: [], selectedOptions3: [], jumpState: true, shopText: ''//商品详情文字 }, // 私有数据,可用于模板渲染 lifetimes: { // 生命周期函数,可以为函数,或一个在methods段中定义的方法名 attached() { }, moved() { }, detached() { }, ready() { console.log(this.properties.store); if (this.properties.store) { this.setData({ store: this.data.store }); } if (this.properties.store_id) { this.setData({ store_id: this.data.store_id }); this._getJson(); } if (this.properties.goods_id) { this.setData({ goods_id: this.properties.goods_id }); this._getJson(true); } this._urlUp(); } }, // 生命周期函数,可以为函数,或一个在methods段中定义的方法名 attached() { }, // 此处attached的声明会被lifetimes字段中的声明覆盖 ready() { }, pageLifetimes: { // 组件所在页面的生命周期函数 show() { }, hide() { }, resize() { } }, methods: { areaInp(e) { console.log(e); let txt = e.detail.value; this.setData({ shopText: txt }); }, // 商品图片上传 _urlUp() { if (this.data.store == 'store') { this.setData({ uploadUrl: 'upload.uploadPic' }); } else if (this.data.store == 'supplier') { this.setData({ uploadUrl: 'upload.uploadPic' }); } }, // 选择分类对接数据 _getJson(flag = false) { let that = this; if (that.data.store == 'store') { let urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods-category.get-category-by-store-id'); urlStr += '&store_id=' + that.properties.store_id; app._getNetWork({ url: urlStr, success: function (resdata) { var res = resdata.data; let list = []; res.data.list.map(item => { list.push({ name: item.name, id: item.id, childrens: item.childrens }); }); that.setData({ list }); if (res.result == 1) { let sectionOptionsData = []; for (let i = 0; i < res.data.list.length; i++) { sectionOptionsData.push({ id: res.data.list[i].id, name: res.data.list[i].name, children: [] }); for (let k = 0; k < res.data.list[i].childrens.length; k++) { sectionOptionsData[i].children.push({ id: res.data.list[i].childrens[k].id, name: res.data.list[i].childrens[k].name }); } } that.setData({ sectionOptions: sectionOptionsData }); console.log(sectionOptionsData); that.goodsHandlefunction(); if (flag) that.getGoodsJson(); } else { wx.showToast({ icon: 'none', title: res.msg, duration: 1500 }); } }, fail: function (res) { console.log(res.msg); } }); } else if (that.data.store == 'supplier') { let urlStr = app.getNetAddresss('plugin.supplier.frontend.goods.get-category'); app._postNetWork({ url: urlStr, showToastIn: false, data: {}, success: function (resdata) { var res = resdata.data; let list = []; res.data.map(item => { list.push({ name: item.name, id: item.id, childrens: item.childrens }); }); that.setData({ list }); if (res.result == 1) { let sectionOptionsData = []; for (var i = 0; i < res.data.length; i++) { sectionOptionsData.push({ id: res.data[i].id, name: res.data[i].name, children: [] }); if (!res.data[i].childrens || res.data[i].childrens.length < 1) { // 判断是否有二级分类 } else { // that.sectionOptions[i].children = [] for (let k = 0; k < res.data[i].childrens.length; k++) { sectionOptionsData[i].children.push({ id: res.data[i].childrens[k].id, name: res.data[i].childrens[k].name, children: [] }); if (!res.data[i].childrens[k].childrens || res.data[i].childrens[k].childrens.length < 1) { // 判断是否有三级分类 } else { for (var j = 0; j < res.data[i].childrens[k].childrens.length; j++) { sectionOptionsData[i].children[k].children.push({ id: res.data[i].childrens[k].childrens[j].id, name: res.data[i].childrens[k].childrens[j].name }); } } } } } that.setData({ sectionOptions: sectionOptionsData }); that.goodsHandlefunction(); if (flag) that.getGoodsJson(); } else { wx.showToast({ icon: 'none', title: res.msg, duration: 1500 }); } }, fail: function (res) { } }); } }, commodityBtn(e) { let column = e.detail.column; let val = e.detail.value; const data = { multiArray: this.data.objectMultiArray }; this.setData({ ['multiIndex[' + column + ']']: val }); switch (column) { case 0: this.selecommodity_0(val); break; case 1: this.selecommodity_1(this.data.multiIndex[0], val); break; case 2: break; } this.setData(data); }, goodsHandlefunction() { let sectionOptions = this.data.sectionOptions; let arr_1 = []; let arr_2 = []; let arr_3 = []; for (let i = 0; i < sectionOptions.length; i++) { arr_1.push({ id: sectionOptions[i].id, name: sectionOptions[i].name }); } if (sectionOptions[0].children.length > 0) { for (let k = 0; k < sectionOptions[0].children.length; k++) { arr_2.push({ id: sectionOptions[0].children[k].id, name: sectionOptions[0].children[k].name }); } } if ((sectionOptions[0].children && sectionOptions[0].children.length > 0) && (sectionOptions[0].children[0].children && sectionOptions[0].children[0].children.length > 0)) { for (let j = 0; j < sectionOptions[0].children[0].children.length; j++) { arr_3.push({ id: sectionOptions[0].children[0].children[j].id, name: sectionOptions[0].children[0].children[j].name }); } } this.setData({ 'objectMultiArray[0]': arr_1, 'objectMultiArray[1]': arr_2, 'objectMultiArray[2]': arr_3 }); console.log(this.data.objectMultiArray); }, selecommodity_0(index) { let sectionOptions = this.data.sectionOptions; let arr_2 = []; let arr_3 = []; if (sectionOptions[index].children.length > 0) { for (let k = 0; k < sectionOptions[index].children.length; k++) { arr_2.push({ id: sectionOptions[index].children[k].id, name: sectionOptions[index].children[k].name }); } } if ((sectionOptions[index].children && sectionOptions[index].children.length > 0) && (sectionOptions[index].children[0].children && sectionOptions[index].children[0].children.length > 0)) { for (let j = 0; j < sectionOptions[index].children[0].children.length; j++) { arr_3.push({ id: sectionOptions[index].children[0].children[j].id, name: sectionOptions[index].children[0].children[j].name }); } } this.setData({ 'objectMultiArray[1]': arr_2, 'objectMultiArray[2]': arr_3, 'multiIndex[1]': 0, 'multiIndex[2]': 0 }); }, selecommodity_1(index_0, index_1) { let sectionOptions = this.data.sectionOptions; let arr_3 = []; if ((sectionOptions[index_0].children && sectionOptions[index_0].children.length > 0) && (sectionOptions[index_0].children[index_1].children && sectionOptions[index_0].children[index_1].children.length > 0)) { for (let j = 0; j < sectionOptions[index_0].children[index_1].children.length; j++) { arr_3.push({ id: sectionOptions[index_0].children[index_1].children[j].id, name: sectionOptions[index_0].children[index_1].children[j].name }); } } this.setData({ 'objectMultiArray[2]': arr_3, 'multiIndex[2]': 0 }); }, commodityconfirm(e) { var i = e.detail.value[0]; if (this.data.list[i].childrens.length == 0) { wx.showToast({ title: '至少要选择二级分类', icon: 'none', duration: 2000 }); return; } let sectionOptions = this.data.sectionOptions; let indexArray = e.detail.value; let index_0 = indexArray[0]; let index_1 = indexArray[1]; let index_2 = indexArray[2]; let idArray = []; let strArray = []; if (sectionOptions.length > 0) { idArray.push(sectionOptions[index_0].id); strArray.push(sectionOptions[index_0].name); if (sectionOptions[index_0].children.length > 0) { idArray.push(sectionOptions[index_0].children[index_1].id); strArray.push(sectionOptions[index_0].children[index_1].name); if (sectionOptions[index_0].children[index_1].children && sectionOptions[index_0].children[index_1].children.length > 0) { idArray.push(sectionOptions[index_0].children[index_1].children[index_2].id); strArray.push(sectionOptions[index_0].children[index_1].children[index_2].name); } } } this.setData({ regional: strArray.join('/'), selectedOptions3: idArray }); }, // 选择图片 onRead() { let that = this; let urlStr = app.getNetAddresss(this.data.uploadUrl); wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success(res) { const tempFilePaths = res.tempFilePaths; wx.compressImage({ src: tempFilePaths[0], // 图片路径 quality: 60, // 压缩质量 success(e) { let photourl = e.tempFilePath; wx.uploadFile({ url: urlStr, filePath: photourl, name: 'file', success(resdata) { var res = JSON.parse(resdata.data); if (res.result == 1) { that.setData({ imageUrl: photourl, 'cargon_data.thumb': res.data.img }); } else { wx.showToast({ icon: 'none', title: res.msg, duration: 1500 }); } } }); }, fail(e) { }, complete(e) { } }); } }); }, // 选择图片 onRead_1() { // if (this.data.upNumMax == 4 || this.data.upNumMax >= 4) { // wx.showToast({ // icon: 'none', // title: '图片数量已达到上限', // duration: 1500 // }) // return false // } let that = this; let urlStr = app.getNetAddresss(this.data.uploadUrl); wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success(res) { const tempFilePaths = res.tempFilePaths; wx.compressImage({ src: tempFilePaths[0], // 图片路径 quality: 60, // 压缩质量 success(e) { let photourl = e.tempFilePath; wx.uploadFile({ url: urlStr, filePath: photourl, name: 'file', formData: { attach: 'upload' }, success(resdata) { var res = JSON.parse(resdata.data); if (res.result == 1) { let fileList1 = that.data.fileList1; let fileList2 = that.data.fileList2; fileList1.push({ url: res.data.img }); fileList2.push({ url: photourl }); that.setData({ upNumMax: that.data.upNumMax + 1, fileList1: fileList1, fileList2: fileList2 }); } else { wx.showToast({ icon: 'none', title: res.msg, duration: 1500 }); } } }); }, fail(e) { }, complete(e) { } }); } }); }, removeImg: function (e) { let index = e.currentTarget.dataset.index; let fileList1 = this.data.fileList1; let fileList2 = this.data.fileList2; fileList1.splice(index, 1); fileList2.splice(index, 1); this.setData({ fileList1: fileList1, fileList2: fileList2 }); this.setData({ upNumMax: this.data.fileList1.length }); }, // 选择图片 onRead_2() { // if (this.data.upNumMaxOther == 4 || this.data.upNumMaxOther >= 4) { // wx.showToast({ // icon: 'none', // title: '图片数量已达到上限', // duration: 1500 // }) // return false // } let that = this; let urlStr = app.getNetAddresss(this.data.uploadUrl); wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success(res) { const tempFilePaths = res.tempFilePaths; wx.compressImage({ src: tempFilePaths[0], // 图片路径 quality: 60, // 压缩质量 success(e) { let photourl = e.tempFilePath; wx.uploadFile({ url: urlStr, filePath: photourl, name: 'file', formData: { attach: 'upload' }, success(resdata) { var res = JSON.parse(resdata.data); if (res.result == 1) { let fileList3 = that.data.fileList3; let fileList4 = that.data.fileList4; fileList3.push({ url: res.data.img_url }); fileList4.push({ url: photourl }); that.setData({ upNumMaxOther: that.data.upNumMaxOther + 1, fileList3: fileList3, fileList4: fileList4 }); } else { wx.showToast({ icon: 'none', title: res.msg, duration: 1500 }); } } }); }, fail(e) { }, complete(e) { } }); } }); }, removeImg_1: function (e) { let index = e.currentTarget.dataset.index; let fileList3 = this.data.fileList3; // let fileList4 = this.data.fileList4 let infoArr = this.data.infoArr; // infoArr.splice(index, 1) fileList3.splice(index, 1); // fileList4.splice(index, 1) this.setData({ fileList3: fileList3, // fileList4: fileList4, infoArr }); this.setData({ upNumMaxOther: this.data.fileList3.length }); }, // 商品名称值绑定 titleinp(e) { let val = e.detail.value; this.setData({ 'cargon_data.title': val }); }, // 商品单位值绑定 skuinp(e) { let val = e.detail.value; this.setData({ 'cargon_data.sku': val }); }, // 商品现价值绑定 priceinp(e) { let val = e.detail.value; if(val.indexOf(".")!==-1){ let index = val.indexOf("."); val = val.replace(/[^\d]/g,""); let newStr = val.slice(0,index) + '.' + val.slice(index); this.setData({ 'cargon_data.price': newStr }); }else{ this.setData({ 'cargon_data.price': val }); } }, // 商品原价值绑定 marketpriceinp(e) { let val = e.detail.value; if(val.indexOf(".")!==-1){ let index = val.indexOf("."); val = val.replace(/[^\d]/g,""); let newStr = val.slice(0,index) + '.' + val.slice(index); this.setData({ 'cargon_data.market_price': newStr }); }else{ this.setData({ 'cargon_data.market_price': val }); } }, // 商品成本值绑定 costPriceinp(e) { let val = e.detail.value; if(val.indexOf(".")!==-1){ let index = val.indexOf("."); val = val.replace(/[^\d]/g,""); let newStr = val.slice(0,index) + '.' + val.slice(index); this.setData({ 'cargon_data.cost_price': newStr }); }else{ this.setData({ 'cargon_data.cost_price': val }); } }, // 商品库存值绑定 stockinp(e) { let val = e.detail.value; this.setData({ 'cargon_data.stock': val }); }, // 搞数据 setJson() { var that = this; var newArr = []; if (that.data.fileList1.length != 0) { for (let i = 0; i < that.data.fileList1.length; i++) { newArr.push(that.data.fileList1[i].url); } } else { newArr = []; } var newArr2 = []; if (that.data.fileList3.length != 0) { for (let i = 0; i < that.data.fileList3.length; i++) { newArr2.push(that.data.fileList3[i].url); } } else { newArr2 = []; } let json = {}; if (!this.data.isType) { if (app._isTextEmpty(this.data.infoArr)) { this.setData({ infoArr: [] }); } console.log(this.data.infoArr, this.data.fileList3, '两个链接的数组'); let newInfo = this.data.infoArr.concat(this.data.fileList3); newArr2 = []; if (newInfo.length != 0) { for (let i = 0; i < newInfo.length; i++) { if (newInfo[i].url.indexOf('wxfile') == '-1') { newArr2.push(newInfo[i].url); } } } else { newArr2 = []; } } if (this.data.store == 'store') { json = { store_id: this.data.store_id, category_pid: this.data.selectedOptions3[0], category_cid: this.data.selectedOptions3[1], title: this.data.cargon_data.title, thumb: this.data.cargon_data.thumb, thumb_url: newArr2, good_content: newArr, cost_price: this.data.cargon_data.cost_price, market_price: this.data.cargon_data.market_price, price: this.data.cargon_data.price, sku: this.data.cargon_data.sku, stock: this.data.cargon_data.stock }; if (!this.data.isType) { json = { store_id: this.data.store_id, goods_id: this.data.goods_id, category_pid: this.data.selectedOptions3[0], category_cid: this.data.selectedOptions3[1], title: this.data.cargon_data.title, thumb: this.data.cargon_data.thumb, thumb_url: newArr2, // good_content: newArr, cost_price: this.data.cargon_data.cost_price, market_price: this.data.cargon_data.market_price, price: this.data.cargon_data.price, sku: this.data.cargon_data.sku, stock: this.data.cargon_data.stock }; } } else if (this.data.store == 'supplier') { json = { title: this.data.cargon_data.title, thumb: this.data.cargon_data.thumb, thumb_url: newArr2, good_content: newArr, cost_price: this.data.cargon_data.cost_price, market_price: this.data.cargon_data.market_price, price: this.data.cargon_data.price, sku: this.data.cargon_data.sku, stock: this.data.cargon_data.stock, content_text: this.data.shopText }; if (!this.data.isType) { json = { title: this.data.cargon_data.title, thumb: this.data.cargon_data.thumb, thumb_url: newArr2, cost_price: this.data.cargon_data.cost_price, market_price: this.data.cargon_data.market_price, price: this.data.cargon_data.price, sku: this.data.cargon_data.sku, stock: this.data.cargon_data.stock }; } if (this.data.selectedOptions3.length < 3) { json.category_pid = this.data.selectedOptions3[0]; json.category_cid = this.data.selectedOptions3[1]; } else { json.category_pid = this.data.selectedOptions3[0]; json.category_cid = this.data.selectedOptions3[1]; json.category_tid = this.data.selectedOptions3[2]; } } this.triggerEvent('getChildoneData', json, { bubbles: false }); }, // 跳转测试 jumpEvent() { this.setData({ jumpState: true }); for (let item in this.data.cargon_data) { if (this.data.cargon_data[item] == '') { this.setData({ jumpState: false }); } } if (this.data.selectedOptions3.length == 0) { this.setData({ jumpState: false }); } if (this.data.imageUrl == '') { this.setData({ jumpState: false }); } }, // 获取数据 getGoodsJson() { let that = this; var urlStr = ''; var json = {}; if (that.data.store == 'store') { urlStr = app.getNetAddresss('plugin.store-cashier.frontend.store.goods.get-goods-detail'); json = { goods_id: that.data.goods_id }; } else if (that.data.store == 'supplier') { urlStr = app.getNetAddresss('plugin.supplier.frontend.goods.goods-detail'); json = { goods_id: that.data.goods_id }; } app._postNetWork({ url: urlStr, showToastIn: false, data: json, success: function (resdata) { var res = resdata.data; if (res.result == 1) { let selectedOptions3 = that.data.selectedOptions3; if (res.data.category_ids.parentid) { selectedOptions3.push(Number(res.data.category_ids.parentid)); } if (res.data.category_ids.childid) { selectedOptions3.push(Number(res.data.category_ids.childid)); } if (res.data.category_ids.thirdid) { selectedOptions3.push(Number(res.data.category_ids.thirdid)); } that.setData({ selectedOptions3: selectedOptions3 }); if ( res.data.thumb_url != null && res.data.thumb_url != '' ) { // let fileList1 = that.data.fileList1 // let fileList2 = that.data.fileList2 // for (var i = 0; i < res.data.thumb_url.length; i++) { // fileList1.push({ // url: res.data.thumb_url[i] // }) // fileList2.push({ // url: res.data.thumb // }) // } // that.setData({ // fileList4: fileList1, // infoArr: fileList1, // fileList2: fileList2, // upNumMax: res.data.thumb_url.length // }) let fileList3 = that.data.fileList3; for (var i = 0; i < res.data.thumb_url.length; i++) { fileList3.push({ url: res.data.thumb_url[i] }); } console.log(that.data); console.log(fileList3); that.setData({ fileList3: fileList3, infoArr: [], // fileList4: fileList3, upNumMax: res.data.thumb_url.length }); } that.setData({ 'cargon_data.title': res.data.title, 'cargon_data.sku': res.data.sku, 'imageUrl': res.data.thumb, 'cargon_data.thumb': res.data.thumb, 'cargon_data.price': res.data.price, 'cargon_data.market_price': res.data.market_price, 'cargon_data.cost_price': res.data.cost_price, 'cargon_data.stock': res.data.stock, 'cargon_data.thumb_url': res.data.thumb_url }); // 初始化分类选择 that.commodityInit(); } else { wx.showToast({ icon: 'none', title: res.msg, duration: 1500 }); } }, fail: function (res) { } }); }, commodityInit() { let selectedOptions3 = this.data.selectedOptions3; let sectionOptions = this.data.sectionOptions; let arr_2 = []; let arr_3 = []; let str = ''; for (let i = 0; i < sectionOptions.length; i++) { if (sectionOptions[i].id == selectedOptions3[0]) { this.setData({ 'multiIndex[0]': i }); str += sectionOptions[i].name; break; } } console.log(this.data.multiIndex, '分类的数组'); console.log(sectionOptions, '选中的项'); if (sectionOptions[this.data.multiIndex[0]].children.length > 0) { for (let k = 0; k < sectionOptions[this.data.multiIndex[0]].children.length; k++) { arr_2.push({ id: sectionOptions[this.data.multiIndex[0]].children[k].id, name: sectionOptions[this.data.multiIndex[0]].children[k].name }); } } this.setData({ 'objectMultiArray[1]': arr_2 }); if (sectionOptions[this.data.multiIndex[0]].children.length > 0) { for (let k = 0; k < sectionOptions[this.data.multiIndex[0]].children.length; k++) { if (sectionOptions[this.data.multiIndex[0]].children[k].id == selectedOptions3[1]) { this.setData({ 'multiIndex[1]': k }); str += '/' + sectionOptions[this.data.multiIndex[0]].children[k].name; break; } } } if ((sectionOptions[this.data.multiIndex[0]].children && sectionOptions[this.data.multiIndex[0]].children.length > 0) && (sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children && sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length > 0)) { for (let j = 0; j < sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length; j++) { arr_3.push({ id: sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[j].id, name: sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[j].name }); } } this.setData({ 'objectMultiArray[2]': arr_3 }); if ((sectionOptions[this.data.multiIndex[0]].children && sectionOptions[this.data.multiIndex[0]].children.length > 0) && (sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children && sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length > 0)) { for (let j = 0; j < sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children.length; j++) { if (sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[j].id == selectedOptions3[2]) { this.setData({ 'multiIndex[1]': j }); str += '/' + sectionOptions[this.data.multiIndex[0]].children[this.data.multiIndex[1]].children[j].name; break; } } } this.setData({ regional: str }); } } });