certification.component.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import { Component, OnInit, ViewChild } from '@angular/core';
  2. import { FormsModule } from '@angular/forms';
  3. import {
  4. LoadingController,
  5. ModalController,
  6. ToastController,
  7. } from '@ionic/angular';
  8. import { HttpService } from '../../../services/http.service';
  9. import { identityCodeValid } from '../../../services/utils';
  10. import { AgreementComponent } from '../../login/agreement/agreement.component';
  11. import * as Parse from 'parse';
  12. import { NavComponent } from '../../../app/components/nav/nav.component';
  13. import {
  14. AlertController,
  15. ionicStandaloneModules,
  16. } from '../../ionic-standalone.modules';
  17. import { UploadComponent } from '../../../app/components/upload/upload.component';
  18. @Component({
  19. selector: 'app-certification',
  20. templateUrl: './certification.component.html',
  21. styleUrls: ['./certification.component.scss'],
  22. standalone: true,
  23. imports: [
  24. ...ionicStandaloneModules,
  25. FormsModule,
  26. NavComponent,
  27. UploadComponent,
  28. ],
  29. })
  30. export class CertificationComponent implements OnInit {
  31. @ViewChild('upload') upload!: UploadComponent;
  32. name: string = '';
  33. idCard: string = '';
  34. title: string =
  35. '根据相关法律法规要求,用户需先进行实名认证,方可使用本平台。请尽快完成下方实名认证,本平台将对您的信息严格保密。';
  36. company: any = window.localStorage.getItem('company');
  37. isReal: boolean = false;
  38. loading: any;
  39. time: any; //节流
  40. secretName: any;
  41. secretIdCard: any;
  42. agreement: boolean = false;
  43. registerAgreement: any;
  44. profile?: Parse.Object;
  45. cardtype: 'regions' | 'cn' = 'cn'; // 身份证类型
  46. eduImage: string = '';
  47. constructor(
  48. private service: HttpService,
  49. public loadCtrl: LoadingController,
  50. private modalController: ModalController,
  51. private alertController: AlertController,
  52. private toastController: ToastController
  53. ) {}
  54. ngOnInit() {
  55. this.getAgreement();
  56. this.getProfile();
  57. }
  58. getAgreement() {
  59. let Agreement = new Parse.Query('ContractAgreement');
  60. Agreement.equalTo('company', this.company);
  61. Agreement.equalTo('type', 'register');
  62. Agreement.first().then((res) => {
  63. console.log(res);
  64. this.registerAgreement = res;
  65. });
  66. }
  67. async getProfile() {
  68. let uid = Parse.User.current()?.id;
  69. let query = new Parse.Query('Profile');
  70. query.equalTo('user', uid);
  71. query.notEqualTo('isDeleted', true);
  72. this.profile = await query.first();
  73. if (this.profile?.id) {
  74. this.isReal = this.profile.get('isCross');
  75. if (this.isReal) {
  76. localStorage.setItem('profile', JSON.stringify(this.profile.toJSON()));
  77. this.secretIdCard = this.profile.get('idcard')?.slice(0, 6);
  78. this.secretName = this.profile.get('name')?.slice(0, 1);
  79. } else if (this.profile.get('cardtype') === 'regions') {
  80. this.cardtype = 'regions'
  81. this.name = this.profile.get('name');
  82. this.idCard = this.profile.get('idcard');
  83. this.eduImage = this.profile.get('eduImage')
  84. this.presentAlert('审核中',true);
  85. }
  86. }
  87. }
  88. async showAgreement() {
  89. const modal = await this.modalController.create({
  90. component: AgreementComponent,
  91. cssClass: 'my-custom-class',
  92. componentProps: {
  93. agreement: this.registerAgreement,
  94. },
  95. });
  96. return await modal.present();
  97. }
  98. async presentToast(title: string, time: number, color: string) {
  99. const toast = await this.toastController.create({
  100. message: title,
  101. duration: time,
  102. color: color,
  103. });
  104. toast.present();
  105. }
  106. async submit() {
  107. if (!this.agreement) {
  108. this.presentToast('请先阅读且同意隐私协议', 1500, 'danger');
  109. return;
  110. }
  111. this.loading = await this.loadCtrl.create();
  112. this.loading.present();
  113. this.time && clearTimeout(this.time);
  114. this.time = setTimeout(async () => {
  115. this.name = this.name.trim();
  116. this.idCard = this.idCard.trim();
  117. console.log(this.name, this.idCard);
  118. if (!this.idCard || this.idCard.length != 18 || !this.name) {
  119. this.loading.dismiss();
  120. await this.presentToast('格式错误', 1500, 'danger');
  121. return;
  122. }
  123. let pass = identityCodeValid(this.idCard);
  124. console.log(pass);
  125. if (!pass) {
  126. this.loading.dismiss();
  127. await this.presentToast(
  128. '认证失败,请填写真实的身份信息',
  129. 1500,
  130. 'danger'
  131. );
  132. return;
  133. }
  134. if (this.getAgeFromIdCard(this.idCard) < 18) {
  135. this.loading.dismiss();
  136. await this.presentToast(
  137. '根据平台规定,仅限满18周岁用户使用。',
  138. 1500,
  139. 'danger'
  140. );
  141. return;
  142. }
  143. let res: any = await this.service.postAuth(
  144. this.company,
  145. this.idCard,
  146. this.name
  147. );
  148. if (res.code == 1) {
  149. let { isok } = res.data.result;
  150. console.log(isok);
  151. if (isok) {
  152. let { sex, city } = res.data.result.IdCardInfor;
  153. this.anthProfile(sex, city);
  154. return;
  155. }
  156. this.loading.dismiss();
  157. await this.presentToast(
  158. '认证失败,请填写真实的身份信息',
  159. 1500,
  160. 'danger'
  161. );
  162. } else {
  163. this.loading.dismiss();
  164. await this.presentToast('认证失败', 1500, 'danger');
  165. }
  166. }, 500);
  167. }
  168. async anthProfile(sex?: string, city?: string) {
  169. let user = Parse.User.current();
  170. // let query = new Parse.Query('Profile');
  171. // query.equalTo('idcard', this.idCard);
  172. // query.equalTo('company', this.company);
  173. // query.notEqualTo('isDeleted', true);
  174. // query.notEqualTo('user', Parse.User.current()?.id);
  175. // let res = await query.first();
  176. // if (res && res.id) {
  177. // this.loading.dismiss();
  178. // await this.presentToast('认证失败,该身份已被认证', 1500, 'danger');
  179. // return;
  180. // }
  181. user?.set('sex', sex);
  182. user?.set('idcard', this.idCard);
  183. user?.set('name', this.name);
  184. await user?.save();
  185. if (!this.profile?.id) {
  186. let obj = Parse.Object.extend('Profile');
  187. this.profile = new obj();
  188. this.profile?.set('company', {
  189. __type: 'Pointer',
  190. className: 'Company',
  191. objectId: this.company,
  192. });
  193. this.profile?.set('user', {
  194. __type: 'Pointer',
  195. className: '_User',
  196. objectId: user?.id,
  197. });
  198. this.profile?.set('mobile', Parse.User.current()?.get('mobile'));
  199. }
  200. this.profile?.set('idcard', this.idCard);
  201. this.profile?.set('name', this.name);
  202. this.profile?.set('cardtype', this.cardtype);
  203. if (this.cardtype === 'cn') {
  204. this.profile?.set('sex', sex);
  205. this.profile?.set('city', city);
  206. this.profile?.set('isCross', true);
  207. this.profile?.set(
  208. 'birthdate',
  209. String(this.getAgeFromIdCard(this.idCard))
  210. );
  211. await this.profile?.save();
  212. if (this.profile?.id) {
  213. localStorage.setItem('profile', JSON.stringify(this.profile.toJSON()));
  214. this.loading.dismiss();
  215. this.isReal = true;
  216. await this.presentToast('认证成功', 1500, 'primary');
  217. setTimeout(() => {
  218. history.back();
  219. }, 1500);
  220. }
  221. } else {
  222. this.profile?.set('eduImage',this.eduImage)
  223. await this.profile?.save();
  224. if (this.profile?.id) {
  225. localStorage.setItem('profile', JSON.stringify(this.profile.toJSON()));
  226. this.loading.dismiss();
  227. this.presentAlert('提交成功,等待审核',true);
  228. }
  229. }
  230. Parse.User.current()?.set('agentLevel', {
  231. __type: 'Pointer',
  232. className: 'UserAgentLevel',
  233. objectId: '2fzw2QMceW',
  234. });
  235. await Parse.User.current()?.save();
  236. }
  237. getAgeFromIdCard(idCard: string): number {
  238. // 提取出生日期部分
  239. const birthDateStr = idCard.substring(6, 14);
  240. const year = parseInt(birthDateStr.substring(0, 4), 10);
  241. const month = parseInt(birthDateStr.substring(4, 6), 10);
  242. const day = parseInt(birthDateStr.substring(6, 8), 10);
  243. // 创建出生日期对象
  244. const birthDate = new Date(year, month - 1, day);
  245. // 获取当前日期
  246. const today = new Date();
  247. // 计算年龄
  248. let age = today.getFullYear() - birthDate.getFullYear();
  249. const monthDiff = today.getMonth() - birthDate.getMonth();
  250. if (
  251. monthDiff < 0 ||
  252. (monthDiff === 0 && today.getDate() < birthDate.getDate())
  253. ) {
  254. age--;
  255. }
  256. return age;
  257. }
  258. async changeFile(e: any) {
  259. this.eduImage = e[0]?.url;
  260. console.log(this.eduImage);
  261. }
  262. async submitRegions() {
  263. if (!this.agreement) {
  264. this.presentToast('请先阅读且同意隐私协议', 1500, 'danger');
  265. return;
  266. }
  267. this.loading = await this.loadCtrl.create();
  268. this.loading.present();
  269. this.time && clearTimeout(this.time);
  270. this.time = setTimeout(async () => {
  271. this.name = this.name.trim();
  272. this.idCard = this.idCard.trim();
  273. console.log(this.name, this.idCard);
  274. if (!this.idCard || !this.name || !this.eduImage) {
  275. this.loading.dismiss();
  276. await this.presentToast('请填写完整信息', 1500, 'danger');
  277. return;
  278. }
  279. this.anthProfile();
  280. }, 500);
  281. }
  282. async presentAlert(msg: string, isBack?: boolean) {
  283. const alert = await this.alertController.create({
  284. header: '提示',
  285. message: msg,
  286. buttons: [
  287. {
  288. text: '好的',
  289. role: 'confirm',
  290. handler: () => {
  291. isBack && history.back();
  292. },
  293. },
  294. ],
  295. });
  296. await alert.present();
  297. }
  298. }