313 lines
8.0 KiB
TypeScript
313 lines
8.0 KiB
TypeScript
// pages/home/home.ts
|
||
import { EmployeeService, Product as ApiProduct } from '../../services/employee';
|
||
|
||
interface Product {
|
||
id: number;
|
||
name: string;
|
||
price: number;
|
||
sales: number;
|
||
image: string;
|
||
category: string;
|
||
tags: string[];
|
||
hotLevel: number; // 1-5 热度等级
|
||
description?: string;
|
||
available_copies?: number;
|
||
}
|
||
|
||
interface Category {
|
||
id: string;
|
||
name: string;
|
||
}
|
||
|
||
Page({
|
||
data: {
|
||
selectedProduct: 0 as number, // 选中的商品ID
|
||
selectedProductName: '' as string, // 选中的商品名称
|
||
products: [] as Product[],
|
||
hasMore: true,
|
||
loading: false,
|
||
page: 1,
|
||
pageSize: 6
|
||
},
|
||
|
||
onLoad() {
|
||
// 不在onLoad检查登录,允许用户浏览首页
|
||
this.loadProducts();
|
||
},
|
||
|
||
// 加载商品列表
|
||
async loadProducts() {
|
||
if (this.data.loading || !this.data.hasMore) return;
|
||
|
||
this.setData({ loading: true });
|
||
|
||
try {
|
||
// 从后端API获取产品列表(公开接口,不需要登录)
|
||
const response = await EmployeeService.getProducts();
|
||
|
||
if (response.code === 200 && response.data) {
|
||
const apiProducts = response.data.list.map((product: ApiProduct, index: number) => ({
|
||
id: product.id,
|
||
name: product.name,
|
||
price: 0, // 后端暂无价格字段
|
||
sales: product.available_copies || 0,
|
||
image: product.image || `https://picsum.photos/id/${237 + index}/300/400`,
|
||
category: 'beauty', // 后端暂无分类字段
|
||
tags: ['种草', '推荐'],
|
||
hotLevel: product.available_copies > 5 ? 5 : 3,
|
||
description: product.description,
|
||
available_copies: product.available_copies
|
||
}));
|
||
|
||
this.setData({
|
||
products: apiProducts,
|
||
loading: false,
|
||
hasMore: false
|
||
});
|
||
return;
|
||
}
|
||
} catch (error) {
|
||
console.error('加载产品失败:', error);
|
||
// API失败,使用模拟数据
|
||
}
|
||
|
||
// 如果API失败,使用模拟数据
|
||
const allMockProducts = [
|
||
{
|
||
id: 1,
|
||
name: '兰蔻小黑瓶精华液',
|
||
price: 680,
|
||
sales: 10234,
|
||
image: 'https://picsum.photos/id/237/300/400',
|
||
category: 'beauty',
|
||
tags: ['美白', '抗老', '保湿'],
|
||
hotLevel: 5
|
||
},
|
||
{
|
||
id: 2,
|
||
name: 'SK-II神仙水',
|
||
price: 1299,
|
||
sales: 8765,
|
||
image: 'https://picsum.photos/id/152/300/400',
|
||
category: 'beauty',
|
||
tags: ['补水', '缩毛孔', '提亮'],
|
||
hotLevel: 5
|
||
},
|
||
{
|
||
id: 3,
|
||
name: '雅诗兰黛小棕瓶',
|
||
price: 790,
|
||
sales: 9432,
|
||
image: 'https://picsum.photos/id/292/300/400',
|
||
category: 'beauty',
|
||
tags: ['修复', '抗氧化', '紧致'],
|
||
hotLevel: 4
|
||
},
|
||
{
|
||
id: 4,
|
||
name: 'Dior烈艳蓝金口红',
|
||
price: 320,
|
||
sales: 15678,
|
||
image: 'https://picsum.photos/id/365/300/400',
|
||
category: 'beauty',
|
||
tags: ['段色', '滑顺', '持久'],
|
||
hotLevel: 5
|
||
},
|
||
{
|
||
id: 5,
|
||
name: 'AirPods Pro 2',
|
||
price: 1899,
|
||
sales: 23456,
|
||
image: 'https://picsum.photos/id/180/300/400',
|
||
category: 'digital',
|
||
tags: ['降噪', '音质', '舒适'],
|
||
hotLevel: 5
|
||
},
|
||
{
|
||
id: 6,
|
||
name: 'iPhone 15 Pro',
|
||
price: 7999,
|
||
sales: 34567,
|
||
image: 'https://picsum.photos/id/119/300/400',
|
||
category: 'digital',
|
||
tags: ['性能', '拍照', '长续航'],
|
||
hotLevel: 5
|
||
},
|
||
{
|
||
id: 7,
|
||
name: '海蓝之谜精华面霜',
|
||
price: 890,
|
||
sales: 7654,
|
||
image: 'https://picsum.photos/id/225/300/400',
|
||
category: 'beauty',
|
||
tags: ['保湿', '修复', '舒缓'],
|
||
hotLevel: 4
|
||
},
|
||
{
|
||
id: 8,
|
||
name: '迪奥烈影精华',
|
||
price: 1680,
|
||
sales: 5432,
|
||
image: 'https://picsum.photos/id/177/300/400',
|
||
category: 'beauty',
|
||
tags: ['抗衰老', '紧致', '提亮'],
|
||
hotLevel: 5
|
||
},
|
||
{
|
||
id: 9,
|
||
name: 'Zara复古风衣',
|
||
price: 299,
|
||
sales: 12345,
|
||
image: 'https://picsum.photos/id/111/300/400',
|
||
category: 'fashion',
|
||
tags: ['复古', '百搭', '时尚'],
|
||
hotLevel: 4
|
||
},
|
||
{
|
||
id: 10,
|
||
name: 'Uniqlo羊绒衫',
|
||
price: 199,
|
||
sales: 23456,
|
||
image: 'https://picsum.photos/id/222/300/400',
|
||
category: 'fashion',
|
||
tags: ['保暖', '舒适', '百搭'],
|
||
hotLevel: 5
|
||
},
|
||
{
|
||
id: 11,
|
||
name: '星巴克咖啡豆',
|
||
price: 88,
|
||
sales: 34567,
|
||
image: 'https://picsum.photos/id/431/300/400',
|
||
category: 'food',
|
||
tags: ['香浓', '精品', '中烘'],
|
||
hotLevel: 5
|
||
},
|
||
{
|
||
id: 12,
|
||
name: '三只松鼠零食礼盒',
|
||
price: 158,
|
||
sales: 19876,
|
||
image: 'https://picsum.photos/id/326/300/400',
|
||
category: 'food',
|
||
tags: ['零食', '礼盒', '美味'],
|
||
hotLevel: 4
|
||
}
|
||
];
|
||
|
||
// 模拟分页加载
|
||
setTimeout(() => {
|
||
const start = (this.data.page - 1) * this.data.pageSize;
|
||
const end = start + this.data.pageSize;
|
||
const newProducts = allMockProducts.slice(start, end);
|
||
|
||
this.setData({
|
||
products: [...this.data.products, ...newProducts],
|
||
hasMore: end < allMockProducts.length,
|
||
loading: false,
|
||
page: this.data.page + 1
|
||
});
|
||
}, 800);
|
||
},
|
||
|
||
// 切换商品选中状态
|
||
toggleProduct(e: any) {
|
||
const productId = e.currentTarget.dataset.id;
|
||
const productName = e.currentTarget.dataset.name;
|
||
|
||
this.setData({
|
||
selectedProduct: productId === this.data.selectedProduct ? 0 : productId,
|
||
selectedProductName: productId === this.data.selectedProduct ? '' : productName
|
||
});
|
||
},
|
||
|
||
// 去生成内容
|
||
async goToGenerate() {
|
||
if (!this.data.selectedProduct) {
|
||
wx.showToast({
|
||
title: '请先选择商品',
|
||
icon: 'none'
|
||
});
|
||
return;
|
||
}
|
||
// 1. 检查登录状态
|
||
const token = wx.getStorageSync('token');
|
||
if (!token) {
|
||
wx.showModal({
|
||
title: '未登录',
|
||
content: '请先登录后再查看文案',
|
||
confirmText: '去登录',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
wx.redirectTo({
|
||
url: '/pages/login/login'
|
||
});
|
||
}
|
||
}
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 2. 检查小红书绑定状态
|
||
try {
|
||
const response = await EmployeeService.getProfile();
|
||
|
||
if (response.code === 200 && response.data) {
|
||
const isBoundXHS = response.data.is_bound_xhs;
|
||
|
||
if (!isBoundXHS) {
|
||
// 未绑定小红书,提示并跳转
|
||
wx.showModal({
|
||
title: '未绑定小红书',
|
||
content: '查看文案前需要先绑定小红书账号',
|
||
confirmText: '去绑定',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
wx.navigateTo({
|
||
url: '/pages/profile/social-binding/social-binding'
|
||
});
|
||
}
|
||
}
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 登录且已绑定,跳转到文案列表页面
|
||
wx.navigateTo({
|
||
url: `/pages/articles/articles?productId=${this.data.selectedProduct}`
|
||
});
|
||
} else {
|
||
throw new Error('获取用户信息失败');
|
||
}
|
||
} catch (error) {
|
||
console.error('检查绑定状态失败:', error);
|
||
wx.showToast({
|
||
title: '获取用户信息失败',
|
||
icon: 'none'
|
||
});
|
||
}
|
||
},
|
||
|
||
// 滚动到底部加载更多
|
||
onScrollToLower() {
|
||
this.loadProducts();
|
||
},
|
||
|
||
// 分享功能
|
||
onShareAppMessage() {
|
||
return {
|
||
title: '万花筒AI助手 - 智能生成种草文案',
|
||
path: '/pages/home/home',
|
||
imageUrl: '' // 可以设置分享图片
|
||
};
|
||
},
|
||
|
||
// 分享到朋友圈
|
||
onShareTimeline() {
|
||
return {
|
||
title: '万花筒AI助手 - 智能生成种草文案',
|
||
imageUrl: '' // 可以设置分享图片
|
||
};
|
||
}
|
||
});
|