index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ use: false }" :up="upOption" @up="upCallback">
  3. <!-- tab栏 -->
  4. <u-tabs :list="tabs" :is-scroll="false" :current="curTab" active-color="#FA2209" :duration="0.2" @change="onChangeTab" />
  5. <!-- 商品评价列表 -->
  6. <view class="comment-list">
  7. <view class="comment-item" v-for="(item, index) in list.data" :key="index">
  8. <view class="item-head">
  9. <!-- 用户信息 -->
  10. <view class="user-info">
  11. <avatar-image class="user-avatar" :url="item.user.avatar_url" :width="50" />
  12. <text class="user-name f-26">{{ item.user.nick_name }}</text>
  13. </view>
  14. <!-- 评星 -->
  15. <u-rate active-color="#f4a213" :current="rates[item.score]" :disabled="true" />
  16. <!-- 评价日期-->
  17. <view class="flex-box f-22 col-9 t-r">{{ item.create_time }}</view>
  18. </view>
  19. <!-- 评价内容 -->
  20. <view class="item-content m-top20">
  21. <text class="f-26">{{ item.content }}</text>
  22. </view>
  23. <!-- 评价图片 -->
  24. <view class="images-list clearfix" v-if="item.images.length">
  25. <view class="image-preview" v-for="(image, imgIdx) in item.images" :key="imgIdx">
  26. <image class="image" mode="aspectFill" :src="image.image_url" @click="onPreviewImages(index, imgIdx)"></image>
  27. </view>
  28. </view>
  29. <!-- 商品规格 -->
  30. <view class="goods-props clearfix">
  31. <view class="goods-props-item" v-for="(props, idx) in item.orderGoods.goods_props" :key="idx">
  32. <text class="group-name">{{ props.group.name }}: </text>
  33. <text>{{ props.value.name }};</text>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. </mescroll-body>
  39. </template>
  40. <script>
  41. import MescrollBody from '@/components/mescroll-uni/mescroll-body.vue'
  42. import MescrollMixin from '@/components/mescroll-uni/mescroll-mixins'
  43. import AvatarImage from '@/components/avatar-image'
  44. import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
  45. import * as CommentApi from '@/api/comment'
  46. const pageSize = 15
  47. const tabs = [{
  48. name: `全部`,
  49. scoreType: -1
  50. }, {
  51. name: `好评`,
  52. scoreType: 10
  53. }, {
  54. name: `中评`,
  55. scoreType: 20
  56. }, {
  57. name: `差评`,
  58. scoreType: 30
  59. }]
  60. export default {
  61. components: {
  62. MescrollBody,
  63. AvatarImage
  64. },
  65. mixins: [MescrollMixin],
  66. data() {
  67. return {
  68. // 当前商品ID
  69. goodsId: null,
  70. // 当前标签索引
  71. curTab: 0,
  72. // 评价列表数据
  73. list: getEmptyPaginateObj(),
  74. // 评价总数量
  75. total: { all: 0, negative: 0, praise: 0, review: 0 },
  76. // 评星数据转换
  77. rates: { 10: 5, 20: 3, 30: 1 },
  78. // 标签栏数据
  79. tabs,
  80. // 上拉加载配置
  81. upOption: {
  82. // 首次自动执行
  83. auto: true,
  84. // 每页数据的数量; 默认10
  85. page: { size: pageSize },
  86. // 数量要大于4条才显示无更多数据
  87. noMoreSize: 4,
  88. // 空布局
  89. empty: {
  90. tip: '亲,暂无相关商品评价'
  91. }
  92. },
  93. }
  94. },
  95. /**
  96. * 生命周期函数--监听页面加载
  97. */
  98. onLoad(options) {
  99. // 记录属性值
  100. this.goodsId = options.goodsId
  101. // 获取指定评分总数
  102. this.getTotal()
  103. },
  104. methods: {
  105. /**
  106. * 上拉加载的回调 (页面初始化时也会执行一次)
  107. * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
  108. * @param {Object} page
  109. */
  110. upCallback(page) {
  111. const app = this
  112. // 设置列表数据
  113. app.getCommentList(page.num)
  114. .then(list => {
  115. const curPageLen = list.data.length
  116. const totalSize = list.data.total
  117. app.mescroll.endBySize(curPageLen, totalSize)
  118. })
  119. .catch(() => app.mescroll.endErr())
  120. },
  121. // 加载评价列表数据
  122. getCommentList(pageNo = 1) {
  123. const app = this
  124. return new Promise((resolve, reject) => {
  125. CommentApi.list(app.goodsId, { scoreType: app.getScoreType(), page: pageNo }, { load: false })
  126. .then(result => {
  127. // 合并新数据
  128. const newList = result.data.list
  129. app.list.data = getMoreListData(newList, app.list, pageNo)
  130. resolve(newList)
  131. })
  132. })
  133. },
  134. // 评分类型
  135. getScoreType() {
  136. return this.tabs[this.curTab].scoreType
  137. },
  138. // 获取指定评分总数
  139. getTotal() {
  140. const app = this
  141. CommentApi.total(app.goodsId)
  142. .then(result => {
  143. // tab标签内容
  144. const total = result.data.total
  145. app.getTabs(total)
  146. })
  147. },
  148. // 获取tab标签内容
  149. getTabs(total) {
  150. const tabs = this.tabs
  151. tabs[0].name = `全部(${total.all})`
  152. tabs[1].name = `好评(${total.praise})`
  153. tabs[2].name = `中评(${total.review})`
  154. tabs[3].name = `差评(${total.negative})`
  155. },
  156. // 切换标签项
  157. onChangeTab(index) {
  158. const app = this
  159. // 设置当前选中的标签
  160. app.curTab = index
  161. // 刷新评价列表
  162. app.onRefreshList()
  163. },
  164. // 刷新评价列表
  165. onRefreshList() {
  166. this.list = getEmptyPaginateObj()
  167. setTimeout(() => {
  168. this.mescroll.resetUpScroll()
  169. }, 120)
  170. },
  171. // 预览评价图片
  172. onPreviewImages(dataIdx, imgIndex) {
  173. const app = this
  174. const images = app.list.data[dataIdx].images
  175. const imageUrls = images.map(item => item.image_url)
  176. uni.previewImage({
  177. current: imageUrls[imgIndex],
  178. urls: imageUrls
  179. })
  180. }
  181. }
  182. }
  183. </script>
  184. <style lang="scss" scoped>
  185. .comment-item {
  186. padding: 30rpx;
  187. box-sizing: border-box;
  188. border-bottom: 1rpx solid #f7f7f7;
  189. background: #fff;
  190. }
  191. .item-head {
  192. display: flex;
  193. align-items: center;
  194. // 用户信息
  195. .user-info {
  196. margin-right: 15rpx;
  197. display: flex;
  198. align-items: center;
  199. .user-avatar {
  200. margin-right: 15rpx;
  201. }
  202. .user-name {
  203. color: #999;
  204. }
  205. }
  206. }
  207. // 评价内容
  208. .item-content {
  209. font-size: 30rpx;
  210. color: #333;
  211. margin: 16rpx 0;
  212. }
  213. // 评价图片
  214. .images-list {
  215. &::after {
  216. clear: both;
  217. content: " ";
  218. display: table;
  219. }
  220. .image-preview {
  221. float: left;
  222. margin-bottom: 15rpx;
  223. margin-right: 15rpx;
  224. &:nth-child(3n+0) {
  225. margin-right: 0;
  226. }
  227. .image {
  228. display: block;
  229. width: 220rpx;
  230. height: 220rpx;
  231. }
  232. }
  233. }
  234. // 商品规格
  235. .goods-props {
  236. font-size: 24rpx;
  237. color: #999;
  238. .goods-props-item {
  239. float: left;
  240. .group-name {
  241. margin-right: 6rpx;
  242. }
  243. }
  244. }
  245. </style>