Login.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018-2020 rights reserved.
  6. // +----------------------------------------------------------------------
  7. // |
  8. // +----------------------------------------------------------------------
  9. // | Date: 2020-08-25 17:23
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\system\controller;
  13. use app\BaseViewController;
  14. use app\model\system\Admin as AdminModel;
  15. use app\model\system\AdminMenu;
  16. use app\Request;
  17. use library\exceptions\GeneralException;
  18. use library\lib\hupun;
  19. use library\lib\weixina;
  20. use library\services\UtilService;
  21. use library\utils\Captcha;
  22. use library\utils\Qiniu;
  23. use think\facade\Db;
  24. class Login extends BaseViewController
  25. {
  26. /**
  27. * 后台登录
  28. * @param Request $request
  29. * @return mixed
  30. * @throws \Psr\SimpleCache\InvalidArgumentException
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function index(Request $request)
  36. {
  37. [$account, $pwd, $imgcode] = UtilService::getMore([
  38. ['account', '', 'empty', '请输入登录账户'],
  39. ['pwd', '', 'empty', '请输入登录密码'],
  40. ['imgcode', '', 'empty', '验证码不能为空'],
  41. ], $request, true);
  42. //验证码
  43. if (!(new Captcha)->check($imgcode)) {
  44. return app('json')->fail('验证码错误,请重新输入');
  45. }
  46. //获取登录
  47. $adminInfo = AdminModel::login($account, $pwd);
  48. if (empty($adminInfo)) {
  49. return app('json')->fail(AdminModel::getErrorInfo('用户名错误,请重新输入'));
  50. }
  51. //生成令牌
  52. $token = AdminModel::createToken($adminInfo, 'admin');
  53. if (empty($token)) {
  54. return app('json')->fail(AdminModel::getErrorInfo());
  55. }
  56. //获取管理菜单
  57. $menuMenu = new AdminMenu();
  58. [$menus, $paths] = $menuMenu->getRoute($adminInfo->role_id);
  59. return app('json')->success([
  60. 'token' => md5($token['token']),
  61. 'expires_time' => $token['params']['exp'],
  62. 'menus' => $menus,
  63. 'paths' => $paths,
  64. 'user_info' => [
  65. 'id' => $adminInfo->getData('id'),
  66. 'username' => $adminInfo->getData('username'),
  67. 'name' => $adminInfo->getData('name'),
  68. 'avatar' => $adminInfo->getData('avatar'),
  69. ],
  70. ]);
  71. }
  72. /**
  73. * 用户发生退出
  74. * @param Request $request
  75. */
  76. public function logut(Request $request)
  77. {
  78. echo 'a';
  79. var_dump($request->post());
  80. exit;
  81. }
  82. /**
  83. * 验证码
  84. * @return \app\adminapi\controller\Login|\think\Response
  85. */
  86. public function captcha()
  87. {
  88. return (new Captcha(["length"=>4]))->create();
  89. }
  90. }