邮箱配置主要是将管理员数据做归属,便于后续业务处理与管理。部门配置功能与级别配置功能也是本次后台功能规划的一个环节。具体见下图:
一、创建表
1、语句
CREATE TABLE `cluster_control`.`nc_param_mail` (
`id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT '编号',
`title` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '邮箱服务标题',
`username` varchar(250) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '发件人邮箱',
`smtp_address` varchar(250) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '邮箱服务地址',
`smtp_port` smallint(5) UNSIGNED NOT NULL DEFAULT 0 COMMENT '邮箱服务端口',
`smtp_password` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '邮箱服务授权码',
`smtp_lifespan` int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '有效期',
`smtp_protocol` varchar(5) CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '邮箱服务协议',
`status` tinyint(1) UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态 1:启用 0:禁用',
`create_time` datetime NOT NULL COMMENT '创建时间',
`update_time` datetime NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
PRIMARY KEY (`id`),
UNIQUE INDEX `m_t`(`title`) USING BTREE,
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '邮箱配置表';
2、表结构
二、级别配置路由管理
在根目录下route文件夹中app.php文件中,添加邮箱配置功能路由,代码如下:
// 邮箱配置操作
Route::get('mail/get_list','param.Mail/getList');// 获取邮箱配置列表
Route::post('mail/get_all','param.Mail/getAll');// 获取所有邮箱配置数据
Route::post('mail/get_info','param.Mail/getInfo');// 获取单个邮箱配置数据
Route::post('mail/save_info','param.Mail/saveInfo');// 保存邮箱配置数据
Route::post('mail/status_info','param.Mail/statusInfo');// 启禁用邮箱配置数据
Route::post('mail/delete_info','param.Mail/deleteInfo');// 删除邮箱配置数据
三、级别配置控制管理
在根目录下的app文件夹下的controller文件夹中,创建文件夹并命名param,在param文件夹中接着创建邮箱配置控制器并命名为Mail。具体代码如下:
<?php
/**
* 邮箱配置控制管理
* User: 龙哥·三年风水
* Date: 2024/11/28
* Time: 11:10
*/
namespace app\controller\param;
use app\controller\Base;
use app\model\param\Mail as MailModel;
class Mail extends Base
{
// 分页数据
public function getList(){
$param = $this->request->param();
$where = [];
if (!empty($param['id'])) $where[] = ['id', '=', $param['id']];
if (!empty($param['title'])) $where[] = ['title', 'LIKE', $param['title'] . '%'];
if (!empty($param['username'])) $where[] = ['username', 'LIKE', $param['username'] . '%'];
$resList = MailModel::getRedisList($where , [$param['currentPage'], $param['currentSize']]);
$list = [];
foreach ($resList['info'] as $k => $v) {
$list[$k] = MailModel::dataFind(['id' => $v],'id,title,username,smtp_address,smtp_port,smtp_lifespan,smtp_protocol,status,create_time,update_time',true);
}
$res['list'] = $list;
$res['meat'] = $resList['page'];
return succ('操作成功',$res);
}
// 所有数据
public function getAll(){
$list = MailModel::getAll(['status' => 1],'id,title','create_time DESC');
return succ('操作成功',$list);
}
// 单个数据
public function getInfo(){
$id = $this->request->param('id', 0);
if(empty($id))return err('主键参数必须上传');
$data = MailModel::dataFind(['id' => $id], 'id,title,username,smtp_address,smtp_port,smtp_password,smtp_lifespan,smtp_protocol,status',true);
return succ('操作成功',$data);
}
// 保存数据
public function saveInfo(){
$this->checkRoleMenu('ParamMailIndexSave');//验证权限
$param = $this->request->param();//接收参数
$validate = new \app\validate\param\Mail;
if (!$validate->check($param)) return err($validate->getError());//验证参数
$data['title'] = $param['title'];
$data['username'] = $param['username'];
$data['smtp_address'] = $param['smtp_address'];
$data['smtp_port'] = $param['smtp_port'];
$data['smtp_password'] = $param['smtp_password'];
$data['smtp_lifespan'] = $param['smtp_lifespan'];
$data['smtp_protocol'] = $param['smtp_protocol'];
$data['update_time'] = date('Y-m-d H:i:s', time());
if (empty($param['id'])){
$data['status'] = 1;
$data['create_time'] = date('Y-m-d H:i:s', time());
}
MailModel::redisSave($data, $param['id']);
return succ('操作成功', []);
}
// 启禁用数据
public function statusInfo(){
$this->checkRoleMenu('ParamMailIndexSave');//验证权限
$param = $this->request->param();
if (!is_array($param['id']) || count($param['id']) === 0) return err('唯一编号必须是数组且至少需要一个');
if (!in_array($param['status'],[0,1])) return err('状态必须选择一种');
MailModel::mysqlSave(['status' =>$param['status']], [['id','in',$param['id']]]);
return succ('操作成功', []);
}
// 删除数据
public function deleteInfo(){
$this->checkRoleMenu('ParamMailIndexDelete');//验证权限
$id = $this->request->param('id',0);
if (!is_array($id) || count($id) === 0) return err('请上传参数');
MailModel::dataDetele($id);
return succ('操作成功', []);
}
}
四、级别配置数据管理
在根目录下app文件夹下model文件夹中创建文件夹并命名为param,在param文件夹中创建邮箱配置数据文件并命名为Mail。具体代码如下:
<?php
/**
* 邮箱配置数据管理
* User: 龙哥·三年风水
* Date: 2024/11/28
* Time: 11:11
*/
namespace app\model\param;
use app\model\Tools;
class Mail extends Tools
{
protected static $name = 'param_mail';
protected static $connection = 'mysql';
protected $pk = 'id';
protected static $unsetField = ['update_time','create_time'];
//获取分页数据
public static function getRedisList($where = [], $limit = [])
{
return self::dataRedisLists(self::$name, $where, $limit, self::$connection);
}
//获取所有数据
public static function getAll($where = [], $field = '*', $order = '')
{
return self::dataAlls(self::$name, $where, $field, $order, [], self::$connection);
}
//获取单条数据
public static function dataFind($where = [], $field, $findType = true){
return self::dataFinds(self::$name, $where, $field, $findType, self::$connection);
}
//保存数据-Redis版本
public static function redisSave($data, $pk = '')
{
return self::dataRedisSaves(self::$name, $data, $pk, self::$connection);
}
//保存数据-MySQL版本
public static function mysqlSave($data, $where = []){
return self::dataSaves(self::$name, $data, $where, self::$connection);
}
//删除数据
public static function dataDetele($pk = '')
{
self::dataRedisDeteles(self::$name, $pk, self::$connection); // TODO: Change the autogenerated stub
}
}
五、级别配置验证管理
在根目录下app文件夹下validate文件夹中创建文件夹并命名为param,在param文件夹中创建邮箱配置验证文件并命名为Mail。具体代码如下:
<?php
/**
* 邮箱配置验证管理
* User: 龙哥·三年风水
* Date: 2024/11/28
* Time: 11:39
*/
namespace app\validate\param;
use app\validate\Tools;
use app\model\param\Mail as MailModel;
class Mail extends Tools
{
protected $rule = [
'id' => 'require|number',
'title' => 'require|length:2,20|unique:param_mail',
'username' => 'require|length:8,250|email',
'smtp_address' => 'require|length:10,250|checkSmtp',
'smtp_port' => 'require|number|checkSmtp',
'smtp_password' => 'require|length:10,50',
'smtp_lifespan' => 'require|number',
'smtp_protocol' => 'require|length:2,5'
];
protected $message = [
'id.require' => '编号必须填写',
'id.number' => '编号必须是数字类型',
'title.require' => '标题必须填写',
'title.length' => '标题必须是2到20位',
'title.unique' => '标题必须唯一',
'username.require' => '发件人邮箱必须填写',
'username.length' => '发件人邮箱必须是8到250位',
'username.email' => '发件人邮箱必须是邮箱格式',
'smtp_address.require' => '地址必须填写',
'smtp_address.length' => '地址必须是2到250位',
'smtp_port.require' => '端口号必须填写',
'smtp_port.number' => '端口号必须是数字类型',
'smtp_password.require' => '授权码必须填写',
'smtp_password.length' => '授权码必须是10到50位',
'smtp_lifespan.require' => '有效期必须填写',
'smtp_lifespan.number' => '有效期必须是数字类型',
'smtp_protocol.require' => '协议必须填写',
'smtp_protocol.length' => '协议必须是2到5位'
];
protected function checkSmtp($value, $rule, $data, $field){
$where[] = ['smtp_address','=',$data['smtp_address']];
$where[] = ['smtp_port','=',$data['smtp_port']];
if($data['id'] > 0)$where[] = ['id','<>',$data['id']];
$id = MailModel::dataFind($where,'id',false);
if(empty($id))return true;
return "邮件服务已存在";
}
}