1
0

gallery.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <template>
  2. <view class="app-page" :class="[uniHaloPluginPageClass]">
  3. <PluginUnavailable v-if="!uniHaloPluginAvailable" :pluginId="uniHaloPluginId"
  4. :error-text="uniHaloPluginAvailableError" />
  5. <template v-else>
  6. <!-- 顶部切换 -->
  7. <view class="e-fixed" v-if="category.list.length > 0">
  8. <tm-tabs color="light-blue" :shadow="0" v-model="category.activeIndex" range-key="displayName"
  9. :list="category.list" align="left" @change="fnOnCategoryChange($event, false)"></tm-tabs>
  10. </view>
  11. <!-- 占位区域 -->
  12. <view v-if="category.list.length > 0" style="width: 100vw;height: 90rpx;"></view>
  13. <!-- 加载区域 -->
  14. <view v-if="loading == 'loading'" class="loading-wrap">
  15. <tm-skeleton model="card"></tm-skeleton>
  16. <tm-skeleton model="card"></tm-skeleton>
  17. <tm-skeleton model="card"></tm-skeleton>
  18. <tm-skeleton model="card"></tm-skeleton>
  19. </view>
  20. <view v-else-if="loading == 'error'" class="flex flex-col flex-center" style="width:100%;height:60vh;">
  21. <tm-empty icon="icon-wind-cry" label="阿偶,获取数据失败了~">
  22. <tm-button theme="light-blue" size="m" :shadow="0" @click="fnGetData(true)">刷新试试</tm-button>
  23. </tm-empty>
  24. </view>
  25. <!-- 内容区域 -->
  26. <view v-else class="content">
  27. <k-touch-listen class="touch-listen-content" @touchLeft="touchLeft" @touchRight="touchRight">
  28. <view v-if="dataList.length === 0" class="content-empty">
  29. <!-- 空布局 -->
  30. <tm-empty icon="icon-shiliangzhinengduixiang-" label="博主还没有分享图片~"></tm-empty>
  31. </view>
  32. <block v-else>
  33. <block v-if="galleryConfig.useWaterfall">
  34. <!--瀑布流-->
  35. <tm-flowLayout-custom ref="wafll" style="width: 100%;"
  36. @click="fnOnFlowClick"></tm-flowLayout-custom>
  37. </block>
  38. <!-- 列表 -->
  39. <block v-else>
  40. <tm-translate v-for="(item, index) in dataList" :key="index"
  41. style="box-sizing: border-box;padding: 6rpx;width: 50%;height: 250rpx;"
  42. animation-name="fadeUp" :wait="calcAniWait(index)">
  43. <view style="border-radius: 12rpx;overflow: hidden;width: 100%;height: 250rpx;">
  44. <image style="width: 100%;height: 100%;" mode="aspectFill" :src="item.spec.url"
  45. @click="fnPreview(item)" />
  46. </view>
  47. </tm-translate>
  48. </block>
  49. <view class="load-text">{{ loadMoreText }}</view>
  50. </block>
  51. </k-touch-listen>
  52. </view>
  53. <view v-if="!calcAuditModeEnabled" class="flot-buttons">
  54. <tm-button v-if="loading == 'error'" @click="fnGetCategory" size="m" :fab="true" theme="light-blue"
  55. icon="icon-sync-alt"></tm-button>
  56. <tm-button @click="fnToTopPage" size="m" :fab="true" theme="light-blue"
  57. icon="icon-angle-up"></tm-button>
  58. </view>
  59. </template>
  60. </view>
  61. </template>
  62. <script>
  63. import tmSkeleton from '@/tm-vuetify/components/tm-skeleton/tm-skeleton.vue';
  64. import tmTranslate from '@/tm-vuetify/components/tm-translate/tm-translate.vue';
  65. import tmFlotbutton from '@/tm-vuetify/components/tm-flotbutton/tm-flotbutton.vue';
  66. import tmTags from '@/tm-vuetify/components/tm-tags/tm-tags.vue';
  67. import tmEmpty from '@/tm-vuetify/components/tm-empty/tm-empty.vue';
  68. import tmIcons from '@/tm-vuetify/components/tm-icons/tm-icons.vue';
  69. import tmImages from '@/tm-vuetify/components/tm-images/tm-images.vue';
  70. import tmFlowLayoutCustom from '@/tm-vuetify/components/tm-flowLayout-custom/tm-flowLayout-custom.vue';
  71. import tmTabs from '@/tm-vuetify/components/tm-tabs/tm-tabs.vue';
  72. import tmButton from '@/tm-vuetify/components/tm-button/tm-button.vue';
  73. import pluginAvailable from "@/common/mixins/pluginAvailable.js"
  74. import PluginUnavailable from '@/components/plugin-unavailable/plugin-unavailable.vue'
  75. export default {
  76. options: {
  77. multipleSlots: true
  78. },
  79. mixins: [pluginAvailable],
  80. components: {
  81. tmSkeleton,
  82. tmTranslate,
  83. tmFlotbutton,
  84. tmTags,
  85. tmEmpty,
  86. tmIcons,
  87. tmImages,
  88. tmFlowLayoutCustom,
  89. tmTabs,
  90. tmButton,
  91. PluginUnavailable
  92. },
  93. data() {
  94. return {
  95. isBlackTheme: false,
  96. loading: 'loading',
  97. category: {
  98. activeIndex: 0,
  99. activeValue: '',
  100. list: []
  101. },
  102. queryParams: {
  103. size: 10,
  104. page: 1,
  105. group: ""
  106. },
  107. isLoadMore: false,
  108. loadMoreText: '',
  109. hasNext: false,
  110. dataList: [],
  111. lock: false
  112. };
  113. },
  114. computed: {
  115. galleryConfig() {
  116. return this.$tm.vx.getters().getConfigs.pageConfig.galleryConfig;
  117. },
  118. haloConfigs() {
  119. return this.$tm.vx.getters().getConfigs;
  120. },
  121. mockJson() {
  122. return this.$tm.vx.getters().getMockJson;
  123. },
  124. calcAuditModeEnabled() {
  125. return this.haloConfigs.auditConfig.auditModeEnabled
  126. },
  127. },
  128. watch: {
  129. galleryConfig: {
  130. async handler(newValue, oldValue) {
  131. if (!newValue) return;
  132. this.fnSetPageTitle(newValue.pageTitle);
  133. this.fnGetCategory();
  134. },
  135. deep: true,
  136. immediate: true
  137. }
  138. },
  139. async onLoad() {
  140. // 检查插件
  141. this.setPluginId(this.NeedPluginIds.PluginPhotos)
  142. this.setPluginError("阿偶,检测到当前插件没有安装或者启用,无法使用图库功能哦,请联系管理员")
  143. await this.checkPluginAvailable()
  144. },
  145. onPullDownRefresh() {
  146. if (!this.uniHaloPluginAvailable) {
  147. uni.hideLoading();
  148. uni.stopPullDownRefresh();
  149. return
  150. }
  151. this.dataList = []
  152. this.isLoadMore = false;
  153. this.queryParams.page = 1;
  154. this.fnGetData(true);
  155. },
  156. onReachBottom(e) {
  157. if (!this.uniHaloPluginAvailable) return;
  158. if (this.calcAuditModeEnabled) {
  159. uni.showToast({
  160. icon: 'none',
  161. title: '没有更多数据了'
  162. });
  163. return;
  164. }
  165. if (this.hasNext) {
  166. this.queryParams.page += 1;
  167. this.isLoadMore = true;
  168. this.fnGetData(false);
  169. } else {
  170. uni.showToast({
  171. icon: 'none',
  172. title: '没有更多数据了'
  173. });
  174. }
  175. },
  176. methods: {
  177. fnGetDataByCategory(index) {
  178. this.fnResetSetAniWaitIndex();
  179. this.queryParams.group = this.category.list[index].name;
  180. this.queryParams.page = 1;
  181. this.fnToTopPage();
  182. this.dataList = [];
  183. this.fnGetData(true);
  184. },
  185. fnOnCategoryChange(index) {
  186. if (this.lock) {
  187. // uni.showToast({
  188. // title: "上一个请求进行中...",
  189. // icon: "none"
  190. // })
  191. return;
  192. }
  193. this.fnGetDataByCategory(index)
  194. },
  195. fnGetCategory() {
  196. if (this.calcAuditModeEnabled) {
  197. this.fnGetData(true);
  198. return
  199. }
  200. this.$httpApi.v2.getPhotoGroupList({
  201. page: 1,
  202. size: 0
  203. }).then(res => {
  204. this.category.list = res.map(item => {
  205. return {
  206. name: item.metadata.name,
  207. displayName: item.spec.displayName,
  208. priority: item.spec.priority
  209. }
  210. }).sort((a, b) => a.priority - b.priority)
  211. this.category.list.unshift({
  212. name: undefined,
  213. displayName: '全部',
  214. priority: 0
  215. });
  216. if (this.category.list.length !== 0) {
  217. this.queryParams.group = this.category.list[0].name;
  218. this.fnGetData(true);
  219. }
  220. }).catch(e => {
  221. this.loading = 'error'
  222. this.category.list = []
  223. this.category.activeIndex = 0
  224. this.category.activeValue = ""
  225. });
  226. },
  227. fnGetData(isClearWaterfall = false) {
  228. if (this.calcAuditModeEnabled) {
  229. this.dataList = this.mockJson.gallery.list.map(item => {
  230. return {
  231. metadata: {
  232. name: Date.now() * Math.random(),
  233. },
  234. spec: {
  235. url: this.$utils.checkImageUrl(item)
  236. }
  237. }
  238. })
  239. this.loading = 'success';
  240. if (this.galleryConfig.useWaterfall) {
  241. this.$nextTick(() => {
  242. if (isClearWaterfall) {
  243. this.$refs.wafll.clear()
  244. }
  245. setTimeout(() => {
  246. this.$refs.wafll.pushData(this.dataList)
  247. }, 50)
  248. })
  249. }
  250. this.loadMoreText = '呜呜,没有更多数据啦~';
  251. uni.hideLoading();
  252. uni.stopPullDownRefresh();
  253. this.lock = false;
  254. return;
  255. }
  256. // 设置状态为加载中
  257. if (!this.isLoadMore) {
  258. this.loading = 'loading';
  259. }
  260. this.loadMoreText = '';
  261. this.$httpApi.v2
  262. .getPhotoListByGroupName(this.queryParams)
  263. .then(res => {
  264. this.hasNext = res.hasNext;
  265. this.loading = 'success';
  266. if (res.items.length !== 0) {
  267. const _list = res.items.map((item, index) => {
  268. item.spec.url = this.$utils.checkImageUrl(item.spec.url || item.spec.cover);
  269. return item;
  270. });
  271. if (this.isLoadMore) {
  272. this.dataList = this.dataList.concat(_list);
  273. } else {
  274. this.dataList = _list;
  275. }
  276. if (this.galleryConfig.useWaterfall) {
  277. this.$nextTick(() => {
  278. if (isClearWaterfall) {
  279. this.$refs.wafll.clear()
  280. }
  281. this.$refs.wafll.pushData(_list)
  282. })
  283. }
  284. }
  285. this.loadMoreText = res.hasNext ? '上拉加载更多' : '呜呜,没有更多数据啦~';
  286. })
  287. .catch(err => {
  288. console.error(err);
  289. this.loading = 'error';
  290. this.loadMoreText = '加载失败,请下拉刷新!';
  291. })
  292. .finally(() => {
  293. setTimeout(() => {
  294. uni.hideLoading();
  295. uni.stopPullDownRefresh();
  296. this.lock = false;
  297. }, 500);
  298. });
  299. },
  300. fnOnFlowClick({
  301. item
  302. }) {
  303. this.fnPreview(item)
  304. },
  305. // 预览
  306. fnPreview(data) {
  307. uni.previewImage({
  308. current: this.dataList.findIndex(x => x.metadata.name === data.metadata.name),
  309. urls: this.dataList.map(x => x.spec.url),
  310. indicator: 'number',
  311. loop: true
  312. });
  313. },
  314. touchLeft() {
  315. if (this.loading != "success") return;
  316. this.category.activeIndex += 1
  317. if (this.category.activeIndex >= this.category.list.length) {
  318. this.category.activeIndex = 0
  319. }
  320. this.lock = true
  321. this.fnGetDataByCategory(this.category.activeIndex)
  322. },
  323. touchRight() {
  324. if (this.loading != "success") return;
  325. this.category.activeIndex -= 1
  326. if (this.category.activeIndex < 0) {
  327. this.category.activeIndex = 0
  328. }
  329. this.lock = true
  330. this.fnGetDataByCategory(this.category.activeIndex)
  331. }
  332. }
  333. };
  334. </script>
  335. <style lang="scss" scoped>
  336. .app-page {
  337. width: 100vw;
  338. min-height: 100vh;
  339. display: flex;
  340. flex-direction: column;
  341. padding-bottom: 24rpx;
  342. background-color: #fafafa;
  343. }
  344. .content {
  345. width: 100%;
  346. display: flex;
  347. flex-wrap: wrap;
  348. box-sizing: border-box;
  349. .content-empty {
  350. width: 100%;
  351. height: 70vh;
  352. display: flex;
  353. align-items: center;
  354. justify-content: center;
  355. }
  356. }
  357. .touch-listen-content {
  358. width: 100%;
  359. display: flex;
  360. flex-wrap: wrap;
  361. box-sizing: border-box;
  362. padding: 24rpx 24rpx 0;
  363. gap: 12rpx 0;
  364. }
  365. .loading-wrap {
  366. box-sizing: border-box;
  367. padding: 24rpx;
  368. }
  369. .load-text {
  370. width: 100%;
  371. text-align: center;
  372. }
  373. .flot-buttons {
  374. position: fixed;
  375. bottom: 100rpx;
  376. right: 32rpx;
  377. flex-direction: column;
  378. display: flex;
  379. gap: 6rpx;
  380. z-index: 999;
  381. }
  382. </style>