| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368 |
- import { defineStore } from 'pinia';
- import { useTransferPage } from '@/hooks/useTransferPage';
- import { getUserInfo, logoutPhysical } from '@/api/modules/login';
- import config from '@/config';
- import type { Study, UserStoreState } from '@/types';
- import type { UserInfo, VipCardInfo } from '@/types/user';
- import tools from '@/utils/uni-tool';
- import defaultAvatar from '@/static/personal/avatar_default.png'
- import {CardType, EnumBindScene, EnumExamType, EnumReviewMode, EnumScoreLock, EnumUserType} from '@/common/enum';
- import { getDirectedSchool, saveDirectedSchool } from '@/api/modules/study';
- import { usePay } from '@/hooks/usePay';
- import { getUserBindCard } from '@/api/modules/user';
- 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 && Object.keys(state.user).length > 0;
- },
- 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 {
- if (!this.card) {
- return false;
- }
- const { outDate } = this.card;
- return !!this.userInfo.cardId && new Date(outDate) > new Date();
- },
- isExperienceVip(): boolean {
- if (!this.card) {
- return false;
- }
- const { outDate } = this.card;
- return this.card?.type === CardType.EXPERIENCE && new Date(outDate) > new Date();
- },
- 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;
- },
- hasTestAndRecord(): boolean {
- // 职高对口升学 目前不支持测评
- return this.getExamType != EnumExamType.VHS;
- },
- /**
- * 需要完成信息完善的用户类型
- * @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 || '';
- },
- isHN(): boolean {
- return this.getLocation == '湖南'
- },
- isVHS(): boolean {
- return this.getExamType === EnumExamType.VHS;
- },
- examMajorName(): string {
- return this.userInfo.examMajorName || '';
- },
- isScoreLocked(): boolean {
- return this.userInfo.scoreLock == EnumScoreLock.locked
- },
- isScoreUnlocked(): boolean {
- return this.userInfo.scoreLock == EnumScoreLock.unlock
- },
- isScoreLocking(): boolean {
- return this.userInfo.scoreLock == EnumScoreLock.locking
- },
- isBindWechat(): boolean {
- return !!this.userInfo.wxOpenId;
- }
- },
- actions: {
- setToken(token: string) {
- this.accessToken = token;
- },
- async login(token: string) {
- try {
- this.accessToken = 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);
- });
- },
- /**
- * 检查老师是否完善信息
- * @returns
- */
- checkInfoTeacherComplete() {
- 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);
- }
- });
- },
- /**
- * 检查学生是否需要完善信息
- * @returns
- */
- checkInfoStudentComplete(): Promise<boolean> {
- const { queryPayStatus } = usePay();
- return new Promise(async (resolve, reject) => {
- const payResult = await queryPayStatus();
- if (payResult && payResult.isPaySuccess) {
- const { data } = await getUserBindCard();
- if (data && data.cardNo && data.password) {
- const submitInfo = {
- token: this.accessToken,
- scene: EnumBindScene.LOGIN_BIND,
- userInfo: this.userInfo,
- cardInfo: data,
- registerInfo: {
- username: data.cardNo,
- password: data.password,
- }
- };
- const { transferTo } = useTransferPage();
- transferTo('/pagesSystem/pages/bind-profile/bind-profile', {
- data: submitInfo
- }).then(res => {
- resolve(res as boolean);
- }).catch(() => {
- resolve(false);
- });
- }
- } else {
- resolve(false);
- }
- });
- },
- 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;
- }
- const { getPrice } = usePay();
- getPrice();
- 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,
- };
- const { transferTo, routes } = useTransferPage();
- setTimeout(() => {
- transferTo(routes.pageIndex, {
- type: 'reLaunch'
- });
- }, 0);
- },
- deleteAccount() {
- uni.$ie.showLoading('注销中...');
- logoutPhysical().then(() => {
- this.logout();
- }).finally(() => {
- uni.$ie.hideLoading();
- });
- },
- /**
- * 拨打客服电话
- * @returns
- */
- callContactPhone() {
- if (!this.orgInfo.contactPhone) {
- uni.$ie.showToast('暂无客服电话');
- return;
- }
- uni.showActionSheet({
- title: '客服电话',
- itemList: [`拨打电话:${this.orgInfo.contactPhone}`],
- success: (res) => {
- uni.makePhoneCall({
- phoneNumber: this.orgInfo.contactPhone
- });
- }
- });
- }
- },
- persist: {
- storage: {
- getItem: uni.getStorageSync,
- setItem: uni.setStorageSync,
- }
- }
- });
|