form.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. util.toast('分成之和应为100%')
  117. return;
  118. }
  119. }
  120. }
  121. if(productCount==0){
  122. util.toast('请选择至少一个商品!')
  123. return;
  124. }
  125. if (!checkRes) {
  126. if(!that.data.isEdit){
  127. this.setData({
  128. ["values.schemeBillEntryList"]: schemeProduct
  129. })
  130. util.request(util.api.addScheme, that.data.values, "POST", false, true, app.globalData.token).then((res) => {
  131. if (res.code == 200) {
  132. // wx.navigateBack()
  133. wx.navigateTo({
  134. url: '/pages/scheme/index',
  135. })
  136. }
  137. }).catch((res) => {})
  138. }else{
  139. util.request(util.api.editScheme, that.data.values, "POST", false, true, app.globalData.token).then((res) => {
  140. if (res.code == 200) {
  141. wx.showToast({
  142. title: '编辑成功!',
  143. })
  144. setTimeout(() => {
  145. wx.navigateBack()
  146. }, 500);
  147. // wx.navigateBack()
  148. }
  149. }).catch((res) => {})
  150. }
  151. } else {
  152. wx.showToast({
  153. title: checkRes,
  154. icon: "none"
  155. });
  156. }
  157. },
  158. formReset: function (e) {
  159. console.log("清空数据")
  160. },
  161. inputedit: function (e) {
  162. let _this = this;
  163. let dataset = e.currentTarget.dataset;
  164. let value = e.detail.value;
  165. let name = dataset.name;
  166. _this.data[name] = value;
  167. _this.setData({
  168. [name]: _this.data[name]
  169. });
  170. },
  171. inputeditRow: function (e) {
  172. let that = this;
  173. let dataset = e.currentTarget.dataset;
  174. let value = e.detail.value;
  175. let name = dataset.name;
  176. var list = that.data.values.schemeBillEntryList
  177. var index = dataset.index;
  178. var itemProduct = dataset.item
  179. value = parseFloat(value)
  180. if(!value){
  181. value = 0.00;
  182. }
  183. console.log(name)
  184. if(name=="storeDivide"){
  185. itemProduct.storeDivide = value
  186. }else if(name=="areaDivide"){
  187. itemProduct.areaDivide = value
  188. }
  189. list.splice(index,1,itemProduct)
  190. that.setData({
  191. ["values.schemeBillEntryList"]:list
  192. })
  193. console.log(this.data.values.schemeBillEntryList)
  194. },
  195. bindStoreList() {
  196. var that = this
  197. util.request(util.api.storeList, {agentId:that.data.values.agentId}, "GET", false, true, app.globalData.token).then((res) => {
  198. if (res.code == 200) {
  199. that.setData({
  200. storeList: res.result
  201. })
  202. }
  203. }).catch((res) => {})
  204. },
  205. //代理商
  206. getAllAgentList() {
  207. var that = this
  208. util.request(util.api.allAgentList, {}, "GET", false, true,app.globalData.token).then((res) => {
  209. if (res.code == 200) {
  210. that.setData({
  211. agentList: res.result
  212. })
  213. }
  214. }).catch((res) => {})
  215. },
  216. toSelect(e) {
  217. wx.navigateTo({
  218. url: '/pages/scheme/select?index=' + e.currentTarget.dataset.index,
  219. })
  220. },
  221. bindScheme(){
  222. var that = this
  223. util.request(util.api.queryBySchemeId, {
  224. id: that.data.id
  225. }, "GET", true, true, app.globalData.token).then((res) => {
  226. if (res.code == 200) {
  227. that.setData({
  228. values: res.result,
  229. })
  230. }
  231. }).catch((res) => {})
  232. },
  233. deleteScheme(e){
  234. let that = this;
  235. util.request(util.api.deleteSchemeById, {'id':that.data.id}, "POST", true, true, app.globalData.token).then((res) => {
  236. if(res.code == 200){
  237. wx.navigateTo({
  238. url: '/pages/scheme/index',
  239. })
  240. }
  241. }).catch((res) => {})
  242. },
  243. //添加格子
  244. addGrid(){
  245. var schemeBillEntryList =this.data.values.schemeBillEntryList;
  246. var product = {productName:'请选择商品',gridNum:schemeBillEntryList.length+1};
  247. schemeBillEntryList.push(product)
  248. this.setData({
  249. ["values.schemeBillEntryList"]: schemeBillEntryList
  250. })
  251. }
  252. })