user-certification.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import DictionariesBehavior from "../../../../core/behavior/dictionaries.behavior";
  2. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  3. import tickleBehavior, {
  4. getTickleContext,
  5. } from "../../../../core/behavior/tickle.behavior";
  6. import { openPrivacyContract } from "../../../../lib/wx/open-api";
  7. // module/user/pages/user-certification/user-certification.ts
  8. import {
  9. createUserInfoMethod,
  10. verifyCardnoMethod,
  11. createPhoneRegister,
  12. } from "../../request";
  13. import { usePhoneNumber } from "../../../../lib/use/use-phone";
  14. Component({
  15. behaviors: [PageContainerBehavior, DictionariesBehavior, tickleBehavior],
  16. lifetimes: {
  17. attached() {
  18. const channel = this.getOpenerEventChannel();
  19. channel.on("navigateBack", (data) => {
  20. this.setData(data);
  21. });
  22. const { getPhoneNumber, updateStatus, updateValue } = usePhoneNumber();
  23. this.getPhoneNumber = <any>getPhoneNumber;
  24. updateStatus((status) => this.setData({ loading: status === "loading" }));
  25. updateValue((value) => this.setData({ "model.phone": value }));
  26. },
  27. },
  28. properties: {
  29. hide: { type: Boolean, value: false },
  30. type: { type: String, value: "" },
  31. },
  32. data: {
  33. loading: false,
  34. verifying: false,
  35. submitting: false,
  36. model: {} as AnyObject,
  37. name: "",
  38. cardno: "",
  39. age: "",
  40. sex: "",
  41. privacyContract: { agree: false, name: "《隐私政策》", show: false },
  42. },
  43. /**
  44. * 组件的方法列表
  45. */
  46. methods: {
  47. cancel() {
  48. wx.navigateBack();
  49. },
  50. getPhoneNumber(event: WechatMiniprogram.ButtonGetPhoneNumber) {
  51. if (wx.getSystemInfoSync().platform === "devtools") {
  52. wx.showToast({ title: "请在真机上测试", icon: "none" });
  53. return;
  54. }
  55. const { getPhoneNumber, updateValue } = usePhoneNumber();
  56. getPhoneNumber(event);
  57. updateValue((value) => this.setData({ "model.phone": value }));
  58. },
  59. openPrivacyContract: openPrivacyContract,
  60. onPrivacySetting({
  61. detail,
  62. }: {
  63. detail: WechatMiniprogram.GetPrivacySettingSuccessCallbackResult;
  64. }) {
  65. this.setData({
  66. "privacyContract.name": detail.privacyContractName,
  67. // 'privacyContract.agree': !detail.needAuthorization,
  68. });
  69. },
  70. onAgreeChange(event: any) {
  71. console.log(event);
  72. const agree = event?.detail?.checked;
  73. if (agree) this.setData({ "privacyContract.show": true });
  74. else this.setData({ "privacyContract.agree": agree });
  75. },
  76. onAgree() {
  77. this.setData({ "privacyContract.agree": true });
  78. },
  79. onDisagree() {
  80. this.setData({ "privacyContract.agree": false });
  81. },
  82. async verifyCardno(event: WechatMiniprogram.InputConfirm) {
  83. const value = event.detail.value;
  84. if (value.length !== 18) {
  85. wx.showToast({ title: "请输入正确的身份证号", icon: "none" });
  86. } else {
  87. this.setData({ verifying: true });
  88. try {
  89. const data = await verifyCardnoMethod(value);
  90. this.setData({ age: data.age, sex: data.sex });
  91. } catch (error) {
  92. this.setData({ cardno: "", age: "", sex: "" });
  93. wx.showToast({ title: error.errMsg, icon: "error" });
  94. }
  95. this.setData({ verifying: false });
  96. }
  97. },
  98. async onSubmit(event: WechatMiniprogram.FormSubmit) {
  99. if (this.data.submitting) return;
  100. this.resetSubmitState(true);
  101. const data = { ...this.data.model, ...event.detail.value };
  102. data.phone = this.data.model.phone;
  103. if (!data.phone) {
  104. this.resetSubmitState(false);
  105. return getTickleContext.call(this).showWarnMessage("请获取手机号码");
  106. }
  107. if (!data.agemust) {
  108. data.agemust = this.data.privacyContract.agree ? "Y" : "N";
  109. }
  110. if (data.agemust === "N") {
  111. this.resetSubmitState(false);
  112. this.setData({ "privacyContract.show": true });
  113. return wx.showToast({
  114. title: `请阅读并同意${this.data.privacyContract.name}`,
  115. icon: "none",
  116. });
  117. }
  118. wx.showLoading({ title: "保存中" });
  119. try {
  120. const { patientId } = await createPhoneRegister(<any>data);
  121. wx.setStorageSync("patientId", patientId);
  122. // 回到相应的页面
  123. if (this.data.type === "home") {
  124. wx.navigateTo({
  125. url: "/pages/home/home",
  126. });
  127. } else {
  128. wx.navigateBack();
  129. }
  130. } catch (error) {
  131. // 请求失败时恢复按钮状态
  132. if (submitBtn) {
  133. submitBtn.resetState();
  134. }
  135. getTickleContext.call(this).showErrorMessage(error.errMsg);
  136. } finally {
  137. this.setData({ submitting: false });
  138. wx.hideLoading();
  139. }
  140. },
  141. resetSubmitState(loading: boolean) {
  142. if (loading) this.setData({ submitting: true });
  143. else {
  144. this.setData({ submitting: false });
  145. const submitBtn = this.selectComponent("#submitBtn");
  146. if (submitBtn) submitBtn.resetState();
  147. }
  148. }
  149. },
  150. });