From 48e073d57375d6aec7530b3cbdc074df4c5355a8 Mon Sep 17 00:00:00 2001 From: Gaoxs <704041637@qq.com> Date: Wed, 19 Aug 2020 20:03:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=85=AC=E4=BC=97=E5=8F=B7?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=99=BB=E5=BD=95=E6=8E=88=E6=9D=83=E6=9C=AA?= =?UTF-8?q?=E4=BF=9D=E5=AD=98token=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/public.js | 2 +- libs/wechat.js | 54 ++++++++++++++++++++++++----------------- main.js | 31 ++++++++++++++++++++++- manifest.json | 7 ++++-- pages/Loading/index.vue | 6 ++--- pages/home/index.vue | 16 ++++++++++-- store/index.js | 2 +- utils/index.js | 35 +++++++++++++++++++++++--- 8 files changed, 117 insertions(+), 36 deletions(-) diff --git a/api/public.js b/api/public.js index 7ba92dd..b34f64b 100644 --- a/api/public.js +++ b/api/public.js @@ -63,7 +63,7 @@ export function getArticleDetails(id) { export function getWechatConfig() { return request.get( "/wechat/config", - { url: document.location.href }, + { url: location.href }, { login: false } ); } diff --git a/libs/wechat.js b/libs/wechat.js index d29c249..0fd4b9f 100644 --- a/libs/wechat.js +++ b/libs/wechat.js @@ -57,6 +57,7 @@ const BACK_URL = "login_back_url"; const LOGINTYPE = "loginType"; let instance; let wechatObj; +let appId export function wechat() { console.log('初始化微信配置') @@ -64,8 +65,10 @@ export function wechat() { if (instance) return resolve(instance); getWechatConfig() .then(res => { + console.log(res.data) const _wx = WechatJSSDK(res.data); console.log(_wx) + appId = res.data.appId wechatObj = _wx; _wx .initialize() @@ -74,7 +77,10 @@ export function wechat() { instance.initConfig = res.data; resolve(instance); }) - .catch(reject); + .catch(error => { + console.log(error) + reject(error) + }); }) .catch(err => { console.log(err); @@ -90,8 +96,11 @@ export function clearAuthStatus() { export function oAuth() { console.log('处理微信授权') + console.log(store) + console.log(store.state) 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() return; } @@ -101,6 +110,8 @@ export function oAuth() { return } resolve() + }).catch(error => { + console.log(error) }) } @@ -108,43 +119,42 @@ export function auth(code) { console.log('获取微信授权') return new Promise((resolve, reject) => { let loginType = cookie.get(LOGINTYPE); + debugger wechatAuth(code, parseInt(cookie.get("spread")), loginType) .then(({ data }) => { + console.log(data) const expires_time = dayjs(data.expires_time); 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.remove(STATE_KEY); - loginType && cookie.remove("loginType"); + loginType && cookie.remove(LOGINTYPE); + debugger resolve(); }) .catch(reject); - }); + }).catch(error => { + debugger + console.log(error) + }) } export function toAuth() { - wechat().then(wx => { - location.href = getAuthUrl(wx.initConfig.appId); - }); + // wechat().then(wx => { + location.href = getAuthUrl("wxc061dee8806ff712"); + // }); } function getAuthUrl(appId) { - const redirect_uri = encodeURIComponent( - `${location.origin}/auth/` + - encodeURIComponent( - encodeURIComponent( - cookie.has(BACK_URL) - ? cookie.get(BACK_URL) - : location.pathname + location.search - ) - ) - ); + const redirect_uri = encodeURIComponent(`${location.origin}/pages/Loading/index`); + // const redirect_uri = encodeURIComponent(`${location.origin}/pages/Loading/index`encodeURIComponent(encodeURIComponent(cookie.has(BACK_URL)? cookie.get(BACK_URL): location.pathname + location.search))); + // const redirect_uri = encodeURIComponent(`${window.location.origin}${window.location.pathname}`) + // const redirect_uri = encodeURIComponent(`${location.origin}`) cookie.remove(BACK_URL); - const state = encodeURIComponent( - ("" + Math.random()).split(".")[1] + "authorizestate" - ); + const state = 'STATE' + // const state = encodeURIComponent(("" + Math.random()).split(".")[1] + "authorizestate"); 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 = {}) { diff --git a/main.js b/main.js index 4f7a4d6..93c8e68 100644 --- a/main.js +++ b/main.js @@ -84,17 +84,46 @@ Vue.prototype.$VUE_APP_API_URL = VUE_APP_API_URL // #ifdef 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' // 判断是否是微信浏览器 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 +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 // #ifdef APP-PLUS diff --git a/manifest.json b/manifest.json index 0a74b03..05cfd3e 100644 --- a/manifest.json +++ b/manifest.json @@ -152,8 +152,11 @@ }, "h5" : { "title" : "yshop", - "devServer": { - "disableHostCheck": true + "devServer" : { + "disableHostCheck" : true + }, + "router" : { + "mode" : "history" } } } diff --git a/pages/Loading/index.vue b/pages/Loading/index.vue index 4592f4f..7171ff8 100644 --- a/pages/Loading/index.vue +++ b/pages/Loading/index.vue @@ -52,9 +52,9 @@ export default { return; } login().finally(() => { - this.$yrouter.switchTab({ - path: "/pages/home/index" - }); + // this.$yrouter.switchTab({ + // path: "/pages/home/index" + // }); }); }, methods: { diff --git a/pages/home/index.vue b/pages/home/index.vue index 845a150..2816c2f 100644 --- a/pages/home/index.vue +++ b/pages/home/index.vue @@ -146,7 +146,6 @@ mapMutations, mapActions } from 'vuex'; - import GoodList from '@/components/GoodList'; import PromotionGood from '@/components/PromotionGood'; import CouponWindow from '@/components/CouponWindow'; @@ -310,7 +309,20 @@ goGoodsPromotion() { 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() { uni.scanCode({ success: (res) => { diff --git a/store/index.js b/store/index.js index 37efa97..c502249 100644 --- a/store/index.js +++ b/store/index.js @@ -80,7 +80,7 @@ const vuexStore = new Vuex.Store({ actions: { getLocation({ state, commit }, force) { uni.getLocation({ - type: 'wgs84', + type: 'gcj02', success: function (res) { console.log(res) commit("setLocation", { diff --git a/utils/index.js b/utils/index.js index 19a1f2d..5f22473 100644 --- a/utils/index.js +++ b/utils/index.js @@ -10,7 +10,7 @@ import dayjs from "dayjs"; import cookie from "@/utils/store/cookie"; import stringify from "@/utils/querystring"; 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) { @@ -158,8 +158,8 @@ export const login = () => { return new Promise((resolve, reject) => { if (Vue.prototype.$deviceType == 'weixin') { // 微信授权登录 - wechat().then(() => oAuth().then((code) => { - // const { code } = parseQuery() + const { code } = parseQuery() + if (code) { auth(code) .then(() => { // location.replace( @@ -173,7 +173,34 @@ export const login = () => { reject('当前运行环境为微信浏览器') 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 } if (Vue.prototype.$deviceType == 'weixinh5') {