const util = require('../../utils/util.js'); const form = require('../../utils/formValidation'); const app = getApp(); Page({ /** * 页面的初始数据 */ data: { tabs: [], tabsWidth:'100%', currentTab:0, isTabs:false, selectGridProductList:[],//当前选中得柜子 userInfo: {}, storeList: [], schemeList:[], deviceTypeList:[], dictList:[], agentList:[], payTypeList:[{name:'微信',value:'wx'},{name:'支付宝',value:'zfb'},{name:'现金',value:'xj'}], values: { deviceSn:'', deviceName:'', deviceLocation: '', schemeId: '', storeId: '', deviceTypeId: '', deviceBrand: '', agentId: '', payType:'wx,zfb,xj',//默认选择所有支付方式 gridProductList:[] }, gridProductList:[]//商品数据 }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.bindStoreList(); this.bindSchemeList(); this.deviceTypeList(); this.getDictByCode(); this.getAllAgentList(); this.setData({ userInfo: app.globalData.userInfo }) if(this.data.userInfo.mgvmRole==3){ this.setData({ ['values.agentId']: this.data.userInfo.id, ['values.agentName']: this.data.userInfo.username, }) } }, //商品方案列表 bindSchemeList() { var that = this util.request(util.api.schemeList, {}, "GET", false, true, app.globalData.token).then((res) => { if (res.code == 200) { that.setData({ schemeList: res.result }) } }).catch((res) => {}) }, //设备类型列表 deviceTypeList() { var that = this util.request(util.api.deviceTypeList, {}, "GET", false, true, app.globalData.token).then((res) => { if (res.code == 200) { that.setData({ deviceTypeList: res.result }) } }).catch((res) => {}) }, //门店列表 bindStoreList() { var that = this util.request(util.api.storeList, {}, "GET", false, true,app.globalData.token).then((res) => { if (res.code == 200) { that.setData({ storeList: res.result }) } }).catch((res) => {}) }, //设备品牌 getDictByCode() { var that = this util.request(util.api.getDictByCode, {code:"device_brand"}, "GET", false, true,app.globalData.token).then((res) => { if (res.code == 200) { that.setData({ dictList: res.result }) } }).catch((res) => {}) }, //代理商 getAllAgentList() { var that = this util.request(util.api.allAgentList, {}, "GET", false, true,app.globalData.token).then((res) => { if (res.code == 200) { that.setData({ agentList: res.result }) } }).catch((res) => {}) }, formSubmit: function (e) { var that = this //表单规则 let rules = [{ name: "deviceSn", rule: ["required"], //可使用区间,此处主要测试功能 msg: ["请输入设备编号"] }, { name: "deviceName", rule: ["required"], //可使用区间,此处主要测试功能 msg: ["请输入设备名称"] }, { name: "deviceLocation", rule: ["required"], msg: ["请输入设备号"] }, { name: "schemeName", rule: ["required"], msg: ["请输入方案"] }, { name: "storeName", rule: ["required"], msg: ["请输入门店"] }, { name: "deviceTypeName", rule: ["required"], msg: ["请输入设备类型"] }, { name: "deviceBrandName", rule: ["required"], msg: ["请输入设备品牌"] }]; if(this.data.userInfo.mgvmRole!=3){ var agentRule = { name: "agentName", rule: ["required"], msg: ["请输入代理商"] } rules.push(agentRule) }else{ this.setData({ ['values.agentId']: this.data.userInfo.id, ['values.agentName']: this.data.userInfo.username, }) } //进行表单检查 let formData = e.detail.value; let checkRes = form.validation(formData, rules); if (!checkRes) { let currentTab = this.data.currentTab; //提交时设置当前选中柜子商品 var gridProductList = `gridProductList[${currentTab}]` var selectGridProductList = this.data.selectGridProductList; var isShu = true; var selectProducts = 0 selectGridProductList.forEach((d,index) => { if(d.productId!=null&&d.productId!=""){ if(d.gridCount==null||d.gridCount==""){ isShu = false; } if(d.stockCount==null||d.stockCount==""){ isShu = false; } selectProducts=1; } }); if(selectProducts==0){ util.toast('至少选择一个商品'); return; } if(!isShu){ util.toast('商品库存与格子数量不许为空'); return; } that.setData({ [gridProductList]:selectGridProductList }) var gridProductListSub = this.data.gridProductList; var gridList = []; gridProductListSub.forEach((d,index) => { gridList = gridList.concat(d) }); console.log(gridList) var valuesGridList = `values.gridProductList` that.setData({ [valuesGridList]:gridList }) console.log( that.data.values) util.request(util.api.addDevice, that.data.values, "POST", false, true,app.globalData.token).then((res) => { if (res.code == 200) { wx.redirectTo({ url: '/pages/device/index', }) }else{ util.toast(res.message) } }).catch((res) => {}) } else { wx.showToast({ title: checkRes, icon: "none" }); } }, formReset: function (e) { console.log("清空数据") }, //选设备类型 bindDeviceTypePickerChange: function (e) { var that = this; var item = that.data.deviceTypeList[e.detail.value] var gridSize = item.gridSize?item.gridSize:0 var gridLists =[];//机子数量 var tabs = []; for (let deviceNum = 0; deviceNum < item.deviceNums; deviceNum++) {//柜子数量 var gridProductList =[]; var tab = {}; tab.name = '柜子'+deviceNum tabs.push(tab) for (let index = 0; index < gridSize; index++) { gridProductList.push({productGrid:index+1,deviceNum:deviceNum}); } gridLists.push(gridProductList) } that.setData({ ['values.deviceNums']: item.deviceNums, ['values.deviceTypeId']: item.id, ['values.deviceTypeName']: item.deviceName, ['gridProductList']:gridLists, isTabs:true, tabsWidth:100/tabs.length+'%', tabs:tabs, selectGridProductList:gridLists[that.data.currentTab] }) }, //选门店 bindStorePickerChange: function (e) { var item = this.data.storeList[e.detail.value] this.setData({ ['values.storeId']: item.id, ['values.storeName']: item.storeName }) }, //选方案 bindSchemePickerChange: function (e) { var item = this.data.schemeList[e.detail.value] this.setData({ ['values.schemeId']: item.id, ['values.schemeName']: item.schemeName }) }, //选设备品牌 bindDictPickerChange: function (e) { var item = this.data.dictList[e.detail.value] this.setData({ ['values.deviceBrand']: item.value, ['values.deviceBrandName']: item.text }) }, //选择代理 bindAgentPickerChange: function (e) { var item = this.data.agentList[e.detail.value] this.setData({ ['values.agentId']: item.id, ['values.agentName']: item.username }) }, //前往选择商品 toSelect(e) { if(this.data.values.agentId==null||this.data.values.agentId==''){ util.toast('请选择代理商') return; } wx.navigateTo({ url: '/pages/vendingMachine/select?index=' + e.currentTarget.dataset.index+'&agentId=' + this.data.values.agentId, }) }, //选择支付类别 数组拼装成逗号分割 checkboxChange(e){ var selectPayTypeList = e.detail.value; var payTypes = ""; selectPayTypeList.forEach(payType => { payTypes+=payType+"," }); if(payTypes!=""){ payTypes = payTypes.slice(0,payTypes.length-1); this.setData({ 'values.payType': payTypes }) } }, number: function (e) { let value = this.validateNumber(e.detail.value) let inx = `selectGridProductList[${e.target.dataset.index}].${e.target.dataset.name}` if(parseInt(value)>5||parseInt(value)<=0){ util.toast('可存放格子数量为1-5') value = 5; } this.setData({ //parseInt将数字字符串转换成数字 [inx]: parseInt(value) }) }, validateNumber(val) { //正则表达式指定字符串只能为数字 return val.replace(/\D/g, '') }, inputedit: function (e) { let _this = this; let dataset = e.currentTarget.dataset; let value = e.detail.value; let name = dataset.name; _this.data[name] = value; _this.setData({ [name]: _this.data[name] }); }, //选择柜子 selectDevice: function(e){ var gridProductList = this.data.gridProductList[e.detail.index] this.setData({ currentTab:e.detail.index, selectGridProductList:gridProductList }); } })