AlsSDK初版

(function (root) {
// const config = require(‘./als_config’)// 1.h5 2.小程序 3.cocos 4.nodejs 5.头条游戏
var Als = {}
const isObject = targe => Object.prototype.toString.call(targe) === ‘[object Object]’
const isNumber = targe => Object.prototype.toString.call(targe) === ‘[object Number]’
const isString = targe => Object.prototype.toString.call(targe) === ‘[object String]’
const isUndefined = targe => Object.prototype.toString.call(targe) === ‘[object Undefined]’
const isBoolean = targe => Object.prototype.toString.call(targe) === ‘[object Boolean]’
const isArray = targe => Object.prototype.toString.call(targe) === ‘[object Array]’
const isFunction = targe => Object.prototype.toString.call(targe) === ‘[object Function]’
const API = ‘https://sjh5.88youxi.cn/v2/’
const Uniacid = 66,Uid = 1021;

const getAppType = () => {
let type
// h5
if (typeof wx !== ‘undefined’) {
// 小程序
type = ‘wx’
} else if (typeof cc !== “undefined”) {
//cocos
type = ‘cc’
} else if (typeof window !== ‘undefined’) {
type = ‘h5’
} else if (typeof tt !== “undefined”) {
//cocos
type = ‘tt’
} else if (process === global.process) {
// nodejs
type = ‘nodejs’
} else {
// 默认H5
type = ‘h5’
}
return type
}
var appSelf = () => {

// return typeof wx ? wx: typeof tt ? tt
}

Als = {
version: 1.1,
url: “”,
Uid: Uid,
Uniacid: Uniacid,
type: getAppType(),
randNum(min, max) { //返回随机值
var range = max – min;
var rand = Math.random();
var num = min + Math.round(rand * range)
return num;
},
getAppType() { //返回平台类型
return this.type;
},
notEmpty(obj) {
// 检验 undefined 和 null 参数检测
if(!obj || obj == ” ||obj == undefined) {return false;}
if (Array.prototype.isPrototypeOf(obj) && obj.length === 0) {return false;  }
if (Object.prototype.isPrototypeOf(obj) && Object.keys(obj).length === 0) {    return false;  }   
return true;
},
setConfig(Uniacid = “”, Uid = “”) {
this.Uniacid = Uniacid,
this.Uid = Uid
},
funcWhere() {
var errs = new Error().stack
console.log(errs)
var names = new Error().stack.match(/at (.*?) /g);
var name = names[1].replace(‘at Object.’, ”).trim();
return name;
},

// loadRes(url,type,callBack){//加载资源修改属性
// if(!url){
// return;
// }
// cc.loader.loadRes(url,type,function(err,res){
// if(err){
// cc.LoadingItems(“–loadRes err:”+err)
// return;
// }
// if(callBack){
// callBack(res);
// }
// })
// },

sendPost(url, params = “”, method = “post”) {
console.log(“==post==”)
// let xhr = cc.loader.getXMLHttpRequest();
let xhr = new XMLHttpRequest();
return new Promise(function (resolve, reject) {
if (method == “post”) {
xhr.open(‘POST’, url, true);
// xhr.open(“GET”, url, true);
xhr.setRequestHeader(‘Access-Control-Allow-Origin’, ‘*’);
// xhr.setRequestHeader(‘Access-Control-Allow-Methods’, ‘GET, POST’);
xhr.setRequestHeader(“Content-Type”, “application/json”);
xhr.setRequestHeader(‘X-Uniacid’, Uniacid);
xhr.setRequestHeader(‘X-Uid’, Uid);

xhr.send(JSON.stringify(params));
} else {
xhr.open(“GET”, url, true);
xhr.send();
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status == 200) {
let respone = xhr.responseText;
let res = JSON.parse(respone);
if (res.status == 1 || res.status == ‘1’) {
var data = res.data;
console.log(res)
resolve(res.data.data);
} else {
reject(“请求异常,请重试!”);
}
}
};
})
},
}
var send = (url, params = “”, method = “post”) => {
if (url.length < 16) { url = API + url } var xhr = new XMLHttpRequest(); return new Promise(function (resolve, reject) { if (method == “post”) { xhr.open(‘POST’, url, true); xhr.setRequestHeader(‘Access-Control-Allow-Origin’, ‘*’); // xhr.setRequestHeader(‘Access-Control-Allow-Methods’, ‘GET, POST’); xhr.setRequestHeader(“Content-Type”, “application/json”); xhr.setRequestHeader(‘X-Uniacid’, Uniacid); xhr.setRequestHeader(‘X-Uid’, Uid); xhr.send(JSON.stringify(params)); } else { xhr.open(“GET”, url, true); xhr.send(); } xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status == 200) { var respone = xhr.responseText; var res = JSON.parse(respone); if (res.status == 1 || res.status == ‘1’) { var data = res.data; console.log(“urlres”) resolve(res.data); } else { if (res.status == 0) { throw new Error(JSON.stringify(res)) } reject(res); } } }; }) } var D = [“GetGoNewRank”, “GetUserList”] for (var p = 0; p < D.length; p++) ! function (e) { Als[e] = function (data = “”, method = “post”) { var url = `${e}` return send(url, data, method); } }(D[p]) var appType = getAppType(); if (“wx” === appType) { wx.Als = Als; } else if (“tt” === appType) { tt.Als = Als } else { if (“h5” === appType) { window.Als = Als; } else { if (“hap” === appType) { context.Als = Als; } else { if (“nodejs” === appType) { context.Als = Als; } } } } if (typeof exports !== “undefined”) { console.log(“====root=”) console.log(this) console.log(root) console.log(wx) if (typeof module !== “undefined” && module.exports) { exports = module.exports = Als } exports.Als = Als } else { root.Als = Als } }.call(this));

This entry was posted in 编程 by .

发表评论

电子邮件地址不会被公开。 必填项已用*标注