form.js 6.9 KB

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