index.vue 14 KB

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