editProduct.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. const app = getApp();
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. code:'',
  8. modelList:[],
  9. },
  10. scanCode() {
  11. var that = this;
  12. wx.scanCode({ //扫描API
  13. scanType:['barCode','qrCode'],
  14. success(res) { //扫描成功
  15. console.log(res) //输出回调信息
  16. that.setData({
  17. code: res.result
  18. });
  19. wx.showToast({
  20. title: '扫码成功',
  21. duration: 1000
  22. })
  23. },
  24. fail:function(err){
  25. app.showToptip(that, 'error', '二维码无效');
  26. }
  27. })
  28. },
  29. setCode(e){
  30. this.setData({
  31. code:e.detail.value
  32. })
  33. },
  34. /**
  35. * 生命周期函数--监听页面加载
  36. */
  37. onLoad: function (options) {
  38. var arr = [];
  39. var item = JSON.parse(options.item);
  40. this.setData({item,code:item.code});
  41. var modelList = JSON.parse(options.content);
  42. if(typeof modelList[0] == 'string'){
  43. var content = [];
  44. modelList.map((item,index)=>{
  45. var temp = item.replace('{','').replace('}','').split(',');
  46. temp.map((temps,index)=>{
  47. var value = temps.split(':');
  48. var valueStr = value[0].replace(/\s/g,'')
  49. content.push({[valueStr]:value[1]})
  50. })
  51. })
  52. content.map((item,index)=>{
  53. if(index % 2 != 0){
  54. arr.push(Object.assign(content[index],content[index-1]))
  55. }
  56. })
  57. }else if(typeof modelList[0] == 'object'){
  58. arr = modelList;
  59. }
  60. console.log(arr);
  61. this.setData({modelList:arr});
  62. },
  63. formSubmit(e){
  64. var value = e.detail.value;
  65. var code = this.data.code;
  66. const {name,companyName} = value;
  67. var that = this;
  68. var arr = [];
  69. delete value.code
  70. Object.keys(value).forEach(function (e,index) {
  71. if(value[e].length == 0){
  72. app.showToptip(that, "error",'信息不能为空!');
  73. arr = [];
  74. throw new Error('err');
  75. }else {
  76. if(index > 1){
  77. arr.push({'keyStr':e,'valueStr':value[e]})
  78. // arr.push("{keyStr:"+e+",valueStr:"+value[e]+"}")
  79. }
  80. }
  81. })
  82. if(arr){
  83. app.requestP({
  84. url:'/product/updateProduct',
  85. method:'post',
  86. contentType:' application/json',
  87. data:{
  88. code,
  89. name,
  90. companyName,
  91. id:that.data.item.id,
  92. content:JSON.stringify(arr),
  93. typeId:that.data.item.typeId
  94. }
  95. }).then(res=>{
  96. app.showToptip(that,'success',res.data.msg)
  97. wx.navigateBack({
  98. delta: 1,
  99. })
  100. }).catch(err=>{
  101. app.showToptip(that,'error',err.data.msg)
  102. })
  103. }
  104. },
  105. /**
  106. * 生命周期函数--监听页面初次渲染完成
  107. */
  108. onReady: function () {
  109. },
  110. /**
  111. * 生命周期函数--监听页面显示
  112. */
  113. onShow: function () {
  114. },
  115. /**
  116. * 生命周期函数--监听页面隐藏
  117. */
  118. onHide: function () {
  119. },
  120. /**
  121. * 生命周期函数--监听页面卸载
  122. */
  123. onUnload: function () {
  124. },
  125. /**
  126. * 页面相关事件处理函数--监听用户下拉动作
  127. */
  128. onPullDownRefresh: function () {
  129. },
  130. /**
  131. * 页面上拉触底事件的处理函数
  132. */
  133. onReachBottom: function () {
  134. },
  135. /**
  136. * 用户点击右上角分享
  137. */
  138. onShareAppMessage: function () {
  139. }
  140. })