Browse Source

fix: 核酸挂号添加就诊人列表返回刷新

liweimin 2 years ago
parent
commit
d514f8f4cd

+ 412 - 412
antbuilder/industry/hospitalV2/components/edit-patient/index.js

@@ -1,12 +1,12 @@
 import { connect } from "herculex";
 import { checkPhone, checkIdCard } from "./check";
 import {
-	editCard,
-	createCard,
-	createPatient,
-	patientUpdate,
-	getPatientList,
-	getLoginUserCardList,
+  editCard,
+  createCard,
+  createPatient,
+  patientUpdate,
+  getPatientList,
+  getLoginUserCardList,
 } from "./service";
 import doLogin from "../../utils/doLogin";
 import history from "../../utils/history";
@@ -14,418 +14,418 @@ import loadOcr from "../../utils/loadOcr";
 import getEncryptStr from "../../utils/getEncryptStr";
 import { reportCmPV_YL } from "../../utils/cloudMonitorHelper";
 const caIcon =
-	"https://gw.alipayobjects.com/mdn/rms_373ab8/afts/img/A*w6MrS5tmnFAAAAAAAAAAAAAAARQnAQ";
+  "https://gw.alipayobjects.com/mdn/rms_373ab8/afts/img/A*w6MrS5tmnFAAAAAAAAAAAAAAARQnAQ";
 const bookIcon =
-	"https://gw.alipayobjects.com/mdn/rms_373ab8/afts/img/A*nta9QpHSTvkAAAAAAAAAAAAAARQnAQ";
+  "https://gw.alipayobjects.com/mdn/rms_373ab8/afts/img/A*nta9QpHSTvkAAAAAAAAAAAAAARQnAQ";
 
 const getDefaultForm = () => ({
-	id: "",
-	name: "",
-	sex: "男",
-	idCardNo: "",
-	relationShip: "",
-	phoneNumber: "",
-	idCardType: "身份证",
-
-	/* 新增卡字段 */
-	type: "",
-	cardNum: "",
-	defaultCard: "",
-	phoneRsa: "",
-	fullNameRsa: "",
-	idcardNoRsa: "",
+  id: "",
+  name: "",
+  sex: "男",
+  idCardNo: "",
+  relationShip: "",
+  phoneNumber: "",
+  idCardType: "身份证",
+
+  /* 新增卡字段 */
+  type: "",
+  cardNum: "",
+  defaultCard: "",
+  phoneRsa: "",
+  fullNameRsa: "",
+  idcardNoRsa: "",
 });
 
 const copy = (data) => JSON.parse(JSON.stringify(data));
 
 Component(
-	connect({
-		mapStateToProps: {
-			userInfo: ({ $global }) => $global.userInfo || {},
-		},
-	})({
-		props: {
-			componentData: {},
-		},
-		data: {
-			caIcon,
-			bookIcon,
-			show: false,
-			routeState: null,
-			medicCardsList: [],
-			isAuthSuccess: false,
-			editForm: getDefaultForm(),
-			genders: [
-				{
-					label: "男",
-					value: "男",
-				},
-				{
-					label: "女",
-					value: "女",
-				},
-			],
-			certTypes: [
-				{
-					id: 1,
-					name: "身份证",
-				},
-			],
-		},
-
-		didMount() {
-			const { componentData } = this.props;
-			const { editForm, routeState } = componentData || {};
-			const state = {};
-
-			if (routeState) {
-				state.routeState = routeState;
-			}
-			/* 编辑 */
-
-			if (editForm) {
-				state.editForm = this.initData(editForm);
-			}
-
-			this.setData(state);
-			/* 埋点服务预警 */
-
-			reportCmPV_YL({
-				title: "在线建档",
-			});
-		},
-
-		methods: {
-			isEditCard() {
-				const { $routeConfig } = this.$page;
-				const { query } = $routeConfig;
-				return query.editType === "card";
-			},
-
-			initData(data) {
-				const _data = {};
-				const defaultData = getDefaultForm();
-				Object.keys(defaultData).forEach((key) => {
-					const value = data[key] || defaultData[key];
-					if (value) _data[key] = value;
-				});
-				return _data;
-			},
-
-			onClosePopup() {
-				this.setData({
-					show: false,
-				});
-			},
-
-			onTap() {
-				this.onClosePopup();
-			},
-
-			async onRelationChange({ id, name }) {
-				let isAuthSuccess = false;
-				const { editForm: _editForm } = this.data;
-				let editForm = copy(_editForm);
-				/* 如果是从本人切换到其他关系上的需要清空数据 */
-
-				if (_editForm.relationShip === "本人" && id !== 1) {
-					editForm = getDefaultForm();
-				}
-
-				editForm.relationShip = name;
-				/* 如果为本人,唤起授权 */
-
-				if (id === 1) {
-					const { userInfo } = this.data; // 判断用户是否已经登陆
-
-					if (userInfo.isLogin) {
-						isAuthSuccess = true;
-					} else {
-						isAuthSuccess = await doLogin.call(this, {
-							scopes: "auth_user",
-						});
-					}
-
-					if (isAuthSuccess) {
-						const {
-							phone,
-							gender = "m",
-							idCardNo,
-							fullName,
-							phoneRsa,
-							fullNameRsa,
-							idcardNoRsa,
-						} = this.data.userInfo;
-						editForm.name = fullName;
-						editForm.idCardNo = idCardNo;
-						editForm.phoneNumber = phone;
-						editForm.phoneRsa = phoneRsa;
-						editForm.fullNameRsa = fullNameRsa;
-						editForm.idcardNoRsa = idcardNoRsa;
-						editForm.sex = gender === "m" ? "男" : "女";
-					}
-				}
-
-				this.setData({
-					isAuthSuccess,
-					editForm: { ...editForm },
-				});
-			},
-
-			/* 一键清除 */
-			clearInput(value, key) {
-				// eslint-disable-next-line no-param-reassign
-				value = value || "";
-				const { editForm } = this.data;
-				const oldValue = editForm[key] || "";
-				return oldValue.indexOf("*") < 0 ? value : "";
-			},
-
-			/* 输入姓名 */
-			onNameInput({ detail }) {
-				this.setData({
-					"editForm.name": this.clearInput(detail.value, "name"),
-				});
-			},
-
-			/* 选择性别 */
-			onSexChange(item) {
-				this.setData({
-					"editForm.sex": item,
-				});
-			},
-
-			/* 选择证件类别 */
-			onCertTypeChange({ detail }) {
-				const { value } = detail;
-				const { certTypes = [] } = this.data;
-				const { name = "" } = certTypes[value];
-				this.setData({
-					"editForm.certTypes": name,
-				});
-			},
-
-			/* 输入身份证 */
-			onCardNoInput({ detail }) {
-				this.setData({
-					"editForm.idCardNo": this.clearInput(detail.value, "idCardNo"),
-				});
-			},
-
-			/* 输入电话号码 */
-			onPhoneInput({ detail }) {
-				this.setData({
-					"editForm.phoneNumber": this.clearInput(detail.value, "phoneNumber"),
-				});
-			},
-
-			async onCardChange({ cardNum }) {
-				this.resolveFn && this.resolveFn(cardNum);
-			},
-
-			showTip(msg) {
-				my.showToast({
-					content: msg,
-					duration: 3000,
-				});
-				return false;
-			},
-
-			showSuccess(msg) {
-				my.showToast({
-					type: "success",
-					content: msg,
-					duration: 1500,
-				});
-			},
-
-			showError(msg) {
-				my.showToast({
-					type: "fail",
-					content: msg,
-					duration: 1500,
-				});
-			},
-
-			vailForm(form) {
-				const { idCardNo = "", phoneNumber = "" } = form;
-				const { sex, name, idCardType, relationShip } = form;
-				const rest = {
-					sex,
-					name,
-					idCardType,
-					relationShip,
-				};
-				const values = Object.values(rest);
-
-				if (values.filter((v) => !!v).length !== values.length) {
-					return this.showTip("将页面上的信息填写完整!");
-				}
-
-				if (!checkIdCard(idCardNo)) {
-					return this.showTip("身份证格式不正确!");
-				}
-
-				if (!checkPhone(phoneNumber)) {
-					return this.showTip("手机号格式不正确!");
-				}
-
-				return true;
-			},
-
-			/*
-			 * 获取非脱敏的字段
-			 * */
-			restForm(form) {
-				const _form = {};
-				Object.keys(form).forEach((key) => {
-					const value = form[key] || "";
-					if (value.indexOf("*") < 0) _form[key] = value;
-				});
-				return _form;
-			},
-
-			//  保存就诊人
-			async patientSave() {
-				const { editForm, isAuthSuccess } = this.data;
-				/* 如果不是本人,校验字段 */
-
-				if (!isAuthSuccess) {
-					if (!this.vailForm(editForm)) return;
-				}
-				/* 获取非脱敏的字段 */
-
-				const form = this.restForm(editForm);
-				/* 不是编辑的情况,才需要选择就诊卡 */
-				if (!editForm.id && !this.isEditCard()) {
-					form.bindCardNum = await this.onCheckCard(form);
-				}
-				await this.onSubmit(this.initData(form));
-			},
-
-			/* 提交 */
-			async onSubmit(_form) {
-				const { routeState } = this.data;
-				/* 加密数据 */
-
-				const form = {
-					..._form,
-					idCardNo: await getEncryptStr(_form.idCardNo),
-					phoneNumber: await getEncryptStr(_form.phoneNumber),
-				};
-				let msg = "";
-
-				try {
-					my.showLoading();
-					const isEditCard = this.isEditCard();
-					/* 如果有id 或者绑定的卡id为编辑 */
-					if (form.id || form.cardNum) {
-						if (isEditCard) {
-							msg = "就诊卡编辑成功";
-							await editCard(form);
-						} else {
-							msg = "就诊人编辑成功";
-							await patientUpdate(form);
-						}
-					} else if (isEditCard) {
-						msg = "就诊卡添加成功";
-						await createCard(form);
-					} else {
-						msg = "就诊人添加成功";
-						await createPatient(form);
-					}
-					this.goNav(routeState);
-					this.showSuccess(msg);
-				} catch (e) {
-					this.showError(e.msg);
-				}
-				my.hideLoading();
-			},
-
-			goNav(state) {
-				if (state) {
-					const { routeType = "push", ...rest } = state;
-					/* routeType push/replace */
-
-					if (history[routeType]) {
-						history[routeType](rest);
-					}
-				} else {
-					my.navigateBack();
-				}
-			},
-
-			/* 获取就诊卡 */
-			async onCheckCard() {
-				const { editForm, isAuthSuccess } = this.data;
-				my.showLoading();
-				const { idCardNo } = editForm;
-				return new Promise(async (resolve) => {
-					const medicCardsList = isAuthSuccess
-						? await getLoginUserCardList()
-						: await this.getCardByIdCardNo(idCardNo);
-					/* 如果卡大于一张 */
-
-					if (medicCardsList.length <= 1) {
-						return resolve("");
-					}
-
-					my.hideLoading();
-					this.resolveFn = resolve;
-					this.setData({
-						show: true,
-						medicCardsList,
-					});
-				});
-			},
-
-			/* 根据idCardNo查询某个就诊人的就诊卡 */
-			async getCardByIdCardNo(idCardNo) {
-				const value = await getEncryptStr(idCardNo);
-				const list = await getPatientList({
-					idCardNo: value,
-				});
-				if (!list.length) return [];
-				const { medicCards = [] } = list[0];
-				return medicCards;
-			},
-
-			choosePhoneContact() {
-				my.choosePhoneContact({
-					success: ({ mobile }) => {
-						this.setData({
-							"editForm.phoneNumber": mobile.replace(/(\s|-|\+)/g, ""),
-						});
-					},
-				});
-			},
-
-			async onLaunchOcr() {
-				const { readIDCard, IDCardTypes } = await loadOcr();
-				readIDCard({
-					bizId: "ocr_01",
-					type: IDCardTypes.FRONT,
-				})
-					.then((res) => {
-						if (res.error) {
-							return Promise.reject(res.error);
-						}
-
-						const {
-							num: { data: idCardNo },
-							name: { data: realName },
-							sex: { data: userSex },
-						} = res.data;
-						this.setData({
-							"editForm.sex": userSex,
-							"editForm.name": realName,
-							"editForm.idCardNo": idCardNo,
-						});
-					})
-					.catch((e) => {
-						this.showTip(e.message || "识别身份证失败");
-					});
-			},
-		},
-	})
+  connect({
+    mapStateToProps: {
+      userInfo: ({ $global }) => $global.userInfo || {},
+    },
+  })({
+    props: {
+      componentData: {},
+    },
+    data: {
+      caIcon,
+      bookIcon,
+      show: false,
+      routeState: null,
+      medicCardsList: [],
+      isAuthSuccess: false,
+      editForm: getDefaultForm(),
+      genders: [
+        {
+          label: "男",
+          value: "男",
+        },
+        {
+          label: "女",
+          value: "女",
+        },
+      ],
+      certTypes: [
+        {
+          id: 1,
+          name: "身份证",
+        },
+      ],
+    },
+
+    didMount() {
+      const { componentData } = this.props;
+      const { editForm, routeState } = componentData || {};
+      const state = {};
+
+      if (routeState) {
+        state.routeState = routeState;
+      }
+      /* 编辑 */
+
+      if (editForm) {
+        state.editForm = this.initData(editForm);
+      }
+
+      this.setData(state);
+      /* 埋点服务预警 */
+
+      reportCmPV_YL({
+        title: "在线建档",
+      });
+    },
+
+    methods: {
+      isEditCard() {
+        const { $routeConfig } = this.$page;
+        const { query } = $routeConfig;
+        return query.editType === "card";
+      },
+
+      initData(data) {
+        const _data = {};
+        const defaultData = getDefaultForm();
+        Object.keys(defaultData).forEach((key) => {
+          const value = data[key] || defaultData[key];
+          if (value) _data[key] = value;
+        });
+        return _data;
+      },
+
+      onClosePopup() {
+        this.setData({
+          show: false,
+        });
+      },
+
+      onTap() {
+        this.onClosePopup();
+      },
+
+      async onRelationChange({ id, name }) {
+        let isAuthSuccess = false;
+        const { editForm: _editForm } = this.data;
+        let editForm = copy(_editForm);
+        /* 如果是从本人切换到其他关系上的需要清空数据 */
+
+        if (_editForm.relationShip === "本人" && id !== 1) {
+          editForm = getDefaultForm();
+        }
+
+        editForm.relationShip = name;
+        /* 如果为本人,唤起授权 */
+
+        if (id === 1) {
+          const { userInfo } = this.data; // 判断用户是否已经登陆
+
+          if (userInfo.isLogin) {
+            isAuthSuccess = true;
+          } else {
+            isAuthSuccess = await doLogin.call(this, {
+              scopes: "auth_user",
+            });
+          }
+
+          if (isAuthSuccess) {
+            const {
+              phone,
+              gender = "m",
+              idCardNo,
+              fullName,
+              phoneRsa,
+              fullNameRsa,
+              idcardNoRsa,
+            } = this.data.userInfo;
+            editForm.name = fullName;
+            editForm.idCardNo = idCardNo;
+            editForm.phoneNumber = phone;
+            editForm.phoneRsa = phoneRsa;
+            editForm.fullNameRsa = fullNameRsa;
+            editForm.idcardNoRsa = idcardNoRsa;
+            editForm.sex = gender === "m" ? "男" : "女";
+          }
+        }
+
+        this.setData({
+          isAuthSuccess,
+          editForm: { ...editForm },
+        });
+      },
+
+      /* 一键清除 */
+      clearInput(value, key) {
+        // eslint-disable-next-line no-param-reassign
+        value = value || "";
+        const { editForm } = this.data;
+        const oldValue = editForm[key] || "";
+        return oldValue.indexOf("*") < 0 ? value : "";
+      },
+
+      /* 输入姓名 */
+      onNameInput({ detail }) {
+        this.setData({
+          "editForm.name": this.clearInput(detail.value, "name"),
+        });
+      },
+
+      /* 选择性别 */
+      onSexChange(item) {
+        this.setData({
+          "editForm.sex": item,
+        });
+      },
+
+      /* 选择证件类别 */
+      onCertTypeChange({ detail }) {
+        const { value } = detail;
+        const { certTypes = [] } = this.data;
+        const { name = "" } = certTypes[value];
+        this.setData({
+          "editForm.certTypes": name,
+        });
+      },
+
+      /* 输入身份证 */
+      onCardNoInput({ detail }) {
+        this.setData({
+          "editForm.idCardNo": this.clearInput(detail.value, "idCardNo"),
+        });
+      },
+
+      /* 输入电话号码 */
+      onPhoneInput({ detail }) {
+        this.setData({
+          "editForm.phoneNumber": this.clearInput(detail.value, "phoneNumber"),
+        });
+      },
+
+      async onCardChange({ cardNum }) {
+        this.resolveFn && this.resolveFn(cardNum);
+      },
+
+      showTip(msg) {
+        my.showToast({
+          content: msg,
+          duration: 3000,
+        });
+        return false;
+      },
+
+      showSuccess(msg) {
+        my.showToast({
+          type: "success",
+          content: msg,
+          duration: 1500,
+        });
+      },
+
+      showError(msg) {
+        my.showToast({
+          type: "fail",
+          content: msg,
+          duration: 1500,
+        });
+      },
+
+      vailForm(form) {
+        const { idCardNo = "", phoneNumber = "" } = form;
+        const { sex, name, idCardType, relationShip } = form;
+        const rest = {
+          sex,
+          name,
+          idCardType,
+          relationShip,
+        };
+        const values = Object.values(rest);
+
+        if (values.filter((v) => !!v).length !== values.length) {
+          return this.showTip("将页面上的信息填写完整!");
+        }
+
+        if (!checkIdCard(idCardNo)) {
+          return this.showTip("身份证格式不正确!");
+        }
+
+        if (!checkPhone(phoneNumber)) {
+          return this.showTip("手机号格式不正确!");
+        }
+
+        return true;
+      },
+
+      /*
+       * 获取非脱敏的字段
+       * */
+      restForm(form) {
+        const _form = {};
+        Object.keys(form).forEach((key) => {
+          const value = form[key] || "";
+          if (value.indexOf("*") < 0) _form[key] = value;
+        });
+        return _form;
+      },
+
+      //  保存就诊人
+      async patientSave() {
+        const { editForm, isAuthSuccess } = this.data;
+        /* 如果不是本人,校验字段 */
+
+        if (!isAuthSuccess) {
+          if (!this.vailForm(editForm)) return;
+        }
+        /* 获取非脱敏的字段 */
+
+        const form = this.restForm(editForm);
+        /* 不是编辑的情况,才需要选择就诊卡 */
+        if (!editForm.id && !this.isEditCard()) {
+          form.bindCardNum = await this.onCheckCard(form);
+        }
+        await this.onSubmit(this.initData(form));
+      },
+
+      /* 提交 */
+      async onSubmit(_form) {
+        const { routeState } = this.data;
+        /* 加密数据 */
+
+        const form = {
+          ..._form,
+          idCardNo: await getEncryptStr(_form.idCardNo),
+          phoneNumber: await getEncryptStr(_form.phoneNumber),
+        };
+        let msg = "";
+
+        try {
+          my.showLoading();
+          const isEditCard = this.isEditCard();
+          /* 如果有id 或者绑定的卡id为编辑 */
+          if (form.id || form.cardNum) {
+            if (isEditCard) {
+              msg = "就诊卡编辑成功";
+              await editCard(form);
+            } else {
+              msg = "就诊人编辑成功";
+              await patientUpdate(form);
+            }
+          } else if (isEditCard) {
+            msg = "就诊卡添加成功";
+            await createCard(form);
+          } else {
+            msg = "就诊人添加成功";
+            await createPatient(form);
+          }
+          this.goNav(routeState);
+          this.showSuccess(msg);
+        } catch (e) {
+          this.showError(e.msg);
+        }
+        my.hideLoading();
+      },
+
+      goNav(state) {
+        if (state) {
+          const { routeType = "push", ...rest } = state;
+          /* routeType push/replace */
+
+          if (history[routeType]) {
+            history[routeType](rest);
+          }
+        } else {
+          my.navigateBack();
+        }
+      },
+
+      /* 获取就诊卡 */
+      async onCheckCard() {
+        const { editForm, isAuthSuccess } = this.data;
+        my.showLoading();
+        const { idCardNo } = editForm;
+        return new Promise(async (resolve) => {
+          const medicCardsList = isAuthSuccess
+            ? await getLoginUserCardList()
+            : await this.getCardByIdCardNo(idCardNo);
+          /* 如果卡大于一张 */
+
+          if (medicCardsList.length <= 1) {
+            return resolve("");
+          }
+
+          my.hideLoading();
+          this.resolveFn = resolve;
+          this.setData({
+            show: true,
+            medicCardsList,
+          });
+        });
+      },
+
+      /* 根据idCardNo查询某个就诊人的就诊卡 */
+      async getCardByIdCardNo(idCardNo) {
+        const value = await getEncryptStr(idCardNo);
+        const list = await getPatientList({
+          idCardNo: value,
+        });
+        if (!list.length) return [];
+        const { medicCards = [] } = list[0];
+        return medicCards;
+      },
+
+      choosePhoneContact() {
+        my.choosePhoneContact({
+          success: ({ mobile }) => {
+            this.setData({
+              "editForm.phoneNumber": mobile.replace(/(\s|-|\+)/g, ""),
+            });
+          },
+        });
+      },
+
+      async onLaunchOcr() {
+        const { readIDCard, IDCardTypes } = await loadOcr();
+        readIDCard({
+          bizId: "ocr_01",
+          type: IDCardTypes.FRONT,
+        })
+          .then((res) => {
+            if (res.error) {
+              return Promise.reject(res.error);
+            }
+
+            const {
+              num: { data: idCardNo },
+              name: { data: realName },
+              sex: { data: userSex },
+            } = res.data;
+            this.setData({
+              "editForm.sex": userSex,
+              "editForm.name": realName,
+              "editForm.idCardNo": idCardNo,
+            });
+          })
+          .catch((e) => {
+            this.showTip(e.message || "识别身份证失败");
+          });
+      },
+    },
+  })
 );

+ 8 - 2
antbuilder/industry/hospitalV2/components/hospital-home/index.js

@@ -1,7 +1,13 @@
+import { createSubscribe } from 'applet-page-component';
 import history from '../../utils/history';
 import { getHospitalDistrictList } from '../select-hospital-area/service';
 import { getPatientList } from '../edit-patient/service';
-Component({
+Component(createSubscribe({
+  async onShow() {
+    await this.getPatientLists();
+  }
+
+})({
   data: {
     hospitalList: [],
     showPerson: false,
@@ -115,4 +121,4 @@ Component({
     }
 
   }
-});
+}));

+ 8 - 2
antbuilder/industry/hospitalV2/components/hospital-project/index.js

@@ -1,9 +1,15 @@
+import { createSubscribe } from 'applet-page-component';
 import { getTestTimeList, getQuestionnaire, getItemList, nucleicOrderConfirm } from '../../service/common';
 import { getPatientList } from '../edit-patient/service';
 import history from '../../utils/history';
 import { tradePay } from '../../utils/tradePay';
 import { reportCmPV_YL } from '../../utils/cloudMonitorHelper';
-Component({
+Component(createSubscribe({
+  async onShow() {
+    await this.getPatientLists();
+  }
+
+})({
   data: {
     personItem: {},
     hospitalDistrictId: '',
@@ -349,4 +355,4 @@ Component({
     }
 
   }
-});
+}));

+ 116 - 0
antbuilder/industry/hospitalV2/utils/eventBus.js

@@ -0,0 +1,116 @@
+class EventBus {
+  constructor() {
+    // 初始化事件列表
+    this.eventObject = {};
+    // 回调函数列表的id
+    this.callbackId = 0;
+  }
+  // 发布事件
+  publish(eventName, ...args) {
+    // 取出当前事件所有的回调函数
+    const callbackObject = this.eventObject[eventName];
+    if (!callbackObject) return console.warn(eventName + " not found!");
+    // 执行每一个回调函数
+    for (let id in callbackObject) {
+      // 执行时传入参数
+      callbackObject[id](...args);
+      // 只订阅一次的回调函数需要删除
+      if (id[0] === "d") {
+        delete callbackObject[id];
+      }
+    }
+  }
+  // 订阅事件
+  subscribe(eventName, callback) {
+    // 初始化这个事件
+    if (!this.eventObject[eventName]) {
+      // 使用对象存储,注销回调函数的时候提高删除的效率
+      this.eventObject[eventName] = {};
+    }
+
+    const id = this.callbackId++;
+
+    // 存储订阅者的回调函数
+    // callbackId使用后需要自增,供下一个回调函数使用
+    this.eventObject[eventName][id] = callback;
+
+    // 每一次订阅事件,都生成唯一一个取消订阅的函数
+    const unSubscribe = () => {
+      // 清除这个订阅者的回调函数
+      delete this.eventObject[eventName][id];
+
+      // 如果这个事件没有订阅者了,也把整个事件对象清除
+      if (Object.keys(this.eventObject[eventName]).length === 0) {
+        delete this.eventObject[eventName];
+      }
+    };
+
+    return { unSubscribe };
+  }
+
+  // 只订阅一次
+  subscribeOnce(eventName, callback) {
+    // 初始化这个事件
+    if (!this.eventObject[eventName]) {
+      // 使用对象存储,注销回调函数的时候提高删除的效率
+      this.eventObject[eventName] = {};
+    }
+
+    // 标示为只订阅一次的回调函数
+    const id = "d" + this.callbackId++;
+
+    // 存储订阅者的回调函数
+    // callbackId使用后需要自增,供下一个回调函数使用
+    this.eventObject[eventName][id] = callback;
+
+    // 每一次订阅事件,都生成唯一一个取消订阅的函数
+    const unSubscribe = () => {
+      // 清除这个订阅者的回调函数
+      delete this.eventObject[eventName][id];
+
+      // 如果这个事件没有订阅者了,也把整个事件对象清除
+      if (Object.keys(this.eventObject[eventName]).length === 0) {
+        delete this.eventObject[eventName];
+      }
+    };
+
+    return { unSubscribe };
+  }
+
+  // 清除事件
+  clear(eventName) {
+    // 未提供事件名称,默认清除所有事件
+    if (!eventName) {
+      this.eventObject = {};
+      return;
+    }
+
+    // 清除指定事件
+    delete this.eventObject[eventName];
+  }
+}
+
+// 测试
+const eventBus = new EventBus();
+
+// 订阅事件eventX
+// eventBus.subscribe("eventX", (obj, num) => {
+//   console.log("模块A", obj, num);
+// });
+// eventBus.subscribe("eventX", (obj, num) => {
+//   console.log("模块B", obj, num);
+// });
+// eventBus.subscribe("eventX", (obj, num) => {
+//   console.log("模块C", obj, num);
+// });
+
+// 发布事件eventX
+// eventBus.publish("eventX", { msg: "EventX published!" }, 1);
+
+// 清除
+// eventBus.clear("eventX");
+
+// 再次发布事件eventX,由于已经清除,所有模块都不会再收到消息了
+// eventBus.publish("eventX", { msg: "EventX published again!" }, 2);
+
+export default eventBus;