主要改了路径,还有vite.config.ts配置后端链接
This commit is contained in:
68
src/userStores/cart.ts
Normal file
68
src/userStores/cart.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
export interface CartItem {
|
||||
id: number
|
||||
name: string
|
||||
price: number
|
||||
quantity: number
|
||||
imageUrl: string
|
||||
}
|
||||
|
||||
export const useCartStore = defineStore('cart', {
|
||||
state: () => ({
|
||||
cartItems: [] as CartItem[],
|
||||
}),
|
||||
|
||||
getters: {
|
||||
// 总价计算
|
||||
totalPrice(state): number {
|
||||
return state.cartItems.reduce((sum, item) => sum + item.price * item.quantity, 0)
|
||||
},
|
||||
},
|
||||
|
||||
actions: {
|
||||
addToCart(item: CartItem) {
|
||||
const existing = this.cartItems.find(i => i.id === item.id)
|
||||
if (existing) {
|
||||
existing.quantity += item.quantity
|
||||
} else {
|
||||
this.cartItems.push(item)
|
||||
}
|
||||
this.saveToLocalStorage()
|
||||
},
|
||||
|
||||
updateQuantity(id: number, quantity: number) {
|
||||
const item = this.cartItems.find(i => i.id === id);
|
||||
if (item && quantity >= 1) {
|
||||
item.quantity = quantity;
|
||||
this.saveToLocalStorage();
|
||||
}
|
||||
},
|
||||
|
||||
removeFromCart(id: number) {
|
||||
this.cartItems = this.cartItems.filter(i => i.id !== id)
|
||||
this.saveToLocalStorage()
|
||||
},
|
||||
|
||||
clearCart() {
|
||||
this.cartItems = []
|
||||
this.saveToLocalStorage()
|
||||
},
|
||||
|
||||
loadFromLocalStorage() {
|
||||
const stored = localStorage.getItem('cart')
|
||||
if (stored) {
|
||||
this.cartItems = JSON.parse(stored)
|
||||
}
|
||||
},
|
||||
|
||||
saveToLocalStorage() {
|
||||
localStorage.setItem('cart', JSON.stringify(this.cartItems))
|
||||
},
|
||||
|
||||
setCart(items: CartItem[]) {
|
||||
this.cartItems = items
|
||||
this.saveToLocalStorage()
|
||||
},
|
||||
},
|
||||
})
|
25
src/userStores/store.ts
Normal file
25
src/userStores/store.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
|
||||
export const useStoreStore = defineStore('store', () => {
|
||||
const selectedStoreName = ref<string>('未选择门店');
|
||||
const selectedStoreId = ref<number | null>(null);
|
||||
|
||||
function setStore(name: string, id: number) {
|
||||
selectedStoreName.value = name;
|
||||
selectedStoreId.value = id;
|
||||
localStorage.setItem('selectedStoreName', name);
|
||||
localStorage.setItem('selectedStoreId', id.toString());
|
||||
}
|
||||
|
||||
function loadStoreFromLocal() {
|
||||
const name = localStorage.getItem('selectedStoreName');
|
||||
const id = Number(localStorage.getItem('selectedStoreId'));
|
||||
if (name && !isNaN(id)) {
|
||||
selectedStoreName.value = name;
|
||||
selectedStoreId.value = id;
|
||||
}
|
||||
}
|
||||
|
||||
return { selectedStoreName, selectedStoreId, setStore, loadStoreFromLocal }
|
||||
})
|
34
src/userStores/user.ts
Normal file
34
src/userStores/user.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import axios from 'axios'
|
||||
|
||||
export const useUserStore = defineStore('user', {
|
||||
state: () => ({
|
||||
userId: 10, // 默认用户 ID
|
||||
username: '', // 新增:用户名
|
||||
}),
|
||||
getters: {
|
||||
getUserId: (state) => state.userId,
|
||||
getUsername: (state) => state.username, // 新增 getter
|
||||
},
|
||||
actions: {
|
||||
setUserId(id: number) {
|
||||
this.userId = id
|
||||
},
|
||||
setUsername(name: string) {
|
||||
this.username = name
|
||||
},
|
||||
async fetchUsernameById(id: number) {
|
||||
try {
|
||||
// 设置 userId
|
||||
this.userId = id
|
||||
|
||||
const response = await axios.get(`/api/user/customers/${id}`)//ok
|
||||
|
||||
// 后端返回的用户数据结构中包含 accountName
|
||||
this.username = response.data.accountName
|
||||
} catch (error) {
|
||||
console.error('获取用户名失败:', error)
|
||||
}
|
||||
},
|
||||
},
|
||||
})
|
Reference in New Issue
Block a user