popup-user-auth.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. async onSubmit(event: WechatMiniprogram.FormSubmit) {
  49. if (this.data.submitting || submitting) return;
  50. submitting = true;
  51. try {
  52. const data = { ...this.data.model, ...event.detail.value, scene: this.data.scene };
  53. if (!data.agemust) data.agemust = this.data.privacyContract.agree ? "Y" : "N";
  54. if (data.agemust === "N") {
  55. this.setData({ "privacyContract.show": true });
  56. throw `请阅读并同意${this.data.privacyContract.name}`;
  57. }
  58. if (!data.phone) throw `请获取手机号码`;
  59. this.setData({ "submitting": submitting });
  60. const { patientId } = await createPhoneRegister(<any>data);
  61. wx.setStorageSync("patientId", patientId);
  62. this.setData({ 'visible': false });
  63. this.triggerEvent('register', { patientId });
  64. } catch (error) {
  65. if (typeof error === 'string') this.triggerEvent('message', { type: 'warning', content: error });
  66. else if (error.errMsg) {
  67. this.triggerEvent('message', { type: 'error', content: error.errMsg });
  68. this.setData({ "model.phone": '', loading: false })
  69. }
  70. }
  71. submitting = false;
  72. this.setData({ "submitting": submitting });
  73. },
  74. }
  75. })