user-certification.ts 5.8 KB

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