index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. <template>
  2. <view v-if="!isLoading" class="container" :style="appThemeStyle">
  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="scaleToFill"></image>
  10. </view>
  11. <!-- 商品信息 -->
  12. <view class="goods-content">
  13. <view class="goods-title"><text class="twoline-hide">{{ item.goods_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-camera"></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(reject)
  230. })
  231. }))
  232. .then(resolve, reject)
  233. })
  234. }
  235. }
  236. }
  237. </script>
  238. <style lang="scss" scoped>
  239. .container {
  240. // 设置ios刘海屏底部横线安全区域
  241. padding-bottom: calc(constant(safe-area-inset-bottom) + 140rpx);
  242. padding-bottom: calc(env(safe-area-inset-bottom) + 140rpx);
  243. }
  244. .goods-list {
  245. font-size: 28rpx;
  246. padding-top: 30rpx;
  247. }
  248. .goods-item {
  249. width: 94%;
  250. background: #fff;
  251. padding: 24rpx 24rpx;
  252. box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
  253. margin: 0 auto 30rpx auto;
  254. border-radius: 20rpx;
  255. .goods-detail {
  256. padding: 24rpx 20rpx;
  257. .left {
  258. .goods-image {
  259. display: block;
  260. width: 150rpx;
  261. height: 150rpx;
  262. }
  263. }
  264. .right {
  265. padding-left: 20rpx;
  266. }
  267. }
  268. .score-row {
  269. display: flex;
  270. justify-content: space-around;
  271. padding: 24rpx 20rpx;
  272. .score-item {
  273. display: flex;
  274. justify-content: center;
  275. align-items: center;
  276. &.score-praise {
  277. color: $main-bg;
  278. .score-icon {
  279. background: $main-bg;
  280. }
  281. }
  282. &.score-review {
  283. color: $vice-bg;
  284. .score-icon {
  285. background: $vice-bg;
  286. }
  287. }
  288. &.score-negative {
  289. color: #9b9b9b;
  290. .score-icon {
  291. background: #9b9b9b;
  292. }
  293. }
  294. .score {
  295. padding: 10rpx 20rpx 10rpx 10rpx;
  296. border-radius: 30rpx;
  297. .score-icon {
  298. margin-right: 10rpx;
  299. padding: 10rpx;
  300. border-radius: 50%;
  301. font-size: 30rpx;
  302. color: #fff;
  303. }
  304. }
  305. &.active {
  306. .score {
  307. color: #fff;
  308. }
  309. &.score-praise {
  310. .score {
  311. background: $main-bg;
  312. }
  313. }
  314. &.score-review {
  315. .score {
  316. background: $vice-bg;
  317. }
  318. }
  319. &.score-negative {
  320. .score {
  321. background: #9b9b9b;
  322. }
  323. }
  324. }
  325. }
  326. }
  327. // 评价内容
  328. .form-content {
  329. padding: 14rpx 10rpx;
  330. .textarea {
  331. width: 100%;
  332. height: 220rpx;
  333. padding: 12rpx;
  334. border: 1rpx solid #e8e8e8;
  335. border-radius: 5rpx;
  336. box-sizing: border-box;
  337. font-size: 26rpx;
  338. }
  339. }
  340. .image-list {
  341. padding: 0 20rpx;
  342. margin-top: 20rpx;
  343. margin-bottom: -20rpx;
  344. &:after {
  345. clear: both;
  346. content: " ";
  347. display: table;
  348. }
  349. .image {
  350. display: block;
  351. width: 100%;
  352. height: 100%;
  353. }
  354. .image-picker,
  355. .image-preview {
  356. width: 184rpx;
  357. height: 184rpx;
  358. margin-right: 30rpx;
  359. margin-bottom: 30rpx;
  360. float: left;
  361. &:nth-child(3n+0) {
  362. margin-right: 0;
  363. }
  364. }
  365. .image-picker {
  366. display: flex;
  367. flex-direction: column;
  368. justify-content: center;
  369. align-items: center;
  370. border: 1rpx dashed #ccc;
  371. color: #ccc;
  372. .choose-icon {
  373. font-size: 48rpx;
  374. margin-bottom: 6rpx;
  375. }
  376. .choose-text {
  377. font-size: 24rpx;
  378. }
  379. }
  380. .image-preview {
  381. position: relative;
  382. .image-delete {
  383. position: absolute;
  384. top: -15rpx;
  385. right: -15rpx;
  386. height: 42rpx;
  387. width: 42rpx;
  388. background: rgba(0, 0, 0, 0.64);
  389. border-radius: 50%;
  390. color: #fff;
  391. font-weight: bolder;
  392. font-size: 22rpx;
  393. z-index: 10;
  394. display: flex;
  395. justify-content: center;
  396. align-items: center;
  397. }
  398. }
  399. }
  400. }
  401. // 商品项
  402. .goods-main {
  403. display: flex;
  404. margin-bottom: 20rpx;
  405. // 商品图片
  406. .goods-image {
  407. width: 180rpx;
  408. height: 180rpx;
  409. .image {
  410. display: block;
  411. width: 100%;
  412. height: 100%;
  413. border-radius: 8rpx;
  414. }
  415. }
  416. // 商品内容
  417. .goods-content {
  418. flex: 1;
  419. padding-left: 16rpx;
  420. padding-top: 16rpx;
  421. .goods-title {
  422. font-size: 26rpx;
  423. max-height: 76rpx;
  424. }
  425. .goods-props {
  426. margin-top: 14rpx;
  427. color: #ababab;
  428. font-size: 24rpx;
  429. overflow: hidden;
  430. .goods-props-item {
  431. padding: 4rpx 16rpx;
  432. border-radius: 12rpx;
  433. background-color: #fcfcfc;
  434. }
  435. }
  436. }
  437. // 交易信息
  438. .goods-trade {
  439. padding-top: 16rpx;
  440. width: 150rpx;
  441. text-align: right;
  442. color: $uni-text-color-grey;
  443. font-size: 26rpx;
  444. .goods-price {
  445. vertical-align: bottom;
  446. margin-bottom: 16rpx;
  447. .unit {
  448. margin-right: -2rpx;
  449. font-size: 24rpx;
  450. }
  451. }
  452. }
  453. }
  454. // 底部操作栏
  455. .footer-fixed {
  456. position: fixed;
  457. bottom: var(--window-bottom);
  458. left: 0;
  459. right: 0;
  460. z-index: 11;
  461. // 设置ios刘海屏底部横线安全区域
  462. padding-bottom: constant(safe-area-inset-bottom);
  463. padding-bottom: env(safe-area-inset-bottom);
  464. .btn-wrapper {
  465. height: 140rpx;
  466. display: flex;
  467. align-items: center;
  468. padding: 0 20rpx;
  469. }
  470. .btn-item {
  471. flex: 1;
  472. font-size: 28rpx;
  473. height: 80rpx;
  474. color: #fff;
  475. border-radius: 50rpx;
  476. display: flex;
  477. justify-content: center;
  478. align-items: center;
  479. }
  480. .btn-item-main {
  481. background: linear-gradient(to right, $main-bg, $main-bg2);
  482. color: $main-text;
  483. // 禁用按钮
  484. &.disabled {
  485. opacity: 0.6;
  486. }
  487. }
  488. }
  489. </style>