material.vue 8.5 KB

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