130 lines
3.9 KiB
TypeScript
130 lines
3.9 KiB
TypeScript
|
/************************************************************************************
|
||
|
* FileName: HttpBase.ts
|
||
|
* Description:
|
||
|
*
|
||
|
* Version: v1.0.0
|
||
|
* Creator: Jacky(jackylvm@foxmail.com)
|
||
|
* CreationTime: 2024-04-22 16:42:58
|
||
|
* Copyright: 2021 - 2024
|
||
|
* ==============================================================
|
||
|
* History update record:
|
||
|
*
|
||
|
* ==============================================================
|
||
|
*************************************************************************************/
|
||
|
import * as CCEnv from 'cc/env';
|
||
|
import IHttpProto from "db://assets/module_basic/network/IHttpProto";
|
||
|
import {
|
||
|
BodiesType,
|
||
|
HeadersType,
|
||
|
HttpMethod,
|
||
|
ParamsType,
|
||
|
RequestCallback
|
||
|
} from "db://assets/module_basic/network/HttpProto";
|
||
|
|
||
|
export type ReqSuccessCallback = (response: HttpBase) => void;
|
||
|
export type ReqFailCallback = (errCode: number, response: HttpBase) => void;
|
||
|
|
||
|
export class HttpBase implements IHttpProto {
|
||
|
protected _url: string = "";
|
||
|
protected _cmd: string = "";
|
||
|
protected _autoUp: boolean = true;
|
||
|
|
||
|
protected _payload: Record<string, any> = undefined;
|
||
|
|
||
|
protected _successCallback: ReqSuccessCallback = undefined;
|
||
|
protected _failCallback: ReqFailCallback = undefined;
|
||
|
|
||
|
// 原始数据
|
||
|
public jsonData: Record<string, any> = undefined;
|
||
|
|
||
|
constructor(url: string, cmd: string, successCb?: ReqSuccessCallback, failCb?: ReqFailCallback, autoUp?: boolean) {
|
||
|
console.log("[HttpBase>constructor:38]>>", cmd);
|
||
|
this._url = url;
|
||
|
this._cmd = cmd;
|
||
|
this._successCallback = successCb;
|
||
|
this._failCallback = failCb;
|
||
|
this._autoUp = autoUp ?? true;
|
||
|
}
|
||
|
|
||
|
public set successCallback(cb: ReqSuccessCallback) {
|
||
|
this._successCallback = cb;
|
||
|
}
|
||
|
|
||
|
public set failCallback(cb: ReqFailCallback) {
|
||
|
this._failCallback = cb;
|
||
|
}
|
||
|
|
||
|
public get url(): string {
|
||
|
return this._url;
|
||
|
}
|
||
|
|
||
|
public get method(): HttpMethod {
|
||
|
return HttpMethod.POST;
|
||
|
}
|
||
|
|
||
|
get headers(): HeadersType {
|
||
|
// 所有的请求Content-Type都是application/x-www-form-urlencoded
|
||
|
// 但是login提交的数据是表单数据,其他请求提交都是json转的字符串
|
||
|
// 现在这里设置的application/json只是为了让body转换成json字符串
|
||
|
// 实际提交给服务器的Content-Type是application/x-www-form-urlencoded
|
||
|
return {"Content-Type": "application/json"};
|
||
|
}
|
||
|
|
||
|
get params(): ParamsType {
|
||
|
if (CCEnv.MINIGAME && CCEnv.WECHAT) {
|
||
|
return {};
|
||
|
}
|
||
|
return {"cmd": this._cmd};
|
||
|
}
|
||
|
|
||
|
get bodies(): BodiesType {
|
||
|
// 父类里面会创建通用的数据,如果子类有更多的数据需要携带
|
||
|
// 先调用父类的方法,获得通用数据,再添加子类自己的数据
|
||
|
this._payload.data = this.fillBodies();
|
||
|
return this._payload;
|
||
|
}
|
||
|
|
||
|
protected fillBodies(): Record<string, any> {
|
||
|
return {};
|
||
|
}
|
||
|
|
||
|
get callback(): RequestCallback {
|
||
|
return this.onResponse.bind(this);
|
||
|
}
|
||
|
|
||
|
private onResponse(err: Error | null, response: { type: XMLHttpRequestResponseType, data: any }): void {
|
||
|
if (err) {
|
||
|
console.error(err);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
this.jsonData = JSON.parse(response.data);
|
||
|
let errCode: number = this.jsonData.r ?? 0;
|
||
|
if (errCode != 0) {
|
||
|
console.log(`======服务器返回错误! cmd:[${this._cmd}](${errCode})`);
|
||
|
this.descErrCode(errCode);
|
||
|
}
|
||
|
|
||
|
if (errCode == 0) {
|
||
|
|
||
|
this.parsingData();
|
||
|
|
||
|
if (this._successCallback) {
|
||
|
this._successCallback(this);
|
||
|
}
|
||
|
} else {
|
||
|
if (this._failCallback) {
|
||
|
this._failCallback(errCode, this);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 解析 jsonData 数据,同步本地数据
|
||
|
protected parsingData() {
|
||
|
}
|
||
|
|
||
|
// 错误码描述
|
||
|
protected descErrCode(errCode: number) {
|
||
|
}
|
||
|
}
|