Login.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @2021-7-10
  4. **/
  5. namespace app\Admin\controller;
  6. use think\admin\helper\TokenHelper;
  7. use think\Db;
  8. class Login
  9. {
  10. /**
  11. * @method post
  12. * @param username 账户名
  13. * @param password 密码
  14. *
  15. */
  16. public function __construct(){
  17. if(request()->isOptions()){
  18. echo '';
  19. die();
  20. }
  21. }
  22. public function index(){
  23. $post=request()->post();
  24. $username = isset($post['username'])&&$post['username']!="" ? trim($post['username']) :"";
  25. if($username==""){
  26. return error_show(1004,"参数username 不能为空");
  27. }
  28. $password = isset($post['password'])&&$post['password']!="" ? trim($post['password']):"";
  29. if($password==""){
  30. return error_show(1004,"参数password 不能为空");
  31. }
  32. $account = Db::name("admin")->where(["is_del"=>0,"username"=>$username])->find();
  33. if(empty($account)){
  34. return error_show(1005,"账户未找到");
  35. }
  36. if($account['status']==0){
  37. return error_show(1005,"账户已禁用");
  38. }
  39. $pass = sha1($password.$account['salt']);
  40. if($pass!=$account['password']){
  41. return error_show(1006,"账户密码错误");
  42. }
  43. $token = makeToken($account);
  44. $userinfo = Db::name("account_info")->alias("a")->join("fc_rela_account b","b.account_info=a.id")->where(["b.accountid"=>$account['id']])->field("a.*")->find();
  45. $userinfo['token'] = $token;
  46. write_log("账户{$account['username']}登录系统","","login","",0);
  47. return app_show(0,"登录成功",$userinfo);
  48. }
  49. /**
  50. * @param Token
  51. * 退出登录
  52. */
  53. public function logout(){
  54. $post=request()->post();
  55. $token = isset($post['token'])&&$post['token']!="" ? trim($post['token']) :"";
  56. if($token==""){
  57. return error_show(101,"参数token 不能为空");
  58. }
  59. $verify = verifyToken($token);
  60. if($verify['code']!=0){
  61. return error_show($verify['code'],$verify['msg']);
  62. }
  63. $info = Db::name("admin_token")->where(["token"=>$token])->update(['token'=>""]);
  64. if($info){
  65. return app_show(0,"退出成功");
  66. }else{
  67. return error_show(1004,"退出失败");
  68. }
  69. }
  70. public function LastVersion(){
  71. $version = Db::name("version")->order("addtime desc")->find();
  72. return app_show(0,"获取成功",$version);
  73. }
  74. }