edit.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. const util = require('../../utils/util')
  2. const app = getApp()
  3. const form = require('../../utils/formValidation')
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. oneSelect:1,
  10. productInfo: {},
  11. productId: '',
  12. isEdit: false
  13. },
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad: function (options) {
  18. if (options && options.productId) {
  19. this.setData({
  20. productId: options.productId
  21. })
  22. this.bindProduct()
  23. }
  24. if (options && options.id) {
  25. this.setData({
  26. ["productInfo.id"]: options.id,
  27. isEdit: true
  28. })
  29. this.bindVmProduct()
  30. }
  31. },
  32. /**
  33. * 生命周期函数--监听页面初次渲染完成
  34. */
  35. onReady: function () {
  36. },
  37. /**
  38. * 生命周期函数--监听页面显示
  39. */
  40. onShow: function () {
  41. },
  42. /**
  43. * 生命周期函数--监听页面隐藏
  44. */
  45. onHide: function () {
  46. },
  47. /**
  48. * 生命周期函数--监听页面卸载
  49. */
  50. onUnload: function () {
  51. },
  52. /**
  53. * 页面相关事件处理函数--监听用户下拉动作
  54. */
  55. onPullDownRefresh: function () {
  56. },
  57. /**
  58. * 页面上拉触底事件的处理函数
  59. */
  60. onReachBottom: function () {
  61. },
  62. /**
  63. * 用户点击右上角分享
  64. */
  65. onShareAppMessage: function () {
  66. },
  67. formSubmit: function (e) {
  68. var that = this
  69. //表单规则
  70. let rules = [{
  71. name: "productPrice",
  72. rule: ["required", "isAmount"],
  73. msg: ["请输入金额", "请输入正确的金额,允许保留两位小数"]
  74. }, {
  75. name: "productSellPrice",
  76. rule: ["required", "isAmount"],
  77. msg: ["请输入金额", "请输入正确的金额,允许保留两位小数"]
  78. }];
  79. //进行表单检查
  80. let formData = e.detail.value;
  81. console.log(e.detail.value)
  82. let checkRes = form.validation(formData, rules);
  83. if (!checkRes) {
  84. if (!that.data.isEdit) {
  85. that.setData({
  86. ["productInfo.sysProductId"]: that.data.productInfo.id,
  87. ["productInfo.id"]: ""
  88. })
  89. console.log("==========")
  90. var productPrice = e.detail.value.productPrice
  91. var productSellPrice = e.detail.value.productSellPrice
  92. if(productPrice<=0){
  93. util.toast("请输入商品进货价")
  94. return;
  95. }
  96. if(productSellPrice<=0){
  97. util.toast("请输入商品售价")
  98. return;
  99. }
  100. //js 19.9 * 100有bug
  101. that.data.productInfo.productPrice = productPrice *10
  102. that.data.productInfo.productPrice = that.data.productInfo.productPrice *10
  103. that.data.productInfo.productSellPrice = productSellPrice *10
  104. that.data.productInfo.productSellPrice = that.data.productInfo.productSellPrice * 10
  105. util.request(util.api.addVmProduct, that.data.productInfo, "POST", false, true, app.globalData.token).then((res) => {
  106. if (res.code == 200) {
  107. wx.navigateTo({
  108. url: '/pages/product/index',
  109. })
  110. // wx.navigateBack({
  111. // delta: 2
  112. // })
  113. }else{
  114. util.toast(res.message)
  115. setTimeout(()=>{
  116. wx.navigateTo({
  117. url: '/pages/product/index',
  118. })
  119. },500)
  120. }
  121. }).catch((res) => {})
  122. } else {
  123. util.request(util.api.editVmProduct, that.data.productInfo, "POST", false, true, app.globalData.token).then((res) => {
  124. if (res.code == 200) {
  125. wx.navigateBack({
  126. delta: 1
  127. })
  128. }
  129. }).catch((res) => {})
  130. }
  131. } else {
  132. wx.showToast({
  133. title: checkRes,
  134. icon: "none"
  135. });
  136. }
  137. },
  138. formReset: function (e) {
  139. console.log("清空数据")
  140. },
  141. bindProduct() {
  142. var that = this
  143. util.request(util.api.queryByProductId, {
  144. id: that.data.productId
  145. }, "GET", true, true, app.globalData.token).then((res) => {
  146. if (res.code == 200) {
  147. that.setData({
  148. productInfo: res.result,
  149. })
  150. }
  151. }).catch((res) => {})
  152. },
  153. bindVmProduct() {
  154. var that = this
  155. util.request(util.api.queryByVmProductId, {
  156. id: that.data.productInfo.id
  157. }, "GET", true, true, app.globalData.token).then((res) => {
  158. if (res.code == 200) {
  159. that.setData({
  160. productInfo: res.result,
  161. })
  162. }
  163. }).catch((res) => {})
  164. },
  165. inputedit: function (e) {
  166. let _this = this;
  167. let dataset = e.currentTarget.dataset;
  168. let value = e.detail.value;
  169. let name = dataset.name;
  170. value = parseFloat(value)
  171. if(!value){
  172. value = 0.00;
  173. }
  174. if (dataset.type && dataset.type == 'float') {
  175. value = parseInt(parseFloat(value));
  176. _this.data[name] = value;
  177. } else {
  178. _this.data[name] = value;
  179. }
  180. _this.setData({
  181. [name]: _this.data[name]
  182. });
  183. }
  184. })