app.js
3.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
//app.js
const appId = 'wxe91c67adba6ac02d';
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) {
that.getTokenByCode(res.code, true)
}
}
})
},
onShow: function (res) {
console.log("onShow----", res);
var that = this;
that.globalData.onshow_count++;
if (that.globalData.onshow_count > 1) {//除第一次进入小程序之外的打开时
wx.login({
success: function (res) {
if (res.code) {
that.getTokenByCode(res.code, false)
}
}
})
}
},
getTokenByCode: function (code, relaunch) {
var that = this;
var baseUrl = that.globalData.baseUrl;
wx.request({
url: baseUrl + 'uaa/v1/auth/tokens',
data: {
"grant_type": "wx_app_code",
"code": code,
"scope": "global_access:end_user,tenant:" + appId
},
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") {//注册用户
if (relaunch) {
// wx.reLaunch({ //重新加载
// url: '../home/home'
// })
wx.reLaunch({
url: '../guide/guide'
})
}
that.configOssUrl()
}
},
fail: function (res) {
console.log("login-res-fail", res);
},
})
},
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,
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: "https://api.workai.com.cn/",
OSSUrl: "",//"https://oss.workai.com.cn/",
userInfo: null,
hasLogin: false,
openid: null,
Authorization: null,
refresh_token: "",
tenant_id: "",
wx_open_id: "",
newComerOrder: null
}
})