添加邮件管理模块
This commit is contained in:
@@ -5,7 +5,7 @@ from .init_login import init_login_manager
|
||||
from .init_debug_tool import init_debug_tool
|
||||
from .init_template_directives import init_template_directives
|
||||
from .init_error_views import init_error_views
|
||||
from .init_mail import init_mail
|
||||
from .init_mail import init_mail, mail as flask_mail
|
||||
from .init_apscheduler import init_scheduler
|
||||
from .init_upload import init_upload
|
||||
from .init_dotenv import init_dotenv
|
||||
|
||||
@@ -6,4 +6,5 @@ from .admin_power import Power
|
||||
from .admin_role import Role
|
||||
from .admin_role_power import role_power
|
||||
from .admin_user import User
|
||||
from .admin_user_role import user_role
|
||||
from .admin_user_role import user_role
|
||||
from .admin_mail import Mail
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import datetime
|
||||
from applications.extensions import db
|
||||
|
||||
|
||||
class Mail(db.Model):
|
||||
__tablename__ = 'admin_mail'
|
||||
id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='邮件编号')
|
||||
receiver = db.Column(db.String(1024), comment='收件人邮箱')
|
||||
subject = db.Column(db.String(128), comment='邮件主题')
|
||||
content = db.Column(db.Text(), comment='邮件正文')
|
||||
user_id = db.Column(db.Integer, comment='发送人id')
|
||||
create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
|
||||
@@ -5,3 +5,4 @@ from .admin_dict import DictDataOutSchema, DictTypeOutSchema
|
||||
from .admin_dept import DeptOutSchema
|
||||
from .admin_log import LogOutSchema
|
||||
from .admin_photo import PhotoOutSchema
|
||||
from .admin_mail import MailOutSchema
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
from applications.extensions import ma
|
||||
from marshmallow import fields
|
||||
from applications.models import User
|
||||
|
||||
|
||||
# 用户models的序列化类
|
||||
class MailOutSchema(ma.Schema):
|
||||
id = fields.Integer()
|
||||
receiver = fields.Str()
|
||||
subject = fields.Str()
|
||||
content = fields.Str()
|
||||
realname = fields.Method("get_realname")
|
||||
create_at = fields.DateTime()
|
||||
|
||||
def get_realname(self, obj):
|
||||
if obj.user_id != None:
|
||||
return User.query.filter_by(id=obj.user_id).first().realname
|
||||
else:
|
||||
return None
|
||||
@@ -9,6 +9,7 @@ from applications.view.admin.role import admin_role
|
||||
from applications.view.admin.user import admin_user
|
||||
from applications.view.admin.monitor import admin_monitor_bp
|
||||
from applications.view.admin.task import admin_task
|
||||
from applications.view.admin.mail import admin_mail
|
||||
|
||||
|
||||
def register_admin_views(app: Flask):
|
||||
@@ -21,3 +22,4 @@ def register_admin_views(app: Flask):
|
||||
app.register_blueprint(admin_role)
|
||||
app.register_blueprint(admin_dict)
|
||||
app.register_blueprint(admin_task)
|
||||
app.register_blueprint(admin_mail)
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
from flask import Blueprint, render_template, request, current_app
|
||||
from flask_login import current_user
|
||||
from flask_mail import Message
|
||||
|
||||
from applications.common.curd import model_to_dicts
|
||||
from applications.common.helper import ModelFilter
|
||||
from applications.common.utils.http import table_api, fail_api, success_api
|
||||
from applications.common.utils.rights import authorize
|
||||
from applications.common.utils.validate import xss_escape
|
||||
from applications.extensions import db, flask_mail
|
||||
from applications.models import Mail
|
||||
from applications.schemas import MailOutSchema
|
||||
|
||||
admin_mail = Blueprint('adminMail', __name__, url_prefix='/admin/mail')
|
||||
|
||||
|
||||
# 用户管理
|
||||
@admin_mail.get('/')
|
||||
@authorize("admin:mail:main", log=True)
|
||||
def main():
|
||||
return render_template('admin/mail/main.html')
|
||||
|
||||
|
||||
# 用户分页查询
|
||||
@admin_mail.get('/data')
|
||||
@authorize("admin:mail:main", log=True)
|
||||
def data():
|
||||
# 获取请求参数
|
||||
receiver = xss_escape(request.args.get("receiver"))
|
||||
subject = xss_escape(request.args.get('subject'))
|
||||
content = xss_escape(request.args.get('content'))
|
||||
# 查询参数构造
|
||||
mf = ModelFilter()
|
||||
if receiver:
|
||||
mf.contains(field_name="receiver", value=receiver)
|
||||
if subject:
|
||||
mf.contains(field_name="subject", value=subject)
|
||||
if content:
|
||||
mf.exact(field_name="content", value=content)
|
||||
# orm查询
|
||||
# 使用分页获取data需要.items
|
||||
mail = Mail.query.filter(mf.get_filter(Mail)).layui_paginate()
|
||||
count = mail.total
|
||||
# 返回api
|
||||
return table_api(data=model_to_dicts(schema=MailOutSchema, data=mail.items), count=count)
|
||||
|
||||
|
||||
# 用户增加
|
||||
@admin_mail.get('/add')
|
||||
@authorize("admin:mail:add", log=True)
|
||||
def add():
|
||||
return render_template('admin/mail/add.html')
|
||||
|
||||
|
||||
@admin_mail.post('/save')
|
||||
@authorize("admin:mail:add", log=True)
|
||||
def save():
|
||||
req_json = request.json
|
||||
receiver = xss_escape(req_json.get("receiver"))
|
||||
subject = xss_escape(req_json.get('subject'))
|
||||
content = xss_escape(req_json.get('content'))
|
||||
user_id = current_user.id
|
||||
|
||||
try:
|
||||
msg = Message(subject=subject, recipients=receiver.split(";"), body=content)
|
||||
flask_mail.send(msg)
|
||||
except Exception as e:
|
||||
current_app.log_exception(e)
|
||||
return fail_api(msg="发送失败,请检查邮件配置或发送人邮箱是否写错")
|
||||
|
||||
mail = Mail(receiver=receiver, subject=subject, content=content, user_id=user_id)
|
||||
|
||||
db.session.add(mail)
|
||||
db.session.commit()
|
||||
return success_api(msg="增加成功")
|
||||
|
||||
|
||||
# 删除用户
|
||||
@admin_mail.delete('/remove/<int:id>')
|
||||
@authorize("admin:mail:remove", log=True)
|
||||
def delete(id):
|
||||
res = Mail.query.filter_by(id=id).delete()
|
||||
if not res:
|
||||
return fail_api(msg="删除失败")
|
||||
db.session.commit()
|
||||
return success_api(msg="删除成功")
|
||||
|
||||
|
||||
# 批量删除
|
||||
@admin_mail.delete('/batchRemove')
|
||||
@authorize("admin:mail:remove", log=True)
|
||||
def batch_remove():
|
||||
ids = request.form.getlist('ids[]')
|
||||
for id in ids:
|
||||
res = Mail.query.filter_by(id=id).delete()
|
||||
if not res:
|
||||
return fail_api(msg="批量删除失败")
|
||||
db.session.commit()
|
||||
return success_api(msg="批量删除成功")
|
||||
@@ -0,0 +1,80 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>邮件管理</title>
|
||||
{% include 'admin/common/header.html' %}
|
||||
</head>
|
||||
<body>
|
||||
<form class="layui-form">
|
||||
<div class="mainBox">
|
||||
<div class="main-container">
|
||||
<div class="main-container">
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">收件人邮箱</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="text" name="receiver" lay-verify="title" autocomplete="off"
|
||||
placeholder="请输入收件人邮箱,多个邮箱使用英文;分割" class="layui-input">
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">主题</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="text" name="subject" lay-verify="title" autocomplete="off" placeholder="请输入标题"
|
||||
class="layui-input">
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">正文</label>
|
||||
<div class="layui-input-block">
|
||||
<textarea placeholder="请输入内容" class="layui-textarea" name="content" ></textarea>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="bottom">
|
||||
<div class="button-container">
|
||||
<button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="mail-save">
|
||||
<i class="layui-icon layui-icon-ok"></i>
|
||||
提交
|
||||
</button>
|
||||
<button type="reset" class="pear-btn pear-btn-sm">
|
||||
<i class="layui-icon layui-icon-refresh"></i>
|
||||
重置
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
{% include 'admin/common/footer.html' %}
|
||||
<script>
|
||||
layui.use(['form', 'jquery'], function () {
|
||||
let form = layui.form
|
||||
let $ = layui.jquery
|
||||
form.on('submit(mail-save)', function (data) {
|
||||
let loading = layer.load()
|
||||
$.ajax({
|
||||
url: '/admin/mail/save',
|
||||
data: JSON.stringify(data.field),
|
||||
dataType: 'json',
|
||||
contentType: 'application/json',
|
||||
type: 'post',
|
||||
success: function (result) {
|
||||
layer.close(loading)
|
||||
if (result.success) {
|
||||
layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
|
||||
parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
|
||||
parent.layui.table.reload('mail-table')
|
||||
})
|
||||
} else {
|
||||
layer.msg(result.msg, { icon: 2, time: 1000 })
|
||||
}
|
||||
}
|
||||
})
|
||||
return false
|
||||
})
|
||||
})
|
||||
</script>
|
||||
<script>
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,230 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>邮件管理</title>
|
||||
{% include 'admin/common/header.html' %}
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='/admin/admin/css/other/user.css') }}"/>
|
||||
</head>
|
||||
<body class="pear-container">
|
||||
{# 查询表单 #}
|
||||
<div class="layui-card">
|
||||
<div class="layui-card-body">
|
||||
<form class="layui-form" action="" lay-filter="mail-query-form">
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">收件人邮箱</label>
|
||||
<div class="layui-input-inline">
|
||||
<input type="text" name="receiver" placeholder="" class="layui-input">
|
||||
</div>
|
||||
<label class="layui-form-label">主题</label>
|
||||
<div class="layui-input-inline">
|
||||
<input type="text" name="subject" placeholder="" class="layui-input">
|
||||
</div>
|
||||
<label class="layui-form-label">正文</label>
|
||||
<div class="layui-input-inline">
|
||||
<input type="text" name="content" placeholder="" class="layui-input">
|
||||
</div>
|
||||
<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="mail-query">
|
||||
<i class="layui-icon layui-icon-search"></i>
|
||||
查询
|
||||
</button>
|
||||
<button type="reset" class="pear-btn pear-btn-md">
|
||||
<i class="layui-icon layui-icon-refresh"></i>
|
||||
重置
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{# 用户表格 #}
|
||||
<div class="user-main user-collasped">
|
||||
<div class="layui-card">
|
||||
<div class="layui-card-body">
|
||||
<table id="mail-table" lay-filter="mail-table"></table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
{# 表格操作 #}
|
||||
<script type="text/html" id="mail-toolbar">
|
||||
{% if authorize("admin:mail:add") %}
|
||||
<button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
|
||||
<i class="pear-icon pear-icon-add"></i>
|
||||
新增
|
||||
</button>
|
||||
{% endif %}
|
||||
{% if authorize("admin:mail:remove") %}
|
||||
<button class="pear-btn pear-btn-md" lay-event="batchRemove">
|
||||
<i class="pear-icon pear-icon-ashbin"></i>
|
||||
删除
|
||||
</button>
|
||||
{% endif %}
|
||||
</script>
|
||||
|
||||
{# 用户修改操作 #}
|
||||
<script type="text/html" id="mail-bar">
|
||||
{% if authorize("admin:mail:remove") %}
|
||||
<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
|
||||
class="pear-icon pear-icon-ashbin"></i>
|
||||
</button>
|
||||
{% endif %}
|
||||
</script>
|
||||
|
||||
{# 启动与禁用 #}
|
||||
<script type="text/html" id="mail-enable">
|
||||
<input type="checkbox" name="enable" value="{{ "{{ d.id }}" }}" lay-skin="switch" lay-text="启用|禁用"
|
||||
lay-filter="mail-enable"
|
||||
{{ "{{# if(d.enable==1){ }} checked {{# } }}" }} />
|
||||
</script>
|
||||
|
||||
{# 用户注册时间 #}
|
||||
<script type="text/html" id="mail-createTime">
|
||||
{{ ' {{layui.util.toDateString(d.create_at, "yyyy-MM-dd HH:mm:ss")}' |safe }}}
|
||||
</script>
|
||||
|
||||
{% include 'admin/common/footer.html' %}
|
||||
|
||||
<script>
|
||||
layui.use(['table', 'form', 'jquery', 'popup', 'common'], function () {
|
||||
let table = layui.table
|
||||
let form = layui.form
|
||||
let $ = layui.jquery
|
||||
let dtree = layui.dtree
|
||||
let popup = layui.popup
|
||||
let common = layui.common
|
||||
let MODULE_PATH = '/admin/mail/'
|
||||
|
||||
// 表格数据
|
||||
let cols = [
|
||||
[
|
||||
{% if authorize("admin:mail:remove") %}
|
||||
{ type: 'checkbox' },
|
||||
{% endif %}
|
||||
{ title: '邮件编号', field: 'id', align: 'center', width: 110 },
|
||||
{ title: '收件人邮箱', field: 'receiver', align: 'center' },
|
||||
{ title: '主题', field: 'subject', align: 'center' },
|
||||
{ title: '正文', field: 'content', align: 'center' },
|
||||
{ title: '创建人', field: 'realname', align: 'center' },
|
||||
{ title: '创建时间', field: 'create_at', templet: '#mail-createTime', align: 'center' },
|
||||
{ title: '操作', toolbar: '#mail-bar', align: 'center', width: 130 }
|
||||
]
|
||||
]
|
||||
|
||||
// 渲染表格数据
|
||||
table.render({
|
||||
elem: '#mail-table',
|
||||
url: MODULE_PATH + 'data',
|
||||
page: true,
|
||||
cols: cols,
|
||||
skin: 'line',
|
||||
height: 'full-148',
|
||||
toolbar: '#mail-toolbar', /*工具栏*/
|
||||
text: { none: '暂无人员信息' },
|
||||
defaultToolbar: [{ layEvent: 'refresh', icon: 'layui-icon-refresh' }, 'filter', 'print', 'exports'] /*默认工具栏*/
|
||||
})
|
||||
|
||||
|
||||
|
||||
table.on('tool(mail-table)', function (obj) {
|
||||
if (obj.event === 'remove') {
|
||||
window.remove(obj)
|
||||
}
|
||||
})
|
||||
|
||||
table.on('toolbar(mail-table)', function (obj) {
|
||||
if (obj.event === 'add') {
|
||||
window.add()
|
||||
} else if (obj.event === 'refresh') {
|
||||
window.refresh()
|
||||
} else if (obj.event === 'batchRemove') {
|
||||
window.batchRemove(obj)
|
||||
}
|
||||
})
|
||||
|
||||
form.on('submit(mail-query)', function (data) {
|
||||
window.refresh(data.field)
|
||||
return false
|
||||
})
|
||||
|
||||
window.add = function () {
|
||||
layer.open({
|
||||
type: 2,
|
||||
title: '新增',
|
||||
shade: 0.1,
|
||||
area: ['550px', '550px'],
|
||||
content: MODULE_PATH + 'add'
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
window.remove = function (obj) {
|
||||
layer.confirm('确定要删除', { icon: 3, title: '提示' }, function (index) {
|
||||
layer.close(index)
|
||||
let loading = layer.load()
|
||||
$.ajax({
|
||||
url: MODULE_PATH + 'remove/' + obj.data['id'],
|
||||
dataType: 'json',
|
||||
type: 'delete',
|
||||
success: function (result) {
|
||||
layer.close(loading)
|
||||
if (result.success) {
|
||||
popup.success(result.msg, function () {
|
||||
obj.del()
|
||||
})
|
||||
} else {
|
||||
popup.failure(result.msg)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
window.batchRemove = function (obj) {
|
||||
let data = table.checkStatus(obj.config.id).data
|
||||
if (data.length === 0) {
|
||||
layer.msg('未选中数据', {
|
||||
icon: 3,
|
||||
time: 1000
|
||||
})
|
||||
return false
|
||||
}
|
||||
var ids = []
|
||||
var hasCheck = table.checkStatus('mail-table')
|
||||
var hasCheckData = hasCheck.data
|
||||
if (hasCheckData.length > 0) {
|
||||
$.each(hasCheckData, function (index, element) {
|
||||
ids.push(element.id)
|
||||
})
|
||||
}
|
||||
{#console.log(ids);#}
|
||||
layer.confirm('确定要删除选中数据', {
|
||||
icon: 3,
|
||||
title: '提示'
|
||||
}, function (index) {
|
||||
layer.close(index)
|
||||
let loading = layer.load()
|
||||
$.ajax({
|
||||
|
||||
url: MODULE_PATH + 'batchRemove',
|
||||
data: { ids: ids },
|
||||
dataType: 'json',
|
||||
type: 'delete',
|
||||
success: function (result) {
|
||||
layer.close(loading)
|
||||
if (result.success) {
|
||||
popup.success(result.msg, function () {
|
||||
table.reload('mail-table')
|
||||
})
|
||||
} else {
|
||||
popup.failure(result.msg)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
window.refresh = function (param) {
|
||||
table.reload('mail-table', { where: param })
|
||||
}
|
||||
})
|
||||
</script>
|
||||
</html>
|
||||
@@ -110,6 +110,28 @@ CREATE TABLE `admin_dict_type` (
|
||||
-- ----------------------------
|
||||
INSERT INTO `admin_dict_type` VALUES (1, '用户性别', 'user_sex', '用户性别', 1, NULL, '2021-04-16 13:37:11');
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for admin_mail
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `admin_mail`;
|
||||
CREATE TABLE `admin_mail` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '邮件编号',
|
||||
`receiver` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '收件人邮箱',
|
||||
`subject` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '邮件主题',
|
||||
`content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '邮件正文',
|
||||
`user_id` int(11) NULL DEFAULT NULL COMMENT '发送人id',
|
||||
`create_at` datetime(0) NULL DEFAULT NULL COMMENT '创建时间',
|
||||
PRIMARY KEY (`id`) USING BTREE
|
||||
) ENGINE = InnoDB AUTO_INCREMENT = 17 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of admin_mail
|
||||
-- ----------------------------
|
||||
INSERT INTO `admin_mail` VALUES (1, '1242733702@qq.com', 'pear-admin-flask', 'pear-admin-flask', 1, '2022-10-11 13:41:21');
|
||||
INSERT INTO `admin_mail` VALUES (4, '1242733702@qq.com', '湖人总冠军', '湖人总冠军', 1, '2022-10-11 14:03:30');
|
||||
INSERT INTO `admin_mail` VALUES (5, '1242733702@qq.com', '这是flask测试邮箱', '正文', 1, '2022-10-11 14:10:30');
|
||||
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for admin_photo
|
||||
-- ----------------------------
|
||||
@@ -186,6 +208,9 @@ INSERT INTO `admin_power` VALUES (53, '任务管理', '1', 'admin:task:main', '/
|
||||
INSERT INTO `admin_power` VALUES (54, '任务增加', '2', 'admin:task:add', '', '', '53', 'layui-icon ', 1, '2021-06-22 22:20:54', '2021-06-22 22:20:54', 1);
|
||||
INSERT INTO `admin_power` VALUES (55, '任务修改', '2', 'admin:task:edit', '', '', '53', 'layui-icon ', 2, '2021-06-22 22:21:34', '2021-06-22 22:21:34', 1);
|
||||
INSERT INTO `admin_power` VALUES (56, '任务删除', '2', 'admin:task:remove', '', '', '53', 'layui-icon ', 3, '2021-06-22 22:22:18', '2021-06-22 22:22:18', 1);
|
||||
INSERT INTO `admin_power` VALUES (57, '邮件管理', '1', 'admin:mail:main', '/admin/mail', '_iframe', '1', 'layui-icon layui-icon layui-icon-release', 7, '2022-10-11 11:21:05', '2022-10-11 11:21:22', 1);
|
||||
INSERT INTO `admin_power` VALUES (58, '邮件发送', '2', 'admin:mail:add', '', '', '57', 'layui-icon layui-icon-ok-circle', 1, '2022-10-11 11:22:26', '2022-10-11 11:22:26', 1);
|
||||
INSERT INTO `admin_power` VALUES (59, '邮件删除', '2', 'admin:mail:remove', '', '', '57', 'layui-icon layui-icon layui-icon-close', 2, '2022-10-11 11:23:06', '2022-10-11 11:23:18', 1);
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for admin_role
|
||||
@@ -271,6 +296,9 @@ INSERT INTO `admin_role_power` VALUES (363, 53, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (364, 54, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (365, 55, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (366, 56, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (367, 57, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (368, 58, 1);
|
||||
INSERT INTO `admin_role_power` VALUES (369, 59, 1);
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for admin_user
|
||||
|
||||
Reference in New Issue
Block a user