userStore.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 { 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;
  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. return !!this.userInfo.cardId;
  65. },
  66. vipInfo(state: UserStoreState): VipCardInfo {
  67. if (state.card) {
  68. return state.card;
  69. }
  70. return {} as VipCardInfo;
  71. },
  72. orgInfo(state: UserStoreState) {
  73. return state.org;
  74. },
  75. isStudent(): boolean {
  76. return this.userInfo.userType === EnumUserType.STUDENT;
  77. },
  78. isTeacher(): boolean {
  79. return this.userInfo.userType === EnumUserType.TEACHER;
  80. },
  81. isAgent(): boolean {
  82. return this.userInfo.userType === EnumUserType.AGENT;
  83. },
  84. isAuditor(): boolean {
  85. return false;
  86. return this.userInfo.accountType === 2;
  87. },
  88. hasDirectedSchool(): boolean {
  89. return this.directedSchoolList.length > 0;
  90. },
  91. /**
  92. * 需要完成信息完善的用户类型
  93. * @returns
  94. */
  95. needCompleteInfo(): boolean {
  96. return this.isTeacher || this.isAgent;
  97. },
  98. // 兼容游客时显示
  99. getLocation(): string {
  100. if (this.isLogin) {
  101. return this.user?.location || '';
  102. }
  103. return this.tempInfo?.location || '';
  104. },
  105. getExamType(): string {
  106. if (this.isLogin) {
  107. return this.user?.examType || '';
  108. }
  109. return this.tempInfo?.examType || '';
  110. }
  111. },
  112. actions: {
  113. setToken(token: string) {
  114. this.accessToken = token;
  115. },
  116. async login(token: string) {
  117. try {
  118. this.accessToken = token;
  119. await oldUserStore.SyncToken(token);
  120. const userInfo = await this.getUserInfo();;
  121. return Promise.resolve({
  122. success: true,
  123. userInfo: userInfo
  124. });
  125. } catch (error) {
  126. return Promise.resolve({
  127. success: false,
  128. userInfo: null
  129. });
  130. }
  131. },
  132. checkLogin({ askToLogin = true }: CheckLoginOptions = {}): Promise<boolean> {
  133. if (this.isLogin) {
  134. return Promise.resolve(true);
  135. }
  136. const { transferTo } = useTransferPage();
  137. return new Promise((resolve, reject) => {
  138. if (askToLogin) {
  139. tools.showModal({
  140. title: '提示',
  141. content: '当前未登录,是否前往登录?',
  142. confirmColor: themeColor
  143. }).then(confirm => {
  144. if (confirm) {
  145. transferTo('/pagesSystem/pages/login/login').then((res) => {
  146. resolve(res as boolean);
  147. }).catch(() => {
  148. resolve(false);
  149. });
  150. }
  151. });
  152. } else {
  153. transferTo('/pagesSystem/pages/login/login').then((res) => {
  154. resolve(res as boolean);
  155. }).catch(() => {
  156. resolve(false);
  157. });
  158. }
  159. });
  160. },
  161. checkVip() {
  162. return new Promise((resolve, reject) => {
  163. if (!this.isVip) {
  164. resolve(false);
  165. }
  166. resolve(true);
  167. });
  168. },
  169. checkInfoComplete() {
  170. return new Promise((resolve, reject) => {
  171. if (this.needCompleteInfo && (!this.userInfo.location || !this.userInfo.examType || !this.userInfo.endYear)) {
  172. const { transferTo } = useTransferPage();
  173. transferTo('/pagesSystem/pages/bind-teacher-profile/bind-teacher-profile').then(res => {
  174. resolve(res as boolean);
  175. }).catch(() => {
  176. resolve(false);
  177. });
  178. } else {
  179. resolve(true);
  180. }
  181. });
  182. },
  183. async getUserInfo() {
  184. const res = await getUserInfo();
  185. const { data, isDefaultModifyPwd, isPasswordExpired, card, org } = res;
  186. if (data) {
  187. this.user = data;
  188. if (card) {
  189. this.card = card;
  190. }
  191. this.isDefaultModifyPwd = isDefaultModifyPwd;
  192. this.isPasswordExpired = isPasswordExpired;
  193. }
  194. if (org) {
  195. this.org = org;
  196. }
  197. return data;
  198. },
  199. /**
  200. * 保存定向学校列表
  201. * @param list 定向学校列表
  202. */
  203. async saveDirectedSchoolList(list: Study.DirectedSchool[]) {
  204. await saveDirectedSchool(list);
  205. await this.getDirectedSchoolList();
  206. },
  207. /**
  208. * 获取定向学校列表
  209. */
  210. async getDirectedSchoolList() {
  211. const { data } = await getDirectedSchool();
  212. this.directedSchoolList = data || [];
  213. },
  214. setPracticeSettings(settings: Study.PracticeSettings) {
  215. this.practiceSettings = settings;
  216. },
  217. askLogout() {
  218. return new Promise((resolve, reject) => {
  219. uni.$ie.showModal({
  220. title: '提示',
  221. content: '确定退出登录吗?',
  222. confirmColor: themeColor
  223. }).then(confirm => {
  224. if (confirm) {
  225. this.logout();
  226. resolve(true);
  227. } else {
  228. resolve(false);
  229. }
  230. });
  231. });
  232. },
  233. rememberLoginInfo(remember: boolean, cardNo: string, cardPassword: string) {
  234. this.rememberPwd = remember;
  235. if (remember) {
  236. this.cardNo = cardNo;
  237. this.cardPassword = cardPassword;
  238. } else {
  239. this.cardNo = '';
  240. this.cardPassword = '';
  241. }
  242. },
  243. logout() {
  244. this.accessToken = null;
  245. this.user = null;
  246. this.isDefaultModifyPwd = false;
  247. this.isPasswordExpired = false;
  248. this.org = {
  249. ...config.defaultOrg,
  250. };
  251. oldUserStore.Logout();
  252. // const { transferTo } = useTransferPage();
  253. // setTimeout(() => {
  254. // transferTo('/pagesMain/pages/index/index', {
  255. // type: 'reLaunch'
  256. // })
  257. // }, 300);
  258. },
  259. },
  260. persist: {
  261. storage: {
  262. getItem: uni.getStorageSync,
  263. setItem: uni.setStorageSync,
  264. }
  265. }
  266. });