popup-user-auth.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // components/popup-user-auth/popup-user-auth.ts
  2. import { openPrivacyContract } from "../../lib/wx/open-api";
  3. import { login } from "../../lib/logic";
  4. import { usePhoneNumber } from "../../lib/use/use-phone";
  5. import { getSceneRegisterMethod, setSceneRegisterMethod } from "../../lib/request/common";
  6. let submitting = false;
  7. Component({
  8. properties: {
  9. visible: { type: Boolean, value: false },
  10. overlay: { type: Boolean, value: true },
  11. scene: { type: String, value: '' },
  12. cardno: { type: String, value: '' },
  13. phone: { type: String, value: '' },
  14. },
  15. data: {
  16. title: '完善信息',
  17. privacyContract: { agree: false, name: "《隐私政策》", show: false },
  18. loading: false,
  19. submitting: false,
  20. cardnoProps: { prefix: '', suffix: '', fill: '', length: 0 },
  21. phoneProps: { prefix: '', suffix: '', fill: '', length: 0 },
  22. },
  23. observers: {
  24. cardno(value: string) {
  25. if (!value) return;
  26. const length = 18;
  27. const tag = '*';
  28. if (!value.includes(tag)) value = value.padEnd(length, tag);
  29. const [_, prefix = '', fill = '', suffix = ''] = value.match(/(\d+)?(\*+)?(\d+)?/) || [];
  30. this.setData({ cardnoProps: { prefix, suffix, fill: '', length: fill.length === length ? 0 : fill.length } });
  31. if (this.data.cardnoProps.length) this.setData({ visible: true });
  32. },
  33. phone(value: string) {
  34. if (!value) return;
  35. const length = 11;
  36. const tag = '*';
  37. if (!value.includes(tag)) value = value.padEnd(length, tag);
  38. const [_, prefix = '', fill = '', suffix = ''] = value.match(/(\d+)?(\*+)?(\d+)?/) || [];
  39. this.setData({ phoneProps: { prefix, suffix, fill: '', length: fill.length === length ? 0 : fill.length } });
  40. if (this.data.phoneProps.length) this.setData({ visible: true });
  41. },
  42. scene(value: string) { this.init(value); }
  43. },
  44. methods: {
  45. async init(scene: string) {
  46. if (!scene) return;
  47. await login();
  48. const data = await getSceneRegisterMethod(scene);
  49. this.setData({ ...data });
  50. this.triggerEvent('loaded', { visible: this.data.visible, scene });
  51. if (data.patientId) this.register(data.patientId);
  52. },
  53. getPhoneNumberHandle(event: WechatMiniprogram.ButtonGetPhoneNumber) {
  54. this.setData({ loading: true });
  55. const { getPhoneNumber } = usePhoneNumber();
  56. getPhoneNumber(event, (value) => {
  57. this.setData({ "phoneProps.fill": value, loading: false })
  58. })
  59. },
  60. openPrivacyContract: openPrivacyContract,
  61. onPrivacySetting({ detail }: { detail: WechatMiniprogram.GetPrivacySettingSuccessCallbackResult; }) {
  62. this.setData({
  63. "privacyContract.name": detail.privacyContractName,
  64. // 'privacyContract.agree': !detail.needAuthorization,
  65. });
  66. },
  67. onAgreeChange(event: any) {
  68. const agree = event?.detail?.checked;
  69. if (agree) this.setData({ "privacyContract.show": true });
  70. else this.setData({ "privacyContract.agree": agree });
  71. },
  72. onAgree() {
  73. this.setData({ "privacyContract.agree": true });
  74. },
  75. onDisagree() {
  76. this.setData({ "privacyContract.agree": false });
  77. },
  78. register(patientId: string) {
  79. if (!patientId) return;
  80. wx.setStorageSync("patientId", patientId);
  81. this.setData({ 'visible': false });
  82. this.triggerEvent('register', { patientId });
  83. },
  84. async onSubmit(event: WechatMiniprogram.FormSubmit) {
  85. if (this.data.submitting || submitting) return;
  86. submitting = true;
  87. try {
  88. const data = { agemust: this.data.privacyContract.agree ? 'Y' : 'N', phone: '' } as any;
  89. if (data.agemust === "N") {
  90. this.setData({ "privacyContract.show": true });
  91. throw `请阅读并同意${this.data.privacyContract.name}`;
  92. }
  93. const phone = this.data.phoneProps;
  94. if (phone.length && phone.fill.length !== phone.length) throw `手机号码请补充完整`;
  95. else if (!phone.fill) throw `请获取手机号码`;
  96. else data.phone = `${phone.prefix}${phone.fill}${phone.suffix}`;
  97. const cardno = this.data.cardnoProps;
  98. if (cardno.length && cardno.fill.length !== cardno.length) throw `身份证号请补充完整`;
  99. else if (cardno.length) data.cardno = `${phone.prefix}${phone.fill}${phone.suffix}`;
  100. if (this.data.scene) data.scene = decodeURIComponent(this.data.scene);
  101. this.setData({ "submitting": submitting });
  102. const { patientId } = await setSceneRegisterMethod(data);
  103. this.register(patientId);
  104. } catch (error) {
  105. if (typeof error === 'string') this.triggerEvent('message', { type: 'warning', content: error });
  106. else if (error.errMsg) {
  107. this.triggerEvent('message', { type: 'error', content: error.errMsg });
  108. if (!this.data.phoneProps.length) this.setData({ "phoneProps.fill": '', loading: false });
  109. }
  110. }
  111. setTimeout(() => {
  112. submitting = false;
  113. this.setData({ "submitting": submitting });
  114. }, 100);
  115. },
  116. }
  117. })