/*
 * @Author: Maple
 * @Date: 2021-03-22 11:42:26
 * @LastEditors: Do not edit
 * @LastEditTime: 2021-03-29 14:45:43
 * @Description: 全局验证码服务
 */
import { ComponentRef, Injectable, Injector } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { BaseService } from '../core/base.service';

@Injectable({
  providedIn: 'root'
})
export class EACaptchaService extends BaseService {
  // 通过手机号发送短信验证码
  private $api_send_sms_by_mobile = `/api/mdc/pbc/smsSend/getSMVerificationCode?_allow_anonymous=true&_allow_badcode=true`;

  // 验证手机号为平台用户后发送短信验证码
  private $api_send_sms__by_validate_mobile = `/chiauserBasicInfo/forgetPassword/getAccountSMVerificationCode`;

  // 滑块验证码获取短信
  $api_dun_sms = `/scce/pbc/pbc/verification/verificationSlider?_allow_anonymous=true&_allow_badcode=true`;

  // 根据当前登录用户绑定的手机号码获取短信验证码
  $api_captcha_sms_code = `/scce/pbc/pbc/verification/getSMVerificationCodeByToken`;

  // 获取应用租户的管理员用户发送验证码
  $api_getAppLesseeAdminSMVerificationCode = `/api/mdc/cuc/userAuthority/adminSmverificationCode?_allow_badcode=true`;

  /**
   * 根据当前登录用户绑定的手机号码获取短信验证码
   */
  getCaptchaBySMSNoPhone(): Observable<any> {
    return this.request(this.$api_captcha_sms_code, { appId: this.envSrv.getEnvironment().appId }, 'POST', true, 'FORM');
  }

  constructor(public injector: Injector) {
    super(injector);
  }

  /**
   * 发送短信验证码
   * @param mobile 手机号码
   */
  sendSMSCaptchaByMobile(mobile: string): Observable<any> {
    return this.request(
      this.$api_send_sms_by_mobile,
      { appId: this.envSrv.getEnvironment()?.appId, phoneNumber: mobile },
      'POST',
      true,
      'FORM'
    );
  }

  /**
   * 滑块验证获取短信码
   * @param tel 手机号
   * @param validate 滑动验证通过字符串
   * @param url api地址
   */
  getCaptchaByDun(mobile: string, validate: string, url?: string): Observable<any> {
    return this.request(
      url || this.$api_dun_sms,
      { appId: this.envSrv.getEnvironment()?.appId, phoneNumber: mobile, user: mobile, validate },
      'POST',
      true,
      'FORM'
    );
  }
}
