From 0605a2b4c9833495f3d29579074ceb6698ffb088 Mon Sep 17 00:00:00 2001 From: Gao xiaosong <704041637@qq.com> Date: Tue, 22 Sep 2020 01:20:02 +0800 Subject: [PATCH 01/16] =?UTF-8?q?=E4=BC=98=E5=8C=96h5=E7=AB=AF=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E7=99=BB=E5=BD=95=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/index.js | 4 +-- libs/wechat.js | 23 ++++++++++++------ main.js | 44 ++++++++++++++++++--------------- pages/Loading/index.vue | 54 ++++++++++++++++++++--------------------- store/index.js | 3 ++- utils/index.js | 24 ++++++++++++------ utils/request.js | 2 +- 7 files changed, 89 insertions(+), 65 deletions(-) diff --git a/config/index.js b/config/index.js index c54667e..d2febdd 100644 --- a/config/index.js +++ b/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'; diff --git a/libs/wechat.js b/libs/wechat.js index 2e5146b..a252815 100644 --- a/libs/wechat.js +++ b/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); diff --git a/main.js b/main.js index 97c5680..e88baee 100644 --- a/main.js +++ b/main.js @@ -107,31 +107,20 @@ import { isWeixin } from '@/utils' const CACHE_KEY = "clear_0.0.1"; if (!cookie.has(CACHE_KEY)) { - cookie.clearAll(); - cookie.set(CACHE_KEY, 1); + cookie.clearAll(); + cookie.set(CACHE_KEY, 1); } var urlSpread = parseQuery()["spread"]; if (urlSpread !== undefined) { - var spread = cookie.get("spread"); - urlSpread = parseInt(urlSpread); - if (!Number.isNaN(urlSpread) && spread !== urlSpread) { - cookie.set("spread", urlSpread || 0); - } else if (spread === 0 || typeof spread !== "number") { - cookie.set("spread", urlSpread || 0); - } -} - - -// 判断是否是微信浏览器 -if (isWeixin()) { - Vue.prototype.$deviceType = 'weixin' - store.commit('updateDevicetype', 'weixin') - wechat().then(() => oAuth()); -} else { - Vue.prototype.$deviceType = 'weixinh5' - store.commit('updateDevicetype', 'weixinh5') + var spread = cookie.get("spread"); + urlSpread = parseInt(urlSpread); + if (!Number.isNaN(urlSpread) && spread !== urlSpread) { + cookie.set("spread", urlSpread || 0); + } else if (spread === 0 || typeof spread !== "number") { + cookie.set("spread", urlSpread || 0); + } } Vue.prototype.wechat = wechat @@ -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 diff --git a/pages/Loading/index.vue b/pages/Loading/index.vue index 7dedc8a..4c9d143 100644 --- a/pages/Loading/index.vue +++ b/pages/Loading/index.vue @@ -52,37 +52,37 @@ this.toLaunch(); return; } - // cookie.get("spread"); - // if (this.$deviceType == "weixin") { - // let path = parseQuery().path - // console.log(this) - // if (path) { - // this.$yrouter.push({ - // path - // }); - // } else { - // this.$yrouter.switchTab({ - // path: "/pages/home/index" - // }); - // } + } - // return - // } - // this.toLaunch(); - if (this.$deviceType == "app" || this.$deviceType == "h5") { - // this.toLaunch(); + // cookie.get("spread"); + // if (this.$deviceType == "weixin") { + // let path = parseQuery().path + // console.log(this) + // if (path) { + // this.$yrouter.push({ + // path + // }); + // } else { + // this.$yrouter.switchTab({ + // path: "/pages/home/index" + // }); + // } - this.$yrouter.switchTab({ - path: "/pages/home/index" - }); - return; - } - login().finally(() => { - this.$yrouter.switchTab({ - path: "/pages/home/index" - }); + // return + // } + // this.toLaunch(); + if (this.$deviceType == "app" || this.$deviceType == "weixinh5") { + // this.toLaunch(); + this.$yrouter.switchTab({ + path: "/pages/home/index" }); + return; } + login().finally(() => { + this.$yrouter.switchTab({ + path: "/pages/home/index" + }); + }); }, methods: { ...mapActions(["changeAuthorization", "setUserInfo"]), diff --git a/store/index.js b/store/index.js index 4c244eb..1279a98 100644 --- a/store/index.js +++ b/store/index.js @@ -138,7 +138,7 @@ const vuexStore = new Vuex.Store({ state, commit }, user) { - + commit("updateUserInfo", user); }, changeAuthorizationPage({ @@ -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 }); diff --git a/utils/index.js b/utils/index.js index e9cd650..5f738c0 100644 --- a/utils/index.js +++ b/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) ); @@ -275,7 +280,7 @@ export const login = () => { console.log('登录接口调用成功') console.log('开始检查用户信息授权') let code = loginRes.code; - cookie.set('wxLoginCode',loginRes.code) + cookie.set('wxLoginCode', loginRes.code) // 检查授权, 检查用户信息授权 authorize('userInfo').then(() => { console.log('授权通过') @@ -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 失效 // 判断当前是不是已经在登录页面或者授权页,防止二次跳转 @@ -895,7 +905,7 @@ export const handleLoginFailure = () => { console.log('————————') if (qrCode.pinkId) { path = parseUrl({ - path: `/${currentPageUrl}`, + path: `/ ${currentPageUrl} `, query: { id: qrCode.pinkId, } @@ -917,7 +927,7 @@ export const handleLoginFailure = () => { if (qrCode.bargainId) { path = parseUrl({ - path: `/${currentPageUrl}`, + path: `/ ${currentPageUrl} `, query: { id: qrCode.bargainId, partake: qrCode.uid @@ -939,7 +949,7 @@ export const handleLoginFailure = () => { if (qrCode.productId) { path = parseUrl({ - path: `/${currentPageUrl}`, + path: `/ ${currentPageUrl} `, query: { id: qrCode.productId, } @@ -988,7 +998,7 @@ export function chooseImage(callback) { console.log(image); uni.showLoading({ title: "图片上传中", mask: true }); uni.uploadFile({ - url: `${VUE_APP_API_URL}/api/upload`, + url: `${VUE_APP_API_URL} /api/upload`, file: image, filePath: image.path, header: { diff --git a/utils/request.js b/utils/request.js index 3a732c1..0732c76 100644 --- a/utils/request.js +++ b/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"; From 59055982ab5f92c1dbec642332e36cee097c4a39 Mon Sep 17 00:00:00 2001 From: Gaoxs <704041637@qq.com> Date: Tue, 22 Sep 2020 17:32:13 +0800 Subject: [PATCH 02/16] =?UTF-8?q?=E4=BC=98=E5=8C=96h5=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E6=8E=88=E6=9D=83=E9=80=BB=E8=BE=91;=E4=BF=AE=E6=94=B9CountDow?= =?UTF-8?q?n=E7=BB=84=E4=BB=B6=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/CountDown.vue | 177 ++++++++++++++++++++------------------- libs/wechat.js | 7 +- main.js | 99 +++++++++++----------- utils/index.js | 21 +++-- 4 files changed, 154 insertions(+), 150 deletions(-) diff --git a/components/CountDown.vue b/components/CountDown.vue index 8ca5cff..1f61135 100644 --- a/components/CountDown.vue +++ b/components/CountDown.vue @@ -1,99 +1,104 @@ diff --git a/libs/wechat.js b/libs/wechat.js index a252815..5cad6b7 100644 --- a/libs/wechat.js +++ b/libs/wechat.js @@ -88,7 +88,7 @@ let wechatObj; let appId let wechatLoading = false -export async function wechat() { +export function wechat() { console.log('初始化微信配置') wechatLoading = false return new Promise((resolve, reject) => { @@ -148,7 +148,7 @@ export async function oAuth() { }) } -export function auth(code) { +export async function auth(code) { console.log('获取微信授权') return new Promise((resolve, reject) => { let loginType = cookie.get(LOGINTYPE); @@ -170,7 +170,7 @@ export function auth(code) { }) } -export function toAuth() { +export async function toAuth() { if (wechatLoading) { return } @@ -187,7 +187,6 @@ function getAuthUrl(appId) { // #ifdef H5 // #endif - 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)}`); diff --git a/main.js b/main.js index e88baee..23eb888 100644 --- a/main.js +++ b/main.js @@ -82,6 +82,29 @@ Object.defineProperty(Vue.prototype, '$yroute', { Vue.prototype.$VUE_APP_RESOURCES_URL = VUE_APP_RESOURCES_URL Vue.prototype.$VUE_APP_API_URL = VUE_APP_API_URL Vue.component('cu-custom', cuCustom); + + +// #ifdef APP-PLUS +// App平台编译的代码 +Vue.prototype.$deviceType = 'app' +store.commit('updateDevicetype', 'app') +Vue.prototype.$platform = uni.getSystemInfoSync().platform +// #endif + +// #ifdef MP-WEIXIN +// 微信小程序编译的代码 +Vue.prototype.$deviceType = 'routine' +store.commit('updateDevicetype', 'routine') +// #endif + +// !!! ps 不建议在 template 中使用 $deviceType 去判断当前环境,很有可能出现 $deviceType 为 undefined 导致判断出错的问题,可以在 script 模块中正常使用 +// 建议通过 store 去获取 $deviceType 可以保证 template 中取到的值有效 +// import { mapState, mapMutations, mapActions } from 'vuex'; +// computed: { +// ...mapState(['$deviceType']) +// }, + + // #ifdef H5 // H5编译的代码 @@ -102,8 +125,6 @@ import { } from '@/libs/wechat' import { isWeixin } from '@/utils' - - const CACHE_KEY = "clear_0.0.1"; if (!cookie.has(CACHE_KEY)) { @@ -122,56 +143,36 @@ if (urlSpread !== undefined) { cookie.set("spread", urlSpread || 0); } } +// #endif -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 - -// 判断是否是微信浏览器 -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') -} + +async function init() { + + // #ifdef H5 + // H5编译的代码 + // 判断是否是微信浏览器 + if (isWeixin()) { + Vue.prototype.$deviceType = 'weixin' + store.commit('updateDevicetype', 'weixin') + let wechatInit = await wechat() + console.log(wechatInit) + if (wechatInit) { + await oAuth() + app.$mount() + } + } else { + Vue.prototype.$deviceType = 'weixinh5' + store.commit('updateDevicetype', 'weixinh5') + app.$mount() + } + // #endif -// #endif + // #ifndef H5 + app.$mount() + // #endif -// #ifdef APP-PLUS -// App平台编译的代码 -Vue.prototype.$deviceType = 'app' -store.commit('updateDevicetype', 'app') -Vue.prototype.$platform = uni.getSystemInfoSync().platform -// #endif - -// #ifdef MP-WEIXIN -// 微信小程序编译的代码 -Vue.prototype.$deviceType = 'routine' -store.commit('updateDevicetype', 'routine') -// #endif - -// !!! ps 不建议在 template 中使用 $deviceType 去判断当前环境,很有可能出现 $deviceType 为 undefined 导致判断出错的问题,可以在 script 模块中正常使用 -// 建议通过 store 去获取 $deviceType 可以保证 template 中取到的值有效 -// import { mapState, mapMutations, mapActions } from 'vuex'; -// computed: { -// ...mapState(['$deviceType']) -// }, +} -app.$mount() +init() diff --git a/utils/index.js b/utils/index.js index 5f738c0..7bc8f02 100644 --- a/utils/index.js +++ b/utils/index.js @@ -204,25 +204,25 @@ 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) - ); + if (redirect) { + redirect = redirect.split('/pages')[1] + reLaunch({ + path: '/pages' + redirect, + }); + } else { + reLaunch({ + path: '/pages/home/index', + }); + } }) .catch(() => { reject('当前运行环境为微信浏览器') @@ -866,7 +866,6 @@ export const handleLoginFailure = () => { store.commit("updateAuthorization", false); let currentPageUrl = getCurrentPageUrl() - debugger if (store.state.$deviceType == 'weixin') { // 如果不是授权页面, if (!store.getters.isAuthorizationPage) { From c60e5fb5fdc0f61ff9c5fe689fc48832018d1c0d Mon Sep 17 00:00:00 2001 From: Gaoxs <704041637@qq.com> Date: Wed, 23 Sep 2020 18:15:45 +0800 Subject: [PATCH 03/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=86=E9=94=80?= =?UTF-8?q?=E5=92=8C=E5=95=86=E5=93=81=E8=AF=A6=E6=83=85=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E7=94=9F=E6=88=90=E5=85=B3=E7=B3=BB=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.js | 12 +----- pages/Loading/index.vue | 51 +++++++++++-------------- pages/activity/DargainDetails/index.vue | 19 ++++++--- pages/shop/GoodsCon/index.vue | 24 ++++++++---- store/index.js | 9 +---- utils/index.js | 34 ++++------------- utils/store/cookie.js | 11 +++++- 7 files changed, 71 insertions(+), 89 deletions(-) diff --git a/main.js b/main.js index 23eb888..66258c1 100644 --- a/main.js +++ b/main.js @@ -38,14 +38,6 @@ Vue.prototype.$validator = function (rule) { return new schema(rule); }; -// const CACHE_KEY = "clear_0.0.1"; - -// if (!cookie.has(CACHE_KEY)) { -// cookie.clearAll(); -// cookie.set(CACHE_KEY, 1); -// } - - Vue.config.productionTip = false App.mpType = 'app' Vue.prototype.$store = store @@ -128,7 +120,7 @@ import { isWeixin } from '@/utils' const CACHE_KEY = "clear_0.0.1"; if (!cookie.has(CACHE_KEY)) { - cookie.clearAll(); + cookie.clearAll() cookie.set(CACHE_KEY, 1); } @@ -146,7 +138,7 @@ if (urlSpread !== undefined) { // #endif - + async function init() { // #ifdef H5 diff --git a/pages/Loading/index.vue b/pages/Loading/index.vue index 4c9d143..578a2eb 100644 --- a/pages/Loading/index.vue +++ b/pages/Loading/index.vue @@ -48,36 +48,20 @@ } else if (spread === 0 || typeof spread !== "number") { cookie.set("spread", urlSpread || 0); } - if (this.$store.getters.token) { - this.toLaunch(); - return; - } } - - // cookie.get("spread"); - // if (this.$deviceType == "weixin") { - // let path = parseQuery().path - // console.log(this) - // if (path) { - // this.$yrouter.push({ - // path - // }); - // } else { - // this.$yrouter.switchTab({ - // path: "/pages/home/index" - // }); - // } - - // return + // if (this.$deviceType == "app" || this.$deviceType == "weixinh5") { + // this.$yrouter.switchTab({ + // path: "/pages/home/index" + // }); + // return; // } - // this.toLaunch(); - if (this.$deviceType == "app" || this.$deviceType == "weixinh5") { - // this.toLaunch(); - this.$yrouter.switchTab({ - path: "/pages/home/index" - }); + if (this.$store.getters.token) { + // 如果token存在,直接进行进页面 + console.log('登录状态存在,直接进页面') + this.toLaunch(); return; } + console.log('进行登录操作') login().finally(() => { this.$yrouter.switchTab({ path: "/pages/home/index" @@ -89,9 +73,18 @@ toLaunch() { console.log("loading home"); this.changeAuthorization(false); - this.$yrouter.switchTab({ - path: "/pages/home/index" - }); + let redirect = cookie.get('redirect') + if (redirect) { + redirect = redirect.split('/pages')[1] + this.$yrouter.replace({ + path: '/pages' + redirect, + }); + cookie.remove('redirect'); + } else { + this.$yrouter.replace({ + path: '/pages/home/index', + }); + } } } }; diff --git a/pages/activity/DargainDetails/index.vue b/pages/activity/DargainDetails/index.vue index d931b90..1e40300 100644 --- a/pages/activity/DargainDetails/index.vue +++ b/pages/activity/DargainDetails/index.vue @@ -1,10 +1,11 @@