1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <template>
- <view>
- <u-toast ref="uToast" />
- </view>
- </template>
- <script>
- import getUrlParams from "./../../utils/getUrlParams.js";
- export default {
- data() {
- return {
- backUrl: '',
- code: ''
- }
- },
- onLoad(page) {
- let local = window.location.href;
- let locationLocaturl = window.location.search;
- // 微信返回的回调地址
- let backUrl = local.split('backUrl=')[1]
- if (backUrl) {
- this.backUrl = decodeURIComponent(backUrl)
- }
- let code = getUrlParams(local, "code")
- if (code) {
- this.code = code
- this.handleGetWXInfo(this.code)
- } else {
- window.location.href =
- `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.config.wxAppid}&redirect_uri=${encodeURIComponent(local)}&response_type=code&scope=snsapi_userinfo&#wechat_redirect`
- }
- },
- methods: {
- // 通过code获取 openId等用户信息
- handleGetWXInfo(code) {
- uni.showLoading({
- title: '加载中'
- })
- this.$u.api.getWXInfo(code).then((res) => {
- if (res.code === 200) {
- this.$u.vuex('vuex_wxinfo', res.data);
- this.getToken(res.data.openId)
- } else {
- this.$refs.uToast.show({
- title: '获取用户信息失败!',
- type: 'error'
- });
- uni.hideLoading()
- }
- }).catch((err) => {
- this.$refs.uToast.show({
- title: '获取用户信息失败!',
- type: 'error'
- });
- uni.hideLoading()
- })
- },
- /**
- * 通过openId获取token
- * @param {Object} openId
- */
- getToken(openId) {
- this.$u.api.codeV2Api.sendSmsCodeV2api({ openId }).then(res => {
- if (res.code === 200) {
- if (res.data.needVerify) {
- localStorage.setItem('backUrl', this.backUrl)
- uni.navigateTo({
- url: '/pages/center/phoneLogin/phoneLogin'
- })
- } else {
- this.$u.vuex('vuex_user', res.data);
- this.$u.vuex('vuex_token', res.data.accessToken);
- this.$u.vuex('vuex_hasLogin', true);
- location.href = this.backUrl
- }
- uni.hideLoading()
- } else {
- this.$refs.uToast.show({
- title: res.msg || '获取用户信息失败!',
- type: 'error'
- });
- uni.hideLoading()
- }
- })
- }
- }
- }
- </script>
- <style>
- </style>
|