Commit 579d89ce by 胡锦波

1. init sdk请求打通

parent 6f71b59f
......@@ -33,7 +33,7 @@ export default abstract class BaseService {
protected readonly positiveValue = 1;
protected readonly negativeValue = 0;
private onceAppId: string | number = 0;
private onceCompanyId: string | number = 0;
protected constructor() {
if (!this.isDevEnvironment()) {
......@@ -189,43 +189,16 @@ export default abstract class BaseService {
return config;
}
public setEnterprise4Cache(data: any) {
localStorage.setItem('currentEnterPriseList', JSON.stringify(data));
protected useOnceApp(poid: string | number) {
this.onceCompanyId = poid;
}
public getEnterprise4Cache() {
const item = localStorage.getItem('currentEnterPriseList');
return (item && JSON.parse(item)) || null;
}
public getEnterpriseToken4RegisterId() {
const currentRegisterId = localStorage.getItem("currentTemmixCompany") || "";
const cache = this.getEnterprise4Cache();
if (cache && cache[currentRegisterId]) {
return cache[currentRegisterId].appRegisterId;
}
return '';
}
public getEnterpriseToken4Id() {
const currentRegisterId = localStorage.getItem("currentTemmixCompany") || "";
const cache = this.getEnterprise4Cache();
if (cache && cache[currentRegisterId]) {
return cache[currentRegisterId].id;
}
return '';
}
protected useOnceApp(app: string | number) {
this.onceAppId = app;
}
protected async buildAppToken2Config(config?: AxiosRequestConfig) {
protected async buildCompanyToken2Config(config?: AxiosRequestConfig) {
config = this.buildRequestConfig(config);
if (config.headers) {
config.headers[this.authKey] = await TokenManager.getCompanyToken(this.onceAppId || this.getEnterpriseToken4RegisterId());
config.headers[this.authKey] = await TokenManager.getCompanyToken(this.onceCompanyId);
}
this.onceAppId = 0;
this.onceCompanyId = 0;
return this.checkContentType(config);
}
......@@ -251,8 +224,8 @@ export default abstract class BaseService {
return this.invokeGet(url, config) as Promise<T>;
}
protected async getWithAppToken<T>(url: string, config?: AxiosRequestConfig): Promise<T> {
config = await this.buildAppToken2Config(config);
protected async getWithCompanyToken<T>(url: string, config?: AxiosRequestConfig): Promise<T> {
config = await this.buildCompanyToken2Config(config);
return this.invokeGet(url, config) as Promise<T>;
}
......@@ -261,13 +234,13 @@ export default abstract class BaseService {
return this.invokePost(url, data, config) as Promise<T>;
}
protected async postWithAppToken<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
config = await this.buildAppToken2Config(config);
protected async postWithCompanyToken<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
config = await this.buildCompanyToken2Config(config);
return this.invokePost(url, data, config) as Promise<T>;
}
protected async deleteWithAppToken<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
config = await this.buildAppToken2Config(config);
protected async deleteWithCompanyToken<T>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
config = await this.buildCompanyToken2Config(config);
return this.invokeDelete(url, data, config) as Promise<T>;
}
......
import Axios, { AxiosRequestConfig } from "axios";
import { TokenManager } from "@/api/token";
import EnterpriseHost from "@/views/service/enterprise-host";
class SdkCoreService {
private baseUrl = "";
private config = {};
protected async buildSdkHeaders() {
this.baseUrl = 'http://hro.test-api.qqxb.jinsehuaqin.com:8800' || process.env.VUE_APP_UNIPLAT_BASE_URL || 'https://api-hro.qinqinxiaobao.com';
const comid = EnterpriseHost.getPoid();
const token = await TokenManager.getCompanyToken(comid);
this.config = {
headers: {
passportToken: `${token}`
},
baseURL: this.baseUrl,
};
return this.config;
}
protected invokeGet(url: string, config: AxiosRequestConfig) {
return new Promise((resolve, reject) => {
return Axios.get(`${this.baseUrl}${url}`, config).then(r => {
if (!r) {
return reject(r);
}
if (r.data.rescode) {
reject(r.data);
} else {
resolve(r.data.data);
}
});
});
}
protected invokePost(url: string, body: any, config = this.config) {
return new Promise((resolve, reject) => {
return Axios.post(`${this.baseUrl}${url}`, body, config).then(r => {
if (!r) {
return reject(r);
}
if (r.data.rescode) {
reject(r.data);
} else {
resolve(r.data.data);
}
});
});
}
protected async get(url: string) {
const config = await this.buildSdkHeaders();
return this.invokeGet(url, config);
}
protected async post(url: string, body: any) {
const config = await this.buildSdkHeaders();
return this.invokePost(url, body, config);
}
}
class SdkService extends SdkCoreService {
getJoinedList() {
return this.get(`/system/org/joined/list`);
}
getWaitJoinList() {
return this.get(`/system/org/waitjoin/list`);
}
}
const sdkService = new SdkService();
export { sdkService };
......@@ -10,10 +10,12 @@
import { Component, Vue } from "vue-property-decorator";
import { TokenManager } from "@/api/token";
import EnterpriseHost from "@/views/service/enterprise-host";
import { sdkService } from "@/api/sdk-service";
@Component({ components: {} })
export default class AddressBook extends Vue {
private test() {
sdkService.getJoinedList();
const comid = EnterpriseHost.getPoid();
TokenManager.getCompanyToken(comid);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment