| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- import { defineStore } from 'pinia';
- import { useTransferPage } from '@/hooks/useTransferPage';
- import { ref, computed } from 'vue';
- import { getUserInfo } from '@/api/modules/login';
- import config from '@/config';
- import { Study, UserStoreState } from '@/types';
- import { UserInfo, VipCardInfo } from '@/types/user';
- import tools from '@/utils/uni-tool';
- import defaultAvatar from '@/static/personal/avatar_default.png'
- // @ts-ignore
- import { useUserStore as useOldUserStore } from '@/hooks/useUserStore';
- import { EnumReviewMode, EnumUserType } from '@/common/enum';
- import { OPEN_VIP_POPUP } from '@/types/injectionSymbols';
- import { getDirectedSchool, saveDirectedSchool } from '@/api/modules/study';
- const oldUserStore = useOldUserStore()
- const themeColor = '#31A0FC';
- type CheckLoginOptions = {
- askToLogin?: boolean;
- }
- export const useUserStore = defineStore('ie-user', {
- state: (): UserStoreState => ({
- accessToken: null,
- user: null,
- card: null,
- isDefaultModifyPwd: false,
- isPasswordExpired: false,
- isExamGuideShow: false,
- tempInfo: {
- location: '',
- examType: undefined,
- },
- org: {
- ...config.defaultOrg,
- },
- rememberPwd: false,
- cardPassword: '',
- cardNo: '',
- directedSchoolList: [],
- practiceSettings: {
- reviewMode: EnumReviewMode.AFTER_SUBMIT,
- autoNext: false
- }
- }),
- getters: {
- isLogin(state: UserStoreState): boolean {
- return !!state.accessToken && state.user !== null;
- },
- userInfo(state: UserStoreState): UserInfo {
- return state.user || {} as UserInfo;
- },
- avatar(state: UserStoreState): string {
- return this.userInfo.avatar || defaultAvatar
- },
- nickName(state: UserStoreState): string {
- return this.userInfo.nickName || '游客'
- },
- anonymousPhoneNumber(state: UserStoreState): string {
- if (this.userInfo.phonenumber && this.userInfo.phonenumber.length === 11) {
- return this.userInfo.phonenumber.slice(0, 3) + '****' + this.userInfo.phonenumber.slice(7)
- }
- return '';
- },
- isVip(): boolean {
- return !!this.userInfo.cardId;
- },
- vipInfo(state: UserStoreState): VipCardInfo {
- if (state.card) {
- return state.card;
- }
- return {} as VipCardInfo;
- },
- orgInfo(state: UserStoreState) {
- return state.org;
- },
- isStudent(): boolean {
- return this.userInfo.userType === EnumUserType.STUDENT;
- },
- isTeacher(): boolean {
- return this.userInfo.userType === EnumUserType.TEACHER;
- },
- isAgent(): boolean {
- return this.userInfo.userType === EnumUserType.AGENT;
- },
- isAuditor(): boolean {
- return false;
- return this.userInfo.accountType === 2;
- },
- hasDirectedSchool(): boolean {
- return this.directedSchoolList.length > 0;
- },
- /**
- * 需要完成信息完善的用户类型
- * @returns
- */
- needCompleteInfo(): boolean {
- return this.isTeacher || this.isAgent;
- },
- // 兼容游客时显示
- getLocation(): string {
- if (this.isLogin) {
- return this.user?.location || '';
- }
- return this.tempInfo?.location || '';
- },
- getExamType(): string {
- if (this.isLogin) {
- return this.user?.examType || '';
- }
- return this.tempInfo?.examType || '';
- }
- },
- actions: {
- setToken(token: string) {
- this.accessToken = token;
- },
- async login(token: string) {
- try {
- this.accessToken = token;
- await oldUserStore.SyncToken(token);
- const userInfo = await this.getUserInfo();;
- return Promise.resolve({
- success: true,
- userInfo: userInfo
- });
- } catch (error) {
- return Promise.resolve({
- success: false,
- userInfo: null
- });
- }
- },
- checkLogin({ askToLogin = true }: CheckLoginOptions = {}): Promise<boolean> {
- if (this.isLogin) {
- return Promise.resolve(true);
- }
- const { transferTo } = useTransferPage();
- return new Promise((resolve, reject) => {
- if (askToLogin) {
- tools.showModal({
- title: '提示',
- content: '当前未登录,是否前往登录?',
- confirmColor: themeColor
- }).then(confirm => {
- if (confirm) {
- transferTo('/pagesSystem/pages/login/login').then((res) => {
- resolve(res as boolean);
- }).catch(() => {
- resolve(false);
- });
- }
- });
- } else {
- transferTo('/pagesSystem/pages/login/login').then((res) => {
- resolve(res as boolean);
- }).catch(() => {
- resolve(false);
- });
- }
- });
- },
- checkVip() {
- return new Promise((resolve, reject) => {
- if (!this.isVip) {
- resolve(false);
- }
- resolve(true);
- });
- },
- checkInfoComplete() {
- return new Promise((resolve, reject) => {
- if (this.needCompleteInfo && (!this.userInfo.location || !this.userInfo.examType || !this.userInfo.endYear)) {
- const { transferTo } = useTransferPage();
- transferTo('/pagesSystem/pages/bind-teacher-profile/bind-teacher-profile').then(res => {
- resolve(res as boolean);
- }).catch(() => {
- resolve(false);
- });
- } else {
- resolve(true);
- }
- });
- },
- async getUserInfo() {
- const res = await getUserInfo();
- const { data, isDefaultModifyPwd, isPasswordExpired, card, org } = res;
- if (data) {
- this.user = data;
- if (card) {
- this.card = card;
- }
- this.isDefaultModifyPwd = isDefaultModifyPwd;
- this.isPasswordExpired = isPasswordExpired;
- }
- if (org) {
- this.org = org;
- }
- return data;
- },
- /**
- * 保存定向学校列表
- * @param list 定向学校列表
- */
- async saveDirectedSchoolList(list: Study.DirectedSchool[]) {
- await saveDirectedSchool(list);
- await this.getDirectedSchoolList();
- },
- /**
- * 获取定向学校列表
- */
- async getDirectedSchoolList() {
- const { data } = await getDirectedSchool();
- this.directedSchoolList = data || [];
- },
- setPracticeSettings(settings: Study.PracticeSettings) {
- this.practiceSettings = settings;
- },
- askLogout() {
- return new Promise((resolve, reject) => {
- uni.$ie.showModal({
- title: '提示',
- content: '确定退出登录吗?',
- confirmColor: themeColor
- }).then(confirm => {
- if (confirm) {
- this.logout();
- resolve(true);
- } else {
- resolve(false);
- }
- });
- });
- },
- rememberLoginInfo(remember: boolean, cardNo: string, cardPassword: string) {
- this.rememberPwd = remember;
- if (remember) {
- this.cardNo = cardNo;
- this.cardPassword = cardPassword;
- } else {
- this.cardNo = '';
- this.cardPassword = '';
- }
- },
- logout() {
- this.accessToken = null;
- this.user = null;
- this.isDefaultModifyPwd = false;
- this.isPasswordExpired = false;
- this.org = {
- ...config.defaultOrg,
- };
- oldUserStore.Logout();
- // const { transferTo } = useTransferPage();
- // setTimeout(() => {
- // transferTo('/pagesMain/pages/index/index', {
- // type: 'reLaunch'
- // })
- // }, 300);
- },
- },
- persist: {
- storage: {
- getItem: uni.getStorageSync,
- setItem: uni.setStorageSync,
- }
- }
- });
|