tplus-api
Version:
tplus api invoke
239 lines (213 loc) • 7.14 kB
text/typescript
import axios from 'axios';
import Auth from '../model/User';
import _isEmpty from 'lodash/isEmpty';
import _trim from 'lodash/trim';
import querystring from 'querystring';
import tplusApi from './TplusApi';
import UIConfirm from '../components/UIConfirm';
import {clientId,clientSecret,cspAS,tplusWebAppApiHost,ciaHost,cspAk,prodProxyHost,proxyServer} from '../const/app';
import User from '../model/User';
import {env} from 'mutants-microfx';
const {constant,platform} = env;
const QS = require('qs');
export default class LoginApi{
//用户登录验证 获取企业列表信息
static async authentication({username,password}){
let ajax = axios.create({
baseURL: ciaHost
});
const param = {
'auth_username': username,
'password': password,
'needOrgLists': '1',
'client_id': clientId,
'client_secret': clientSecret,
};
const response = await ajax.post('/internal_api/client_authentication_with_userInfo', querystring.stringify(param));
var result = response.data;
var errorInfo = '';
if (result.auth_result == 'true') {
return result;
}else{
return undefined;
}
}
//用户修改密码操作
static async updatePwd(oldPassword:string,newPassword:string){
const user = User.restore();
const access_token = user.accessToken;
const appKey = atob(cspAk);
const passwordLevel = 1;
let ajax = axios.create({
baseURL: ciaHost
});
const passwordInfo = JSON.stringify({
oldPassword,
newPassword,
passwordLevel
});
const param = {
appKey,
access_token,
passwordInfo
};
const response = await ajax.post('/api/v1/user/password', querystring.stringify(param));
return response.data;
}
static async authenticationByCode(code:string){
let ajax = axios.create({
baseURL: ciaHost
});
let param,response;
if(platform === constant.platform.chanjet){
param = QS.stringify({
client_id: clientId,
client_secret:clientSecret,
auth_code: code
});
response = await ajax.post('/internal_api/codeForAccessTokenForClient', param);
}else if(platform === constant.platform.yonyou){
param = QS.stringify({
client_id: clientId,
client_secret:clientSecret,
// appKey: atob(cspAk),
// appSecret: atob(cspAS),
code
});
response = await ajax.post('/special_api/v1/user/getTokenByYonyouCode', param);
}
const d = response.data;
if(d.hasOwnProperty('error') && d.hasOwnProperty('error_description') && !_isEmpty(_trim(d.error_description))){
return Promise.reject(d.error_description);
}else{
if(_isEmpty(_trim(d.user_id)) || _isEmpty(_trim(d.access_token))){
return Promise.reject('获取用户信息错误');
}else{
return Promise.resolve(d);
}
}
}
//获取云端的服务器地址
static async getTargetURL(){
var ajax = axios.create({
baseURL: tplusWebAppApiHost
});
const resp = await ajax.post('/serverUrl');
return resp.data;
}
//获取云端的服务器地址
static async getTargetURLByOrgID(targetOrgID:string){
var ajax = axios.create({
baseURL: tplusWebAppApiHost
});
const resp = await ajax.post('/serverUrl',{targetOrgID});
return resp.data;
}
//获取T+的token
static async checkTplusToken(orgId:string,serverUrl:any):Promise<string>{
let checkThroughProxy = false;
if(serverUrl.indexOf(proxyServer) > -1){
checkThroughProxy = true;
}
const method = 'chanjet.EAP.GZQ.CSP.GetTPlusToken';
let auth = Auth.restore();
let clientName = window.localStorage.clientName || '';
let desc = window.localStorage.desc || ''
//供循环调用时使用
if(!!clientName) window.localStorage.clientName = clientName;
if(!!desc) window.localStorage.desc = desc;
const param = {
orgId: orgId,
cspUserId: auth.userId,
cspAppKey: atob(cspAk),
cspAppSecret: '',
lastLoginedToken: '',
clientName,
desc,
NoLogout:true
};
let requestObj = {};
requestObj = QS.stringify({
'Apis': JSON.stringify({
'Args': param,
'Method': method
})
});
const user = User.restore();
var ajax = axios.create({baseURL: serverUrl});
let url = 'api/rest';
url = url + '?IsFree=1&methodName='+method+'&userId='+user.userId +'&orgId='+orgId;
let user_req_id =`${user.userId}x${new Date().getTime().toString(16)}`;
url += '&user_req_id=' + user_req_id;
const resp = await ajax.post(url, requestObj,{headers:{checkThroughProxy,checkOrgId:orgId}})
.then(resp=>{
return Promise.resolve(resp);
}).catch(err => {
UIConfirm.show("未找到云企业(orgid:90015044950)对应的账套,请重新选择企业登录",()=>{
});
return Promise.reject(err);
});
return resp.data;
}
static async changeOrg(orgId){
var ajax = axios.create({
baseURL: tplusWebAppApiHost
});
const auth = Auth.restore();
const user = User.restore();
const resp = await ajax.post('gettplusauthinfo',QS.stringify({orgId,ciaUserId:auth.userId,ciaToken:user.accessToken,targetOrgID:orgId}), { headers: {'Content-Type':'application/x-www-form-urlencoded'}} );
return resp.data;
}
//获取云端的配置信息
static async getApiConfig(){
return axios.get(`${prodProxyHost}?url=https://newretail-static-pro-bj.oss-cn-beijing.aliyuncs.com/fe_gateway/api_config.json`);
}
//获取T+的token
static async getTplusToken({clientName = window.localStorage.clientName || '',desc = window.localStorage.desc || ''}):Promise<string>{
const method = 'chanjet.EAP.GZQ.CSP.GetTPlusToken';
let auth = Auth.restore();
//供循环调用时使用
if(!!clientName) window.localStorage.clientName = clientName;
if(!!desc) window.localStorage.desc = desc;
const param = {
orgId: auth.orgId,
cspUserId: auth.userId,
cspAppKey: atob(cspAk),
cspAppSecret: '',
lastLoginedToken: '',
clientName,
desc
};
const resp:string = (await tplusApi(param,method,false,true)) as string;
return resp;
}
//获取jsTicket信息
static async getSignature(){
const user = User.restore();
const access_token = user.accessToken;
const param = {
appKey:atob(cspAk),
access_token:access_token
};
const query = QS.stringify(param);
const url = `/api/v1/thirdplatform/getSignature?${query}`;
const ajax = axios.create({
baseURL: ciaHost
});
return new Promise((res,rej)=>{
ajax.get(url).then(resp=>{
const d = resp.data;
return res(d);
}).catch(err=>{
return rej(err);
});
});
}
//注销T+
static async logout(){
const method = 'chanjet.Authorization.Logout';
const param = {};
const resp = await tplusApi(param,method,false,false);
return resp;
}
}