|
@@ -1,17 +1,9 @@
|
|
|
-import {
|
|
|
- defineStore
|
|
|
-} from 'pinia';
|
|
|
+import { defineStore } from 'pinia';
|
|
|
import $share from '@/sheep/platform/share';
|
|
|
-import {
|
|
|
- isEmpty,
|
|
|
- cloneDeep,
|
|
|
- clone
|
|
|
-} from 'lodash';
|
|
|
+import { clone, cloneDeep } from 'lodash';
|
|
|
import cart from './cart';
|
|
|
import app from './app';
|
|
|
-import {
|
|
|
- showAuthModal
|
|
|
-} from '@/sheep/hooks/useModal';
|
|
|
+import { showAuthModal } from '@/sheep/hooks/useModal';
|
|
|
import UserApi from '@/sheep/api/member/user';
|
|
|
import PayWalletApi from '@/sheep/api/pay/wallet';
|
|
|
import OrderApi from '@/sheep/api/trade/order';
|
|
@@ -19,17 +11,17 @@ import CouponApi from '@/sheep/api/promotion/coupon';
|
|
|
|
|
|
// 默认用户信息
|
|
|
const defaultUserInfo = {
|
|
|
- avatar: '', // 头像
|
|
|
- nickname: '', // 昵称
|
|
|
- gender: 0, // 性别
|
|
|
- mobile: '', // 手机号
|
|
|
+ avatar: '', // 头像
|
|
|
+ nickname: '', // 昵称
|
|
|
+ gender: 0, // 性别
|
|
|
+ mobile: '', // 手机号
|
|
|
point: 0, // 积分
|
|
|
};
|
|
|
|
|
|
// 默认钱包信息
|
|
|
const defaultUserWallet = {
|
|
|
balance: 0, // 余额
|
|
|
-}
|
|
|
+};
|
|
|
|
|
|
// 默认订单、优惠券等其他资产信息
|
|
|
const defaultNumData = {
|
|
@@ -41,29 +33,29 @@ const defaultNumData = {
|
|
|
deliveredCount: 0,
|
|
|
uncommentedCount: 0,
|
|
|
afterSaleCount: 0,
|
|
|
- },
|
|
|
+ },
|
|
|
};
|
|
|
|
|
|
const user = defineStore({
|
|
|
- id: 'user',
|
|
|
- state: () => ({
|
|
|
- userInfo: clone(defaultUserInfo), // 用户信息
|
|
|
+ id: 'user',
|
|
|
+ state: () => ({
|
|
|
+ userInfo: clone(defaultUserInfo), // 用户信息
|
|
|
userWallet: clone(defaultUserWallet), // 用户钱包信息
|
|
|
- isLogin: !!uni.getStorageSync('token'), // 登录状态
|
|
|
- numData: cloneDeep(defaultNumData), // 用户其他数据
|
|
|
- lastUpdateTime: 0, // 上次更新时间
|
|
|
- }),
|
|
|
-
|
|
|
- actions: {
|
|
|
- // 获取用户信息
|
|
|
- async getInfo() {
|
|
|
- const { code, data } = await UserApi.getUserInfo();
|
|
|
+ isLogin: !!uni.getStorageSync('token'), // 登录状态
|
|
|
+ numData: cloneDeep(defaultNumData), // 用户其他数据
|
|
|
+ lastUpdateTime: 0, // 上次更新时间
|
|
|
+ }),
|
|
|
+
|
|
|
+ actions: {
|
|
|
+ // 获取用户信息
|
|
|
+ async getInfo() {
|
|
|
+ const { code, data } = await UserApi.getUserInfo();
|
|
|
if (code !== 0) {
|
|
|
return;
|
|
|
}
|
|
|
this.userInfo = data;
|
|
|
- return Promise.resolve(data);
|
|
|
- },
|
|
|
+ return Promise.resolve(data);
|
|
|
+ },
|
|
|
|
|
|
// 获得用户钱包
|
|
|
async getWallet() {
|
|
@@ -74,7 +66,7 @@ const user = defineStore({
|
|
|
this.userWallet = data;
|
|
|
},
|
|
|
|
|
|
- // 获取订单、优惠券等其他资产信息
|
|
|
+ // 获取订单、优惠券等其他资产信息
|
|
|
getNumData() {
|
|
|
OrderApi.getOrderCount().then(res => {
|
|
|
if (res.code === 0) {
|
|
@@ -86,101 +78,103 @@ const user = defineStore({
|
|
|
this.numData.unusedCouponCount = res.data;
|
|
|
}
|
|
|
});
|
|
|
- },
|
|
|
+ },
|
|
|
|
|
|
- // 添加分享记录
|
|
|
+ // 添加分享记录
|
|
|
// TODO 芋艿:整理下;
|
|
|
- async addShareLog(params) {
|
|
|
- const {
|
|
|
- error
|
|
|
- } = await userApi.addShareLog(params);
|
|
|
- if (error === 0) uni.removeStorageSync('shareLog');
|
|
|
- },
|
|
|
-
|
|
|
- // 设置 token
|
|
|
+ // async addShareLog(params) {
|
|
|
+ // const {
|
|
|
+ // error
|
|
|
+ // } = await userApi.addShareLog(params);
|
|
|
+ // if (error === 0) uni.removeStorageSync('shareLog');
|
|
|
+ // },
|
|
|
+
|
|
|
+ // 设置 token
|
|
|
setToken(token = '', refreshToken = '') {
|
|
|
- if (token === '') {
|
|
|
- this.isLogin = false;
|
|
|
- uni.removeStorageSync('token');
|
|
|
- uni.removeStorageSync('refresh-token')
|
|
|
- } else {
|
|
|
- this.isLogin = true;
|
|
|
- uni.setStorageSync('token', token);
|
|
|
+ if (token === '') {
|
|
|
+ this.isLogin = false;
|
|
|
+ uni.removeStorageSync('token');
|
|
|
+ uni.removeStorageSync('refresh-token');
|
|
|
+ } else {
|
|
|
+ this.isLogin = true;
|
|
|
+ uni.setStorageSync('token', token);
|
|
|
uni.setStorageSync('refresh-token', refreshToken);
|
|
|
- this.loginAfter();
|
|
|
- }
|
|
|
- return this.isLogin;
|
|
|
- },
|
|
|
+ this.loginAfter();
|
|
|
+ }
|
|
|
+ return this.isLogin;
|
|
|
+ },
|
|
|
|
|
|
- // 更新用户相关信息 (手动限流,5 秒之内不刷新)
|
|
|
+ // 更新用户相关信息 (手动限流,5 秒之内不刷新)
|
|
|
async updateUserData() {
|
|
|
- if (!this.isLogin) {
|
|
|
- this.resetUserData();
|
|
|
- return;
|
|
|
- }
|
|
|
+ if (!this.isLogin) {
|
|
|
+ this.resetUserData();
|
|
|
+ return;
|
|
|
+ }
|
|
|
// 防抖,5 秒之内不刷新
|
|
|
- const nowTime = new Date().getTime();
|
|
|
+ const nowTime = new Date().getTime();
|
|
|
if (this.lastUpdateTime + 5000 > nowTime) {
|
|
|
return;
|
|
|
}
|
|
|
this.lastUpdateTime = nowTime;
|
|
|
|
|
|
// 获取最新信息
|
|
|
- await this.getInfo();
|
|
|
+ await this.getInfo();
|
|
|
this.getWallet();
|
|
|
- this.getNumData();
|
|
|
- return this.userInfo;
|
|
|
- },
|
|
|
+ this.getNumData();
|
|
|
+ return this.userInfo;
|
|
|
+ },
|
|
|
|
|
|
- // 重置用户默认数据
|
|
|
+ // 重置用户默认数据
|
|
|
resetUserData() {
|
|
|
// 清空 token
|
|
|
- this.setToken();
|
|
|
+ this.setToken();
|
|
|
// 清空用户相关的缓存
|
|
|
- this.userInfo = clone(defaultUserInfo);
|
|
|
+ this.userInfo = clone(defaultUserInfo);
|
|
|
this.userWallet = clone(defaultUserWallet);
|
|
|
- this.numData = cloneDeep(defaultNumData);
|
|
|
+ this.numData = cloneDeep(defaultNumData);
|
|
|
// 清空购物车的缓存
|
|
|
- cart().emptyList();
|
|
|
- },
|
|
|
+ cart().emptyList();
|
|
|
+ },
|
|
|
|
|
|
- // 登录后,加载各种信息
|
|
|
+ // 登录后,加载各种信息
|
|
|
// TODO 芋艿:整理下;
|
|
|
async loginAfter() {
|
|
|
- await this.updateUserData();
|
|
|
+ await this.updateUserData();
|
|
|
|
|
|
// 加载购物车
|
|
|
- cart().getList();
|
|
|
- // 登录后设置全局分享参数
|
|
|
- $share.getShareInfo();
|
|
|
+ cart().getList();
|
|
|
+ // 登录后设置全局分享参数
|
|
|
+ $share.getShareInfo();
|
|
|
|
|
|
- // 提醒绑定手机号
|
|
|
- if (app().platform.bind_mobile && !this.userInfo.mobile) {
|
|
|
- showAuthModal('changeMobile');
|
|
|
- }
|
|
|
+ // 提醒绑定手机号
|
|
|
+ if (app().platform.bind_mobile && !this.userInfo.mobile) {
|
|
|
+ showAuthModal('changeMobile');
|
|
|
+ }
|
|
|
|
|
|
- // 添加分享记录
|
|
|
+ // 绑定推广员
|
|
|
+ $share.bindBrokerageUser()
|
|
|
+ // 添加分享记录
|
|
|
// TODO 芋艿:整理下;
|
|
|
- const shareLog = uni.getStorageSync('shareLog');
|
|
|
- if (!isEmpty(shareLog)) {
|
|
|
- this.addShareLog({
|
|
|
- ...shareLog,
|
|
|
- });
|
|
|
- }
|
|
|
- },
|
|
|
-
|
|
|
- // 登出系统
|
|
|
+ // const shareLog = uni.getStorageSync('shareLog');
|
|
|
+ // if (!isEmpty(shareLog)) {
|
|
|
+ // this.addShareLog({
|
|
|
+ // ...shareLog,
|
|
|
+ // });
|
|
|
+ // }
|
|
|
+ },
|
|
|
+
|
|
|
+ // 登出系统
|
|
|
async logout() {
|
|
|
this.resetUserData();
|
|
|
- return !this.isLogin;
|
|
|
- }
|
|
|
- },
|
|
|
- persist: {
|
|
|
- enabled: true,
|
|
|
- strategies: [{
|
|
|
- key: 'user-store',
|
|
|
- }, ],
|
|
|
- },
|
|
|
+ return !this.isLogin;
|
|
|
+ }
|
|
|
+ },
|
|
|
+ persist: {
|
|
|
+ enabled: true,
|
|
|
+ strategies: [{
|
|
|
+ key: 'user-store',
|
|
|
+ }]
|
|
|
+ },
|
|
|
});
|
|
|
|
|
|
export default user;
|