Jelajahi Sumber

feat: 把部分信息提取到store里

chenyixian 3 bulan lalu
induk
melakukan
6ffb64f3e3

+ 5 - 4
hook/use-preser-member/index.ts

@@ -1,5 +1,6 @@
 import { QueryBaseMemberList_V3 } from '../../service/base';
 import { common } from '../../utils';
+import store from '../../store';
 
 /** 查询并保存全局所有就诊人数据信息 (就诊人改变后都要重新调用,并保存一遍)*/
 export const usePreserMember = async () => {
@@ -7,16 +8,16 @@ export const usePreserMember = async () => {
 		isCache: false,
 		isEncrypt: true,
 		hosId: getApp().globalData.districtId || getApp().globalData.hosId,
-		openid: uni.getStorageSync('openid'),
+		openid: store.state.base.openId,
 	};
 	let { resp, resData } = await QueryBaseMemberList_V3(queryData);
 	// 请求成功且有返回值保存全局就诊人数据列表
-	getApp().globalData.currentUser = null;
+	store.commit('setCurrentUser', null);
 	if (resData.RespCode == 10000 && common.isNotEmpty(resp)) {
-		uni.setStorageSync('memberList', resp);
+		store.commit('setMemberList', resp);
 	} else {
 		// 否则保存空
-		uni.setStorageSync('memberList', null);
+		store.commit('setMemberList', null);
 	}
 	return resp;
 };

+ 11 - 12
hook/use-small-program-login/index.ts

@@ -1,6 +1,7 @@
 import { REQUEST_CONFIG } from '../../config';
 import { WebOauth2Authorize } from '../../service/base';
 import { useDomain } from '../use-domain';
+import store from '../../store';
 
 /** 缓存是否过期 */
 export const useIsExpiration = async () => {
@@ -67,12 +68,11 @@ const smallProgramLoginByMPWEIXIN = async (app: App.AppInstance) => {
 					},
 					success(resp: any) {
 						if (resp.data.RespCode == '10000') {
-							uni.setStorageSync('token', resp.data.token);
-							getApp().globalData.token = resp.data.token;
-							uni.setStorageSync('openid', resp.data.openId);
-							uni.setStorageSync('unionid', resp.data.unionId);
-							uni.setStorageSync('smallProOpenId', resp.data.smallProOpenId);
-							uni.setStorageSync('wechatOpenid', resp.data.wechatOpenid);
+							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();
@@ -154,12 +154,11 @@ const smallProgramLoginByGONGZHONGHAO = (app: App.AppInstance) => {
 				success(resp: any) {
 					console.log('1111', resp);
 					if (resp.data.RespCode == '10000') {
-						uni.setStorageSync('token', resp.data.token);
-						getApp().globalData.token = resp.data.token;
-						uni.setStorageSync('openid', resp.data.openId);
-						uni.setStorageSync('unionid', resp.data.unionId);
-						uni.setStorageSync('smallProOpenId', resp.data.smallProOpenId);
-						uni.setStorageSync('wechatOpenid', resp.data.wechatOpenid);
+						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();

+ 3 - 0
package.json

@@ -10,5 +10,8 @@
 	],
 	"publishConfig": {
 		"registry": "http://maven.kasitesoft.com/repository/intelmt-npm-hosted/"
+	},
+	"peerDependencies": {
+		"vuex": "^4.1.0"
 	}
 }

+ 3 - 2
service/request.ts

@@ -1,5 +1,6 @@
 import { common } from '../utils';
 import { useSmallProgramLogin } from '../hook';
+import store from '../store';
 let _app;
 
 /**
@@ -29,10 +30,10 @@ const httpPost = (url: string, data: any, options: any = {}, complete = undefine
 	if (showLoading) {
 		common.showLoading();
 	}
-
+	console.log('request', store.state);
 	let header: any = {
 		'content-type': 'application/json',
-		token: getApp().globalData.token || uni.getStorageSync('token'),
+		token: store.state.base.token,
 		'call-appId': uni.getStorageSync('frontEndConfig').fixedAppjsGlobalData.appId,
 	};
 	// 如果has 为true 代表当前请求接口需要加密 且获取的加密秘钥不为空

+ 11 - 0
store/index.ts

@@ -0,0 +1,11 @@
+import { createStore } from 'vuex';
+import { base } from './modules';
+
+const store = createStore({
+	state: {},
+	modules: {
+		base,
+	},
+});
+
+export default store;

+ 73 - 0
store/modules/base.ts

@@ -0,0 +1,73 @@
+export const base = {
+	state: {
+		token: '',
+		openId: '',
+		unionId: '',
+		smallProOpenId: '',
+		wechatOpenId: '',
+		memberList: [],
+		currentUser: null,
+	},
+	getters: {
+		/** 获取token */
+		getToken: (state: any) => {
+			return state.token;
+		},
+		/** 获取openId */
+		getOpenId: (state: any) => {
+			return state.openId;
+		},
+		/** 获取unionId */
+		getUnionId: (state: any) => {
+			return state.unionId;
+		},
+		/** 获取smallProOpenId */
+		getSmallProOpenId: (state: any) => {
+			return state.smallProOpenId;
+		},
+		/** 获取wechatOpenId */
+		getWechatOpenId: (state: any) => {
+			return state.wechatOpenId;
+		},
+		/** 获取就诊人列表 */
+		getMemberList: (state: any) => {
+			return state.memberList;
+		},
+		/** 获取当前就诊人 */
+		getCurrentUser: (state: any) => {
+			return state.currentUser;
+		},
+	},
+	mutations: {
+		/** 设置token */
+		setToken: (state: any, token: string) => {
+			state.token = token;
+		},
+		/** 设置openId */
+		setOpenId: (state: any, openId: string) => {
+			state.openId = openId;
+		},
+		/** 设置unionId */
+		setUnionId: (state: any, unionId: string) => {
+			state.unionId = unionId;
+		},
+		/** 设置smallProOpenId */
+		setSmallProOpenId: (state: any, smallProOpenId: string) => {
+			state.smallProOpenId = smallProOpenId;
+		},
+		/** 设置wechatOpenId */
+		setWechatOpenId: (state: any, wechatOpenId: string) => {
+			state.wechatOpenId = wechatOpenId;
+		},
+		/** 设置就诊人列表 */
+		setMemberList: (state: any, list: any[]) => {
+			state.memberList = list;
+		},
+		/** 设置当前就诊人 */
+		setCurrentUser: (state: any, member: any) => {
+			state.currentUser = member;
+		},
+	},
+	actions: {},
+	modules: {},
+};

+ 1 - 0
store/modules/index.ts

@@ -0,0 +1 @@
+export * from './base';