<?php
namespace controller;
use service\DataService;
use think\Controller;
use think\Db;
use think\db\Query;
class BasicAdmin extends Controller
{
public $title;
public $table;
protected function _form($dbQuery = null, $tplFile = '', $pkField = '', $where = [], $extendData = [])
{
$db = is_null($dbQuery) ? Db::name($this->table) : (is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery);
$pk = empty($pkField) ? ($db->getPk() ? $db->getPk() : 'id') : $pkField;
$pkValue = $this->request->request($pk, isset($where[$pk]) ? $where[$pk] : (isset($extendData[$pk]) ? $extendData[$pk] : null));
if (!$this->request->isPost()) {
$vo = ($pkValue !== null) ? array_merge((array)$db->where($pk, $pkValue)->where($where)->find(), $extendData) : $extendData;
if (false !== $this->_callback('_form_filter', $vo, [])) {
empty($this->title) || $this->assign('title', $this->title);
return $this->fetch($tplFile, ['vo' => $vo]);
}
return $vo;
}
$data = array_merge($this->request->post(), $extendData);
if (false !== $this->_callback('_form_filter', $data, [])) {
$result = DataService::save($db, $data, $pk, $where);
if (false !== $this->_callback('_form_result', $result, $data)) {
if ($result !== false) {
$this->success('恭喜, 数据保存成功!', '');
}
$this->error('数据保存失败, 请稍候再试!');
}
}
}
protected function _list($dbQuery = null, $isPage = true, $isDisplay = true, $total = false, $result = [])
{
$db = is_null($dbQuery) ? Db::name($this->table) : (is_string($dbQuery) ? Db::name($dbQuery) : $dbQuery);
if ($this->request->isPost() && $this->request->post('action') === 'resort') {
foreach ($this->request->post() as $key => $value) {
if (preg_match('/^_\d{1,}$/', $key) && preg_match('/^\d{1,}$/', $value)) {
list($where, $update) = [['id' => trim($key, '_')], ['sort' => $value]];
if (false === Db::table($db->getTable())->where($where)->update($update)) {
$this->error('列表排序失败, 请稍候再试');
}
}
}
$this->success('列表排序成功, 正在刷新列表', '');
}
if (null === $db->getOptions('order')) {
in_array('sort', $db->getTableFields($db->getTable())) && $db->order('sort asc');
}
if ($isPage) {
$rows = intval($this->request->get('rows', cookie('page-rows')));
cookie('page-rows', $rows = $rows >= 10 ? $rows : 20);
$query = $this->request->get();
$page = $db->paginate($rows, $total, ['query' => $query]);
if (($totalNum = $page->total()) > 0) {
list($rowsHTML, $pageHTML, $maxNum) = [[], [], $page->lastPage()];
foreach ([10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200] as $num) {
list($query['rows'], $query['page']) = [$num, '1'];
$url = url('@admin') . '#' . $this->request->baseUrl() . '?' . http_build_query($query);
$rowsHTML[] = "<option data-url='{$url}' " . ($rows === $num ? 'selected' : '') . " value='{$num}'>{$num}</option>";
}
for ($i = 1; $i <= $maxNum; $i++) {
list($query['rows'], $query['page']) = [$rows, $i];
$url = url('@admin') . '#' . $this->request->baseUrl() . '?' . http_build_query($query);
$selected = $i === intval($page->currentPage()) ? 'selected' : '';
$pageHTML[] = "<option data-url='{$url}' {$selected} value='{$i}'>{$i}</option>";
}
list($pattern, $replacement) = [['|href="(.*?)"|', '|pagination|'], ['data-open="$1"', 'pagination pull-right']];
$html = "<span class='pagination-trigger nowrap'>共 {$totalNum} 条记录,每页显示 <select data-auto-none>" . join('', $rowsHTML) . "</select> 条,共 " . ceil($totalNum / $rows) . " 页当前显示第 <select>" . join('', $pageHTML) . "</select> 页。</span>";
list($result['total'], $result['list'], $result['page']) = [$totalNum, $page->all(), $html . preg_replace($pattern, $replacement, $page->render())];
} else {
list($result['total'], $result['list'], $result['page']) = [$totalNum, $page->all(), $page->render()];
}
} else {
$result['list'] = $db->select();
}
if (false !== $this->_callback('_data_filter', $result['list'], []) && $isDisplay) {
!empty($this->title) && $this->assign('title', $this->title);
return $this->fetch('', $result);
}
return $result;
}
protected function _callback($method, &$data1, $data2)
{
foreach ([$method, "_" . $this->request->action() . "{$method}"] as $_method) {
if (method_exists($this, $_method) && false === $this->$_method($data1, $data2)) {
return false;
}
}
return true;
}
}