Asset.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\admin\controller;
  3. use ba\Random;
  4. use Throwable;
  5. use think\facade\Db;
  6. use app\common\controller\Backend;
  7. use app\admin\model\Asset as AssetModel;
  8. class Asset extends Backend
  9. {
  10. /**
  11. * 模型
  12. * @var object
  13. * @phpstan-var AssetModel
  14. */
  15. protected object $model;
  16. protected array|string $preExcludeFields = ['create_time', 'update_time'];
  17. protected array|string $quickSearchField = ['asset_id', 'asset_name'];
  18. public function initialize(): void
  19. {
  20. parent::initialize();
  21. $this->model = new AssetModel();
  22. }
  23. /**
  24. * 查看
  25. * @throws Throwable
  26. */
  27. public function index(): void
  28. {
  29. if ($this->request->param('select')) {
  30. $this->select();
  31. }
  32. list($where, $alias, $limit, $order) = $this->queryBuilder();
  33. $res = $this->model
  34. ->withJoin($this->withJoinTable, $this->withJoinType)
  35. ->alias($alias)
  36. ->where($where)
  37. ->order($order)
  38. ->paginate($limit);
  39. $this->success('', [
  40. 'list' => $res->items(),
  41. 'total' => $res->total(),
  42. 'remark' => get_route_remark(),
  43. ]);
  44. }
  45. /**
  46. * 添加
  47. * @throws Throwable
  48. */
  49. public function add(): void
  50. {
  51. if ($this->request->isPost()) {
  52. $data = $this->request->post();
  53. if (!$data) {
  54. $this->error(__('Parameter %s can not be empty', ['']));
  55. }
  56. if ($this->modelValidate) {
  57. try {
  58. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  59. $validate = new $validate;
  60. $validate->scene('add')->check($data);
  61. } catch (Throwable $e) {
  62. $this->error($e->getMessage());
  63. }
  64. }
  65. $data = $this->excludeFields($data);
  66. $result = false;
  67. $this->model->startTrans();
  68. try {
  69. $result = $this->model->save($data);
  70. $this->model->commit();
  71. } catch (Throwable $e) {
  72. $this->model->rollback();
  73. $this->error($e->getMessage());
  74. }
  75. if ($result !== false) {
  76. $this->success(__('Added successfully'));
  77. } else {
  78. $this->error(__('No rows were added'));
  79. }
  80. }
  81. $this->error(__('Parameter error'));
  82. }
  83. /**
  84. * 编辑
  85. * @throws Throwable
  86. */
  87. public function edit($id = null): void
  88. {
  89. $row = $this->model->find($id);
  90. if (!$row) {
  91. $this->error(__('Record not found'));
  92. }
  93. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  94. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  95. $this->error(__('You have no permission'));
  96. }
  97. if ($this->request->isPost()) {
  98. $data = $this->request->post();
  99. if (!$data) {
  100. $this->error(__('Parameter %s can not be empty', ['']));
  101. }
  102. /**
  103. * 由于有密码字段-对方法进行重写
  104. * 数据验证
  105. */
  106. if ($this->modelValidate) {
  107. try {
  108. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  109. $validate = new $validate;
  110. $validate->scene('edit')->check($data);
  111. } catch (Throwable $e) {
  112. $this->error($e->getMessage());
  113. }
  114. }
  115. if ($this->auth->id == $data['id'] && $data['status'] == '0') {
  116. $this->error(__('Please use another administrator account to disable the current account!'));
  117. }
  118. if (isset($data['password']) && $data['password']) {
  119. $this->model->resetPassword($data['id'], $data['password']);
  120. }
  121. $data = $this->excludeFields($data);
  122. $result = false;
  123. $this->model->startTrans();
  124. try {
  125. $result = $row->save($data);
  126. $this->model->commit();
  127. } catch (Throwable $e) {
  128. $this->model->rollback();
  129. $this->error($e->getMessage());
  130. }
  131. if ($result !== false) {
  132. $this->success(__('Update successful'));
  133. } else {
  134. $this->error(__('No rows updated'));
  135. }
  136. }
  137. unset($row['salt'], $row['login_failure']);
  138. $row['password'] = '';
  139. $this->success('', [
  140. 'row' => $row
  141. ]);
  142. }
  143. /**
  144. * 删除
  145. * @param null $ids
  146. * @throws Throwable
  147. */
  148. public function del($ids = null): void
  149. {
  150. if (!$this->request->isDelete() || !$ids) {
  151. $this->error(__('Parameter error'));
  152. }
  153. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  154. if ($dataLimitAdminIds) {
  155. $this->model->where($this->dataLimitField, 'in', $dataLimitAdminIds);
  156. }
  157. $pk = $this->model->getPk();
  158. $data = $this->model->where($pk, 'in', $ids)->select();
  159. $count = 0;
  160. $this->model->startTrans();
  161. try {
  162. foreach ($data as $v) {
  163. if ($v->id != $this->auth->id) {
  164. $count += $v->delete();
  165. Db::name('admin_group_access')
  166. ->where('uid', $v['id'])
  167. ->delete();
  168. }
  169. }
  170. $this->model->commit();
  171. } catch (Throwable $e) {
  172. $this->model->rollback();
  173. $this->error($e->getMessage());
  174. }
  175. if ($count) {
  176. $this->success(__('Deleted successfully'));
  177. } else {
  178. $this->error(__('No rows were deleted'));
  179. }
  180. }
  181. /**
  182. * 检查分组权限
  183. * @throws Throwable
  184. */
  185. public function checkGroupAuth(array $groups): void
  186. {
  187. if ($this->auth->isSuperAdmin()) {
  188. return;
  189. }
  190. $authGroups = $this->auth->getAllAuthGroups('allAuthAndOthers');
  191. foreach ($groups as $group) {
  192. if (!in_array($group, $authGroups)) {
  193. $this->error(__('You have no permission to add an administrator to this group!'));
  194. }
  195. }
  196. }
  197. }