123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- 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) {
- //以'https:'开头
- } 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/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
- success(spriteFrame);
- })
- }
- };
- image.src = avatarUrl;
- } catch (e) {
- cc.log(e);
- cc.loader.loadRes('./textures/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/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
- success(spriteFrame);
- })
- }
- });
- }
- } else {
- cc.loader.loadRes('./textures/default_avatar', cc.SpriteFrame, (err, spriteFrame) => {
- success(spriteFrame);
- })
- }
- }
- /**
- * get请求对外暴露的方法
- * @param {object} requestBody 请求body
- * @param {string} requestBody.url 请求地址
- * @param {object} requestBody.data 请求发送的data
- * @param {function} requestBody.success 请求成功的回调
- * @param {function} requestBody.fail 请求失败的回调
- */
- 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);
- }
- }
- /**
- * post请求对外暴露的方法
- * @param {*} 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)) {
- // console.log('response:' + xhr.responseText);
- 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;
- }
- /**
- * get请求对外暴露的方法
- * @param {*} requestBody
- */
- 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);
- }
- }
- /**
- * post请求对外暴露的方法
- * @param {*} requestBody
- */
- 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);
- }
- resolve(res.data);
- // if (res.data.code === ResponseStateCode.OK) {
- // } else {
- // reject(res.data);
- // }
- },
- fail: (res) => {
- console.log('request 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) {
- var response = JSON.parse(xhr.responseText);
- if (xhr.status >= 200 && xhr.status < 400) {
- resolve(response);
- } else {
- reject(response);
- }
- // console.log('response:' + xhr.responseText);
- // var response = JSON.parse(xhr.responseText);
- // if (response.code == ResponseStateCode.OK) {
- // resolve(response);
- // } else {
- // reject(response);
- // }
- }
- };
- return xhr;
- }
- /**
- * 将每个接口都需要的公用参数都放到这里
- * @param target [Object] 请求参数
- */
- 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;
- }
- /**
- * 自动拼接请求接口
- * @param targetUrl [string] 请求接口
- */
- static requestUrl(targetUrl) {
- let host = Global.debug ? 'https://test-api-allstar.duowan.com' : 'https://api-allstar.duowan.com';
- // let host = Global.debug ? 'http://172.16.15.170:8080' : 'https://api-allstar.duowan.com';
- return host + targetUrl;
- }
- }
- module.exports = Api;
|