AuthController.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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:info:{$userId}", $this->userInfo, 4*24*3600);
  57. return message('获取授权成功', true, $result);
  58. }
  59. /**
  60. * 获取授权跳转地址
  61. * @return array
  62. */
  63. public function authUrl(){
  64. $url = request()->get('url');
  65. return message('获取授权成功', true, WechatService::makeRedirectUrl($url));
  66. }
  67. /**
  68. * 微信授权配置入口
  69. */
  70. public function check(){
  71. return WechatService::valid();
  72. }
  73. }