|
|
@ -1,11 +1,12 @@
|
|
|
|
import { isUrl } from "@/microhost/util"
|
|
|
|
import { isUrl } from "@/microhost/util"
|
|
|
|
import { CastMode, CacheUserData } from "../params"
|
|
|
|
import { CastMode, CacheUserData } from "../params"
|
|
|
|
import {defaultSetting as cfg} from "@/config/default"
|
|
|
|
import { defaultSetting as cfg } from "@/config/default"
|
|
|
|
import util from "@/microhost/common"
|
|
|
|
import util from "@/microhost/common"
|
|
|
|
import { SystemData } from "@/microhost/models/system"
|
|
|
|
import { SystemData } from "@/microhost/models/system"
|
|
|
|
import MicroApps from "../apps"
|
|
|
|
import MicroApps from "../apps"
|
|
|
|
import { Log } from "./logger"
|
|
|
|
import { Log } from "./logger"
|
|
|
|
import {history} from "umi"
|
|
|
|
import { history } from "umi"
|
|
|
|
|
|
|
|
import requestaxios from 'axios';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 运行时用户
|
|
|
|
* 运行时用户
|
|
|
@ -13,152 +14,156 @@ import {history} from "umi"
|
|
|
|
* @date 2023-03-27
|
|
|
|
* @date 2023-03-27
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
export default class RuntimeUser {
|
|
|
|
export default class RuntimeUser {
|
|
|
|
protected userData?: CacheUserData
|
|
|
|
protected userData?: CacheUserData
|
|
|
|
protected cacheName: string = cfg.userInfo
|
|
|
|
protected cacheName: string = cfg.userInfo
|
|
|
|
protected timeoutHours: number = 12
|
|
|
|
protected timeoutHours: number = 12
|
|
|
|
protected model: SystemData = new SystemData()
|
|
|
|
protected model: SystemData = new SystemData()
|
|
|
|
|
|
|
|
|
|
|
|
get timeout() {
|
|
|
|
get timeout() {
|
|
|
|
return this.timeoutHours
|
|
|
|
return this.timeoutHours
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
set timeout(hours: number){
|
|
|
|
set timeout(hours: number) {
|
|
|
|
this.timeoutHours = hours < 1 ? 1 : hours
|
|
|
|
this.timeoutHours = hours < 1 ? 1 : hours
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
get data() {
|
|
|
|
get data() {
|
|
|
|
this.reload()
|
|
|
|
this.reload()
|
|
|
|
return this.userData
|
|
|
|
return this.userData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
set data(d: CacheUserData | undefined){
|
|
|
|
set data(d: CacheUserData | undefined) {
|
|
|
|
if(d){
|
|
|
|
if (d) {
|
|
|
|
d.touchTime = new Date().getTime()
|
|
|
|
d.touchTime = new Date().getTime()
|
|
|
|
util.saveCache(cfg.userInfo, d)
|
|
|
|
util.saveCache(cfg.userInfo, d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
else {
|
|
|
|
util.removeCache(cfg.userInfo)
|
|
|
|
util.removeCache(cfg.userInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.userData = d
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
get loginId(){
|
|
|
|
|
|
|
|
return this.data ? this.data.loginId : undefined
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public reload = () => {
|
|
|
|
|
|
|
|
let cache = util.loadCache(this.cacheName)
|
|
|
|
|
|
|
|
this.data = !cache ? undefined : cache
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public isTimeout = () => {
|
|
|
|
|
|
|
|
if(!this.data) return true
|
|
|
|
|
|
|
|
let touchTime = this.data.touchTime
|
|
|
|
|
|
|
|
if(!touchTime) return true
|
|
|
|
|
|
|
|
if(new Date().getTime() - touchTime >= this.timeout * 3600 * 1000) return true
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public checkAllowed = async (redirect?: string) => {
|
|
|
|
|
|
|
|
if(!this.isAllowed() || !this.isLogin()){
|
|
|
|
|
|
|
|
let quick = await this.quickLogin()
|
|
|
|
|
|
|
|
if(!quick){
|
|
|
|
|
|
|
|
this.logout(false)
|
|
|
|
|
|
|
|
let url = `${cfg.url.login}${redirect && isUrl(redirect) && !redirect?.includes(cfg.url.login) ? ("?redirect=" + encodeURIComponent(redirect)) : ""}`
|
|
|
|
|
|
|
|
location.href = url
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else{
|
|
|
|
|
|
|
|
location.reload()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected quickLogin = async (uid?: any, pass?: any) => {
|
|
|
|
|
|
|
|
let loginId: any = uid || history.location.query?.userId
|
|
|
|
|
|
|
|
let password: any = pass || history.location.query?.password
|
|
|
|
|
|
|
|
// console.log("querys", history.location.query, loginId, password)
|
|
|
|
|
|
|
|
// debugger
|
|
|
|
|
|
|
|
if(loginId){
|
|
|
|
|
|
|
|
password = password || cfg.quickLogin.defpass
|
|
|
|
|
|
|
|
let result = await this.doLogin(loginId, password)
|
|
|
|
|
|
|
|
return result === true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public isAllowed = () => {
|
|
|
|
|
|
|
|
let data = util.loadCookie(cfg.allowKey)
|
|
|
|
|
|
|
|
return data == "1"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public setAllowed = (allowed: boolean) => {
|
|
|
|
|
|
|
|
if(allowed){
|
|
|
|
|
|
|
|
util.saveCookie(cfg.allowKey, 1, 0)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else{
|
|
|
|
|
|
|
|
util.removeCookie(cfg.allowKey)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public isLogin = () => {
|
|
|
|
|
|
|
|
this.reload()
|
|
|
|
|
|
|
|
if(!this.userData) return false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public login = (loginId: string, accessToken: string, refreshToken: string, nickname?: string, orgname?: string, clientId?: string) => {
|
|
|
|
|
|
|
|
let d: CacheUserData = {
|
|
|
|
|
|
|
|
loginId: loginId,
|
|
|
|
|
|
|
|
clientId: clientId,
|
|
|
|
|
|
|
|
refresh_token: refreshToken,
|
|
|
|
|
|
|
|
access_token: accessToken,
|
|
|
|
|
|
|
|
nickname: nickname,
|
|
|
|
|
|
|
|
org: orgname,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//console.log("11")
|
|
|
|
|
|
|
|
this.data = d
|
|
|
|
|
|
|
|
this.setAllowed(true)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public logout = (jump?: boolean) => {
|
|
|
|
|
|
|
|
this.data = undefined
|
|
|
|
|
|
|
|
util.removeCache(this.cacheName)
|
|
|
|
|
|
|
|
this.setAllowed(false)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(jump){
|
|
|
|
|
|
|
|
location.href = cfg.url.login
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public doLogin = async (loginId: string, password: any) : Promise<string | true> => {
|
|
|
|
|
|
|
|
//debugger
|
|
|
|
|
|
|
|
let result: any = await this.model.login(loginId, password)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!result){
|
|
|
|
|
|
|
|
await MicroApps.GetRuntimeLogger().failed(Log.Login)
|
|
|
|
|
|
|
|
return "登录出现错误"
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(!result.success || result.success == 500){
|
|
|
|
this.userData = d
|
|
|
|
await MicroApps.GetRuntimeLogger().failed(Log.Login)
|
|
|
|
}
|
|
|
|
return result.error ? result.error : (result.msg ? result.msg : "登陆失败!")
|
|
|
|
|
|
|
|
|
|
|
|
get loginId() {
|
|
|
|
|
|
|
|
return this.data ? this.data.loginId : undefined
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public reload = () => {
|
|
|
|
|
|
|
|
let cache = util.loadCache(this.cacheName)
|
|
|
|
|
|
|
|
this.data = !cache ? undefined : cache
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public isTimeout = () => {
|
|
|
|
|
|
|
|
if (!this.data) return true
|
|
|
|
|
|
|
|
let touchTime = this.data.touchTime
|
|
|
|
|
|
|
|
if (!touchTime) return true
|
|
|
|
|
|
|
|
if (new Date().getTime() - touchTime >= this.timeout * 3600 * 1000) return true
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public checkAllowed = async (redirect?: string) => {
|
|
|
|
|
|
|
|
if (!this.isAllowed() || !this.isLogin()) {
|
|
|
|
|
|
|
|
let quick = await this.quickLogin()
|
|
|
|
|
|
|
|
if (!quick) {
|
|
|
|
|
|
|
|
this.logout(false)
|
|
|
|
|
|
|
|
let url = `${cfg.url.login}${redirect && isUrl(redirect) && !redirect?.includes(cfg.url.login) ? ("?redirect=" + encodeURIComponent(redirect)) : ""}`
|
|
|
|
|
|
|
|
location.href = url
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
location.reload()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected quickLogin = async (uid?: any, pass?: any) => {
|
|
|
|
|
|
|
|
let loginId: any = uid || history.location.query?.userId
|
|
|
|
|
|
|
|
let password: any = pass || history.location.query?.password
|
|
|
|
|
|
|
|
// console.log("querys", history.location.query, loginId, password)
|
|
|
|
|
|
|
|
// debugger
|
|
|
|
|
|
|
|
if (loginId) {
|
|
|
|
|
|
|
|
password = password || cfg.quickLogin.defpass
|
|
|
|
|
|
|
|
let result = await this.doLogin(loginId, password)
|
|
|
|
|
|
|
|
return result === true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public isAllowed = () => {
|
|
|
|
|
|
|
|
let data = util.loadCookie(cfg.allowKey)
|
|
|
|
|
|
|
|
return data == "1"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public setAllowed = (allowed: boolean) => {
|
|
|
|
|
|
|
|
if (allowed) {
|
|
|
|
|
|
|
|
util.saveCookie(cfg.allowKey, 1, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
util.removeCookie(cfg.allowKey)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public isLogin = () => {
|
|
|
|
|
|
|
|
this.reload()
|
|
|
|
|
|
|
|
if (!this.userData) return false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public login = (loginId: string, accessToken: string, refreshToken: string, nickname?: string, orgname?: string, clientId?: string) => {
|
|
|
|
|
|
|
|
let d: CacheUserData = {
|
|
|
|
|
|
|
|
loginId: loginId,
|
|
|
|
|
|
|
|
clientId: clientId,
|
|
|
|
|
|
|
|
refresh_token: refreshToken,
|
|
|
|
|
|
|
|
access_token: accessToken,
|
|
|
|
|
|
|
|
nickname: nickname,
|
|
|
|
|
|
|
|
org: orgname,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//console.log("11")
|
|
|
|
|
|
|
|
this.data = d
|
|
|
|
|
|
|
|
this.setAllowed(true)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public logout = (jump?: boolean) => {
|
|
|
|
|
|
|
|
this.data = undefined
|
|
|
|
|
|
|
|
util.removeCache(this.cacheName)
|
|
|
|
|
|
|
|
this.setAllowed(false)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (jump) {
|
|
|
|
|
|
|
|
location.href = cfg.url.login
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let userData = await this.model.userData(loginId)
|
|
|
|
public doLogin = async (loginId: string, password: any): Promise<string | true> => {
|
|
|
|
|
|
|
|
//debugger
|
|
|
|
|
|
|
|
let result: any = await this.model.login(loginId, password)
|
|
|
|
|
|
|
|
|
|
|
|
this.login(loginId
|
|
|
|
if (!result) {
|
|
|
|
, result.access_token
|
|
|
|
await MicroApps.GetRuntimeLogger().failed(Log.Login)
|
|
|
|
, result.refresh_token
|
|
|
|
return "登录出现错误"
|
|
|
|
, userData?.Name
|
|
|
|
}
|
|
|
|
, userData?.OrganName)
|
|
|
|
|
|
|
|
await MicroApps.GetRuntimeLogger().success(Log.Login, loginId)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
if (!result.success || result.success == 500) {
|
|
|
|
}
|
|
|
|
await MicroApps.GetRuntimeLogger().failed(Log.Login)
|
|
|
|
|
|
|
|
return result.error ? result.error : (result.msg ? result.msg : "登陆失败!")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let userData = await this.model.userData(loginId)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.login(loginId
|
|
|
|
|
|
|
|
, result.access_token
|
|
|
|
|
|
|
|
, result.refresh_token
|
|
|
|
|
|
|
|
, userData?.Name
|
|
|
|
|
|
|
|
, userData?.OrganName)
|
|
|
|
|
|
|
|
await MicroApps.GetRuntimeLogger().success(Log.Login, loginId)
|
|
|
|
|
|
|
|
let obj = {
|
|
|
|
|
|
|
|
OperateUser: loginId || "未登录",
|
|
|
|
|
|
|
|
Operate: `${loginId}登陆了工艺数据管理子系统`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
await requestaxios.post('/Personnel/Personnelapi/LogRecording', obj)
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|