user-certification.ts 5.6 KB

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