index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import { connect } from "herculex";
  2. import { checkPhone, checkIdCard } from "./check";
  3. import {
  4. editCard,
  5. createCard,
  6. createPatient,
  7. patientUpdate,
  8. getPatientList,
  9. getLoginUserCardList,
  10. } from "./service";
  11. import doLogin from "../../utils/doLogin";
  12. import history from "../../utils/history";
  13. import loadOcr from "../../utils/loadOcr";
  14. import getEncryptStr from "../../utils/getEncryptStr";
  15. import { reportCmPV_YL } from "../../utils/cloudMonitorHelper";
  16. const caIcon =
  17. "https://gw.alipayobjects.com/mdn/rms_373ab8/afts/img/A*w6MrS5tmnFAAAAAAAAAAAAAAARQnAQ";
  18. const bookIcon =
  19. "https://gw.alipayobjects.com/mdn/rms_373ab8/afts/img/A*nta9QpHSTvkAAAAAAAAAAAAAARQnAQ";
  20. const getDefaultForm = () => ({
  21. id: "",
  22. name: "",
  23. sex: "男",
  24. idCardNo: "",
  25. relationShip: "",
  26. phoneNumber: "",
  27. idCardType: "身份证",
  28. /* 新增卡字段 */
  29. type: "",
  30. cardNum: "",
  31. defaultCard: "",
  32. phoneRsa: "",
  33. fullNameRsa: "",
  34. idcardNoRsa: "",
  35. });
  36. const copy = (data) => JSON.parse(JSON.stringify(data));
  37. Component(
  38. connect({
  39. mapStateToProps: {
  40. userInfo: ({ $global }) => $global.userInfo || {},
  41. },
  42. })({
  43. props: {
  44. componentData: {},
  45. },
  46. data: {
  47. caIcon,
  48. bookIcon,
  49. show: false,
  50. routeState: null,
  51. medicCardsList: [],
  52. isAuthSuccess: false,
  53. editForm: getDefaultForm(),
  54. genders: [
  55. {
  56. label: "男",
  57. value: "男",
  58. },
  59. {
  60. label: "女",
  61. value: "女",
  62. },
  63. ],
  64. certTypes: [
  65. {
  66. id: 1,
  67. name: "身份证",
  68. },
  69. ],
  70. },
  71. didMount() {
  72. const { componentData } = this.props;
  73. const { editForm, routeState } = componentData || {};
  74. const state = {};
  75. if (routeState) {
  76. state.routeState = routeState;
  77. }
  78. /* 编辑 */
  79. if (editForm) {
  80. state.editForm = this.initData(editForm);
  81. }
  82. this.setData(state);
  83. /* 埋点服务预警 */
  84. reportCmPV_YL({
  85. title: "在线建档",
  86. });
  87. },
  88. methods: {
  89. isEditCard() {
  90. const { $routeConfig } = this.$page;
  91. const { query } = $routeConfig;
  92. return query.editType === "card";
  93. },
  94. initData(data) {
  95. const _data = {};
  96. const defaultData = getDefaultForm();
  97. Object.keys(defaultData).forEach((key) => {
  98. const value = data[key] || defaultData[key];
  99. if (value) _data[key] = value;
  100. });
  101. return _data;
  102. },
  103. onClosePopup() {
  104. this.setData({
  105. show: false,
  106. });
  107. },
  108. onTap() {
  109. this.onClosePopup();
  110. },
  111. async onRelationChange({ id, name }) {
  112. let isAuthSuccess = false;
  113. const { editForm: _editForm } = this.data;
  114. let editForm = copy(_editForm);
  115. /* 如果是从本人切换到其他关系上的需要清空数据 */
  116. if (_editForm.relationShip === "本人" && id !== 1) {
  117. editForm = getDefaultForm();
  118. }
  119. editForm.relationShip = name;
  120. /* 如果为本人,唤起授权 */
  121. if (id === 1) {
  122. const { userInfo } = this.data; // 判断用户是否已经登陆
  123. if (userInfo.isLogin) {
  124. isAuthSuccess = true;
  125. } else {
  126. isAuthSuccess = await doLogin.call(this, {
  127. scopes: "auth_user",
  128. });
  129. }
  130. if (isAuthSuccess) {
  131. const {
  132. phone,
  133. gender = "m",
  134. idCardNo,
  135. fullName,
  136. phoneRsa,
  137. fullNameRsa,
  138. idcardNoRsa,
  139. } = this.data.userInfo;
  140. editForm.name = fullName;
  141. editForm.idCardNo = idCardNo;
  142. editForm.phoneNumber = phone;
  143. editForm.phoneRsa = phoneRsa;
  144. editForm.fullNameRsa = fullNameRsa;
  145. editForm.idcardNoRsa = idcardNoRsa;
  146. editForm.sex = gender === "m" ? "男" : "女";
  147. }
  148. }
  149. this.setData({
  150. isAuthSuccess,
  151. editForm: { ...editForm },
  152. });
  153. },
  154. /* 一键清除 */
  155. clearInput(value, key) {
  156. // eslint-disable-next-line no-param-reassign
  157. value = value || "";
  158. const { editForm } = this.data;
  159. const oldValue = editForm[key] || "";
  160. return oldValue.indexOf("*") < 0 ? value : "";
  161. },
  162. /* 输入姓名 */
  163. onNameInput({ detail }) {
  164. this.setData({
  165. "editForm.name": this.clearInput(detail.value, "name"),
  166. });
  167. },
  168. /* 选择性别 */
  169. onSexChange(item) {
  170. this.setData({
  171. "editForm.sex": item,
  172. });
  173. },
  174. /* 选择证件类别 */
  175. onCertTypeChange({ detail }) {
  176. const { value } = detail;
  177. const { certTypes = [] } = this.data;
  178. const { name = "" } = certTypes[value];
  179. this.setData({
  180. "editForm.certTypes": name,
  181. });
  182. },
  183. /* 输入身份证 */
  184. onCardNoInput({ detail }) {
  185. this.setData({
  186. "editForm.idCardNo": this.clearInput(detail.value, "idCardNo"),
  187. });
  188. },
  189. /* 输入电话号码 */
  190. onPhoneInput({ detail }) {
  191. this.setData({
  192. "editForm.phoneNumber": this.clearInput(detail.value, "phoneNumber"),
  193. });
  194. },
  195. async onCardChange({ cardNum }) {
  196. this.resolveFn && this.resolveFn(cardNum);
  197. },
  198. showTip(msg) {
  199. my.showToast({
  200. content: msg,
  201. duration: 3000,
  202. });
  203. return false;
  204. },
  205. showSuccess(msg) {
  206. my.showToast({
  207. type: "success",
  208. content: msg,
  209. duration: 1500,
  210. });
  211. },
  212. showError(msg) {
  213. my.showToast({
  214. type: "fail",
  215. content: msg,
  216. duration: 1500,
  217. });
  218. },
  219. vailForm(form) {
  220. const { idCardNo = "", phoneNumber = "" } = form;
  221. const { sex, name, idCardType, relationShip } = form;
  222. const rest = {
  223. sex,
  224. name,
  225. idCardType,
  226. relationShip,
  227. };
  228. const values = Object.values(rest);
  229. if (values.filter((v) => !!v).length !== values.length) {
  230. return this.showTip("将页面上的信息填写完整!");
  231. }
  232. if (!checkIdCard(idCardNo)) {
  233. return this.showTip("身份证格式不正确!");
  234. }
  235. if (!checkPhone(phoneNumber)) {
  236. return this.showTip("手机号格式不正确!");
  237. }
  238. return true;
  239. },
  240. /*
  241. * 获取非脱敏的字段
  242. * */
  243. restForm(form) {
  244. const _form = {};
  245. Object.keys(form).forEach((key) => {
  246. const value = form[key] || "";
  247. if (value.indexOf("*") < 0) _form[key] = value;
  248. });
  249. return _form;
  250. },
  251. // 保存就诊人
  252. async patientSave() {
  253. const { editForm, isAuthSuccess } = this.data;
  254. /* 如果不是本人,校验字段 */
  255. if (!isAuthSuccess) {
  256. if (!this.vailForm(editForm)) return;
  257. }
  258. /* 获取非脱敏的字段 */
  259. const form = this.restForm(editForm);
  260. /* 不是编辑的情况,才需要选择就诊卡 */
  261. if (!editForm.id && !this.isEditCard()) {
  262. form.bindCardNum = await this.onCheckCard(form);
  263. }
  264. await this.onSubmit(this.initData(form));
  265. },
  266. /* 提交 */
  267. async onSubmit(_form) {
  268. const { routeState } = this.data;
  269. /* 加密数据 */
  270. const form = {
  271. ..._form,
  272. idCardNo: await getEncryptStr(_form.idCardNo),
  273. phoneNumber: await getEncryptStr(_form.phoneNumber),
  274. };
  275. let msg = "";
  276. try {
  277. my.showLoading();
  278. const isEditCard = this.isEditCard();
  279. /* 如果有id 或者绑定的卡id为编辑 */
  280. if (form.id || form.cardNum) {
  281. if (isEditCard) {
  282. msg = "就诊卡编辑成功";
  283. await editCard(form);
  284. } else {
  285. msg = "就诊人编辑成功";
  286. await patientUpdate(form);
  287. }
  288. } else if (isEditCard) {
  289. msg = "就诊卡添加成功";
  290. await createCard(form);
  291. } else {
  292. msg = "就诊人添加成功";
  293. await createPatient(form);
  294. }
  295. this.goNav(routeState);
  296. this.showSuccess(msg);
  297. } catch (e) {
  298. this.showError(e.msg);
  299. }
  300. my.hideLoading();
  301. },
  302. goNav(state) {
  303. if (state) {
  304. const { routeType = "push", ...rest } = state;
  305. /* routeType push/replace */
  306. if (history[routeType]) {
  307. history[routeType](rest);
  308. }
  309. } else {
  310. my.navigateBack();
  311. }
  312. },
  313. /* 获取就诊卡 */
  314. async onCheckCard() {
  315. const { editForm, isAuthSuccess } = this.data;
  316. my.showLoading();
  317. const { idCardNo } = editForm;
  318. return new Promise(async (resolve) => {
  319. const medicCardsList = isAuthSuccess
  320. ? await getLoginUserCardList()
  321. : await this.getCardByIdCardNo(idCardNo);
  322. /* 如果卡大于一张 */
  323. if (medicCardsList.length <= 1) {
  324. return resolve("");
  325. }
  326. my.hideLoading();
  327. this.resolveFn = resolve;
  328. this.setData({
  329. show: true,
  330. medicCardsList,
  331. });
  332. });
  333. },
  334. /* 根据idCardNo查询某个就诊人的就诊卡 */
  335. async getCardByIdCardNo(idCardNo) {
  336. const value = await getEncryptStr(idCardNo);
  337. const list = await getPatientList({
  338. idCardNo: value,
  339. });
  340. if (!list.length) return [];
  341. const { medicCards = [] } = list[0];
  342. return medicCards;
  343. },
  344. choosePhoneContact() {
  345. my.choosePhoneContact({
  346. success: ({ mobile }) => {
  347. this.setData({
  348. "editForm.phoneNumber": mobile.replace(/(\s|-|\+)/g, ""),
  349. });
  350. },
  351. });
  352. },
  353. async onLaunchOcr() {
  354. const { readIDCard, IDCardTypes } = await loadOcr();
  355. readIDCard({
  356. bizId: "ocr_01",
  357. type: IDCardTypes.FRONT,
  358. })
  359. .then((res) => {
  360. if (res.error) {
  361. return Promise.reject(res.error);
  362. }
  363. const {
  364. num: { data: idCardNo },
  365. name: { data: realName },
  366. sex: { data: userSex },
  367. } = res.data;
  368. this.setData({
  369. "editForm.sex": userSex,
  370. "editForm.name": realName,
  371. "editForm.idCardNo": idCardNo,
  372. });
  373. })
  374. .catch((e) => {
  375. this.showTip(e.message || "识别身份证失败");
  376. });
  377. },
  378. },
  379. })
  380. );