123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- import { Component, OnInit, ViewChild } from '@angular/core';
- import { LoginModule } from './login.module';
- import { AuthService } from '../../services/auth.service';
- import {
- AlertController,
- ModalController,
- ToastController,
- } from '@ionic/angular';
- import * as Parse from 'parse';
- import { Router, ActivatedRoute } from '@angular/router';
- import { HttpClient, HttpHeaders } from '@angular/common/http';
- import { catchError } from 'rxjs/operators';
- import { AgreementComponent } from './agreement/agreement.component';
- @Component({
- standalone: true,
- imports: [LoginModule],
- selector: 'app-login',
- templateUrl: './login.component.html',
- styleUrls: ['./login.component.scss'],
- })
- export class LoginComponent implements OnInit {
- user: { username?: string; password?: string } = {
- username: '',
- password: '',
- };
- currentUser: any;
- constructor(
- public toastController: ToastController,
- public authServ: AuthService,
- private router: Router,
- private activatedRoute: ActivatedRoute,
- private http: HttpClient,
- private modalController: ModalController,
- private alertController: AlertController
- ) {
- this.company = authServ.company;
- }
- company: any;
- appName: any;
- logo: any;
- // 显示登陆、注册内容
- status: string = 'login';
- type: string = 'password';
- loginInfo: any = {
- mobile: '',
- password: '',
- code: '',
- };
- // 忘记密码
- reset: any = {
- mobile: '',
- code: '',
- password: '',
- confirmPassword: '',
- };
- inputType: string = 'password';
- // 注册
- agreement: boolean = false;
- registerAgreement: any;
- registerInfo: any = {
- mobile: '',
- code: '',
- password: '',
- confirmPassword: '',
- invite: '',
- };
- ngOnInit() {
- this.authServ.logout();
- this.activatedRoute.paramMap.subscribe((param) => {
- localStorage.setItem('APP_DEFAULT_COMPANY', 'Qje9D4bqol');
- let APP_DEFAULT_COMPANY = localStorage.getItem('APP_DEFAULT_COMPANY');
- if (APP_DEFAULT_COMPANY) {
- this.company = APP_DEFAULT_COMPANY;
- localStorage.setItem('company', this.company);
- }
- if (param.get('c')) {
- this.company = param.get('c');
- }
- if (this.company) {
- // localStorage.setItem("company", this.company);
- let App = new Parse.Query('App');
- App.equalTo('company', this.company);
- App.first().then((res) => {
- console.log(res);
- if (res && res.id) {
- this.appName = res.get('name');
- this.logo = res.get('logo');
- }
- });
- }
- this.getAgreement();
- let Company = new Parse.Query('Company');
- Company.get(this.company).then((res) => {
- console.log(res, res.get('tabsOption'));
- let value = res.get('tabsOption');
- localStorage.setItem('tabsOption', JSON.stringify(value));
- });
- });
- }
- // 获取验证码
- wait: number = 60;
- waitStatus: boolean = false;
- time() {
- if (this.wait == 0) {
- this.waitStatus = false;
- this.wait = 60;
- } else {
- this.waitStatus = true;
- this.wait--;
- setTimeout(() => {
- this.time();
- }, 1000);
- }
- }
- // 获取验证码
- vcode: string = '';
- mobile: string = '';
- loginToken: string = '';
- timer: boolean = false;
- async sendVerifyCode(mobile: string) {
- if (this.timer) {
- return;
- }
- this.timer = true;
- let a = /^1[3456789]\d{9}$/;
- if (mobile == undefined || !String(mobile).match(a)) {
- const toast = await this.toastController.create({
- message: '请填写正确手机号',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- this.timer = false;
- return;
- }
- if (this.status == 'login') {
- let query = new Parse.Query('_User');
- query.equalTo('company', this.company);
- query.equalTo('mobile', mobile);
- query.select('objectId');
- let r = await query.first();
- if (!r?.id) {
- const toast = await this.toastController.create({
- message: '用户手机号不存在,请先注册',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- this.timer = false;
- return;
- }
- }
- this.http
- .post('https://server.fmode.cn/api/apig/message', {
- company: this.company,
- mobile: mobile,
- })
- .subscribe((res: any) => {
- this.waitStatus = true;
- this.time();
- this.vcode = res.data.code;
- this.timer = false;
- });
- }
- changeType() {
- let type = this.type;
- if (type == 'password') {
- this.type = 'code';
- } else {
- this.type = 'password';
- }
- }
- async login() {
- console.log(this.loginInfo);
- if (this.loginInfo.mobile == undefined || this.loginInfo.mobile == '') {
- const toast = await this.toastController.create({
- message: '请输入手机号',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- let a = /^1[3456789]\d{9}$/;
- if (!String(this.loginInfo.mobile).match(a)) {
- const toast = await this.toastController.create({
- message: '请填写正确手机号',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- if (this.type == 'password') {
- if (this.loginInfo.password == '' || this.loginInfo.password.trim == '') {
- const toast = await this.toastController.create({
- message: '请填写密码',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- this.authServ
- .authMobile(
- this.loginInfo.mobile,
- this.loginInfo.password,
- this.loginInfo.mobile
- )
- .then((data) => {
- console.log(data);
- })
- .catch(async (err) => {
- const toast = await this.toastController.create({
- message: err,
- color: 'danger',
- duration: 1000,
- });
- toast.present();
- });
- } else if (this.type == 'code') {
- if (!this.loginInfo.code) {
- const toast = await this.toastController.create({
- message: '请填写有效验证码',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- this.authServ
- .authMobile(
- this.loginInfo.mobile,
- undefined,
- this.loginInfo.mobile,
- this.loginInfo.code
- )
- .then((data) => {
- console.log(data);
- })
- .catch(async (err) => {
- const toast = await this.toastController.create({
- message: err,
- color: 'danger',
- duration: 1000,
- });
- toast.present();
- });
- }
- }
- // 忘记密码部分逻辑
- back() {
- this.status = 'login';
- }
- togglePassword() {
- if (this.inputType == 'password') {
- this.inputType = 'text';
- } else {
- this.inputType = 'password';
- }
- }
- async resetPassword() {
- let a = /^1[3456789]\d{9}$/;
- if (this.reset.mobile == undefined || !String(this.reset.mobile).match(a)) {
- const toast = await this.toastController.create({
- message: '请输入手机号',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- if (this.reset.code == undefined || this.reset.code == '') {
- const toast = await this.toastController.create({
- message: '请输入验证码',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- if (this.reset.password == undefined || this.reset.password.trim() == '') {
- const toast = await this.toastController.create({
- message: '请输入密码',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- if (
- this.reset.password.length < 6 ||
- this.reset.password.length.length > 20
- ) {
- const toast = await this.toastController.create({
- message: '密码长度不得小于6位或大于20位',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- if (
- this.reset.confirmPassword == undefined ||
- this.reset.confirmPassword.trim() == ''
- ) {
- const toast = await this.toastController.create({
- message: '请确认密码',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- if (this.reset.password.trim() != this.reset.confirmPassword.trim()) {
- const toast = await this.toastController.create({
- message: '两次输入密码不一致',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- let headers: HttpHeaders = new HttpHeaders({});
- headers.append('Content-Type', 'application/json');
- headers.set('Access-Control-Allow-Origin', '*');
- this.http
- .post(
- `https://server.fmode.cn/api/reset_password`,
- {
- company: this.company,
- code: this.reset.code,
- mobile: this.reset.mobile,
- password: this.reset.password,
- },
- { headers }
- )
- .pipe(
- catchError(async (e) => {
- // 显示报错
- const toast = await this.toastController.create({
- message: e.message,
- color: 'danger',
- duration: 1000,
- });
- toast.present();
- console.log(e);
- return;
- })
- )
- .subscribe(async (res: any) => {
- console.log(res);
- if (res && res.code == 200) {
- const toast = await this.toastController.create({
- message: '密码重置成功',
- color: 'success',
- duration: 1000,
- });
- toast.present();
- } else {
- const toast = await this.toastController.create({
- // 接口返回的错误,提示用户
- message: res.mess,
- color: 'danger',
- duration: 1000,
- });
- toast.present();
- }
- });
- }
- // 注册账号
- agree() {
- this.agreement = !this.agreement;
- }
- getAgreement() {
- let Agreement = new Parse.Query('ContractAgreement');
- Agreement.equalTo('company', this.company);
- Agreement.equalTo('type', 'register');
- Agreement.first().then((res) => {
- console.log(res);
- this.registerAgreement = res;
- });
- }
- async showAgreement() {
- const modal = await this.modalController.create({
- component: AgreementComponent,
- cssClass: 'my-custom-class',
- componentProps: {
- agreement: this.registerAgreement,
- },
- });
- return await modal.present();
- }
- async registerUser() {
- let a = /^1[3456789]\d{9}$/;
- if (
- this.registerInfo.mobile == undefined ||
- !String(this.registerInfo.mobile).match(a)
- ) {
- const toast = await this.toastController.create({
- message: '请填写正确的手机号',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- if (this.registerInfo.code == undefined || this.registerInfo.code == '') {
- const toast = await this.toastController.create({
- message: '请输入验证码',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- if (
- this.registerInfo.password == undefined ||
- this.registerInfo.password.trim() == ''
- ) {
- const toast = await this.toastController.create({
- message: '请输入密码',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- if (
- this.registerInfo.password.length < 6 ||
- this.registerInfo.password.length.length > 20
- ) {
- const toast = await this.toastController.create({
- message: '密码长度不得小于6位或大于20位',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- if (
- this.registerInfo.confirmPassword == undefined ||
- this.registerInfo.confirmPassword.trim() == ''
- ) {
- const toast = await this.toastController.create({
- message: '请确认密码',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- if (
- this.registerInfo.password.trim() !=
- this.registerInfo.confirmPassword.trim()
- ) {
- const toast = await this.toastController.create({
- message: '两次输入密码不一致',
- color: 'warning',
- duration: 1000,
- });
- toast.present();
- return;
- }
- if (!this.agreement) {
- const alert = await this.alertController.create({
- cssClass: 'my-custom-class',
- header: '提示',
- message: '请先勾选数据用户服务及隐私协议',
- buttons: [
- {
- text: '取消',
- role: 'cancel',
- cssClass: 'secondary',
- handler: (blah) => {
- console.log('Confirm Cancel: blah');
- },
- },
- {
- text: '同意',
- handler: () => {
- this.agreement = true;
- },
- },
- ],
- });
- await alert.present();
- return;
- }
- let data = this.registerInfo.invite
- ? {
- company: this.company,
- code: this.registerInfo.code,
- mobile: this.registerInfo.mobile,
- password: this.registerInfo.password,
- invite: this.registerInfo.invite,
- }
- : {
- company: this.company,
- code: this.registerInfo.code,
- mobile: this.registerInfo.mobile,
- password: this.registerInfo.password,
- };
- this.http
- .post(`https://server.fmode.cn/api/auth/register`, data)
- .pipe(
- catchError(async (e) => {
- // 显示报错
- const toast = await this.toastController.create({
- message: e.message,
- color: 'danger',
- duration: 1000,
- });
- toast.present();
- console.log(e);
- return;
- })
- )
- .subscribe(async (res: any) => {
- console.log(res);
- if (res && res.code == 200) {
- const toast = await this.toastController.create({
- message: res.msg,
- color: 'success',
- duration: 1000,
- });
- toast.present();
- this.registerInfo = {
- mobile: '',
- code: '',
- password: '',
- confirmPassword: '',
- invite: '',
- };
- this.status = 'login';
- } else {
- const toast = await this.toastController.create({
- // 接口返回的错误,提示用户
- message: res.mess,
- color: 'danger',
- duration: 1000,
- });
- toast.present();
- }
- });
- }
- }
|