apply.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. const App = getApp();
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. // 订单商品id
  8. order_goods_id: null,
  9. // 订单商品详情
  10. detail: {},
  11. // 图片列表
  12. imageList: [],
  13. // 服务类型
  14. serviceType: 10,
  15. },
  16. disable: false,
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function(options) {
  21. // 记录页面参数
  22. this.data.order_goods_id = options.order_goods_id;
  23. // 获取订单商品详情
  24. this.getGoodsDetail();
  25. },
  26. /**
  27. * 获取订单商品详情
  28. */
  29. getGoodsDetail: function() {
  30. let _this = this;
  31. App._get('user.refund/apply', {
  32. order_goods_id: this.data.order_goods_id
  33. }, function(result) {
  34. _this.setData(result.data);
  35. });
  36. },
  37. /**
  38. * 切换标签
  39. */
  40. onSwitchService: function(e) {
  41. this.setData({
  42. serviceType: e.detail.target.dataset.type
  43. });
  44. },
  45. /**
  46. * 跳转商品详情
  47. */
  48. onGoodsDetail: function(e) {
  49. wx.navigateTo({
  50. url: '../../../goods/index?goods_id=' + e.detail.target.dataset.id
  51. });
  52. },
  53. /**
  54. * 选择图片
  55. */
  56. chooseImage: function(e) {
  57. let _this = this,
  58. index = e.currentTarget.dataset.index,
  59. imageList = _this.data.imageList;
  60. // 选择图片
  61. wx.chooseImage({
  62. count: 6 - imageList.length,
  63. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  64. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  65. success: function(res) {
  66. _this.setData({
  67. imageList: imageList.concat(res.tempFilePaths)
  68. });
  69. }
  70. });
  71. },
  72. /**
  73. * 删除图片
  74. */
  75. deleteImage: function(e) {
  76. let dataset = e.currentTarget.dataset,
  77. imageList = this.data.imageList;
  78. imageList.splice(dataset.imageIndex, 1);
  79. this.setData({
  80. imageList
  81. });
  82. },
  83. /**
  84. * 表单提交
  85. */
  86. onSubmit: function(e) {
  87. let _this = this;
  88. if (!e.detail.value.content) {
  89. App.showError('申请原因不能为空');
  90. return false;
  91. }
  92. // 判断是否重复提交
  93. if (_this.disable === true) {
  94. return false;
  95. }
  96. // 表单提交按钮设为禁用 (防止重复提交)
  97. _this.disable = true;
  98. wx.showLoading({
  99. title: '正在处理...',
  100. mask: true
  101. });
  102. // form参数
  103. let postParams = {
  104. order_goods_id: _this.data.order_goods_id,
  105. type: _this.data.serviceType,
  106. content: e.detail.value.content,
  107. };
  108. // form提交执行函数
  109. let fromPostCall = function(params) {
  110. console.log('fromPostCall');
  111. App._post_form('user.refund/apply', params, function(result) {
  112. if (result.code === 1) {
  113. App.showSuccess(result.msg, function() {
  114. // 跳转售后管理页面
  115. wx.navigateTo({
  116. url: "../index"
  117. });
  118. });
  119. } else {
  120. App.showError(result.msg);
  121. }
  122. },
  123. false,
  124. function() {
  125. wx.hideLoading();
  126. _this.disable = false;
  127. });
  128. };
  129. // 统计图片数量
  130. let imagesLength = _this.data.imageList.length;
  131. // 判断是否需要上传图片
  132. imagesLength > 0 ? _this.uploadFile(imagesLength, fromPostCall, postParams) : fromPostCall(postParams);
  133. },
  134. /**
  135. * 上传图片
  136. */
  137. uploadFile: function(imagesLength, callBack, formData) {
  138. let uploaded = [];
  139. // 文件上传
  140. let i = 0;
  141. this.data.imageList.forEach(function(filePath, fileKey) {
  142. wx.uploadFile({
  143. url: App.api_root + 'upload/image',
  144. filePath: filePath,
  145. name: 'iFile',
  146. formData: {
  147. wxapp_id: App.getWxappId(),
  148. token: wx.getStorageSync('token')
  149. },
  150. success: function(res) {
  151. let result = typeof res.data === "object" ? res.data : JSON.parse(res.data);
  152. if (result.code === 1) {
  153. uploaded[fileKey] = result.data.file_id;
  154. }
  155. },
  156. complete: function() {
  157. i++;
  158. if (imagesLength === i) {
  159. // 所有文件上传完成
  160. console.log('upload complete');
  161. formData['images'] = uploaded;
  162. // 执行回调函数
  163. callBack && callBack(formData);
  164. }
  165. }
  166. });
  167. });
  168. },
  169. })