settledStore.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. const util = require('../../utils/util.js');
  2. const form = require('../../utils/formValidation');
  3. const app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. tabs: [],
  10. tabsWidth:'100%',
  11. currentTab:0,
  12. isTabs:false,
  13. selectGridProductList:[],//当前选中得柜子
  14. userInfo: {},
  15. storeList: [],
  16. schemeList:[],
  17. deviceTypeList:[],
  18. dictList:[],
  19. agentList:[],
  20. payTypeList:[{name:'微信',value:'wx'},{name:'支付宝',value:'zfb'},{name:'现金',value:'xj'}],
  21. values: {
  22. deviceSn:'',
  23. deviceName:'',
  24. deviceLocation: '',
  25. schemeId: '',
  26. storeId: '',
  27. deviceTypeId: '',
  28. deviceBrand: '',
  29. agentId: '',
  30. payType:'wx,zfb,xj',//默认选择所有支付方式
  31. gridProductList:[]
  32. },
  33. gridProductList:[]//商品数据
  34. },
  35. /**
  36. * 生命周期函数--监听页面加载
  37. */
  38. onLoad: function (options) {
  39. this.bindStoreList();
  40. this.bindSchemeList();
  41. this.deviceTypeList();
  42. this.getDictByCode();
  43. this.getAllAgentList();
  44. this.setData({
  45. userInfo: app.globalData.userInfo
  46. })
  47. },
  48. //商品方案列表
  49. bindSchemeList() {
  50. var that = this
  51. util.request(util.api.schemeList, {}, "GET", false, true, app.globalData.token).then((res) => {
  52. if (res.code == 200) {
  53. that.setData({
  54. schemeList: res.result
  55. })
  56. }
  57. }).catch((res) => {})
  58. },
  59. //设备类型列表
  60. deviceTypeList() {
  61. var that = this
  62. util.request(util.api.deviceTypeList, {}, "GET", false, true, app.globalData.token).then((res) => {
  63. if (res.code == 200) {
  64. that.setData({
  65. deviceTypeList: res.result
  66. })
  67. }
  68. }).catch((res) => {})
  69. },
  70. //门店列表
  71. bindStoreList() {
  72. var that = this
  73. util.request(util.api.storeList, {}, "GET", false, true,app.globalData.token).then((res) => {
  74. if (res.code == 200) {
  75. that.setData({
  76. storeList: res.result
  77. })
  78. }
  79. }).catch((res) => {})
  80. },
  81. //设备品牌
  82. getDictByCode() {
  83. var that = this
  84. util.request(util.api.getDictByCode, {code:"device_brand"}, "GET", false, true,app.globalData.token).then((res) => {
  85. if (res.code == 200) {
  86. that.setData({
  87. dictList: res.result
  88. })
  89. }
  90. }).catch((res) => {})
  91. },
  92. //代理商
  93. getAllAgentList() {
  94. var that = this
  95. util.request(util.api.allAgentList, {}, "GET", false, true,app.globalData.token).then((res) => {
  96. if (res.code == 200) {
  97. that.setData({
  98. agentList: res.result
  99. })
  100. }
  101. }).catch((res) => {})
  102. },
  103. formSubmit: function (e) {
  104. var that = this
  105. //表单规则
  106. let rules = [{
  107. name: "deviceSn",
  108. rule: ["required"], //可使用区间,此处主要测试功能
  109. msg: ["请输入设备编号"]
  110. }, {
  111. name: "deviceName",
  112. rule: ["required"], //可使用区间,此处主要测试功能
  113. msg: ["请输入设备名称"]
  114. }, {
  115. name: "deviceLocation",
  116. rule: ["required"],
  117. msg: ["请输入设备号"]
  118. }, {
  119. name: "schemeName",
  120. rule: ["required"],
  121. msg: ["请输入方案"]
  122. }, {
  123. name: "storeName",
  124. rule: ["required"],
  125. msg: ["请输入门店"]
  126. }, {
  127. name: "deviceTypeName",
  128. rule: ["required"],
  129. msg: ["请输入设备类型"]
  130. }, {
  131. name: "deviceBrandName",
  132. rule: ["required"],
  133. msg: ["请输入设备品牌"]
  134. }];
  135. if(this.data.userInfo.mgvmRole!=3){
  136. var agentRule = {
  137. name: "agentName",
  138. rule: ["required"],
  139. msg: ["请输入代理商"]
  140. }
  141. rules.push(agentRule)
  142. }else{
  143. this.setData({
  144. ['values.agentId']: this.data.userInfo.id,
  145. ['values.agentName']: this.data.userInfo.username,
  146. })
  147. }
  148. //进行表单检查
  149. let formData = e.detail.value;
  150. let checkRes = form.validation(formData, rules);
  151. if (!checkRes) {
  152. let currentTab = this.data.currentTab;
  153. //提交时设置当前选中柜子商品
  154. var gridProductList = `gridProductList[${currentTab}]`
  155. var selectGridProductList = this.data.selectGridProductList;
  156. that.setData({
  157. [gridProductList]:selectGridProductList
  158. })
  159. var gridProductListSub = this.data.gridProductList;
  160. var gridList = [];
  161. gridProductListSub.forEach((d,index) => {
  162. gridList = gridList.concat(d)
  163. });
  164. var valuesGridList = `values.gridProductList`
  165. that.setData({
  166. [valuesGridList]:gridList
  167. })
  168. console.log( that.data.values)
  169. util.request(util.api.addDevice, that.data.values, "POST", false, true,app.globalData.token).then((res) => {
  170. if (res.code == 200) {
  171. wx.redirectTo({
  172. url: '/pages/device/index',
  173. })
  174. }else{
  175. util.toast(res.message)
  176. }
  177. }).catch((res) => {})
  178. } else {
  179. wx.showToast({
  180. title: checkRes,
  181. icon: "none"
  182. });
  183. }
  184. },
  185. formReset: function (e) {
  186. console.log("清空数据")
  187. },
  188. //选设备类型
  189. bindDeviceTypePickerChange: function (e) {
  190. var that = this;
  191. var item = that.data.deviceTypeList[e.detail.value]
  192. var gridSize = item.gridSize?item.gridSize:0
  193. var gridLists =[];//机子数量
  194. var tabs = [];
  195. for (let deviceNum = 0; deviceNum < item.deviceNums; deviceNum++) {//柜子数量
  196. var gridProductList =[];
  197. var tab = {};
  198. tab.name = '柜子'+deviceNum
  199. tabs.push(tab)
  200. for (let index = 0; index < gridSize; index++) {
  201. gridProductList.push({productGrid:index+1,deviceNum:deviceNum});
  202. }
  203. gridLists.push(gridProductList)
  204. }
  205. that.setData({
  206. ['values.deviceTypeId']: item.id,
  207. ['values.deviceTypeName']: item.deviceName,
  208. ['gridProductList']:gridLists,
  209. isTabs:true,
  210. tabsWidth:100/tabs.length+'%',
  211. tabs:tabs,
  212. selectGridProductList:gridLists[that.data.currentTab]
  213. })
  214. },
  215. //选门店
  216. bindStorePickerChange: function (e) {
  217. var item = this.data.storeList[e.detail.value]
  218. this.setData({
  219. ['values.storeId']: item.id,
  220. ['values.storeName']: item.storeName
  221. })
  222. },
  223. //选方案
  224. bindSchemePickerChange: function (e) {
  225. var item = this.data.schemeList[e.detail.value]
  226. this.setData({
  227. ['values.schemeId']: item.id,
  228. ['values.schemeName']: item.schemeName
  229. })
  230. },
  231. //选设备品牌
  232. bindDictPickerChange: function (e) {
  233. var item = this.data.dictList[e.detail.value]
  234. this.setData({
  235. ['values.deviceBrand']: item.value,
  236. ['values.deviceBrandName']: item.text
  237. })
  238. },
  239. //选择代理
  240. bindAgentPickerChange: function (e) {
  241. var item = this.data.agentList[e.detail.value]
  242. this.setData({
  243. ['values.agentId']: item.id,
  244. ['values.agentName']: item.username
  245. })
  246. },
  247. //前往选择商品
  248. toSelect(e) {
  249. wx.navigateTo({
  250. url: '/pages/vendingMachine/select?index=' + e.currentTarget.dataset.index,
  251. })
  252. },
  253. //选择支付类别 数组拼装成逗号分割
  254. checkboxChange(e){
  255. var selectPayTypeList = e.detail.value;
  256. var payTypes = "";
  257. selectPayTypeList.forEach(payType => {
  258. payTypes+=payType+","
  259. });
  260. if(payTypes!=""){
  261. payTypes = payTypes.slice(0,payTypes.length-1);
  262. this.setData({
  263. 'values.payType': payTypes
  264. })
  265. }
  266. },
  267. number: function (e) {
  268. let value = this.validateNumber(e.detail.value)
  269. let inx = `selectGridProductList[${e.target.dataset.index}].${e.target.dataset.name}`
  270. this.setData({
  271. //parseInt将数字字符串转换成数字
  272. [inx]: parseInt(value)
  273. })
  274. },
  275. validateNumber(val) {
  276. //正则表达式指定字符串只能为数字
  277. return val.replace(/\D/g, '')
  278. },
  279. inputedit: function (e) {
  280. let _this = this;
  281. let dataset = e.currentTarget.dataset;
  282. let value = e.detail.value;
  283. let name = dataset.name;
  284. _this.data[name] = value;
  285. _this.setData({
  286. [name]: _this.data[name]
  287. });
  288. },
  289. //选择柜子
  290. selectDevice: function(e){
  291. var gridProductList = this.data.gridProductList[e.detail.index]
  292. this.setData({
  293. currentTab:e.detail.index,
  294. selectGridProductList:gridProductList
  295. });
  296. }
  297. })