index.js 10 KB

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