index.js 9.7 KB

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