| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <template>
- <view class="container">
- <!-- 搜索栏 -->
- <view class="search-bar">
- <view class="search-input-wrap">
- <text class="search-icon">🔍</text>
- <input
- class="search-input"
- type="text"
- placeholder="搜索素材标题或标签"
- v-model="searchKeyword"
- confirm-type="search"
- @confirm="onSearch"
- />
- <text class="search-clear" v-if="searchKeyword" @click="clearSearch">✕</text>
- </view>
- </view>
- <!-- 类型标签栏 -->
- <scroll-view class="type-tabs" scroll-x>
- <view
- class="type-tab"
- :class="{ active: currentType === item.value }"
- v-for="item in typeTabs"
- :key="item.value"
- @click="selectType(item.value)"
- >
- <text>{{ item.label }}</text>
- </view>
- </scroll-view>
- <!-- 素材列表 -->
- <scroll-view class="material-list" scroll-y @scrolltolower="loadMore">
- <view class="material-grid">
- <view
- class="material-item"
- v-for="item in materialList"
- :key="item.id"
- @click="goDetail(item)"
- >
- <image class="material-cover" :src="getImageUrl(item.coverUrl) || '/static/default-cover.png'" mode="aspectFill" />
- <view class="material-info">
- <text class="material-title">{{ item.title }}</text>
- <text class="material-tags" v-if="item.tags">{{ item.tags }}</text>
- <view class="material-actions">
- <view class="action-btn share-btn" @click.stop="shareItem(item)">
- <text>分享</text>
- </view>
- <view class="action-btn download-btn" @click.stop="downloadItem(item)">
- <text>下载</text>
- </view>
- </view>
- </view>
- </view>
- </view>
- <!-- 加载状态 -->
- <view class="loading-more" v-if="loading">
- <text>加载中...</text>
- </view>
- <view class="no-more" v-if="noMore && materialList.length > 0">
- <text>没有更多了</text>
- </view>
- <view class="empty-state" v-if="!loading && materialList.length === 0">
- <text class="empty-text">暂无素材</text>
- </view>
- </scroll-view>
- </view>
- </template>
- <script>
- import config from '@/config.js'
- var BASE_URL = config.API_BASE_URL
- function _request(url, method, data) {
- var token = uni.getStorageSync('token')
- return new Promise(function(resolve, reject) {
- uni.request({
- url: BASE_URL + url,
- method: method || 'POST',
- data: data || {},
- header: {
- 'Content-Type': 'application/json',
- 'Authorization': token ? 'Bearer ' + token : ''
- },
- success: function(res) {
- if (res.statusCode === 401 || (res.data && res.data.code === 401)) {
- uni.removeStorageSync('token')
- uni.removeStorageSync('userId')
- uni.showToast({ title: '登录已过期,请重新登录', icon: 'none' })
- reject(res.data)
- return
- }
- if (res.data && res.data.code === 200) {
- resolve(res.data)
- } else {
- uni.showToast({ title: (res.data && res.data.message) || '请求失败', icon: 'none' })
- reject(res.data)
- }
- },
- fail: function(err) {
- uni.showToast({ title: '网络请求失败', icon: 'none' })
- reject(err)
- }
- })
- })
- }
- export default {
- data() {
- return {
- searchKeyword: '',
- currentType: 'all',
- materialList: [],
- pageNum: 1,
- pageSize: 20,
- loading: false,
- noMore: false,
- typeTabs: [
- { label: '全部', value: 'all' },
- { label: '图片', value: 'image' },
- { label: '文章', value: 'article' },
- { label: '视频', value: 'video' },
- { label: '海报', value: 'poster' }
- ]
- }
- },
- onLoad() {
- this.loadMaterials()
- },
- methods: {
- loadMaterials() {
- if (this.loading || this.noMore) return
- this.loading = true
- var self = this
- var params = {
- materialType: this.currentType === 'all' ? null : this.currentType,
- keyword: this.searchKeyword || null,
- pageNum: this.pageNum,
- pageSize: this.pageSize
- }
- _request('/api/promotion/material/list', 'POST', params).then(function(res) {
- if (res.code === 200 && res.data) {
- var records = res.data.records || []
- if (self.pageNum === 1) {
- self.materialList = records
- } else {
- self.materialList = self.materialList.concat(records)
- }
- self.noMore = records.length < self.pageSize
- }
- self.loading = false
- }).catch(function() {
- self.loading = false
- })
- },
- loadMore() {
- if (this.noMore || this.loading) return
- this.pageNum = this.pageNum + 1
- this.loadMaterials()
- },
- selectType(type) {
- this.currentType = type
- this.pageNum = 1
- this.noMore = false
- this.materialList = []
- this.loadMaterials()
- },
- onSearch() {
- this.pageNum = 1
- this.noMore = false
- this.materialList = []
- this.loadMaterials()
- },
- clearSearch() {
- this.searchKeyword = ''
- this.onSearch()
- },
- goDetail(item) {
- uni.navigateTo({
- url: '/pages/promotion/material-detail?id=' + item.id
- })
- },
- shareItem(item) {
- uni.showShareMenu({
- withShareTicket: true
- })
- uni.showToast({ title: '分享功能开发中', icon: 'none' })
- },
- downloadItem(item) {
- if (!item.fileUrl) {
- uni.showToast({ title: '暂无下载链接', icon: 'none' })
- return
- }
- uni.downloadFile({
- url: item.fileUrl,
- success: function(res) {
- if (res.statusCode === 200) {
- uni.saveImageToPhotosAlbum({
- filePath: res.tempFilePath,
- success: function() {
- uni.showToast({ title: '已保存到相册', icon: 'success' })
- },
- fail: function() {
- uni.showToast({ title: '保存失败', icon: 'none' })
- }
- })
- }
- },
- fail: function() {
- uni.showToast({ title: '下载失败', icon: 'none' })
- }
- })
- },
- getImageUrl: function(path) {
- return config.API_BASE_URL + path
- }
- }
- }
- </script>
- <style scoped>
- .container {
- min-height: 100vh;
- background: #F5F5F5;
- }
- /* 搜索栏 */
- .search-bar {
- padding: 20rpx 30rpx;
- background: #fff;
- }
- .search-input-wrap {
- display: flex;
- align-items: center;
- background: #F5F5F5;
- border-radius: 40rpx;
- padding: 16rpx 24rpx;
- }
- .search-icon {
- font-size: 28rpx;
- margin-right: 12rpx;
- color: #999;
- }
- .search-input {
- flex: 1;
- font-size: 28rpx;
- color: #333;
- }
- .search-clear {
- font-size: 24rpx;
- color: #999;
- padding: 8rpx;
- }
- /* 类型标签 */
- .type-tabs {
- white-space: nowrap;
- background: #fff;
- padding: 0 20rpx 20rpx;
- border-bottom: 1rpx solid #eee;
- }
- .type-tab {
- display: inline-block;
- padding: 12rpx 28rpx;
- margin-right: 16rpx;
- border-radius: 32rpx;
- background: #F5F5F5;
- font-size: 26rpx;
- color: #666;
- }
- .type-tab.active {
- background: #F97316;
- color: #fff;
- }
- /* 素材列表 */
- .material-list {
- padding: 20rpx;
- }
- .material-grid {
- display: flex;
- flex-wrap: wrap;
- justify-content: space-between;
- }
- .material-item {
- width: 48%;
- background: #fff;
- border-radius: 16rpx;
- overflow: hidden;
- margin-bottom: 20rpx;
- box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
- }
- .material-cover {
- width: 100%;
- height: 240rpx;
- background: #f0f0f0;
- }
- .material-info {
- padding: 16rpx;
- }
- .material-title {
- font-size: 28rpx;
- color: #333;
- font-weight: 500;
- display: block;
- margin-bottom: 8rpx;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .material-tags {
- font-size: 22rpx;
- color: #999;
- display: block;
- margin-bottom: 12rpx;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .material-actions {
- display: flex;
- justify-content: space-between;
- }
- .action-btn {
- flex: 1;
- text-align: center;
- padding: 12rpx 0;
- border-radius: 8rpx;
- font-size: 24rpx;
- margin-right: 12rpx;
- }
- .action-btn:last-child {
- margin-right: 0;
- }
- .share-btn {
- background: #FFF3E0;
- color: #F97316;
- }
- .download-btn {
- background: #E3F2FD;
- color: #2196F3;
- }
- /* 加载和空状态 */
- .loading-more, .no-more {
- text-align: center;
- padding: 24rpx;
- font-size: 24rpx;
- color: #999;
- }
- .empty-state {
- text-align: center;
- padding: 120rpx 0;
- }
- .empty-text {
- font-size: 28rpx;
- color: #999;
- }
- </style>
|