form表单公用
<?php
/**
* 后台总控制器
*/
namespace app\common\controller;
use think\Controller;
use app\common\service\DataService;
class Admin extends Controller
{
/**
* 默认操作数据表
* @var string
*/
protected $table;
/**
* 检测登录权限
*/
public $chkLogin = true;
/**
* 检查节点访问权限
*/
public $chkAuth = true;
/**
* 页面标题
*/
public $title = '后台管理';
/**
* 结构分隔符
*/
public static $dept = '<span style="color:gray;">├─ </span>';
/**
* 表单默认操作
* @param Query $db 数据库查询对象
* @param string $tpl 显示模板名字
* @param string $pk 更新主键规则
* @param array $where 查询规则
* @param array $data 扩展数据
* @return array|string
*/
protected function _form($db = null, $tpl = null, $pk = null, $where = [], $data = [])
{
$db = is_null($db) ? db($this->table) : (is_string($db) ? db($db) : $db);
$pk = empty($pk) ? ($db->getPk() ? $db->getPk() : 'id') : $pk;
$pk_value = input($pk, isset($where[$pk]) ? $where[$pk] : (isset($data[$pk]) ? $data[$pk] : null));
// POST请求, 数据自动存库
if ($this->request->isPost()) {
$data = array_merge(input('post.'), $data);
if ($this->_callback('_form_filter', $data) !== false) {
$result = DataService::save($db, $data, $pk, $where);
$url = input('open_type') == 'modal' ? '' : cookie('_listpage_');
return $result !== false ? $this->success('恭喜, 数据保存成功!', $url) : $this->error('数据保存失败, 请稍候再试!');
}
}
// 非POST请求, 获取数据并显示表单页面
$row = $db->where($pk, $pk_value)->where($where)->find();
$vo = ($pk_value !== null) ? array_merge($row, $data) : $data;
if (false !== $this->_callback('_form_filter', $vo)) {
$this->view->title = !empty($this->title) ? $this->title : null;
$this->view->vo = !empty($vo) ? $vo : null;
return $this->fetch($tpl);
}
}
/**
* 列表集成处理方法
* @param Query $db 数据库查询对象
* @param bool $is_page 是启用分页
* @param bool $is_display 是否直接输出显示
* @param bool $total 总记录数
* @return array|string
*/
protected function _list($db = null, $is_page = true, $is_display = true, $total = false)
{
$db = is_null($db) ? db($this->table) : (is_string($db) ? db($db) : $db);
# 列表排序默认处理
if ($this->request->isPost() && input('post.action') === 'resort') {
$data = input('post.');
unset($data['action']);
foreach ($data as $key => &$value) {
if (false === $db->where('id', intval(ltrim($key, '_')))->update(['sort' => $value])) {
$this->error('列表排序失败,请稍候再试!');
}
}
$this->success('列表排序成功,正在刷新列表!', '');
}
# 默认排序
if ($db->getOptions('order') === null) {
$fields = $db->getTableFields();
in_array('sort', $fields) && $db->order('sort asc');
}
# 列表显示
$result = [];
if ($is_page) {
// $row_page = intval($this->request->get('rows', cookie('rows')));
// cookie('rows', $row_page >= 10 ? $row_page : 15);
// $row_page = 1;
// 每页条数
$rows = input('limit', config('paginate.list_rows'), 'intval');
$page = $db->paginate($rows, $total, ['query' => input('get.', '', 'urlencode')]);
// 获取总数
$result['total'] = $page->total();
$result['list'] = $page->all();
$result['page'] = preg_replace(['|href="(.*?)"|', '|pagination|'], ['data-load="$1"', 'pagination pull-right'], $page->render());
} else {
$result['list'] = $db->select();
}
if ($this->_callback('_data_filter', $result['list']) === false) {
return $result;
}
if ($is_display) {
!empty($this->title) && $this->view->title = $this->title;
cookie('_listpage_', url('admin/index/index', '', '') . '#' .$this->request->url());
exit($this->fetch('', $result));
}
return $result;
}
/**
* 当前对象回调成员方法
* @param string $method
* @param array $data
* @return bool
*/
protected function _callback($method, &$data)
{
foreach ([$method, "_" . $this->request->action() . "{$method}"] as $_method) {
if (method_exists($this, $_method) && false === $this->$_method($data)) {
return false;
}
}
return true;
}
/**
* 更新操作
* @param string $name 类型名称
*/
protected function _save ($name = '')
{
$arr = [
'success' => [
'delete' => ($name ? : '删除') . '成功!',
'forbid' => ($name ? : '禁用') . '成功!',
'resume' => ($name ? : '启用') . '成功!',
],
'error' => [
'delete' => ($name ? : '删除') . '失败,请稍候再试!',
'forbid' => ($name ? : '禁用') . '失败,请稍候再试!',
'resume' => ($name ? : '启用') . '失败,请稍候再试!',
],
];
$success = $arr['success'][$this->request->action()];
$error = $arr['error'][$this->request->action()];
return DataService::update($this->table) ? $this->success($success, '') : $this->error($error);
}
/**
* 生成静态页面
*/
protected function makeHtml ($mode, $id = '')
{
$root = $this->request->root();
// 获取内容
$content = action('article/Index/' . $mode, ['id' => $id]);
if (!empty($id)) {
$file = 'topic/' . $id . '.html';
// 替换内容
$content = str_replace($root . '/'. $mode. '/', $root . '/topic/', $content);
} else {
$file = 'index.html';
// 替换内容
$content = str_replace($root . '/category/', $root . '/topic/', $content);
$content = str_replace($root . '/detail/', $root . '/topic/', $content);
}
// 替换内容需要正则拿出所有需要替换的链接,循环判定静态文件是否存在,存在则使用静态替换,不存在则使用动态路径
// ...............
file_put_contents(ROOT_PATH . $file, $content);
}
/**
* 全站生成静态
*/
protected function makeHtmls ()
{
}
}
子类 /** * 标签列表 */ public function index () { $this->view->name = $this->title; // 设置页面标题 $this->title = $this->title . '管理'; // 实例Query对象 $db = db($this->table)->where('delete_time', null)->where('type', $this->type); if ($area = input('countryid', 0, 'intval')) { $db->where('area', $area); } // 实例化并显示 return parent::_list($db, 'expert/index'); } /** * 列表数据处理 * @param array $data */ protected function _index_data_filter(&$data) { foreach ($data as &$vo) { $vo['create'] = date('Y-m-d H:i:s', $vo['create_time']); // 显示地区 $vo['country'] = db('Country')->where('id', $vo['area'])->value('namecn') ? : '未设置'; // 显示行业标签 $vo['industry'] = db('Industry')->where('id', $vo['industry'])->value('namecn') ? : '未设置'; } } /** * 表单数据默认处理 * @param array $data */ protected function _form_filter(&$data) { if ($this->request->isPost()) { if (!isset($data['id'])) { $data['create_time'] = time(); $data['type'] = $this->type; } } } /** * 标签编辑 */ public function add() { $this->view->name = $this->title; $this->title = '新增' . $this->title; return $this->_form($this->table, 'expert/form'); } /** * 标签编辑 */ public function edit() { $this->view->name = $this->title; $this->title = '编辑' . $this->title; return $this->_form($this->table, 'expert/form'); }
form表单公用的更多相关文章
- [js开源组件开发]query组件,获取url参数和form表单json格式
query组件,获取url参数和form表单json格式 距离上次的组件[js开源组件开发]ajax分页组件一转眼过去了近二十天,或许我一周一组件的承诺有了质疑声,但其实我一直在做,只是没人看到……, ...
- 项目回顾1-图片上传-form表单还是base64-前端图片压缩
第一个项目终于上线了,是一个叫亲青筹的公益众筹平台,微信端,电脑端还有后台界面大部分都是我完成的,几个月过来,感觉收获了很多,觉得要总结一下. 首先想到的是图片上传的问题.在通常表单数据都是ajax上 ...
- Django学习笔记(6)——Form表单
知识储备:HTML表单form学习 表单,在前端页面中属于最常见的一个东西了.基本上网站信息的提交都用到了表单,所以下面来学习Django中优雅的表单系统:Form 表单的主要作用是在网页上提供一个图 ...
- JS form 表单收集 数据 formSerialize
做后台系统的时候通常会用到form表单来做数据采集:每次一个字段一个字段的去收集就会很麻烦,网站也有form.js插件可以进行表单收集,并封装成一个对象,通过ajax方法传到后台:现在介绍一种直觉采集 ...
- C#之Form表单认证
原文地址: https://blog.csdn.net/chadcao/article/details/7859394 ASP.NET的安全认证,共有“Windows”.“Form”.“Passpor ...
- form表单验证-Javascript
Form表单验证: js基础考试内容,form表单验证,正则表达式,blur事件,自动获取数组,以及css布局样式,动态清除等.完整代码如下: <!DOCTYPE html PUBLIC &qu ...
- Form 表单提交参数
今天因为要额外提交参数数组性的参数给form传到后台而苦恼了半天,结果发现,只需要在form表单对应的字段html空间中定义name = 后台参数名 的属性就ok了. 后台本来是只有模型参数的,但是后 ...
- form表单 ----在路上(15)
form 表单就是将用户的信息提交到服务器,服务器会将信息存储活着根据信息查询数据进行增删改查,再将其返回给用户. 基本格式: <form action="" method ...
- form表单的字符串进行utf-8编码
<form>表单有assept-charset属性.该属性规定字符的编码方式,默认是"unknown",与文档的字符集相同. 该属性除了Internet explore ...
随机推荐
- JS 样式字符串 转 JSON对象
项目中需要把div 上的样式值转成数据展示 形如: padding: 7px 2px 1px 3px; color: rgb(238, 65, 65); background-color: rgb(2 ...
- NPOI导出Excel2007-xlsx
今天在用npoi导出xls时会报错,经过在网上查找资料,找到一篇博客文章介绍的,原文地址https://www.cnblogs.com/spring_wang/p/3160020.html 1.今天再 ...
- JDK8新特性04 方法引用与构造器引用
import java.io.PrintStream; import java.util.Comparator; import java.util.function.*; /** * 一.方法引用 * ...
- EF 复杂语句的使用
//EF多重排序 context.Serials .Where(s => ("," + s.VideoGenreIds + ",").Contains(& ...
- python写商品管理练习
#.添加 #.商品名称 #.要从文件里面把所有的商品读出来 #.价格 #.写一个方法判断是否为合理的价格 #.数量 #整数 # product = { # "爱疯差":{ # &q ...
- nginx常用指令
./nginx #打开 nginx nginx -s reload|reopen|stop|quit #重新加载配置|重启|停止|退出 nginx nginx -t #测试配置是否有语法错误 ngin ...
- git修改文件夹名字
git mv -f oldfolder newfolder git add -u newfolder (-u选项会更新已经追踪的文件和文件夹) git commit -m "changed ...
- css3动画transition animation
CSS动画简介 transition animation transition过渡:css3通过transitions属性引入时间概念,通过开始.结束状态自动计算中间状态,实现状态改变的过渡效果 ...
- 帮助类-AD域操作
private static void GetAllUsersInAD() { DirectorySearcher searcher = new DirectorySearcher(); search ...
- Python 12 - Mysql & ORM
本节内容 1.数据库介绍 2.mysql数据库安装使用 3.mysql数据库基础 4.mysql命令 5.事务 6.索引 7.Python操作mysql 8.ORM sqlalchemy了解 数据库介 ...