123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- namespace app\common\library;
- use app\common\utility\WhiteList;
- use Overtrue\Socialite\AuthorizeFailedException;
- use think\Config;
- use think\Cookie;
- use think\Exception;
- use think\exception\HttpException;
- use think\Loader;
- use think\Log;
- use think\Request;
- use EasyWeChat\Factory;
- use Symfony\Component\Cache\Simple\RedisCache;
- use fast\Random;
- use traits\controller\Jump;
- Loader::import('controller/Jump', TRAIT_PATH, EXT);
- class User
- {
- private $useTime = 0;
- /**
- * @var object 对象实例
- */
- protected static $instance;
- /**
- * @var mixed|null|array 用户信息
- */
- protected $userInfo = null;
- /**
- * 当前请求实例
- *
- * @var Request
- */
- protected $request;
- /**
- * 当前url访问类型 0未识别 1后台首页 2支付页 3推广页
- *
- * @var int
- */
- protected $urlType = 0;
- /**
- * 当前访问链接隶属于哪个渠道商
- *
- * @var null
- */
- protected $urlChannelId = null;
- use Jump;
- /**
- * 类架构函数
- * Auth constructor.
- */
- public function __construct($urlType = 0, $urlChannelId = null)
- {
- // 初始化request
- $this->request = Request::instance();
- $this->urlType = $urlType;
- $this->urlChannelId = $urlChannelId;
- if ($urlType == 3) { //推广页
- if (!Ua::isWeiXin() && !Cookie::has('visitor')) {
- Cookie::forever('visitor', $this->visitor());
- }
- }
- }
- /**
- * 初始化
- *
- * @access public
- * @return User
- */
- public static function instance($urlType = 0, $urlChannelId = null)
- {
- if (is_null(self::$instance)) {
- self::$instance = new static($urlType, $urlChannelId);
- }
- return self::$instance;
- }
- /**
- * 魔术方法获取(只支持单级属性获取,多级属性请使用getUserInfo)
- *
- * @param $name
- * @return mixed|null
- */
- public function __get($name)
- {
- if (is_null($this->userInfo)) {
- $this->getUserInfo();
- }
- return $this->userInfo[$name] ?? null;
- }
- /**
- * 获取visitor,先获取cookie,若无,自动生成一个
- *
- * @param bool $force 是否强制生成一个新的
- */
- public function visitor($force = false)
- {
- if (!$force && Cookie::has('visitor')) {
- return Cookie::get('visitor');
- } else {
- return date('YmdHis') . Random::uuid();
- }
- }
- }
|