123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <template>
- <view>
- <u-toast ref="uToast" />
- </view>
- </template>
- <script>
- import getUrlParams from '@/utils/getUrlParams.js';
- export default {
- data() {
- return {
- backUrl: '',
- code: '',
- loginType: 2
- };
- },
- onLoad(page) {
- uni.showLoading({
- title: '登录中...',
- mask: true
- });
- let local = window.location.href;
- let locationLocaturl = window.location.search;
- // 微信返回的回调地址
- let backUrl = getUrlParams(local, 'backUrl');
- if (backUrl) {
- this.backUrl = decodeURIComponent(backUrl);
- }
- console.log('backUrl', decodeURIComponent(backUrl));
- let loginType = getUrlParams(local, 'loginType');
- if (loginType) {
- this.loginType = loginType;
- }
- console.log('loginType', loginType);
- let code = getUrlParams(locationLocaturl, 'code');
- console.log('code', code);
- if (code) {
- this.code = code;
- // 判断重复回调问题
- let wechatLoginKey = localStorage.getItem('wechatLoginKey');
- if (wechatLoginKey) {
- wechatLoginKey = JSON.parse(wechatLoginKey);
- if (code === wechatLoginKey.code) {
- this.getUserInfo(wechatLoginKey.openId);
- } else {
- // this.handleGetWXInfo(this.code);
- this.handleGetWxOpenId(this.code);
- }
- } else {
- // this.handleGetWXInfo(this.code);
- this.handleGetWxOpenId(this.code);
- }
- } else {
- // scope (snsapi_userinfo 获取用户基本信息 snsapi_base 只获取OpenId)
- location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?
- appid=${this.config.wxAppid}&
- redirect_uri=${encodeURIComponent(local)}&
- response_type=code&
- scope=snsapi_base&
- state=STATE#wechat_redirect`;
- }
- },
- methods: {
- // 通过code获取 openId等用户信息
- handleGetWXInfo(code) {
- this.$u.api
- .getWXInfo(code)
- .then((res) => {
- if (res.code === 200) {
- this.$u.vuex('vuex_wxinfo', res.data);
- localStorage.setItem(
- 'wechatLoginKey',
- JSON.stringify({
- code,
- openId: res.data.openId
- })
- );
- this.getUserInfo(res.data.openId);
- } else {
- uni.hideLoading();
- }
- })
- .catch((err) => {
- uni.hideLoading();
- });
- },
- /**
- * @description: 通过code获取OpenId
- * @param {*} code
- * @return {*}
- */
- handleGetWxOpenId(code) {
- this.$u.api
- .getWxOpenidApi({ code })
- .then((res) => {
- if (res.code === 200) {
- this.$u.vuex('vuex_wxinfo', res.data);
- localStorage.setItem(
- 'wechatLoginKey',
- JSON.stringify({
- code,
- openId: res.data.openId
- })
- );
- this.getUserInfo(res.data.openId);
- } else {
- uni.hideLoading();
- }
- })
- .catch((err) => {
- uni.hideLoading();
- });
- },
- /**
- * 通过openId获取token
- * @param {Object} openId
- */
- getToken(openId) {
- this.$u.api.codeV2Api.sendSmsCodeV2api({ openId }).then((res) => {
- if (res.code === 200) {
- this.$u.vuex('vuex_token', res.data.accessToken);
- this.$u.vuex('vuex_hasLogin', true);
- if (res.data.needVerify) {
- localStorage.setItem('backUrl', this.backUrl);
- this.$u.route({
- url: 'pages/center/phoneLogin/phoneLogin',
- type: 'reLaunch'
- });
- } else {
- this.$u.vuex('vuex_user', res.data);
- if (this.backUrl.indexOf('wechatLogin') > -1) {
- location.href = '/';
- } else {
- location.href = this.backUrl;
- }
- }
- uni.hideLoading();
- } else {
- this.$refs.uToast.show({
- title: res.msg || '获取用户信息失败!',
- type: 'error'
- });
- uni.hideLoading();
- }
- });
- },
- /**
- * @description:
- * @param {*} openId
- * @return {*}
- */
- async getUserInfo(openId) {
- try {
- const queryParams = {
- loginType: this.loginType,
- openid: openId,
- nickName: this?.vuex_wxinfo?.nickname,
- avatar: this?.vuex_wxinfo?.headImgUrl
- };
- const userInfo = await this.$u.api.userLoginApi.openidLoginApi({ ...queryParams });
- const { accessToken, needVerify } = userInfo.data;
- this.$u.vuex('vuex_token', accessToken);
- this.$u.vuex('vuex_hasLogin', true);
- if (needVerify) {
- localStorage.setItem('backUrl', this.backUrl);
- this.$u.route({
- url: 'pages/center/phoneLogin/phoneLogin',
- type: 'reLaunch'
- });
- } else {
- this.$u.vuex('vuex_user', userInfo.data);
- if (this.backUrl.indexOf('wechatLogin') > -1) {
- location.href = '/';
- } else {
- location.href = this.backUrl;
- }
- }
- uni.hideLoading();
- } catch (error) {
- this.$refs.uToast.show({
- title: '获取用户信息失败!',
- type: 'error'
- });
- uni.hideLoading();
- }
- }
- }
- };
- </script>
|