Files
spring-festival-greetings/pages/wallpaper/index.vue

493 lines
11 KiB
Vue
Raw Normal View History

2026-01-26 18:37:32 +08:00
<template>
2026-02-03 11:17:58 +08:00
<view class="wallpaper-page">
2026-02-06 09:42:56 +08:00
<NavBar title="精美壁纸" />
2026-01-26 18:37:32 +08:00
<!-- Category Tabs -->
<view class="category-tabs">
<scroll-view scroll-x class="tabs-scroll" :show-scrollbar="false">
<view class="tabs-content">
<view
v-for="(item, index) in categories"
:key="index"
class="tab-item"
:class="{ active: currentCategoryId === item.id }"
@tap="switchCategory(item.id)"
>
{{ item.name }}
</view>
</view>
</scroll-view>
</view>
2026-02-24 20:36:36 +08:00
<!-- Points Info -->
<view class="points-bar">
<view class="points-left">
<uni-icons type="download" size="14" color="#ff9800" />
<text>每次下载消耗 {{ downloadCost }} 积分</text>
</view>
<view class="points-right">
<text>当前积分</text>
<text class="score-val">{{ userScore }}</text>
</view>
</view>
2026-01-26 18:37:32 +08:00
<!-- Wallpaper Grid -->
<scroll-view
scroll-y
class="wallpaper-scroll"
@scrolltolower="loadMore"
refresher-enabled
:refresher-triggered="isRefreshing"
@refresherrefresh="onRefresh"
>
<view class="grid-container">
<view
class="grid-item"
v-for="(item, index) in wallpapers"
:key="index"
>
<image
2026-02-03 11:17:58 +08:00
:src="getThumbUrl(item.imageUrl)"
2026-01-26 18:37:32 +08:00
mode="aspectFill"
class="wallpaper-img"
@tap="previewImage(index)"
/>
<view class="action-overlay">
2026-01-28 21:13:27 +08:00
<button
class="action-btn share"
open-type="share"
:data-item="item"
@tap.stop="shareWallpaper(item)"
>
2026-01-26 18:37:32 +08:00
<text class="icon"></text>
2026-01-28 21:13:27 +08:00
</button>
2026-02-03 12:35:26 +08:00
<view
class="action-btn download"
@tap.stop="downloadWallpaper(item)"
>
<text class="icon"></text>
</view>
2026-01-26 18:37:32 +08:00
</view>
</view>
</view>
<!-- Loading State -->
<view class="loading-state" v-if="loading">
<text>加载中...</text>
</view>
<view class="empty-state" v-if="!loading && wallpapers.length === 0">
<text>暂无壁纸</text>
</view>
<view
class="no-more"
v-if="!loading && !hasMore && wallpapers.length > 0"
>
<text>没有更多了</text>
</view>
</scroll-view>
2026-01-28 21:13:27 +08:00
2026-02-09 23:42:06 +08:00
<LoginPopup
ref="loginPopupRef"
@logind="handleLogind"
:share-token="shareToken"
/>
2026-02-25 10:27:30 +08:00
<RewardAd ref="rewardAdRef" @onReward="handleAdReward" />
2026-01-26 18:37:32 +08:00
</view>
</template>
<script setup>
2026-02-09 23:42:06 +08:00
import { ref, computed } from "vue";
2026-01-26 18:37:32 +08:00
import { getWallpaperList, getWallpaperCategoryList } from "@/api/wallpaper.js";
2026-01-28 21:13:27 +08:00
import {
saveRemoteImageToLocal,
saveRecordRequest,
getShareToken,
} from "@/utils/common.js";
2026-02-09 23:42:06 +08:00
import { onShareAppMessage, onShareTimeline, onLoad } from "@dcloudio/uni-app";
2026-03-04 12:35:59 +08:00
import { getShareReward, watchAdReward } from "@/api/system.js";
import { checkAbilityAndHandle } from "@/utils/ability.js";
2026-01-28 21:13:27 +08:00
import { useUserStore } from "@/stores/user";
2026-02-12 17:22:47 +08:00
import { saveViewRequest, trackRecord } from "@/utils/common.js";
2026-02-01 17:54:59 +08:00
import NavBar from "@/components/NavBar/NavBar.vue";
2026-02-25 10:27:30 +08:00
import RewardAd from "@/components/RewardAd/RewardAd.vue";
2026-01-28 21:13:27 +08:00
const userStore = useUserStore();
const loginPopupRef = ref(null);
2026-02-25 10:27:30 +08:00
const rewardAdRef = ref(null);
2026-01-28 21:13:27 +08:00
const isLoggedIn = computed(() => !!userStore.userInfo.nickName);
2026-02-25 10:27:30 +08:00
const userScore = computed(() => userStore.userInfo.points || 0);
2026-02-24 20:36:36 +08:00
const downloadCost = ref(20);
2026-01-26 18:37:32 +08:00
const categories = ref([]);
const currentCategoryId = ref(null);
const wallpapers = ref([]);
const page = ref(1);
const loading = ref(false);
const hasMore = ref(true);
const isRefreshing = ref(false);
2026-02-09 23:42:06 +08:00
const shareToken = ref("");
2026-01-26 18:37:32 +08:00
2026-01-28 21:13:27 +08:00
onShareAppMessage(async (options) => {
2026-02-09 22:31:01 +08:00
if (!isLoggedIn.value) {
2026-02-08 18:57:45 +08:00
const shareToken = await getShareToken("wallpaper_download_index", "");
return {
2026-02-12 01:47:53 +08:00
title: "新年好运已送达 🎊|祝福卡·头像·壁纸",
2026-02-08 18:57:45 +08:00
path: `/pages/index/index?shareToken=${shareToken}`,
imageUrl:
"https://file.lihailezzc.com/resource/8dd026d76ef7a63d123b7fd698fb989b.png",
};
}
2026-01-28 21:13:27 +08:00
getShareReward({ scene: "wallpaper_download" });
if (options.from === "button") {
2026-02-05 23:43:51 +08:00
const shareToken = await getShareToken(
2026-01-28 21:13:27 +08:00
"wallpaper_download",
options?.target?.dataset?.item?.id,
);
return {
2026-02-01 16:32:37 +08:00
title: "快来挑选喜欢的新春壁纸吧",
2026-02-05 23:43:51 +08:00
path: `/pages/wallpaper/detail?shareToken=${shareToken}`,
2026-01-28 21:13:27 +08:00
};
} else {
2026-02-06 09:42:56 +08:00
const shareToken = await getShareToken("wallpaper_download_index", "");
2026-01-28 21:13:27 +08:00
return {
2026-02-12 01:47:53 +08:00
title: "新年好运已送达 🎊|祝福卡·头像·壁纸",
2026-02-05 23:43:51 +08:00
path: `/pages/index/index?shareToken=${shareToken}`,
2026-02-03 11:17:58 +08:00
imageUrl:
2026-02-08 15:01:42 +08:00
"https://file.lihailezzc.com/resource/8dd026d76ef7a63d123b7fd698fb989b.png",
2026-01-28 21:13:27 +08:00
};
}
});
2026-02-09 22:31:01 +08:00
onShareTimeline(async () => {
const shareToken = await getShareToken("wallpaper_timeline");
2026-02-08 18:57:45 +08:00
return {
title: "精选新年壁纸,让手机也过年 🖼",
2026-02-09 22:31:01 +08:00
query: `shareToken=${shareToken}`,
2026-02-08 18:57:45 +08:00
imageUrl:
"https://file.lihailezzc.com/resource/8dd026d76ef7a63d123b7fd698fb989b.png",
};
});
2026-02-09 23:42:06 +08:00
onLoad((options) => {
fetchCategories();
if (options.shareToken) {
shareToken.value = options.shareToken;
saveViewRequest(options.shareToken, "wallpaper_download");
}
2026-02-26 14:58:04 +08:00
if (options.categoryId) {
currentCategoryId.value = options.categoryId;
}
fetchCategories();
2026-02-12 17:22:47 +08:00
trackRecord({
eventName: "wallpaper_page_visit",
eventType: `visit`,
});
2026-01-26 18:37:32 +08:00
});
2026-02-01 17:54:59 +08:00
const getThumbUrl = (url) => {
2026-02-03 11:17:58 +08:00
return `${url}?imageView2/1/w/340/h/600/q/80`;
2026-01-26 18:37:32 +08:00
};
const fetchCategories = async () => {
try {
const res = await getWallpaperCategoryList();
const list = Array.isArray(res) ? res : res?.list || [];
if (list.length > 0) {
categories.value = list;
2026-02-26 14:58:04 +08:00
if (!currentCategoryId.value) {
currentCategoryId.value = list[0].id;
}
2026-01-26 18:37:32 +08:00
loadWallpapers(true);
}
} catch (e) {
console.error("Failed to fetch categories", e);
uni.showToast({ title: "获取分类失败", icon: "none" });
}
};
const switchCategory = (id) => {
if (currentCategoryId.value === id) return;
currentCategoryId.value = id;
loadWallpapers(true);
2026-02-12 17:22:47 +08:00
trackRecord({
eventName: "wallpaper_category_click",
eventType: `select`,
elementId: id || "",
});
2026-01-26 18:37:32 +08:00
};
const loadWallpapers = async (reset = false) => {
if (loading.value) return;
if (reset) {
page.value = 1;
hasMore.value = true;
wallpapers.value = [];
}
if (!hasMore.value) return;
loading.value = true;
try {
const res = await getWallpaperList(currentCategoryId.value, page.value);
const list = res?.list || [];
hasMore.value = !!res?.hasNext;
if (reset) {
wallpapers.value = list;
} else {
wallpapers.value = [...wallpapers.value, ...list];
}
if (hasMore.value) {
page.value++;
}
} catch (e) {
console.error("Failed to fetch wallpapers", e);
uni.showToast({ title: "获取壁纸失败", icon: "none" });
} finally {
loading.value = false;
isRefreshing.value = false;
}
};
const loadMore = () => {
loadWallpapers();
};
2026-01-28 21:13:27 +08:00
const handleLogind = async () => {
// Logic after successful login if needed
};
2026-01-26 18:37:32 +08:00
const onRefresh = () => {
isRefreshing.value = true;
loadWallpapers(true);
};
const previewImage = (index) => {
2026-02-12 17:22:47 +08:00
// const urls = wallpapers.value.map((item) => item.url);
// uni.previewImage({
// urls,
// current: index,
// });
const item = wallpapers.value[index];
trackRecord({
eventName: "wallpaper_preview_click",
eventType: `select`,
elementId: item?.id || "",
2026-01-26 18:37:32 +08:00
});
};
2026-02-25 10:27:30 +08:00
const handleAdReward = async (token) => {
try {
const res = await watchAdReward(token);
if (res) {
uni.showToast({
title: "获得50积分",
icon: "success",
});
await userStore.fetchUserAssets();
}
} catch (e) {
console.error("Reward claim failed", e);
uni.showToast({ title: "奖励发放失败", icon: "none" });
}
};
2026-01-28 21:13:27 +08:00
const downloadWallpaper = async (item) => {
2026-02-12 17:22:47 +08:00
trackRecord({
eventName: "wallpaper_download_click",
eventType: `click`,
elementId: item?.id || "",
});
2026-01-28 21:13:27 +08:00
if (!isLoggedIn.value) {
loginPopupRef.value.open();
return;
}
2026-03-04 12:35:59 +08:00
const canProceed = await checkAbilityAndHandle(
"wallpaper_download",
rewardAdRef,
);
if (!canProceed) return;
2026-01-28 21:13:27 +08:00
2026-01-26 18:37:32 +08:00
uni.showLoading({ title: "下载中..." });
2026-02-25 11:02:27 +08:00
try {
// Parallelize save record and download
// Wait for saveRecordRequest to ensure backend deducts points
await Promise.all([
saveRecordRequest("", item.id, "wallpaper_download", item.imageUrl),
saveRemoteImageToLocal(item.imageUrl),
]);
// Refresh user assets to show updated points
await userStore.fetchUserAssets();
uni.hideLoading();
uni.showToast({ title: "保存成功 消耗 20 积分", icon: "success" });
} catch (e) {
uni.hideLoading();
console.error("Download failed", e);
uni.showToast({ title: "下载失败", icon: "none" });
}
2026-01-26 18:37:32 +08:00
};
2026-01-28 21:13:27 +08:00
const shareWallpaper = (item) => {};
2026-01-26 18:37:32 +08:00
</script>
<style lang="scss" scoped>
.wallpaper-page {
height: 100vh;
2026-02-03 12:35:26 +08:00
background-color: #ffffff;
2026-01-26 18:37:32 +08:00
display: flex;
flex-direction: column;
box-sizing: border-box;
}
2026-02-03 12:35:26 +08:00
.category-tabs {
padding: 0;
background-color: #ffffff;
border-bottom: 1rpx solid #eeeeee;
2026-01-26 18:37:32 +08:00
position: sticky;
top: 0;
z-index: 100;
}
2026-02-24 20:36:36 +08:00
.points-bar {
display: flex;
justify-content: space-between;
align-items: center;
padding: 16rpx 30rpx;
background-color: #fffbf0;
font-size: 24rpx;
color: #666;
}
.points-left {
display: flex;
align-items: center;
gap: 8rpx;
color: #ff9800;
}
.points-right {
display: flex;
align-items: center;
}
.score-val {
color: #d81e06;
font-weight: bold;
font-size: 28rpx;
margin-left: 4rpx;
}
2026-01-26 18:37:32 +08:00
.tabs-scroll {
white-space: nowrap;
width: 100%;
}
.tabs-content {
display: inline-flex;
2026-02-03 12:35:26 +08:00
padding: 0 30rpx;
2026-01-26 18:37:32 +08:00
}
.tab-item {
2026-02-03 12:35:26 +08:00
padding: 24rpx 30rpx;
font-size: 30rpx;
color: #999999;
position: relative;
2026-01-26 18:37:32 +08:00
transition: all 0.3s;
2026-02-03 12:35:26 +08:00
font-weight: 500;
2026-01-26 18:37:32 +08:00
}
.tab-item.active {
2026-02-03 12:35:26 +08:00
color: #e60012;
font-weight: bold;
&::after {
content: "";
position: absolute;
bottom: 0;
left: 50%;
transform: translateX(-50%);
width: 80rpx;
height: 4rpx;
background-color: #e60012;
border-radius: 2rpx;
}
2026-01-26 18:37:32 +08:00
}
.wallpaper-scroll {
flex: 1;
overflow: hidden;
box-sizing: border-box;
}
.grid-container {
2026-02-03 12:35:26 +08:00
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 30rpx;
padding: 30rpx;
2026-01-26 18:37:32 +08:00
}
.grid-item {
height: 600rpx;
2026-02-03 12:35:26 +08:00
border-radius: 32rpx;
2026-01-26 18:37:32 +08:00
overflow: hidden;
position: relative;
2026-02-03 12:35:26 +08:00
background: #f5f5f5;
2026-01-26 18:37:32 +08:00
}
.wallpaper-img {
width: 100%;
height: 100%;
}
.action-overlay {
position: absolute;
bottom: 20rpx;
right: 20rpx;
display: flex;
flex-direction: column;
gap: 16rpx;
}
.action-btn {
width: 64rpx;
height: 64rpx;
border-radius: 50%;
2026-02-03 12:35:26 +08:00
background: rgba(0, 0, 0, 0.4);
2026-01-26 18:37:32 +08:00
backdrop-filter: blur(4px);
display: flex;
align-items: center;
justify-content: center;
2026-02-03 12:35:26 +08:00
border: 1rpx solid rgba(255, 255, 255, 0.2);
padding: 0;
margin: 0;
line-height: 1;
2026-01-26 18:37:32 +08:00
2026-02-03 12:35:26 +08:00
&::after {
border: none;
}
2026-01-26 18:37:32 +08:00
}
.action-btn.share .icon {
2026-02-03 12:35:26 +08:00
font-size: 30rpx;
}
.action-btn .icon {
color: #fff;
font-size: 36rpx;
font-weight: normal;
2026-01-26 18:37:32 +08:00
}
.loading-state,
.empty-state,
.no-more {
text-align: center;
padding: 40rpx;
2026-02-03 12:35:26 +08:00
color: #999999;
2026-01-26 18:37:32 +08:00
font-size: 24rpx;
}
</style>