You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
3.4 KiB
TypeScript

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<Area>;
}
@Module({
dynamic: true,
store,
namespaced: true,
name: 'session',
})
class SessionService extends VuexModule {
/**
*
*/
user: User | null = null;
Version: string = '';
url: 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<User> {
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;
}
4 years ago
//查询版本号
@MutationAction
async QueryVersion() {
let res: any = await request.get('http://seamom.haier.net/sgNginxDownload/Debug/setup.json');
4 years ago
let Version = res.version;
let url = res.url + '/' + res.file;
console.log("url..........", url);
return { Version, url };
}
//直接下载
@MutationAction
async xiazai() {
let url: any = await request.get(this.url);
return { url };
4 years ago
}
}
export const session = getModule(SessionService);