useUserStore.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import {defineStore} from "pinia";
  2. import storage from "@/utils/storage";
  3. import {useConfigStore} from "@/store/useConfigStore";
  4. import {getInfo, login} from "@/api/common";
  5. import {showToast} from "@/utils/app";
  6. export const useUserStore = defineStore({
  7. id: 'user',
  8. state: () => ({
  9. token: null,
  10. userInfo: {},
  11. userName: '',
  12. }),
  13. getters: {},
  14. actions: {
  15. async _init() {
  16. try {
  17. this.checkLogin()
  18. if (this.token) {
  19. const {list} = await getInfo()
  20. this.userInfo = list
  21. this.userName = list.mac
  22. const configStore = useConfigStore()
  23. configStore.mType = list.type
  24. configStore.all = list.config
  25. } else {
  26. }
  27. } catch (e) {
  28. }
  29. },
  30. checkLogin() {
  31. let token = storage.getKey('LOGIN_TEACHER_TOKEN')
  32. if (token) {
  33. this.token = token
  34. }
  35. },
  36. async login(loginForm) {
  37. return new Promise(async (resolve, reject) => {
  38. const res = await login({
  39. username: loginForm.mac,
  40. password: loginForm.pwd,
  41. })
  42. if (res.code == 1 && res.data.token) {
  43. this.setToken(res.data)
  44. resolve(res.data)
  45. } else {
  46. showToast(res.msg)
  47. reject(res.msg)
  48. }
  49. })
  50. },
  51. logout() {
  52. this.token = null
  53. this.userInfo = {}
  54. const configStore = useConfigStore()
  55. configStore.clear()
  56. storage.remove("LOGIN_TEACHER_TOKEN")
  57. },
  58. setToken(data) {
  59. this.token = data.token
  60. this.userInfo = data.info
  61. storage.setKey('LOGIN_TEACHER_TOKEN', data.token)
  62. }
  63. // getMachineInfo() {
  64. // this.mac = '123'
  65. // },
  66. }
  67. })