form.js 6.4 KB

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