uni-cms-article-list.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <template>
  2. <unicloud-db
  3. ref='udb'
  4. v-slot:default="{ data, pagination, hasMore, loading, error }"
  5. :collection="collectionList"
  6. :page-size="10"
  7. :loadtime="loadTime"
  8. orderby="publish_date desc"
  9. @load="onListLoad"
  10. @error="onListLoadError"
  11. >
  12. <view
  13. v-if="networkType == 'none'"
  14. class="error-box"
  15. @click="checkNetwork"
  16. >
  17. <image class="disconnect-icon" src="/uni_modules/uni-cms-article/static/disconnection.png" mode="widthFix"></image>
  18. <text class="tip-text">当前网络不可用,请点击重试</text>
  19. </view>
  20. <list-view
  21. v-else
  22. class="list-view"
  23. :scroll-y="true"
  24. :refresher-enabled="refresherEnabled"
  25. refresher-default-style="none"
  26. :refresher-triggered="refresherTriggered"
  27. @refresherpulling="refresherpulling"
  28. @refresherrefresh="refresherrefresh"
  29. @scrolltolower="scrolltolower"
  30. >
  31. <list-item slot="refresher" class="refresh-box">
  32. <text class="text">{{ refreshText[refreshState] }}</text>
  33. </list-item>
  34. <!-- 列表渲染 -->
  35. <list-item
  36. v-for="item in articleList"
  37. :class="['list-item', `list-item__thumbnail-${item.thumbnail.length}`]"
  38. :key="item._id"
  39. @click="goToDetailPage(item)"
  40. >
  41. <template v-if="item.thumbnail.length == 0">
  42. <view class="list-item__content">
  43. <view class="list-item__content-title">
  44. <text class="text">{{ item.title }}</text>
  45. </view>
  46. <view class="list-item__content-info">
  47. <view class="list-item__author">
  48. <text class="text">{{ item!.user_id!.length > 0 ? item.user_id[0].nickname : '' }}</text>
  49. </view>
  50. <view class="list-item__publish-date">
  51. <text class="text">{{ publishTime(item.publish_date) }}</text>
  52. </view>
  53. </view>
  54. </view>
  55. </template>
  56. <template v-if="item.thumbnail.length == 1">
  57. <view class="list-item__content">
  58. <view class="list-item__content-title">
  59. <text class="text">{{ item.title }}</text>
  60. </view>
  61. <view class="list-item__content-info">
  62. <view class="list-item__author">
  63. <text class="text">{{ item!.user_id!.length > 0 ? item.user_id[0].nickname : '' }}</text>
  64. </view>
  65. <view class="list-item__publish-date">
  66. <text class="text">{{ publishTime(item.publish_date) }}</text>
  67. </view>
  68. </view>
  69. </view>
  70. <view class="list-item__thumbnails">
  71. <image
  72. v-for="image in item.thumbnail"
  73. :src="image"
  74. mode="aspectFill"
  75. class="list-item__img"
  76. ></image>
  77. </view>
  78. </template>
  79. <template v-if="item.thumbnail.length == 3">
  80. <view class="list-item__content">
  81. <view class="list-item__content-title">
  82. <text>{{ item.title }}</text>
  83. </view>
  84. <view class="list-item__thumbnails">
  85. <image
  86. v-for="image in item.thumbnail"
  87. :src="image"
  88. mode="aspectFill"
  89. class="list-item__img"
  90. ></image>
  91. </view>
  92. <view class="list-item__content-info">
  93. <view class="list-item__author">
  94. <text class="text">{{ item!.user_id!.length > 0 ? item.user_id[0].nickname : '' }}</text>
  95. </view>
  96. <view class="list-item__publish-date">
  97. <text class="text">{{ publishTime(item.publish_date) }}</text>
  98. </view>
  99. </view>
  100. </view>
  101. </template>
  102. </list-item>
  103. <list-item class="load-state">
  104. <text class="text">{{ loading ? '加载中...' : (hasMore ? '上拉加载更多' : '没有更多数据了') }}</text>
  105. </list-item>
  106. </list-view>
  107. </unicloud-db>
  108. </template>
  109. <script lang="uts">
  110. type ArticleAuthor = {
  111. _id: string
  112. nickname: string
  113. }
  114. type ArticleItem = {
  115. _id: string
  116. title: string
  117. publish_date: number
  118. thumbnail: string[]
  119. user_id: ArticleAuthor[]
  120. }
  121. import {parseImageUrl} from "@/uni_modules/uni-cms-article/common/parse-image-url.uts";
  122. import translatePublishTime from "@/uni_modules/uni-cms-article/common/publish-time.uts";
  123. import type {ParseImageUrlResult} from '@/uni_modules/uni-cms-article/common/parse-image-url.uts'
  124. export default {
  125. name: "uni-cms-article-list",
  126. emits: ['onRefresh', 'onLoadMore'],
  127. props: {
  128. collectionList: {
  129. type: Array as any[],
  130. default: (): any[] => []
  131. },
  132. loadTime: {
  133. type: String,
  134. default: 'auto'
  135. },
  136. refresherEnabled: {
  137. type: Boolean,
  138. default: false
  139. }
  140. },
  141. data() {
  142. return {
  143. articleList: [] as ArticleItem[],
  144. refresherTriggered: false,
  145. refreshState: 0,
  146. refreshText: [
  147. '继续下拉执行刷新',
  148. '释放立即刷新',
  149. '正在加载中',
  150. '加载成功'
  151. ],
  152. networkType: ""
  153. }
  154. },
  155. mounted () {
  156. this.checkNetwork()
  157. },
  158. methods: {
  159. checkNetwork() {
  160. uni.getNetworkType({
  161. success: (res) => {
  162. this.networkType = res.networkType;
  163. }
  164. });
  165. },
  166. publishTime(timestamp: number): string {
  167. return translatePublishTime(timestamp)
  168. },
  169. async onListLoad(data: UTSJSONObject[], ended: boolean, pagination: UTSJSONObject): Promise<void> {
  170. const listData: ArticleItem[] = data.map((item: UTSJSONObject): ArticleItem => {
  171. let articleItem: ArticleItem = {
  172. _id: item.getString('_id')!,
  173. title: item.getString('title')!,
  174. publish_date: item.getNumber('publish_date')!,
  175. thumbnail: [],
  176. user_id: item.getArray<ArticleAuthor>('user_id')! as ArticleAuthor[]
  177. }
  178. if (typeof item.getAny('thumbnail') === 'string') {
  179. articleItem.thumbnail = [item.getAny('thumbnail')! as string]
  180. } else {
  181. articleItem.thumbnail = item.getArray<string>('thumbnail')!
  182. }
  183. return articleItem
  184. })
  185. // 处理cloud://文件链接
  186. for (let i = 0; i < listData.length; i++) {
  187. const article = listData[i]
  188. const parseImages = await parseImageUrl(article.thumbnail)
  189. if (parseImages != null) {
  190. article.thumbnail = parseImages.map((image: ParseImageUrlResult): string => image.src)
  191. }
  192. }
  193. this.articleList = pagination.getNumber('current') == 1 ? listData : this.articleList.concat(listData)
  194. },
  195. refresherrefresh() {
  196. this.refresherTriggered = true
  197. this.refreshState = 2;
  198. (this.$refs['udb'] as UniCloudDBElement)!.loadData({
  199. clear: true,
  200. success: (_: any) => {
  201. this.refresherTriggered = false
  202. this.refreshState = 3
  203. }
  204. })
  205. },
  206. refresherpulling(e: RefresherEvent) {
  207. if (e.detail.dy.toDouble() == 0.0) {
  208. this.refreshState = 0
  209. } else if (e.detail.dy > 45) {
  210. this.refreshState = 1
  211. }
  212. },
  213. scrolltolower() {
  214. (this.$refs['udb'] as UniCloudDBElement)!.loadMore()
  215. },
  216. reLoadList() {
  217. (this.$refs['udb'] as UniCloudDBElement)!.loadData({
  218. clear: true
  219. })
  220. },
  221. goToDetailPage(article: ArticleItem) {
  222. uni.navigateTo({
  223. url: `/uni_modules/uni-cms-article/pages/detail/detail?id=${article._id}&title=${article.title}`
  224. })
  225. },
  226. onListLoadError () {
  227. this.checkNetwork()
  228. }
  229. }
  230. }
  231. </script>
  232. <style scoped lang="scss">
  233. .refresh-box {
  234. display: flex;
  235. align-items: center;
  236. justify-content: center;
  237. .text {
  238. padding: 30rpx 0;
  239. font-size: 26rpx;
  240. color: #999999;
  241. }
  242. }
  243. .error-box {
  244. width: 100%;
  245. height: 100%;
  246. display: flex;
  247. flex-direction: column;
  248. margin: 0 auto;
  249. align-items: center;
  250. justify-content: center;
  251. .disconnect-icon {
  252. width: 200rpx;
  253. margin: 0 auto;
  254. }
  255. .tip-text {
  256. font-size: 26rpx;
  257. color: #333;
  258. margin-top: 40rpx;
  259. }
  260. }
  261. .load-state {
  262. height: 90rpx;
  263. display: flex;
  264. align-items: center;
  265. justify-content: center;
  266. .text {
  267. font-size: 28rpx;
  268. color: #999999;
  269. }
  270. }
  271. .list-view {
  272. height: 100%;
  273. padding: 0 20rpx;
  274. }
  275. .list-item {
  276. display: flex;
  277. flex-direction: row;
  278. align-items: center;
  279. padding: 20rpx 0;
  280. border-bottom: #f5f5f5 solid 1px;
  281. &__thumbnail-1 {
  282. .list-item__content-title {
  283. height: 88rpx;
  284. }
  285. }
  286. &__thumbnail-3 {
  287. .list-item__thumbnails {
  288. display: flex;
  289. flex-direction: row;
  290. margin: 20rpx -10rpx;
  291. margin-left: -10rpx;
  292. margin-bottom: 0;
  293. }
  294. .list-item__img {
  295. margin: 0 10rpx;
  296. flex: 1;
  297. }
  298. }
  299. &__thumbnails {
  300. margin-left: 20rpx;
  301. }
  302. &__img {
  303. width: 240rpx;
  304. height: 160rpx;
  305. border-radius: 8rpx;
  306. }
  307. &__content {
  308. display: flex;
  309. justify-content: space-between;
  310. flex-direction: column;
  311. flex: 1;
  312. height: 100%;
  313. &-title {
  314. overflow: hidden;
  315. .text {
  316. font-size: 30rpx;
  317. color: #333333;
  318. line-height: 44rpx;
  319. }
  320. }
  321. &-info {
  322. display: flex;
  323. flex-direction: row;
  324. align-items: center;
  325. margin-top: 20rpx;
  326. }
  327. }
  328. &__author,
  329. &__publish-date {
  330. .text {
  331. font-size: 24rpx;
  332. color: #bbbbbb;
  333. }
  334. }
  335. &__author {
  336. margin-right: 14rpx;
  337. }
  338. }
  339. </style>