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,cAAc,CAAC,KAAK,EAAC;QAAC,OAAO,EAAC,MAAM,CAAC;QAAA,KAAK,EAAC,MAAM,CAAC;QAAA,SAAS,EAAC,MAAM,CAAC;QAAA,UAAU,EAAC,MAAM,CAAA;KAAC;IAsBrF,OAAO,CAAC,EAAC,MAAM,EAAE,KAAK,EAAC;;;KAAA;IAQ7B,MAAM,CAAC,UAAU,CAAC,QAAQ,KAAA;gBAOd,GAAG,CAAC,EAAE,MAAM;IAMxB,MAAM,CAAC,MAAM,KAAA;IAab,KAAK;IAWL,MAAM,CAAC,OAAO;IAkBd,MAAM,CAAC,KAAK;IAOZ,MAAM,CAAC,iBAAiB;WASX,MAAM,CAAC,EAAE,CAAC,EAAE,MAAI,GAAG,EAAE,YAAY,UAAO;WAaxC,YAAY,CAAC,YAAY,UAAO;WAgBhC,SAAS;WAMT,eAAe,CAAC,QAAQ,EAAC,MAAM;IAM5C,MAAM,CAAC,SAAS,IAAG,OAAO;CAQ3B","file":"User.d.ts","sourcesContent":["import LoginApi from '../api/LoginApi';\n\nimport {localStore} from 'mutants-microfx';\nimport {authKey,authKeys,prodProxyHost,proxyServer} from '../const/app';\nimport Channel from './Channel';\nimport {stores} from 'mutants-microfx';\nimport LogoutMV from '../middleware/LogoutMW';\nimport tools from '../util/tools';\nimport {destroyApp} from 'mutants-jsbridge';\nimport {runInAction} from 'mobx'\nimport {env} from 'mutants-microfx';\nimport _findIndex from 'lodash/findIndex'; \nconst {constant,platform,browser,os} = env;\nconst crypto = require('crypto');\nconst emptyAuth = {\n userId: '',\n orgId: '',\n accessToken: '',\n tplusToken: '',\n authorization: '',\n sid: '',\n OrganID:'',\n proxyserver: prodProxyHost,\n channel:{\n targetURL : window.localStorage.targetURL\n }\n}\nexport default class User {\n\n username:string;\n password:string;\n userId:string;\n orgId:string;\n orgName:string;\n accessToken:string;\n tplusToken:string;\n authorization:string;\n sid:string;\n channel:{targetURL:string,throughProxy:boolean};\n orgList:Array<{orgId:string,orgName:string,orgFullName:string}>;\n proxyserver:string;\n\n\n\n //认证用户名 密码\n static async authentication(username:string,password:string):Promise<User|undefined>{\n password = User.pwdEncrypt(password);\n let auth = new User({username,password});\n let authResult = await LoginApi.authentication(auth);\n if(authResult){\n const authObj = auth.fromJS(authResult);\n return authObj;\n }\n }\n //认证用户名 密码 直接通过加密的密码进行登录的\n static async authentication2(username:string,password:string):Promise<User|undefined>{\n // password = User.pwdEncrypt(password);\n let auth = new User({username,password});\n let authResult = await LoginApi.authentication(auth);\n if(authResult){\n const authObj = auth.fromJS(authResult);\n return authObj;\n }\n }\n\n //修改密码\n static async updatePwd(oldPassword:string,newPassword:string){\n oldPassword = User.pwdEncrypt(oldPassword);\n newPassword = User.pwdEncrypt(newPassword);\n let updateResult = await LoginApi.updatePwd(oldPassword,newPassword);\n await runInAction(()=>{\n stores.user.options.set(\"password\",newPassword)\n })\n \n return updateResult;\n }\n\n async initChannel(orgId:string){\n if(this.orgList){\n const checkOrg = this.orgList.some((org)=>{\n if(org.orgId === orgId){\n this.orgName = org.orgFullName;\n return true;\n }\n });\n if(!checkOrg){\n throw new Error(`orgId ${orgId} is out of range user orgList`);\n }\n \n }\n this.orgId = orgId;\n this.cache(); //初始化通道\n this.channel = await Channel.getChannel();\n //执行前端网关策略\n this.executeFEGatewayStrategy();\n //将会话信息缓存\n this.channel = await Channel.getChannel();\n this.cache();\n }\n\n //执行代理\n async executeFEGatewayStrategy(){\n try{\n const {data} = await LoginApi.getApiConfig();\n //执行代理灰度策略\n await this.executeProxyGrayStrategy(data);\n //执行云代理灰度策略\n await this.executeCloudproxyGrayStrategy(data);\n }catch(err){\n console.error('err=======',err);\n }\n \n }\n \n /**\n * 执行代理灰度策略\n * @param {*} data 配置信息\n */\n async executeProxyGrayStrategy(data){\n const {gray_filter} = data;\n const {gray_release = '',gray_domain = '',org_id=[],user_id=[],domain=[]} = gray_filter;\n if(!!gray_domain){\n switch (gray_release) {\n //全部灰度\n case 'all':\n this.proxyserver = gray_domain;\n break;\n \n //部分灰度\n case 'part':\n const isGray = _findIndex(org_id, orgId => this.orgId == orgId) > -1\n || _findIndex(user_id, userId => this.userId == userId) > -1\n || _findIndex(domain, dom => dom == this.channel.targetURL) > -1;\n \n if(isGray){\n this.proxyserver = gray_domain;\n }\n break;\n default:\n break;\n }\n }\n }\n \n \n /**\n * 执行云代理灰度策略\n * @param {*} data 配置信息\n */\n async executeCloudproxyGrayStrategy(data){\n const {cloudproxy_filter} = data;\n const {cloudproxy_domain = '',org_id=[],user_id=[],domain=[]} = cloudproxy_filter;\n if(!!cloudproxy_domain && !!this.channel.targetURL && this.channel.targetURL.indexOf('.chanjet.com')==-1){\n const isProxyserver = _findIndex(org_id, orgId => this.orgId == orgId) > -1\n || _findIndex(user_id, userId => this.userId == userId) > -1\n || _findIndex(domain, dom => dom == this.channel.targetURL) > -1;\n if(isProxyserver){\n this.channel.targetURL = proxyServer;\n }\n }\n \n }\n \n \n\n \n\n setChannel(targetURL:string){\n this.channel = Channel.setChannel(targetURL)\n this.cache();\n }\n\n //登录T+\n async loginTplus(param:{clientName? :string,desc? :string}){\n if(tools.isApp()&&!!window.localStorage.tplusToken){\n //移动端做共享token 基于性能优化考虑 复用token 其余登录绕过此过程\n //从ls中获取登录token信息,绕过此步骤\n this.tplusToken = window.localStorage.tplusToken;\n this.authorization = `token ${this.tplusToken}`;\n if(!!window.localStorage.sid){\n this.sid = window.window.localStorage.sid;\n }\n this.cache();\n stores.user.login(this);\n return;\n }\n let result = await LoginApi.getTplusToken(param);\n if(result.indexOf(';')>-1){\n let data = result.split(';');\n this.tplusToken = data[0];\n this.sid = data[1];\n }else{\n this.tplusToken = result;\n }\n this.authorization = `token ${this.tplusToken}`;\n this.cache();\n stores.user.login(this);\n }\n\n //切换账套后更新User信息\n async changeUserInfo(param:{orgName:string,orgId:string,targetURL:string,tplusToken:string}){\n if(param.tplusToken.indexOf(';')>-1){\n let data = param.tplusToken.split(';');\n this.tplusToken = data[0];\n this.sid = data[1];\n }else{\n this.tplusToken = param.tplusToken;\n }\n this.authorization = `token ${this.tplusToken}`;\n this.orgId = param.orgId;\n this.orgName = param.orgName;\n this.setChannel(param.targetURL);\n if(!!window.localStorage.sid){\n this.sid = window.window.localStorage.sid;\n }\n this.cache();\n stores.user.login(this);\n}\n\n \n\n // 二维码登录,只传入userid orgid\n async QRLogin({userId, orgId}) {\n this.orgId = orgId;\n this.userId = userId;\n this.accessToken = '***';\n this.cache();\n await this.loginTplus({clientName: '扫码登录', desc: '扫码登录'});\n }\n\n static pwdEncrypt(password){\n let md5 = crypto.createHash('md5');\n md5.update(password);\n password = md5.digest('hex');\n return password;\n }\n\n constructor(obj? :object){\n if(obj){\n Object.assign(this,obj);\n }\n }\n\n fromJS(result){\n //获取企业信息\n var orgListInfo = result.orgListInfo;\n const orgLength = orgListInfo.orgList.length;\n if (orgLength > 0) {\n this.userId = result.user_id;\n this.accessToken = result.access_token;\n this.orgList = orgListInfo.orgList\n }\n return this;\n }\n\n //缓存数据\n cache(){\n localStore.set(authKey,this);\n authKeys.forEach((authKey)=>{\n if(!tools.isNull(this[authKey])){\n localStore.set(authKey,this[authKey]);\n }\n });\n }\n\n\n //恢复数据\n static restore(){\n let auth = localStore.get(authKey);\n if(auth == null || auth == undefined){\n auth = emptyAuth;\n }\n return auth;\n\n // const accessToken = localStore.get('accessToken');\n // if(!tools.isNull(accessToken)){\n // let cacheObj = {};\n // authKeys.forEach((item)=>{\n // cacheObj[item] = localStore.get(item);\n // });\n // return cacheObj; \n // }\n // return emptyAuth;\n }\n\n static clear(){\n if ((stores.posLoginStore || {}).savePosCode) {\n stores.posLoginStore.savePosCode(false);\n }\n User.clearLocalSession();\n }\n\n static clearLocalSession(){\n localStore.remove(authKey);\n authKeys.forEach((item)=>{\n localStore.remove(item);\n });\n Channel.clear();\n }\n\n //注销对象\n static async logout(fn? :()=>any, isdestroyApp = true) {\n await User.posLogout();//pos注销\n LogoutMV.execute();\n User.clear();\n fn && fn();\n // 用户退出不用调用history,门户可以通过用户状态判断显示登录页面\n if(stores.user){\n stores.user.logout();\n }\n await User.mobileLogout(isdestroyApp);//移动端注销\n }\n\n //移动端注销\n static async mobileLogout(isdestroyApp = true){\n //移动端的场景\n //轻应用 与 微信\n if(tools.isApp() || platform === constant.platform.weixin ){\n //清空store中的用户信息\n if(stores.user){\n stores.user.logout();\n }\n if(isdestroyApp){\n destroyApp();\n }\n \n }\n }\n\n //pos端注销\n static async posLogout(){\n const {posIsLogin} = stores.posLoginStore || {};\n posIsLogin && await LoginApi.logout();\n }\n\n //通过一次性码登录CIA\n static async loginByAuthcode(authCode:string){\n let authResult = await LoginApi.authenticationByCode(authCode);\n return authResult;\n }\n\n //判断用户是否登录\n static isLogined():boolean{\n const user = User.restore();\n if(tools.isNull(user.accessToken)||tools.isNull(user.userId)||tools.isNull(user.orgId)){\n return false;\n }\n return true;\n }\n\n}\n"]}