commentPost.js 3.8 KB

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