123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace app\controller\admin\system\config;
- use ln\basic\BaseController;
- use app\common\repositories\system\config\ConfigClassifyRepository;
- use app\common\repositories\system\config\ConfigRepository;
- use app\validate\admin\ConfigValidate;
- use ln\services\UploadService;
- use FormBuilder\Exception\FormBuilderException;
- use think\App;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- class Config extends BaseController
- {
-
- protected $repository;
-
- public function __construct(App $app, ConfigRepository $repository)
- {
- parent::__construct($app);
- $this->repository = $repository;
- }
-
- public function lst()
- {
- $where = $this->request->params(['keyword']);
- [$page, $limit] = $this->getPage();
- $lst = $this->repository->lst($where, $page, $limit);
- return app('json')->success($lst);
- }
-
- public function createTable()
- {
- $form = $this->repository->form();
- return app('json')->success(formToData($form));
- }
-
- public function updateTable($id)
- {
- if (!$this->repository->exists($id)) app('json')->fail('数据不存在');
- $form = $this->repository->updateForm($id);
- return app('json')->success(formToData($form));
- }
-
- public function switchStatus($id)
- {
- $status = $this->request->param('status', 0);
- if (!$this->repository->exists($id))
- return app('json')->fail('分类不存在');
- $this->repository->switchStatus($id, $status == 1 ? 1 : 0);
- return app('json')->success('修改成功');
- }
-
- public function get($id)
- {
- $data = $this->repository->get($id);
- if (!$data)
- return app('json')->fail('配置不存在');
- else
- return app('json')->success($data->hidden(['mer_id', 'value']));
- }
-
- public function create(ConfigValidate $validate, ConfigClassifyRepository $configClassifyRepository)
- {
- $data = $this->request->params(['user_type', 'config_classify_id', 'config_name', 'config_key', 'config_type', 'config_rule', 'required', 'info', 'sort', 'status']);
- $validate->check($data);
- if (!$configClassifyRepository->exists($data['config_classify_id']))
- return app('json')->fail('配置分类不已存在');
- if ($this->repository->keyExists($data['config_key']))
- return app('json')->fail('配置key已存在');
- $this->repository->create($data);
- return app('json')->success('添加成功');
- }
-
- public function update($id, ConfigValidate $validate, ConfigClassifyRepository $configClassifyRepository)
- {
- $data = $this->request->params(['user_type', 'config_classify_id', 'config_name', 'config_key', 'config_type', 'config_rule', 'required', 'info', 'sort', 'status']);
- $validate->check($data);
- if (!$this->repository->exists($id))
- return app('json')->fail('分类不存在');
- if (!$configClassifyRepository->exists($data['config_classify_id']))
- return app('json')->fail('配置分类不已存在');
- if ($this->repository->keyExists($data['config_key'], $id))
- return app('json')->fail('配置key已存在');
- $this->repository->update($id, $data);
- return app('json')->success('修改成功');
- }
-
- public function delete($id)
- {
- $this->repository->delete($id);
- return app('json')->success('删除成功');
- }
-
- public function form($key, ConfigClassifyRepository $configClassifyRepository)
- {
- if (!$configClassifyRepository->keyExists($key) || !($configClassfiy = $configClassifyRepository->keyByData($key)))
- return app('json')->fail('配置分类不存在');
- $form = $this->repository->cidByFormRule($configClassfiy, $this->request->merId());
- return app('json')->success(formToData($form));
- }
- public function upload($field)
- {
- $file = $this->request->file($field);
- if (!$file)
- return app('json')->fail('请上传附件');
- $upload = UploadService::create(1);
- $data = $upload->to('attach')->validate()->move($field);
- if ($data === false) {
- return app('json')->fail($upload->getError());
- }
- return app('json')->success(['src' => path_to_url($upload->getFileInfo()->filePath)]);
- }
- }
|