acdr-ui/dist/dev/mp-weixin/modules/mall/sheep/store/user.js
2024-10-01 09:15:35 +08:00

183 lines
5.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use strict";
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
const common_vendor = require("../../../../common/vendor.js");
const modules_mall_sheep_platform_share = require("../platform/share.js");
const modules_mall_sheep_store_cart = require("./cart.js");
const modules_mall_sheep_store_app = require("./app.js");
const modules_mall_sheep_hooks_useModal = require("../hooks/useModal.js");
const modules_mall_sheep_api_member_user = require("../api/member/user.js");
const modules_mall_sheep_api_pay_wallet = require("../api/pay/wallet.js");
const modules_mall_sheep_api_trade_order = require("../api/trade/order.js");
const modules_mall_sheep_api_promotion_coupon = require("../api/promotion/coupon.js");
const defaultUserInfo = {
avatar: "",
// 头像
nickname: "",
// 昵称
gender: 0,
// 性别
mobile: "",
// 手机号
point: 0
// 积分
};
const defaultUserWallet = {
balance: 0
// 余额
};
const defaultNumData = {
unusedCouponCount: 0,
orderCount: {
allCount: 0,
unpaidCount: 0,
undeliveredCount: 0,
deliveredCount: 0,
uncommentedCount: 0,
afterSaleCount: 0
}
};
const user = common_vendor.defineStore({
id: "user",
state: () => ({
userInfo: common_vendor.clone(defaultUserInfo),
// 用户信息
userWallet: common_vendor.clone(defaultUserWallet),
// 用户钱包信息
isLogin: !!common_vendor.index.getStorageSync("token"),
// 登录状态
numData: common_vendor.cloneDeep(defaultNumData),
// 用户其他数据
lastUpdateTime: 0
// 上次更新时间
}),
actions: {
// 获取用户信息
getInfo() {
return __async(this, null, function* () {
const { code, data } = yield modules_mall_sheep_api_member_user.UserApi.getUserInfo();
if (code !== 0) {
return;
}
this.userInfo = data;
return Promise.resolve(data);
});
},
// 获得用户钱包
getWallet() {
return __async(this, null, function* () {
const { code, data } = yield modules_mall_sheep_api_pay_wallet.PayWalletApi.getPayWallet();
if (code !== 0) {
return;
}
this.userWallet = data;
});
},
// 获取订单、优惠券等其他资产信息
getNumData() {
modules_mall_sheep_api_trade_order.OrderApi.getOrderCount().then((res) => {
if (res.code === 0) {
this.numData.orderCount = res.data;
}
});
modules_mall_sheep_api_promotion_coupon.CouponApi.getUnusedCouponCount().then((res) => {
if (res.code === 0) {
this.numData.unusedCouponCount = res.data;
}
});
},
// 设置 token
setToken(token = "", refreshToken = "") {
if (token === "") {
this.isLogin = false;
common_vendor.index.removeStorageSync("token");
common_vendor.index.removeStorageSync("refresh-token");
} else {
this.isLogin = true;
common_vendor.index.setStorageSync("token", token);
common_vendor.index.setStorageSync("refresh-token", refreshToken);
this.loginAfter();
}
return this.isLogin;
},
// 更新用户相关信息 (手动限流5 秒之内不刷新)
updateUserData() {
return __async(this, null, function* () {
if (!this.isLogin) {
this.resetUserData();
return;
}
const nowTime = (/* @__PURE__ */ new Date()).getTime();
if (this.lastUpdateTime + 5e3 > nowTime) {
return;
}
this.lastUpdateTime = nowTime;
yield this.getInfo();
this.getWallet();
this.getNumData();
return this.userInfo;
});
},
// 重置用户默认数据
resetUserData() {
this.setToken();
this.userInfo = common_vendor.clone(defaultUserInfo);
this.userWallet = common_vendor.clone(defaultUserWallet);
this.numData = common_vendor.cloneDeep(defaultNumData);
modules_mall_sheep_store_cart.cart().emptyList();
},
// 登录后,加载各种信息
// TODO 芋艿:整理下;
loginAfter() {
return __async(this, null, function* () {
yield this.updateUserData();
modules_mall_sheep_store_cart.cart().getList();
modules_mall_sheep_platform_share.$share.getShareInfo();
if (modules_mall_sheep_store_app.app().platform.bind_mobile && !this.userInfo.mobile) {
modules_mall_sheep_hooks_useModal.showAuthModal("changeMobile");
}
modules_mall_sheep_platform_share.$share.bindBrokerageUser();
});
},
// 登出系统
logout() {
return __async(this, null, function* () {
this.resetUserData();
return !this.isLogin;
});
}
},
persist: {
enabled: true,
strategies: [
{
key: "user-store"
}
]
}
});
const __vite_glob_0_4 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
__proto__: null,
default: user
}, Symbol.toStringTag, { value: "Module" }));
exports.__vite_glob_0_4 = __vite_glob_0_4;
exports.user = user;