material.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <template>
  2. <view class="container">
  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="item.coverUrl || '/static/default-cover.png'" mode="aspectFill" />
  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. 'X-User-Id': uni.getStorageSync('userId') || '',
  80. 'Authorization': token ? 'Bearer ' + token : ''
  81. },
  82. success: function(res) {
  83. if (res.statusCode === 401 || (res.data && res.data.code === 401)) {
  84. uni.removeStorageSync('token')
  85. uni.removeStorageSync('userId')
  86. uni.showToast({ title: '登录已过期,请重新登录', icon: 'none' })
  87. reject(res.data)
  88. return
  89. }
  90. if (res.data && res.data.code === 200) {
  91. resolve(res.data)
  92. } else {
  93. uni.showToast({ title: (res.data && res.data.message) || '请求失败', icon: 'none' })
  94. reject(res.data)
  95. }
  96. },
  97. fail: function(err) {
  98. uni.showToast({ title: '网络请求失败', icon: 'none' })
  99. reject(err)
  100. }
  101. })
  102. })
  103. }
  104. export default {
  105. data() {
  106. return {
  107. searchKeyword: '',
  108. currentType: 'all',
  109. materialList: [],
  110. pageNum: 1,
  111. pageSize: 20,
  112. loading: false,
  113. noMore: false,
  114. typeTabs: [
  115. { label: '全部', value: 'all' },
  116. { label: '图片', value: 'image' },
  117. { label: '文章', value: 'article' },
  118. { label: '视频', value: 'video' },
  119. { label: '海报', value: 'poster' }
  120. ]
  121. }
  122. },
  123. onLoad() {
  124. this.loadMaterials()
  125. },
  126. methods: {
  127. loadMaterials() {
  128. if (this.loading || this.noMore) return
  129. this.loading = true
  130. var self = this
  131. var params = {
  132. materialType: this.currentType === 'all' ? null : this.currentType,
  133. keyword: this.searchKeyword || null,
  134. pageNum: this.pageNum,
  135. pageSize: this.pageSize
  136. }
  137. _request('/api/promotion/material/list', 'POST', params).then(function(res) {
  138. if (res.code === 200 && res.data) {
  139. var records = res.data.records || []
  140. if (self.pageNum === 1) {
  141. self.materialList = records
  142. } else {
  143. self.materialList = self.materialList.concat(records)
  144. }
  145. self.noMore = records.length < self.pageSize
  146. }
  147. self.loading = false
  148. }).catch(function() {
  149. self.loading = false
  150. })
  151. },
  152. loadMore() {
  153. if (this.noMore || this.loading) return
  154. this.pageNum = this.pageNum + 1
  155. this.loadMaterials()
  156. },
  157. selectType(type) {
  158. this.currentType = type
  159. this.pageNum = 1
  160. this.noMore = false
  161. this.materialList = []
  162. this.loadMaterials()
  163. },
  164. onSearch() {
  165. this.pageNum = 1
  166. this.noMore = false
  167. this.materialList = []
  168. this.loadMaterials()
  169. },
  170. clearSearch() {
  171. this.searchKeyword = ''
  172. this.onSearch()
  173. },
  174. goDetail(item) {
  175. uni.navigateTo({
  176. url: '/pages/promotion/material-detail?id=' + item.id
  177. })
  178. },
  179. shareItem(item) {
  180. uni.showShareMenu({
  181. withShareTicket: true
  182. })
  183. uni.showToast({ title: '分享功能开发中', icon: 'none' })
  184. },
  185. downloadItem(item) {
  186. if (!item.fileUrl) {
  187. uni.showToast({ title: '暂无下载链接', icon: 'none' })
  188. return
  189. }
  190. uni.downloadFile({
  191. url: item.fileUrl,
  192. success: function(res) {
  193. if (res.statusCode === 200) {
  194. uni.saveImageToPhotosAlbum({
  195. filePath: res.tempFilePath,
  196. success: function() {
  197. uni.showToast({ title: '已保存到相册', icon: 'success' })
  198. },
  199. fail: function() {
  200. uni.showToast({ title: '保存失败', icon: 'none' })
  201. }
  202. })
  203. }
  204. },
  205. fail: function() {
  206. uni.showToast({ title: '下载失败', icon: 'none' })
  207. }
  208. })
  209. }
  210. }
  211. }
  212. </script>
  213. <style scoped>
  214. .container {
  215. min-height: 100vh;
  216. background: #F5F5F5;
  217. }
  218. /* 搜索栏 */
  219. .search-bar {
  220. padding: 20rpx 30rpx;
  221. background: #fff;
  222. }
  223. .search-input-wrap {
  224. display: flex;
  225. align-items: center;
  226. background: #F5F5F5;
  227. border-radius: 40rpx;
  228. padding: 16rpx 24rpx;
  229. }
  230. .search-icon {
  231. font-size: 28rpx;
  232. margin-right: 12rpx;
  233. color: #999;
  234. }
  235. .search-input {
  236. flex: 1;
  237. font-size: 28rpx;
  238. color: #333;
  239. }
  240. .search-clear {
  241. font-size: 24rpx;
  242. color: #999;
  243. padding: 8rpx;
  244. }
  245. /* 类型标签 */
  246. .type-tabs {
  247. white-space: nowrap;
  248. background: #fff;
  249. padding: 0 20rpx 20rpx;
  250. border-bottom: 1rpx solid #eee;
  251. }
  252. .type-tab {
  253. display: inline-block;
  254. padding: 12rpx 28rpx;
  255. margin-right: 16rpx;
  256. border-radius: 32rpx;
  257. background: #F5F5F5;
  258. font-size: 26rpx;
  259. color: #666;
  260. }
  261. .type-tab.active {
  262. background: #F97316;
  263. color: #fff;
  264. }
  265. /* 素材列表 */
  266. .material-list {
  267. padding: 20rpx;
  268. }
  269. .material-grid {
  270. display: flex;
  271. flex-wrap: wrap;
  272. justify-content: space-between;
  273. }
  274. .material-item {
  275. width: 48%;
  276. background: #fff;
  277. border-radius: 16rpx;
  278. overflow: hidden;
  279. margin-bottom: 20rpx;
  280. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  281. }
  282. .material-cover {
  283. width: 100%;
  284. height: 240rpx;
  285. background: #f0f0f0;
  286. }
  287. .material-info {
  288. padding: 16rpx;
  289. }
  290. .material-title {
  291. font-size: 28rpx;
  292. color: #333;
  293. font-weight: 500;
  294. display: block;
  295. margin-bottom: 8rpx;
  296. overflow: hidden;
  297. text-overflow: ellipsis;
  298. white-space: nowrap;
  299. }
  300. .material-tags {
  301. font-size: 22rpx;
  302. color: #999;
  303. display: block;
  304. margin-bottom: 12rpx;
  305. overflow: hidden;
  306. text-overflow: ellipsis;
  307. white-space: nowrap;
  308. }
  309. .material-actions {
  310. display: flex;
  311. justify-content: space-between;
  312. }
  313. .action-btn {
  314. flex: 1;
  315. text-align: center;
  316. padding: 12rpx 0;
  317. border-radius: 8rpx;
  318. font-size: 24rpx;
  319. margin-right: 12rpx;
  320. }
  321. .action-btn:last-child {
  322. margin-right: 0;
  323. }
  324. .share-btn {
  325. background: #FFF3E0;
  326. color: #F97316;
  327. }
  328. .download-btn {
  329. background: #E3F2FD;
  330. color: #2196F3;
  331. }
  332. /* 加载和空状态 */
  333. .loading-more, .no-more {
  334. text-align: center;
  335. padding: 24rpx;
  336. font-size: 24rpx;
  337. color: #999;
  338. }
  339. .empty-state {
  340. text-align: center;
  341. padding: 120rpx 0;
  342. }
  343. .empty-text {
  344. font-size: 28rpx;
  345. color: #999;
  346. }
  347. </style>