123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- namespace app\adminapi\controller\v1\setting;
- use app\adminapi\controller\AuthController;
- use crmeb\services\CacheService;
- use crmeb\services\FormBuilder as Form;
- use crmeb\services\UtilService;
- use Exception;
- use FormBuilder\exception\FormBuilderException;
- use Psr\SimpleCache\InvalidArgumentException;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- use think\facade\Config;
- use think\facade\Route as Url;
- use app\models\system\SystemRole;
- use app\models\system\SystemAdmin as SystemAdminModel;
- use think\Request;
- use think\Response;
- class SystemAdmin extends AuthController
- {
-
- public function index()
- {
- [$name, $roles, $page, $limit] = UtilService::getMore([
- ['name', ''],
- ['roles', ''],
- ['page', 1],
- ['limit', 10],
- ], $this->request, true);
- return $this->success(SystemAdminModel::getAdminList($name, bcadd($this->adminInfo['level'], 1, 0), $roles, $page, $limit, $this->merId));
- }
-
- public function create()
- {
- $f[] = Form::input('account', '管理员账号')->required('请填写管理员账号');
- $f[] = Form::input('pwd', '管理员密码')->type('password')->required('请填写管理员密码');
- $f[] = Form::input('conf_pwd', '确认密码')->type('password')->required('请输入确认密码');
- $f[] = Form::input('real_name', '管理员姓名')->required('请输入管理员姓名');
- $list = SystemRole::getRole(bcadd($this->adminInfo['level'], 1, 0));
- $options = [];
- foreach ($list as $id => $roleName) {
- $options[] = ['label' => $roleName, 'value' => $id];
- }
- $f[] = Form::select('roles', '管理员身份')->setOptions($options)->multiple(true)->required('请选择管理员身份');
- $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
- return $this->makePostForm('管理员添加', $f, Url::buildUrl('/setting/admin')->suffix(false));
- }
-
- public function save(Request $request)
- {
- $data = UtilService::postMore([
- ['account', ''],
- ['conf_pwd', ''],
- ['pwd', ''],
- ['real_name', ''],
- ['roles', []],
- ['status', 0],
- ], $request);
- $this->validate($data, \app\adminapi\validates\setting\SystemAdminValidata::class);
- if ($data['conf_pwd'] != $data['pwd']) return $this->fail('两次输入的密码不相同');
- unset($data['conf_pwd']);
- if (SystemAdminModel::be(['account' => $data['account']])) return $this->fail('管理员账号已存在');
- $data['pwd'] = password_hash($data['pwd'], PASSWORD_BCRYPT);
- $data['add_time'] = time();
- $data['level'] = $this->adminInfo['level'] + 1;
- $data['mer_id'] = $this->merId;
- $data['roles'] = implode(',', $data['roles']);
- if (SystemAdminModel::create($data))
- return $this->success('添加成功');
- else
- return $this->fail('添加失败');
- }
-
- public function edit($id)
- {
- if (!$id || !($adminInfo = SystemAdminModel::where("mer_id", 'in', [0, $this->merId])->where('id', $id)->find()))
- return $this->fail('管理员信息读取失败');
- $f[] = Form::input('account', '管理员账号', $adminInfo->getData('account'))->required('请填写管理员账号');
- $f[] = Form::input('pwd', '管理员密码')->type('password')->placeholder('请填写管理员密码');
- $f[] = Form::input('conf_pwd', '确认密码')->type('password')->placeholder('请输入确认密码');
- $f[] = Form::input('real_name', '管理员姓名', $adminInfo->getData('real_name'))->required('请输入管理员姓名');
- $list = SystemRole::getRole(bcadd($this->adminInfo['level'], 1, 0));
- $options = [];
- foreach ($list as $k => $roleName) {
- $options[] = ['label' => $roleName, 'value' => $k];
- }
- $f[] = Form::select('roles', '管理员身份', $adminInfo->roles)->setOptions($options)->multiple(true)->required('请选择管理员身份');
- $f[] = Form::radio('status', '状态', $adminInfo->getData('status'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
- return $this->makePostForm('管理员修改', $f, Url::buildUrl('/setting/admin/' . $id)->suffix(false), 'PUT');
- }
-
- public function update(Request $request, $id)
- {
- $data = UtilService::postMore([
- ['account', ''],
- ['conf_pwd', ''],
- ['pwd', ''],
- ['real_name', ''],
- ['roles', []],
- ['status', 0],
- ], $request);
- $this->validate($data, \app\adminapi\validates\setting\SystemAdminValidata::class, 'update');
- if (!$adminInfo = SystemAdminModel::where("mer_id", 'in', [0, $this->merId])->where('id', $id)->find())
- return $this->fail('管理员不存在,无法修改');
- if ($data['pwd']) {
- if (!$data['conf_pwd'])
- return $this->fail('请输入确认密码');
- if ($data['conf_pwd'] != $data['pwd'])
- return $this->fail('上次输入的密码不相同');
- $adminInfo->pwd = password_hash($data['pwd'], PASSWORD_BCRYPT);
- }
- if (SystemAdminModel::where(['account' => $data['account']])->where('id', '<>', $id)->count())
- return $this->fail('管理员账号已存在');
- $adminInfo->roles = implode(',', $data['roles']);
- $adminInfo->real_name = $data['real_name'];
- $adminInfo->account = $data['account'];
- $adminInfo->status = $data['status'];
- if ($adminInfo->save())
- return $this->success('修改成功');
- else
- return $this->fail('修改失败');
- }
-
- public function delete($id)
- {
- if (!$id) return $this->fail('删除失败,缺少参数');
- if (!SystemAdminModel::where("mer_id", 'in', [0, $this->merId])->where('id', $id)->find()) {
- if (!$id) return $this->fail('删除失败');
- }
- if (SystemAdminModel::edit(['is_del' => 1, 'status' => 0], $id, 'id'))
- return $this->success('删除成功!');
- else
- return $this->fail('删除失败');
- }
-
- public function set_status($id, $status)
- {
- if (!$id) return $this->fail('修改失败,缺少参数');
- if (!SystemAdminModel::where("mer_id", 'in', [0, $this->merId])->where('id', $id)->find()) {
- if (!$id) return $this->fail('修改失败');
- }
- SystemAdminModel::where(['id' => $id])->update(['status' => $status]);
- return $this->success($status == 0 ? '关闭成功' : '开启成功');
- }
-
- public function info()
- {
- return $this->success(SystemAdminModel::where(['id' => $this->adminId])->find()->hidden(['pwd', 'is_del', 'status'])->toArray());
- }
-
- public function update_admin()
- {
- $data = UtilService::postMore([
- ['real_name', ''],
- ['head_pic', ''],
- ['pwd', ''],
- ['new_pwd', ''],
- ['conf_pwd', ''],
- ], $this->request);
- $adminInfo = SystemAdminModel::get($this->adminId);
- if (!$adminInfo)
- return $this->fail('管理员信息未查到');
- if (!$data['real_name'])
- return $this->fail('管理员姓名不能为空');
- if ($data['pwd']) {
- if (!password_verify($data['pwd'], $this->adminInfo['pwd']))
- return $this->fail('原始密码错误');
- if (!$data['new_pwd'])
- return $this->fail('请输入新密码');
- if (!$data['conf_pwd'])
- return $this->fail('请输入确认密码');
- if ($data['new_pwd'] != $data['conf_pwd'])
- return $this->fail('两次输入的密码不一致');
- $adminInfo->pwd = password_hash($data['new_pwd'], PASSWORD_BCRYPT);
- }
- $adminInfo->real_name = $data['real_name'];
- $adminInfo->head_pic = $data['head_pic'];
- if ($adminInfo->save())
- return $this->success('修改成功');
- else
- return $this->fail('修改失败');
- }
-
- public function logout()
- {
- $key = trim(ltrim($this->request->header(Config::get('cookie.token_name')), 'Bearer'));
- $res = CacheService::redisHandler()->delete($key);
- return $this->success();
- }
- }
|