sunChengjie 11 months ago
parent
commit
f02b8c73b6
3 changed files with 61 additions and 20 deletions
  1. 13 19
      src/main.js
  2. 1 1
      src/pages/email.vue
  3. 47 0
      src/script/wxGetOpenId.js

+ 13 - 19
src/main.js

@@ -40,35 +40,20 @@ import 'swiper/swiper.min.css' //样式文件
 import wx from 'weixin-js-sdk'
 Vue.prototype.$wx = wx
 
-//Vue.prototype.common=common
-// Vue.http.options.emulateJSON = true;
-// Vue.http.options.headers = {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'};
-
-// Vue.http.interceptors.push((request, next) => {
-// next((response) => {}
-
-// })
-/* eslint-disable no-new */
-let overallExample = new Vue({
-	el: '#app',
-	router,
-	store: store,
-	components: { App },
-	template: '<App/>',
-
-})
+// import { getWxOpenId } from './script/wxGetOpenId.js'
+import { getWxOpenId } from './script/wxGetOpenId.js'
 const whiteList = ['/Login']
 router.beforeEach((to, from, next) => {
 	// if (to.matched.some(record => record.meta.needLogin)) { // 判断该路由是否需要登录权限
 	if (!whiteList.includes(to.path)) {
 		console.log('有登录权限')
-		//登录判断
 		if (!localStorage.getItem('userId')) { // 判断当前用户的登录信息userId是否存在
 			console.log('无userId')
 			next('/Login')
 		} else if (!localStorage.getItem('openid')) {
 			console.log('无openId')
-			overallExample.getOpenid()
+			// overallExample.getOpenid()
+			getWxOpenId()
 			console.log('overallExample.getOpenid()走完了')
 		} else {
 			console.log('什么都有')
@@ -80,3 +65,12 @@ router.beforeEach((to, from, next) => {
 	}
 })
 
+
+new Vue({
+	el: '#app',
+	router,
+	store: store,
+	components: { App },
+	template: '<App/>',
+
+})

+ 1 - 1
src/pages/email.vue

@@ -24,7 +24,7 @@ export default {
 	created() {
 		console.log('走开头了============')
 		// 微信网页登录
-		this.getOpenid()
+		// this.getOpenid()
 	},
 	mounted() {
 		this.getUser()

+ 47 - 0
src/script/wxGetOpenId.js

@@ -0,0 +1,47 @@
+import { getWxUserInfo, getWxConfig } from '../api/login'
+
+export async function getWxOpenId() {
+    if (localStorage.getItem("openid") != 'undefined' && localStorage.getItem("openid") != null && localStorage.getItem("openid") != '') {
+        return;
+    }
+
+    var code = getUrlParam("code");
+    console.log(code, 'code====++++')
+    if (code == 'undefined' || code == null || code == "") {
+        // alert("参数异常,请返回首页重试");
+        urlredirect();
+        return;
+    }
+
+    const res = await getWxUserInfo({ code: code, userId: localStorage.getItem("userId") })
+    console.log(res, '================================getWxUserInfo')
+    if (res.code == '0') {
+        console.log('直接走+======', res)
+        localStorage.setItem("openid", res.openId);
+        localStorage.setItem("headimgurl", res.headimgurl);
+    } else {
+        urlredirect();
+    }
+}
+
+
+async function urlredirect() {
+    const res = await getWxConfig({})
+    const href = window.location.href
+    window.location.href =
+        `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${res.wxConfig.appId}&redirect_uri=${encodeURIComponent(href)}` + `&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect`;
+}
+
+function getUrlParam(name) {
+    var vars = [],
+        hash;
+    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
+    for (var i = 0; i < hashes.length; i++) {
+        hash = hashes[i].split('=');
+        vars.push(hash[0]);
+        vars[hash[0]] = hash[1];
+    }
+    return decodeURIComponent(vars[name]);
+}
+
+