authorization.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
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
import {
request
} from "../../request/index.js"
const a = getApp()
Page({
data: {
imagesUrl: a.globalData.baseUrl,
code: '',
encryptedData: '',
iv: '',
msg: '',
num: 0
},
onReady: function () {
},
onLoad: function (options) {
console.log(options);
let that = this
that.setData({
num: options.num || ''
})
},
onShow: function () {},
// 立即授权
bindGetUserInfo(e) {
let that = this;
console.log(e);
if (e.detail.errMsg == "getUserInfo:ok") {
that.setData({
encryptedData: e.detail.encryptedData,
iv: e.detail.iv
})
}
wx.login({
success(res) {
console.log(res);
that.setData({
code: res.code
})
request({
url: 'api/common/login',
method: "POST",
data: {
rawData: e.detail.rawData,
code: that.data.code,
iv: that.data.iv,
encryptedData: that.data.encryptedData,
}
})
.then(res => {
console.log(res);
if (res.data.code == 1) {
wx.setStorageSync('token', res.data.data.userInfo.token);
wx.setStorageSync('avatar', res.data.data.userInfo.avatar);
wx.setStorageSync('name', res.data.data.userInfo.nickname);
wx.setStorageSync('user_id', res.data.data.userInfo.user_id);
that.showModal();
}
}).catch(err => {
console.log(err);
})
}
})
},
getPhoneNumber(e) { //手机号授权
let that = this
if (e.detail.errMsg == "getPhoneNumber:ok") {
that.setData({
encryptedData: e.detail.encryptedData,
iv: e.detail.iv
})
that.getPhoneCall()
} else {
that.setData({
msg: '重新进行手机号授权'
})
that.popTest()
}
},
async getPhoneCall() {
let that = this
const {
data
} = await request({
url: 'api/common/savePhone',
data: {
encryptedData: that.data.encryptedData,
iv: that.data.iv
}
})
that.setData({
msg: data.msg
})
that.popSuccessTest();
setTimeout(function () {
if (that.data.num == 1) {
wx.navigateBack()
} else {
wx.navigateTo({
url: '/pages/already/already'
})
}
}, 1100)
},
//显示对话框
showModal() {
// 显示遮罩层
var animation = wx.createAnimation({
duration: 200,
timingFunction: "linear",
delay: 0
})
this.animation = animation
animation.translateY(300).step()
this.setData({
animationData: animation.export(),
showModalStatus: true
})
setTimeout(function () {
animation.translateY(0).step()
this.setData({
animationData: animation.export()
})
}.bind(this), 100)
},
//隐藏对话框
hideModal() {
// 隐藏遮罩层
var animation = wx.createAnimation({
duration: 200,
timingFunction: "linear",
delay: 0
})
this.animation = animation
animation.translateY(300).step()
this.setData({
animationData: animation.export(),
})
setTimeout(function () {
animation.translateY(0).step()
this.setData({
animationData: animation.export(),
showModalStatus: false
})
}.bind(this), 100)
},
popTest() {
wx.showToast({
title: this.data.msg,
icon: 'none', //如果要纯文本,不要icon,将值设为'none'
duration: 1300
})
},
popSuccessTest() {
wx.showToast({
title: this.data.msg,
icon: '', //默认值是success,就算没有icon这个值,就算有其他值最终也显示success
duration: 1300, //停留时间
})
},
})