index.js 12 KB

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