1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// js版explode
function explode(inputstring, separators, includeEmpties) {
inputstring = new String(inputstring);
separators = new String(separators);
if (separators == "undefined") {
separators = " :;";
}
fixedExplode = new Array(1);
currentElement = "";
count = 0;
for (x = 0; x < inputstring.length; x++) {
str = inputstring.charAt(x);
if (separators.indexOf(str) != -1) {
if (((includeEmpties <= 0) || (includeEmpties == false)) && (currentElement == "")) {} else {
fixedExplode[count] = currentElement;
count++;
currentElement = "";
}
} else {
currentElement += str;
}
}
if ((!(includeEmpties <= 0) && (includeEmpties != false)) || (currentElement != "")) {
fixedExplode[count] = currentElement;
}
return fixedExplode;
}

// 获取get参数
function getQueryStr(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURIComponent(r[2]);
return null;
}

// 获取指定范围随机数min ≤ r ≤ max
function randBetween(Min, Max) {
var Range = Max - Min;
var Rand = Math.random();
var num = Min + Math.round(Rand * Range); //四舍五入
return num;
}
/**
* 获取get参数
*/
function getQueryStr(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURIComponent(r[2]);
return null;
}
/**
* 通过res name获取bitmap
*/
function createBitmapByName(name: string) {
let result = new egret.Bitmap();
let texture: egret.Texture = RES.getRes(name);
if(!texture){
console.log("no image:"+name );
}
result.texture = texture;
return result;
}
/**
* 截图 获取base64
*/
function doShot(obj:egret.DisplayObject,x:number=0,y=0,w=null,h=null){
let renderTexture = new egret.RenderTexture;
renderTexture.drawToTexture(obj,new egret.Rectangle(x,y,w,h));
return renderTexture.toDataURL("image/jpeg",new egret.Rectangle(0,0,w,h));
}
/**
* 发送ajax
*/
function ajax({url,data,success,type="POST"}){
var xmlhttp = new XMLHttpRequest();
xmlhttp.withCredentials = false;
xmlhttp.open(type, url, true);
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4){
if(xmlhttp.status == 200){
var res = xmlhttp.response
success(JSON.parse(res));
}else{
success("error")
console.log("ajax error");
}
}
}
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded;");
xmlhttp.send(buildForm(data,null));
}
/**
* 生成url
*/
function buildForm (data: any, query: any): string {
let params: any = [];
if (data) {
for (let n in data) {
params.push(encodeURIComponent(n) + "=" + encodeURIComponent(data[n]));
}
}
if (query) {
params = params.concat(query);
}
return params.join("&");
};
/**
* 设置元素样式
*/
function setStyle(obj,style){
for (var key in style) {
let val = style[key];
let sty = obj.style;
sty[key] = val;
}
}
/**
* 判断是否PC
*/
function IsPC() {
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}
/**
* 读取cookie
**/
function getCookie(name)
{
var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
if(arr=document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
/**
* 获取拓展名
**/
function getExtension(filename)
{
var index1 = filename.lastIndexOf(".");
if(index1 < 0){
return null;
}
var index2 = filename.length;
var postf = filename.substring(index1+1,index2);
return postf;
}

/**
* 字符串填充
**/
function strPad(str) {
var pad = "00000"
return pad.substring(0, pad.length - str.length) + str
}

// 获取web目录 无斜杠
function getRootPath() {
var pathName = window.location.pathname.substring(1);
var webName = pathName == '' ? '' : pathName.substring(0, pathName.indexOf('/'));
if (webName == "") {
return window.location.protocol + '//' + window.location.host;
} else {
return window.location.protocol + '//' + window.location.host + '/' + webName;
}
}

// js数组打乱
function randomsort(a, b) {
return Math.random()>.5 ? -1 : 1;
//用Math.random()函数生成0~1之间的随机数与0.5比较,返回-1或1
}
var arr = [1, 2, 3, 4, 5];
arr.sort(randomsort);