settledStore.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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.realname,
  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.realname,
  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. that.setData({
  163. [gridProductList]:selectGridProductList
  164. })
  165. var gridProductListSub = this.data.gridProductList;
  166. var gridList = [];
  167. gridProductListSub.forEach((d,index) => {
  168. gridList = gridList.concat(d)
  169. });
  170. var valuesGridList = `values.gridProductList`
  171. that.setData({
  172. [valuesGridList]:gridList
  173. })
  174. console.log( that.data.values)
  175. util.request(util.api.addDevice, that.data.values, "POST", false, true,app.globalData.token).then((res) => {
  176. if (res.code == 200) {
  177. wx.redirectTo({
  178. url: '/pages/device/index',
  179. })
  180. }else{
  181. util.toast(res.message)
  182. }
  183. }).catch((res) => {})
  184. } else {
  185. wx.showToast({
  186. title: checkRes,
  187. icon: "none"
  188. });
  189. }
  190. },
  191. formReset: function (e) {
  192. console.log("清空数据")
  193. },
  194. //选设备类型
  195. bindDeviceTypePickerChange: function (e) {
  196. var that = this;
  197. var item = that.data.deviceTypeList[e.detail.value]
  198. var gridSize = item.gridSize?item.gridSize:0
  199. var gridLists =[];//机子数量
  200. var tabs = [];
  201. for (let deviceNum = 0; deviceNum < item.deviceNums; deviceNum++) {//柜子数量
  202. var gridProductList =[];
  203. var tab = {};
  204. tab.name = '柜子'+deviceNum
  205. tabs.push(tab)
  206. for (let index = 0; index < gridSize; index++) {
  207. gridProductList.push({productGrid:index+1,deviceNum:deviceNum});
  208. }
  209. gridLists.push(gridProductList)
  210. }
  211. that.setData({
  212. ['values.deviceNums']: item.deviceNums,
  213. ['values.deviceTypeId']: item.id,
  214. ['values.deviceTypeName']: item.deviceName,
  215. ['gridProductList']:gridLists,
  216. isTabs:true,
  217. tabsWidth:100/tabs.length+'%',
  218. tabs:tabs,
  219. selectGridProductList:gridLists[that.data.currentTab]
  220. })
  221. },
  222. //选门店
  223. bindStorePickerChange: function (e) {
  224. var item = this.data.storeList[e.detail.value]
  225. this.setData({
  226. ['values.storeId']: item.id,
  227. ['values.storeName']: item.storeName
  228. })
  229. },
  230. //选方案
  231. bindSchemePickerChange: function (e) {
  232. var item = this.data.schemeList[e.detail.value]
  233. this.setData({
  234. ['values.schemeId']: item.id,
  235. ['values.schemeName']: item.schemeName
  236. })
  237. },
  238. //选设备品牌
  239. bindDictPickerChange: function (e) {
  240. var item = this.data.dictList[e.detail.value]
  241. this.setData({
  242. ['values.deviceBrand']: item.value,
  243. ['values.deviceBrandName']: item.text
  244. })
  245. },
  246. //选择代理
  247. bindAgentPickerChange: function (e) {
  248. var item = this.data.agentList[e.detail.value]
  249. this.setData({
  250. ['values.agentId']: item.id,
  251. ['values.agentName']: item.realname
  252. })
  253. },
  254. //前往选择商品
  255. toSelect(e) {
  256. if(this.data.values.agentId==null||this.data.values.agentId==''){
  257. util.toast('请选择代理商')
  258. return;
  259. }
  260. wx.navigateTo({
  261. url: '/pages/vendingMachine/select?index=' + e.currentTarget.dataset.index+'&agentId=' + this.data.values.agentId,
  262. })
  263. },
  264. //选择支付类别 数组拼装成逗号分割
  265. checkboxChange(e){
  266. var selectPayTypeList = e.detail.value;
  267. var payTypes = "";
  268. selectPayTypeList.forEach(payType => {
  269. payTypes+=payType+","
  270. });
  271. if(payTypes!=""){
  272. payTypes = payTypes.slice(0,payTypes.length-1);
  273. this.setData({
  274. 'values.payType': payTypes
  275. })
  276. }
  277. },
  278. number: function (e) {
  279. let value = this.validateNumber(e.detail.value)
  280. let inx = `selectGridProductList[${e.target.dataset.index}].${e.target.dataset.name}`
  281. this.setData({
  282. //parseInt将数字字符串转换成数字
  283. [inx]: parseInt(value)
  284. })
  285. },
  286. validateNumber(val) {
  287. //正则表达式指定字符串只能为数字
  288. return val.replace(/\D/g, '')
  289. },
  290. inputedit: function (e) {
  291. let _this = this;
  292. let dataset = e.currentTarget.dataset;
  293. let value = e.detail.value;
  294. let name = dataset.name;
  295. _this.data[name] = value;
  296. _this.setData({
  297. [name]: _this.data[name]
  298. });
  299. },
  300. //选择柜子
  301. selectDevice: function(e){
  302. var gridProductList = this.data.gridProductList[e.detail.index]
  303. this.setData({
  304. currentTab:e.detail.index,
  305. selectGridProductList:gridProductList
  306. });
  307. }
  308. })