commentinfo.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // pages/commentinfo/commentinfo.js
  2. import commentsApi from '../../api/comments';
  3. import uploadApi from '../../api/upload'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. shopStars: 5,
  10. productStars: 5,
  11. textWidth: 0,
  12. wordsNum: 0,
  13. limitNum: 70,
  14. outLimit: false,
  15. commentText: "",
  16. imgs: [],
  17. orderId: 0,
  18. productId: 0,
  19. shopId: 0,
  20. imgurls: [],
  21. },
  22. // 上传图片
  23. chooseImg: async function (e) {
  24. var that = this;
  25. var imgs = this.data.imgs;
  26. if (imgs.length >= 9) {
  27. this.setData({
  28. lenMore: 1
  29. });
  30. setTimeout(function () {
  31. that.setData({
  32. lenMore: 0
  33. });
  34. }, 2500);
  35. return false;
  36. }
  37. wx.chooseImage({
  38. // count: 1, // 默认9
  39. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  40. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  41. success: async function (res) {
  42. // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  43. let tempFilePaths = res.tempFilePaths;
  44. console.log(tempFilePaths)
  45. let imgs = that.data.imgs;
  46. // console.log(tempFilePaths + '----');
  47. for (var i = 0; i < tempFilePaths.length; i++) {
  48. if (imgs.length >= 9) {
  49. that.setData({
  50. imgs: imgs
  51. });
  52. return false;
  53. } else {
  54. var imgUrl = await uploadApi.uploadCommentImgs(tempFilePaths[i]);
  55. imgs.push(imgUrl);
  56. }
  57. }
  58. that.setData({
  59. imgs
  60. })
  61. }
  62. });
  63. },
  64. // 删除图片
  65. deleteImg: function (e) {
  66. var imgs = this.data.imgs;
  67. var index = e.currentTarget.dataset.index;
  68. imgs.splice(index, 1);
  69. this.setData({
  70. imgs: imgs
  71. });
  72. },
  73. // 预览图片
  74. previewImg: function (e) {
  75. //获取当前图片的下标
  76. var index = e.currentTarget.dataset.index;
  77. //所有图片
  78. var imgs = this.data.imgs;
  79. wx.previewImage({
  80. //当前显示图片
  81. current: imgs[index],
  82. //所有图片
  83. urls: imgs
  84. })
  85. },
  86. sendComment: async function (e) {
  87. let self = this;
  88. let wordsNum = this.data.wordsNum;
  89. let limitNum = this.data.limitNum;
  90. let content = this.data.commentText;
  91. let imgs = this.data.imgs;
  92. if (wordsNum > limitNum) {
  93. wx.showModal({
  94. title: '字数超出限制!',
  95. content: '请按规定的字数评价。',
  96. showCancel: false,
  97. confirmText: "确定"
  98. })
  99. return;
  100. }
  101. if (wordsNum == 0) {
  102. wx.showModal({
  103. title: '请填写评论',
  104. content: '请谈谈你对产品的评价吧',
  105. showCancel: false,
  106. confirmText: "确定"
  107. })
  108. return;
  109. }
  110. let storeScore = this.data.shopStars;
  111. let productScore = this.data.productStars;
  112. let imgUrls = imgs.join(',');
  113. let storeId = this.data.shopId;
  114. let productId = this.data.productId;
  115. let orderId = this.data.orderId;
  116. let code = await commentsApi.sendDiscuss(storeId, productId, orderId, productScore, storeScore, content, imgUrls);
  117. if (code == 1) {
  118. wx.showModal({
  119. title: '评论成功!',
  120. content: '评论可以在我的评论中查看',
  121. showCancel: false,
  122. confirmText: "确定",
  123. success(res) {
  124. wx.switchTab({
  125. url: '../personorder/personorder',
  126. })
  127. }
  128. })
  129. } else {
  130. wx.showModal({
  131. title: '评论失败!',
  132. showCancel: false,
  133. confirmText: "确定"
  134. })
  135. }
  136. },
  137. //给店铺评分
  138. changeShopStars: function (e) {
  139. this.setData({
  140. shopStars: e.currentTarget.dataset.i
  141. })
  142. },
  143. //给产品评分
  144. changeProductStars: function (e) {
  145. this.setData({
  146. productStars: e.currentTarget.dataset.i
  147. })
  148. },
  149. //根据当前的评论字数更改字数的num
  150. changeComment: function (e) {
  151. var length = e.detail.cursor;
  152. var content = e.detail.value;
  153. if (length > this.data.limitNum) {
  154. this.setData({
  155. wordsNum: length,
  156. outLimit: true,
  157. commentText: content
  158. })
  159. return;
  160. }
  161. this.setData({
  162. wordsNum: length,
  163. outLimit: false,
  164. commentText: content
  165. })
  166. },
  167. /**
  168. * 生命周期函数--监听页面加载
  169. */
  170. onLoad: function (options) {
  171. var textWidth = wx.getSystemInfoSync().windowWidth * (750 / wx.getSystemInfoSync().windowWidth) * 0.9;
  172. this.setData({
  173. textWidth
  174. })
  175. console.log(options)
  176. var orderId = options.id;
  177. var shopId = options.shopId;
  178. var productId = options.productId;
  179. this.setData({
  180. orderId,
  181. productId,
  182. shopId
  183. })
  184. },
  185. /**
  186. * 生命周期函数--监听页面初次渲染完成
  187. */
  188. onReady: function () {
  189. },
  190. /**
  191. * 生命周期函数--监听页面显示
  192. */
  193. onShow: function () {
  194. },
  195. /**
  196. * 生命周期函数--监听页面隐藏
  197. */
  198. onHide: function () {
  199. },
  200. /**
  201. * 生命周期函数--监听页面卸载
  202. */
  203. onUnload: function () {
  204. },
  205. /**
  206. * 页面相关事件处理函数--监听用户下拉动作
  207. */
  208. onPullDownRefresh: function () {
  209. },
  210. /**
  211. * 页面上拉触底事件的处理函数
  212. */
  213. onReachBottom: function () {
  214. },
  215. /**
  216. * 用户点击右上角分享
  217. */
  218. onShareAppMessage: function () {
  219. }
  220. })