CollegeApproval.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\admin\controller\auth;
  3. use ba\Random;
  4. use Throwable;
  5. use think\facade\Db;
  6. use app\common\controller\Backend;
  7. use app\admin\model\Admin as AdminModel;
  8. class CollegeApproval extends Backend
  9. {
  10. /**
  11. * 模型
  12. * @var object
  13. * @phpstan-var AdminModel
  14. */
  15. protected object $model;
  16. protected array|string $preExcludeFields = ['create_time', 'update_time', 'password', 'salt', 'login_failure', 'last_login_time', 'last_login_ip'];
  17. protected array|string $quickSearchField = ['nickname'];
  18. public function initialize(): void
  19. {
  20. parent::initialize();
  21. $this->model = new AdminModel();
  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. // halt($where, $alias, $limit, $order);
  34. // halt($where, $alias, $limit, $order,$this->withJoinTable);
  35. $res = $this->model
  36. ->withoutField('login_failure,password,salt')
  37. ->leftJoin("admin_group_access group",'admin.id = group.uid')
  38. ->alias($alias)
  39. ->where($where)
  40. ->where('group.group_id',3)
  41. ->order($order)
  42. ->paginate($limit);
  43. $this->success('', [
  44. 'list' => $res->items(),
  45. 'total' => $res->total(),
  46. 'remark' => get_route_remark(),
  47. ]);
  48. }
  49. /**
  50. * 编辑
  51. * @throws Throwable
  52. */
  53. public function edit($id = null): void
  54. {
  55. $row = $this->model->find($id);
  56. if (!$row) {
  57. $this->error(__('Record not found'));
  58. }
  59. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  60. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  61. $this->error(__('You have no permission'));
  62. }
  63. if ($this->request->isPost()) {
  64. $data = $this->request->post();
  65. if (!$data) {
  66. $this->error(__('Parameter %s can not be empty', ['']));
  67. }
  68. if( $data['switch'] == 0 && !isset($data['types'])){
  69. $this->error('请确保至少有一个学院审批人!');
  70. }
  71. $data = $this->excludeFields($data);
  72. $result = false;
  73. $this->model->startTrans();
  74. try {
  75. if( $data['switch'] == 1 && !isset($data['types'])){
  76. $this->model->where('switch',1)->update(['switch' => 0]);
  77. }
  78. $result = $row->save($data);
  79. $this->model->commit();
  80. } catch (Throwable $e) {
  81. $this->model->rollback();
  82. $this->error($e->getMessage());
  83. }
  84. if ($result !== false) {
  85. $this->success(__('Update successful'));
  86. } else {
  87. $this->error(__('No rows updated'));
  88. }
  89. }
  90. unset($row['salt'], $row['login_failure']);
  91. $row['password'] = '';
  92. $this->success('', [
  93. 'row' => $row
  94. ]);
  95. }
  96. }