index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 { idCardNo = "", phoneNumber = "" } = form;
  260. const { sex, name, idCardType, relationShip, isInsu } = form;
  261. const rest = {
  262. // sex,
  263. name,
  264. idCardType,
  265. relationShip,
  266. };
  267. const values = Object.values(rest);
  268. if (values.filter((v) => !!v).length !== values.length) {
  269. return this.showTip("将页面上的信息填写完整!");
  270. }
  271. if (idCardType === "中国大陆居民身份证" && !checkIdCard(idCardNo)) {
  272. return this.showTip("身份证格式不正确!");
  273. }
  274. if (!checkPhone(phoneNumber)) {
  275. return this.showTip("手机号格式不正确!");
  276. }
  277. if (!isInsu) {
  278. return this.showTip("请选择是否医保");
  279. }
  280. return true;
  281. },
  282. /*
  283. * 获取非脱敏的字段
  284. * */
  285. restForm(form) {
  286. const _form = {};
  287. Object.keys(form).forEach((key) => {
  288. const value = form[key] || "";
  289. if (value.indexOf("*") < 0) _form[key] = value;
  290. });
  291. return _form;
  292. },
  293. // 保存就诊人
  294. async patientSave() {
  295. const { editForm, isAuthSuccess } = this.data;
  296. /* 如果不是本人,校验字段 */
  297. if (!isAuthSuccess) {
  298. if (!this.vailForm(editForm)) return;
  299. }
  300. /* 获取非脱敏的字段 */
  301. const form = this.restForm(editForm);
  302. /* 不是编辑的情况,才需要选择就诊卡 */
  303. if (!editForm.id && !this.isEditCard()) {
  304. form.bindCardNum = await this.onCheckCard(form);
  305. }
  306. await this.onSubmit(this.initData(form));
  307. },
  308. /* 提交 */
  309. async onSubmit(_form) {
  310. const { routeState } = this.data;
  311. const { idCardType } = _form;
  312. /* 加密数据 */
  313. const form = {
  314. ..._form,
  315. idCardNo: await getEncryptStr(_form.idCardNo),
  316. phoneNumber: await getEncryptStr(_form.phoneNumber),
  317. idCardType: cardTypeMap[idCardType],
  318. };
  319. let msg = "";
  320. if (form.isInsu !== "1") {
  321. form.isInsu = "";
  322. }
  323. try {
  324. my.showLoading();
  325. const isEditCard = this.isEditCard();
  326. /* 如果有id 或者绑定的卡id为编辑 */
  327. if (form.id || form.cardNum) {
  328. if (isEditCard) {
  329. msg = "就诊卡编辑成功";
  330. await editCard(form);
  331. } else {
  332. msg = "就诊人编辑成功";
  333. await patientUpdate(form);
  334. }
  335. } else if (isEditCard) {
  336. msg = "就诊卡添加成功";
  337. await createCard(form);
  338. } else {
  339. msg = "就诊人添加成功";
  340. await createPatient(form);
  341. }
  342. this.goNav(routeState);
  343. this.showSuccess(msg);
  344. } catch (e) {
  345. this.showError(e.msg);
  346. }
  347. my.hideLoading();
  348. },
  349. goNav(state) {
  350. if (state) {
  351. const { routeType = "push", ...rest } = state;
  352. /* routeType push/replace */
  353. if (history[routeType]) {
  354. history[routeType](rest);
  355. }
  356. } else {
  357. my.navigateBack();
  358. }
  359. },
  360. /* 获取就诊卡 */
  361. async onCheckCard() {
  362. console.log("onCheckCard");
  363. const { editForm, isAuthSuccess } = this.data;
  364. my.showLoading();
  365. const { idCardNo } = editForm;
  366. return new Promise(async (resolve) => {
  367. const medicCardsList = isAuthSuccess
  368. ? await getLoginUserCardList()
  369. : await this.getCardByIdCardNo(idCardNo);
  370. /* 如果卡大于一张 */
  371. if (medicCardsList.length <= 1) {
  372. return resolve("");
  373. }
  374. my.hideLoading();
  375. this.resolveFn = resolve;
  376. this.setData({
  377. show: true,
  378. medicCardsList,
  379. });
  380. });
  381. },
  382. /* 根据idCardNo查询某个就诊人的就诊卡 */
  383. async getCardByIdCardNo(idCardNo) {
  384. const value = await getEncryptStr(idCardNo);
  385. const list = await getPatientList({
  386. idCardNo: value,
  387. });
  388. if (!list.length) return [];
  389. const { medicCards = [] } = list[0];
  390. return medicCards;
  391. },
  392. choosePhoneContact() {
  393. my.choosePhoneContact({
  394. success: ({ mobile }) => {
  395. this.setData({
  396. "editForm.phoneNumber": mobile.replace(/(\s|-|\+)/g, ""),
  397. });
  398. },
  399. });
  400. },
  401. async onLaunchOcr() {
  402. const { readIDCard, IDCardTypes } = await loadOcr();
  403. readIDCard({
  404. bizId: "ocr_01",
  405. type: IDCardTypes.FRONT,
  406. })
  407. .then((res) => {
  408. if (res.error) {
  409. return Promise.reject(res.error);
  410. }
  411. const {
  412. num: { data: idCardNo },
  413. name: { data: realName },
  414. sex: { data: userSex },
  415. } = res.data;
  416. this.setData({
  417. "editForm.sex": userSex,
  418. "editForm.name": realName,
  419. "editForm.idCardNo": idCardNo,
  420. });
  421. })
  422. .catch((e) => {
  423. this.showTip(e.message || "识别身份证失败");
  424. });
  425. },
  426. },
  427. })
  428. );