kd-sdk.1.0.0-old.js
15.8 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
(function () {
let BASE_HOST = "https://api.kdao.xin";
let BIZ_BASE_HOST = "https://bizapi.kdao.xin";
// 打开微信调试的开关
let DEBUG_SWITCH = "false";
let KD = {};
window.KD = KD;
let _app_id = "", _app_secret = "", _module_code = "";
KD.init = function (appId, appKey, moduleCode) {
_app_id = appId;
_app_secret = appKey;
_module_code = moduleCode;
}
let _request_url = {
wx_signature: BASE_HOST + "/wx/signature",
// 授权后的用户信息
wx_sns_user_info: BASE_HOST + "/wx_user_info",
// 含unionid 的用户信息
wx_user_info: BASE_HOST + "/cgi_bin/user_info",
// 统计
stat: BIZ_BASE_HOST + "/kd_platform_api/stat/s"
}
KD._tools = {};
(function () {
KD._tools._post_json = function (url, data, header, callback) {
var xhr;
//创建ajax引擎
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.AtiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
let data = { code: xhr.status, response: xhr.responseText };
(typeof callback == "function") && callback(data);
}
};
//创建请求
xhr.open("post", url, true);
xhr.setRequestHeader('Content-type', 'application/json');
if (header) {
for (let key in header) {
xhr.setRequestHeader(key, header[key]);
}
}
//发送请求
xhr.send(JSON.stringify(data));
}
KD._tools._post_form = function (url, data, header, callback) {
var xhr;
//创建ajax引擎
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.AtiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
let data = { code: xhr.status, response: xhr.responseText };
(typeof callback == "function") && callback(data);
}
};
//创建请求
xhr.open("post", url, true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
if (header) {
for (let key in header) {
xhr.setRequestHeader(key, header[key]);
}
}
//发送请求
let postStr = "";
for (let key in data) {
postStr += key + "=" + data[key] + "&";
}
if (postStr) {
postStr = postStr.substring(0, postStr.length - 1);
}
xhr.send(postStr);
}
KD._tools._get = function (url, data, header, callback) {
var xhr;
//创建ajax引擎
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.AtiveXObject) {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
let data = { code: xhr.status, response: xhr.responseText };
(typeof callback == "function") && callback(data);
}
};
//发送请求
let postStr = "";
for (let key in data) {
postStr += key + "=" + data[key] + "&";
}
if (postStr) {
postStr = "?" + postStr.substring(0, postStr.length - 1);
}
xhr.open("GET", url + postStr, true);
//创建请求
if (header) {
for (let key in header) {
xhr.setRequestHeader(key, header[key]);
}
}
xhr.send(null);
}
KD._tools._get_token = function () {
let key = "pl_token_" + _app_id;
return KD._tools._get_cookie(key);
}
KD._tools._get_wx_app_id = function () {
let key = "pl_wx_" + _app_id;
let res = KD._tools._get_cookie(key);
let wxAppId = res ? res.split(":")[0] : "";
return wxAppId;
}
KD._tools._get_cookie = function (name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg)) {
return decodeURIComponent(arr[2])
} else {
return null
}
};
/**
* 处理响应结果
*/
KD._tools._handle_response = function (response) {
if (response.code == 200) {
let res = JSON.parse(response.response);
if (res.code == 200) {
return res;
} else {
if (DEBUG_SWITCH) {
alert("_handle_response failure : " + response.content)
}
}
} else {
if (DEBUG_SWITCH) {
alert("_handle_response : " + JSON.stringify(response))
}
}
}
/**
* 加解密CDN方式引入。
*
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/enc-base64.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/md5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/evpkdf.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/cipher-core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/aes.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/pad-pkcs7.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/mode-ecb.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/enc-utf8.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/enc-hex.min.js"></script>
*/
KD._tools._init_aes = function () {
if (typeof KD._tools._aes_decrypt == "function") {
return true;
}
if (typeof CryptoJS == "undefined") {
console.error("install CryptoJS first");
return false;
}
if (CryptoJS && _app_secret) {
const key = CryptoJS.enc.Utf8.parse(_app_secret);
const iv = CryptoJS.enc.Utf8.parse(_app_secret.substring(0, 16));
// 解密方法
KD._tools._aes_decrypt = function (srcs) {
// let encryptedHexStr = CryptoJS.enc.Hex.parse(word);
// console.log("1,", encryptedHexStr)
// let srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
let decrypt = CryptoJS.AES.decrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
return decryptedStr.toString();
}
// 加密方法
KD._tools._aes_encrypt = function (word) {
var srcs = CryptoJS.enc.Utf8.parse(word);
var encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
return true;
}
return false;
}
KD._tools._init_headers = function () {
return {
"-kd-platform-id": _app_id,
"-kd-platform-module": _module_code
}
}
})();
// 微信SDK地址
KD.weixin = function () {
let weixin = {};
let tools = KD._tools;
/**
* 权限列表:
* updateAppMessageShareData,updateTimelineShareData,onMenuShareWeibo,onMenuShareQZone,
* startRecord,stopRecord,onVoiceRecordEnd,playVoice,pauseVoice,stopVoice,onVoicePlayEnd,uploadVoice,downloadVoice,
* chooseImage,previewImage,uploadImage,downloadImage,
* translateVoice,
* getNetworkType,openLocation,getLocation,
* hideOptionMenu,showOptionMenu,hideMenuItems,showMenuItems,hideAllNonBaseMenuItem,showAllNonBaseMenuItem,
* closeWindow,scanQRCode,chooseWXPay,openProductSpecificView,addCard,chooseCard,openCard,
*/
weixin.initConfig = function (jsApiList) {
let url = window.location.href;
// jsApiList = jsApiList && jsApiList.length > 0 ? jsApiList : ["updateAppMessageShareData", "updateTimelineShareData", "onMenuShareWeibo", "onMenuShareQZone"];
jsApiList = jsApiList && jsApiList.length > 0 ? jsApiList : ["onMenuShareTimeline", "onMenuShareAppMessage", "hideMenuItems"];
tools._post_json(_request_url.wx_signature, { appId: _app_id, url: url }, null, function (response) {
let res = KD._tools._handle_response(response);
wx.config({
debug: DEBUG_SWITCH, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: res.content.appId, // 必填,公众号的唯一标识
timestamp: res.content.timestamp, // 必填,生成签名的时间戳
nonceStr: res.content.nonceStr, // 必填,生成签名的随机串
signature: res.content.signature,// 必填,签名
jsApiList: jsApiList // 必填,需要使用的JS接口列表
});
wx.ready(function () {
wx.hideMenuItems({
menuList: ["menuItem:copyUrl"] // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
});
})
});
}
/**
* 单独设置分享朋友圈
*/
weixin.updateAppMessageShareData = function (title, desc, link, imageUrl, succ) {
wx.ready(function () { //需在用户可能点击分享按钮前就先调用
wx.onMenuShareAppMessage({
title: title, // 分享标题
desc: desc, // 分享描述
link: link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: imageUrl, // 分享图标
success: function () {
(typeof succ == "function") && succ();
}
})
});
}
weixin.updateTimelineShareData = function (title, link, imageUrl, succ) {
wx.ready(function () { //需在用户可能点击分享按钮前就先调用
wx.onMenuShareTimeline({
title: title, // 分享标题
link: link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: imageUrl, // 分享图标
success: function () {
(typeof succ == "function") && succ();
}
})
});
}
/**
* 微信设置分享
*/
weixin.setShare = function (title, desc, link, imageUrl, succ) {
weixin.updateAppMessageShareData(title, desc, link, imageUrl, function () {
(typeof succ == "function") && succ("appMessage");
});
weixin.updateTimelineShareData(desc, link, imageUrl, function () {
(typeof succ == "function") && succ("timeline");
});
}
/**
* 获取用户的openid
*/
weixin.getOpenId = function () {
let openId = tools._get_token();
return openId;
}
/**
* 读取用户基本信息
*/
weixin.getUserInfo = function (callback) {
if (typeof callback != "function") {
return;
}
if (typeof CryptoJS != "undefined") {
let info = _get_user_info_from_cookie();
if (info) {
callback(info);
} else {
_get_user_info_by_openid(callback);
}
} else {
_get_user_info_by_openid(callback);
}
}
/**
* 这个是用户授权后,从微信端读取用户基本信息。这个接口需要用户显示授权
*/
weixin.getCgiUserInfo = function (callback) {
if (typeof callback != "function") {
return;
}
let openId = tools._get_token();
let wxAppId = tools._get_wx_app_id();
tools._get(_request_url.wx_user_info, { wxAppId: wxAppId, openId: openId }, null, function (response) {
let res = tools._handle_response(response);
callback(res.content);
})
}
/**
* 通过openid读取用户基本信息
*
* @param {function} callback
*/
function _get_user_info_by_openid(callback) {
let openId = tools._get_token();
let wxAppId = tools._get_wx_app_id();
tools._get(_request_url.wx_sns_user_info, { wxAppId: wxAppId, openId: openId }, null, function (response) {
let res = tools._handle_response(response);
if (typeof callback == "function") {
callback(res.content);
}
})
}
/**
* 从cookie中读取信息
*/
function _get_user_info_from_cookie() {
let data = tools._get_cookie("pl_info_" + _app_id);
if (tools._init_aes()) {
let infoStr = tools._aes_decrypt(data);
return infoStr ? JSON.parse(infoStr) : null
}
return null;
}
return weixin;
}
KD.biz = function () {
let biz = {};
let tools = KD._tools;
/**
* 统计接口
*
* @param {String} channelCode 渠道编号
* @param {String} statClassify 统计类型,可见统计表定义
* @param {String} statKey 统计标识,可见统计表定义
* @param {String} primaryKey 唯一标识,如用户标识、按钮标识
*/
biz.stat = function (channelCode, statClassify, statKey, primaryKey) {
let form = {
aid: _app_id,
cc: !channelCode || channelCode == undefined ? "" : channelCode,//渠道
sc: !statClassify || statClassify == undefined ? "" : statClassify,//统计类型
sk: !statKey || statKey == undefined ? "" : statKey,//统计key
pc: !primaryKey || primaryKey == undefined ? "" : primaryKey //用户标识
}
let headers = tools._init_headers();
tools._get(_request_url.stat, form, headers, function (response) { })
}
return biz;
}
})();