list.nvue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <template>
  2. <view class="pages">
  3. <view class="placeholder-bar">
  4. <statusBar></statusBar>
  5. <view :style="{ height: `${navBarHeight}px` }"></view>
  6. </view>
  7. <view class="nav-box">
  8. <!-- #ifndef H5 -->
  9. <statusBar></statusBar>
  10. <!-- #endif -->
  11. <view class="nav" :style="{ height: `${navBarHeight}px` }">
  12. <!-- #ifdef MP -->
  13. <view class="mp-button-left-placeholder" :style="{ width: `${mpButtonLeftPlaceholderSize}px` }"></view>
  14. <!-- #endif -->
  15. <!-- 搜索功能 -->
  16. <view class="uni-search-box">
  17. <uni-search-bar ref="searchBar" radius="100" cancelButton="none" disabled
  18. :placeholder="inputPlaceholder"/>
  19. <view class="cover-search-bar" @click="searchClick"></view>
  20. </view>
  21. <!-- #ifdef MP -->
  22. <view class="mp-button-placeholder" :style="{ width: `${mpButtonPlaceholderSize}px` }"></view>
  23. <!-- #endif -->
  24. </view>
  25. </view>
  26. <unicloud-db ref='udb' v-slot:default="{ pagination, hasMore, loading, error, options }" @error="onqueryerror"
  27. :collection="colList" :page-size="10" orderby="publish_date desc" @load="listLoad">
  28. <!-- 基于 uni-list 的页面布局 field="user_id.nickname"-->
  29. <!-- #ifdef APP-NVUE -->
  30. <list class="uni-list" :border="false" :style="{ height: listHeight }">
  31. <!-- #endif -->
  32. <!-- #ifndef APP-NVUE -->
  33. <scroll-view
  34. scroll-y
  35. class="uni-list"
  36. refresher-enabled
  37. :refresher-triggered="loadType=== 'refresh'"
  38. :style="{ height: listHeight }"
  39. @refresherrefresh="refresh"
  40. @scrolltolower="loadMore"
  41. >
  42. <!-- #endif -->
  43. <!-- #ifdef APP-NVUE -->
  44. <refresh-box :loading="loading" @refresh="refresh"></refresh-box>
  45. <!-- #endif -->
  46. <!-- 列表渲染 -->
  47. <template v-for="item in listData">
  48. <not-cover v-if="item.thumbnail && item.thumbnail.length === 0" :data="item"></not-cover>
  49. <right-small-cover v-else-if="item.thumbnail && item.thumbnail.length === 1"
  50. :data="item"></right-small-cover>
  51. <three-cover v-else-if="item.thumbnail && item.thumbnail.length === 3"
  52. :data="item"></three-cover>
  53. </template>
  54. <!-- 加载状态:上拉加载更多,加载中,没有更多数据了,加载错误 -->
  55. <!-- #ifdef APP-PLUS -->
  56. <uni-list-item>
  57. <template v-slot:body>
  58. <!-- #endif -->
  59. <uni-load-state @networkResume="refresh"
  60. :state="{ data: listData, pagination, hasMore, loading, error }"
  61. @loadMore="loadMore">
  62. </uni-load-state>
  63. <!-- #ifdef APP-PLUS -->
  64. </template>
  65. </uni-list-item>
  66. <!-- #endif -->
  67. <!-- #ifndef APP-NVUE -->
  68. </scroll-view>
  69. <!-- #endif -->
  70. <!-- #ifdef APP-NVUE -->
  71. </list>
  72. <!-- #endif -->
  73. </unicloud-db>
  74. </view>
  75. </template>
  76. <script>
  77. import statusBar from "@/uni_modules/uni-nav-bar/components/uni-nav-bar/uni-status-bar";
  78. import translatePublishTime from "@/uni_modules/uni-cms-article/common/publish-time";
  79. import refreshBox from "@/uni_modules/uni-cms-article/components/refresh-box/refreshBox.nvue";
  80. import notCover from "@/uni_modules/uni-cms-article/components/list-template/not-cover.vue";
  81. import rightSmallCover from "@/uni_modules/uni-cms-article/components/list-template/right-small-cover.vue";
  82. import threeCover from "@/uni_modules/uni-cms-article/components/list-template/three-cover.vue";
  83. import {parseImageUrl} from "@/uni_modules/uni-cms-article/common/parse-image-url";
  84. const db = uniCloud.database();
  85. const articleDBName = 'uni-cms-articles'
  86. const userDBName = 'uni-id-users'
  87. export default {
  88. components: {
  89. statusBar,
  90. refreshBox,
  91. notCover,
  92. rightSmallCover,
  93. threeCover
  94. },
  95. computed: {
  96. // 根据当前语言返回不同的搜索框占位符
  97. inputPlaceholder(e) {
  98. if (uni.getStorageSync('CURRENT_LANG') == "en") {
  99. return 'Please enter the search content' // 英文
  100. } else {
  101. return '请输入搜索内容' // 中文
  102. }
  103. },
  104. // 连表查询,返回两个集合的查询结果
  105. colList() {
  106. return [
  107. db.collection(articleDBName).where(this.where).field('thumbnail,title,publish_date,user_id').getTemp(), // 文章集合
  108. db.collection(userDBName).field('_id,nickname').getTemp() // 用户集合
  109. ]
  110. }
  111. },
  112. data() {
  113. return {
  114. where: '"article_status" == 1', // 查询条件
  115. showRefresh: false, // 是否显示刷新按钮
  116. listHeight: 0, // 列表高度
  117. mpButtonLeftPlaceholderSize: 0, // 小程序左侧icon占位大小
  118. mpButtonPlaceholderSize: 87, // 小程序导航栏按钮占位大小
  119. navBarHeight: 44, // 导航栏高度
  120. refreshStatus: 0, // 刷新状态 0: 未刷新 1: 刷新中 2: 刷新完成
  121. listData: [], // 列表数据
  122. loadType: null
  123. }
  124. },
  125. async onReady() {
  126. // #ifdef MP
  127. this.initNavBarSize() // 初始化导航栏大小
  128. // #endif
  129. /* 可用窗口高度 - 搜索框高 - 状态栏高 */
  130. this.listHeight = uni.getSystemInfoSync().windowHeight - uni.getSystemInfoSync().statusBarHeight - this.navBarHeight + 'px'; // 计算列表高度
  131. },
  132. methods: {
  133. async listLoad(data) {
  134. const listData = data.map(item => {
  135. if (typeof item.thumbnail === 'string') {
  136. item.thumbnail = [item.thumbnail]
  137. }
  138. return item
  139. })
  140. // 处理腾讯云文件链接
  141. for (const article of listData) {
  142. const parseImages = await parseImageUrl(article.thumbnail)
  143. article.thumbnail = parseImages ? parseImages.map(image => image.src): []
  144. }
  145. this.listData = this.loadType === 'loadMore' ? this.listData.concat(listData) : listData
  146. this.loadType = null
  147. },
  148. // 初始化导航栏大小
  149. initNavBarSize() {
  150. // 获取小程序导航栏按钮信息
  151. // #ifdef MP-TOUTIAO
  152. let menuButtonInfo = tt.getCustomButtonBoundingClientRect()
  153. menuButtonInfo.width = menuButtonInfo.capsule.width // 小程序按钮区域中使用的按钮宽度
  154. this.mpButtonLeftPlaceholderSize = menuButtonInfo.leftIcon.width + 10
  155. // #endif
  156. // #ifndef MP-TOUTIAO
  157. let menuButtonInfo = uni.getMenuButtonBoundingClientRect()
  158. // #endif
  159. // 计算小程序导航栏按钮占位大小
  160. this.mpButtonPlaceholderSize = menuButtonInfo.width + 10
  161. // 获取系统信息,判断是否为 iOS 系统,设置导航栏高度
  162. this.navBarHeight = uni.getSystemInfoSync().system.toLowerCase().includes('ios') ? 44 : 48
  163. },
  164. // 格式化时间戳
  165. publishTime(timestamp) {
  166. return translatePublishTime(timestamp)
  167. },
  168. // 点击搜索框
  169. searchClick(e) {
  170. uni.hideKeyboard();
  171. uni.navigateTo({
  172. url: '/uni_modules/uni-cms-article/pages/search/search'
  173. });
  174. },
  175. // 重试
  176. retry() {
  177. this.refresh()
  178. },
  179. // 刷新
  180. refresh() {
  181. this.loadType = 'refresh'
  182. this.$refs.udb.loadData({
  183. clear: true
  184. }, () => {
  185. uni.stopPullDownRefresh()
  186. // #ifdef APP-NVUE
  187. this.showRefresh = false
  188. // #endif
  189. })
  190. },
  191. // 加载更多
  192. loadMore() {
  193. this.loadType = 'loadMore'
  194. this.$refs.udb.loadMore()
  195. },
  196. // 查询出错
  197. onqueryerror(e) {
  198. console.error(e);
  199. }
  200. },
  201. // #ifdef H5
  202. // 下拉刷新
  203. onPullDownRefresh() {
  204. this.refresh()
  205. },
  206. // #endif
  207. }
  208. </script>
  209. <style scoped>
  210. /* #ifndef APP-NVUE */
  211. .pages view {
  212. display: flex;
  213. box-sizing: border-box;
  214. flex-direction: column;
  215. }
  216. /* #endif */
  217. .pages {
  218. background-color: #FFFFFF;
  219. }
  220. .refresh {
  221. text-align: center;
  222. }
  223. .nav-box {
  224. background-color: #FFFFFF;
  225. position: fixed;
  226. top: 0;
  227. left: 0;
  228. right: 0;
  229. /* #ifndef APP-PLUS */
  230. z-index: 9;
  231. /* #endif */
  232. }
  233. .pages .nav {
  234. display: flex;
  235. align-items: center;
  236. flex-direction: row;
  237. }
  238. .uni-search-box {
  239. flex: 1;
  240. padding: 0 10px;
  241. }
  242. .uni-search-box ::v-deep .uni-searchbar {
  243. padding: 0;
  244. }
  245. .uni-search-box ::v-deep .uni-searchbar__box {
  246. height: 32px;
  247. flex-direction: row;
  248. }
  249. .cover-search-bar {
  250. position: absolute;
  251. left: 0;
  252. right: 0;
  253. bottom: 0;
  254. top: 0;
  255. /* #ifndef APP-NVUE */
  256. z-index: 999;
  257. /* #endif */
  258. }
  259. .pages .uni-list ::v-deep .uni-list-item__container {
  260. flex-direction: row;
  261. width: 100%;
  262. }
  263. .pages .uni-list ::v-deep .uni-list-item {
  264. align-items: flex-start;
  265. }
  266. .pages .uni-list ::v-deep .uni-load-more {
  267. display: flex;
  268. }
  269. .pages .uni-list ::v-deep .uni-list--border:after {
  270. background-color: #f5f5f5;
  271. }
  272. </style>