123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\utils\AppResult;
- use app\utils\DataUtil;
- use app\utils\VueAdmin\AdminData;
- use think\Request;
- /**
- * Admin 模块父级
- * Module 接管 CURD 公共操作
- * Class ModuleController
- * @package App\Http\Controllers\Framework
- */
- class VueModule extends Backend
- {
- protected $model; // 在子类中赋值
- protected array $params = []; // 请求参数
- protected array $unset_fields = ['status']; // 初始化隐藏字段
- protected int $statusValue = 1; // 状态默认值
- protected bool $isPost; // 请求类型是否为POST
- public function __construct(Request $request = null)
- {
- parent::__construct($request);
- $this->isPost = $this->request->isPost();
- $this->params = $this->request->param();
- // 前置操作
- $this->callAction($this->request->action(), $this->request->param());
- }
- /**
- * Execute an action on the controller.
- *
- * @param $method
- * @param $parameters
- * @return mixed
- */
- public function callAction($method, $parameters = [])
- {
- return $this;
- }
- /**
- * 列表页
- * @return string|\think\response\Json
- * @throws \think\Exception
- */
- public function index()
- {
- if ($this->isPost) {
- $params = $this->params;
- $query = $this->model;
- // 自动排序
- if ($orderBy = AdminData::orderBy()) {
- foreach ($orderBy as $order) {
- $query->order($order['sort'], $order['orderBy']);
- }
- }
- if (!isset($params['page'])) {
- $list = $query->select();
- } else {
- $list = $query->paginate($params['size'] ?? 15);
- }
- return AppResult::response200('success', $list);
- } else {
- return $this->view->fetch();
- }
- }
- /**
- * 添加
- * @return string
- */
- public function insert()
- {
- $params = $this->params;
- // 初始化空数据
- $form = DataUtil::field($params, $this->unset_fields);
- $form['status'] = $this->statusValue;
- $form['create_time'] = time();
- if (!$this->model->insert($form)) {
- return AppResult::response201('创建失败');
- }
- return AppResult::response200('创建成功');
- }
- /**
- * 修改
- * @return string
- */
- public function update()
- {
- $params = $this->params;
- if (empty($params['id'])) {
- return AppResult::response203('操作超时!');
- }
- // 初始化空数据
- $form = DataUtil::field($params, array_merge(['is_system'], $this->unset_fields));
- $form['update_time'] = time();
- if (!$this->model->where('id', $params['id'])->update($form)) {
- return AppResult::response201('操作失败');
- }
- return AppResult::response200('操作成功');
- }
- /**
- * 状态
- * @return string
- */
- public function status()
- {
- $params = $this->params;
- if (empty($params['id']) || !isset($params['status'])) {
- return AppResult::response203('操作超时!');
- }
- $form['status'] = $params['status'] == 1 ? 2 : 1;
- $form['update_time'] = time();
- if (!$this->model->where('id', $params['id'])->update($form)) {
- return AppResult::response201('操作失败');
- }
- return AppResult::response200('操作成功');
- }
- /**
- * 删除
- * @return string
- */
- public function remove()
- {
- $params = $this->params;
- if (empty($params['id'])) {
- return AppResult::response203('操作超时!');
- }
- // $form['delete_time'] = time();
- // $form['update_time'] = time();
- // if (!$this->model->where('id', $params['id'])->update($form)) {
- // return AppResult::response201('操作失败');
- // }
- if (!$this->model->where('id', $params['id'])->delete()) {
- return AppResult::response201('操作失败');
- }
- return AppResult::response200('操作成功');
- }
- /**
- * 排序
- * @return string
- */
- public function rank()
- {
- $params = $this->params;
- if (!isset($params['id']) || !isset($params['weigh'])) {
- return AppResult::response203('操作超时!');
- }
- $form['weigh'] = $params['weigh'];
- $form['update_time'] = time();
- if (!$this->model->where('id', $params['id'])->update($form)) {
- return AppResult::response201('操作失败');
- }
- return AppResult::response200('操作成功');
- }
- }
|