app.js
4.6 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
const appId = 'wx04262fa98a7de5b6';
App({
onLaunch: function() {
var that = this;
console.log("onlaunch---", "onlaunch");
//调用API从本地缓存中获取数据
var logs = wx.getStorageSync('logs') || []
logs.unshift(Date.now())
wx.setStorageSync('logs', logs)
wx.login({
success: function(res) {
if (res.code) {
var requestdata = {
"grant_type": "wx_app_code",
"code": res.code,
"scope": "global_access:end_user,tenant:" + appId
};
that.getTokenByCode(requestdata, true, "")
}
}
})
},
onShow: function(res) {
console.log("onShow----", res);
var that = this;
that.globalData.onshow_count++;
if (that.globalData.onshow_count > 1) { //除第一次进入小程序之外的打开时
if (res.path == "pages/main/smartchat/chat") { //聊天页面锁屏再打开,走该逻辑
var requestdata = {
"grant_type": "refresh_token",
"refresh_token": that.globalData.refresh_token,
"scope": "global_access:end_user,tenant:" + that.globalData.com_id
};
that.getTokenByCode(requestdata, false, "")
} else {
wx.login({
success: function(res) {
if (res.code) {
var requestdata = {
"grant_type": "wx_app_code",
"code": res.code,
"scope": "global_access:end_user,tenant:" + appId
};
that.getTokenByCode(requestdata, false, "chat")
}
}
})
}
}
},
getTokenByCode: function(requestdata, relaunch, str) {
var that = this;
var baseUrl = that.globalData.baseUrl;
wx.request({
url: baseUrl + 'uaa/v1/auth/tokens',
data: requestdata,
method: "POST",
header: {
'content-type': 'application/json'
},
success: function(res) {
console.log("getTokenByCode", res.data)
var user = res.data.user;
var tenant = res.data.tenant;
if (tenant) {
that.globalData.tenant_id = tenant.id;
}
that.globalData.userInfo = res.data.user;
that.globalData.wx_open_id = user.wx_open_id;
that.globalData.Authorization = "Bearer " + res.data.access_token;
that.globalData.refresh_token = res.data.refresh_token;
if (user && user.type == "0") { //匿名用户
wx.redirectTo({
url: '../../getPhone/getPhone'
})
} else if (user && user.type == "1") { //注册用户
that.configOssUrl()
if (relaunch) {
wx.reLaunch({
url: '../guide/guide'
})
}
}
},
fail: function(res) {
console.log("login-res-fail", res);
},
<<<<<<< HEAD
complete: function(res) {
=======
complete:function(res){
>>>>>>> dev_无非居民
}
})
},
getUserInfo: function(cb) {
var that = this
if (this.globalData.userInfo) {
typeof cb == "function" && cb(this.globalData.userInfo)
} else {
//调用登录接口
wx.getUserInfo({
withCredentials: false,
success: function(res) {
console.log("userInfo---", res)
that.globalData.userInfo = res.userInfo
typeof cb == "function" && cb(that.globalData.userInfo)
}
})
}
},
configOssUrl: function() {
wx.showLoading()
var that = this
wx.request({
url: this.globalData.baseUrl + "filemeta/v1/config",
header: {
'Authorization': this.globalData.Authorization
},
method: 'GET',
success: function(result) {
console.log('获取OSSURL 成功', result.data)
that.globalData.OSSUrl = result.data.app_url;
},
fail: function(res) {
console.log('获取OSSURL 失败', res)
wx.showToast({
icon: "none",
title: "云存储功能失效,请联系开发人员",
})
},
complete: function() {
wx.hideLoading()
}
})
},
globalData: {
appId: appId,
com_id: "",
onshow_count: 0,
// baseUrl: "http://47.99.47.16:20000/",
// baseUrl: "http://47.96.75.229:20000/",
// baseUrl: "http://192.144.144.220:20000/",
// baseUrl: "http://154.8.229.55:20000/",
// baseUrl: "http://47.110.250.177:20000/",
// baseUrl: "http://47.110.158.110:20000/",
baseUrl: "https://api.fjhxrl.com/",
OSSUrl: "", //"https://oss.workai.com.cn/",
userInfo: null,
hostInfo: null,
hasLogin: false,
openid: null,
Authorization: null,
refresh_token: "",
tenant_id: "",
wx_open_id: "",
newComerOrder: null
}
})