_authCallback.vue
2.12 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
<template>
<div class="home">
<div class="logo ac">linkWeChat</div>
</div>
</template>
<script>
import { getQueryValue } from '@/utils/index'
import { getWechatUserInfo } from '@/api/common'
export default {
name: 'authCallback',
data() {
return {
keyword: '',
active: 0,
list: [],
loading: false,
finished: false,
show: false,
// userId: this.$store.state.userId,
}
},
created() {
alert(1)
this.getWxCode()
},
methods: {
getWxCode() {
try {
let appid = window.lwConfig.appId // 公众号appid
// let appid = 'wx8bfe6bc2ca5c45ae' // 公众号appid
alert(2)
let code = getQueryValue('code') //是否存在code
alert(3)
let local = window.location.origin.includes('localhost')
? 'http://h5.linkwechat.cn/test.html'
: window.location.href
// let local = 'http://h5.x*****o.com/'
alert(code)
if (!code) {
//不存在就打开上面的地址进行授权
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(
local
)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
// window.location.href =
// `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
} else {
this.getUserInfo(code)
}
} catch (error) {
alert(error)
this.$toast(JSON.stringify(error))
}
},
getUserInfo(code) {
getWechatUserInfo(code).then(({ data }) => {
if (data && data.openId) {
localStorage.setItem('userinfo', JSON.stringify(data))
alert(data.openId)
}
})
},
},
}
</script>
<style lang="less" scoped>
.logo {
margin: 0 auto;
width: 150px;
height: 50px;
line-height: 50px;
border: 2px solid #6582ff;
border-radius: 50px;
}
.router-link {
display: block;
text-align: center;
line-height: 40px;
}
</style>