topicCommentPost.js 3.8 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. valueId: 0,
  8. topic: {},
  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. if (parseInt(options.type) !== 1) {
  93. return;
  94. }
  95. var that = this;
  96. that.setData({
  97. valueId: options.valueId
  98. });
  99. this.getTopic();
  100. },
  101. getTopic: function() {
  102. let that = this;
  103. util.request(api.TopicDetail, {
  104. id: that.data.valueId
  105. }).then(function(res) {
  106. if (res.errno === 0) {
  107. that.setData({
  108. topic: res.data.topic
  109. });
  110. }
  111. });
  112. },
  113. onClose: function() {
  114. wx.navigateBack();
  115. },
  116. onPost: function() {
  117. let that = this;
  118. if (!this.data.content) {
  119. util.showErrorToast('请填写评论')
  120. return false;
  121. }
  122. util.request(api.CommentPost, {
  123. type: 1,
  124. valueId: that.data.valueId,
  125. content: that.data.content,
  126. star: that.data.star,
  127. hasPicture: that.data.hasPicture,
  128. picUrls: that.data.picUrls
  129. }, 'POST').then(function(res) {
  130. if (res.errno === 0) {
  131. wx.showToast({
  132. title: '评论成功',
  133. complete: function() {
  134. wx.navigateBack();
  135. }
  136. })
  137. }
  138. });
  139. },
  140. bindInputValue(event) {
  141. let value = event.detail.value;
  142. //判断是否超过140个字符
  143. if (value && value.length > 140) {
  144. return false;
  145. }
  146. this.setData({
  147. content: event.detail.value,
  148. })
  149. },
  150. onReady: function() {
  151. },
  152. onShow: function() {
  153. // 页面显示
  154. },
  155. onHide: function() {
  156. // 页面隐藏
  157. },
  158. onUnload: function() {
  159. // 页面关闭
  160. }
  161. })