Blame view

稻城亚丁小程序/backend/app/controller/admin/Auth.php 4.17 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
  <?php
  declare(strict_types=1);
  
  namespace app\controller\admin;
  
  use app\BaseController;
  use app\service\AuthContext;
  use think\facade\Db;
  
  class Auth extends BaseController
  {
      public function login()
      {
          $username = trim((string) $this->request->post('username', ''));
          $password = (string) $this->request->post('password', '');
          if ($username === '' || $password === '') {
              return json(['code' => 400, 'msg' => '请输入账号密码', 'data' => []]);
          }
  
          $admin = Db::name('sys_admin')->where('username', $username)->where('status', 1)->find();
          if (!$admin || !password_verify($password, (string) $admin['password'])) {
              return json(['code' => 400, 'msg' => '账号或密码错误', 'data' => []]);
          }
  
          $token  = bin2hex(random_bytes(32));
          $expire = date('Y-m-d H:i:s', time() + 86400 * 7);
          Db::name('sys_admin')->where('id', $admin['id'])->update([
              'api_token'     => $token,
              'token_expire'  => $expire,
              'update_time'   => date('Y-m-d H:i:s'),
          ]);
  
          $roleName = '';
          if ((int) $admin['role_id'] > 0) {
              $roleName = (string) (Db::name('sys_role')->where('id', $admin['role_id'])->value('name') ?: '');
          }
  
          return json([
              'code' => 0,
              'msg'  => 'ok',
              'data' => [
                  'token' => $token,
                  'admin' => [
                      'id'        => (int) $admin['id'],
                      'username'  => $admin['username'],
                      'nickname'  => $admin['nickname'],
                      'role_id'   => (int) $admin['role_id'],
                      'role_name' => $roleName,
                  ],
              ],
          ]);
      }
  
      public function profile()
      {
          $admin = AuthContext::admin();
          if (!$admin) {
              return json(['code' => 401, 'msg' => '未登录', 'data' => []]);
          }
  
          $roleName = '';
          if ((int) $admin['role_id'] > 0) {
              $roleName = (string) (Db::name('sys_role')->where('id', $admin['role_id'])->value('name') ?: '');
          }
  
          return json([
              'code' => 0,
              'msg'  => 'ok',
              'data' => [
                  'id'        => (int) $admin['id'],
                  'username'  => $admin['username'],
                  'nickname'  => $admin['nickname'],
                  'role_id'   => (int) $admin['role_id'],
                  'role_name' => $roleName,
              ],
          ]);
      }
  
      public function menus()
      {
          $admin = AuthContext::admin();
          if (!$admin) {
              return json(['code' => 401, 'msg' => '未登录', 'data' => []]);
          }
  
          $roleId = (int) $admin['role_id'];
          $menuIds = Db::name('sys_role_menu')->where('role_id', $roleId)->column('menu_id');
          if ($menuIds === []) {
              return json(['code' => 0, 'msg' => 'ok', 'data' => ['list' => []]]);
          }
  
          $rows = Db::name('sys_menu')
              ->whereIn('id', $menuIds)
              ->where('status', 1)
              ->order('sort', 'asc')
              ->order('id', 'asc')
              ->select()
              ->toArray();
  
          $tree = $this->buildMenuTree($rows, 0);
  
          return json(['code' => 0, 'msg' => 'ok', 'data' => ['list' => $tree]]);
      }
  
      /**
       * @param list<array<string,mixed>> $rows
       * @return list<array<string,mixed>>
       */
      protected function buildMenuTree(array $rows, int $parentId): array
      {
          $out = [];
          foreach ($rows as $r) {
              if ((int) $r['parent_id'] !== $parentId) {
                  continue;
              }
              $node = [
                  'id'        => (int) $r['id'],
                  'parent_id' => (int) $r['parent_id'],
                  'name'      => $r['name'],
                  'type'      => (int) $r['type'],
                  'path'      => $r['path'],
                  'component' => $r['component'],
                  'perms'     => $r['perms'],
                  'icon'      => $r['icon'],
                  'sort'      => (int) $r['sort'],
                  'children'  => $this->buildMenuTree($rows, (int) $r['id']),
              ];
              $out[] = $node;
          }
  
          return $out;
      }
  }