123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284 |
- /**
- * 商品相关API服务
- * 模拟后台接口,便于未来替换为真实API
- */
- import { MOCK_PRODUCTS, MOCK_ASSETS } from '@/mock/mockData.js'
- // 模拟网络延迟
- const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms))
- /**
- * 获取商品详情
- * @param {string} productId 商品ID
- * @returns {Promise} 商品详情数据
- */
- export const getProductDetail = async (productId) => {
- await delay(600) // 模拟网络延迟
- console.log('模拟API: 获取商品详情', productId)
-
- try {
- // 从模拟数据中查找商品
- const product = MOCK_PRODUCTS.data.products.find(p => p.id === productId)
-
- if (!product) {
- return {
- success: false,
- message: '商品不存在',
- data: null
- }
- }
-
- // 构建商品详情数据
- const productDetail = {
- id: product.id,
- name: product.name,
- price: Math.floor(Math.random() * 1000) + 200, // 模拟价格 200-1200
- points: Math.floor(Math.random() * 500) + 100, // 模拟积分 100-600
- mainImage: MOCK_ASSETS.products[product.assetId] || '',
- packageImage: MOCK_ASSETS.products[product.assetId] || '', // 包装图片
- qualityImage: MOCK_ASSETS.products[product.assetId] || '', // 品质图片
- qualityDescription: '采用高温制曲、二次投料,一年为一个生产周期,具有独特的酿造工艺。',
-
- // 商品特点字段 - 从酒谷商品主数据表获取
- material: '水、高粱、小麦',
- alcohol: '53%vol.',
- origin: '贵州省仁怀市茅台镇',
- type: '酱香型白酒',
- capacity: '500ml',
- storage: '干燥、通风、阴凉的环境条件下存储',
- company: '贵州茅台酒股份有限公司'
- }
-
- return {
- success: true,
- message: '商品详情获取成功',
- data: productDetail
- }
-
- } catch (error) {
- console.error('获取商品详情失败:', error)
- return {
- success: false,
- message: '商品详情获取失败',
- data: null
- }
- }
- }
- /**
- * 获取商品图片
- * @param {string} productId 商品ID
- * @param {number} imageType 图片分类 (2=商品详情图片)
- * @returns {Promise} 商品图片数据
- */
- export const getProductImages = async (productId, imageType = 2) => {
- await delay(400) // 模拟网络延迟
- console.log('模拟API: 获取商品图片', { productId, imageType })
-
- try {
- // 模拟图片数据 - 根据图片分类==2获取
- const mockImages = [
- {
- id: `${productId}_img_001`,
- url: 'https://images.unsplash.com/photo-1470337458703-46ad1756a187?w=750&h=600&fit=crop',
- type: imageType,
- sort: 1
- },
- {
- id: `${productId}_img_002`,
- url: 'https://images.unsplash.com/photo-1569529465841-dfecdab7503b?w=750&h=600&fit=crop',
- type: imageType,
- sort: 2
- },
- {
- id: `${productId}_img_003`,
- url: 'https://images.unsplash.com/photo-1584464491033-06628f3a6b7b?w=750&h=600&fit=crop',
- type: imageType,
- sort: 3
- },
- {
- id: `${productId}_img_004`,
- url: 'https://images.unsplash.com/photo-1607622750671-6cd5a99ec572?w=750&h=600&fit=crop',
- type: imageType,
- sort: 4
- },
- {
- id: `${productId}_img_005`,
- url: 'https://images.unsplash.com/photo-1596040977107-3c27c6e95ac3?w=750&h=600&fit=crop',
- type: imageType,
- sort: 5
- },
- {
- id: `${productId}_img_006`,
- url: 'https://images.unsplash.com/photo-1558346648-9757f2fa4c64?w=750&h=600&fit=crop',
- type: imageType,
- sort: 6
- }
- ]
-
- // 根据图片分类筛选
- const filteredImages = mockImages.filter(img => img.type === imageType)
-
- return {
- success: true,
- message: '商品图片获取成功',
- data: {
- images: filteredImages,
- total: filteredImages.length
- }
- }
-
- } catch (error) {
- console.error('获取商品图片失败:', error)
- return {
- success: false,
- message: '商品图片获取失败',
- data: {
- images: [],
- total: 0
- }
- }
- }
- }
- /**
- * 搜索商品
- * @param {string} keyword 搜索关键词
- * @param {number} page 页码
- * @param {number} pageSize 每页数量
- * @returns {Promise} 搜索结果
- */
- export const searchProducts = async (keyword, page = 1, pageSize = 20) => {
- await delay(500) // 模拟网络延迟
- console.log('模拟API: 搜索商品', { keyword, page, pageSize })
-
- try {
- // 从模拟数据中搜索商品
- const allProducts = MOCK_PRODUCTS.data.products
- const filteredProducts = allProducts.filter(product =>
- product.name.toLowerCase().includes(keyword.toLowerCase())
- )
-
- // 分页处理
- const startIndex = (page - 1) * pageSize
- const endIndex = startIndex + pageSize
- const paginatedProducts = filteredProducts.slice(startIndex, endIndex)
-
- // 添加图片URL
- const productsWithImages = paginatedProducts.map(product => ({
- ...product,
- imageUrl: MOCK_ASSETS.products[product.assetId] || '',
- price: Math.floor(Math.random() * 1000) + 200, // 模拟价格
- points: Math.floor(Math.random() * 500) + 100 // 模拟积分
- }))
-
- return {
- success: true,
- message: '商品搜索成功',
- data: {
- products: productsWithImages,
- total: filteredProducts.length,
- page,
- pageSize,
- totalPages: Math.ceil(filteredProducts.length / pageSize)
- }
- }
-
- } catch (error) {
- console.error('搜索商品失败:', error)
- return {
- success: false,
- message: '商品搜索失败',
- data: {
- products: [],
- total: 0,
- page,
- pageSize,
- totalPages: 0
- }
- }
- }
- }
- /**
- * 获取商品分类
- * @param {string} categoryId 分类ID (可选)
- * @returns {Promise} 分类数据
- */
- export const getCategories = async (categoryId = null) => {
- await delay(300) // 模拟网络延迟
- console.log('模拟API: 获取商品分类', { categoryId })
-
- try {
- // 这里应该调用真实的分类API
- // 暂时返回模拟数据
- const categories = [
- {
- id: 'category_jiugu',
- name: '酒谷酒',
- description: '自主品牌酒谷系列',
- imageUrl: 'https://images.unsplash.com/photo-1470337458703-46ad1756a187?w=300&h=200&fit=crop'
- },
- {
- id: 'category_maotai',
- name: '茅台酒',
- description: '茅台系列白酒',
- imageUrl: 'https://images.unsplash.com/photo-1569529465841-dfecdab7503b?w=300&h=200&fit=crop'
- },
- {
- id: 'category_wine',
- name: '红酒',
- description: '进口和国产红酒',
- imageUrl: 'https://images.unsplash.com/photo-1584464491033-06628f3a6b7b?w=300&h=200&fit=crop'
- }
- ]
-
- const result = categoryId
- ? categories.find(c => c.id === categoryId)
- : categories
-
- return {
- success: true,
- message: '分类获取成功',
- data: result
- }
-
- } catch (error) {
- console.error('获取分类失败:', error)
- return {
- success: false,
- message: '分类获取失败',
- data: null
- }
- }
- }
- /**
- * 错误处理
- * @param {Error} error 错误对象
- */
- export const handleProductError = (error) => {
- console.error('商品API错误:', error)
-
- // 根据错误类型显示不同的提示
- if (error.message.includes('网络')) {
- uni.showToast({
- title: '网络连接失败,请检查网络设置',
- icon: 'none',
- duration: 3000
- })
- } else if (error.message.includes('超时')) {
- uni.showToast({
- title: '请求超时,请稍后重试',
- icon: 'none',
- duration: 2000
- })
- } else {
- uni.showToast({
- title: '操作失败,请稍后重试',
- icon: 'none',
- duration: 2000
- })
- }
- }
|