之前前端可以通过微信的接口获取openid,现在不允许了,所以把code传给后端后端反数据给我们
<template>
<view class="content yd-center">
<view class="phonebox">
<view class="u-demo-block__content" style=" width: 91vw;">
<u-input maxlength="11" placeholder="请输入手机号码" type="number" clearable color="#000" v-model="phone"
@blur="phoneblur" border="bottom">
<u--text text="手机号:" color="#6b6b6b" slot="prefix" size="18" margin="0 3px 0 0"></u--text>
<template slot="suffix">
<button class="Localnumber" type="default" open-type="getPhoneNumber"
@getphonenumber.stop="GetPhoneNumber">获取手机号</button>
</template>
</u-input>
</view>
<view class="u-demo-block__content" style="margin-top: 15px; width: 91vw;">
<u-input maxlength="6" type="number" placeholder="请输入验证码" color="#000" v-model="Scode" border="bottom">
<u--text text="验证码:" color="#6b6b6b" slot="prefix" size="18" margin="0 3px 0 0"></u--text>
<template slot="suffix">
<u-code ref="uCode" @change="codeChange" seconds="60" changeText="x秒重新获取"></u-code>
<view class="Codebox" @click="getCode">
{{tips}}
</view>
</template>
</u-input>
</view>
</view>
<view class="loginbtn yd-center" @click="phonelogin">登录</view>
<view class="weichatloginbtn yd-center" @click="weichatlogin">一键授权登录</view>
</view>
</template>
<script>
import {
login,
loginPhone,
sendCode,
encrypPhone,
getOpenId
} from '@/api/login.js'
import {
HTTP_REQUEST_URL
} from '@/common/config.js';
export default {
data() {
return {
tips: '',
phone: '',
Scode: '',
code: '',
sessionKey: '',
iv: '',
encryptedData: '',
};
},
onLoad() {
this.updateSessionKey()
},
methods: {
GetPhoneNumber(e) {
console.log(e)
uni.checkSession({
success: (_res) => {
if (e.detail.errMsg == 'getPhoneNumber:user deny') {
uni.showToast({
title: "您已拒绝!授权失败",
icon: "none"
})
} else {
this.iv = e.detail.iv;
this.encryptedData = e.detail.encryptedData;
this.getencrypPhone()
}
},
fail: (_res) => {
console.log(_res)
uni.hideLoading()
this.updateSessionKey()
uni.showModal({
title: '提示',
content: '您的登录失效,请重新授权',
showCancel: false
})
}
})
},
getencrypPhone() {
let data = {
iv: this.iv,
encrypted: this.encryptedData,
sessionKey: this.sessionKey,
}
encrypPhone(data).then(res => {
let data = JSON.parse(res.data)
this.phone = data.phoneNumber
})
.catch(res => {
uni.hideLoading();
uni.$u.toast(res.msg);
})
},
updateSessionKey() {
this.getLoginCode()
},
getLoginCode() {
uni.login({
provider: 'weixin',
success: async (res) => {
this.code = res.code;
this.jsCode2OpenId()
}
})
},
jsCode2OpenId() {
getOpenId({
code: this.code
}).then(res => {
let info = JSON.parse(res.data)
this.openid = info.openid;
this.sessionKey = info.session_key
})
},
phonelogin() {
if (this.phone == '') {
uni.showToast({
title: '请填写电话号码',
icon: 'error',
duration: 2000
});
return
} else if (this.Scode == '') {
uni.showToast({
title: '请输入验证码',
icon: 'error',
duration: 2000
});
return
}
loginPhone({
phone: this.phone,
code: this.Scode
}).then(res => {
if (res.code == 0) {
uni.setStorageSync('token', res.data.token)
uni.setStorageSync('phone', res.data.loginName)
uni.reLaunch({
url: '/pages/index/index'
})
}
})
},
weichatlogin() {
let that = this;
let token = uni.getStorageSync('token');
if (token) {
uni.redirectTo({
url: '/pages/index/index'
})
} else {
uni.getUserProfile({
lang: 'zh_CN',
desc: '获取你的昵称、头像、地区及性别',
success: (res) => {
uni.setStorageSync('userInfo', res.userInfo)
uni.getProvider({
service: 'oauth',
success: function(res) {
console.log(res.provider);
if (~res.provider.indexOf('weixin')) {
login({
openId: that
.openid
}).then(
res => {
if (res.code == 0) {
uni.setStorageSync('token', res.data.token)
uni.reLaunch({
url: '/pages/index/index'
})
}
});
}
}
});
}
});
}
},
phoneblur(e) {
let reg = /^[1][3,4,5,7,8][0-9]{9}$/
if (reg.test(Number(e)) == false) {
uni.showToast({
title: '请填写正确的电话号码',
icon: 'error',
duration: 2000
});
this.phone = ''
}
},
codeChange(text) {
this.tips = text;
},
getCode() {
if (this.phone == '') {
uni.showToast({
title: '请填写电话号码',
icon: 'error',
duration: 2000
});
return
}
if (this.$refs.uCode.canGetCode) {
uni.showLoading({
title: '正在获取验证码'
})
setTimeout(() => {
sendCode({
phone: this.phone
}).then(res => {
uni.hideLoading();
console.log(1, res)
uni.$u.toast('验证码已发送');
this.$refs.uCode.start();
})
.catch(res => {
uni.hideLoading();
uni.$u.toast(res.msg);
})
.finally(res => {
uni.hideLoading();
uni.$u.toast(res.msg);
})
}, 2000);
} else {
uni.$u.toast('倒计时结束后再发送');
}
},
change(e) {
console.log('change', e);
}
}
};
</script>
<style lang="scss" socped>
button {
margin: 0;
background-color: #ffffff;
border: none;
}
.content {
margin-bottom: 140rpx;
width: 100vw;
height: 100vh;
background-color: #ffffff;
}
.phonebox {
margin-bottom: 124rpx;
margin-top: -606rpx;
}
.loginbtn {
width: 92vw;
height: 86rpx;
background-color: #0092ff;
color: white;
border: 2rpx solid #0092ff;
border-radius: 10rpx;
margin: 20rpx auto;
font-size: 30rpx;
letter-spacing: 10rpx;
box-shadow: 0 0 10rpx #919191;
}
.weichatloginbtn {
width: 92vw;
height: 86rpx;
background-color: #ffffff;
color: #0092ff;
border: 2rpx solid #0092ff;
border-radius: 10rpx;
margin: 20rpx auto;
font-size: 30rpx;
letter-spacing: 10rpx;
box-shadow: 0 0 10rpx #919191;
}
.sysbox {
margin: 500rpx 0 30rpx;
color: #b5b5b5;
}
.icon-ydkjweixin {
color: #5ac725;
font-size: 83rpx;
}
</style>

- 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
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330