| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\controller\admin\system\admin;
- use app\common\repositories\system\auth\RoleRepository;
- use crmeb\basic\BaseController;
- use app\common\repositories\system\admin\AdminRepository;
- use app\validate\admin\AdminEditValidate;
- use app\validate\admin\AdminValidate;
- use Exception;
- use FormBuilder\Exception\FormBuilderException;
- use think\App;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\response\Json;
- /**
- * 后台管理员
- */
- class Admin extends BaseController
- {
- protected $repository;
- public function __construct(App $app, AdminRepository $repository)
- {
- parent::__construct($app);
- $this->repository = $repository;
- }
- /**
- * 列表
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- * @author xaboy
- * @day 2020-04-09
- */
- public function getList()
- {
- $where = $this->request->params(['keyword', 'date', 'status','region_id']);
- [$page, $limit] = $this->getPage();
- return app('json')->success($this->repository->getList($where, $page, $limit));
- }
- /**
- * 修改状态
- * @param int $id
- * @return mixed
- * @throws DbException
- * @author xaboy
- * @day 2020-04-09
- */
- public function switchStatus($id)
- {
- $status = $this->request->param('status');
- if (!$this->repository->exists($id))
- return app('json')->fail('数据不存在');
- $this->repository->update($id, ['status' => $status == 1 ? 1 : 0]);
- return app('json')->success('编辑成功');
- }
- /**
- * 添加表单
- * @return mixed
- * @throws FormBuilderException
- * @author xaboy
- * @day 2020-04-09
- */
- public function createForm()
- {
- return app('json')->success(formToData($this->repository->form()));
- }
- /**
- * 修改表单
- * @param $id
- * @return Json
- * @author Qinii
- */
- public function updateForm($id)
- {
- if (!$this->repository->exists($id))
- return app('json')->fail('数据不存在');
- return app('json')->success(formToData($this->repository->updateForm($id)));
- }
- /**
- * 修改密码表单
- * @param $id
- * @return Json
- * @author Qinii
- */
- public function passwordForm($id)
- {
- if (!$this->repository->exists($id))
- return app('json')->fail('数据不存在');
- return app('json')->success(formToData($this->repository->passwordForm($id)));
- }
- /**
- * 添加
- * @param AdminValidate $validate
- * @return Json
- * @author Qinii
- */
- public function create(AdminValidate $validate)
- {
- $data = $this->request->params(['account', 'pwd', 'phone', 'againPassword', 'real_name', ['roles', []], ['status', 0]]);
- $region_ids = $this->request->param('region_ids',[]);
- if ($region_ids) {
- $data['region_ids'] = ','.implode(',', $region_ids).',';
- }
- $validate->check($data);
- if ($data['pwd'] !== $data['againPassword'])
- return app('json')->fail('两次密码输入不一致');
- unset($data['againPassword']);
- if ($this->repository->fieldExists('account', $data['account']))
- return app('json')->fail('账号已存在');
- $data['pwd'] = $this->repository->passwordEncode($data['pwd']);
- $check = app()->make(RoleRepository::class)->checkRole($data['roles'], 0);
- if (!$check) {
- return app('json')->fail('未开启或者不存在的身份不能添加');
- }
- $this->repository->create($data);
- return app('json')->success('添加成功');
- }
- /**
- * 编辑
- * @param $id
- * @param AdminValidate $validate
- * @return Json
- * @author Qinii
- */
- public function update($id, AdminValidate $validate)
- {
- $data = $this->request->params(['account', 'real_name', 'phone', ['roles', []], ['status', 0]]);
- $region_ids = $this->request->param('region_ids',[]);
- $data['region_ids'] = '';
- if ($region_ids) {
- $data['region_ids'] = ','.implode(',', $region_ids).',';
- }
- $validate->isUpdate()->check($data);
- if ($this->repository->fieldExists('account', $data['account'], $id))
- return app('json')->fail('账号已存在');
- $check = app()->make(RoleRepository::class)->checkRole($data['roles'], 0);
- if (!$check) {
- return app('json')->fail('未开启或者不存在的身份不能添加');
- }
- $this->repository->update($id, $data);
- return app('json')->success('编辑成功');
- }
- /**
- * 修改密码
- * @param $id
- * @param AdminValidate $validate
- * @return Json
- * @author Qinii
- */
- public function password($id, AdminValidate $validate)
- {
- $data = $this->request->params(['pwd', 'againPassword']);
- $validate->isPassword()->check($data);
- if ($data['pwd'] !== $data['againPassword'])
- return app('json')->fail('两次密码输入不一致');
- if (!$this->repository->exists($id))
- return app('json')->fail('管理员不存在');
- $data['pwd'] = $this->repository->passwordEncode($data['pwd']);
- unset($data['againPassword']);
- $this->repository->update($id, $data);
- return app('json')->success('修改密码成功');
- }
- /**
- * 删除
- * @param int $id
- * @return Json
- * @throws Exception
- * @author 张先生
- * @date 2020-03-30
- */
- public function delete($id)
- {
- if (!$this->repository->exists($id))
- return app('json')->fail('数据不存在');
- $this->repository->update($id, ['is_del' => 1]);
- return app('json')->success('删除成功');
- }
- /**
- * 修改信息
- * @param AdminEditValidate $validate
- * @return mixed
- * @throws DbException
- * @author xaboy
- * @day 2020-04-20
- */
- public function edit(AdminEditValidate $validate)
- {
- $data = $this->request->params(['real_name', 'phone']);
- $validate->check($data);
- $this->repository->update($this->request->adminId(), $data);
- return app('json')->success('修改成功');
- }
- /**
- * 修改表单
- * @return mixed
- * @throws FormBuilderException
- * @author xaboy
- * @day 2020-04-20
- */
- public function editForm()
- {
- $adminInfo = $this->request->adminInfo();
- return app('json')->success(formToData($this->repository->editForm(['real_name' => $adminInfo->real_name, 'phone' => $adminInfo->phone, 'admin_id' => $adminInfo->admin_id])));
- }
- /**
- * 修改自己的密码
- * @param AdminValidate $validate
- * @return mixed
- * @throws DbException
- * @author xaboy
- * @day 2020-04-20
- */
- public function editPassword(AdminValidate $validate)
- {
- $data = $this->request->params(['pwd', 'againPassword']);
- $validate->isPassword()->check($data);
- if ($data['pwd'] !== $data['againPassword'])
- return app('json')->fail('两次密码输入不一致');
- $data['pwd'] = $this->repository->passwordEncode($data['pwd']);
- unset($data['againPassword']);
- $this->repository->update($this->request->adminId(), $data);
- return app('json')->success('修改密码成功');
- }
- /**
- * 修改自己的密码表单
- * @return mixed
- * @throws FormBuilderException
- * @author xaboy
- * @day 2020-04-20
- */
- public function editPasswordForm()
- {
- return app('json')->success(formToData($this->repository->passwordForm($this->request->adminId(), true)));
- }
- }
|