1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import {defineStore} from "pinia";
- import storage from "@/utils/storage";
- import {useConfigStore} from "@/store/useConfigStore";
- import {getInfo, login} from "@/api/common";
- import {showToast} from "@/utils/app";
- export const useUserStore = defineStore({
- id: 'user',
- state: () => ({
- token: null,
- userInfo: {},
- userName: '',
- }),
- getters: {},
- actions: {
- async _init() {
- try {
- this.checkLogin()
- if (this.token) {
- const {list} = await getInfo()
- this.userInfo = list
- this.userName = list.mac
- const configStore = useConfigStore()
- configStore.mType = list.type
- configStore.all = list.config
- } else {
- }
- } catch (e) {
- }
- },
- checkLogin() {
- let token = storage.getKey('LOGIN_TEACHER_TOKEN')
- if (token) {
- this.token = token
- }
- },
- async login(loginForm) {
- return new Promise(async (resolve, reject) => {
- const res = await login({
- username: loginForm.mac,
- password: loginForm.pwd,
- })
- if (res.code == 1 && res.data.token) {
- this.setToken(res.data)
- resolve(res.data)
- } else {
- showToast(res.msg)
- reject(res.msg)
- }
- })
- },
- logout() {
- this.token = null
- this.userInfo = {}
- const configStore = useConfigStore()
- configStore.clear()
- storage.remove("LOGIN_TEACHER_TOKEN")
- },
- setToken(data) {
- this.token = data.token
- this.userInfo = data.info
- storage.setKey('LOGIN_TEACHER_TOKEN', data.token)
- }
- // getMachineInfo() {
- // this.mac = '123'
- // },
- }
- })
|