weChatOffAcc.vue
4.56 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
<template>
<view class="container">
<div>微信公众号-授权认证中</div>
</view>
</template>
<script>
const {
Base64
} = require('js-base64');
export default {
data() {
return {
currentModeIndex: 2, //1账号密码 2验证码 3忘记密码
inputType: 'password',
codeDisabled: false,
codeTips: '',
formData: {
mobile: '',
password: '',
},
flag: true,
rules: {
mobile: [{
type: 'string',
required: true,
message: '请输入手机号',
trigger: ['blur', 'change']
},
// {
// // 自定义验证函数,见上说明
// validator: (rule, value, callback) => {
// // 上面有说,返回true表示校验通过,返回false表示不通过
// // uni.$u.test.mobile()就是返回true或者false的
// return uni.$u.test.mobile(value)
// },
// message: '手机号码不正确',
// // 触发器可以同时用blur和change
// trigger: ['change', 'blur']
// }
],
password: {
type: 'string',
min: 4,
max: 16,
required: true,
message: '密码长度4-16位密码',
trigger: ['blur', 'change']
},
code: {
type: 'integer',
len: 4,
required: true,
message: '请填写4位验证码',
trigger: ['blur', 'change']
}
},
isAutoLogin: [], //是否自动登录
hasForgot: false, //忘记密码
loginText: "短信登录",
btnText: "发送验证码",
checkValue: ['agree'],
accountTile: "短信验证码登录",
accountTip: "未注册手机号登录后自动生成账号",
accountPlaceholder: "请输入手机号",
appid: this.$config.appid,
openId: null
}
},
created() {
this.handleAuthorization();
},
methods: {
handleAuthorization() {
console.log("================================");
this.code = "";
console.log("window.location.href", window.location.href);
let local = window.location.href;
console.log("local", local);
let scope = "snsapi_base"; //静默授权 用户无感知
console.log("this.scope", scope);
this.code = this.getParameters('code');
console.log("this.code", this.code);
let jumpToUri =
`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.$config.appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=${scope}&state=hotent#wechat_redirect`
console.log("jumpToUri", jumpToUri);
// var redirect_uri = decodeURIComponent(this.getParameters("redirect"));
// console.log("redirect_uri", redirect_uri);
// if (redirect_uri) {} else {
// }
if (this.code == null || this.code === "") {
window.location.href = jumpToUri;
} else {
this.getOpenId(this.code);
}
console.log("this.code", this.code);
// this.getOpenId(this.code);
console.log("================================");
},
getOpenId(code) {
let that = this;
this.$api.loginApi.getWechatOpenId(code).then(res => {
console.log("res", res);
if (res.openid) {
that.openId = res.openid;
//首次登录未绑定账号去登录页登录后触发绑定
console.log("获取openId", that.openId);
uni.setStorageSync('openId', this.openId)
uni.navigateTo({
url:'/pages/login'
})
} else {
//已经绑定直接缓存token 跳转到主页
this.$store.dispatch('wxAuthLogin', res).then(e => {
uni.switchTab({
url: '/pages/home/home'
});
}).catch(err => {
uni.$u.toast(err)
})
}
});
},
getUrlCode() {
let fullUrl = window.location.href;
console.log("fullUrl ", fullUrl);
let theRequest = new Object();
if (fullUrl.indexOf("?") != -1) {
var temp = fullUrl.split("?");
let str = temp[1];
let strs = str.split("&");
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
}
}
return theRequest;
},
//获取url中的参数
getParameters(name) {
var locUrl = decodeURI(window.location.search.substr(1))
var aryParams = locUrl.split('&')
var rtn = ''
for (var i = 0; i < aryParams.length; i++) {
var pair = aryParams[i]
var aryEnt = pair.split('=')
var key = aryEnt[0]
var val = aryEnt[1]
if (key != name) continue
if (rtn == '') {
rtn = val
} else {
rtn += ',' + val
}
}
return rtn
},
},
};
</script>
<style lang="scss" scoped>
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
</style>