清空代码-初始化yudao
This commit is contained in:
+48
-53
@@ -1,98 +1,93 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import cartApi from '@/sheep/api/cart';
|
||||
import CartApi from '@/sheep/api/trade/cart';
|
||||
|
||||
const cart = defineStore({
|
||||
id: 'cart',
|
||||
state: () => ({
|
||||
list: [], // 购物车列表
|
||||
selectedIds: [], // 已选列表
|
||||
isAllSelected: false, //是否全选
|
||||
cartSelectedTotalPrice: '0.00', // 选中项总金额
|
||||
isAllSelected: false, // 是否全选
|
||||
totalPriceSelected: 0, // 选中项总金额
|
||||
}),
|
||||
getters: {
|
||||
totalPriceSelected: (state) => {
|
||||
let price = 0;
|
||||
if (!state.selectedIds.length) return price.toFixed(2);
|
||||
state.list.forEach((item) => {
|
||||
price += state.selectedIds.includes(item.id)
|
||||
? Number(item.sku.price/100) * item.count
|
||||
: 0;
|
||||
});
|
||||
return price.toFixed(2);
|
||||
},
|
||||
},
|
||||
actions: {
|
||||
// 获取购物车列表
|
||||
async getList() {
|
||||
const { data, code } = await cartApi.list();
|
||||
const { data, code } = await CartApi.getCartList();
|
||||
if (code === 0) {
|
||||
this.list = data.validList;
|
||||
|
||||
// 计算各种关联属性
|
||||
this.selectedIds = [];
|
||||
this.isAllSelected = true;
|
||||
this.totalPriceSelected = 0;
|
||||
this.list.forEach((item) => {
|
||||
if (item.selected) {
|
||||
this.selectedIds.push(item.id);
|
||||
this.totalPriceSelected += item.count * item.sku.price;
|
||||
} else {
|
||||
this.isAllSelected = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
// 添加购物车
|
||||
async add(goodsInfo) {
|
||||
const { error } = await cartApi.append({
|
||||
goods_id: goodsInfo.goods_id,
|
||||
goods_num: goodsInfo.goods_num,
|
||||
goods_sku_price_id: goodsInfo.id,
|
||||
// 添加购物项
|
||||
const { code } = await CartApi.addCart({
|
||||
skuId: goodsInfo.id,
|
||||
count: goodsInfo.goods_num,
|
||||
});
|
||||
if (error === 0) {
|
||||
this.getList();
|
||||
// 刷新购物车列表
|
||||
if (code === 0) {
|
||||
await this.getList();
|
||||
}
|
||||
},
|
||||
|
||||
// 更新购物车
|
||||
async update(goodsInfo) {
|
||||
const { error } = await cartApi.update({
|
||||
const { code } = await CartApi.updateCartCount({
|
||||
id: goodsInfo.goods_id,
|
||||
count: goodsInfo.goods_num,
|
||||
goods_sku_price_id: goodsInfo.goods_sku_price_id,
|
||||
});
|
||||
if (error === 0) {
|
||||
// this.getList();
|
||||
if (code === 0) {
|
||||
await this.getList();
|
||||
}
|
||||
},
|
||||
|
||||
// 移除购物车
|
||||
async delete(ids) {
|
||||
if (typeof ids === 'array') {
|
||||
ids = ids.join(',');
|
||||
}
|
||||
const { code } = await cartApi.delete(ids);
|
||||
const { code } = await CartApi.deleteCart(ids.join(','));
|
||||
if (code === 0) {
|
||||
this.selectAll(false);
|
||||
this.getList();
|
||||
await this.getList();
|
||||
}
|
||||
},
|
||||
|
||||
// 选择购物车商品
|
||||
selectSingle(goodsId) {
|
||||
if (!this.selectedIds.includes(goodsId)) {
|
||||
this.selectedIds.push(goodsId);
|
||||
} else {
|
||||
this.selectedIds.splice(this.selectedIds.indexOf(goodsId), 1);
|
||||
// 单选购物车商品
|
||||
async selectSingle(goodsId) {
|
||||
const { code } = await CartApi.updateCartSelected({
|
||||
ids: [goodsId],
|
||||
selected: !this.selectedIds.includes(goodsId), // 取反
|
||||
});
|
||||
if (code === 0) {
|
||||
await this.getList();
|
||||
}
|
||||
this.isAllSelected = this.selectedIds.length === this.list.length;
|
||||
},
|
||||
|
||||
// 全选
|
||||
selectAll(flag) {
|
||||
this.isAllSelected = flag;
|
||||
if (!flag) {
|
||||
this.selectedIds = [];
|
||||
} else {
|
||||
this.list.forEach((item) => {
|
||||
this.selectedIds.push(item.id);
|
||||
});
|
||||
// 全选购物车商品
|
||||
async selectAll(flag) {
|
||||
const { code } = await CartApi.updateCartSelected({
|
||||
ids: this.list.map((item) => item.id),
|
||||
selected: flag
|
||||
});
|
||||
if (code === 0) {
|
||||
await this.getList();
|
||||
}
|
||||
},
|
||||
|
||||
// 清空购物车
|
||||
emptyList() {
|
||||
this.list = [];
|
||||
this.selectedIds = [];
|
||||
this.isAllSelected = false;
|
||||
this.cartSelectedTotalPrice = '0.00';
|
||||
async emptyList() {
|
||||
await this.delete(this.list.map((item) => item.id));
|
||||
},
|
||||
},
|
||||
persist: {
|
||||
|
||||
@@ -13,10 +13,11 @@ const modal = defineStore({
|
||||
smsRegister: 0,
|
||||
changeMobile: 0,
|
||||
resetPassword: 0,
|
||||
changePassword: 0,
|
||||
}
|
||||
}),
|
||||
persist: {
|
||||
enabled: true,
|
||||
enabled: true,
|
||||
strategies: [
|
||||
{
|
||||
key: 'modal-store',
|
||||
|
||||
+24
-15
@@ -14,6 +14,7 @@ import app from './app';
|
||||
import {
|
||||
showAuthModal
|
||||
} from '@/sheep/hooks/useModal';
|
||||
import AuthUtil from '@/sheep/api/member/auth';
|
||||
|
||||
// 默认用户信息
|
||||
const defaultUserInfo = {
|
||||
@@ -51,6 +52,7 @@ const user = defineStore({
|
||||
|
||||
actions: {
|
||||
// 获取个人信息
|
||||
// TODO 芋艿:整理下;
|
||||
async getInfo() {
|
||||
const {
|
||||
code,
|
||||
@@ -58,20 +60,21 @@ const user = defineStore({
|
||||
} = await userApi.profile();
|
||||
|
||||
// 为了兼容 获取用户余额 可能还会用到其他参数
|
||||
// 优惠券数量,积分数量 应该在这里
|
||||
// 优惠券数量,积分数量 应该在这里
|
||||
const {
|
||||
code: code2,
|
||||
data: data2
|
||||
} = await userApi.balance();
|
||||
if (code !== 0 || code2 != 0) return;
|
||||
data.money = data2.balance / 100;
|
||||
data.money = data2.balance;
|
||||
this.userInfo = data;
|
||||
console.log(data2, '信息')
|
||||
return Promise.resolve(data);
|
||||
},
|
||||
|
||||
// 获取分销商信息
|
||||
async getAgentInfo() {
|
||||
// TODO 芋艿:整理下;
|
||||
async getAgentInfo() {
|
||||
const res = await commissionApi.agent();
|
||||
if (res.error === 0) {
|
||||
this.agentInfo = res.data;
|
||||
@@ -80,7 +83,8 @@ const user = defineStore({
|
||||
},
|
||||
|
||||
// 获取订单、优惠券等其他资产信息
|
||||
async getNumData() {
|
||||
// TODO 芋艿:整理下;
|
||||
async getNumData() {
|
||||
const {
|
||||
code,
|
||||
data
|
||||
@@ -103,7 +107,8 @@ const user = defineStore({
|
||||
},
|
||||
|
||||
// 添加分享记录
|
||||
async addShareLog(params) {
|
||||
// TODO 芋艿:整理下;
|
||||
async addShareLog(params) {
|
||||
const {
|
||||
error
|
||||
} = await userApi.addShareLog(params);
|
||||
@@ -111,7 +116,8 @@ const user = defineStore({
|
||||
},
|
||||
|
||||
// 设置token
|
||||
setToken(token = '') {
|
||||
// TODO 芋艿:整理下;
|
||||
setToken(token = '') {
|
||||
if (token === '') {
|
||||
this.isLogin = false;
|
||||
uni.removeStorageSync('token');
|
||||
@@ -124,7 +130,8 @@ const user = defineStore({
|
||||
},
|
||||
|
||||
// 更新用户相关信息 (手动限流 5秒之内不刷新)
|
||||
async updateUserData() {
|
||||
// TODO 芋艿:整理下;
|
||||
async updateUserData() {
|
||||
if (!this.isLogin) {
|
||||
this.resetUserData();
|
||||
return;
|
||||
@@ -138,7 +145,8 @@ const user = defineStore({
|
||||
},
|
||||
|
||||
// 重置用户默认数据
|
||||
resetUserData() {
|
||||
// TODO 芋艿:整理下;
|
||||
resetUserData() {
|
||||
this.setToken();
|
||||
this.userInfo = clone(defaultUserInfo);
|
||||
this.numData = cloneDeep(defaultNumData);
|
||||
@@ -147,7 +155,8 @@ const user = defineStore({
|
||||
},
|
||||
|
||||
// 登录后
|
||||
async loginAfter() {
|
||||
// TODO 芋艿:整理下;
|
||||
async loginAfter() {
|
||||
await this.updateUserData();
|
||||
cart().getList();
|
||||
// 登录后设置全局分享参数
|
||||
@@ -158,7 +167,8 @@ const user = defineStore({
|
||||
// }
|
||||
|
||||
// 添加分享记录
|
||||
const shareLog = uni.getStorageSync('shareLog');
|
||||
// TODO 芋艿:整理下;
|
||||
const shareLog = uni.getStorageSync('shareLog');
|
||||
if (!isEmpty(shareLog)) {
|
||||
this.addShareLog({
|
||||
...shareLog,
|
||||
@@ -167,12 +177,11 @@ const user = defineStore({
|
||||
},
|
||||
|
||||
// 登出
|
||||
async logout(force = false) {
|
||||
// TODO 芋艿:整理下;
|
||||
async logout(force = false) {
|
||||
if (!force) {
|
||||
const {
|
||||
error
|
||||
} = await userApi.logout();
|
||||
if (error === 0) {
|
||||
const { code } = AuthUtil.logout();
|
||||
if (code === 0) {
|
||||
this.resetUserData();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user