commentPost.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // 上传组件 基于https://github.com/Tencent/weui-wxss/tree/master/src/example/uploader
  2. var app = getApp();
  3. var util = require('../../utils/util.js');
  4. var api = require('../../config/api.js');
  5. Page({
  6. data: {
  7. orderId: 0,
  8. type: 0,
  9. valueId: 0,
  10. orderGoods: {},
  11. content: '',
  12. stars: [0, 1, 2, 3, 4],
  13. star: 5,
  14. starText: '十分满意',
  15. hasPicture: false,
  16. picUrls: [],
  17. files: []
  18. },
  19. chooseImage: function(e) {
  20. if (this.data.files.length >= 5) {
  21. util.showErrorToast('只能上传五张图片')
  22. return false;
  23. }
  24. var that = this;
  25. wx.chooseImage({
  26. count: 1,
  27. sizeType: ['original', 'compressed'],
  28. sourceType: ['album', 'camera'],
  29. success: function(res) {
  30. that.setData({
  31. files: that.data.files.concat(res.tempFilePaths)
  32. });
  33. that.upload(res);
  34. }
  35. })
  36. },
  37. upload: function(res) {
  38. var that = this;
  39. const uploadTask = wx.uploadFile({
  40. url: api.StorageUpload,
  41. filePath: res.tempFilePaths[0],
  42. name: 'file',
  43. success: function(res) {
  44. var _res = JSON.parse(res.data);
  45. if (_res.errno === 0) {
  46. var url = _res.data.url
  47. that.data.picUrls.push(url)
  48. that.setData({
  49. hasPicture: true,
  50. picUrls: that.data.picUrls
  51. })
  52. }
  53. },
  54. fail: function(e) {
  55. wx.showModal({
  56. title: '错误',
  57. content: '上传失败',
  58. showCancel: false
  59. })
  60. },
  61. })
  62. uploadTask.onProgressUpdate((res) => {
  63. console.log('上传进度', res.progress)
  64. console.log('已经上传的数据长度', res.totalBytesSent)
  65. console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend)
  66. })
  67. },
  68. previewImage: function(e) {
  69. wx.previewImage({
  70. current: e.currentTarget.id, // 当前显示图片的http链接
  71. urls: this.data.files // 需要预览的图片http链接列表
  72. })
  73. },
  74. selectRater: function(e) {
  75. var star = e.currentTarget.dataset.star + 1;
  76. var starText;
  77. if (star == 1) {
  78. starText = '很差';
  79. } else if (star == 2) {
  80. starText = '不太满意';
  81. } else if (star == 3) {
  82. starText = '满意';
  83. } else if (star == 4) {
  84. starText = '比较满意';
  85. } else {
  86. starText = '十分满意'
  87. }
  88. this.setData({
  89. star: star,
  90. starText: starText
  91. })
  92. },
  93. onLoad: function(options) {
  94. var that = this;
  95. that.setData({
  96. orderId: options.orderId,
  97. type: options.type,
  98. valueId: options.valueId
  99. });
  100. this.getOrderGoods();
  101. },
  102. getOrderGoods: function() {
  103. let that = this;
  104. util.request(api.OrderGoods, {
  105. orderId: that.data.orderId,
  106. goodsId: that.data.valueId
  107. }).then(function(res) {
  108. if (res.errno === 0) {
  109. that.setData({
  110. orderGoods: res.data,
  111. });
  112. }
  113. });
  114. },
  115. onClose: function() {
  116. wx.navigateBack();
  117. },
  118. onPost: function() {
  119. let that = this;
  120. if (!this.data.content) {
  121. util.showErrorToast('请填写评论')
  122. return false;
  123. }
  124. util.request(api.OrderComment, {
  125. orderGoodsId: that.data.orderGoods.id,
  126. content: that.data.content,
  127. star: that.data.star,
  128. hasPicture: that.data.hasPicture,
  129. picUrls: that.data.picUrls
  130. }, 'POST').then(function(res) {
  131. if (res.errno === 0) {
  132. wx.showToast({
  133. title: '评论成功',
  134. complete: function() {
  135. wx.switchTab({
  136. url: '/pages/ucenter/index/index'
  137. })
  138. }
  139. })
  140. }
  141. });
  142. },
  143. bindInputValue(event) {
  144. let value = event.detail.value;
  145. //判断是否超过140个字符
  146. if (value && value.length > 140) {
  147. return false;
  148. }
  149. this.setData({
  150. content: event.detail.value,
  151. })
  152. },
  153. onReady: function() {
  154. },
  155. onShow: function() {
  156. // 页面显示
  157. },
  158. onHide: function() {
  159. // 页面隐藏
  160. },
  161. onUnload: function() {
  162. // 页面关闭
  163. }
  164. })