user-certification.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. },
  31. data: {
  32. loading: false,
  33. verifying: false,
  34. model: {} as AnyObject,
  35. name: "",
  36. cardno: "",
  37. age: "",
  38. sex: "",
  39. privacyContract: { agree: false, name: "《隐私政策》", show: false },
  40. },
  41. /**
  42. * 组件的方法列表
  43. */
  44. methods: {
  45. getPhoneNumber(event: WechatMiniprogram.ButtonGetPhoneNumber) {
  46. if (wx.getSystemInfoSync().platform === "devtools") {
  47. wx.showToast({ title: "请在真机上测试", icon: "none" });
  48. return;
  49. }
  50. const { getPhoneNumber, updateValue } = usePhoneNumber();
  51. getPhoneNumber(event);
  52. updateValue((value) => this.setData({ "model.phone": value }));
  53. console.log("手机号", this.data.model.phone);
  54. },
  55. // getPhoneNumber() { /** usePhoneNumber 中实现 */ },
  56. openPrivacyContract: openPrivacyContract,
  57. onPrivacySetting({
  58. detail,
  59. }: {
  60. detail: WechatMiniprogram.GetPrivacySettingSuccessCallbackResult;
  61. }) {
  62. this.setData({
  63. "privacyContract.name": detail.privacyContractName,
  64. // 'privacyContract.agree': !detail.needAuthorization,
  65. });
  66. },
  67. onAgreeChange(event: any) {
  68. console.log(event);
  69. const agree = event?.detail?.checked;
  70. if (agree) this.setData({ "privacyContract.show": true });
  71. else this.setData({ "privacyContract.agree": agree });
  72. },
  73. onAgree() {
  74. const submitBtn = this.selectComponent("#submitBtn");
  75. console.log("onAgree-->");
  76. this.setData({ "privacyContract.agree": true });
  77. if (submitBtn) submitBtn.resetState();
  78. },
  79. async verifyCardno(event: WechatMiniprogram.InputConfirm) {
  80. const value = event.detail.value;
  81. if (value.length !== 18) {
  82. wx.showToast({ title: "请输入正确的身份证号", icon: "none" });
  83. } else {
  84. this.setData({ verifying: true });
  85. try {
  86. const data = await verifyCardnoMethod(value);
  87. this.setData({ age: data.age, sex: data.sex });
  88. } catch (error) {
  89. this.setData({ cardno: "", age: "", sex: "" });
  90. wx.showToast({ title: error.errMsg, icon: "error" });
  91. }
  92. this.setData({ verifying: false });
  93. }
  94. },
  95. async onSubmit(event: WechatMiniprogram.FormSubmit) {
  96. const data = { ...this.data.model, ...event.detail.value };
  97. data.phone = this.data.model.phone;
  98. const submitBtn = this.selectComponent("#submitBtn");
  99. if (!data.phone) {
  100. if (submitBtn) submitBtn.resetState();
  101. return getTickleContext.call(this).showWarnMessage("请获取手机号码");
  102. }
  103. // if (!data.cardno) data.cardno = this.data.cardno;
  104. // if (data.sex == null) data.sex = this.data.sex;
  105. if (!data.agemust) {
  106. data.agemust = this.data.privacyContract.agree ? "Y" : "N";
  107. }
  108. // if (!data.name)
  109. // return getTickleContext.call(this).showWarnMessage("请输入姓名");
  110. // if (data.cardno?.length !== 18)
  111. // return getTickleContext
  112. // .call(this)
  113. // .showWarnMessage("请输入正确的身份证号");
  114. // if (data.sex === "1" && !data.womenSpecialPeriod)
  115. // return getTickleContext
  116. // .call(this)
  117. // .showWarnMessage("请至少选择一项女性特殊期");
  118. // if (!data.height)
  119. // return getTickleContext.call(this).showWarnMessage("请输入身高");
  120. // if (!data.weight)
  121. // return getTickleContext.call(this).showWarnMessage("请输入体重");
  122. if (data.agemust === "N") {
  123. if (submitBtn) submitBtn.resetState();
  124. this.setData({ "privacyContract.show": true });
  125. return wx.showToast({
  126. title: `请阅读并同意${this.data.privacyContract.name}`,
  127. icon: "none",
  128. });
  129. }
  130. wx.showLoading({ title: "保存中" });
  131. try {
  132. // const { patientId } = await createUserInfoMethod(<any>data);
  133. const { patientId } = await createPhoneRegister(<any>data);
  134. // await wx.navigateBack();
  135. // if (patientId) this.getOpenerEventChannel().emit('update', { patientId });
  136. wx.setStorageSync("patientId", patientId);
  137. // wx.redirectTo({
  138. // url: `/module/chats/pages/index/index?component=questionnaire`,
  139. // });
  140. wx.redirectTo({
  141. url: `/pages/home/home`,
  142. });
  143. } catch (error) {
  144. // 请求失败时恢复按钮状态
  145. if (submitBtn) {
  146. submitBtn.resetState();
  147. }
  148. getTickleContext.call(this).showErrorMessage(error.errMsg);
  149. }
  150. wx.hideLoading();
  151. },
  152. },
  153. });