acdr-ui/dist/dev/mp-weixin/sheep/store/user.js

184 lines
5.3 KiB
JavaScript
Raw Normal View History

2024-09-19 07:20:14 +08:00
"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 sheep_platform_share = require("../platform/share.js");
const sheep_store_cart = require("./cart.js");
const sheep_store_app = require("./app.js");
const sheep_hooks_useModal = require("../hooks/useModal.js");
const sheep_api_member_user = require("../api/member/user.js");
const sheep_api_pay_wallet = require("../api/pay/wallet.js");
const sheep_api_trade_order = require("../api/trade/order.js");
const 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 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 sheep_api_pay_wallet.PayWalletApi.getPayWallet();
if (code !== 0) {
return;
}
this.userWallet = data;
});
},
// 获取订单、优惠券等其他资产信息
getNumData() {
sheep_api_trade_order.OrderApi.getOrderCount().then((res) => {
if (res.code === 0) {
this.numData.orderCount = res.data;
}
});
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);
sheep_store_cart.cart().emptyList();
},
// 登录后,加载各种信息
// TODO 芋艿:整理下;
loginAfter() {
return __async(this, null, function* () {
yield this.updateUserData();
sheep_store_cart.cart().getList();
sheep_platform_share.$share.getShareInfo();
if (sheep_store_app.app().platform.bind_mobile && !this.userInfo.mobile) {
sheep_hooks_useModal.showAuthModal("changeMobile");
}
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;
//# sourceMappingURL=user.js.map