Browse Source

优化h5端授权登录流程

master
Gao xiaosong 5 years ago
parent
commit
0605a2b4c9
  1. 4
      config/index.js
  2. 23
      libs/wechat.js
  3. 26
      main.js
  4. 6
      pages/Loading/index.vue
  5. 1
      store/index.js
  6. 14
      utils/index.js
  7. 2
      utils/request.js

4
config/index.js

@ -1,8 +1,8 @@
// export const VUE_APP_API_URL = 'http://natapp.xinxintuan.co/api';
export const VUE_APP_API_URL = 'https://wxapi.yixiang.co/api';
// export const VUE_APP_API_URL = 'https://wxapi.yixiang.co/api';
// export const VUE_APP_API_URL = 'http://192.168.31.223:8008/api';
// export const VUE_APP_API_URL = 'https://h5api.xinxintuan.co/api';
export const VUE_APP_API_URL = 'https://h5api.xinxintuan.co/api';
// export const VUE_APP_API_URL = 'https://h5api.xinxintuan.co/api';
export const VUE_APP_RESOURCES_URL = 'https://wx.yixiang.co/static';

23
libs/wechat.js

@ -86,9 +86,11 @@ const LOGINTYPE = "loginType";
let instance;
let wechatObj;
let appId
let wechatLoading = false
export function wechat() {
export async function wechat() {
console.log('初始化微信配置')
wechatLoading = false
return new Promise((resolve, reject) => {
if (instance) return resolve(instance);
getWechatConfig()
@ -107,12 +109,12 @@ export function wechat() {
})
.catch(error => {
console.log(error)
reject(error)
reject()
});
})
.catch(err => {
console.log(err);
reject(err);
reject();
});
});
}
@ -122,7 +124,7 @@ export function clearAuthStatus() {
cookie.remove(STATE_KEY);
}
export function oAuth() {
export async function oAuth() {
console.log('处理微信授权')
console.log('处理微信授权cookie', cookie.get("spread"))
console.log(store)
@ -137,6 +139,8 @@ export function oAuth() {
if (!code) {
toAuth();
return
} else {
auth(code)
}
resolve()
}).catch(error => {
@ -167,6 +171,10 @@ export function auth(code) {
}
export function toAuth() {
if (wechatLoading) {
return
}
wechatLoading = true
wechat().then(wx => {
location.href = getAuthUrl(appId);
});
@ -179,9 +187,10 @@ function getAuthUrl(appId) {
// #ifdef H5
// #endif
const redirect_uri = encodeURIComponent(`${location.origin}/pages/Loading/index?path=${encodeURIComponent(window.location.href)}`);
debugger
cookie.set('redirect', window.location.href)
const redirect_uri = encodeURIComponent(`${location.origin}/pages/Loading/index`);
// const redirect_uri = encodeURIComponent(`${location.origin}/pages/Loading/index?path=${encodeURIComponent(window.location.href)}`);
// const redirect_uri = encodeURIComponent(`${window.location.origin}${window.location.pathname}`)
// const redirect_uri = encodeURIComponent(`${location.origin}`)
cookie.remove(BACK_URL);

26
main.js

@ -123,17 +123,6 @@ if (urlSpread !== undefined) {
}
}
// 判断是否是微信浏览器
if (isWeixin()) {
Vue.prototype.$deviceType = 'weixin'
store.commit('updateDevicetype', 'weixin')
wechat().then(() => oAuth());
} else {
Vue.prototype.$deviceType = 'weixinh5'
store.commit('updateDevicetype', 'weixinh5')
}
Vue.prototype.wechat = wechat
Vue.prototype.clearAuthStatus = clearAuthStatus
Vue.prototype.oAuth = oAuth
@ -148,6 +137,21 @@ Vue.prototype.wechatEvevt = wechatEvevt
Vue.prototype.ready = ready
Vue.prototype.wxShowLocation = wxShowLocation
// 判断是否是微信浏览器
if (isWeixin()) {
Vue.prototype.$deviceType = 'weixin'
store.commit('updateDevicetype', 'weixin')
let wechat = await wechat()
if (wechat) {
await oAuth()
}
} else {
Vue.prototype.$deviceType = 'weixinh5'
store.commit('updateDevicetype', 'weixinh5')
}
// #endif
// #ifdef APP-PLUS

6
pages/Loading/index.vue

@ -52,6 +52,8 @@
this.toLaunch();
return;
}
}
// cookie.get("spread");
// if (this.$deviceType == "weixin") {
// let path = parseQuery().path
@ -69,9 +71,8 @@
// return
// }
// this.toLaunch();
if (this.$deviceType == "app" || this.$deviceType == "h5") {
if (this.$deviceType == "app" || this.$deviceType == "weixinh5") {
// this.toLaunch();
this.$yrouter.switchTab({
path: "/pages/home/index"
});
@ -82,7 +83,6 @@
path: "/pages/home/index"
});
});
}
},
methods: {
...mapActions(["changeAuthorization", "setUserInfo"]),

1
store/index.js

@ -163,6 +163,7 @@ const vuexStore = new Vuex.Store({
location: state => state.location,
storeItems: state => state.storeItems,
goName: state => state.goName,
$deviceType: state => state.$deviceType,
},
strict: debug
});

14
utils/index.js

@ -204,17 +204,22 @@ export const authorize = (authorizeStr) => {
}
export const login = () => {
debugger;
console.log(Vue.prototype)
return new Promise((resolve, reject) => {
if (Vue.prototype.$deviceType == 'weixin') {
// 微信授权登录
const { code } = parseQuery()
debugger;
if (code) {
auth(code)
.then(() => {
// location.replace(
// decodeURIComponent(decodeURIComponent(this.$route.params.url))
// );
let redirect = cookie.get('redirect')
console.log(redirect)
debugger;
location.href = decodeURIComponent(
decodeURIComponent(this.$route.params.url)
);
@ -857,14 +862,19 @@ export const handleLoginFailure = () => {
console.log('————————')
store.commit("logout");
// 改为授权取消
store.commit("updateAuthorization", false);
let currentPageUrl = getCurrentPageUrl()
debugger
if (store.state.$deviceType == 'weixin') {
if (store.getters.isAuthorizationPage){
// 如果不是授权页面,
if (!store.getters.isAuthorizationPage) {
// 标识当前为授权页面
store.commit("updateAuthorizationPage", true);
toAuth()
}
return
} else {
// token 失效
// 判断当前是不是已经在登录页面或者授权页,防止二次跳转

2
utils/request.js

@ -12,7 +12,7 @@ import Fly from "flyio/dist/npm/wx";
import Fly from "flyio/dist/npm/wx";
// #endif
import $store from "../store";
import store from "../store";
import { handleLoginFailure } from "@/utils";
import { VUE_APP_API_URL } from "@/config";
import cookie from "@/utils/store/cookie";

Loading…
Cancel
Save