123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- const ResponseStateCode = require('../utils/GameEnum').ResponseStateCode;
- class Api {
- static createImageFromUrl(avatarUrl, success, fail) {
- if (avatarUrl) {
- var index = avatarUrl.indexOf('https:');
- var httpIndex = avatarUrl.indexOf('http:');
- if (index === 0 || httpIndex === 0) {
-
- } else {
- avatarUrl = 'https:' + avatarUrl;
- }
- if (window.wx != undefined) {
- try {
- let image = wx.createImage();
- image.onload = () => {
- try {
- let texture = new cc.Texture2D();
- texture.initWithElement(image);
- texture.handleLoadedTexture();
- success(new cc.SpriteFrame(texture))
- } catch (e) {
- cc.log(e);
- cc.loader.loadRes('./textures/common/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
- success(spriteFrame);
- });
- }
- };
- image.src = avatarUrl;
- } catch (e) {
- cc.log(e);
- cc.loader.loadRes('./textures/common/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
- success(spriteFrame);
- });
- }
- } else {
- cc.loader.load({
- url: avatarUrl, type: 'jpg'
- }, (err, texture) => {
- if (err === undefined || err === null) {
- success(new cc.SpriteFrame(texture));
- } else {
- cc.loader.loadRes('./textures/common/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
- success(spriteFrame);
- });
- }
- });
- }
- } else {
- cc.loader.loadRes('./textures/common/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
- success(spriteFrame);
- });
- }
- }
-
- static httpGet(requestBody) {
- requestBody.data = Api.baseParam(requestBody.data);
- requestBody.url = Api.requestUrl(requestBody.url);
- if (window.wx != undefined) {
- Api.wxGet(requestBody);
- } else {
- Api.jsGet(requestBody);
- }
- }
-
- static httpPost(requestBody) {
- requestBody.data = Api.baseParam(requestBody.data);
- requestBody.url = Api.requestUrl(requestBody.url);
- if (window.wx != undefined) {
- Api.wxPost(requestBody);
- } else {
- Api.jsPost(requestBody);
- }
- }
- static wxGet(requestBody) {
- let onSuccess = requestBody.success;
- let onFail = requestBody.fail;
- let onComplete = requestBody.complete;
- wx.request({
- url: requestBody.url,
- data: requestBody.data,
- method: requestBody.method === undefined ? 'GET' : 'POST',
- header: requestBody.header === undefined ? {} : requestBody.header,
- success: (res) => {
-
- if (res.data.code === ResponseStateCode.LOGIN_INVALIDATE) {
- Global.needLogin = true;
- cc.director.loadScene('login');
- return;
- }
- if (res.data.code === ResponseStateCode.OK) {
- if (onSuccess != undefined) {
- onSuccess(res.data.data, res.data.msg);
- }
- } else {
- if (onFail != undefined) {
- onFail(res.data.code, res.data.msg);
- }
- }
- },
- fail: (res) => {
- console.log('wxGet fail: ' + res);
- if (onFail != undefined) {
- onFail(-1, res.errMsg);
- }
- },
- complete: onComplete,
- });
- }
- static wxPost(requestBody) {
- requestBody.header = {
- "Content-Type": "application/x-www-form-urlencoded"
- };
- requestBody.method = 'POST';
- Api.wxGet(requestBody);
- }
- static jsGet(requestBody) {
- let url = requestBody.url;
- let data = requestBody.data;
- url = url + "?";
- for (var key in data) {
- url = url + key + '=' + data[key] + '&';
- }
- url = url.substr(0, url.length - 1)
- var xhr = Api.jsBaseRequest(requestBody.success, requestBody.fail, requestBody.complete);
- xhr.open("GET", url, true);
- xhr.send();
- }
- static jsPost(requestBody) {
- let data = requestBody.data;
- var xhr = Api.jsBaseRequest(requestBody.success, requestBody.fail, requestBody.complete);
- xhr.open("POST", requestBody.url, true);
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- let formData = new String();
- for (var key in data) {
- formData = formData + key + '=' + data[key] + "&";
- }
- xhr.send(formData.substr(0, formData.length - 1));
- }
- static jsBaseRequest(onSuccess, onFail, onComplete) {
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange = () => {
- if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
-
- var response = JSON.parse(xhr.responseText);
- if (response.code == ResponseStateCode.OK) {
- if (onSuccess != undefined) {
- onSuccess(response.data, response.msg);
- }
- } else {
- if (onFail != undefined) {
- onFail(response.code, response.msg)
- }
- }
- if (onComplete != undefined) {
- onComplete();
- }
- }
- };
- return xhr;
- }
-
- static httpGetPromise(url, data) {
- url = Api.requestUrl(url);
- data = Api.baseParam(data);
- if (window.wx != undefined) {
- return Api.wxGetP(url, data);
- } else {
- return Api.jsGetP(url, data);
- }
- }
-
- static httpPostPromise(url, data) {
- url = Api.requestUrl(url);
- data = Api.baseParam(data);
- if (window.wx != undefined) {
- return Api.wxPostP(url, data);
- } else {
- return Api.jsPostP(url, data);
- }
- }
- static wxGetP(url, data) {
- return Api.wxRequestPromise(url, data, 'GET');
- }
- static wxPostP(url, data) {
- let header = {
- "Content-Type": "application/x-www-form-urlencoded"
- };
- return Api.wxRequestPromise(url, data, 'POST', header);
- }
- static wxRequestPromise(url, data, method, header) {
- return new Promise((resolve, reject) => {
- wx.request({
- url: url,
- data: data,
- method: method,
- header: header === undefined ? {} : header,
- success: (res) => {
-
- if (res.data.code === ResponseStateCode.LOGIN_INVALIDATE) {
- Global.needLogin = true;
- cc.director.loadScene('login');
- reject(res.data);
- }
- if (res.data.code === ResponseStateCode.OK) {
- resolve(res.data);
- } else {
- reject(res.data);
- }
- },
- fail: (res) => {
- reject({
- code: -1,
- msg: '微信接口调用失败'
- });
- },
- complete: {},
- });
- });
- }
- static jsGetP(url, data) {
- return new Promise((resolve, reject) => {
- url = url + "?";
- for (var key in data) {
- url = url + key + '=' + data[key] + '&';
- }
- url = url.substr(0, url.length - 1);
- var xhr = Api.jsBaseRequestP(resolve, reject);
- xhr.open("GET", url, true);
- xhr.send();
- });
- }
- static jsPostP(url, data) {
- return new Promise((resolve, reject) => {
- var xhr = Api.jsBaseRequestP(resolve, reject);
- xhr.open("POST", url, true);
- xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- let formData = new String();
- for (var key in data) {
- formData = formData + key + '=' + data[key] + "&";
- }
- xhr.send(formData.substr(0, formData.length - 1));
- });
- }
- static jsBaseRequestP(resolve, reject) {
- var xhr = new XMLHttpRequest();
- xhr.onreadystatechange = () => {
- if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status < 400)) {
-
- var response = JSON.parse(xhr.responseText);
- if (response.code == ResponseStateCode.OK) {
- resolve(response);
- } else {
- reject(response);
- }
- }
- };
- return xhr;
- }
-
- static baseParam(target) {
- if (target === undefined) {
- target = {};
- }
- target.thirdType = 1;
- target.channel = Global.channel;
- target.ver = Global.ver;
- target.os = Global.os;
- target.uid = Global.user ? Global.user.uid : '';
- target.token = Global.user ? Global.user.token : '';
- return target;
- }
-
- static requestUrl(targetUrl) {
- let host = Global.debug ? 'https://test-api-taptap.duowan.com' : 'https://api-taptap.duowan.com';
-
-
-
-
- return host + targetUrl;
- }
- static setRequestBody(url, data, success, fail) {
- let requestBody = {
- url: url,
- data: data,
- success: success,
- fail: fail,
- complete: () => {
- }
- };
- return requestBody;
- }
- }
- module.exports = Api;
|