123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?php
- /**
- * Created by PhpStorm.
- * User: lts
- * Date: 2019-04-01
- * Time: 11:48
- */
- namespace app\common\controller;
- use app\common\constants\ErrorCodeConstants;
- use app\common\library\Redis;
- use app\common\service\LogService;
- use app\main\service\WebUserService;
- use think\Controller;
- use think\Cookie;
- use think\Hook;
- use think\Log;
- use think\Request;
- use think\Env;
- use think\Config;
- class ClientWeb extends Controller
- {
- /**
- * @var int 当前请求时间戳
- */
- protected $time = null;
- /**
- * @var Request Request 实例
- */
- protected $request;
- //客户端传递的公参
- protected $aCommon = [];
- //客户端传过来的私参
- protected $pCommon = [];
- //url传过来的参数
- protected $urlParams = null;
- //全参数
- protected $params = null;
- //是否开启debug
- protected $debug = false;
- //是否登录
- protected $isLogin = false;
- //用户基本信息
- protected $userInfo = null;
- //用户ID
- protected $userid = null;
- //用户性别 默认为1 男性
- protected $sex = 1;
- /**
- * @var Redis
- */
- protected $redis = null;
- /**
- * 构造方法
- * @access public
- * @param Request $request Request 对象
- */
- public function __construct(Request $request = null)
- {
- $this->request = is_null($request) ? Request::instance() : $request;
- $this->redis = Redis::instance();
- // 控制器初始化
- parent::__construct($this->request);
- }
- protected function _initialize()
- {
- // Cookie::set('user_id', '60007');
- // Cookie::set('token', 'oiYYI1l0kANcDG6Ti8B7Tjr45xbU');
- $this->debug = Config::get('client.app_debug');
- $this->time = $this->request->server('REQUEST_TIME');
- $commonParam = $this->request->header('common');
- if ($commonParam) {
- $this->aCommon = json_decode($commonParam, true);
- }
- $this->_sign();
- $this->params = $allParams = $this->request->param();
- if (isset($allParamss['params'])) {
- $this->pCommon = json_decode($allParams['params'], true);
- unset($allParams['params']);
- }
- $this->urlParams = $allParams;
- LogService::info("公参:". json_encode($this->aCommon, 256));
- LogService::info("私参:". json_encode($this->pCommon, 256));
- LogService::info("url参数:".json_encode($this->urlParams, 256));
- // $this->aCommon['uid'] = '20000034';
- // $this->aCommon['token'] = 'oKWvT037RMlmaBm-pQ8nY2o3G3vw';
- //登录
- if (!$this->isLogin)
- $this->h5Login();
- /**
- * 配置项处理
- */
- $site = Config::get("site");
- $this->assign('log_host', Config::get('site.loghost')); //设置打点域名
- $moduleName = Request::instance()->module();
- $controllerName = strtolower(Request::instance()->controller());
- $actionName = strtolower(Request::instance()->action());
- // 配置信息
- $config = [
- 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
- 'modulename' => $moduleName,
- 'controllername' => $controllerName,
- 'actionname' => $actionName,
- 'jsname' => 'frontend/' . str_replace('.', '/', $controllerName),
- 'moduleurl' => rtrim(url("/{$moduleName}", '', false), '/'),
- ];
- // 配置信息后
- Hook::listen("config_init", $config);
- $this->assign('site', $site);
- $this->assign('config', $config);
- //域名
- $this->view->assign('app_domain', $this->request->domain());
- $this->view->assign('is_login', $this->isLogin);
- }
- /**
- * 验证签名
- * @return bool
- */
- private function _sign()
- {
- return true;
- //return $this->checkSign() || $this->checkCookie();
- }
- /**
- * header签名校验
- */
- private function checkSign()
- {
- if ($this->debug) {
- LogService::info('debug模式,跳过签名校验');
- return true;
- }
- $originalSign = Request::instance()->header('sign');
- if (empty($originalSign)) {
- LogService::error('客户端签名错误, 缺少sign参数');
- return false;
- }
- $arrSign = [];
- ksort($this->aCommon);
- foreach ($this->aCommon as $k => $param) {
- $strTmp = trim($k) . '=' . trim($param);
- $arrSign[] = $strTmp;
- }
- $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1';
- $strSign = implode('&', $arrSign);
- $sign = md5($strSign);
- if ($originalSign != $sign) {
- LogService::error('客户端签名错误,验证失败');
- return false;
- }
- return true;
- }
- /**
- * cookie签名验证
- * 使用token简单的参与签名
- */
- private function checkCookie()
- {
- if ($this->debug) {
- LogService::info('debug模式,跳过签名校验');
- return true;
- }
- if (Cookie::has('h5Sign') && Cookie::has('token')) {
- $h5Sign = Cookie::get('h5Sign');
- $arrSign['token'] = Cookie::get('token');
- $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1';
- $strSign = implode('&', $arrSign);
- if ($h5Sign != md5($strSign)) {
- LogService::error('h5签名错误,验证失败');
- return false;
- }
- return true;
- }
- LogService::error('h5签名错误,缺少参数');
- return false;
- }
- /**
- * h5登陆
- */
- private function h5Login()
- {
- //是否是客户端直接拉起
- if (!empty($this->aCommon)) {
- $uid = $this->aCommon['uid'] ?? 0;
- $token = $this->aCommon['token'] ?? '';
- if (!!$uid && !!$token) {
- $userInfoResult = WebUserService::instance()->setUserInfo($uid, $token);
- if ($userInfoResult->code == ErrorCodeConstants::SUCCESS) {
- $this->userInfo = WebUserService::instance()->getUserInfo()->toArray();
- $this->isLogin = true;
- $this->userid = $this->userInfo['id'];
- $this->sex = $this->userInfo['sex'] ? $this->userInfo['sex'] : 1; //未知默认男
- //设置cookie
- $arrSign['token'] = $token;
- $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1';
- $strSign = implode('&', $arrSign);
- Cookie::set('user_id', $uid, 3600 * 24 * 30);
- Cookie::set('token', $token, 3600 * 24 * 30);
- Cookie::set('h5Sign', md5($strSign), 3600 * 24 * 30);
- Cookie::set('channel_id', $this->userInfo['channel_id'], 3600 * 24 * 30);
- Cookie::set('agent_id', $this->userInfo['agent_id'], 3600 * 24 * 30);
- } else {
- Log::info('h5客户端登录失败:'.$userInfoResult->msg);
- }
- } else {
- Log::info('h5客户端登录失败:缺少参数');
- }
- }
- if (!$this->isLogin) {
- //判断是否有cookie
- if (Cookie::has('user_id') && Cookie::has('token')) {
- $userId = Cookie::get('user_id');
- $token = Cookie::get('token');
- $userInfoResult = WebUserService::instance()->setUserInfo($userId, $token);
- if ($userInfoResult->code == ErrorCodeConstants::SUCCESS) {
- $this->userInfo = WebUserService::instance()->getUserInfo()->toArray();
- $this->isLogin = true;
- $this->userid = $this->userInfo['id'];
- $this->sex = $this->userInfo['sex'] ? $this->userInfo['sex'] : 1; //未知默认男
- } else {
- Log::info('h5页内登录失败:'.$userInfoResult->msg);
- }
- } else {
- Log::info('h5页内登录失败:缺少参数');
- }
- }
- }
- }
|