Admin.php 7.3 KB

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