material.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. menus: ['shareAppMessage', 'shareTimeline']
  182. })
  183. // 触发系统分享面板
  184. uni.shareAppMessage({
  185. title: item.title || '推荐内容',
  186. imageUrl: item.coverImage || '',
  187. imageUrl() {
  188. return ''
  189. }
  190. })
  191. },
  192. downloadItem(item) {
  193. if (!item.fileUrl) {
  194. uni.showToast({ title: '暂无下载链接', icon: 'none' })
  195. return
  196. }
  197. uni.downloadFile({
  198. url: item.fileUrl,
  199. success: function(res) {
  200. if (res.statusCode === 200) {
  201. uni.saveImageToPhotosAlbum({
  202. filePath: res.tempFilePath,
  203. success: function() {
  204. uni.showToast({ title: '已保存到相册', icon: 'success' })
  205. },
  206. fail: function() {
  207. uni.showToast({ title: '保存失败', icon: 'none' })
  208. }
  209. })
  210. }
  211. },
  212. fail: function() {
  213. uni.showToast({ title: '下载失败', icon: 'none' })
  214. }
  215. })
  216. },getImageUrl: function(path) {
  217. if (!path || path === 'null' || path === 'undefined' || path === '/null') return ''
  218. var s = String(path).trim()
  219. if (!s || s === 'null' || s === '/null') return ''
  220. if (s.indexOf('http://') === 0 || s.indexOf('https://') === 0) return s
  221. return config.API_BASE_URL + (s.charAt(0) === '/' ? s : '/' + s)
  222. }
  223. }
  224. }
  225. </script>
  226. <style scoped>
  227. .container {
  228. min-height: 100vh;
  229. background: #F5F5F5;
  230. }
  231. /* 搜索栏 */
  232. .search-bar {
  233. padding: 20rpx 30rpx;
  234. background: #fff;
  235. }
  236. .search-input-wrap {
  237. display: flex;
  238. align-items: center;
  239. background: #F5F5F5;
  240. border-radius: 40rpx;
  241. padding: 16rpx 24rpx;
  242. }
  243. .search-icon {
  244. font-size: 28rpx;
  245. margin-right: 12rpx;
  246. color: #999;
  247. }
  248. .search-input {
  249. flex: 1;
  250. font-size: 28rpx;
  251. color: #333;
  252. }
  253. .search-clear {
  254. font-size: 24rpx;
  255. color: #999;
  256. padding: 8rpx;
  257. }
  258. /* 类型标签 */
  259. .type-tabs {
  260. white-space: nowrap;
  261. background: #fff;
  262. padding: 0 20rpx 20rpx;
  263. border-bottom: 1rpx solid #eee;
  264. }
  265. .type-tab {
  266. display: inline-block;
  267. padding: 12rpx 28rpx;
  268. margin-right: 16rpx;
  269. border-radius: 32rpx;
  270. background: #F5F5F5;
  271. font-size: 26rpx;
  272. color: #666;
  273. }
  274. .type-tab.active {
  275. background: #F97316;
  276. color: #fff;
  277. }
  278. /* 素材列表 */
  279. .material-list {
  280. padding: 20rpx;
  281. }
  282. .material-grid {
  283. display: flex;
  284. flex-wrap: wrap;
  285. justify-content: space-between;
  286. }
  287. .material-item {
  288. width: 48%;
  289. background: #fff;
  290. border-radius: 16rpx;
  291. overflow: hidden;
  292. margin-bottom: 20rpx;
  293. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  294. }
  295. .material-cover {
  296. width: 100%;
  297. height: 240rpx;
  298. background: #f0f0f0;
  299. }
  300. .material-info {
  301. padding: 16rpx;
  302. }
  303. .material-title {
  304. font-size: 28rpx;
  305. color: #333;
  306. font-weight: 500;
  307. display: block;
  308. margin-bottom: 8rpx;
  309. overflow: hidden;
  310. text-overflow: ellipsis;
  311. white-space: nowrap;
  312. }
  313. .material-tags {
  314. font-size: 22rpx;
  315. color: #999;
  316. display: block;
  317. margin-bottom: 12rpx;
  318. overflow: hidden;
  319. text-overflow: ellipsis;
  320. white-space: nowrap;
  321. }
  322. .material-actions {
  323. display: flex;
  324. justify-content: space-between;
  325. }
  326. .action-btn {
  327. flex: 1;
  328. text-align: center;
  329. padding: 12rpx 0;
  330. border-radius: 8rpx;
  331. font-size: 24rpx;
  332. margin-right: 12rpx;
  333. }
  334. .action-btn:last-child {
  335. margin-right: 0;
  336. }
  337. .share-btn {
  338. background: #FFF3E0;
  339. color: #F97316;
  340. }
  341. .download-btn {
  342. background: #E3F2FD;
  343. color: #2196F3;
  344. }
  345. /* 加载和空状态 */
  346. .loading-more, .no-more {
  347. text-align: center;
  348. padding: 24rpx;
  349. font-size: 24rpx;
  350. color: #999;
  351. }
  352. .empty-state {
  353. text-align: center;
  354. padding: 120rpx 0;
  355. }
  356. .empty-text {
  357. font-size: 28rpx;
  358. color: #999;
  359. }
  360. </style>