123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- namespace app\admin\controller;
- use ba\Random;
- use Throwable;
- use think\facade\Db;
- use app\common\controller\Backend;
- use app\admin\model\Asset as AssetModel;
- class Asset extends Backend
- {
- /**
- * 模型
- * @var object
- * @phpstan-var AssetModel
- */
- protected object $model;
- protected array|string $preExcludeFields = ['create_time', 'update_time'];
- protected array|string $quickSearchField = ['asset_id', 'asset_name'];
- public function initialize(): void
- {
- parent::initialize();
- $this->model = new AssetModel();
- }
- /**
- * 查看
- * @throws Throwable
- */
- public function index(): void
- {
- if ($this->request->param('select')) {
- $this->select();
- }
- list($where, $alias, $limit, $order) = $this->queryBuilder();
- $res = $this->model
- ->withJoin($this->withJoinTable, $this->withJoinType)
- ->alias($alias)
- ->where($where)
- ->order($order)
- ->paginate($limit);
- $this->success('', [
- 'list' => $res->items(),
- 'total' => $res->total(),
- 'remark' => get_route_remark(),
- ]);
- }
- /**
- * 添加
- * @throws Throwable
- */
- public function add(): void
- {
- if ($this->request->isPost()) {
- $data = $this->request->post();
- if (!$data) {
- $this->error(__('Parameter %s can not be empty', ['']));
- }
- if ($this->modelValidate) {
- try {
- $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = new $validate;
- $validate->scene('add')->check($data);
- } catch (Throwable $e) {
- $this->error($e->getMessage());
- }
- }
- $data = $this->excludeFields($data);
- $result = false;
- $this->model->startTrans();
- try {
- $result = $this->model->save($data);
- $this->model->commit();
- } catch (Throwable $e) {
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success(__('Added successfully'));
- } else {
- $this->error(__('No rows were added'));
- }
- }
- $this->error(__('Parameter error'));
- }
- /**
- * 编辑
- * @throws Throwable
- */
- public function edit($id = null): void
- {
- $row = $this->model->find($id);
- if (!$row) {
- $this->error(__('Record not found'));
- }
- $dataLimitAdminIds = $this->getDataLimitAdminIds();
- if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
- $this->error(__('You have no permission'));
- }
- if ($this->request->isPost()) {
- $data = $this->request->post();
- if (!$data) {
- $this->error(__('Parameter %s can not be empty', ['']));
- }
- /**
- * 由于有密码字段-对方法进行重写
- * 数据验证
- */
- if ($this->modelValidate) {
- try {
- $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = new $validate;
- $validate->scene('edit')->check($data);
- } catch (Throwable $e) {
- $this->error($e->getMessage());
- }
- }
- if ($this->auth->id == $data['id'] && $data['status'] == '0') {
- $this->error(__('Please use another administrator account to disable the current account!'));
- }
- if (isset($data['password']) && $data['password']) {
- $this->model->resetPassword($data['id'], $data['password']);
- }
- $data = $this->excludeFields($data);
- $result = false;
- $this->model->startTrans();
- try {
- $result = $row->save($data);
- $this->model->commit();
- } catch (Throwable $e) {
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success(__('Update successful'));
- } else {
- $this->error(__('No rows updated'));
- }
- }
- unset($row['salt'], $row['login_failure']);
- $row['password'] = '';
- $this->success('', [
- 'row' => $row
- ]);
- }
- /**
- * 删除
- * @param null $ids
- * @throws Throwable
- */
- public function del($ids = null): void
- {
- if (!$this->request->isDelete() || !$ids) {
- $this->error(__('Parameter error'));
- }
- $dataLimitAdminIds = $this->getDataLimitAdminIds();
- if ($dataLimitAdminIds) {
- $this->model->where($this->dataLimitField, 'in', $dataLimitAdminIds);
- }
- $pk = $this->model->getPk();
- $data = $this->model->where($pk, 'in', $ids)->select();
- $count = 0;
- $this->model->startTrans();
- try {
- foreach ($data as $v) {
- if ($v->id != $this->auth->id) {
- $count += $v->delete();
- Db::name('admin_group_access')
- ->where('uid', $v['id'])
- ->delete();
- }
- }
- $this->model->commit();
- } catch (Throwable $e) {
- $this->model->rollback();
- $this->error($e->getMessage());
- }
- if ($count) {
- $this->success(__('Deleted successfully'));
- } else {
- $this->error(__('No rows were deleted'));
- }
- }
- /**
- * 检查分组权限
- * @throws Throwable
- */
- public function checkGroupAuth(array $groups): void
- {
- if ($this->auth->isSuperAdmin()) {
- return;
- }
- $authGroups = $this->auth->getAllAuthGroups('allAuthAndOthers');
- foreach ($groups as $group) {
- if (!in_array($group, $authGroups)) {
- $this->error(__('You have no permission to add an administrator to this group!'));
- }
- }
- }
- }
|