import { ApiModel } from "../../utils/api"; import { TestVerify } from "../../utils/verify"; const _Http = new ApiModel(); const _Verify = new TestVerify(); Component({ /** * 组件的属性列表 */ properties: { productData: { type: Object } }, /** * 组件的初始数据 */ data: { newlyBuilt: true, //是否为新增产品,非新增会修改为false 新增请求修改接口后改为false 如果页面关闭为true 会删除该商品 productName: "", //产品名称 serialNumber: "", //产品编号 fileList: [], //图片列表 productLabel: "", //产品标签 productExplain: "", //产品说明 checked: Boolean, //是否上架 /* 必填项目 */ inputVerify: { productName: false, //产品名称 serialNumber: false, //身份 }, tagents_productid: 0, //产品ID fileListLength: 0, //记录进入图片数量 }, lifetimes: { ready: function () { if (this.data.productData) { const { productData } = this.data let checked = Boolean; if (productData.fisonsale == 1) { checked = true; } else { checked = false; } this.manageImage(productData.attinfos) /* */ this.setData({ productName: productData.fprodname, serialNumber: productData.fprodnum, productLabel: productData.ftag, tagents_productid: productData.tagents_productid, checked: checked, newlyBuilt: false, }) } else { /* 新增产品 */ _Http.basic({ "accesstoken": wx.getStorageSync('token'), "classname": "customer.products.products", "method": "insertOrModifyProducts", "content": { "tagents_productid": 0 } }).then(s => { if (s.msg != "成功") return; this.setData({ tagents_productid: s.data[0].tagents_productid, checked: true }) }) } }, detached() { //新增账号未保存会删除账号 if (!this.data.newlyBuilt) return; //删除账号 _Http.basic({ "accesstoken": wx.getStorageSync('token'), "classname": "customer.products.products", "method": "deleteProducts", "content": { "tagents_productid": this.data.tagents_productid } }).then(s => { console.log(s) }) } }, methods: { /* 获焦恢复 */ inputFocus(e) { let inputVerify = this.data.inputVerify; const { name } = e.currentTarget.dataset; inputVerify[name] = false; this.setData({ inputVerify }) }, /* 失焦验证 */ inputBlur(e) { let inputVerify = this.data.inputVerify; const { name } = e.currentTarget.dataset; /* 手机号验证 */ if (name == 'cellPhoneNum' && !_Verify.phoneNumber(this.data.cellPhoneNum, true)) { inputVerify[name] = true; this.setData({ inputVerify }) } /* 验证 */ if (e.detail.value != "") return; inputVerify[name] = true; this.setData({ inputVerify }) }, /* 验证 */ submitVerify() { let inputVerify = this.data.inputVerify, nextStep = true; if (!_Verify.required(this.data.productName)) { inputVerify.productName = true; nextStep = false; } if(this.data.fileList.length<1){ nextStep = false; } if (!_Verify.required(this.data.serialNumber)) { inputVerify.serialNumber = true nextStep = false; } if (!nextStep) { this.setData({ inputVerify }) } return nextStep; }, /* 提交 */ submit() { /* 验证必填项 */ if (!this.submitVerify()) return wx.showToast({ title: '请检查输入框内容!', icon: "none" }) /* 发送请求 */ _Http.basic({ "accesstoken": wx.getStorageSync('token'), "classname": "customer.products.products", "method": "insertOrModifyProducts", "content": { "tagents_productid": this.data.tagents_productid, "fprodnum": this.data.serialNumber, "fprodname": this.data.productName, // "fprice": 100, // "fprodclassname": "墙布", "fintroduction": this.data.productExplain, "ftag": this.data.productLabel, // "fnotes": "备注" } }).then(s => { if (s.msg != "成功") return; let putaway = 1; if (this.data.checked) { putaway = 1 } else { putaway = 0 } /* 设置是否上架 */ _Http.basic({ "accesstoken": wx.getStorageSync('token'), "classname": "customer.products.products", "method": "updatesalestatus", "content": { "productlist": [{ "tagents_productid": this.data.tagents_productid, "fisonsale": putaway }] } }).then(s => { console.log(s) }) this.setData({ newlyBuilt: false }) wx.showToast({ title: s.msg, }) setTimeout(() => { wx.navigateBack({ delta: 1 }) }, 500) }) }, /* 是否上架 */ switchOnChange() { this.setData({ checked: !this.data.checked }) }, /* 上传图片 */ afterRead(event) { // 初始化数据 this.setData({ logoErrTips: "" }) var that = this const { file } = event.detail; var index = file.url.lastIndexOf("."); var ext = file.url.substr(index + 1); const fileName = file.url.split('/'), name = fileName[fileName.length - 1]; //验证文件格式大小 const Uploader = _Verify.verifyUploader({ file }); if (Uploader == "发送请求") { wx.getFileSystemManager().readFile({ filePath: file.url, // encoding:'utf-8', success: result => { //返回临时文件路径 const fileData = result.data _Http.basic({ "accesstoken": wx.getStorageSync('token'), "classname": "system.system.docManage", "method": "getFileName", "content": { "filename": name, "filetype": ext, "ownertable": "tagents_product", "ownerid": that.data.tagents_productid, "ftype": "default" } }).then(res => { that.uploadFile(res, fileData) }).catch(err => { console.log(err) }) }, fail: console.error }) } else { this.setData({ logoErrTips: Uploader }) } }, /* 上传成功反馈 */ uploadFile(res, data) { let that = this; wx.request({ url: res.data.obsuploadurl, method: "PUT", data: data, header: { 'content-type': 'application/octet-stream' // 默认值 }, success() { _Http.basic({ "accesstoken": wx.getStorageSync('token'), "classname": "system.system.docManage", "method": "uploadSuccess", "content": { "obsfilename": res.data.obsfilename } }).then(res => { if (res.msg != "成功") return; let data = { url: res.data[0].fobsurl, ownerid: res.data[0].ownerid, tattachmentid: res.data[0].tattachmentid }; let fileList = that.data.fileList fileList.push(data) that.setData({ fileList }) }).catch(err => { console.log(err) }) } }) }, /* 加工处理图片列表 */ manageImage(data) { let fileList = []; for (let i = 0; i < data.length && i < 3; i++) { let a1 = { url: data[i].fobsurl, ownerid: data[i].ownerid, tattachmentid: data[i].tattachmentid } fileList.push(a1) } this.setData({ fileList, fileListLength: data.length }) }, /* 删除图片 */ imageDelete(e) { const index = e.detail.index _Http.basic({ "accesstoken": wx.getStorageSync('token'), "classname": "system.system.docManage", "method": "deleteDoc", "content": { "ownertable": "tagents_product", "ownerid": this.data.fileList[index].ownerid, "tattachmentid": this.data.fileList[index].tattachmentid } }).then(s => { if (s.msg != "成功") return; this.manageImage(s.data) }) }, } })