login.js
4.0 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
//login.js
// const loginUrl = require('../../config').loginUrl;
//获取应用实例
var app = getApp();
var baseUrl = app.globalData.baseUrl;
var regMobile = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;
Page({
data: {
mobile: '',
verifyCode: "",
tenant_id: "",
wx_open_id: "",
verifyCodeTime: "获取验证码",
buttonDisable: false
},
mobileInputEvent: function (e) {
this.setData({
mobile: e.detail.value
});
},
verifyInputEvent: function (e) {
this.setData({
verifyCode: e.detail.value
});
console.log("verifyCode", this.data.verifyCode);
},
verifyCodeEvent: function () {
console.log("buttonDisable", this.data.buttonDisable);
if (this.data.buttonDisable) return;
var that = this;
var mobile = this.data.mobile;
console.log("mobile---", mobile);
if (!regMobile.test(mobile)) {
wx.showToast({
icon: 'none',
title: '手机号有误!'
})
return;
}
var timelength = 60
var intervalId = setInterval(function () {
timelength = timelength - 1;
that.setData({
verifyCodeTime: timelength + 's后重发',
buttonDisable: true
})
if (timelength == 0) {
clearInterval(intervalId);
that.setData({
verifyCodeTime: '获取验证码',
buttonDisable: false
})
}
}, 1000);
that.getVerifyCode(mobile);
},
getVerifyCode: function (mobile) {
var that = this;
var Authorization = app.globalData.Authorization;
that.setData({
tenant_id: app.globalData.tenant_id,
wx_open_id: app.globalData.wx_open_id
})
wx.request({
url: baseUrl + 'sms/v1/sms_codes',
method: "POST",
data: {
"mobile": mobile,
"type": "bindmobile",
"scope": "global_access:end_user"
},
header: {
'content-type': 'application/json', // 默认值
"Authorization": Authorization
},
success: function (res) {
console.log(res.data)
},
fail(res) {
console.log(res.data)
}
})
},
login: function () {
var that = this;
console.log("mobile---login", that.data.mobile);
if (!regMobile.test(that.data.mobile)) {
wx.showToast({
icon:'none',
title: '手机号有误!'
})
return;
}
var Authorization = getApp().globalData.Authorization;
wx.request({
url: baseUrl + "uaa/v1/users/op/bind-mobile",
data: {
"mobile": that.data.mobile,
"sms_code": that.data.verifyCode,
"wx_open_id": that.data.wx_open_id, //可选,如果传了,绑定到对应的微信号
"tenant_id": that.data.tenant_id,
"source": "wx_app",
"scope": "end_user",
"app_id": app.globalData.appId
},
method: "POST",
header: {
'content-type': 'application/json', // 默认值
"Authorization": Authorization
},
success: function (res) {
console.log("succ", res);
if (200 == res.statusCode) {
app.globalData.Authorization = "Bearer " + res.data.access_token;
app.globalData.refresh_token = res.data.refresh_token;
var user = res.data.user;
var tenant = res.data.tenant;
app.globalData.tenant_id = tenant.id;
app.globalData.userInfo = user;
app.globalData.real_auth_status = res.data.user.real_auth_status;
app.globalData.wx_open_id = user.wx_open_id;
app.configOssUrl()
wx.reLaunch({
url: '../main/guide/guide'
})
} else {
wx.showToast({
image: "/images/error.png",
title: '请重新登录',
})
}
},
fail: function (res) {
console.log("fail ", res.data)
},
complete: function (res) {
// wx.reLaunch({
// url: '../main/guide/guide'
// })
}
})
},
onLoad: function () {
}
})