185 lines
6.6 KiB
PHP
185 lines
6.6 KiB
PHP
<?php
|
||
|
||
namespace app\api\logic;
|
||
|
||
use app\common\service\{UtilsService};
|
||
use app\common\model\decorate\{DecorateHint};
|
||
use app\common\enum\notice\NoticeEnum;
|
||
use app\common\model\notice\EmailRecord;
|
||
use app\common\cache\UserAccountSafeCache;
|
||
use app\common\model\user\{User,UserInfo};
|
||
use app\common\logic\BaseLogic;
|
||
|
||
|
||
/**
|
||
* 短信逻辑
|
||
* Class SmsLogic
|
||
* @package app\api\logic
|
||
*/
|
||
class SmsLogic extends BaseLogic
|
||
{
|
||
|
||
/**
|
||
* @notes 发送验证码
|
||
* @param $params
|
||
* @return false|mixed
|
||
* @author BD
|
||
* @date 2024/9/21 17:29
|
||
*/
|
||
public static function sendCode($params)
|
||
{
|
||
try {
|
||
$scene = NoticeEnum::getSceneByTag($params['scene']);
|
||
if (empty($scene)) {
|
||
throw new \Exception('network.parameterAbnormality');//场景值异常
|
||
}
|
||
|
||
$result = event('Notice', [
|
||
'scene_id' => $scene,
|
||
'params' => [
|
||
'mobile' => $params['mobile'],
|
||
'country_code' => $params['country_code'],
|
||
'code' => mt_rand(100000, 999999),
|
||
]
|
||
]);
|
||
|
||
return $result[0];
|
||
|
||
} catch (\Exception $e) {
|
||
self::$error = $e->getMessage();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 发送邮件
|
||
* @param $params
|
||
* @return false|mixed
|
||
* @author BD
|
||
* @date 2024/9/21 17:29
|
||
*/
|
||
public static function sendEmail($params)
|
||
{
|
||
try {
|
||
if(!filter_var($params['email'], FILTER_VALIDATE_EMAIL)){
|
||
throw new \Exception('network.parameterAbnormality');//请输入正确的邮箱地址
|
||
}
|
||
//判断发送频率
|
||
$time = time() - 1*60;//1分钟发送一次
|
||
|
||
$count = EmailRecord::where(['user_id' => $params['user_id']])->where("create_time > $time")->count();
|
||
if($count > 0) throw new \Exception('captcha.validTips');//验证码5分钟内有效,请勿重复发送
|
||
$count = EmailRecord::where(['ip' => request()->ip()])->where("create_time > $time")->count();
|
||
if($count > 0) throw new \Exception('captcha.validTips');//验证码5分钟内有效,请勿重复发送
|
||
|
||
$email = DecorateHint::findOrEmpty(18)->toArray();
|
||
//多语言替换
|
||
$data = UtilsService::get_langs_data($email['langs'],$params['lang']);
|
||
$code = mt_rand(100000, 999999);
|
||
$content = str_ireplace('{code}', $code, $data['content']);
|
||
|
||
$result = UtilsService::send_mail($params['email'],$data['text'],$content);
|
||
if (!$result) {
|
||
throw new \Exception('network.sendFailed');
|
||
}
|
||
|
||
EmailRecord::create([
|
||
'user_id' => $params['user_id'],
|
||
'email' => $params['email'],
|
||
'subject' => $data['text'],
|
||
'content' => $content,
|
||
'code' => $code,
|
||
'ip' => request()->ip(),
|
||
]);
|
||
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
self::$error = $e->getMessage();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @notes 发送邮件
|
||
* @param $params
|
||
* @return false|mixed
|
||
* @author BD
|
||
* @date 2024/9/21 17:29
|
||
*/
|
||
public static function sendEmailNoLogin($params)
|
||
{
|
||
try {
|
||
//账号安全机制,连续输错后锁定,防止账号密码暴力破解
|
||
$userAccountSafeCache = new UserAccountSafeCache();
|
||
if (!$userAccountSafeCache->isSafe()) {
|
||
throw new \Exception('network.frequentOperation');
|
||
//密码连续' . $userAccountSafeCache->count . '次输入错误,请' . $userAccountSafeCache->minute . '分钟后重试
|
||
}
|
||
|
||
if(!filter_var($params['email'], FILTER_VALIDATE_EMAIL)){
|
||
$userAccountSafeCache->record();
|
||
throw new \Exception('network.parameterAbnormality');//请输入正确的邮箱地址
|
||
}
|
||
//判断发送频率
|
||
$time = time() - 1*60;//1分钟发送一次
|
||
|
||
$user = User::where(['mobile' => $params['mobile'],'country_code' => $params['country_code']])->findOrEmpty();
|
||
if($user->isEmpty()) {
|
||
$userAccountSafeCache->record();
|
||
throw new \Exception('login.userNoExist');//用户不存在
|
||
}
|
||
|
||
$userInfo = UserInfo::where(['user_id' => $user['id']])->findOrEmpty();
|
||
if($userInfo->isEmpty()) {
|
||
throw new \Exception('network.parameterAbnormality');
|
||
}
|
||
|
||
if($userInfo['auth_email'] == 0){
|
||
$userAccountSafeCache->record();
|
||
throw new \Exception('pwd.emailNoExist');//该电子邮箱不存在
|
||
}
|
||
if($userInfo['email'] != $params['email']){
|
||
$userAccountSafeCache->record();
|
||
throw new \Exception('auth.emailError');//请输入正确的邮箱地址
|
||
}
|
||
|
||
|
||
$count = EmailRecord::where(['user_id' => $user['id']])->where("create_time > $time")->count();
|
||
if($count > 0){
|
||
throw new \Exception('captcha.validTips');//验证码5分钟内有效,请勿重复发送
|
||
}
|
||
$count = EmailRecord::where(['ip' => request()->ip()])->where("create_time > $time")->count();
|
||
if($count > 0){
|
||
throw new \Exception('captcha.validTips');//验证码5分钟内有效,请勿重复发送
|
||
}
|
||
|
||
$email = DecorateHint::findOrEmpty(22)->toArray();
|
||
//多语言替换
|
||
$data = UtilsService::get_langs_data($email['langs'],$params['lang']);
|
||
$code = mt_rand(100000, 999999);
|
||
$content = str_ireplace('{code}', $code, $data['content']);
|
||
|
||
$result = UtilsService::send_mail($params['email'],$data['text'],$content);
|
||
if (!$result) {
|
||
throw new \Exception('network.sendFailed');
|
||
}
|
||
|
||
EmailRecord::create([
|
||
'user_id' => $user['id'],
|
||
'email' => $params['email'],
|
||
'subject' => $data['text'],
|
||
'content' => $content,
|
||
'code' => $code,
|
||
'ip' => request()->ip(),
|
||
]);
|
||
|
||
$userAccountSafeCache->relieve();
|
||
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
self::$error = $e->getMessage();
|
||
return false;
|
||
}
|
||
}
|
||
|
||
} |