Files
swiftadmin/app/api/controller/Ajax.php

107 lines
3.0 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +08:00
<?php
declare (strict_types=1);
namespace app\api\controller;
use app\ApiController;
use app\common\library\Email;
use app\common\library\Sms;
use app\common\model\system\User;
2022-11-28 19:11:12 +08:00
use PHPMailer\PHPMailer\Exception;
use support\Response;
2022-08-19 19:48:37 +08:00
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
/**
* 异步调用
*/
class Ajax extends ApiController
{
2022-11-28 19:11:12 +08:00
public bool $needLogin = true;
2022-08-19 19:48:37 +08:00
/**
* 发送短信
2022-11-28 19:11:12 +08:00
* @return Response|void
2022-08-19 19:48:37 +08:00
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
*/
public function smsSend()
{
if (request()->isPost()) {
$mobile = input('mobile');
$event = input('event', 'register');
if (!is_mobile($mobile)) {
return $this->error('手机号码不正确');
}
$sms = Sms::instance();
$data = $sms->getLast($mobile);
if ($data && (time() - strtotime($data['create_time'])) < 60) {
return $this->error(__('发送频繁'));
}
2022-11-28 19:11:12 +08:00
$userData = User::getByMobile($mobile);
if (in_array($event, ['register', 'changer']) && $userData) {
2022-08-19 19:48:37 +08:00
return $this->error('当前手机号已被占用');
2022-11-28 19:11:12 +08:00
} else if ($event == 'forgot' && !$userData) {
2022-08-19 19:48:37 +08:00
return $this->error('当前手机号未注册');
}
if ($sms->send($mobile, $event)) {
return $this->success("验证码发送成功!");
} else {
return $this->error($sms->getError());
}
}
return json(['msg' => 'success', 'data' => 'Hello']);
}
/**
* 发送邮件
2022-11-28 19:11:12 +08:00
* @return Response|void
2022-08-19 19:48:37 +08:00
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
2022-11-28 19:11:12 +08:00
* @throws Exception
2022-08-19 19:48:37 +08:00
*/
public function emailSend()
{
if (request()->isPost()) {
$email = input('email');
$event = input('event', 'register');
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
return $this->error('邮件格式不正确');
}
$Ems = Email::instance();
$data = $Ems->getLast($email);
if ($data && (time() - strtotime($data['create_time'])) < 60) {
return $this->error(__('发送频繁'));
}
2022-11-28 19:11:12 +08:00
$userData = User::getByEmail($email);
if (in_array($event, ['register', 'changer']) && $userData) {
2022-08-19 19:48:37 +08:00
return $this->error('当前邮箱已被注册');
2022-11-28 19:11:12 +08:00
} else if ($event == 'forgot' && !$userData) {
2022-08-19 19:48:37 +08:00
return $this->error('当前邮箱不存在');
}
if ($Ems->captcha($email, $event)->send()) {
return $this->success("验证码发送成功!");
} else {
return $this->error($Ems->getError());
}
}
}
}