-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
95 lines (89 loc) · 2.78 KB
/
app.js
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
//app.js
App({
onLaunch: function () {
//调用API从本地缓存中获取数据
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
},
getUserInfo:function(cb){
var that = this
if(this.globalData.userInfo){
typeof cb == "function" && cb(this.globalData.userInfo)
}else{
//调用登录接口
wx.login({
success (res) {
console.log(res)
if (res.code) {
//发起网络请求
wx.request({
url: that.globalData.ip +'/wx/loginWithwxxcx',
method:'get',
data: {
code: res.code
},
success (resChangeSession) {
// 必须是在用户已经授权的情况下调用
console.log(resChangeSession)
that.globalData.userInfo = resChangeSession.data.data.loginUser.user
that.globalData.token = resChangeSession.data.data.token
wx.setStorageSync('user', resChangeSession.data.data.loginUser.user);
wx.setStorageSync('token', resChangeSession.data.data.token)
typeof cb == "function" && cb(resChangeSession.data.data.loginUser.user)
}
})
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
}
},
//arraybuffer转字符串
arrayBufferToString:function(arr){
if(typeof arr === 'string') {
return arr;
}
var dataview=new DataView(arr.data);
var ints=new Uint8Array(arr.data.byteLength);
for(var i=0;i<ints.length;i++){
ints[i]=dataview.getUint8(i);
}
arr=ints;
var str = '',
_arr = arr;
for(var i = 0; i < _arr.length; i++) {
var one = _arr[i].toString(2),
v = one.match(/^1+?(?=0)/);
if(v && one.length == 8) {
var bytesLength = v[0].length;
var store = _arr[i].toString(2).slice(7 - bytesLength);
for(var st = 1; st < bytesLength; st++) {
store += _arr[st + i].toString(2).slice(2);
}
str += String.fromCharCode(parseInt(store, 2));
i += bytesLength - 1;
} else {
str += String.fromCharCode(_arr[i]);
}
}
return str;
},
//格式化响应流数据
formatText(text){
text = text.replaceAll("\n","")
text = text.replaceAll("event:message","")
let arr = text.split("data:")
return arr
},
globalData:{
userInfo:null,
token: null,
answeredTime:0,
// ip:"https://"+"inner.luqingwu.top"+":80",
ip:"https://"+"inner.luqingwu.top"+":9099",
// ip:"https://"+"127.0.0.1"+":80",
// wsip:"ws://"+"192.168.1.173"+":80"
}
})