Browse Source

fix:微信公众号网页授权

sunChengjie 11 months ago
parent
commit
254ef36c12
4 changed files with 41 additions and 30 deletions
  1. 1 1
      src/App.vue
  2. 20 0
      src/api/login.js
  3. 4 0
      src/pages/email.vue
  4. 16 29
      src/script/common.js

+ 1 - 1
src/App.vue

@@ -28,7 +28,7 @@ export default {
 		// 	emulateJSON: true
 		// })
 
-		const res = await getWxConfig({ url: window.location.href.split('#')[0] })
+		const res = await getWxConfig({})
 
 		//发送成功
 		var timestamp = res.wxConfig.timestamp;

+ 20 - 0
src/api/login.js

@@ -0,0 +1,20 @@
+import request from '../utils/request'
+
+export function getWxUserInfo(data) {
+    return request({
+        url: '/tianzong/weixin/getWxUserInfo',
+        method: 'post',
+        data: data,
+        emulateJSON: true
+    })
+}
+
+// 获取appid
+export function getWxConfig(data) {
+    return request({
+        url: '/tianzong/weixin/getWxConfig',
+        method: 'post',
+        data: data,
+        emulateJSON: true
+    })
+}

+ 4 - 0
src/pages/email.vue

@@ -21,6 +21,10 @@ export default {
 			active: 0,
 		}
 	},
+	created() {
+		// 微信网页登录
+		this.getOpenid()
+	},
 	mounted() {
 		this.getUser()
 	},

+ 16 - 29
src/script/common.js

@@ -1,44 +1,31 @@
+import { getWxUserInfo, getWxConfig } from '../api/login'
 export default {
 	install(Vue) {
-		Vue.prototype.getOpenid = function (page) {
-			var host = 'http://47.101.145.23/tianzong';
+		Vue.prototype.getOpenid = async function () {
 			if (localStorage.getItem("openid") != 'undefined' && localStorage.getItem("openid") != null)
 				return;
 			var code = this.getUrlParam("code");
 			if (code == 'undefined' || code == null || code == "") {
-				//alert("参数异常,请返回首页重试");
-				this.urlredirect(page);
+				this.urlredirect();
 				return;
 			}
-			this.$http.post(host + "/weixin/getWxUserInfo", {
-				code: code,
-				userId: localStorage.getItem("userId")
-			}, {
-				emulateJSON: true
-			})
-				.then(res => {
-					//发送成功
-					console.log(res);
-
-					if (res.body.code == '0') {
-						localStorage.setItem("openid", res.body.openId);
-						localStorage.setItem("headimgurl", res.body.headimgurl);
-						//this.$store.commit("getId",res.body.data)
-					} else {
-						this.urlredirect(page);
-					}
-
-					//if()
-				}, res => {
-					//发送失败
-				})
+			const res = await getWxUserInfo({ code: code, userId: localStorage.getItem("userId") })
+			console.log(res, '================================getWxUserInfo')
+			if (res.body.code == '0') {
+				console.log('直接走+======', res)
+				localStorage.setItem("openid", res.body.openId);
+				localStorage.setItem("headimgurl", res.body.headimgurl);
+			} else {
+				this.urlredirect();
+			}
 
 		}
 
-		Vue.prototype.urlredirect = function (page) {
-			var host = 'http://47.101.145.23/wuliu/ljy/';
+		Vue.prototype.urlredirect = async function (page) {
+			const res = await getWxConfig({})
+			const href = window.location.href
 			window.location.href =
-				"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3d3b98cc26425f40&redirect_uri=" + host + page + "&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect";
+				`https://open.weixin.qq.com/connect/oauth2/authorize?appid=&redirect_uri=${res.wxConfig.appId}&redirect_uri=${encodeURIComponent(href)}` + "&response_type=code&scope=snsapi_userinfo&state=STATE&connect_redirect=1#wechat_redirect";
 		}
 		Vue.prototype.getUrlParam = function (name) {
 			var vars = [],