123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- const ResponseStateCode = require('../utils/GameEnum').ResponseStateCode;
- class Api {
- static createImageFromUrl(avatarUrl, success, fail) {
- 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);
- fail();
- }
- };
- image.src = avatarUrl;
- } catch (e) {
- cc.log(e);
- fail();
- }
- } else {
- cc.loader.load({
- url: avatarUrl, type: 'jpg'
- }, (err, texture) => {
- if (err === undefined || err === null) {
- success(new cc.SpriteFrame(texture));
- } else {
- fail();
- }
- });
- }
- }
- /**
- * 测试网络用的方法,可以注释掉
- */
- testGetBuildingInfo() {
- Api.httpGet({
- url: '/building/getUserBuildings.do',
- data: {
- targetUid: 1,
- // cityId: 1
- },
- success: (response) => {
- // console.log('success: ' + JSON.stringify(response));
- },
- fail: (response) => {
- console.log('success: ' + JSON.stringify(response));
- },
- complete: () => {
- }
- });
- }
- /**
- * 测试网络用的方法,可以注释掉
- */
- testUpgradeBuildingInfo() {
- Api.httpPost({
- url: '/building/upGrade.do',
- data: {
- formInfo: '1'
- },
- success: (response) => {
- console.log('success: ' + JSON.stringify(response));
- },
- fail: (response) => {
- console.log('success: ' + JSON.stringify(response));
- },
- complete: () => {
- }
- });
- }
- /**
- * get请求对外暴露的方法
- * @param {*} requestBody
- */
- 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) {
- 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) => {
- 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;
- }
- /**
- * 将每个接口都需要的公用参数都放到这里
- * @param target [Object] 请求参数
- */
- static baseParam(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;
|