Files
YwxAppThink/ywxapp/library/Email.php
T
ywxapp 1d49e6f5ee feat: 公共表更名 common/member 前缀 + 插件命令自动加载 + 版本 1.1.1
- 18 张公共表更名(addon/attachment/configure/links/spider_log/spider_stat/sms/notice/ad/task/prop/medal/help/card -> common_*,member_wallets->member_wallet,score_rule/score_log -> member_*,addon_config->common_addonconf),模型全部对齐新表名,Db 直引用清零
- Attachment 模型补  表名绑定,修复富文本上传查 wxapp_attachment 1146 隐患
- install.sql + 迁移 SQL:backend_admin/backend_role delete_at 默认 0,修复软删除(NULL != 0)误过滤导致后台菜单为空
- AppService::boot() 支持插件 info.php 声明 commands 自动注册插件命令(psr-4 自动加载,坏类名自动跳过)
- 各插件(haonav/mqttbroker/wxchat/articles/blog/forum 等)字段与配置同步调整
- 框架版本 1.1.0 -> 1.1.1
2026-08-20 20:19:15 +08:00

160 lines
4.4 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: ywxapp<admin@ywxapp.cn>
// +----------------------------------------------------------------------
namespace ywxapp\library;
use think\facade\Event;
use ywxapp\library\Result;
use ywxapp\model\CommonEmail as EmailModel;
use ywxapp\model\MemberUser as UsersModel;
/**
* 邮箱验证码类.
*/
class Email
{
/**
* 验证码有效时长
* @var int
*/
protected $expire = 120;
/**
* 最大允许检测的次数.
* @var int
*/
protected $maxCheckNums = 10;
/**
* 获取当前对象实例.
*
* @return EmailModel
*/
public static function instance()
{
return app()->email;
}
/**
* 发送验证码
*
* @param int $email 邮箱
* @param int $code 验证码,为空时将自动生成4位数字
* @param string $event 事件
*
* @return bool
*/
public function sendEmail($emailAddr, $code = null, $event = 'default')
{
$lastEmail = EmailModel::where('email', $emailAddr)
->where('event', $event)
->order('id', 'DESC')
->find();
Event::trigger('email_get', $lastEmail, true);
if ($lastEmail && time() - $lastEmail['create_at'] < 60)
Result::instance()->error(message: ('发送频繁'));
if ($event) {
$userinfo = UsersModel::where('email', $emailAddr)->find();
if ($event == 'register' && $userinfo)
Result::instance()->error(('已被注册'));
elseif (in_array($event, ['changeemail']) && $userinfo)
Result::instance()->error(('已被占用'));
elseif (in_array($event, ['changepwd', 'resetpwd']) && !$userinfo)
Result::instance()->error(('未注册'));
}
$code = is_null($code) ? mt_rand(100000, 999999) : $code;
$time = time();
$ip = request()->ip();
$email = EmailModel::create([
'event' => $event,
'email' => $emailAddr,
'code' => $code,
'ip' => $ip,
'create_at' => $time,
]);
$result = Event::trigger('email_send', $email, true);
if (!$result)
Result::instance()->error(message: ('发送失败'));
Result::instance()->success(message: ('发送成功'));
}
/**
* 发送通知.
*
* @param mixed $email 邮箱,多个以,分隔
* @param string $msg 消息内容
* @param string $template 消息模板
*
* @return bool
*/
public function notice($email, $msg = '', $template = null)
{
$params = [
'email' => $email,
'msg' => $msg,
'template' => $template,
];
$result = Event::trigger('email_notice', $params, true);
return $result ? true : false;
}
/**
* 校验验证码
*
* @param int $email 邮箱
* @param int $code 验证码
* @param string $event 事件
*
* @return bool
*/
public function check($emailAddr, $code, $event = 'default')
{
$lastEmail = EmailModel::where('email', $emailAddr)
->where('event', $event)
->order('id', 'DESC')
->find();
if (!$lastEmail || $code != $lastEmail['code'])
Result::instance()->error(message: ('验证码不正确'));
if ($lastEmail['create_at'] > time() - $this->expire)
Result::instance()->error(message: ('验证码已经过期'));
if ($code != $lastEmail['code'])
Result::instance()->error(message: ('验证码不正确'));
$result = Event::trigger('ems_check', $lastEmail, true);
$lastEmail->status = 1;
$lastEmail->save();
return $result;
}
/**
* 清空指定邮箱验证码
*
* @param int $email 邮箱
* @param string $event 事件
*
* @return bool
*/
public static function flush($email, $event = 'default')
{
EmailModel::where(['email' => $email, 'event' => $event])->delete();
Event::trigger('email_flush');
return true;
}
}