form.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. const util = require('../../utils/util')
  2. const form = require('../../utils/formValidation')
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. isEdit: false,
  10. storeList: [],
  11. agentList:[],
  12. userInfo: {},
  13. values: {
  14. storeId: '',
  15. storeName: '',
  16. schemeName: '',
  17. schemeBillEntryList: []
  18. },
  19. id:''
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad: function (options) {
  25. var that = this
  26. if (options && options.id) {
  27. that.setData({
  28. isEdit: true,
  29. id:options.id
  30. })
  31. that.bindScheme()
  32. }
  33. wx.setNavigationBarTitle({
  34. title: that.data.isEdit ? '方案编辑' : '新增方案'
  35. })
  36. this.setData({
  37. userInfo: app.globalData.userInfo
  38. })
  39. this.getAllAgentList();
  40. if(this.data.userInfo.mgvmRole==3){
  41. this.setData({
  42. ['values.agentId']: this.data.userInfo.id,
  43. ['values.agentName']: this.data.userInfo.username,
  44. })
  45. }
  46. this.bindStoreList()
  47. },
  48. bindStorePickerChange: function (e) {
  49. var item = this.data.storeList[e.detail.value]
  50. this.setData({
  51. ['values.storeId']: item.id,
  52. ['values.storeName']: item.storeName
  53. })
  54. },
  55. bindAgentPickerChange: function (e) {
  56. var item = this.data.agentList[e.detail.value]
  57. this.setData({
  58. ['values.agentId']: item.id,
  59. ['values.agentName']: item.username
  60. })
  61. this.bindStoreList()
  62. },
  63. formSubmit: function (e) {
  64. console.log(this.data.values.schemeBillEntryList)
  65. var that = this
  66. //表单规则
  67. let rules = [{
  68. name: "schemeName",
  69. rule: ["required"], //可使用区间,此处主要测试功能
  70. msg: ["请输入方案名称"]
  71. },{
  72. name: "storeName",
  73. rule: ["required"], //可使用区间,此处主要测试功能
  74. msg: ["请输入门店"]
  75. }];
  76. if(this.data.userInfo.mgvmRole!=3){
  77. var agentRule = {
  78. name: "agentName",
  79. rule: ["required"],
  80. msg: ["请输入代理商"]
  81. }
  82. rules.push(agentRule)
  83. }else{
  84. this.setData({
  85. ['values.agentId']: this.data.userInfo.id,
  86. ['values.agentName']: this.data.userInfo.username,
  87. })
  88. }
  89. //进行表单检查
  90. let formData = e.detail.value;
  91. let checkRes = form.validation(formData, rules);
  92. let schemeBillEntryList = this.data.values.schemeBillEntryList;
  93. var schemeProduct = [];
  94. var productCount = 0;
  95. for (let i = 0; i < schemeBillEntryList.length; i++) {
  96. if(schemeBillEntryList[i].productId!=null&&schemeBillEntryList[i].productId!=""){
  97. schemeProduct.push(schemeBillEntryList[i])
  98. productCount+=1;
  99. if(schemeBillEntryList[i].areaDivide<0||schemeBillEntryList[i].storeDivide==null){
  100. util.toast('请输入代理分成')
  101. return;
  102. }
  103. if(schemeBillEntryList[i].areaDivide<0||schemeBillEntryList[i].areaDivide>100){
  104. util.toast('代理分成必须是0-100的分成')
  105. return;
  106. }
  107. if(schemeBillEntryList[i].areaDivide<0||schemeBillEntryList[i].storeDivide==null){
  108. util.toast('请输入门店分成')
  109. return;
  110. }
  111. if(schemeBillEntryList[i].storeDivide<0||schemeBillEntryList[i].storeDivide>100){
  112. util.toast('门店分成必须是0-100的分成')
  113. return;
  114. }
  115. if(schemeBillEntryList[i].storeDivide+schemeBillEntryList[i].areaDivide<100||
  116. schemeBillEntryList[i].storeDivide+schemeBillEntryList[i].areaDivide>100){
  117. util.toast('分成之和应为100%')
  118. return;
  119. }
  120. }
  121. }
  122. return
  123. if(productCount==0){
  124. util.toast('请选择至少一个商品!')
  125. return;
  126. }
  127. if (!checkRes) {
  128. if(!that.data.isEdit){
  129. this.setData({
  130. ["values.schemeBillEntryList"]: schemeProduct
  131. })
  132. util.request(util.api.addScheme, that.data.values, "POST", false, true, app.globalData.token).then((res) => {
  133. if (res.code == 200) {
  134. // wx.navigateBack()
  135. wx.navigateTo({
  136. url: '/pages/scheme/index',
  137. })
  138. }
  139. }).catch((res) => {})
  140. }else{
  141. util.request(util.api.editScheme, that.data.values, "POST", false, true, app.globalData.token).then((res) => {
  142. if (res.code == 200) {
  143. wx.showToast({
  144. title: '编辑成功!',
  145. })
  146. setTimeout(() => {
  147. wx.navigateBack()
  148. }, 500);
  149. // wx.navigateBack()
  150. }
  151. }).catch((res) => {})
  152. }
  153. } else {
  154. wx.showToast({
  155. title: checkRes,
  156. icon: "none"
  157. });
  158. }
  159. },
  160. formReset: function (e) {
  161. console.log("清空数据")
  162. },
  163. inputedit: function (e) {
  164. let _this = this;
  165. let dataset = e.currentTarget.dataset;
  166. let value = e.detail.value;
  167. let name = dataset.name;
  168. _this.data[name] = value;
  169. _this.setData({
  170. [name]: _this.data[name]
  171. });
  172. },
  173. inputeditRow: function (e) {
  174. let that = this;
  175. let dataset = e.currentTarget.dataset;
  176. let value = e.detail.value;
  177. let name = dataset.name;
  178. var list = that.data.values.schemeBillEntryList
  179. var index = dataset.index;
  180. var itemProduct = dataset.item
  181. value = parseFloat(value)
  182. if(!value){
  183. value = 0.00;
  184. }
  185. console.log(name)
  186. if(name=="storeDivide"){
  187. itemProduct.storeDivide = value
  188. }else if(name=="areaDivide"){
  189. itemProduct.areaDivide = value
  190. }
  191. list.splice(index,1,itemProduct)
  192. that.setData({
  193. ["values.schemeBillEntryList"]:list
  194. })
  195. console.log(this.data.values.schemeBillEntryList)
  196. },
  197. bindStoreList() {
  198. var that = this
  199. util.request(util.api.storeList, {agentId:that.data.values.agentId}, "GET", false, true, app.globalData.token).then((res) => {
  200. if (res.code == 200) {
  201. that.setData({
  202. storeList: res.result
  203. })
  204. }
  205. }).catch((res) => {})
  206. },
  207. //代理商
  208. getAllAgentList() {
  209. var that = this
  210. util.request(util.api.allAgentList, {}, "GET", false, true,app.globalData.token).then((res) => {
  211. if (res.code == 200) {
  212. that.setData({
  213. agentList: res.result
  214. })
  215. }
  216. }).catch((res) => {})
  217. },
  218. toSelect(e) {
  219. wx.navigateTo({
  220. url: '/pages/scheme/select?index=' + e.currentTarget.dataset.index,
  221. })
  222. },
  223. bindScheme(){
  224. var that = this
  225. util.request(util.api.queryBySchemeId, {
  226. id: that.data.id
  227. }, "GET", true, true, app.globalData.token).then((res) => {
  228. if (res.code == 200) {
  229. that.setData({
  230. values: res.result,
  231. })
  232. }
  233. }).catch((res) => {})
  234. },
  235. deleteScheme(e){
  236. let that = this;
  237. util.request(util.api.deleteSchemeById, {'id':that.data.id}, "POST", true, true, app.globalData.token).then((res) => {
  238. if(res.code == 200){
  239. wx.navigateTo({
  240. url: '/pages/scheme/index',
  241. })
  242. }
  243. }).catch((res) => {})
  244. },
  245. //添加格子
  246. addGrid(){
  247. var schemeBillEntryList =this.data.values.schemeBillEntryList;
  248. var product = {productName:'请选择商品',gridNum:schemeBillEntryList.length+1};
  249. schemeBillEntryList.push(product)
  250. this.setData({
  251. ["values.schemeBillEntryList"]: schemeBillEntryList
  252. })
  253. }
  254. })