User.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\common\library;
  3. use app\common\utility\WhiteList;
  4. use Overtrue\Socialite\AuthorizeFailedException;
  5. use think\Config;
  6. use think\Cookie;
  7. use think\Exception;
  8. use think\exception\HttpException;
  9. use think\Loader;
  10. use think\Log;
  11. use think\Request;
  12. use EasyWeChat\Factory;
  13. use Symfony\Component\Cache\Simple\RedisCache;
  14. use fast\Random;
  15. use traits\controller\Jump;
  16. Loader::import('controller/Jump', TRAIT_PATH, EXT);
  17. class User
  18. {
  19. private $useTime = 0;
  20. /**
  21. * @var object 对象实例
  22. */
  23. protected static $instance;
  24. /**
  25. * @var mixed|null|array 用户信息
  26. */
  27. protected $userInfo = null;
  28. /**
  29. * 当前请求实例
  30. *
  31. * @var Request
  32. */
  33. protected $request;
  34. /**
  35. * 当前url访问类型 0未识别 1后台首页 2支付页 3推广页
  36. *
  37. * @var int
  38. */
  39. protected $urlType = 0;
  40. /**
  41. * 当前访问链接隶属于哪个渠道商
  42. *
  43. * @var null
  44. */
  45. protected $urlChannelId = null;
  46. use Jump;
  47. /**
  48. * 类架构函数
  49. * Auth constructor.
  50. */
  51. public function __construct($urlType = 0, $urlChannelId = null)
  52. {
  53. // 初始化request
  54. $this->request = Request::instance();
  55. $this->urlType = $urlType;
  56. $this->urlChannelId = $urlChannelId;
  57. if ($urlType == 3) { //推广页
  58. if (!Ua::isWeiXin() && !Cookie::has('visitor')) {
  59. Cookie::forever('visitor', $this->visitor());
  60. }
  61. }
  62. }
  63. /**
  64. * 初始化
  65. *
  66. * @access public
  67. * @return User
  68. */
  69. public static function instance($urlType = 0, $urlChannelId = null)
  70. {
  71. if (is_null(self::$instance)) {
  72. self::$instance = new static($urlType, $urlChannelId);
  73. }
  74. return self::$instance;
  75. }
  76. /**
  77. * 魔术方法获取(只支持单级属性获取,多级属性请使用getUserInfo)
  78. *
  79. * @param $name
  80. * @return mixed|null
  81. */
  82. public function __get($name)
  83. {
  84. if (is_null($this->userInfo)) {
  85. $this->getUserInfo();
  86. }
  87. return $this->userInfo[$name] ?? null;
  88. }
  89. /**
  90. * 获取visitor,先获取cookie,若无,自动生成一个
  91. *
  92. * @param bool $force 是否强制生成一个新的
  93. */
  94. public function visitor($force = false)
  95. {
  96. if (!$force && Cookie::has('visitor')) {
  97. return Cookie::get('visitor');
  98. } else {
  99. return date('YmdHis') . Random::uuid();
  100. }
  101. }
  102. }