index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. <template>
  2. <view v-if="!isLoading" class="container">
  3. <view class="goods-list">
  4. <view class="goods-item" v-for="(item, index) in goodsList" :key="index">
  5. <!-- 商品详情 -->
  6. <view class="goods-main">
  7. <!-- 商品图片 -->
  8. <view class="goods-image">
  9. <image class="image" :src="item.goods_image" mode="widthFix"></image>
  10. </view>
  11. <!-- 商品信息 -->
  12. <view class="goods-content">
  13. <view class="goods-title twolist-hidden"><text>{{ item.name }}</text></view>
  14. <view class="goods-props clearfix">
  15. <view class="goods-props-item" v-for="(props, idx) in item.goods_props" :key="idx">
  16. <text>{{ props.value.name }}</text>
  17. </view>
  18. </view>
  19. </view>
  20. <!-- 交易信息 -->
  21. <view class="goods-trade">
  22. <view class="goods-price">
  23. <text class="unit">¥</text>
  24. <text class="value">{{ item.goods_price }}</text>
  25. </view>
  26. <view class="goods-num">
  27. <text>×{{ item.total_num }}</text>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 选择评价 -->
  32. <view class="score-row">
  33. <view class="score-item score-praise" :class="{ active: formData[index].score == 10 }" @click="setScore(index, 10)">
  34. <view class="score">
  35. <text class="score-icon iconfont icon-haoping"></text>
  36. <text class="score-text">好评</text>
  37. </view>
  38. </view>
  39. <view class="score-item score-review" :class="{ active: formData[index].score == 20 }" @click="setScore(index, 20)">
  40. <view class="score">
  41. <text class="score-icon iconfont icon-zhongping"></text>
  42. <text class="score-text">中评</text>
  43. </view>
  44. </view>
  45. <view class="score-item score-negative" :class="{ active: formData[index].score == 30 }" @click="setScore(index, 30)">
  46. <view class="score">
  47. <text class="score-icon iconfont icon-chaping"></text>
  48. <text class="score-text">差评</text>
  49. </view>
  50. </view>
  51. </view>
  52. <!-- 评价内容 -->
  53. <view class="form-content">
  54. <textarea class="textarea" v-model="formData[index].content" maxlength="500" placeholder="请输入评价内容 (留空则不评价)"></textarea>
  55. </view>
  56. <!-- 图片列表 -->
  57. <view class="image-list">
  58. <view class="image-preview" v-for="(image, imageIndex) in formData[index].imageList" :key="imageIndex">
  59. <text class="image-delete iconfont icon-shanchu" @click="deleteImage(index, imageIndex)"></text>
  60. <image class="image" mode="aspectFill" :src="image.path"></image>
  61. </view>
  62. <view v-if="!formData[index].imageList || formData[index].imageList.length < maxImageLength" class="image-picker"
  63. @click="chooseImage(index)">
  64. <text class="choose-icon iconfont icon-tubiao_xiangji"></text>
  65. <text class="choose-text">上传图片</text>
  66. </view>
  67. </view>
  68. </view>
  69. </view>
  70. <!-- 底部操作按钮 -->
  71. <view class="footer-fixed">
  72. <view class="btn-wrapper">
  73. <view class="btn-item btn-item-main" :class="{ disabled }" @click="handleSubmit()">确认提交</view>
  74. </view>
  75. </view>
  76. </view>
  77. </template>
  78. <script>
  79. import * as UploadApi from '@/api/upload'
  80. import * as OrderCommentApi from '@/api/order/comment'
  81. const maxImageLength = 6
  82. export default {
  83. data() {
  84. return {
  85. // 正在加载
  86. isLoading: true,
  87. // 当前订单ID
  88. orderId: null,
  89. // 待评价商品列表
  90. goodsList: [],
  91. // 表单数据
  92. formData: [],
  93. // 最大图片数量
  94. maxImageLength,
  95. // 按钮禁用
  96. disabled: false
  97. }
  98. },
  99. /**
  100. * 生命周期函数--监听页面加载
  101. */
  102. onLoad({ orderId }) {
  103. this.orderId = orderId
  104. // 获取待评价商品列表
  105. this.getGoodsList()
  106. },
  107. methods: {
  108. // 获取待评价商品列表
  109. getGoodsList() {
  110. const app = this
  111. app.isLoading = true
  112. OrderCommentApi.list(app.orderId)
  113. .then(result => {
  114. app.goodsList = result.data.goodsList
  115. app.initFormData()
  116. app.isLoading = false
  117. })
  118. },
  119. // 初始化form数据
  120. initFormData() {
  121. const { goodsList } = this
  122. const formData = goodsList.map(item => {
  123. return {
  124. goods_id: item.goods_id,
  125. order_goods_id: item.order_goods_id,
  126. score: 10,
  127. content: '',
  128. imageList: [],
  129. uploaded: []
  130. }
  131. })
  132. this.formData = formData
  133. },
  134. // 设置评分
  135. setScore(index, score) {
  136. this.formData[index].score = score
  137. },
  138. // 选择图片
  139. chooseImage(index) {
  140. const app = this
  141. const oldImageList = app.formData[index].imageList
  142. // 选择图片
  143. uni.chooseImage({
  144. count: maxImageLength - oldImageList.length,
  145. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  146. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  147. success({ tempFiles }) {
  148. // tempFiles = [{path:'xxx', size:100}]
  149. app.formData[index].imageList = oldImageList.concat(tempFiles)
  150. }
  151. });
  152. },
  153. // 删除图片
  154. deleteImage(index, imageIndex) {
  155. this.formData[index].imageList.splice(imageIndex, 1)
  156. },
  157. // 表单提交
  158. handleSubmit() {
  159. const app = this
  160. // 判断是否重复提交
  161. if (app.disabled === true) return false
  162. // 按钮禁用
  163. app.disabled = true
  164. // 判断是否需要上传图片
  165. const imagesLength = app.getImagesLength()
  166. if (imagesLength > 0) {
  167. app.uploadFile()
  168. .then(() => {
  169. console.log('then')
  170. app.onSubmit()
  171. })
  172. .catch(err => {
  173. console.log('catch')
  174. app.disabled = false
  175. if (err.statusCode !== 0) {
  176. app.$toast(err.errMsg)
  177. }
  178. console.log('err', err)
  179. })
  180. } else {
  181. app.onSubmit()
  182. }
  183. },
  184. // 统计图片数量
  185. getImagesLength() {
  186. const { formData } = this
  187. let imagesLength = 0
  188. formData.forEach(item => {
  189. if (item.content.trim()) {
  190. imagesLength += item.imageList.length
  191. }
  192. })
  193. return imagesLength
  194. },
  195. // 提交到后端
  196. onSubmit() {
  197. const app = this
  198. OrderCommentApi.submit(app.orderId, app.formData)
  199. .then(result => {
  200. app.$toast(result.message)
  201. setTimeout(() => {
  202. app.disabled = false
  203. uni.navigateBack()
  204. }, 1500)
  205. })
  206. .catch(err => app.disabled = false)
  207. },
  208. // 上传图片
  209. uploadFile() {
  210. const app = this
  211. const { formData } = app
  212. // 整理上传文件路径
  213. const files = []
  214. formData.forEach((item, index) => {
  215. if (item.content.trim() && item.imageList.length) {
  216. const images = item.imageList.map(image => image)
  217. files.push({ formDataIndex: index, images })
  218. }
  219. })
  220. // 批量上传
  221. return new Promise((resolve, reject) => {
  222. Promise.all(files.map((file, index) => {
  223. return new Promise((resolve, reject) => {
  224. UploadApi.image(file.images)
  225. .then(fileIds => {
  226. app.formData[index].uploaded = fileIds
  227. resolve(fileIds)
  228. })
  229. .catch(err => reject(err))
  230. })
  231. }))
  232. .then(resolve, reject)
  233. })
  234. }
  235. }
  236. }
  237. </script>
  238. <style lang="scss" scoped>
  239. .container {
  240. padding-bottom: 100rpx;
  241. }
  242. .goods-list {
  243. font-size: 28rpx;
  244. padding-top: 30rpx;
  245. }
  246. .goods-item {
  247. width: 94%;
  248. background: #fff;
  249. padding: 24rpx 24rpx;
  250. box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
  251. margin: 0 auto 30rpx auto;
  252. border-radius: 20rpx;
  253. .goods-detail {
  254. padding: 24rpx 20rpx;
  255. .left {
  256. .goods-image {
  257. display: block;
  258. width: 150rpx;
  259. height: 150rpx;
  260. }
  261. }
  262. .right {
  263. padding-left: 20rpx;
  264. }
  265. }
  266. .score-row {
  267. display: flex;
  268. justify-content: space-around;
  269. padding: 24rpx 20rpx;
  270. .score-item {
  271. display: flex;
  272. justify-content: center;
  273. align-items: center;
  274. &.score-praise {
  275. color: #ff4544;
  276. .score-icon {
  277. background: #ff4544;
  278. }
  279. }
  280. &.score-review {
  281. color: #fcb500;
  282. .score-icon {
  283. background: #fcb500;
  284. }
  285. }
  286. &.score-negative {
  287. color: #9b9b9b;
  288. .score-icon {
  289. background: #9b9b9b;
  290. }
  291. }
  292. .score {
  293. padding: 10rpx 20rpx 10rpx 10rpx;
  294. border-radius: 30rpx;
  295. .score-icon {
  296. margin-right: 10rpx;
  297. padding: 10rpx;
  298. border-radius: 50%;
  299. font-size: 30rpx;
  300. color: #fff;
  301. }
  302. }
  303. &.active {
  304. .score {
  305. color: #fff;
  306. }
  307. &.score-praise {
  308. .score {
  309. background: #ff4544;
  310. }
  311. }
  312. &.score-review {
  313. .score {
  314. background: #fcb500;
  315. }
  316. }
  317. &.score-negative {
  318. .score {
  319. background: #9b9b9b;
  320. }
  321. }
  322. }
  323. }
  324. }
  325. // 评价内容
  326. .form-content {
  327. padding: 14rpx 10rpx;
  328. .textarea {
  329. width: 100%;
  330. height: 220rpx;
  331. padding: 12rpx;
  332. border: 1rpx solid #e8e8e8;
  333. border-radius: 5rpx;
  334. box-sizing: border-box;
  335. font-size: 26rpx;
  336. }
  337. }
  338. .image-list {
  339. padding: 0 20rpx;
  340. margin-top: 20rpx;
  341. margin-bottom: -20rpx;
  342. &:after {
  343. clear: both;
  344. content: " ";
  345. display: table;
  346. }
  347. .image {
  348. display: block;
  349. width: 100%;
  350. height: 100%;
  351. }
  352. .image-picker,
  353. .image-preview {
  354. width: 184rpx;
  355. height: 184rpx;
  356. margin-right: 30rpx;
  357. margin-bottom: 30rpx;
  358. float: left;
  359. &:nth-child(3n+0) {
  360. margin-right: 0;
  361. }
  362. }
  363. .image-picker {
  364. display: flex;
  365. flex-direction: column;
  366. justify-content: center;
  367. align-items: center;
  368. border: 1rpx dashed #ccc;
  369. color: #ccc;
  370. .choose-icon {
  371. font-size: 48rpx;
  372. margin-bottom: 6rpx;
  373. }
  374. .choose-text {
  375. font-size: 24rpx;
  376. }
  377. }
  378. .image-preview {
  379. position: relative;
  380. .image-delete {
  381. position: absolute;
  382. top: -15rpx;
  383. right: -15rpx;
  384. height: 42rpx;
  385. width: 42rpx;
  386. line-height: 42rpx;
  387. background: rgba(0, 0, 0, 0.64);
  388. border-radius: 50%;
  389. color: #fff;
  390. font-weight: bolder;
  391. font-size: 22rpx;
  392. z-index: 10;
  393. text-align: center;
  394. }
  395. }
  396. }
  397. }
  398. // 商品项
  399. .goods-main {
  400. display: flex;
  401. margin-bottom: 20rpx;
  402. // 商品图片
  403. .goods-image {
  404. width: 180rpx;
  405. height: 180rpx;
  406. .image {
  407. display: block;
  408. width: 100%;
  409. height: 100%;
  410. border-radius: 8rpx;
  411. }
  412. }
  413. // 商品内容
  414. .goods-content {
  415. flex: 1;
  416. padding-left: 16rpx;
  417. padding-top: 16rpx;
  418. .goods-title {
  419. font-size: 26rpx;
  420. max-height: 76rpx;
  421. }
  422. .goods-props {
  423. margin-top: 14rpx;
  424. height: 40rpx;
  425. color: #ababab;
  426. font-size: 24rpx;
  427. overflow: hidden;
  428. .goods-props-item {
  429. display: inline-block;
  430. margin-right: 14rpx;
  431. padding: 4rpx 16rpx;
  432. border-radius: 12rpx;
  433. background-color: #F5F5F5;
  434. width: auto;
  435. }
  436. }
  437. }
  438. // 交易信息
  439. .goods-trade {
  440. padding-top: 16rpx;
  441. width: 150rpx;
  442. text-align: right;
  443. color: $uni-text-color-grey;
  444. font-size: 26rpx;
  445. .goods-price {
  446. vertical-align: bottom;
  447. margin-bottom: 16rpx;
  448. .unit {
  449. margin-right: -2rpx;
  450. font-size: 24rpx;
  451. }
  452. }
  453. }
  454. }
  455. // 底部操作栏
  456. .footer-fixed {
  457. position: fixed;
  458. bottom: var(--window-bottom);
  459. left: 0;
  460. right: 0;
  461. height: 180rpx;
  462. padding-bottom: 30rpx;
  463. z-index: 11;
  464. box-shadow: 0 -4rpx 40rpx 0 rgba(144, 52, 52, 0.1);
  465. background: #fff;
  466. .btn-wrapper {
  467. height: 100%;
  468. display: flex;
  469. align-items: center;
  470. padding: 0 20rpx;
  471. }
  472. .btn-item {
  473. flex: 1;
  474. font-size: 28rpx;
  475. height: 80rpx;
  476. line-height: 90rpx;
  477. text-align: center;
  478. color: #fff;
  479. border-radius: 8rpx;
  480. }
  481. .btn-item-main {
  482. background: linear-gradient(to right, #f9211c, #ff6335);
  483. // 禁用按钮
  484. &.disabled {
  485. background: #ff9779;
  486. }
  487. }
  488. }
  489. </style>