BaseController.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Http\Controllers\KuaiYingYong;
  3. use Illuminate\Routing\Controller;
  4. use App\Modules\User\Models\Xcxuser;
  5. use JWTAuth;
  6. class BaseController extends Controller
  7. {
  8. /**
  9. * 公众号接口签名密钥
  10. * @var string
  11. */
  12. protected $secret_key = 'Uv%vkPI5K8Opqoww';
  13. protected function checkUid(){
  14. if(!$this->getAuthenticatedUser()) return false;
  15. return true;
  16. }
  17. public function __get($name)
  18. {
  19. static $user =null;
  20. if(is_null($user)) $user = $this->getAuthenticatedUser();
  21. if($name == '_user_info'){
  22. return $user;
  23. }
  24. if($name == 'uid'){
  25. return $user->id;
  26. }
  27. if($name == 'distribution_channel_id'){
  28. return $user->distribution_channel_id;
  29. }
  30. if($name == 'openid'){
  31. $xcx_user = Xcxuser::where('uid',$user->id)->select('openid')->first();
  32. return $xcx_user->openid;
  33. }
  34. return null;
  35. }
  36. public function getAuthenticatedUser(){
  37. try {
  38. if (! $user = JWTAuth::parseToken()->authenticate()) {
  39. return false;
  40. }
  41. } catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {
  42. return false;
  43. //return response()->json(['token_expired'], $e->getStatusCode());
  44. } catch (\Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {
  45. return false;
  46. //return response()->json(['token_invalid'], $e->getStatusCode());
  47. } catch (\Tymon\JWTAuth\Exceptions\JWTException $e) {
  48. return false;
  49. //return response()->json(['token_absent'], $e->getStatusCode());
  50. }
  51. return $user;
  52. }
  53. public function checkSign($param){
  54. if(!isset($param['sign'])) return false;
  55. if(empty($param['sign'])) return false;
  56. return $param['sigin'] == _sign($param,$this->secret_key);
  57. }
  58. }