import { Action, getModule, Module, Mutation, VuexModule, MutationAction } from 'vuex-module-decorators'; import { Area } from '@/pages/login/area/model'; import { Lang } from '@/i18n'; import request from '@/utils/request'; import { url } from '@/utils/url'; import { stringify } from 'query-string'; import store from '@/store'; import { page } from '@/utils/page'; import { result } from 'lodash'; export interface User { code?: string; msg?: string; userId?: string; lang?: Lang; loginName?: string; userName?: string; factoryCode?: string; regionCode?: string; list?: Array; } @Module({ dynamic: true, store, namespaced: true, name: 'session', }) class SessionService extends VuexModule { /** * 当前登录用户 */ user: User | null = null; Version: string = ''; /** * 当前工作区 */ area: Area | null = null; get isUserLoggedIn(): boolean { return this.user !== null; } /** * 当前用户ID */ get userId(): any { if (this.user === null) { return null; } else { return this.user.userId; } } /** * 当前选择语言 */ get lang(): any { if (this.user === null) { return null; } else { return this.user.lang; } } /** * 当前登录名 */ get loginName(): any { if (this.user === null) { return null; } else { return this.user.loginName; } } /** * 当前工厂代码 */ get factoryCode(): any { if (this.user === null) { return null; } else { return this.user.factoryCode; } } /** * 当前区域代码 */ get regionCode(): string | null { if (this.area === null) { return null; } else { return this.area.regionCode; } } /** * 当前工作区代码 */ get workareaCode(): string | null { if (this.area === null) { return null; } else { return this.area.workareaCode; } } /** * 当前仓库代码 */ get warehouseCode(): string | null { if (this.area === null) { return null; } else { return this.area.warehouseCode; } } // 用户 getter get getUser(): User { if (this.user === null) { throw new Error('当前无登录用户'); } else { return this.user; } } // 登录 @Action({ rawError: true }) async login({ username, password, lang }: { username: string; password: string; lang: Lang }): Promise { return request.post( url.wmspda.system.login, stringify({ userName: username, passWord: password, lang: lang == 'en' ? 'en_US' : 'zh_CN', }), ); } // 登出 @Mutation logout(): void { // logout this.user = null; this.area = null; uni.reLaunch({ url: page.login.login }); } // 保存用户信息 @Mutation setUser(user: User): void { this.user = user; } // 保存工作区信息 @Mutation setArea(area: Area): void { this.area = area; } //查询版本号 @MutationAction async QueryVersion() { let res: any = await request.get('http://seamom.haier.net/sgNginxDownload/Debug/setup.json'); let Version = res.version; return { Version }; } } export const session = getModule(SessionService);