123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- /**
- * @Created by PhpStorm
- * @author: Kirin
- * @day: 2024/11/20
- * @time: 11:24
- */
- namespace app\common;
- use app\Request;
- use qiniu\basic\BaseController;
- use qiniu\exceptions\AdminException;
- abstract class AdminBaseController extends BaseController
- {
- /**
- * 当前登陆管理员信息
- * @var
- */
- protected $adminInfo;
- /**
- * 当前登陆管理员ID
- * @var
- */
- protected $adminId;
- /**
- * 当前管理员权限
- * @var array
- */
- protected $auth = [];
- // 搜索条件
- protected $searchable = [];
- // 搜索处理
- protected $searchDeal = null;
- // 创建参数
- protected $createParams = [];
- // 新增处理
- protected $saveDeal = null;
- // 更新处理
- protected $updateDeal = null;
- protected $validate = null;
- protected $service = null;
- /**
- * 初始化
- */
- public function __construct(Request $request)
- {
- parent::__construct($request);
- $this->initialize();
- }
- protected function initialize()
- {
- $this->adminId = $this->request->adminId();
- $this->adminInfo = $this->request->adminInfo();
- $this->auth = $this->adminInfo['rule'] ?? [];
- }
- public function index()
- {
- if (!$this->service) {
- throw new AdminException('接口不存在');
- }
- $where = $this->request->getMore($this->searchable, false, $this->searchDeal);
- list($page, $limit) = $this->service->getPageValue();
- $list = $this->service->getList($where, '*', $page, $limit);
- $count = $this->service->getCount($where);
- return $this->success(compact('list', 'count'));
- }
- public function read($id)
- {
- if (!$this->service) {
- throw new AdminException('接口不存在');
- }
- $info = $this->service->get($id);
- if (!$info)
- return $this->error('数据不存在');
- return $this->success('ok', $info->toArray());
- }
- public function save()
- {
- if (!$this->service) {
- throw new AdminException('接口不存在');
- }
- $data = $this->request->postMore($this->createParams, false, $this->saveDeal);
- if ($this->validate) {
- $this->validate($data, $this->validate, 'save');
- }
- $res = $this->service->create($data);
- if ($res) return $this->success('添加成功');
- return $this->error('添加失败');
- }
- public function validate($data, $validate, $s = '')
- {
- $scene = $validate->allScene() ?? [];
- if (in_array($s, $scene))
- $res = $validate->scene($s)->check($data);
- else
- $res = $validate->check($data);
- if (!$res) throw new AdminException($validate->getError());
- }
- public function update($id)
- {
- if (!$this->service) {
- throw new AdminException('接口不存在');
- }
- $data = $this->service->get($id);
- if (!$data)
- return $this->error('数据不存在');
- $data = $this->request->postMore($this->createParams, false, $this->updateDeal, $id);
- if ($this->validate) {
- $this->validate($data, $this->validate, 'update');
- }
- $res = $this->service->update($id, $data);
- if ($res) return $this->success('修改成功');
- return $this->error('修改失败');
- }
- public function delete($id)
- {
- if (!$this->service) {
- throw new AdminException('接口不存在');
- }
- $data = $this->service->get($id);
- if (!$data)
- return $this->error('数据不存在');
- $res = $this->service->delete($id);
- if ($res) return $this->success('已删除');
- return $this->error('删除失败');
- }
- public function export()
- {
- if (!$this->service) {
- throw new AdminException('接口不存在');
- }
- $where = $this->request->getMore($this->searchable, false, $this->searchDeal);
- $export_type = sys_config('export_type', 1);
- return $this->success($this->service->export($where, $export_type));
- }
- }
|