index.js 9.4 KB

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