index.vue 7.1 KB

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