topicCommentPost.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. }
  78. else if (star == 2) {
  79. starText = '不太满意';
  80. }
  81. else if (star == 3) {
  82. starText = '满意';
  83. }
  84. else if (star == 4) {
  85. starText = '比较满意';
  86. }
  87. else {
  88. starText = '十分满意'
  89. }
  90. this.setData({
  91. star: star,
  92. starText: starText
  93. })
  94. },
  95. onLoad: function (options) {
  96. if (parseInt(options.type) !== 1){
  97. return;
  98. }
  99. var that = this;
  100. that.setData({
  101. valueId: options.valueId
  102. });
  103. this.getTopic();
  104. },
  105. getTopic: function () {
  106. let that = this;
  107. util.request(api.TopicDetail, { id: that.data.valueId }).then(function (res) {
  108. if (res.errno === 0) {
  109. that.setData({
  110. topic: res.data.topic
  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.CommentPost, {
  125. type: 1,
  126. valueId: that.data.valueId,
  127. content: that.data.content,
  128. star: that.data.star,
  129. hasPicture: that.data.hasPicture,
  130. picUrls: that.data.picUrls
  131. }, 'POST').then(function (res) {
  132. if (res.errno === 0) {
  133. wx.showToast({
  134. title: '评论成功',
  135. complete: function () {
  136. wx.navigateBack();
  137. }
  138. })
  139. }
  140. });
  141. },
  142. bindInputValue(event) {
  143. let value = event.detail.value;
  144. //判断是否超过140个字符
  145. if (value && value.length > 140) {
  146. return false;
  147. }
  148. this.setData({
  149. content: event.detail.value,
  150. })
  151. },
  152. onReady: function () {
  153. },
  154. onShow: function () {
  155. // 页面显示
  156. },
  157. onHide: function () {
  158. // 页面隐藏
  159. },
  160. onUnload: function () {
  161. // 页面关闭
  162. }
  163. })