const util = require('../../utils/util.js'); const form = require('../../utils/formValidation'); const app = getApp(); Page({ /** * 页面的初始数据 */ data: { 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:[] } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.bindStoreList(); this.bindSchemeList(); this.deviceTypeList(); this.getDictByCode(); this.getAllAgentList(); this.setData({ userInfo: app.globalData.userInfo }) }, //商品方案列表 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) { console.log(this.data.values) 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) { 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 item = this.data.deviceTypeList[e.detail.value] var gridSize = item.gridSize?item.gridSize:0 var gridProductList =[]; for (let index = 0; index < gridSize; index++) { gridProductList.push({productGrid:index+1}); } console.log(gridProductList) this.setData({ ['values.deviceTypeId']: item.id, ['values.deviceTypeName']: item.deviceName, ['values.gridProductList']:gridProductList }) }, //选门店 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) { wx.navigateTo({ url: '/pages/vendingMachine/select?index=' + e.currentTarget.dataset.index, }) }, //选择支付类别 数组拼装成逗号分割 checkboxChange(e){ console.log(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) { console.log(e) let value = this.validateNumber(e.detail.value) let inx = `values.gridProductList[${e.target.dataset.index}].${e.target.dataset.name}` 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] }); }, })