修改公众号微信登录授权未保存token的问题
This commit is contained in:
@ -63,7 +63,7 @@ export function getArticleDetails(id) {
|
|||||||
export function getWechatConfig() {
|
export function getWechatConfig() {
|
||||||
return request.get(
|
return request.get(
|
||||||
"/wechat/config",
|
"/wechat/config",
|
||||||
{ url: document.location.href },
|
{ url: location.href },
|
||||||
{ login: false }
|
{ login: false }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -57,6 +57,7 @@ const BACK_URL = "login_back_url";
|
|||||||
const LOGINTYPE = "loginType";
|
const LOGINTYPE = "loginType";
|
||||||
let instance;
|
let instance;
|
||||||
let wechatObj;
|
let wechatObj;
|
||||||
|
let appId
|
||||||
|
|
||||||
export function wechat() {
|
export function wechat() {
|
||||||
console.log('初始化微信配置')
|
console.log('初始化微信配置')
|
||||||
@ -64,8 +65,10 @@ export function wechat() {
|
|||||||
if (instance) return resolve(instance);
|
if (instance) return resolve(instance);
|
||||||
getWechatConfig()
|
getWechatConfig()
|
||||||
.then(res => {
|
.then(res => {
|
||||||
|
console.log(res.data)
|
||||||
const _wx = WechatJSSDK(res.data);
|
const _wx = WechatJSSDK(res.data);
|
||||||
console.log(_wx)
|
console.log(_wx)
|
||||||
|
appId = res.data.appId
|
||||||
wechatObj = _wx;
|
wechatObj = _wx;
|
||||||
_wx
|
_wx
|
||||||
.initialize()
|
.initialize()
|
||||||
@ -74,7 +77,10 @@ export function wechat() {
|
|||||||
instance.initConfig = res.data;
|
instance.initConfig = res.data;
|
||||||
resolve(instance);
|
resolve(instance);
|
||||||
})
|
})
|
||||||
.catch(reject);
|
.catch(error => {
|
||||||
|
console.log(error)
|
||||||
|
reject(error)
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
@ -90,8 +96,11 @@ export function clearAuthStatus() {
|
|||||||
|
|
||||||
export function oAuth() {
|
export function oAuth() {
|
||||||
console.log('处理微信授权')
|
console.log('处理微信授权')
|
||||||
|
console.log(store)
|
||||||
|
console.log(store.state)
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (cookie.has(WX_AUTH) && store.state.app.token) {
|
// if (cookie.has(WX_AUTH)) {
|
||||||
|
if (cookie.has(WX_AUTH) && store.state.token) {
|
||||||
reject()
|
reject()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -101,6 +110,8 @@ export function oAuth() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
resolve()
|
resolve()
|
||||||
|
}).catch(error => {
|
||||||
|
console.log(error)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,43 +119,42 @@ export function auth(code) {
|
|||||||
console.log('获取微信授权')
|
console.log('获取微信授权')
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let loginType = cookie.get(LOGINTYPE);
|
let loginType = cookie.get(LOGINTYPE);
|
||||||
|
debugger
|
||||||
wechatAuth(code, parseInt(cookie.get("spread")), loginType)
|
wechatAuth(code, parseInt(cookie.get("spread")), loginType)
|
||||||
.then(({ data }) => {
|
.then(({ data }) => {
|
||||||
|
console.log(data)
|
||||||
const expires_time = dayjs(data.expires_time);
|
const expires_time = dayjs(data.expires_time);
|
||||||
const newTime = Math.round(new Date() / 1000);
|
const newTime = Math.round(new Date() / 1000);
|
||||||
store.commit("LOGIN", data.token, expires_time - newTime);
|
store.commit("login", data.token, expires_time - newTime);
|
||||||
cookie.set(WX_AUTH, code, expires_time);
|
cookie.set(WX_AUTH, code, expires_time);
|
||||||
cookie.remove(STATE_KEY);
|
cookie.remove(STATE_KEY);
|
||||||
loginType && cookie.remove("loginType");
|
loginType && cookie.remove(LOGINTYPE);
|
||||||
|
debugger
|
||||||
resolve();
|
resolve();
|
||||||
})
|
})
|
||||||
.catch(reject);
|
.catch(reject);
|
||||||
});
|
}).catch(error => {
|
||||||
|
debugger
|
||||||
|
console.log(error)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function toAuth() {
|
export function toAuth() {
|
||||||
wechat().then(wx => {
|
// wechat().then(wx => {
|
||||||
location.href = getAuthUrl(wx.initConfig.appId);
|
location.href = getAuthUrl("wxc061dee8806ff712");
|
||||||
});
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAuthUrl(appId) {
|
function getAuthUrl(appId) {
|
||||||
const redirect_uri = encodeURIComponent(
|
const redirect_uri = encodeURIComponent(`${location.origin}/pages/Loading/index`);
|
||||||
`${location.origin}/auth/` +
|
// const redirect_uri = encodeURIComponent(`${location.origin}/pages/Loading/index`encodeURIComponent(encodeURIComponent(cookie.has(BACK_URL)? cookie.get(BACK_URL): location.pathname + location.search)));
|
||||||
encodeURIComponent(
|
// const redirect_uri = encodeURIComponent(`${window.location.origin}${window.location.pathname}`)
|
||||||
encodeURIComponent(
|
// const redirect_uri = encodeURIComponent(`${location.origin}`)
|
||||||
cookie.has(BACK_URL)
|
|
||||||
? cookie.get(BACK_URL)
|
|
||||||
: location.pathname + location.search
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
cookie.remove(BACK_URL);
|
cookie.remove(BACK_URL);
|
||||||
const state = encodeURIComponent(
|
const state = 'STATE'
|
||||||
("" + Math.random()).split(".")[1] + "authorizestate"
|
// const state = encodeURIComponent(("" + Math.random()).split(".")[1] + "authorizestate");
|
||||||
);
|
|
||||||
cookie.set(STATE_KEY, state);
|
cookie.set(STATE_KEY, state);
|
||||||
return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;
|
return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
|
||||||
}
|
}
|
||||||
|
|
||||||
function toPromise(fn, config = {}) {
|
function toPromise(fn, config = {}) {
|
||||||
|
31
main.js
31
main.js
@ -84,17 +84,46 @@ Vue.prototype.$VUE_APP_API_URL = VUE_APP_API_URL
|
|||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
// H5编译的代码
|
// H5编译的代码
|
||||||
|
|
||||||
import { wechat, oAuth } from '@/libs/wechat'
|
import {
|
||||||
|
wechat,
|
||||||
|
clearAuthStatus,
|
||||||
|
oAuth,
|
||||||
|
auth,
|
||||||
|
toAuth,
|
||||||
|
pay,
|
||||||
|
openAddress,
|
||||||
|
openShareAll,
|
||||||
|
openShareAppMessage,
|
||||||
|
openShareTimeline,
|
||||||
|
wechatEvevt,
|
||||||
|
ready,
|
||||||
|
wxShowLocation,
|
||||||
|
} from '@/libs/wechat'
|
||||||
import { isWeixin } from '@/utils'
|
import { isWeixin } from '@/utils'
|
||||||
// 判断是否是微信浏览器
|
// 判断是否是微信浏览器
|
||||||
if (isWeixin()) {
|
if (isWeixin()) {
|
||||||
Vue.prototype.$deviceType = 'weixin'
|
Vue.prototype.$deviceType = 'weixin'
|
||||||
store.commit('updateDevicetype', 'weixin')
|
store.commit('updateDevicetype', 'weixin')
|
||||||
|
// wechat().then(() => oAuth());
|
||||||
} else {
|
} else {
|
||||||
Vue.prototype.$deviceType = 'weixinh5'
|
Vue.prototype.$deviceType = 'weixinh5'
|
||||||
store.commit('updateDevicetype', 'weixinh5')
|
store.commit('updateDevicetype', 'weixinh5')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vue.prototype.wechat = wechat
|
||||||
|
Vue.prototype.clearAuthStatus = clearAuthStatus
|
||||||
|
Vue.prototype.oAuth = oAuth
|
||||||
|
Vue.prototype.auth = auth
|
||||||
|
Vue.prototype.toAuth = toAuth
|
||||||
|
Vue.prototype.pay = pay
|
||||||
|
Vue.prototype.openAddress = openAddress
|
||||||
|
Vue.prototype.openShareAll = openShareAll
|
||||||
|
Vue.prototype.openShareAppMessage = openShareAppMessage
|
||||||
|
Vue.prototype.openShareTimeline = openShareTimeline
|
||||||
|
Vue.prototype.wechatEvevt = wechatEvevt
|
||||||
|
Vue.prototype.ready = ready
|
||||||
|
Vue.prototype.wxShowLocation = wxShowLocation
|
||||||
|
|
||||||
// #endif
|
// #endif
|
||||||
|
|
||||||
// #ifdef APP-PLUS
|
// #ifdef APP-PLUS
|
||||||
|
@ -152,8 +152,11 @@
|
|||||||
},
|
},
|
||||||
"h5" : {
|
"h5" : {
|
||||||
"title" : "yshop",
|
"title" : "yshop",
|
||||||
"devServer": {
|
"devServer" : {
|
||||||
"disableHostCheck": true
|
"disableHostCheck" : true
|
||||||
|
},
|
||||||
|
"router" : {
|
||||||
|
"mode" : "history"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,9 +52,9 @@ export default {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
login().finally(() => {
|
login().finally(() => {
|
||||||
this.$yrouter.switchTab({
|
// this.$yrouter.switchTab({
|
||||||
path: "/pages/home/index"
|
// path: "/pages/home/index"
|
||||||
});
|
// });
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
@ -146,7 +146,6 @@
|
|||||||
mapMutations,
|
mapMutations,
|
||||||
mapActions
|
mapActions
|
||||||
} from 'vuex';
|
} from 'vuex';
|
||||||
|
|
||||||
import GoodList from '@/components/GoodList';
|
import GoodList from '@/components/GoodList';
|
||||||
import PromotionGood from '@/components/PromotionGood';
|
import PromotionGood from '@/components/PromotionGood';
|
||||||
import CouponWindow from '@/components/CouponWindow';
|
import CouponWindow from '@/components/CouponWindow';
|
||||||
@ -310,7 +309,20 @@
|
|||||||
goGoodsPromotion() {
|
goGoodsPromotion() {
|
||||||
this.$yrouter.push('/pages/shop/GoodsPromotion/index');
|
this.$yrouter.push('/pages/shop/GoodsPromotion/index');
|
||||||
},
|
},
|
||||||
setOpenShare: function() {},
|
setOpenShare: function() {
|
||||||
|
if (this.$deviceType == 'weixin') {
|
||||||
|
getShare().then(res => {
|
||||||
|
var data = res.data.data;
|
||||||
|
var configAppMessage = {
|
||||||
|
desc: data.synopsis,
|
||||||
|
title: data.title,
|
||||||
|
link: location.href,
|
||||||
|
imgUrl: data.img
|
||||||
|
};
|
||||||
|
this.openShareAll(configAppMessage);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
},
|
||||||
startQr: function() {
|
startQr: function() {
|
||||||
uni.scanCode({
|
uni.scanCode({
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
|
@ -80,7 +80,7 @@ const vuexStore = new Vuex.Store({
|
|||||||
actions: {
|
actions: {
|
||||||
getLocation({ state, commit }, force) {
|
getLocation({ state, commit }, force) {
|
||||||
uni.getLocation({
|
uni.getLocation({
|
||||||
type: 'wgs84',
|
type: 'gcj02',
|
||||||
success: function (res) {
|
success: function (res) {
|
||||||
console.log(res)
|
console.log(res)
|
||||||
commit("setLocation", {
|
commit("setLocation", {
|
||||||
|
@ -10,7 +10,7 @@ import dayjs from "dayjs";
|
|||||||
import cookie from "@/utils/store/cookie";
|
import cookie from "@/utils/store/cookie";
|
||||||
import stringify from "@/utils/querystring";
|
import stringify from "@/utils/querystring";
|
||||||
import { VUE_APP_API_URL } from "@/config";
|
import { VUE_APP_API_URL } from "@/config";
|
||||||
import { wechat, auth, oAuth } from '@/libs/wechat'
|
import { wechat, auth, oAuth, toAuth } from '@/libs/wechat'
|
||||||
|
|
||||||
|
|
||||||
export function dataFormat(time, option) {
|
export function dataFormat(time, option) {
|
||||||
@ -158,8 +158,8 @@ export const login = () => {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (Vue.prototype.$deviceType == 'weixin') {
|
if (Vue.prototype.$deviceType == 'weixin') {
|
||||||
// 微信授权登录
|
// 微信授权登录
|
||||||
wechat().then(() => oAuth().then((code) => {
|
const { code } = parseQuery()
|
||||||
// const { code } = parseQuery()
|
if (code) {
|
||||||
auth(code)
|
auth(code)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
// location.replace(
|
// location.replace(
|
||||||
@ -173,7 +173,34 @@ export const login = () => {
|
|||||||
reject('当前运行环境为微信浏览器')
|
reject('当前运行环境为微信浏览器')
|
||||||
location.replace("/pages/home/index");
|
location.replace("/pages/home/index");
|
||||||
});
|
});
|
||||||
}));
|
} else {
|
||||||
|
wechat().then(() => oAuth());
|
||||||
|
}
|
||||||
|
// if (!code) {
|
||||||
|
// toAuth("wxc061dee8806ff712")
|
||||||
|
// } else {
|
||||||
|
// // wechat().then(() => oAuth().then((code) => {
|
||||||
|
// // // const { code } = parseQuery()
|
||||||
|
// // debugger
|
||||||
|
// // auth(code)
|
||||||
|
// // .then(() => {
|
||||||
|
// // // location.replace(
|
||||||
|
// // // decodeURIComponent(decodeURIComponent(this.$route.params.url))
|
||||||
|
// // // );
|
||||||
|
// // debugger
|
||||||
|
// // location.href = decodeURIComponent(
|
||||||
|
// // decodeURIComponent(this.$route.params.url)
|
||||||
|
// // );
|
||||||
|
// // })
|
||||||
|
// // .catch(() => {
|
||||||
|
// // reject('当前运行环境为微信浏览器')
|
||||||
|
// // location.replace("/pages/home/index");
|
||||||
|
// // });
|
||||||
|
// // })).catch(error => {
|
||||||
|
// // console.log(error)
|
||||||
|
// // reject('自动登录失败')
|
||||||
|
// // });
|
||||||
|
// }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (Vue.prototype.$deviceType == 'weixinh5') {
|
if (Vue.prototype.$deviceType == 'weixinh5') {
|
||||||
|
Reference in New Issue
Block a user