popup-user-auth.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // components/popup-user-auth/popup-user-auth.ts
  2. import { openPrivacyContract } from "../../lib/wx/open-api";
  3. import { usePhoneNumber } from "../../lib/use/use-phone";
  4. import { createPhoneRegister } from "../../lib/request/common";
  5. let submitting = false;
  6. Component({
  7. properties: {
  8. show: { type: Boolean, value: false },
  9. overlay: { type: Boolean, value: true },
  10. scene: { type: String, value: '' },
  11. },
  12. data: {
  13. visible: false,
  14. title: '完善信息',
  15. privacyContract: { agree: false, name: "《隐私政策》", show: false },
  16. loading: false,
  17. submitting: false,
  18. model: { } as { phone?: string },
  19. },
  20. observers: {
  21. 'show'(visible) {
  22. this.setData({ visible })
  23. }
  24. },
  25. methods: {
  26. getPhoneNumberHandle(event: WechatMiniprogram.ButtonGetPhoneNumber) {
  27. this.setData({ loading: true });
  28. const { getPhoneNumber } = usePhoneNumber();
  29. getPhoneNumber(event, (value) => {
  30. this.setData({ "model.phone": value, loading: false })
  31. })
  32. },
  33. openPrivacyContract: openPrivacyContract,
  34. onPrivacySetting({ detail }: { detail: WechatMiniprogram.GetPrivacySettingSuccessCallbackResult; }) {
  35. this.setData({
  36. "privacyContract.name": detail.privacyContractName,
  37. // 'privacyContract.agree': !detail.needAuthorization,
  38. });
  39. },
  40. onAgreeChange(event: any) {
  41. const agree = event?.detail?.checked;
  42. if (agree) this.setData({ "privacyContract.show": true });
  43. else this.setData({ "privacyContract.agree": agree });
  44. },
  45. onAgree() {
  46. this.setData({ "privacyContract.agree": true });
  47. },
  48. onDisagree() {
  49. this.setData({ "privacyContract.agree": false });
  50. },
  51. async onSubmit(event: WechatMiniprogram.FormSubmit) {
  52. if (this.data.submitting || submitting) return;
  53. submitting = true;
  54. try {
  55. const data = { ...this.data.model, ...event.detail.value, scene: this.data.scene };
  56. if (!data.agemust) data.agemust = this.data.privacyContract.agree ? "Y" : "N";
  57. if (data.agemust === "N") {
  58. this.setData({ "privacyContract.show": true });
  59. throw `请阅读并同意${this.data.privacyContract.name}`;
  60. }
  61. if (!data.phone) throw `请获取手机号码`;
  62. this.setData({ "submitting": submitting });
  63. const { patientId } = await createPhoneRegister(<any>data);
  64. wx.setStorageSync("patientId", patientId);
  65. this.setData({ 'visible': false });
  66. this.triggerEvent('register', { patientId });
  67. } catch (error) {
  68. if (typeof error === 'string') this.triggerEvent('message', { type: 'warning', content: error });
  69. else if (error.errMsg) {
  70. this.triggerEvent('message', { type: 'error', content: error.errMsg });
  71. this.setData({ "model.phone": '', loading: false })
  72. }
  73. }
  74. submitting = false;
  75. this.setData({ "submitting": submitting });
  76. },
  77. }
  78. })