settledStore.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. if(this.data.userInfo.mgvmRole==3){
  48. this.setData({
  49. ['values.agentId']: this.data.userInfo.id,
  50. ['values.agentName']: this.data.userInfo.username,
  51. })
  52. }
  53. },
  54. //商品方案列表
  55. bindSchemeList() {
  56. var that = this
  57. util.request(util.api.schemeList, {}, "GET", false, true, app.globalData.token).then((res) => {
  58. if (res.code == 200) {
  59. that.setData({
  60. schemeList: res.result
  61. })
  62. }
  63. }).catch((res) => {})
  64. },
  65. //设备类型列表
  66. deviceTypeList() {
  67. var that = this
  68. util.request(util.api.deviceTypeList, {}, "GET", false, true, app.globalData.token).then((res) => {
  69. if (res.code == 200) {
  70. that.setData({
  71. deviceTypeList: res.result
  72. })
  73. }
  74. }).catch((res) => {})
  75. },
  76. //门店列表
  77. bindStoreList() {
  78. var that = this
  79. util.request(util.api.storeList, {}, "GET", false, true,app.globalData.token).then((res) => {
  80. if (res.code == 200) {
  81. that.setData({
  82. storeList: res.result
  83. })
  84. }
  85. }).catch((res) => {})
  86. },
  87. //设备品牌
  88. getDictByCode() {
  89. var that = this
  90. util.request(util.api.getDictByCode, {code:"device_brand"}, "GET", false, true,app.globalData.token).then((res) => {
  91. if (res.code == 200) {
  92. that.setData({
  93. dictList: res.result
  94. })
  95. }
  96. }).catch((res) => {})
  97. },
  98. //代理商
  99. getAllAgentList() {
  100. var that = this
  101. util.request(util.api.allAgentList, {}, "GET", false, true,app.globalData.token).then((res) => {
  102. if (res.code == 200) {
  103. that.setData({
  104. agentList: res.result
  105. })
  106. }
  107. }).catch((res) => {})
  108. },
  109. formSubmit: function (e) {
  110. var that = this
  111. //表单规则
  112. let rules = [{
  113. name: "deviceSn",
  114. rule: ["required"], //可使用区间,此处主要测试功能
  115. msg: ["请输入设备编号"]
  116. }, {
  117. name: "deviceName",
  118. rule: ["required"], //可使用区间,此处主要测试功能
  119. msg: ["请输入设备名称"]
  120. }, {
  121. name: "deviceLocation",
  122. rule: ["required"],
  123. msg: ["请输入设备号"]
  124. }, {
  125. name: "schemeName",
  126. rule: ["required"],
  127. msg: ["请输入方案"]
  128. }, {
  129. name: "storeName",
  130. rule: ["required"],
  131. msg: ["请输入门店"]
  132. }, {
  133. name: "deviceTypeName",
  134. rule: ["required"],
  135. msg: ["请输入设备类型"]
  136. }, {
  137. name: "deviceBrandName",
  138. rule: ["required"],
  139. msg: ["请输入设备品牌"]
  140. }];
  141. if(this.data.userInfo.mgvmRole!=3){
  142. var agentRule = {
  143. name: "agentName",
  144. rule: ["required"],
  145. msg: ["请输入代理商"]
  146. }
  147. rules.push(agentRule)
  148. }else{
  149. this.setData({
  150. ['values.agentId']: this.data.userInfo.id,
  151. ['values.agentName']: this.data.userInfo.username,
  152. })
  153. }
  154. //进行表单检查
  155. let formData = e.detail.value;
  156. let checkRes = form.validation(formData, rules);
  157. if (!checkRes) {
  158. let currentTab = this.data.currentTab;
  159. //提交时设置当前选中柜子商品
  160. var gridProductList = `gridProductList[${currentTab}]`
  161. var selectGridProductList = this.data.selectGridProductList;
  162. var isShu = true;
  163. var selectProducts = 0
  164. selectGridProductList.forEach((d,index) => {
  165. if(d.productId!=null&&d.productId!=""){
  166. if(d.gridCount==null||d.gridCount==""){
  167. isShu = false;
  168. }
  169. if(d.stockCount==null||d.stockCount==""){
  170. isShu = false;
  171. }
  172. selectProducts=1;
  173. }
  174. });
  175. if(selectProducts==0){
  176. util.toast('至少选择一个商品');
  177. return;
  178. }
  179. if(!isShu){
  180. util.toast('商品库存与格子数量不许为空');
  181. return;
  182. }
  183. that.setData({
  184. [gridProductList]:selectGridProductList
  185. })
  186. var gridProductListSub = this.data.gridProductList;
  187. var gridList = [];
  188. gridProductListSub.forEach((d,index) => {
  189. gridList = gridList.concat(d)
  190. });
  191. console.log(gridList)
  192. var valuesGridList = `values.gridProductList`
  193. that.setData({
  194. [valuesGridList]:gridList
  195. })
  196. console.log( that.data.values)
  197. util.request(util.api.addDevice, that.data.values, "POST", false, true,app.globalData.token).then((res) => {
  198. if (res.code == 200) {
  199. wx.redirectTo({
  200. url: '/pages/device/index',
  201. })
  202. }else{
  203. util.toast(res.message)
  204. }
  205. }).catch((res) => {})
  206. } else {
  207. wx.showToast({
  208. title: checkRes,
  209. icon: "none"
  210. });
  211. }
  212. },
  213. formReset: function (e) {
  214. console.log("清空数据")
  215. },
  216. //选设备类型
  217. bindDeviceTypePickerChange: function (e) {
  218. var that = this;
  219. var item = that.data.deviceTypeList[e.detail.value]
  220. var gridSize = item.gridSize?item.gridSize:0
  221. var gridLists =[];//机子数量
  222. var tabs = [];
  223. for (let deviceNum = 0; deviceNum < item.deviceNums; deviceNum++) {//柜子数量
  224. var gridProductList =[];
  225. var tab = {};
  226. tab.name = '柜子'+deviceNum
  227. tabs.push(tab)
  228. for (let index = 0; index < gridSize; index++) {
  229. gridProductList.push({productGrid:index+1,deviceNum:deviceNum,gridCount:5});
  230. }
  231. gridLists.push(gridProductList)
  232. }
  233. that.setData({
  234. ['values.deviceNums']: item.deviceNums,
  235. ['values.deviceTypeId']: item.id,
  236. ['values.deviceTypeName']: item.deviceName,
  237. ['gridProductList']:gridLists,
  238. isTabs:true,
  239. tabsWidth:100/tabs.length+'%',
  240. tabs:tabs,
  241. selectGridProductList:gridLists[that.data.currentTab]
  242. })
  243. },
  244. //选门店
  245. bindStorePickerChange: function (e) {
  246. var item = this.data.storeList[e.detail.value]
  247. this.setData({
  248. ['values.storeId']: item.id,
  249. ['values.storeName']: item.storeName
  250. })
  251. },
  252. //选方案
  253. bindSchemePickerChange: function (e) {
  254. var item = this.data.schemeList[e.detail.value]
  255. this.setData({
  256. ['values.schemeId']: item.id,
  257. ['values.schemeName']: item.schemeName
  258. })
  259. },
  260. //选设备品牌
  261. bindDictPickerChange: function (e) {
  262. var item = this.data.dictList[e.detail.value]
  263. this.setData({
  264. ['values.deviceBrand']: item.value,
  265. ['values.deviceBrandName']: item.text
  266. })
  267. },
  268. //选择代理
  269. bindAgentPickerChange: function (e) {
  270. var item = this.data.agentList[e.detail.value]
  271. this.setData({
  272. ['values.agentId']: item.id,
  273. ['values.agentName']: item.username
  274. })
  275. },
  276. //前往选择商品
  277. toSelect(e) {
  278. if(this.data.values.agentId==null||this.data.values.agentId==''){
  279. util.toast('请选择代理商')
  280. return;
  281. }
  282. wx.navigateTo({
  283. url: '/pages/vendingMachine/select?index=' + e.currentTarget.dataset.index+'&agentId=' + this.data.values.agentId,
  284. })
  285. },
  286. //选择支付类别 数组拼装成逗号分割
  287. checkboxChange(e){
  288. var selectPayTypeList = e.detail.value;
  289. var payTypes = "";
  290. selectPayTypeList.forEach(payType => {
  291. payTypes+=payType+","
  292. });
  293. if(payTypes!=""){
  294. payTypes = payTypes.slice(0,payTypes.length-1);
  295. this.setData({
  296. 'values.payType': payTypes
  297. })
  298. }
  299. },
  300. number: function (e) {
  301. let value = this.validateNumber(e.detail.value)
  302. let inx = `selectGridProductList[${e.target.dataset.index}].${e.target.dataset.name}`
  303. if(parseInt(value)>5||parseInt(value)<=0){
  304. util.toast('可存放格子数量为1-5')
  305. value = 5;
  306. }
  307. this.setData({
  308. //parseInt将数字字符串转换成数字
  309. [inx]: parseInt(value)
  310. })
  311. },
  312. validateNumber(val) {
  313. //正则表达式指定字符串只能为数字
  314. return val.replace(/\D/g, '')
  315. },
  316. inputedit: function (e) {
  317. let _this = this;
  318. let dataset = e.currentTarget.dataset;
  319. let value = e.detail.value;
  320. let name = dataset.name;
  321. _this.data[name] = value;
  322. _this.setData({
  323. [name]: _this.data[name]
  324. });
  325. },
  326. //选择柜子
  327. selectDevice: function(e){
  328. var gridProductList = this.data.gridProductList[e.detail.index]
  329. this.setData({
  330. currentTab:e.detail.index,
  331. selectGridProductList:gridProductList
  332. });
  333. }
  334. })