userStore.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import { defineStore } from 'pinia';
  2. import { useTransferPage } from '@/hooks/useTransferPage';
  3. import { ref, computed } from 'vue';
  4. import { getUserInfo } from '@/api/modules/login';
  5. import config from '@/config';
  6. import { Study, UserStoreState } from '@/types';
  7. import { UserInfo, VipCardInfo } from '@/types/user';
  8. import tools from '@/utils/uni-tool';
  9. import defaultAvatar from '@/static/personal/avatar_default.png'
  10. // @ts-ignore
  11. import { useUserStore as useOldUserStore } from '@/hooks/useUserStore';
  12. import { CardType, EnumReviewMode, EnumUserType } from '@/common/enum';
  13. import { OPEN_VIP_POPUP } from '@/types/injectionSymbols';
  14. import { getDirectedSchool, saveDirectedSchool } from '@/api/modules/study';
  15. const oldUserStore = useOldUserStore()
  16. const themeColor = '#31A0FC';
  17. type CheckLoginOptions = {
  18. askToLogin?: boolean;
  19. }
  20. export const useUserStore = defineStore('ie-user', {
  21. state: (): UserStoreState => ({
  22. accessToken: null,
  23. user: null,
  24. card: null,
  25. isDefaultModifyPwd: false,
  26. isPasswordExpired: false,
  27. isExamGuideShow: false,
  28. tempInfo: {
  29. location: '',
  30. examType: undefined,
  31. },
  32. org: {
  33. ...config.defaultOrg,
  34. },
  35. rememberPwd: false,
  36. cardPassword: '',
  37. cardNo: '',
  38. directedSchoolList: [],
  39. practiceSettings: {
  40. reviewMode: EnumReviewMode.AFTER_SUBMIT,
  41. autoNext: false
  42. }
  43. }),
  44. getters: {
  45. isLogin(state: UserStoreState): boolean {
  46. return !!state.accessToken && state.user !== null && Object.keys(state.user).length > 0;
  47. },
  48. userInfo(state: UserStoreState): UserInfo {
  49. return state.user || {} as UserInfo;
  50. },
  51. avatar(state: UserStoreState): string {
  52. return this.userInfo.avatar || defaultAvatar
  53. },
  54. nickName(state: UserStoreState): string {
  55. return this.userInfo.nickName || '游客'
  56. },
  57. anonymousPhoneNumber(state: UserStoreState): string {
  58. if (this.userInfo.phonenumber && this.userInfo.phonenumber.length === 11) {
  59. return this.userInfo.phonenumber.slice(0, 3) + '****' + this.userInfo.phonenumber.slice(7)
  60. }
  61. return '';
  62. },
  63. isVip(): boolean {
  64. if (!this.card) {
  65. return false;
  66. }
  67. const { outDate } = this.card;
  68. return !!this.userInfo.cardId && new Date(outDate) > new Date();
  69. },
  70. isExperienceVip(): boolean {
  71. if (!this.card) {
  72. return false;
  73. }
  74. const { outDate } = this.card;
  75. return this.card?.type === CardType.EXPERIENCE && new Date(outDate) > new Date();
  76. },
  77. vipInfo(state: UserStoreState): VipCardInfo {
  78. if (state.card) {
  79. return state.card;
  80. }
  81. return {} as VipCardInfo;
  82. },
  83. orgInfo(state: UserStoreState) {
  84. return state.org;
  85. },
  86. isStudent(): boolean {
  87. return this.userInfo.userType === EnumUserType.STUDENT;
  88. },
  89. isTeacher(): boolean {
  90. return this.userInfo.userType === EnumUserType.TEACHER;
  91. },
  92. isAgent(): boolean {
  93. return this.userInfo.userType === EnumUserType.AGENT;
  94. },
  95. isAuditor(): boolean {
  96. return false;
  97. return this.userInfo.accountType === 2;
  98. },
  99. hasDirectedSchool(): boolean {
  100. return this.directedSchoolList.length > 0;
  101. },
  102. /**
  103. * 需要完成信息完善的用户类型
  104. * @returns
  105. */
  106. needCompleteInfo(): boolean {
  107. return this.isTeacher || this.isAgent;
  108. },
  109. // 兼容游客时显示
  110. getLocation(): string {
  111. if (this.isLogin) {
  112. return this.user?.location || '';
  113. }
  114. return this.tempInfo?.location || '';
  115. },
  116. getExamType(): string {
  117. if (this.isLogin) {
  118. return this.user?.examType || '';
  119. }
  120. return this.tempInfo?.examType || '';
  121. }
  122. },
  123. actions: {
  124. setToken(token: string) {
  125. this.accessToken = token;
  126. },
  127. async login(token: string) {
  128. try {
  129. this.accessToken = token;
  130. await oldUserStore.SyncToken(token);
  131. const userInfo = await this.getUserInfo();;
  132. return Promise.resolve({
  133. success: true,
  134. userInfo: userInfo
  135. });
  136. } catch (error) {
  137. return Promise.resolve({
  138. success: false,
  139. userInfo: null
  140. });
  141. }
  142. },
  143. checkLogin({ askToLogin = true }: CheckLoginOptions = {}): Promise<boolean> {
  144. if (this.isLogin) {
  145. return Promise.resolve(true);
  146. }
  147. const { transferTo } = useTransferPage();
  148. return new Promise((resolve, reject) => {
  149. if (askToLogin) {
  150. tools.showModal({
  151. title: '提示',
  152. content: '当前未登录,是否前往登录?',
  153. confirmColor: themeColor
  154. }).then(confirm => {
  155. if (confirm) {
  156. transferTo('/pagesSystem/pages/login/login').then((res) => {
  157. resolve(res as boolean);
  158. }).catch(() => {
  159. resolve(false);
  160. });
  161. }
  162. });
  163. } else {
  164. transferTo('/pagesSystem/pages/login/login').then((res) => {
  165. resolve(res as boolean);
  166. }).catch(() => {
  167. resolve(false);
  168. });
  169. }
  170. });
  171. },
  172. checkVip() {
  173. return new Promise((resolve, reject) => {
  174. if (!this.isVip) {
  175. resolve(false);
  176. }
  177. resolve(true);
  178. });
  179. },
  180. checkInfoComplete() {
  181. return new Promise((resolve, reject) => {
  182. if (this.needCompleteInfo && (!this.userInfo.location || !this.userInfo.examType || !this.userInfo.endYear)) {
  183. const { transferTo } = useTransferPage();
  184. transferTo('/pagesSystem/pages/bind-teacher-profile/bind-teacher-profile').then(res => {
  185. resolve(res as boolean);
  186. }).catch(() => {
  187. resolve(false);
  188. });
  189. } else {
  190. resolve(true);
  191. }
  192. });
  193. },
  194. async getUserInfo() {
  195. const res = await getUserInfo();
  196. const { data, isDefaultModifyPwd, isPasswordExpired, card, org } = res;
  197. if (data) {
  198. this.user = data;
  199. if (card) {
  200. this.card = card;
  201. }
  202. this.isDefaultModifyPwd = isDefaultModifyPwd;
  203. this.isPasswordExpired = isPasswordExpired;
  204. }
  205. if (org) {
  206. this.org = org;
  207. }
  208. return data;
  209. },
  210. /**
  211. * 保存定向学校列表
  212. * @param list 定向学校列表
  213. */
  214. async saveDirectedSchoolList(list: Study.DirectedSchool[]) {
  215. await saveDirectedSchool(list);
  216. await this.getDirectedSchoolList();
  217. },
  218. /**
  219. * 获取定向学校列表
  220. */
  221. async getDirectedSchoolList() {
  222. const { data } = await getDirectedSchool();
  223. this.directedSchoolList = data || [];
  224. },
  225. setPracticeSettings(settings: Study.PracticeSettings) {
  226. this.practiceSettings = settings;
  227. },
  228. askLogout() {
  229. return new Promise((resolve, reject) => {
  230. uni.$ie.showModal({
  231. title: '提示',
  232. content: '确定退出登录吗?',
  233. confirmColor: themeColor
  234. }).then(confirm => {
  235. if (confirm) {
  236. this.logout();
  237. resolve(true);
  238. } else {
  239. resolve(false);
  240. }
  241. });
  242. });
  243. },
  244. rememberLoginInfo(remember: boolean, cardNo: string, cardPassword: string) {
  245. this.rememberPwd = remember;
  246. if (remember) {
  247. this.cardNo = cardNo;
  248. this.cardPassword = cardPassword;
  249. } else {
  250. this.cardNo = '';
  251. this.cardPassword = '';
  252. }
  253. },
  254. logout() {
  255. this.accessToken = null;
  256. this.user = null;
  257. this.isDefaultModifyPwd = false;
  258. this.isPasswordExpired = false;
  259. this.org = {
  260. ...config.defaultOrg,
  261. };
  262. oldUserStore.Logout();
  263. // const { transferTo } = useTransferPage();
  264. // setTimeout(() => {
  265. // transferTo('/pagesMain/pages/index/index', {
  266. // type: 'reLaunch'
  267. // })
  268. // }, 300);
  269. },
  270. },
  271. persist: {
  272. storage: {
  273. getItem: uni.getStorageSync,
  274. setItem: uni.setStorageSync,
  275. }
  276. }
  277. });