comment.js 4.9 KB

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