协议修改
This commit is contained in:
@@ -15,7 +15,7 @@ export class ApiClient {
|
||||
}
|
||||
private constructor(private http = HttpManager.I) {}
|
||||
private get baseUrl() { return AppConfig.I.getHttpBase() }
|
||||
private get token() { return DataManager.I.getDataById<LoginData>(DataId.Login).getToken();}
|
||||
private get token() { return DataManager.I.getDataById<LoginData>(DataId.Login).token;}
|
||||
|
||||
public async call<Req, Res>(ep: Endpoint<Req, Res>, req: Req): Promise<ApiResponse<Res>> {
|
||||
const url = `${this.baseUrl}/${ep.path.replace(/^\/+/, "")}`;
|
||||
@@ -67,12 +67,12 @@ export class ApiClient {
|
||||
if (parsed && typeof parsed === "object" && typeof parsed.code === "number") {
|
||||
return {
|
||||
code: parsed.code,
|
||||
msg: parsed.msg ?? (parsed.code === 1 ? "ok" : "error"),
|
||||
data: parsed.Data as Res
|
||||
msg: parsed.msg ?? (parsed.code === 0 ? "ok" : "error"),
|
||||
data: parsed.data as Res
|
||||
};
|
||||
}
|
||||
// 否则我们包一层
|
||||
return { code: ApiCode.OK, msg: "ok", data: parsed.Data as Res };
|
||||
return { code: ApiCode.OK, msg: "ok", data: parsed.data as Res };
|
||||
} else if (ep.codec === "proto") {
|
||||
const data = ProtoCodec.decode<Res>(ep.resType!, r.body);
|
||||
// proto 场景一般没有 {code,msg} 包装,我们统一包一层
|
||||
|
||||
@@ -5,7 +5,7 @@ export interface ApiResponse<T = any> {
|
||||
}
|
||||
|
||||
export const ApiCode = {
|
||||
OK: 1,
|
||||
OK: 0,
|
||||
AUTH_EXPIRED: -401,
|
||||
HTTP_ERROR: -400,
|
||||
TIMEOUT: -408,
|
||||
|
||||
@@ -12,9 +12,9 @@ export class AuthService {
|
||||
private constructor(private api = ApiClient.I) {}
|
||||
|
||||
// 登录
|
||||
public async login(req: proto.ProtoMsg.ILoginReq): Promise<ApiResponse<proto.ProtoMsg.ILoginRsp>> {
|
||||
let epData: Endpoint<proto.ProtoMsg.ILoginReq, proto.ProtoMsg.ILoginRsp> = {
|
||||
path: "login",
|
||||
public async login(req: proto.cs.ICSLoginReq): Promise<ApiResponse<proto.cs.ICSLoginRes>> {
|
||||
let epData: Endpoint<proto.cs.ICSLoginReq, proto.cs.ICSLoginRes> = {
|
||||
path: "api/acc/login",
|
||||
method: "POST",
|
||||
codec: "json",
|
||||
needsAuth: false
|
||||
|
||||
@@ -48,13 +48,10 @@ export class HttpManager {
|
||||
xhr.open(method, url, true);
|
||||
if (sys.isNative) {
|
||||
xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
|
||||
xhr.setRequestHeader("content-type", "text/html;charset=utf-8");
|
||||
xhr.setRequestHeader("Content-type", "text/html;charset=utf-8");
|
||||
}
|
||||
} else {
|
||||
xhr.open(method, url, true);
|
||||
if (!opt.rawBody) {
|
||||
xhr.setRequestHeader("Content-Type", "application/json");
|
||||
}
|
||||
}
|
||||
if (opt.headers) {
|
||||
Object.keys(opt.headers).forEach(k => xhr.setRequestHeader(k, opt.headers![k]));
|
||||
|
||||
Reference in New Issue
Block a user