login.component.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. import { Component, OnInit, ViewChild } from '@angular/core';
  2. import { AuthService } from '../../services/auth.service';
  3. import * as Parse from 'parse';
  4. import { ActivatedRoute } from '@angular/router';
  5. import { HttpClient, HttpHeaders } from '@angular/common/http';
  6. import { catchError } from 'rxjs/operators';
  7. import { AgreementComponent } from './agreement/agreement.component';
  8. import { FormsModule, ReactiveFormsModule } from '@angular/forms';
  9. import { CommonModule } from '@angular/common';
  10. import {
  11. ionicStandaloneModules,
  12. AlertController,
  13. ToastController,
  14. ModalController,
  15. LoadingController,
  16. } from '../ionic-standalone.modules';
  17. @Component({
  18. standalone: true,
  19. imports: [
  20. CommonModule,
  21. FormsModule,
  22. ReactiveFormsModule,
  23. ...ionicStandaloneModules,
  24. ],
  25. selector: 'app-login',
  26. templateUrl: './login.component.html',
  27. styleUrls: ['./login.component.scss'],
  28. })
  29. export class LoginComponent implements OnInit {
  30. user: { username?: string; password?: string } = {
  31. username: '',
  32. password: '',
  33. };
  34. currentUser: any;
  35. constructor(
  36. public toastController: ToastController,
  37. public authServ: AuthService,
  38. // private router: Router,
  39. private activatedRoute: ActivatedRoute,
  40. private http: HttpClient,
  41. private modalCtrl: ModalController,
  42. private alertController: AlertController,
  43. private loadingCtrl: LoadingController
  44. ) {
  45. this.company = authServ.company;
  46. }
  47. company: any;
  48. appName: any;
  49. logo: any;
  50. // 显示登陆、注册内容
  51. status: string = 'login';
  52. type: string = 'password';
  53. loginInfo: any = {
  54. mobile: '',
  55. password: '',
  56. code: '',
  57. };
  58. // 忘记密码
  59. reset: any = {
  60. mobile: '',
  61. code: '',
  62. password: '',
  63. confirmPassword: '',
  64. };
  65. inputType: string = 'password';
  66. // 注册
  67. agreement: boolean = false;
  68. registerAgreement: any;
  69. registerInfo: any = {
  70. mobile: '',
  71. code: '',
  72. password: '',
  73. confirmPassword: '',
  74. invite: '',
  75. };
  76. ngOnInit() {
  77. let invite = localStorage.getItem('invite');
  78. console.log('invite:',invite);
  79. if(invite) this.registerInfo.invite = invite;
  80. this.authServ.logout();
  81. this.activatedRoute.paramMap.subscribe((param) => {
  82. localStorage.setItem('APP_DEFAULT_COMPANY', 'Qje9D4bqol');
  83. let APP_DEFAULT_COMPANY = localStorage.getItem('APP_DEFAULT_COMPANY');
  84. if (APP_DEFAULT_COMPANY) {
  85. this.company = APP_DEFAULT_COMPANY;
  86. localStorage.setItem('company', this.company);
  87. }
  88. if (param.get('c')) {
  89. this.company = param.get('c');
  90. }
  91. if (this.company) {
  92. // localStorage.setItem("company", this.company);
  93. let App = new Parse.Query('App');
  94. App.equalTo('company', this.company);
  95. App.first().then((res) => {
  96. console.log(res);
  97. if (res && res.id) {
  98. this.appName = res.get('name');
  99. this.logo = res.get('logo');
  100. }
  101. });
  102. }
  103. this.getAgreement();
  104. let Company = new Parse.Query('Company');
  105. Company.get(this.company).then((res) => {
  106. console.log(res, res.get('tabsOption'));
  107. let value = res.get('tabsOption');
  108. localStorage.setItem('tabsOption', JSON.stringify(value));
  109. });
  110. });
  111. }
  112. // 获取验证码
  113. wait: number = 60;
  114. waitStatus: boolean = false;
  115. time() {
  116. if (this.wait == 0) {
  117. this.waitStatus = false;
  118. this.wait = 60;
  119. } else {
  120. this.waitStatus = true;
  121. this.wait--;
  122. setTimeout(() => {
  123. this.time();
  124. }, 1000);
  125. }
  126. }
  127. // 获取验证码
  128. vcode: string = '';
  129. mobile: string = '';
  130. loginToken: string = '';
  131. timer: boolean = false;
  132. async sendVerifyCode(mobile: string) {
  133. if (this.timer) {
  134. return;
  135. }
  136. this.timer = true;
  137. let a = /^1[3456789]\d{9}$/;
  138. if (mobile == undefined || !String(mobile).match(a)) {
  139. const toast = await this.toastController.create({
  140. message: '请填写正确手机号',
  141. color: 'warning',
  142. duration: 1000,
  143. });
  144. toast.present();
  145. this.timer = false;
  146. return;
  147. }
  148. if (this.status == 'login') {
  149. let query = new Parse.Query('_User');
  150. query.equalTo('company', this.company);
  151. query.equalTo('mobile', mobile);
  152. query.select('objectId');
  153. let r = await query.first();
  154. if (!r?.id) {
  155. const toast = await this.toastController.create({
  156. message: '用户手机号不存在,请先注册',
  157. color: 'warning',
  158. duration: 1000,
  159. });
  160. toast.present();
  161. this.timer = false;
  162. return;
  163. }
  164. }
  165. this.http
  166. .post('https://server.fmode.cn/api/apig/message', {
  167. company: this.company,
  168. mobile: mobile,
  169. })
  170. .subscribe((res: any) => {
  171. this.waitStatus = true;
  172. this.time();
  173. this.vcode = res.data.code;
  174. this.timer = false;
  175. });
  176. }
  177. changeType() {
  178. let type = this.type;
  179. if (type == 'password') {
  180. this.type = 'code';
  181. } else {
  182. this.type = 'password';
  183. }
  184. }
  185. async login() {
  186. const loading = await this.loadingCtrl.create({
  187. message: '正在登录,请稍等',
  188. });
  189. loading.present();
  190. console.log(this.loginInfo);
  191. if (this.loginInfo.mobile == undefined || this.loginInfo.mobile == '') {
  192. loading.dismiss();
  193. const toast = await this.toastController.create({
  194. message: '请输入手机号',
  195. color: 'warning',
  196. duration: 1000,
  197. });
  198. toast.present();
  199. return;
  200. }
  201. let a = /^1[3456789]\d{9}$/;
  202. if (!String(this.loginInfo.mobile).match(a)) {
  203. loading.dismiss();
  204. const toast = await this.toastController.create({
  205. message: '请填写正确手机号',
  206. color: 'warning',
  207. duration: 1000,
  208. });
  209. toast.present();
  210. return;
  211. }
  212. if (this.type == 'password') {
  213. if (this.loginInfo.password == '' || this.loginInfo.password.trim == '') {
  214. loading.dismiss();
  215. const toast = await this.toastController.create({
  216. message: '请填写密码',
  217. color: 'warning',
  218. duration: 1000,
  219. });
  220. toast.present();
  221. return;
  222. }
  223. this.authServ
  224. .authMobile(
  225. this.loginInfo.mobile,
  226. this.loginInfo.password,
  227. this.loginInfo.mobile
  228. )
  229. .then((data) => {
  230. console.log(data);
  231. loading.dismiss();
  232. })
  233. .catch(async (err) => {
  234. loading.dismiss();
  235. const toast = await this.toastController.create({
  236. message: err,
  237. color: 'danger',
  238. duration: 1000,
  239. });
  240. toast.present();
  241. });
  242. } else if (this.type == 'code') {
  243. if (!this.loginInfo.code) {
  244. loading.dismiss();
  245. const toast = await this.toastController.create({
  246. message: '请填写有效验证码',
  247. color: 'warning',
  248. duration: 1000,
  249. });
  250. toast.present();
  251. return;
  252. }
  253. this.authServ
  254. .authMobile(
  255. this.loginInfo.mobile,
  256. undefined,
  257. this.loginInfo.mobile,
  258. this.loginInfo.code
  259. )
  260. .then((data) => {
  261. console.log(data);
  262. loading.dismiss();
  263. })
  264. .catch(async (err) => {
  265. loading.dismiss();
  266. const toast = await this.toastController.create({
  267. message: err,
  268. color: 'danger',
  269. duration: 1000,
  270. });
  271. toast.present();
  272. });
  273. }
  274. }
  275. // 忘记密码部分逻辑
  276. back() {
  277. this.status = 'login';
  278. }
  279. togglePassword() {
  280. if (this.inputType == 'password') {
  281. this.inputType = 'text';
  282. } else {
  283. this.inputType = 'password';
  284. }
  285. }
  286. async resetPassword() {
  287. let a = /^1[3456789]\d{9}$/;
  288. if (this.reset.mobile == undefined || !String(this.reset.mobile).match(a)) {
  289. const toast = await this.toastController.create({
  290. message: '请输入手机号',
  291. color: 'warning',
  292. duration: 1000,
  293. });
  294. toast.present();
  295. return;
  296. }
  297. if (this.reset.code == undefined || this.reset.code == '') {
  298. const toast = await this.toastController.create({
  299. message: '请输入验证码',
  300. color: 'warning',
  301. duration: 1000,
  302. });
  303. toast.present();
  304. return;
  305. }
  306. if (this.reset.password == undefined || this.reset.password.trim() == '') {
  307. const toast = await this.toastController.create({
  308. message: '请输入密码',
  309. color: 'warning',
  310. duration: 1000,
  311. });
  312. toast.present();
  313. return;
  314. }
  315. if (
  316. this.reset.password.length < 6 ||
  317. this.reset.password.length.length > 20
  318. ) {
  319. const toast = await this.toastController.create({
  320. message: '密码长度不得小于6位或大于20位',
  321. color: 'warning',
  322. duration: 1000,
  323. });
  324. toast.present();
  325. return;
  326. }
  327. if (
  328. this.reset.confirmPassword == undefined ||
  329. this.reset.confirmPassword.trim() == ''
  330. ) {
  331. const toast = await this.toastController.create({
  332. message: '请确认密码',
  333. color: 'warning',
  334. duration: 1000,
  335. });
  336. toast.present();
  337. return;
  338. }
  339. if (this.reset.password.trim() != this.reset.confirmPassword.trim()) {
  340. const toast = await this.toastController.create({
  341. message: '两次输入密码不一致',
  342. color: 'warning',
  343. duration: 1000,
  344. });
  345. toast.present();
  346. return;
  347. }
  348. let headers: HttpHeaders = new HttpHeaders({});
  349. headers.append('Content-Type', 'application/json');
  350. headers.set('Access-Control-Allow-Origin', '*');
  351. this.http
  352. .post(
  353. `https://server.fmode.cn/api/auth/reset_password`,
  354. {
  355. company: this.company,
  356. code: this.reset.code,
  357. mobile: this.reset.mobile,
  358. password: this.reset.password,
  359. },
  360. { headers }
  361. )
  362. .pipe(
  363. catchError(async (e) => {
  364. // 显示报错
  365. const toast = await this.toastController.create({
  366. message: e.error?.mess || '服务器错误',
  367. color: 'danger',
  368. duration: 1000,
  369. });
  370. toast.present();
  371. console.log(e);
  372. return;
  373. })
  374. )
  375. .subscribe(async (res: any) => {
  376. console.log(res);
  377. if (res && res.code == 200) {
  378. const toast = await this.toastController.create({
  379. message: '密码重置成功',
  380. color: 'success',
  381. duration: 1000,
  382. });
  383. toast.present();
  384. } else {
  385. const toast = await this.toastController.create({
  386. // 接口返回的错误,提示用户
  387. message: res.mess,
  388. color: 'danger',
  389. duration: 1000,
  390. });
  391. toast.present();
  392. }
  393. });
  394. }
  395. // 注册账号
  396. agree() {
  397. this.agreement = !this.agreement;
  398. }
  399. getAgreement() {
  400. let Agreement = new Parse.Query('ContractAgreement');
  401. Agreement.equalTo('company', this.company);
  402. Agreement.equalTo('type', 'register');
  403. Agreement.first().then((res) => {
  404. console.log(res);
  405. this.registerAgreement = res;
  406. });
  407. }
  408. async showAgreement() {
  409. const modal = await this.modalCtrl.create({
  410. component: AgreementComponent,
  411. cssClass: 'my-custom-class',
  412. componentProps: {
  413. agreement: this.registerAgreement,
  414. },
  415. });
  416. return await modal.present();
  417. }
  418. async registerUser() {
  419. let a = /^1[3456789]\d{9}$/;
  420. if (
  421. this.registerInfo.mobile == undefined ||
  422. !String(this.registerInfo.mobile).match(a)
  423. ) {
  424. const toast = await this.toastController.create({
  425. message: '请填写正确的手机号',
  426. color: 'warning',
  427. duration: 1000,
  428. });
  429. toast.present();
  430. return;
  431. }
  432. if (this.registerInfo.code == undefined || this.registerInfo.code == '') {
  433. const toast = await this.toastController.create({
  434. message: '请输入验证码',
  435. color: 'warning',
  436. duration: 1000,
  437. });
  438. toast.present();
  439. return;
  440. }
  441. if (
  442. this.registerInfo.password == undefined ||
  443. this.registerInfo.password.trim() == ''
  444. ) {
  445. const toast = await this.toastController.create({
  446. message: '请输入密码',
  447. color: 'warning',
  448. duration: 1000,
  449. });
  450. toast.present();
  451. return;
  452. }
  453. if (
  454. this.registerInfo.password.length < 6 ||
  455. this.registerInfo.password.length.length > 20
  456. ) {
  457. const toast = await this.toastController.create({
  458. message: '密码长度不得小于6位或大于20位',
  459. color: 'warning',
  460. duration: 1000,
  461. });
  462. toast.present();
  463. return;
  464. }
  465. if (
  466. this.registerInfo.confirmPassword == undefined ||
  467. this.registerInfo.confirmPassword.trim() == ''
  468. ) {
  469. const toast = await this.toastController.create({
  470. message: '请确认密码',
  471. color: 'warning',
  472. duration: 1000,
  473. });
  474. toast.present();
  475. return;
  476. }
  477. if (
  478. this.registerInfo.password.trim() !=
  479. this.registerInfo.confirmPassword.trim()
  480. ) {
  481. const toast = await this.toastController.create({
  482. message: '两次输入密码不一致',
  483. color: 'warning',
  484. duration: 1000,
  485. });
  486. toast.present();
  487. return;
  488. }
  489. if (!this.agreement) {
  490. const alert = await this.alertController.create({
  491. cssClass: 'my-custom-class',
  492. header: '提示',
  493. message: `请先勾选${this.registerAgreement?.get('title')}`,
  494. buttons: [
  495. {
  496. text: '取消',
  497. role: 'cancel',
  498. cssClass: 'secondary',
  499. handler: (blah) => {
  500. console.log('Confirm Cancel: blah');
  501. },
  502. },
  503. {
  504. text: '同意',
  505. handler: () => {
  506. this.agreement = true;
  507. },
  508. },
  509. ],
  510. });
  511. await alert.present();
  512. return;
  513. }
  514. let data = this.registerInfo.invite
  515. ? {
  516. company: this.company,
  517. code: this.registerInfo.code,
  518. mobile: this.registerInfo.mobile,
  519. password: this.registerInfo.password,
  520. invite: this.registerInfo.invite,
  521. }
  522. : {
  523. company: this.company,
  524. code: this.registerInfo.code,
  525. mobile: this.registerInfo.mobile,
  526. password: this.registerInfo.password,
  527. };
  528. this.http
  529. .post(`https://server.fmode.cn/api/auth/register`, data)
  530. .pipe(
  531. catchError(async (e) => {
  532. // 显示报错
  533. const toast = await this.toastController.create({
  534. message: e.message,
  535. color: 'danger',
  536. duration: 1000,
  537. });
  538. toast.present();
  539. console.log(e);
  540. return;
  541. })
  542. )
  543. .subscribe(async (res: any) => {
  544. console.log(res);
  545. if (res && res.code == 200) {
  546. const toast = await this.toastController.create({
  547. message: res.msg,
  548. color: 'success',
  549. duration: 1000,
  550. });
  551. toast.present();
  552. this.registerInfo = {
  553. mobile: '',
  554. code: '',
  555. password: '',
  556. confirmPassword: '',
  557. invite: '',
  558. };
  559. this.status = 'login';
  560. } else {
  561. const toast = await this.toastController.create({
  562. // 接口返回的错误,提示用户
  563. message: res.mess,
  564. color: 'danger',
  565. duration: 1000,
  566. });
  567. toast.present();
  568. }
  569. });
  570. }
  571. }