AuthController.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Helpers\Jwt;
  4. use App\Models\UserModel;
  5. use App\Services\RedisService;
  6. use App\Services\SnapshotService;
  7. use App\Services\WechatService;
  8. use Illuminate\Support\Facades\Session;
  9. /**
  10. * 授权控制器基类
  11. * @author wesmiler
  12. * @since 2020/11/10
  13. * Class AuthController
  14. * @package App\Http\Controllers
  15. */
  16. class AuthController extends BaseController
  17. {
  18. /**
  19. * 构造函数
  20. * @author wesmiler
  21. * @since 2020/11/11
  22. * AuthController constructor.
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. public function index(){
  29. $code = request()->get('code');
  30. if(empty($code)){
  31. return message('code参数错误',false);
  32. }
  33. // 授权
  34. $this->userInfo = WechatService::auth();
  35. $openid = isset($this->userInfo['openid'])? $this->userInfo['openid'] : '';
  36. $status = isset($this->userInfo['status'])? $this->userInfo['status'] : '';
  37. $userId = isset($this->userInfo['id'])? $this->userInfo['id'] : 0;
  38. if(empty($this->userInfo) || empty($openid) || $userId<=0){
  39. return message('用户授权失败',false);
  40. }
  41. if($status == 3){
  42. return message('用户已被拉入黑名单',false);
  43. }
  44. if($status != 1){
  45. return message('用户账户已被冻结不可操作',false);
  46. }
  47. // 获取授权TOKEN
  48. $jwt = new Jwt('jwt_wx');
  49. $token = $jwt->getToken($userId, 3);
  50. // 结果返回
  51. $result = [
  52. 'access_token' => $token,
  53. 'info'=> $this->userInfo,
  54. ];
  55. // 用户信息
  56. RedisService::set("auths:request:$userId", request()->all(), 600);
  57. RedisService::set("auths:info:{$userId}", $this->userInfo, 4*24*3600);
  58. return message('获取授权成功', true, $result);
  59. }
  60. /**
  61. * 获取授权跳转地址
  62. * @return array
  63. */
  64. public function authUrl(){
  65. $url = request()->get('url');
  66. return message('获取授权成功', true, WechatService::makeRedirectUrl($url));
  67. }
  68. /**
  69. * 微信授权配置入口
  70. */
  71. public function check(){
  72. return WechatService::valid();
  73. }
  74. }