login.component.ts 15 KB

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