高晟悦 1 місяць тому
батько
коміт
d89eaad431
4 змінених файлів з 235 додано та 25 видалено
  1. 27 1
      App.vue
  2. 2 0
      hook/index.ts
  3. 157 0
      hook/use-small-program-login/index.ts
  4. 49 24
      pagesCrm/business/home/home.vue

+ 27 - 1
App.vue

@@ -7,10 +7,12 @@ import {
 	useAppStatus,
 	useGetUpdateManager,
 	useSmallProgramLogin,
+	useSmallProgramLoginDiy,
 	useGetSysAppPageList,
 	usePreserMember,
 	useSetFrontEndForm,
 	useSetMenuList,
+	useGetMemberIdByEncryptData,
 } from './hook';
 import { nextTick } from 'vue';
 
@@ -55,8 +57,10 @@ const main = async function () {
 	// 同步获取设备信息
 	app.globalData.smallPro_systemInfo = JSON.stringify(uni.getSystemInfoSync());
 	
-	const resp = await useSmallProgramLogin(app);
+	// const resp = await useSmallProgramLogin(app);
+	const resp = await useSmallProgramLoginDiy(app);
 	if (resp) {
+		// await getEncryptData();
 		app.globalData.logSuccess = true;
 		// await useGetSysAppPageList();
 		// await usePreserMember();
@@ -88,6 +92,28 @@ const main = async function () {
 	}
 };
 
+/** 解密 */
+const getEncryptData = async () => {
+	// 只在 H5/WEB 端解析 URL 上的 EncryptData,兼容外部跳转进来的完整地址
+	let encryptData = '';
+	// #ifdef H5 || WEB
+	try {
+		// 例: https://fzsclqyy.com/KasiteWeb/module/hcrmUserCenter/index.html?EncryptData=xxx&token=xxx&openid=xxx#/
+		// 先截掉 hash 部分,再解析 ? 后面的查询参数
+		const url = (typeof window !== 'undefined' && window.location && window.location.href) ? window.location.href : '';
+		const queryPart = url.split('?')[1]?.split('#')[0] || '';
+		if (queryPart) {
+			const params = new URLSearchParams(queryPart);
+			encryptData = params.get('EncryptData') || '';
+		}
+	} catch (e) {
+		console.error('解析 EncryptData 失败', e);
+	}
+	// #endif
+	
+	await useGetMemberIdByEncryptData(encryptData);
+}
+
 export default {
 	globalData: GLOBALDATA,
 	onLaunch: function (options) {

+ 2 - 0
hook/index.ts

@@ -5,3 +5,5 @@ export * from './use-on-load';
 
 export * from './use-menu-click';
 export * from './use-get-memberId-by-encrypt';
+
+export * from "./use-small-program-login";

+ 157 - 0
hook/use-small-program-login/index.ts

@@ -0,0 +1,157 @@
+import { REQUEST_CONFIG } from '@kasite/uni-app-base/config';
+import { WebOauthAuthorize } from '@kasite/uni-app-base/service/base';
+import { useDomain } from '@kasite/uni-app-base/hook/system/use-domain';
+import store from '@kasite/uni-app-base/store';
+
+/** 缓存是否过期 */
+export const useIsExpirationDiy = async () => {
+	// 当前时间
+	var timestamp = Date.parse(new Date());
+	// 缓存中的过期时间
+	var data_expiration = uni.getStorageSync('data_expiration');
+	// 如果缓存中没有data_expiration,说明也没有token,还未登录
+	if (data_expiration) {
+		// 如果超时了,清除缓存,重新登录
+		if (timestamp > data_expiration) {
+			uni.setStorageSync('data_expiration', 0);
+			return false;
+		} else {
+			return true;
+		}
+	}
+	return false;
+};
+
+/** 小程序登录 */
+export const useSmallProgramLoginDiy = async (app: App.AppInstance) => {
+	let res;
+	// #ifdef MP-WEIXIN
+	res = await smallProgramLoginByMPWEIXINDiy(app);
+	// #endif
+
+	// #ifdef MP-GONGZHONGHAO
+	res = await smallProgramLoginByGONGZHONGHAODiy(app);
+	// #endif
+
+	return res;
+};
+
+const smallProgramLoginByMPWEIXINDiy = async (app: App.AppInstance) => {
+	return new Promise(async (resolve, reject) => {
+		// 判断当前token是否过期(存在且没有过期直接返回true)不进行反复登录请求
+		if ((await useIsExpirationDiy()) && uni.getStorageSync('token')) {
+			resolve(true);
+			return;
+		}
+		uni.login({
+			success(res) {
+				uni.request({
+					url:
+						REQUEST_CONFIG.BASE_URL +
+						'wsgw/' +
+						app.globalData.channelId +
+						'/' +
+						app.globalData.configKey +
+						'/' +
+						app.globalData.hosId +
+						'/smallProgramLogin_v2.do?cfgKey=' +
+						app.globalData.wechatConfigKey,
+					method: 'POST',
+					data: {
+						appId: app.globalData.appId,
+						smallPro_authCode: res.code,
+						smallPro_systemInfo: app.globalData.smallPro_systemInfo,
+						smallPro_version: app.globalData.smallPro_version,
+					},
+					header: {
+						'content-type': 'application/x-www-form-urlencoded',
+					},
+					success(resp: any) {
+						if (resp.data.RespCode == '10000') {
+							store.commit('setToken', resp.data.token);
+							store.commit('setOpenId', resp.data.openId);
+							store.commit('setUnionId', resp.data.unionId);
+							store.commit('setSmallProOpenId', resp.data.smallProOpenId);
+							store.commit('setWechatOpenId', resp.data.wechatOpenid);
+							uni.setStorageSync('isCall', 0);
+
+							// publicFn.preserMember();
+							// 当前时间
+							var timestamp = Date.parse(new Date());
+							// 加上过期期限
+							var expiration = timestamp + resp.data.expireTime;
+							uni.setStorageSync('data_expiration', expiration);
+							resolve(resp);
+						} else {
+							if (
+								resp.data.RespCode == '-14019' &&
+								resp.data.RespMessage &&
+								resp.data.RespMessage.indexOf('非白名单用户') >= 0
+							) {
+								uni.showLoading({
+									title: '系统升级中...',
+									mask: true,
+								});
+							} else {
+								uni.showLoading({
+									title: resp.data.RespMessage || '网络异常!',
+									mask: true,
+								});
+							}
+							resolve(false);
+						}
+					},
+					fail(error) {
+						uni.showLoading({
+							title: '网络异常!',
+							mask: true,
+						});
+						reject(error);
+					},
+				});
+			},
+			fail(error) {
+				uni.showLoading({
+					title: '网络异常!',
+					mask: true,
+				});
+				reject(error);
+			},
+		});
+	});
+};
+
+const smallProgramLoginByGONGZHONGHAODiy = (app: App.AppInstance) => {
+	return new Promise(async (resolve, reject) => {
+		// if ((await useIsExpirationDiy()) && uni.getStorageSync('token')) {
+		// 	resolve(true);
+		// 	if (!store.state.token) {
+		// 		store.commit('setToken', uni.getStorageSync('token'));
+		// 	}
+		// 	return;
+		// }
+		const token = new URL(location.href).searchParams.get('token');
+		const openid = new URL(location.href).searchParams.get('openid');
+		console.log(token);
+		const { channelId, configKey, hosId, wechatConfigKey } = app.globalData;
+		if (!token) {
+			// 没有授权登录过的,需要重定向到授权页面
+			const url = WebOauthAuthorize({
+				hosId,
+				wechatConfigKey,
+				toUrl: `${useDomain(true)}/#/`, //encodeURIComponent(`${useDomain(true)}/#/`),
+			});
+			location.href = url;
+		} else {
+			store.commit('setToken', token);
+			store.commit('setOpenId', openid);
+			// 当前时间
+			var timestamp = Date.parse(new Date());
+			// 加上过期期限
+			var expiration = timestamp + 24 * 60 * 60 * 1000;
+			uni.setStorageSync('data_expiration', expiration);
+			// location.href = `${useDomain(true)}/#/`;
+			resolve(true);
+		}
+	});
+};

+ 49 - 24
pagesCrm/business/home/home.vue

@@ -82,35 +82,44 @@ const { setCurrentUser } = mapMutations({
 });
 
 const main = (options) => {
-	console.log(options)
-	uni.setStorageSync('openid', options.openid);
 	/**默认查全部记录 */
 	refresh(options);
 };
 /** 刷新 */
 const refresh = async (options: any = {}) => {
-	await getEncryptData();
-	if (!currentUser.value) {
-		let [user = {}] = memberList.value.filter((item) => {
-			if (options.memberId) {
-				return item.memberId == options.memberId;
-			} else {
-				return !!item.userMemberList[0].isDefaultMember;
-			}
-		});
-		if (common.isEmpty(user)) user = memberList.value[0];
-		setCurrentUser(user);
-		cardInfo.value = user;
-		console.log(user);
-	} else {
-		cardInfo.value = currentUser.value;
+	try {
+		if(!currentUser.value) {
+			// common.showModal("getEncryptData - home - start")
+			await getEncryptData();
+			// common.showModal("getEncryptData - home - end")
+		}
+		
+		
+		// common.showModal("currentUser" + JSON.stringify(currentUser.value))
+		if (!currentUser.value) {
+			let [user = {}] = memberList.value.filter((item) => {
+				if (options.memberId) {
+					return item.memberId == options.memberId;
+				} else {
+					return !!item.userMemberList[0].isDefaultMember;
+				}
+			});
+			if (common.isEmpty(user)) user = memberList.value[0];
+			setCurrentUser(user);
+			cardInfo.value = user;
+			console.log(user);
+		} else {
+			cardInfo.value = currentUser.value;
+		}
+		
+		noMore.value = false;
+		getPatientExecPlanDeptList();
+		// getMemberChatList()
+		await getPatientExecPlanList();
+		uni.stopPullDownRefresh();
+	} catch (e) {
+		// outputInfo("[Error]", e.message)
 	}
-
-	noMore.value = false;
-	getPatientExecPlanDeptList();
-	// getMemberChatList()
-	await getPatientExecPlanList();
-	uni.stopPullDownRefresh();
 };
 /** 查询患者执行计划科室列表 */
 const getPatientExecPlanDeptList = async () => {
@@ -135,7 +144,7 @@ const getPatientExecPlanList = async (e = undefined) => {
 		DeptId: deptId.value,
 		Page: {
 			PIndex,
-			PSize: 10,
+			PSize: 100,
 		},
 	});
 	let data = e ? datelineData.value : {};
@@ -205,6 +214,22 @@ const getEncryptData = async () => {
 	await useGetMemberIdByEncryptData(encryptData);
 }
 
+/**
+ * 输出异常
+ */
+const outputInfo = (key, data) => {
+	if(key && !!!data) {
+		data = key;
+		key = "";
+	}
+	console.log(key, data)
+	const str = typeof(data) != "string" ? JSON.stringify(data) : data;
+	uni.showModal({
+		title: key,
+		content: str,
+	})
+}
+
 useOnLoad((options) => {
 	main(options);
 	// 兜底:监听事件总线的状态更新