yunzer/app/index/controller/BaseController.php
2025-06-07 09:15:14 +08:00

264 lines
6.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* 商业使用授权协议
*
* Copyright (c) 2025 [云泽网]. 保留所有权利.
*
* 本软件仅供评估使用。任何商业用途必须获得书面授权许可。
* 未经授权商业使用本软件属于侵权行为,将承担法律责任。
*
* 授权购买请联系: 357099073@qq.com
* 官方网站: https://www.yunzer.cn
*
* 评估用户须知:
* 1. 禁止移除版权声明
* 2. 禁止用于生产环境
* 3. 禁止转售或分发
*/
declare(strict_types=1);
namespace app\index\controller;
use think\App;
use think\facade\View;
use think\facade\Request;
use think\facade\Db;
use app\service\VisitStatsService;
use PHPMailer\PHPMailer\PHPMailer;
use app\index\model\MailConfig;
use app\index\model\AdminConfig;
use app\index\model\Users;
/**
* 前台控制器基础类
*/
abstract class BaseController
{
/**
* Request实例
* @var \think\Request
*/
protected $request;
protected $visitStats;
/**
* 应用实例
* @var \think\App
*/
protected $app;
/**
* 构造方法
* @access public
* @param App $app 应用对象
*/
public function __construct(App $app)
{
$this->app = $app;
$this->request = $this->app->request;
$this->visitStats = new VisitStatsService();
// 控制器初始化
$this->initialize();
}
/**
* 初始化
*/
protected function initialize()
{
// 记录访问
$this->visitStats->recordVisit($this->getControllerName());
// 获取配置
$configList = AdminConfig::where('config_status', 1)
->order('config_sort DESC')
->select()
->toArray();
// 将配置数据转换为键值对形式
$config = [];
foreach ($configList as $item) {
$config[$item['config_name']] = $item['config_value'];
}
// 判断用户是否登录
$userInfo = [];
if (session('user_id')) {
// 从数据库获取最新用户信息
$user = Users::where('uid', session('user_id'))->find();
if ($user) {
$userInfo = [
'id' => $user->uid,
'name' => $user->name,
'account' => $user->account,
'avatar' => $user->avatar ?? '/static/images/avatar.png',
'is_login' => true,
'last_login_time' => $user->last_login_time
];
} else {
// 用户不存在清除session
session('user_id', null);
session('user_name', null);
$userInfo = ['is_login' => false];
}
} else {
$userInfo = ['is_login' => false];
}
// 设置通用变量
View::assign([
'config' => $config,
'userInfo' => $userInfo
]);
}
/**
* 获取控制器名称移除Controller后缀
* @return string
*/
public function getControllerName()
{
$className = get_class($this);
$className = substr($className, strrpos($className, '\\') + 1);
return str_replace('Controller', '', $className);
}
/**
* 渲染模板输出
* @param string $template 模板文件
* @param array $vars 模板变量
* @return string
*/
protected function fetch($template = '', $vars = [])
{
return View::fetch($template, $vars);
}
/**
* 操作成功跳转
* @param string $msg 提示信息
* @param string $url 跳转地址
* @param mixed $data 返回数据
* @param integer $wait 跳转等待时间
* @return \think\response\Json|string
*/
protected function success($msg = '', $url = null, $data = '', $wait = 3)
{
if (Request::isAjax()) {
return json([
'code' => 1,
'msg' => $msg,
'data' => $data,
'url' => $url
]);
}
return View::fetch('common/success', [
'msg' => $msg,
'url' => $url,
'data' => $data,
'wait' => $wait
]);
}
/**
* 操作失败跳转
* @param string $msg 提示信息
* @param string $url 跳转地址
* @param mixed $data 返回数据
* @param integer $wait 跳转等待时间
* @return \think\response\Json|string
*/
protected function error($msg = '', $url = null, $data = '', $wait = 3)
{
if (Request::isAjax()) {
return json([
'code' => 0,
'msg' => $msg,
'data' => $data,
'url' => $url
]);
}
return View::fetch('common/error', [
'msg' => $msg,
'url' => $url,
'data' => $data,
'wait' => $wait
]);
}
protected function sendEmail($to, $content, $title)
{
// 获取邮件配置
$mailConfig = MailConfig::where('id', 1)->find();
if (!$mailConfig) {
return '邮件配置不存在';
}
//实例化PHPMailer核心类
$mail = new PHPMailer();
//是否启用smtp的debug进行调试 开发环境建议开启 生产环境注释掉即可 默认关闭debug调试模式
$mail->SMTPDebug = 0;
//使用smtp鉴权方式发送邮件
$mail->isSMTP();
//smtp需要鉴权 这个必须是true
$mail->SMTPAuth = true;
//链接qq域名邮箱的服务器地址
$mail->Host = $mailConfig['smtp_host'];
//设置使用ssl加密方式登录鉴权
$mail->SMTPSecure = 'ssl';
//设置ssl连接smtp服务器的远程服务器端口号
$mail->Port = $mailConfig['smtp_port'];
//设置发件人的主机域
$mail->Hostname = $mailConfig['smtp_email'];
//设置发送的邮件的编码
$mail->CharSet = 'UTF-8';
//设置发件人姓名(昵称)
$mail->FromName = $mailConfig['smtp_name'];
//smtp登录的账号
$mail->Username = $mailConfig['smtp_email'];
//smtp登录的密码
$mail->Password = $mailConfig['smtp_password'];
//设置发件人邮箱地址
$mail->setFrom($mailConfig['smtp_email'], $mailConfig['smtp_name']);
//邮件正文是否为html编码
$mail->isHTML(true);
//设置收件人邮箱地址
$mail->addAddress($to);
//添加该邮件的主题
$mail->Subject = $title;
//添加邮件正文
$mail->Body = $content;
try {
$status = $mail->send();
if ($status) {
return '发送成功';
} else {
return '发送失败';
}
} catch (\Exception $e) {
return '发送失败:' . $e->getMessage();
}
}
}