index.js 13 KB

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