topicCommentPost.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. <template>
  2. <view class="container">
  3. <view class="post-comment">
  4. <view class="goods">
  5. <view class="img">
  6. <image :src="topic.picUrl"></image>
  7. </view>
  8. <view class="info">
  9. <view class="t">
  10. <text class="name">{{ topic.title }}</text>
  11. </view>
  12. <view class="attr">{{ topic.subtitle }}</view>
  13. </view>
  14. </view>
  15. <view class="rater">
  16. <text class="rater-title">评分</text>
  17. <block v-for="(item, index) in stars" :key="index">
  18. <van-icon name="star" @tap.native="selectRater($event, { star: item })" color="#ab956d" :data-star="item" v-if="item < star" />
  19. <van-icon name="star-o" @tap.native="selectRater($event, { star: item })" :data-star="item" v-else />
  20. </block>
  21. <text class="rater-desc">{{ starText }}</text>
  22. </view>
  23. <view class="input-box">
  24. <textarea class="content" :focus="true" @input="bindInputValue" maxlength="140" placeholder="留言经过筛选后,对所有人可见" />
  25. <text class="count">{{ 140 - content.length }}</text>
  26. </view>
  27. <view class="weui-uploader">
  28. <view class="weui-uploader__hd">
  29. <view class="weui-uploader__title">图片上传</view>
  30. <view class="weui-uploader__info">{{ picUrls.length }}/{{ files.length }}</view>
  31. </view>
  32. <view class="weui-uploader__bd">
  33. <view class="weui-uploader__files" id="uploaderFiles">
  34. <block v-for="(item, index) in files" :key="index">
  35. <view class="weui-uploader__file" @tap="previewImage" :id="item">
  36. <image class="weui-uploader__img" :src="item" mode="aspectFill" />
  37. </view>
  38. </block>
  39. <view class="weui-uploader__input-box">
  40. <view class="weui-uploader__input" @tap="chooseImage"></view>
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. <view class="btns">
  46. <view class="close" @tap="onClose">取消</view>
  47. <view class="post" @tap="onPost">发表</view>
  48. </view>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. // 上传组件 基于https://github.com/Tencent/weui-wxss/tree/master/src/example/uploader
  54. var app = getApp();
  55. var util = require('../../../utils/util.js');
  56. var api = require('../../../config/api.js');
  57. export default {
  58. data() {
  59. return {
  60. valueId: 0,
  61. topic: {
  62. picUrl: '',
  63. title: '',
  64. subtitle: ''
  65. },
  66. content: '',
  67. stars: [0, 1, 2, 3, 4],
  68. star: 5,
  69. starText: '十分满意',
  70. hasPicture: false,
  71. picUrls: [],
  72. files: []
  73. };
  74. },
  75. onLoad: function (options) {
  76. if (parseInt(options.type) !== 1) {
  77. return;
  78. }
  79. var that = this;
  80. that.setData({
  81. valueId: options.valueId
  82. });
  83. this.getTopic();
  84. },
  85. onReady: function () {},
  86. onShow: function () {
  87. // 页面显示
  88. },
  89. onHide: function () {
  90. // 页面隐藏
  91. },
  92. onUnload: function () {
  93. // 页面关闭
  94. },
  95. methods: {
  96. chooseImage: function (e) {
  97. if (this.files.length >= 5) {
  98. util.showErrorToast('只能上传五张图片');
  99. return false;
  100. }
  101. var that = this;
  102. uni.chooseImage({
  103. count: 1,
  104. sizeType: ['original', 'compressed'],
  105. sourceType: ['album', 'camera'],
  106. success: function (res) {
  107. that.setData({
  108. files: that.files.concat(res.tempFilePaths)
  109. });
  110. that.upload(res);
  111. }
  112. });
  113. },
  114. upload: function (res) {
  115. var that = this;
  116. const uploadTask = uni.uploadFile({
  117. url: api.StorageUpload,
  118. filePath: res.tempFilePaths[0],
  119. name: 'file',
  120. success: function (res) {
  121. var _res = JSON.parse(res.data);
  122. if (_res.errno === 0) {
  123. var url = _res.data.url;
  124. that.picUrls.push(url);
  125. that.setData({
  126. hasPicture: true,
  127. picUrls: that.picUrls
  128. });
  129. }
  130. },
  131. fail: function (e) {
  132. uni.showModal({
  133. title: '错误',
  134. content: '上传失败',
  135. showCancel: false
  136. });
  137. }
  138. });
  139. uploadTask.onProgressUpdate((res) => {
  140. console.log('上传进度', res.progress);
  141. console.log('已经上传的数据长度', res.totalBytesSent);
  142. console.log('预期需要上传的数据总长度', res.totalBytesExpectedToSend);
  143. });
  144. },
  145. previewImage: function (e) {
  146. uni.previewImage({
  147. current: e.currentTarget.id,
  148. // 当前显示图片的http链接
  149. urls: this.files // 需要预览的图片http链接列表
  150. });
  151. },
  152. selectRater: function (e, _dataset) {
  153. /* ---处理dataset begin--- */
  154. this.handleDataset(e, _dataset);
  155. /* ---处理dataset end--- */
  156. var star = e.currentTarget.dataset.star + 1;
  157. var starText;
  158. if (star == 1) {
  159. starText = '很差';
  160. } else if (star == 2) {
  161. starText = '不太满意';
  162. } else if (star == 3) {
  163. starText = '满意';
  164. } else if (star == 4) {
  165. starText = '比较满意';
  166. } else {
  167. starText = '十分满意';
  168. }
  169. this.setData({
  170. star: star,
  171. starText: starText
  172. });
  173. },
  174. getTopic: function () {
  175. let that = this;
  176. util.request(api.TopicDetail, {
  177. id: that.valueId
  178. }).then(function (res) {
  179. if (res.errno === 0) {
  180. that.setData({
  181. topic: res.data.topic
  182. });
  183. }
  184. });
  185. },
  186. onClose: function () {
  187. uni.navigateBack();
  188. },
  189. onPost: function () {
  190. let that = this;
  191. if (!this.content) {
  192. util.showErrorToast('请填写评论');
  193. return false;
  194. }
  195. util.request(
  196. api.CommentPost,
  197. {
  198. type: 1,
  199. valueId: that.valueId,
  200. content: that.content,
  201. star: that.star,
  202. hasPicture: that.hasPicture,
  203. picUrls: that.picUrls
  204. },
  205. 'POST'
  206. ).then(function (res) {
  207. if (res.errno === 0) {
  208. uni.showToast({
  209. title: '评论成功',
  210. complete: function () {
  211. uni.navigateBack();
  212. }
  213. });
  214. }
  215. });
  216. },
  217. bindInputValue(event) {
  218. let value = event.detail.value;
  219. //判断是否超过140个字符
  220. if (value && value.length > 140) {
  221. return false;
  222. }
  223. this.setData({
  224. content: event.detail.value
  225. });
  226. }
  227. }
  228. };
  229. </script>
  230. <style>
  231. page,
  232. .container {
  233. height: 100%;
  234. background: #f4f4f4;
  235. }
  236. .post-comment {
  237. width: 750rpx;
  238. height: auto;
  239. overflow: hidden;
  240. padding: 30rpx;
  241. background: #fff;
  242. }
  243. .post-comment .goods {
  244. display: flex;
  245. align-items: center;
  246. height: 199rpx;
  247. margin-left: 31.25rpx;
  248. }
  249. .post-comment .goods .img {
  250. height: 145.83rpx;
  251. width: 145.83rpx;
  252. background: #f4f4f4;
  253. }
  254. .post-comment .goods .img image {
  255. height: 145.83rpx;
  256. width: 145.83rpx;
  257. }
  258. .post-comment .goods .info {
  259. height: 145.83rpx;
  260. flex: 1;
  261. padding-left: 20rpx;
  262. }
  263. .post-comment .goods .name {
  264. margin-top: 30rpx;
  265. display: block;
  266. height: 44rpx;
  267. line-height: 44rpx;
  268. color: #333;
  269. font-size: 30rpx;
  270. }
  271. .post-comment .goods .number {
  272. display: block;
  273. height: 37rpx;
  274. line-height: 37rpx;
  275. color: #666;
  276. font-size: 25rpx;
  277. }
  278. .post-comment .goods .status {
  279. width: 105rpx;
  280. color: #b4282d;
  281. font-size: 25rpx;
  282. }
  283. .post-comment .rater {
  284. display: flex;
  285. flex-direction: row;
  286. height: 55rpx;
  287. }
  288. .post-comment .rater .rater-title {
  289. font-size: 29rpx;
  290. padding-right: 10rpx;
  291. }
  292. .post-comment .rater image {
  293. padding-left: 5rpx;
  294. height: 50rpx;
  295. width: 50rpx;
  296. }
  297. .post-comment .rater .rater-desc {
  298. font-size: 29rpx;
  299. padding-left: 10rpx;
  300. }
  301. .post-comment .input-box {
  302. height: 337.5rpx;
  303. width: 690rpx;
  304. position: relative;
  305. background: #fff;
  306. }
  307. .post-comment .input-box .content {
  308. position: absolute;
  309. top: 0;
  310. left: 0;
  311. display: block;
  312. background: #fff;
  313. font-size: 29rpx;
  314. border: 5px solid #f4f4f4;
  315. height: 300rpx;
  316. width: 650rpx;
  317. padding: 20rpx;
  318. }
  319. .post-comment .input-box .count {
  320. position: absolute;
  321. bottom: 20rpx;
  322. right: 20rpx;
  323. display: block;
  324. height: 30rpx;
  325. width: 50rpx;
  326. font-size: 29rpx;
  327. color: #999;
  328. }
  329. .post-comment .btns {
  330. height: 108rpx;
  331. }
  332. .post-comment .close {
  333. float: left;
  334. height: 108rpx;
  335. line-height: 108rpx;
  336. text-align: left;
  337. color: #666;
  338. padding: 0 30rpx;
  339. }
  340. .post-comment .post {
  341. float: right;
  342. height: 108rpx;
  343. line-height: 108rpx;
  344. text-align: right;
  345. padding: 0 30rpx;
  346. }
  347. .weui-uploader {
  348. margin-top: 50rpx;
  349. }
  350. .weui-uploader__hd {
  351. display: -webkit-box;
  352. display: -webkit-flex;
  353. display: flex;
  354. padding-bottom: 10px;
  355. -webkit-box-align: center;
  356. -webkit-align-items: center;
  357. align-items: center;
  358. }
  359. .weui-uploader__title {
  360. -webkit-box-flex: 1;
  361. -webkit-flex: 1;
  362. flex: 1;
  363. }
  364. .weui-uploader__info {
  365. color: #b2b2b2;
  366. }
  367. .weui-uploader__bd {
  368. margin-bottom: -4px;
  369. margin-right: -9px;
  370. overflow: hidden;
  371. }
  372. .weui-uploader__file {
  373. float: left;
  374. margin-right: 9px;
  375. margin-bottom: 9px;
  376. }
  377. .weui-uploader__img {
  378. display: block;
  379. width: 79px;
  380. height: 79px;
  381. }
  382. .weui-uploader__file_status {
  383. position: relative;
  384. }
  385. .weui-uploader__file_status:before {
  386. content: ' ';
  387. position: absolute;
  388. top: 0;
  389. right: 0;
  390. bottom: 0;
  391. left: 0;
  392. background-color: rgba(0, 0, 0, 0.5);
  393. }
  394. .weui-uploader__file-content {
  395. position: absolute;
  396. top: 50%;
  397. left: 50%;
  398. -webkit-transform: translate(-50%, -50%);
  399. transform: translate(-50%, -50%);
  400. color: #fff;
  401. }
  402. .weui-uploader__input-box {
  403. float: left;
  404. position: relative;
  405. margin-right: 9px;
  406. margin-bottom: 9px;
  407. width: 77px;
  408. height: 77px;
  409. border: 1px solid #d9d9d9;
  410. }
  411. .weui-uploader__input-box:after,
  412. .weui-uploader__input-box:before {
  413. content: ' ';
  414. position: absolute;
  415. top: 50%;
  416. left: 50%;
  417. -webkit-transform: translate(-50%, -50%);
  418. transform: translate(-50%, -50%);
  419. background-color: #d9d9d9;
  420. }
  421. .weui-uploader__input-box:before {
  422. width: 2px;
  423. height: 39.5px;
  424. }
  425. .weui-uploader__input-box:after {
  426. width: 39.5px;
  427. height: 2px;
  428. }
  429. .weui-uploader__input-box:active {
  430. border-color: #999;
  431. }
  432. .weui-uploader__input-box:active:after,
  433. .weui-uploader__input-box:active:before {
  434. background-color: #999;
  435. }
  436. .weui-uploader__input {
  437. position: absolute;
  438. z-index: 1;
  439. top: 0;
  440. left: 0;
  441. width: 100%;
  442. height: 100%;
  443. opacity: 0;
  444. }
  445. </style>