import Taro from '@tarojs/taro' import type { DesignItem, OrderItem } from '../types' const DESIGN_KEY = 'smart_design_list' const ORDER_KEY = 'smart_order_list' const USER_KEY = 'smart_user_info' const THEME_KEY = 'smart_theme' /** 获取设计清单 */ export const getDesignList = (): DesignItem[] => { try { return Taro.getStorageSync(DESIGN_KEY) || [] } catch { return [] } } /** 保存设计清单 */ export const setDesignList = (list: DesignItem[]) => { Taro.setStorageSync(DESIGN_KEY, list) } /** 获取订单列表 */ export const getOrderList = (): OrderItem[] => { try { return Taro.getStorageSync(ORDER_KEY) || [] } catch { return [] } } /** 保存订单列表 */ export const setOrderList = (list: OrderItem[]) => { Taro.setStorageSync(ORDER_KEY, list) } /** 添加设计清单条目 */ export const addDesign = (product: any, count: number): DesignItem => { const list = getDesignList() const item: DesignItem = { id: 'DSG' + Date.now(), productId: product.id, productName: product.name, productIcon: product.icon, unitPrice: product.price, count, status: 'undesigned', createdAt: new Date().toISOString().slice(0, 10) } setDesignList([...list, item]) return item } /** 更新设计清单条目 */ export const updateDesign = (id: string, patch: Partial) => { const list = getDesignList() const idx = list.findIndex(d => d.id === id) if (idx === -1) return list[idx] = { ...list[idx], ...patch } setDesignList(list) } /** 设计转订单 */ export const designToOrder = (designId: string): OrderItem | null => { const dList = getDesignList() const oList = getOrderList() const design = dList.find(d => d.id === designId) if (!design) return null const order: OrderItem = { id: 'ORD' + Date.now().toString().slice(-9), productName: design.productName, productIcon: design.productIcon, statusCode: 'pending', date: new Date().toLocaleString('zh-CN', { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' }).replace(/\//g, '-'), price: '¥' + (design.unitPrice * design.count).toFixed(2), count: design.count, sku: `${design.productName} × ${design.count}` } // 更新 design 状态 const dIdx = dList.findIndex(d => d.id === designId) if (dIdx !== -1) { dList[dIdx].status = 'ordered' dList[dIdx].orderId = order.id } setDesignList(dList) setOrderList([order, ...oList]) return order } /** 用户数据 */ export const getUserInfo = () => { try { return Taro.getStorageSync(USER_KEY) } catch { return null } } export const setUserInfo = (info: any) => { Taro.setStorageSync(USER_KEY, info) } /** 主题 */ export type ThemeMode = 'light' | 'dark' export const getTheme = (): ThemeMode => { try { return Taro.getStorageSync(THEME_KEY) || 'light' } catch { return 'light' } } export const setTheme = (theme: ThemeMode) => { Taro.setStorageSync(THEME_KEY, theme) }