form表单公用

2023-07-29,

<?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表单公用的相关教程结束。

《form表单公用.doc》

下载本文的Word格式文档,以方便收藏与打印。