完善文档并添加示例插件
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
from flask import Flask
|
||||
|
||||
from .cli import gift_cli
|
||||
from .view.gift import bp
|
||||
|
||||
|
||||
def event_init(app: Flask):
|
||||
app.register_blueprint(bp)
|
||||
|
||||
|
||||
def event_finish(app: Flask):
|
||||
app.cli.add_command(gift_cli)
|
||||
@@ -0,0 +1,96 @@
|
||||
import datetime
|
||||
|
||||
from flask.cli import AppGroup
|
||||
|
||||
from ..models import Gift
|
||||
from applications.models import Power
|
||||
from applications.extensions import db
|
||||
|
||||
gift_cli = AppGroup("gift")
|
||||
|
||||
now_time = datetime.datetime.now()
|
||||
|
||||
powerdata = [
|
||||
Power(
|
||||
name='兑换码添加',
|
||||
type='2',
|
||||
code='system:gift:add',
|
||||
url='',
|
||||
open_type='',
|
||||
parent_id='60',
|
||||
icon='',
|
||||
sort=0,
|
||||
create_time=now_time,
|
||||
enable=1
|
||||
), Power(
|
||||
name='兑换码删除',
|
||||
type='2',
|
||||
code='system:gift:remove',
|
||||
url='',
|
||||
open_type='',
|
||||
parent_id='60',
|
||||
icon='',
|
||||
sort=0,
|
||||
create_time=now_time,
|
||||
enable=1
|
||||
), Power(
|
||||
name='兑换码编辑',
|
||||
type='2',
|
||||
code='system:gift:edit',
|
||||
url='',
|
||||
open_type='',
|
||||
parent_id='60',
|
||||
icon='',
|
||||
sort=0,
|
||||
create_time=now_time,
|
||||
enable=1
|
||||
)
|
||||
]
|
||||
|
||||
giftdata = [
|
||||
Gift(
|
||||
id=0,
|
||||
key='myTestCode',
|
||||
content='8折优惠',
|
||||
enable=1,
|
||||
used=0,
|
||||
create_at=now_time
|
||||
),
|
||||
Gift(
|
||||
id=1,
|
||||
key='DisableCode',
|
||||
content='1折优惠',
|
||||
enable=0,
|
||||
used=0,
|
||||
create_at=now_time
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
@gift_cli.command("init")
|
||||
def init_db():
|
||||
print("存入兑换码管理页面数据")
|
||||
|
||||
top_power = Power(
|
||||
name='兑换码管理',
|
||||
type='1',
|
||||
code='system:gift:main',
|
||||
url='/system/gift/',
|
||||
open_type='_iframe',
|
||||
parent_id='1',
|
||||
icon='layui-icon layui-icon layui-icon layui-icon-diamond',
|
||||
sort=8,
|
||||
create_time=now_time,
|
||||
enable=1
|
||||
)
|
||||
|
||||
db.session.add(top_power)
|
||||
db.session.commit() # 提交了才有 id
|
||||
|
||||
for i in range(len(powerdata)):
|
||||
powerdata[i].parent_id = top_power.id
|
||||
|
||||
db.session.add_all(powerdata)
|
||||
|
||||
db.session.add_all(giftdata)
|
||||
db.session.commit()
|
||||
@@ -0,0 +1,12 @@
|
||||
import datetime
|
||||
from applications.extensions import db
|
||||
|
||||
|
||||
class Gift(db.Model):
|
||||
__tablename__ = 'admin_gift'
|
||||
id = db.Column(db.Integer, primary_key=True, comment="唯一ID")
|
||||
key = db.Column(db.String(50), comment="兑换码")
|
||||
content = db.Column(db.String(), comment="具体内容")
|
||||
enable = db.Column(db.Integer, default=0, comment='是否启用')
|
||||
used = db.Column(db.Integer, default=0, comment='是否已经使用')
|
||||
create_at = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
|
||||
@@ -0,0 +1,12 @@
|
||||
from flask_marshmallow.sqla import SQLAlchemyAutoSchema
|
||||
|
||||
from ..models import Gift
|
||||
|
||||
|
||||
class GiftSchema(SQLAlchemyAutoSchema):
|
||||
class Meta:
|
||||
model = Gift # table = models.Album.__table__
|
||||
# include_relationships = True # 输出模型对象时同时对外键,是否也一并进行处理
|
||||
include_fk = True # 序列化阶段是否也一并返回主键
|
||||
# fields= ["id","name"] # 启动的字段列表
|
||||
# exclude = ["id","name"] # 排除字段列表
|
||||
@@ -0,0 +1,84 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>兑换码添加</title>
|
||||
{% include 'system/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="key" 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="请输入兑换内容" name="content" class="layui-textarea"></textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">状态</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="radio" name="enable" value="1" title="开启" checked>
|
||||
<input type="radio" name="enable" value="0" title="关闭">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="bottom">
|
||||
<div class="button-container">
|
||||
<button type="submit" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="save">
|
||||
<i class="layui-icon layui-icon-ok"></i>
|
||||
提交
|
||||
</button>
|
||||
<button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
|
||||
<i class="layui-icon layui-icon-refresh"></i>
|
||||
重置
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
{% include 'system/common/footer.html' %}
|
||||
<script>
|
||||
layui.use(['form', 'jquery'], function () {
|
||||
let form = layui.form
|
||||
let $ = layui.jquery
|
||||
|
||||
form.on('submit(save)', function (data) {
|
||||
|
||||
$.ajax({
|
||||
url: '/system/gift/save',
|
||||
data: JSON.stringify(data.field),
|
||||
dataType: 'json',
|
||||
contentType: 'application/json',
|
||||
type: 'post',
|
||||
success: function (result) {
|
||||
if (result.success) {
|
||||
layer.msg(result.msg, {icon: 1, time: 1000}, function () {
|
||||
parent.layer.close(parent.layer.getFrameIndex(window.name)) //关闭当前页
|
||||
parent.layui.table.reload('gift-table') // 目标表格
|
||||
})
|
||||
} else {
|
||||
layer.msg(result.msg, {icon: 2, time: 1000})
|
||||
}
|
||||
}
|
||||
})
|
||||
return false
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,91 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>兑换码编辑</title>
|
||||
{% include 'system/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="id" lay-verify="title" autocomplete="off" placeholder="请输入编号"
|
||||
class="layui-input" value="{{ gift.id }}" disabled>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">兑换码</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="text" name="key" lay-verify="title" autocomplete="off" placeholder="请输入兑换码"
|
||||
class="layui-input" value="{{ gift.key }}">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">兑换内容</label>
|
||||
<div class="layui-input-block">
|
||||
<textarea placeholder="请输入兑换内容" name="content" class="layui-textarea">{{ gift.content }}</textarea>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">状态</label>
|
||||
<div class="layui-input-block">
|
||||
<input type="radio" name="enable" value="1" title="开启" {{ 'checked' if gift.enable == 1 }}>
|
||||
<input type="radio" name="enable" value="0" title="关闭" {{ 'checked' if gift.enable == 0 }}>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="bottom">
|
||||
<div class="button-container">
|
||||
<button type="submit" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="save">
|
||||
<i class="layui-icon layui-icon-ok"></i>
|
||||
提交
|
||||
</button>
|
||||
<button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
|
||||
<i class="layui-icon layui-icon-refresh"></i>
|
||||
重置
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
{% include 'system/common/footer.html' %}
|
||||
<script>
|
||||
layui.use(['form', 'jquery'], function () {
|
||||
let form = layui.form
|
||||
let $ = layui.jquery
|
||||
|
||||
form.on('submit(save)', function (data) {
|
||||
|
||||
$.ajax({
|
||||
url: '/system/gift/update',
|
||||
data: JSON.stringify(data.field),
|
||||
dataType: 'json',
|
||||
contentType: 'application/json',
|
||||
type: 'post',
|
||||
success: function (result) {
|
||||
if (result.success) {
|
||||
layer.msg(result.msg, {icon: 1, time: 1000}, function () {
|
||||
parent.layer.close(parent.layer.getFrameIndex(window.name)) //关闭当前页
|
||||
parent.layui.table.reload('gift-table') // 目标表格
|
||||
})
|
||||
} else {
|
||||
layer.msg(result.msg, {icon: 2, time: 1000})
|
||||
}
|
||||
}
|
||||
})
|
||||
return false
|
||||
})
|
||||
})
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,205 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>兑换码管理</title>
|
||||
{% include 'system/common/header.html' %}
|
||||
</head>
|
||||
<body class="pear-container">
|
||||
|
||||
{# 查询表单 #}
|
||||
<div class="layui-card">
|
||||
<div class="layui-card-body">
|
||||
<form class="layui-form" action="" lay-filter="query-form">
|
||||
<div class="layui-form-item" style="margin-bottom: unset;">
|
||||
<label class="layui-form-label">激活码</label>
|
||||
<div class="layui-input-inline">
|
||||
<input type="text" name="key" placeholder="" class="layui-input">
|
||||
</div>
|
||||
<button class="layui-btn layui-btn-md" lay-submit lay-filter="gift-query">
|
||||
<i class="layui-icon layui-icon-search"></i>
|
||||
查询
|
||||
</button>
|
||||
<button type="reset" class="layui-btn layui-btn-primary layui-btn-md">
|
||||
<i class="layui-icon layui-icon-refresh"></i>
|
||||
重置
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{# 用户表格 #}
|
||||
<div>
|
||||
<div class="layui-card">
|
||||
<div class="layui-card-body">
|
||||
<table id="gift-table" lay-filter="gift-table"></table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
<!-- 这里写的都是表格一行元素组件 -->
|
||||
{% raw %}
|
||||
<script type="text/html" id="enable-element">
|
||||
<input type="checkbox" name="enable" value="{{ d.id }}" lay-skin="switch" lay-text="启用|禁用"
|
||||
lay-filter="gift-enable"
|
||||
{{# if(d.enable==1){ }} checked {{# } }}/>
|
||||
</script>
|
||||
|
||||
<script type="text/html" id="used-element">
|
||||
{{# if(d.used==1){ }}
|
||||
<span style="color: red">已用</span>
|
||||
{{# } else { }}
|
||||
<span style="color: green">未用</span>
|
||||
{{# } }}
|
||||
</script>
|
||||
|
||||
<script type="text/html" id="createTime-element">
|
||||
{{layui.util.toDateString(d.create_at, "yyyy-MM-dd HH:mm:ss")}}
|
||||
</script>
|
||||
{% endraw %}
|
||||
|
||||
|
||||
<script type="text/html" id="table-bar">
|
||||
{% if authorize("system:gift:edit") %}
|
||||
<button class="layui-btn layui-btn-xs" lay-event="edit"><i class="pear-icon pear-icon-edit"> 编辑</i>
|
||||
</button>
|
||||
{% endif %}
|
||||
{% if authorize("system:gift:remove") %}
|
||||
<button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="remove"><i
|
||||
class="pear-icon pear-icon-ashbin"> 删除</i>
|
||||
</button>
|
||||
{% endif %}
|
||||
</script>
|
||||
|
||||
<!-- 这里是表格的工具栏 -->
|
||||
<script type="text/html" id="table-toolbar">
|
||||
{% if authorize("system:gift:add") %}
|
||||
<button class="layui-btn layui-btn-primary layui-btn-sm" lay-event="add">
|
||||
<i class="pear-icon pear-icon-add"></i>
|
||||
新增
|
||||
</button>
|
||||
{% endif %}
|
||||
</script>
|
||||
|
||||
|
||||
{% include 'system/common/footer.html' %}
|
||||
<script>
|
||||
layui.use(['table', 'form', 'popup'], function () {
|
||||
let $ = layui.jquery;
|
||||
let table = layui.table;
|
||||
let form = layui.form;
|
||||
let popup = layui.popup;
|
||||
|
||||
// 表格数据
|
||||
let cols = [
|
||||
[
|
||||
{title: '编号', field: 'id', align: 'center'},
|
||||
{title: '激活码', field: 'key', align: 'center'},
|
||||
{title: '内容', field: 'content', align: 'center'},
|
||||
{title: '启用', field: 'enable', align: 'center', templet: '#enable-element'},
|
||||
{title: '已用', field: 'used', align: 'center', templet: '#used-element'},
|
||||
{title: '创建时间', field: 'create_at', templet: '#createTime-element', align: 'center'},
|
||||
{title: '操作', toolbar: '#table-bar', align: 'center', width: 180}
|
||||
]
|
||||
]
|
||||
|
||||
// 渲染表格数据
|
||||
table.render({
|
||||
elem: '#gift-table',
|
||||
url: '/system/gift/data', // 请求链接
|
||||
page: true,
|
||||
cols: cols,
|
||||
skin: 'line',
|
||||
toolbar: '#table-toolbar',
|
||||
text: {none: '暂无激活码信息'},
|
||||
defaultToolbar: [{layEvent: 'refresh', icon: 'layui-icon-refresh'}, 'filter', 'print', 'exports']
|
||||
})
|
||||
|
||||
|
||||
// 表单查询
|
||||
form.on('submit(gift-query)', function (data) {
|
||||
table.reload('gift-table', {where: data.field});
|
||||
return false;
|
||||
})
|
||||
|
||||
// 启用与禁用
|
||||
form.on('switch(gift-enable)', function (obj) {
|
||||
let operate;
|
||||
if (obj.elem.checked) {
|
||||
operate = 'enable'
|
||||
} else {
|
||||
operate = 'disable'
|
||||
}
|
||||
let loading = layer.load()
|
||||
$.ajax({
|
||||
url: '/system/gift/' + operate,
|
||||
data: JSON.stringify({id: this.value}),
|
||||
dataType: 'json',
|
||||
contentType: 'application/json',
|
||||
type: 'put',
|
||||
success: function (result) {
|
||||
layer.close(loading)
|
||||
if (result.success) {
|
||||
popup.success(result.msg)
|
||||
} else {
|
||||
popup.failure(result.msg)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
// 表格各行工具事件
|
||||
table.on('tool(gift-table)', function (obj) {
|
||||
if (obj.event === 'remove') {
|
||||
|
||||
layer.confirm('确定要删除该兑换码?', {icon: 3, title: '提示'}, function (index) {
|
||||
layer.close(index)
|
||||
let loading = layer.load()
|
||||
$.ajax({
|
||||
url: '/system/gift/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)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
} else if (obj.event === 'edit') {
|
||||
|
||||
layer.open({
|
||||
type: 2,
|
||||
title: '修改',
|
||||
shade: 0.1,
|
||||
area: ['550px', '500px'],
|
||||
content: '/system/gift/edit/' + obj.data['id']
|
||||
})
|
||||
|
||||
}
|
||||
})
|
||||
|
||||
// 顶部工具栏
|
||||
table.on('toolbar(gift-table)', function (obj) {
|
||||
if (obj.event === 'add') {
|
||||
layer.open({
|
||||
type: 2,
|
||||
title: '新增',
|
||||
shade: 0.1,
|
||||
area: ['550px', '550px'],
|
||||
content: '/system/gift/add'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
</script>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,142 @@
|
||||
import os
|
||||
|
||||
from flask import Blueprint, request, render_template
|
||||
|
||||
from ..models import Gift
|
||||
from ..schemas import GiftSchema
|
||||
from applications.extensions import db
|
||||
|
||||
from applications.common.helper import ModelFilter
|
||||
from applications.common.curd import enable_status, disable_status, delete_one_by_id, get_one_by_id
|
||||
from applications.common.utils.http import table_api, success_api, fail_api
|
||||
from applications.common.utils.rights import authorize
|
||||
|
||||
# 获取插件所在的目录(结尾没有分割符号)
|
||||
dir_path = os.path.dirname(__file__).replace("\\", "/")
|
||||
|
||||
bp = Blueprint('gift', __name__, url_prefix='/system/gift',
|
||||
template_folder=dir_path + '/../templates')
|
||||
|
||||
|
||||
@bp.get('/')
|
||||
@authorize("system:gift:main")
|
||||
def index():
|
||||
return render_template('system/gift/main.html')
|
||||
|
||||
|
||||
@bp.get('/add')
|
||||
@authorize("system:gift:main")
|
||||
def add():
|
||||
return render_template('system/gift/add.html')
|
||||
|
||||
|
||||
@bp.get('/data')
|
||||
@authorize("system:gift:main")
|
||||
def data():
|
||||
key = request.args.get('key', type=str)
|
||||
|
||||
mf = ModelFilter()
|
||||
if key:
|
||||
mf.vague('key', key) # 模糊查询
|
||||
|
||||
data, total, page, limit = db.session.query(Gift).filter(mf.get_filter(Gift)).layui_paginate_json(GiftSchema)
|
||||
|
||||
return table_api(
|
||||
data=data,
|
||||
count=total,
|
||||
limit=limit
|
||||
)
|
||||
|
||||
|
||||
@bp.put('/enable')
|
||||
@authorize("system:gift:edit")
|
||||
def enable_api():
|
||||
data = request.get_json(force=True)
|
||||
|
||||
if enable_status(Gift, data.get('id')):
|
||||
return success_api(msg="启用成功")
|
||||
|
||||
return success_api(msg="启用失败")
|
||||
|
||||
|
||||
@bp.put('/disable')
|
||||
@authorize("system:gift:edit")
|
||||
def disable_api():
|
||||
req_json = request.get_json(force=True)
|
||||
|
||||
if disable_status(Gift, req_json.get('id')):
|
||||
return success_api(msg="禁用成功")
|
||||
|
||||
return success_api(msg="禁用失败")
|
||||
|
||||
|
||||
@bp.delete('/remove/<int:_id>')
|
||||
@authorize("system:gift:remove")
|
||||
def remove_api(_id):
|
||||
if delete_one_by_id(Gift, _id):
|
||||
return success_api(msg="删除成功")
|
||||
|
||||
return success_api(msg="删除失败")
|
||||
|
||||
|
||||
@bp.post('/save')
|
||||
@authorize("system:gift:add", log=True)
|
||||
def save():
|
||||
req_json = request.get_json(force=True)
|
||||
|
||||
data = {
|
||||
'key': req_json.get('key'),
|
||||
'content': req_json.get('content'),
|
||||
'enable': req_json.get('enable'),
|
||||
'used': 0
|
||||
}
|
||||
|
||||
# 效验参数
|
||||
if not all(list(data.keys())):
|
||||
return fail_api(msg="参数不全")
|
||||
|
||||
if not data['enable'].isdigit():
|
||||
return fail_api(msg="参数 enable 错误")
|
||||
|
||||
try:
|
||||
db.session.add(Gift(**data))
|
||||
db.session.commit()
|
||||
return success_api(msg="添加成功")
|
||||
except Exception as e:
|
||||
return fail_api(msg="添加失败")
|
||||
|
||||
|
||||
@bp.get('/edit/<int:_id>')
|
||||
@authorize("system:gift:edit", log=True)
|
||||
def edit(_id):
|
||||
gift = get_one_by_id(Gift, _id)
|
||||
return render_template('system/gift/edit.html', gift=gift)
|
||||
|
||||
|
||||
@bp.post('/update')
|
||||
@authorize("system:gift:edit", log=True)
|
||||
def update():
|
||||
req_json = request.get_json(force=True)
|
||||
|
||||
_id = req_json.get('id')
|
||||
|
||||
data = {
|
||||
'key': req_json.get('key'),
|
||||
'content': req_json.get('content'),
|
||||
'enable': req_json.get('enable'),
|
||||
'used': 0
|
||||
}
|
||||
|
||||
# 效验参数
|
||||
if not all(list(data.keys())):
|
||||
return fail_api(msg="参数不全")
|
||||
|
||||
if not data['enable'].isdigit():
|
||||
return fail_api(msg="参数 enable 错误")
|
||||
|
||||
try:
|
||||
db.session.query(Gift).filter(Gift.id == _id).update(data)
|
||||
db.session.commit()
|
||||
return success_api(msg="编辑成功")
|
||||
except Exception as e:
|
||||
return fail_api(msg="编辑失败")
|
||||
Reference in New Issue
Block a user