material.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. <template>
  2. <view class="container" :style="{ '--font-scale': fontScale }">
  3. <!-- 搜索栏 -->
  4. <view class="search-bar">
  5. <view class="search-input-wrap">
  6. <text class="search-icon">🔍</text>
  7. <input
  8. class="search-input"
  9. type="text"
  10. placeholder="搜索素材标题或标签"
  11. v-model="searchKeyword"
  12. confirm-type="search"
  13. @confirm="onSearch"
  14. />
  15. <text class="search-clear" v-if="searchKeyword" @click="clearSearch">✕</text>
  16. </view>
  17. </view>
  18. <!-- 类型标签栏 -->
  19. <scroll-view class="type-tabs" scroll-x>
  20. <view
  21. class="type-tab"
  22. :class="{ active: currentType === item.value }"
  23. v-for="item in typeTabs"
  24. :key="item.value"
  25. @click="selectType(item.value)"
  26. >
  27. <text>{{ item.label }}</text>
  28. </view>
  29. </scroll-view>
  30. <!-- 素材列表 -->
  31. <scroll-view class="material-list" scroll-y @scrolltolower="loadMore">
  32. <view class="material-grid">
  33. <view
  34. class="material-item"
  35. v-for="item in materialList"
  36. :key="item.id"
  37. @click="goDetail(item)"
  38. >
  39. <image class="material-cover" :src="getImageUrl(item.coverUrl) || '/static/default-cover.png'" mode="aspectFill" lazy-load />
  40. <view class="material-info">
  41. <text class="material-title">{{ item.title }}</text>
  42. <text class="material-tags" v-if="item.tags">{{ item.tags }}</text>
  43. <view class="material-actions">
  44. <view class="action-btn share-btn" @click.stop="shareItem(item)">
  45. <text>分享</text>
  46. </view>
  47. <view class="action-btn download-btn" @click.stop="downloadItem(item)">
  48. <text>下载</text>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. <!-- 加载状态 -->
  55. <view class="loading-more" v-if="loading">
  56. <text>加载中...</text>
  57. </view>
  58. <view class="no-more" v-if="noMore && materialList.length > 0">
  59. <text>没有更多了</text>
  60. </view>
  61. <view class="empty-state" v-if="!loading && materialList.length === 0">
  62. <text class="empty-text">暂无素材</text>
  63. </view>
  64. </scroll-view>
  65. </view>
  66. </template>
  67. <script>
  68. import config from '@/config.js'
  69. var BASE_URL = config.API_BASE_URL
  70. function _request(url, method, data) {
  71. var token = uni.getStorageSync('token')
  72. return new Promise(function(resolve, reject) {
  73. uni.request({
  74. url: BASE_URL + url,
  75. method: method || 'POST',
  76. data: data || {},
  77. header: {
  78. 'Content-Type': 'application/json',
  79. 'Authorization': token ? 'Bearer ' + token : ''
  80. },
  81. success: function(res) {
  82. if (res.statusCode === 401 || (res.data && res.data.code === 401)) {
  83. uni.removeStorageSync('token')
  84. uni.removeStorageSync('userId')
  85. uni.showToast({ title: '登录已过期,请重新登录', icon: 'none' })
  86. reject(res.data)
  87. return
  88. }
  89. if (res.data && res.data.code === 200) {
  90. resolve(res.data)
  91. } else {
  92. uni.showToast({ title: (res.data && res.data.message) || '请求失败', icon: 'none' })
  93. reject(res.data)
  94. }
  95. },
  96. fail: function(err) {
  97. uni.showToast({ title: '网络请求失败', icon: 'none' })
  98. reject(err)
  99. }
  100. })
  101. })
  102. }
  103. import fontScaleMixin from '@/mixins/font-scale.js'
  104. export default {
  105. mixins: [fontScaleMixin],
  106. data() {
  107. return {
  108. searchKeyword: '',
  109. currentType: 'all',
  110. materialList: [],
  111. pageNum: 1,
  112. pageSize: 20,
  113. loading: false,
  114. noMore: false,
  115. typeTabs: [
  116. { label: '全部', value: 'all' },
  117. { label: '图片', value: 'image' },
  118. { label: '文章', value: 'article' },
  119. { label: '视频', value: 'video' },
  120. { label: '海报', value: 'poster' }
  121. ]
  122. }
  123. },
  124. onLoad() {
  125. this.loadMaterials()
  126. },
  127. methods: {
  128. loadMaterials() {
  129. if (this.loading || this.noMore) return
  130. this.loading = true
  131. var self = this
  132. var params = {
  133. materialType: this.currentType === 'all' ? null : this.currentType,
  134. keyword: this.searchKeyword || null,
  135. pageNum: this.pageNum,
  136. pageSize: this.pageSize
  137. }
  138. _request('/api/promotion/material/list', 'POST', params).then(function(res) {
  139. if (res.code === 200 && res.data) {
  140. var records = res.data.records || []
  141. if (self.pageNum === 1) {
  142. self.materialList = records
  143. } else {
  144. self.materialList = self.materialList.concat(records)
  145. }
  146. self.noMore = records.length < self.pageSize
  147. }
  148. self.loading = false
  149. }).catch(function() {
  150. self.loading = false
  151. })
  152. },
  153. loadMore() {
  154. if (this.noMore || this.loading) return
  155. this.pageNum = this.pageNum + 1
  156. this.loadMaterials()
  157. },
  158. selectType(type) {
  159. this.currentType = type
  160. this.pageNum = 1
  161. this.noMore = false
  162. this.materialList = []
  163. this.loadMaterials()
  164. },
  165. onSearch() {
  166. this.pageNum = 1
  167. this.noMore = false
  168. this.materialList = []
  169. this.loadMaterials()
  170. },
  171. clearSearch() {
  172. this.searchKeyword = ''
  173. this.onSearch()
  174. },
  175. goDetail(item) {
  176. uni.navigateTo({
  177. url: '/pages/promotion/material-detail?id=' + item.id
  178. })
  179. },
  180. shareItem(item) {
  181. uni.showShareMenu({
  182. withShareTicket: true,
  183. menus: ['shareAppMessage', 'shareTimeline']
  184. })
  185. // 触发系统分享面板
  186. uni.shareAppMessage({
  187. title: item.title || '推荐内容',
  188. imageUrl: item.coverImage || '',
  189. imageUrl() {
  190. return ''
  191. }
  192. })
  193. },
  194. downloadItem(item) {
  195. if (!item.fileUrl) {
  196. uni.showToast({ title: '暂无下载链接', icon: 'none' })
  197. return
  198. }
  199. uni.downloadFile({
  200. url: item.fileUrl,
  201. success: function(res) {
  202. if (res.statusCode === 200) {
  203. uni.saveImageToPhotosAlbum({
  204. filePath: res.tempFilePath,
  205. success: function() {
  206. uni.showToast({ title: '已保存到相册', icon: 'success' })
  207. },
  208. fail: function() {
  209. uni.showToast({ title: '保存失败', icon: 'none' })
  210. }
  211. })
  212. }
  213. },
  214. fail: function() {
  215. uni.showToast({ title: '下载失败', icon: 'none' })
  216. }
  217. })
  218. },getImageUrl: function(path) {
  219. if (!path || path === 'null' || path === 'undefined' || path === '/null') return ''
  220. var s = String(path).trim()
  221. if (!s || s === 'null' || s === '/null') return ''
  222. if (s.indexOf('http://') === 0 || s.indexOf('https://') === 0) return s
  223. return config.API_BASE_URL + (s.charAt(0) === '/' ? s : '/' + s)
  224. }
  225. }
  226. }
  227. </script>
  228. <style scoped lang="scss">
  229. .container {
  230. min-height: 100vh;
  231. background: $facade-fill-2;
  232. }
  233. /* 搜索栏 */
  234. .search-bar {
  235. padding: 20rpx $uni-space-page;
  236. background: $app-surface;
  237. }
  238. .search-input-wrap {
  239. display: flex;
  240. align-items: center;
  241. background: $facade-fill-2;
  242. border-radius: 24rpx;
  243. padding: 16rpx 24rpx;
  244. }
  245. .search-icon {
  246. font-size: calc(28rpx * var(--font-scale, 1));
  247. margin-right: 12rpx;
  248. color: $facade-text-muted;
  249. }
  250. .search-input {
  251. flex: 1;
  252. font-size: calc(28rpx * var(--font-scale, 1));
  253. color: $facade-text-strong;
  254. }
  255. .search-clear {
  256. font-size: calc(24rpx * var(--font-scale, 1));
  257. color: $facade-text-muted;
  258. padding: 8rpx;
  259. }
  260. /* 类型标签 */
  261. .type-tabs {
  262. white-space: nowrap;
  263. background: $app-surface;
  264. padding: 0 20rpx 20rpx;
  265. border-bottom: 1rpx solid $facade-hairline-2;
  266. }
  267. .type-tab {
  268. display: inline-block;
  269. padding: 12rpx 28rpx;
  270. margin-right: 16rpx;
  271. border-radius: 24rpx;
  272. background: $facade-fill-2;
  273. font-size: calc(26rpx * var(--font-scale, 1));
  274. color: $facade-text-body;
  275. }
  276. .type-tab.active {
  277. background: $brand-primary;
  278. color: $app-surface;
  279. }
  280. /* 素材列表 */
  281. .material-list {
  282. padding: 20rpx;
  283. }
  284. .material-grid {
  285. display: flex;
  286. flex-wrap: wrap;
  287. justify-content: space-between;
  288. }
  289. .material-item {
  290. width: 48%;
  291. background: $app-surface;
  292. border-radius: 16rpx;
  293. overflow: hidden;
  294. margin-bottom: 20rpx;
  295. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  296. }
  297. .material-cover {
  298. width: 100%;
  299. height: 240rpx;
  300. background: $facade-fill;
  301. }
  302. .material-info {
  303. padding: 16rpx;
  304. }
  305. .material-title {
  306. font-size: calc(28rpx * var(--font-scale, 1));
  307. color: $facade-text-strong;
  308. font-weight: 500;
  309. display: block;
  310. margin-bottom: 8rpx;
  311. overflow: hidden;
  312. text-overflow: ellipsis;
  313. white-space: nowrap;
  314. }
  315. .material-tags {
  316. font-size: calc(22rpx * var(--font-scale, 1));
  317. color: $facade-text-muted;
  318. display: block;
  319. margin-bottom: 12rpx;
  320. overflow: hidden;
  321. text-overflow: ellipsis;
  322. white-space: nowrap;
  323. }
  324. .material-actions {
  325. display: flex;
  326. justify-content: space-between;
  327. }
  328. .action-btn {
  329. flex: 1;
  330. text-align: center;
  331. padding: 12rpx 0;
  332. border-radius: 8rpx;
  333. font-size: calc(24rpx * var(--font-scale, 1));
  334. margin-right: 12rpx;
  335. }
  336. .action-btn:last-child {
  337. margin-right: 0;
  338. }
  339. .share-btn {
  340. background: $fortune-1-tint;
  341. color: $brand-primary;
  342. }
  343. .download-btn {
  344. background: $mat-blue-tint;
  345. color: $mat-blue-base;
  346. }
  347. /* 加载和空状态 */
  348. .loading-more, .no-more {
  349. text-align: center;
  350. padding: 24rpx;
  351. font-size: calc(24rpx * var(--font-scale, 1));
  352. color: $facade-text-muted;
  353. }
  354. .empty-state {
  355. text-align: center;
  356. padding: 120rpx 0;
  357. }
  358. .empty-text {
  359. font-size: calc(28rpx * var(--font-scale, 1));
  360. color: $facade-text-muted;
  361. }
  362. </style>