addProcut.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. this.setData({id:options.type})
  39. this.getProductModel(options.type);
  40. },
  41. getProductModel(type){
  42. var that = this;
  43. app.requestP({
  44. url:'/productModel/ListByType',
  45. method:'post',
  46. contentType:' application/json',
  47. data:{
  48. type
  49. }
  50. }).then(res=>{
  51. that.setData({
  52. modelList:res.data.data
  53. })
  54. }).catch(err=>{
  55. app.showToptip(that,'error',err.data.msg)
  56. })
  57. },
  58. formSubmit(e){
  59. var value = e.detail.value;
  60. var code = this.data.code;
  61. const {name,companyName} = value;
  62. var that = this;
  63. var arr = [];
  64. delete value.code
  65. Object.keys(value).forEach(function (e,index) {
  66. if(value[e].length == 0){
  67. app.showToptip(that, "error",'信息不能为空!');
  68. arr = [];
  69. throw new Error('err');
  70. }else {
  71. if(index > 1){
  72. arr.push({'keyStr':e,'valueStr':value[e]})
  73. // arr.push("{keyStr:"+e+",valueStr:"+value[e]+"}")
  74. }
  75. }
  76. })
  77. if(arr){
  78. app.requestP({
  79. url:'/product/createProduct',
  80. method:'post',
  81. contentType:' application/json',
  82. data:{
  83. code,name,companyName,
  84. content:JSON.stringify(arr),
  85. typeId:that.data.id
  86. }
  87. }).then(res=>{
  88. app.showToptip(that,'success',res.data.msg);
  89. wx.navigateBack({
  90. delta: 1,
  91. },
  92. );
  93. }).catch(err=>{
  94. app.showToptip(that,'error',err.data.msg)
  95. })
  96. }
  97. },
  98. })