123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class HTTPS {
- private static _instance = null;
- public static get Instance(): HTTPS {
- if (this._instance == null) {
- this._instance = new HTTPS();
- }
- return this._instance;
- }
- token = null;
- // private _URL = "http://120.79.5.67:7000";
- //得到现在的时间
- private GetNowTime(): string {
- const date = new Date(cc.sys.now());
- const hours = date.getHours();
- const minutes = date.getMinutes();
- const seconds = date.getSeconds();
- return `${hours}:${minutes}:${seconds} `;
- }
- post(action: string, data: object | string): Promise<any> {
- if (CC_DEBUG) {
- cc.error(this.GetNowTime() + "发送消息 >>>>>> :" + action, data);
- // cc.error(this.GetNowTime() + "消息内容 >>>>>> :", data);
- }
- const promise: Promise<any> = new Promise((resolve, reject) => {
- var timeoutKey: any;
- var retry = 5;//失败重试次数
- const send = () => {
- const httpRequest = new XMLHttpRequest();
- const method = 'POST'
- httpRequest.onreadystatechange = (event: Event): void => {
- const readyState = httpRequest.readyState;
- if (readyState !== 4) return;
- if (!timeoutKey) return;//已通知超时,之后收到数据后也不处理
- clearTimeout(timeoutKey);
- timeoutKey = null;
- const requestStatus = httpRequest.status;
- if (requestStatus >= 200 && requestStatus < 400) {
- let resp = JSON.parse(httpRequest.responseText);
- resolve(resp);
- if (CC_DEBUG) {
- cc.error(this.GetNowTime() + `收到消息(${retry}) >>>>>> :` + action, resp);
- // cc.error(httpRequest.responseText);
- }
- }
- else if (retry-- > 0) send();
- else reject(new Error('全部超时'));
- }
- httpRequest.open(method, Url + action, true);
- // httpRequest.setRequestHeader("Access-Control-Allow-Origin", "*");
- httpRequest.setRequestHeader("Content-Type", "application/json;charset=utf-8");
- httpRequest.setRequestHeader("Authorization", this.token != null ? this.token : "");
- var text = typeof (data) == "string" ? data : JSON.stringify(data);
- data ? httpRequest.send(text) : httpRequest.send();
- timeoutKey = setTimeout(() => {
- timeoutKey = null;
- httpRequest.abort();
- reject(new Error(`Timeout`));
- }, 12000);//超时时间(毫秒)
- }
- send();
- })
- return promise;
- }
- get(action: string): Promise<any> {
- if (CC_DEBUG) {
- cc.error(this.GetNowTime() + "发送消息 >>>>>> :" + action);
- }
- const promise: Promise<any> = new Promise((resolve, reject) => {
- var timeoutKey: any;
- var retry = 5;//失败重试次数
- const send = () => {
- const httpRequest = new XMLHttpRequest();
- const method = 'GET'
- httpRequest.onreadystatechange = (event: Event): void => {
- const readyState = httpRequest.readyState;
- if (readyState !== 4) return;
- if (!timeoutKey) return;//已通知超时,之后收到数据后也不处理
- clearTimeout(timeoutKey);
- timeoutKey = null;
- const requestStatus = httpRequest.status;
- if (requestStatus >= 200 && requestStatus < 400) {
- let resp = JSON.parse(httpRequest.responseText);
- resolve(resp);
- if (CC_DEBUG) {
- cc.error(this.GetNowTime() + `收到消息(${retry}) >>>>>> :` + action, resp);
- }
- }
- else if (retry-- > 0) send();
- else reject(new Error('全部超时'));
- }
- httpRequest.open(method, action, true);
- httpRequest.setRequestHeader("Authorization", this.token != null ? this.token : "");
- httpRequest.send();
- timeoutKey = setTimeout(() => {
- timeoutKey = null;
- httpRequest.abort();
- reject(new Error(`Timeout`));
- }, 12000);//超时时间(毫秒)
- }
- send();
- })
- return promise;
- }
- }
- export var Url = "http://yx.dashenwangluo.com";
- export var NetGet = {
- /**获取任务配置 */
- Task: Url + "/v1/task/config",//token: 用户token
- /**获取提现规则 */
- rule: Url + "/v1/withdraw/rule",//token: 用户token
- /**获取提现记录 */
- withdrawHistory: Url + "/v1/withdraw/log?page=1&limit=20",//token: 用户token
- }
- export var NetPost = {
- /**发送验证码 */
- SendIphoneCode: "/v1/user/send_code",
- /**手机号登录 */
- IphoneLogin: "/v1/user/login",
- /**完成任务 */
- complete: "/v1/task/complete",// task_type: 任务类型(ad_watch/sign_in)
- /**申请提现 */
- withdraw: "/v1/withdraw/apply",// task_type: 任务类型(ad_watch/sign_in)
- /**记录广告观看 */
- record: "/v1/ad/record",// task_type: 任务类型(ad_watch/sign_in)
-
- }
|