comment.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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('user.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. // 'http://tmp/wxe1997e687ecca54e.o6zAJs38WC0RISx_rydS4v4D778c.VzVJOgmUHlH3fd47776794bd803898289bebee12d94c.jpg',
  52. // 'http://tmp/wxe1997e687ecca54e.o6zAJs38WC0RISx_rydS4v4D778c.u8PUZLBNG2ELa7692fe0b9dfebf762cf0cb3677a42d7.jpg',
  53. // 'http://tmp/wxe1997e687ecca54e.o6zAJs38WC0RISx_rydS4v4D778c.8PjhMmysqokY55a19834d4135fbf72d4e653010d375e.jpg'
  54. ],
  55. uploaded: []
  56. });
  57. });
  58. return data;
  59. },
  60. /**
  61. * 设置评分
  62. */
  63. setScore: function(e) {
  64. let dataset = e.currentTarget.dataset;
  65. this.setData({
  66. ['formData[' + dataset.index + '].score']: dataset.score
  67. });
  68. },
  69. /**
  70. * 输入评价内容
  71. */
  72. contentInput: function(e) {
  73. let index = e.currentTarget.dataset.index;
  74. this.setData({
  75. ['formData[' + index + '].content']: e.detail.value
  76. });
  77. },
  78. /**
  79. * 选择图片
  80. */
  81. chooseImage: function(e) {
  82. let _this = this,
  83. index = e.currentTarget.dataset.index,
  84. imageList = _this.data.formData[index].image_list;
  85. // 选择图片
  86. wx.chooseImage({
  87. count: 6 - imageList.length,
  88. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  89. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  90. success: function(res) {
  91. _this.setData({
  92. ['formData[' + index + '].image_list']: imageList.concat(res.tempFilePaths)
  93. });
  94. }
  95. });
  96. },
  97. /**
  98. * 删除图片
  99. */
  100. deleteImage: function(e) {
  101. let dataset = e.currentTarget.dataset,
  102. image_list = this.data.formData[dataset.index].image_list;
  103. image_list.splice(dataset.imageIndex, 1);
  104. this.setData({
  105. ['formData[' + dataset.index + '].image_list']: image_list
  106. });
  107. },
  108. /**
  109. * 表单提交
  110. */
  111. submit: function() {
  112. let _this = this,
  113. formData = _this.data.formData;
  114. // 判断是否重复提交
  115. if (_this.submitDisable === true) {
  116. return false;
  117. }
  118. // 表单提交按钮设为禁用 (防止重复提交)
  119. _this.submitDisable = true;
  120. wx.showLoading({
  121. title: '正在处理...',
  122. mask: true
  123. });
  124. // form提交执行函数
  125. let fromPostCall = function(formData) {
  126. console.log('fromPostCall');
  127. console.log(formData);
  128. App._post_form('user.comment/order', {
  129. order_id: _this.data.options.order_id,
  130. formData: JSON.stringify(formData)
  131. }, function(result) {
  132. if (result.code === 1) {
  133. App.showSuccess(result.msg, function() {
  134. wx.navigateBack();
  135. });
  136. } else {
  137. App.showError(result.msg);
  138. }
  139. },
  140. false,
  141. function() {
  142. wx.hideLoading();
  143. _this.submitDisable = false;
  144. });
  145. };
  146. // 统计图片数量
  147. let imagesLength = 0;
  148. formData.forEach(function(item, formIndex) {
  149. item.content !== '' && (imagesLength += item.image_list.length);
  150. });
  151. // 判断是否需要上传图片
  152. imagesLength > 0 ? _this.uploadFile(imagesLength, formData, fromPostCall) : fromPostCall(formData);
  153. },
  154. /**
  155. * 上传图片
  156. */
  157. uploadFile: function(imagesLength, formData, callBack) {
  158. // POST 参数
  159. let params = {
  160. wxapp_id: App.getWxappId(),
  161. token: wx.getStorageSync('token')
  162. };
  163. // 文件上传
  164. let i = 0;
  165. formData.forEach(function(item, formIndex) {
  166. if (item.content !== '') {
  167. item.image_list.forEach(function(filePath, fileKey) {
  168. wx.uploadFile({
  169. url: App.api_root + 'upload/image',
  170. filePath: filePath,
  171. name: 'iFile',
  172. formData: params,
  173. success: function(res) {
  174. let result = typeof res.data === "object" ? res.data : JSON.parse(res.data);
  175. if (result.code === 1) {
  176. item.uploaded[fileKey] = result.data.file_id;
  177. }
  178. },
  179. complete: function() {
  180. i++;
  181. if (imagesLength === i) {
  182. // 所有文件上传完成
  183. console.log('upload complete');
  184. // 执行回调函数
  185. callBack && callBack(formData);
  186. }
  187. }
  188. });
  189. });
  190. }
  191. });
  192. },
  193. })