浏览代码

fix: 修复获取url跳转参数

chenyixian 2 月之前
父节点
当前提交
d77e1f0d79
共有 3 个文件被更改,包括 39 次插入6 次删除
  1. 8 6
      hook/system/use-small-program-login/index.ts
  2. 1 0
      service/index.ts
  3. 30 0
      service/location-search.ts

+ 8 - 6
hook/system/use-small-program-login/index.ts

@@ -2,6 +2,7 @@ import { REQUEST_CONFIG } from '../../../config';
 import { WebOauthAuthorize } from '../../../service/base';
 import { useDomain } from '../use-domain';
 import store from '../../../store';
+import LocationSearch from '../../../service/location-search'
 
 /** 缓存是否过期 */
 export const useIsExpiration = async () => {
@@ -130,17 +131,17 @@ const smallProgramLoginByGONGZHONGHAO = (app: App.AppInstance) => {
 			}
 			return;
 		}
-		const token = new URL(location.href).searchParams.get('token');
-		const openid = new URL(location.href).searchParams.get('openid');
-		console.log(token);
+		const search = new LocationSearch()
+		const { token, openid } = search.obj;
 		const { channelId, configKey, hosId, wechatConfigKey } = app.globalData;
 		if (!token) {
 			// 没有授权登录过的,需要重定向到授权页面
 			const url = WebOauthAuthorize({
 				hosId,
 				wechatConfigKey,
-				toUrl: `${useDomain(true)}/#/`, //encodeURIComponent(`${useDomain(true)}/#/`),
+				toUrl: `${useDomain(true)}${search.join2Str()}`, //encodeURIComponent(`${useDomain(true)}/#/`),
 			});
+			console.log(url)
 			location.href = url;
 		} else {
 			store.commit('setToken', token);
@@ -150,8 +151,9 @@ const smallProgramLoginByGONGZHONGHAO = (app: App.AppInstance) => {
 			// 加上过期期限
 			var expiration = timestamp + 24 * 60 * 60 * 1000;
 			uni.setStorageSync('data_expiration', expiration);
-			location.href = `${useDomain(true)}/#/`;
+			console.log(location.search)
+			location.href = `${useDomain(true)}${search.join2Str()}`;
 			resolve(true);
 		}
 	});
-};
+};

+ 1 - 0
service/index.ts

@@ -1,2 +1,3 @@
 export { default as request } from './request';
 export { default as handle } from './handle';
+export { default as LocationSearch } from './location-search';

+ 30 - 0
service/location-search.ts

@@ -0,0 +1,30 @@
+ class LocationSearch {
+	obj = {} as any;
+	constructor() {
+		this.obj = this.str2Obj()
+	}
+	
+	str2Obj() {
+		const obj = {} as any;
+		const searchArr = location.search.replace("?", "").split("&");
+		searchArr.map((str) => {
+			const [key, ...value] = str.split("=");
+			obj[key] = value.map((item) => {
+				return item ? item : "="
+			}).join("");
+		})
+		return obj;
+	}
+	
+	join2Str() {
+		const arr = [];
+		Object.keys(this.obj).map((key) => {
+			if(key != 'token' && key != 'openid') {
+				arr.push(`${key}=${this.obj[key]}`)
+			}
+		});
+		return arr.length ? `?${arr.join("&")}` : "";
+	}
+}
+ 
+export default LocationSearch