| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import { defineStore } from "pinia";
- import { USER_INFO, TOKEN, OPEN_ID, REDIRECT_KEY } from "@/settings/enums.js";
- export const useUserStore = defineStore({
- id: "app-user",
- state: () => ({
- userInfo: null,
- token: undefined,
- siteId: undefined,
- openId: undefined,
- }),
- getters: {
- getUserInfo() {
- return this.userInfo || uni.getStorageSync(USER_INFO) || {};
- },
- getToken() {
- return this.token || uni.getStorageSync(TOKEN) || "";
- },
- getOpenId() {
- return this.openId || uni.getStorageSync(USER_INFO).openId || "";
- },
- },
- actions: {
- // 修改用户信息
- setUserInfo(e) {
- this.userInfo = e;
- uni.setStorageSync(USER_INFO, e);
- },
- // 登录
- login(e) {
- this.userInfo = e;
- this.token = e.token;
- this.siteId = e.siteId;
- uni.setStorageSync(TOKEN, e.token);
- uni.setStorageSync(USER_INFO, e);
- setTimeout(() => {
- uni.showToast({
- title: "登录成功",
- duration: 1000,
- });
- });
- },
- // 退出之前的操作
- beforeLogout() {
- uni.showModal({
- title: "提示",
- content: "确定退出登录吗?",
- confirmText: "确定",
- success: (res) => {
- if (res.confirm) {
- this.logout();
- } else if (res.cancel) {
- }
- },
- });
- },
- // 退出登录
- async logout(option = {}) {
- const options = {
- redirct: true,
- ...option,
- };
- try {
- const keys = [USER_INFO, TOKEN];
- await Promise.all(keys.map((key) => uni.removeStorageSync(key))).then(
- () => {
- this.userInfo = null;
- this.token = "";
- options.redirct &&
- uni.reLaunch({
- url: "/pages/index/index",
- });
- }
- );
- return Promise.resolve();
- } catch (e) {
- console.log("eeee login", e);
- //TODO handle the exception
- }
- },
- },
- });
|