material.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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="getImageUrl(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. getImageUrl: function(path) {
  211. return config.API_BASE_URL + path
  212. }
  213. }
  214. }
  215. </script>
  216. <style scoped>
  217. .container {
  218. min-height: 100vh;
  219. background: #F5F5F5;
  220. }
  221. /* 搜索栏 */
  222. .search-bar {
  223. padding: 20rpx 30rpx;
  224. background: #fff;
  225. }
  226. .search-input-wrap {
  227. display: flex;
  228. align-items: center;
  229. background: #F5F5F5;
  230. border-radius: 40rpx;
  231. padding: 16rpx 24rpx;
  232. }
  233. .search-icon {
  234. font-size: 28rpx;
  235. margin-right: 12rpx;
  236. color: #999;
  237. }
  238. .search-input {
  239. flex: 1;
  240. font-size: 28rpx;
  241. color: #333;
  242. }
  243. .search-clear {
  244. font-size: 24rpx;
  245. color: #999;
  246. padding: 8rpx;
  247. }
  248. /* 类型标签 */
  249. .type-tabs {
  250. white-space: nowrap;
  251. background: #fff;
  252. padding: 0 20rpx 20rpx;
  253. border-bottom: 1rpx solid #eee;
  254. }
  255. .type-tab {
  256. display: inline-block;
  257. padding: 12rpx 28rpx;
  258. margin-right: 16rpx;
  259. border-radius: 32rpx;
  260. background: #F5F5F5;
  261. font-size: 26rpx;
  262. color: #666;
  263. }
  264. .type-tab.active {
  265. background: #F97316;
  266. color: #fff;
  267. }
  268. /* 素材列表 */
  269. .material-list {
  270. padding: 20rpx;
  271. }
  272. .material-grid {
  273. display: flex;
  274. flex-wrap: wrap;
  275. justify-content: space-between;
  276. }
  277. .material-item {
  278. width: 48%;
  279. background: #fff;
  280. border-radius: 16rpx;
  281. overflow: hidden;
  282. margin-bottom: 20rpx;
  283. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  284. }
  285. .material-cover {
  286. width: 100%;
  287. height: 240rpx;
  288. background: #f0f0f0;
  289. }
  290. .material-info {
  291. padding: 16rpx;
  292. }
  293. .material-title {
  294. font-size: 28rpx;
  295. color: #333;
  296. font-weight: 500;
  297. display: block;
  298. margin-bottom: 8rpx;
  299. overflow: hidden;
  300. text-overflow: ellipsis;
  301. white-space: nowrap;
  302. }
  303. .material-tags {
  304. font-size: 22rpx;
  305. color: #999;
  306. display: block;
  307. margin-bottom: 12rpx;
  308. overflow: hidden;
  309. text-overflow: ellipsis;
  310. white-space: nowrap;
  311. }
  312. .material-actions {
  313. display: flex;
  314. justify-content: space-between;
  315. }
  316. .action-btn {
  317. flex: 1;
  318. text-align: center;
  319. padding: 12rpx 0;
  320. border-radius: 8rpx;
  321. font-size: 24rpx;
  322. margin-right: 12rpx;
  323. }
  324. .action-btn:last-child {
  325. margin-right: 0;
  326. }
  327. .share-btn {
  328. background: #FFF3E0;
  329. color: #F97316;
  330. }
  331. .download-btn {
  332. background: #E3F2FD;
  333. color: #2196F3;
  334. }
  335. /* 加载和空状态 */
  336. .loading-more, .no-more {
  337. text-align: center;
  338. padding: 24rpx;
  339. font-size: 24rpx;
  340. color: #999;
  341. }
  342. .empty-state {
  343. text-align: center;
  344. padding: 120rpx 0;
  345. }
  346. .empty-text {
  347. font-size: 28rpx;
  348. color: #999;
  349. }
  350. </style>