prodComm.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. var http = require('../../utils/http.js');
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. orderItemInfo: [], //订单列表页参数
  8. submitCommCount:0 //已经提交的评论数量
  9. },
  10. /**
  11. * 生命周期函数--监听页面加载
  12. */
  13. onLoad: function (options) {
  14. // 获取上页(订单列表)数据
  15. var orderItemInfo = wx.getStorageSync("orderItemInfo");
  16. // console.log(orderItemInfo);
  17. for (var i = 0; i < orderItemInfo.length; i++){
  18. orderItemInfo[i].images = [];
  19. orderItemInfo[i].content = "";
  20. orderItemInfo[i].score = 5;
  21. orderItemInfo[i].isAnonymous = 1;
  22. orderItemInfo[i].evaluate = 0;
  23. }
  24. this.setData({
  25. orderItemInfo: orderItemInfo
  26. })
  27. console.log(orderItemInfo)
  28. },
  29. /**
  30. * 发表评论
  31. */
  32. submitComm: function (e) {
  33. var orderItemInfo = this.data.orderItemInfo;
  34. var allFill = true;
  35. for (var i = 0; i < orderItemInfo.length; i++) {
  36. var cont = orderItemInfo[i].content.trim();
  37. if(cont==""){
  38. wx.showToast({
  39. title: '评价不能为空',
  40. icon:"none"
  41. })
  42. allFill = false;
  43. break;
  44. }
  45. }
  46. if (allFill){
  47. for (var i = 0; i < orderItemInfo.length; i++) {
  48. wx.showLoading();
  49. var pics = '';
  50. orderItemInfo[i].images.forEach(function(item){
  51. pics += item.path + ',';
  52. });
  53. if(pics!=''){
  54. pics = pics.substring(0,pics.length-1)
  55. }
  56. // 发布评论
  57. var params = {
  58. url: "/p/prodComm",
  59. method: "POST",
  60. data: {
  61. content: orderItemInfo[i].content,
  62. score: orderItemInfo[i].score,
  63. evaluate: orderItemInfo[i].evaluate,
  64. isAnonymous: orderItemInfo[i].isAnonymous,
  65. orderItemId: orderItemInfo[i].orderItemId,
  66. prodId: orderItemInfo[i].prodId,
  67. pics: pics
  68. },
  69. callBack: (res) => {
  70. this.setData({
  71. submitCommCount: this.data.submitCommCount+1
  72. });
  73. if (this.data.submitCommCount == orderItemInfo.length) {
  74. wx.showModal({
  75. title: '',
  76. content: '评价成功,感谢您!',
  77. showCancel:false,
  78. success(res) {
  79. wx.navigateBack();
  80. }
  81. })
  82. }
  83. wx.hideLoading();
  84. }
  85. };
  86. http.request(params);
  87. }
  88. }
  89. },
  90. /**
  91. * 上传图片
  92. */
  93. getUploadImg: function(e) {
  94. const idx = e.target.dataset.idx
  95. console.log(idx);
  96. var ths = this;
  97. wx.chooseImage({
  98. count: 1, // 默认9
  99. sizeType: ['compressed'],
  100. sourceType: ['album', 'camera'],
  101. success: function (res) {
  102. var tempFilePaths = res.tempFilePaths;
  103. wx.showLoading({
  104. mask: true
  105. })
  106. var params = {
  107. url: "/p/file/upload",
  108. filePath: tempFilePaths[0],
  109. name: 'file',
  110. callBack: function (res2) {
  111. wx.hideLoading();
  112. var img = {};
  113. img.path = JSON.parse(res2).filePath;
  114. img.url = JSON.parse(res2).resourcesUrl + JSON.parse(res2).filePath;
  115. var orderItemInfo = ths.data.orderItemInfo;
  116. orderItemInfo[idx].images.push(img);
  117. ths.setData({
  118. orderItemInfo: orderItemInfo
  119. })
  120. }
  121. };
  122. http.upload(params);
  123. }
  124. })
  125. },
  126. /**
  127. * 删除图片
  128. */
  129. removeImage(e) {
  130. const idx = e.target.dataset.idx
  131. const index = e.target.dataset.index
  132. var orderItemInfo = this.data.orderItemInfo;
  133. orderItemInfo[index].images.splice(idx, 1)
  134. this.setData({
  135. orderItemInfo: orderItemInfo
  136. });
  137. },
  138. onContentInput:function(e){
  139. const index = e.target.dataset.index
  140. var orderItemInfo = this.data.orderItemInfo;
  141. orderItemInfo[index].content = e.detail.value;
  142. this.setData({
  143. orderItemInfo: orderItemInfo
  144. });
  145. },
  146. /**
  147. * 匿名评价
  148. * 每一项的选择事件
  149. */
  150. onSelectedItem: function (e) {
  151. var index = e.target.dataset.index;// 获取data- 传进来的index
  152. var orderItemInfo = this.data.orderItemInfo;// 获取评论项
  153. var isAnonymous = orderItemInfo[index].isAnonymous; // 获取当前评价的选中状态
  154. if (isAnonymous==1){
  155. isAnonymous = 0;
  156. }else{
  157. isAnonymous = 1;
  158. }
  159. orderItemInfo[index].isAnonymous = isAnonymous; // 改变状态
  160. this.setData({
  161. orderItemInfo: orderItemInfo
  162. });
  163. },
  164. onStarChange:function(e){
  165. var index = e.detail.idx;
  166. var val = e.detail.val;
  167. var evaluate = 0;
  168. var orderItemInfo = this.data.orderItemInfo;
  169. if(val<3){
  170. evaluate = 2;
  171. }else if(val==3){
  172. evaluate = 1;
  173. }
  174. orderItemInfo[index].score = val;
  175. orderItemInfo[index].evaluate = evaluate;
  176. this.setData({
  177. orderItemInfo: orderItemInfo
  178. });
  179. },
  180. /**
  181. * 评价图片预览
  182. */
  183. comPicPreView(e){
  184. var index = e.currentTarget.dataset.index
  185. var idx = e.currentTarget.dataset.idx
  186. var urls = []
  187. this.data.orderItemInfo[index].images.forEach(el => {
  188. urls.push(el.url)
  189. })
  190. wx.previewImage({
  191. current: urls[idx],
  192. urls: urls
  193. })
  194. },
  195. /**
  196. * 生命周期函数--监听页面初次渲染完成
  197. */
  198. onReady: function () {
  199. },
  200. /**
  201. * 生命周期函数--监听页面显示
  202. */
  203. onShow: function () {
  204. },
  205. /**
  206. * 生命周期函数--监听页面隐藏
  207. */
  208. onHide: function () {
  209. },
  210. /**
  211. * 生命周期函数--监听页面卸载
  212. */
  213. onUnload: function () {
  214. },
  215. /**
  216. * 页面相关事件处理函数--监听用户下拉动作
  217. */
  218. onPullDownRefresh: function () {
  219. },
  220. /**
  221. * 页面上拉触底事件的处理函数
  222. */
  223. onReachBottom: function () {
  224. },
  225. /**
  226. * 用户点击右上角分享
  227. */
  228. onShareAppMessage: function () {
  229. }
  230. })