user-certification.ts 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import DictionariesBehavior from "../../../../core/behavior/dictionaries.behavior";
  2. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  3. import tickleBehavior, { getTickleContext } from "../../../../core/behavior/tickle.behavior";
  4. import { openPrivacyContract } from "../../../../lib/wx/open-api";
  5. // module/user/pages/user-certification/user-certification.ts
  6. import { createUserInfoMethod, verifyCardnoMethod } from "../../request"
  7. import { usePhoneNumber } from "../../../../lib/use/use-phone";
  8. Component({
  9. behaviors: [
  10. PageContainerBehavior,
  11. DictionariesBehavior,
  12. tickleBehavior,
  13. ],
  14. lifetimes: {
  15. attached() {
  16. const channel = this.getOpenerEventChannel();
  17. channel.on('navigateBack', (data) => { this.setData(data); })
  18. const { getPhoneNumber, updateStatus, updateValue } = usePhoneNumber();
  19. this.getPhoneNumber = <any>getPhoneNumber;
  20. updateStatus(status => this.setData({ 'loading': status === 'loading' }));
  21. updateValue(value => this.setData({ 'model.phone': value }));
  22. }
  23. },
  24. properties: {},
  25. data: {
  26. hide: false,
  27. loading: false,
  28. verifying: false,
  29. model: {} as AnyObject,
  30. privacyContract: { agree: false, name: '《隐私政策》', show: false },
  31. },
  32. /**
  33. * 组件的方法列表
  34. */
  35. methods: {
  36. getPhoneNumber() { /** usePhoneNumber 中实现 */ },
  37. openPrivacyContract: openPrivacyContract,
  38. onPrivacySetting({ detail }: { detail: WechatMiniprogram.GetPrivacySettingSuccessCallbackResult }) {
  39. this.setData({
  40. 'privacyContract.name': detail.privacyContractName,
  41. 'privacyContract.agree': !detail.needAuthorization,
  42. })
  43. },
  44. onAgreeChange(event: any) {
  45. const agree = event?.detail?.checked;
  46. if (agree) this.setData({ 'privacyContract.show': true });
  47. else this.setData({ 'privacyContract.agree': agree });
  48. },
  49. onAgree() {
  50. this.setData({ 'privacyContract.agree': true });
  51. },
  52. async verifyCardno(event: WechatMiniprogram.InputConfirm) {
  53. const value = event.detail.value;
  54. if (value.length !== 18) { wx.showToast({ title: '请输入正确的身份证号', icon: 'none' }); }
  55. else {
  56. this.setData({ verifying: true });
  57. try {
  58. const data = await verifyCardnoMethod(value);
  59. this.setData({ 'model.age': data.age, 'model.sex': data.sex });
  60. } catch (error) {
  61. this.setData({ 'model.cardno': '', 'model.age': '', 'model.sex': '' });
  62. wx.showToast({ title: error.errMsg, icon: 'error' });
  63. }
  64. this.setData({ verifying: false });
  65. }
  66. },
  67. async onSubmit(event: WechatMiniprogram.FormSubmit) {
  68. const data = { ...this.data.model, ...event.detail.value };
  69. // if (!data.phone) return getTickleContext.call(this).showWarnMessage('请获取手机号码');
  70. data.phone = '13012344322'
  71. if (!data.name) return getTickleContext.call(this).showWarnMessage('请输入姓名');
  72. if (data.cardno?.length !== 18) return getTickleContext.call(this).showWarnMessage('请输入正确的身份证号');
  73. if (data.sex === '1' && !data.womenSpecialPeriod) return getTickleContext.call(this).showWarnMessage('请至少选择一项女性特殊期');
  74. if (!data.height) return getTickleContext.call(this).showWarnMessage('请输入身高');
  75. if (!data.weight) return getTickleContext.call(this).showWarnMessage('请输入体重');
  76. console.log(data);
  77. if (!this.data.privacyContract.agree) { wx.showToast({ title: `请阅读并同意${this.data.privacyContract.name}`, icon: 'none' }) }
  78. wx.showLoading({ title: '保存中' });
  79. try {
  80. const { patientId } = await createUserInfoMethod(<any>data);
  81. await wx.navigateBack();
  82. if (patientId) this.getOpenerEventChannel().emit('update', { patientId });
  83. } catch (error) {
  84. getTickleContext.call(this).showErrorMessage(error.errMsg);
  85. }
  86. wx.hideLoading()
  87. }
  88. },
  89. })