user-certification.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import DictionariesBehavior from "../../../../core/behavior/dictionaries.behavior";
  2. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  3. import tickleBehavior, { getTickleContext } from "../../../../core/behavior/tickle.behavior";
  4. import { openPrivacyContract } from "../../../../lib/wx/open-api";
  5. // module/user/pages/user-certification/user-certification.ts
  6. import { createUserInfoMethod, verifyCardnoMethod } from "../../request"
  7. import { usePhoneNumber } from "../../../../lib/use/use-phone";
  8. Component({
  9. behaviors: [
  10. PageContainerBehavior,
  11. DictionariesBehavior,
  12. tickleBehavior,
  13. ],
  14. lifetimes: {
  15. attached() {
  16. const channel = this.getOpenerEventChannel();
  17. channel.on('navigateBack', (data) => { this.setData(data); })
  18. const { getPhoneNumber, updateStatus, updateValue } = usePhoneNumber();
  19. this.getPhoneNumber = <any>getPhoneNumber;
  20. updateStatus(status => this.setData({ 'loading': status === 'loading' }));
  21. updateValue(value => this.setData({ 'model.phone': value }));
  22. }
  23. },
  24. properties: {},
  25. data: {
  26. hide: false,
  27. loading: false,
  28. verifying: false,
  29. model: {} as AnyObject,
  30. cardno: '',
  31. privacyContract: { agree: false, name: '《隐私政策》', show: false },
  32. },
  33. /**
  34. * 组件的方法列表
  35. */
  36. methods: {
  37. getPhoneNumber() { /** usePhoneNumber 中实现 */ },
  38. openPrivacyContract: openPrivacyContract,
  39. onPrivacySetting({ detail }: { detail: WechatMiniprogram.GetPrivacySettingSuccessCallbackResult }) {
  40. this.setData({
  41. 'privacyContract.name': detail.privacyContractName,
  42. // 'privacyContract.agree': !detail.needAuthorization,
  43. })
  44. },
  45. onAgreeChange(event: any) {
  46. console.log(event);
  47. const agree = event?.detail?.checked;
  48. if (agree) this.setData({ 'privacyContract.show': true });
  49. else this.setData({ 'privacyContract.agree': agree });
  50. },
  51. onAgree() {
  52. console.log('onAgree-->');
  53. this.setData({ 'privacyContract.agree': true });
  54. },
  55. async verifyCardno(event: WechatMiniprogram.InputConfirm) {
  56. const value = event.detail.value;
  57. if (value.length !== 18) { wx.showToast({ title: '请输入正确的身份证号', icon: 'none' }); }
  58. else {
  59. this.setData({ verifying: true });
  60. try {
  61. const data = await verifyCardnoMethod(value);
  62. this.setData({ 'model.age': data.age, 'model.sex': data.sex });
  63. } catch (error) {
  64. this.setData({ 'cardno': '', 'model.age': '', 'model.sex': '' });
  65. wx.showToast({ title: error.errMsg, icon: 'error' });
  66. }
  67. this.setData({ verifying: false });
  68. }
  69. },
  70. async onSubmit(event: WechatMiniprogram.FormSubmit) {
  71. const data = { ...this.data.model, ...event.detail.value, };
  72. if (!data.cardno) data.cardno = this.data.cardno;
  73. if (!data.phone) return getTickleContext.call(this).showWarnMessage('请获取手机号码');
  74. if (!data.name) return getTickleContext.call(this).showWarnMessage('请输入姓名');
  75. if (data.cardno?.length !== 18) return getTickleContext.call(this).showWarnMessage('请输入正确的身份证号');
  76. if (data.sex === '1' && !data.womenSpecialPeriod) return getTickleContext.call(this).showWarnMessage('请至少选择一项女性特殊期');
  77. if (!data.height) return getTickleContext.call(this).showWarnMessage('请输入身高');
  78. if (!data.weight) return getTickleContext.call(this).showWarnMessage('请输入体重');
  79. console.log(data);
  80. if (!this.data.privacyContract.agree) {
  81. this.setData({ 'privacyContract.show': true })
  82. return wx.showToast({ title: `请阅读并同意${this.data.privacyContract.name}`, icon: 'none' })
  83. }
  84. wx.showLoading({ title: '保存中' });
  85. try {
  86. const { patientId } = await createUserInfoMethod(<any>data);
  87. await wx.navigateBack();
  88. if (patientId) this.getOpenerEventChannel().emit('update', { patientId });
  89. } catch (error) {
  90. getTickleContext.call(this).showErrorMessage(error.errMsg);
  91. }
  92. wx.hideLoading()
  93. }
  94. },
  95. })