ClientWeb.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lts
  5. * Date: 2019-04-01
  6. * Time: 11:48
  7. */
  8. namespace app\common\controller;
  9. use app\common\constants\ErrorCodeConstants;
  10. use app\common\library\Redis;
  11. use app\common\service\LogService;
  12. use app\main\service\WebUserService;
  13. use think\Controller;
  14. use think\Cookie;
  15. use think\Hook;
  16. use think\Log;
  17. use think\Request;
  18. use think\Env;
  19. use think\Config;
  20. class ClientWeb extends Controller
  21. {
  22. /**
  23. * @var int 当前请求时间戳
  24. */
  25. protected $time = null;
  26. /**
  27. * @var Request Request 实例
  28. */
  29. protected $request;
  30. //客户端传递的公参
  31. protected $aCommon = [];
  32. //客户端传过来的私参
  33. protected $pCommon = [];
  34. //url传过来的参数
  35. protected $urlParams = null;
  36. //全参数
  37. protected $params = null;
  38. //是否开启debug
  39. protected $debug = false;
  40. //是否登录
  41. protected $isLogin = false;
  42. //用户基本信息
  43. protected $userInfo = null;
  44. //用户ID
  45. protected $userid = null;
  46. //用户性别 默认为1 男性
  47. protected $sex = 1;
  48. /**
  49. * @var Redis
  50. */
  51. protected $redis = null;
  52. /**
  53. * 构造方法
  54. * @access public
  55. * @param Request $request Request 对象
  56. */
  57. public function __construct(Request $request = null)
  58. {
  59. $this->request = is_null($request) ? Request::instance() : $request;
  60. $this->redis = Redis::instance();
  61. // 控制器初始化
  62. parent::__construct($this->request);
  63. }
  64. protected function _initialize()
  65. {
  66. // Cookie::set('user_id', '60007');
  67. // Cookie::set('token', 'oiYYI1l0kANcDG6Ti8B7Tjr45xbU');
  68. $this->debug = Config::get('client.app_debug');
  69. $this->time = $this->request->server('REQUEST_TIME');
  70. $commonParam = $this->request->header('common');
  71. if ($commonParam) {
  72. $this->aCommon = json_decode($commonParam, true);
  73. }
  74. $this->_sign();
  75. $this->params = $allParams = $this->request->param();
  76. if (isset($allParamss['params'])) {
  77. $this->pCommon = json_decode($allParams['params'], true);
  78. unset($allParams['params']);
  79. }
  80. $this->urlParams = $allParams;
  81. LogService::info("公参:". json_encode($this->aCommon, 256));
  82. LogService::info("私参:". json_encode($this->pCommon, 256));
  83. LogService::info("url参数:".json_encode($this->urlParams, 256));
  84. // $this->aCommon['uid'] = '20000034';
  85. // $this->aCommon['token'] = 'oKWvT037RMlmaBm-pQ8nY2o3G3vw';
  86. //登录
  87. if (!$this->isLogin)
  88. $this->h5Login();
  89. /**
  90. * 配置项处理
  91. */
  92. $site = Config::get("site");
  93. $this->assign('log_host', Config::get('site.loghost')); //设置打点域名
  94. $moduleName = Request::instance()->module();
  95. $controllerName = strtolower(Request::instance()->controller());
  96. $actionName = strtolower(Request::instance()->action());
  97. // 配置信息
  98. $config = [
  99. 'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
  100. 'modulename' => $moduleName,
  101. 'controllername' => $controllerName,
  102. 'actionname' => $actionName,
  103. 'jsname' => 'frontend/' . str_replace('.', '/', $controllerName),
  104. 'moduleurl' => rtrim(url("/{$moduleName}", '', false), '/'),
  105. ];
  106. // 配置信息后
  107. Hook::listen("config_init", $config);
  108. $this->assign('site', $site);
  109. $this->assign('config', $config);
  110. //域名
  111. $this->view->assign('app_domain', $this->request->domain());
  112. $this->view->assign('is_login', $this->isLogin);
  113. }
  114. /**
  115. * 验证签名
  116. * @return bool
  117. */
  118. private function _sign()
  119. {
  120. return true;
  121. //return $this->checkSign() || $this->checkCookie();
  122. }
  123. /**
  124. * header签名校验
  125. */
  126. private function checkSign()
  127. {
  128. if ($this->debug) {
  129. LogService::info('debug模式,跳过签名校验');
  130. return true;
  131. }
  132. $originalSign = Request::instance()->header('sign');
  133. if (empty($originalSign)) {
  134. LogService::error('客户端签名错误, 缺少sign参数');
  135. return false;
  136. }
  137. $arrSign = [];
  138. ksort($this->aCommon);
  139. foreach ($this->aCommon as $k => $param) {
  140. $strTmp = trim($k) . '=' . trim($param);
  141. $arrSign[] = $strTmp;
  142. }
  143. $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1';
  144. $strSign = implode('&', $arrSign);
  145. $sign = md5($strSign);
  146. if ($originalSign != $sign) {
  147. LogService::error('客户端签名错误,验证失败');
  148. return false;
  149. }
  150. return true;
  151. }
  152. /**
  153. * cookie签名验证
  154. * 使用token简单的参与签名
  155. */
  156. private function checkCookie()
  157. {
  158. if ($this->debug) {
  159. LogService::info('debug模式,跳过签名校验');
  160. return true;
  161. }
  162. if (Cookie::has('h5Sign') && Cookie::has('token')) {
  163. $h5Sign = Cookie::get('h5Sign');
  164. $arrSign['token'] = Cookie::get('token');
  165. $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1';
  166. $strSign = implode('&', $arrSign);
  167. if ($h5Sign != md5($strSign)) {
  168. LogService::error('h5签名错误,验证失败');
  169. return false;
  170. }
  171. return true;
  172. }
  173. LogService::error('h5签名错误,缺少参数');
  174. return false;
  175. }
  176. /**
  177. * h5登陆
  178. */
  179. private function h5Login()
  180. {
  181. //是否是客户端直接拉起
  182. if (!empty($this->aCommon)) {
  183. $uid = $this->aCommon['uid'] ?? 0;
  184. $token = $this->aCommon['token'] ?? '';
  185. if (!!$uid && !!$token) {
  186. $userInfoResult = WebUserService::instance()->setUserInfo($uid, $token);
  187. if ($userInfoResult->code == ErrorCodeConstants::SUCCESS) {
  188. $this->userInfo = WebUserService::instance()->getUserInfo()->toArray();
  189. $this->isLogin = true;
  190. $this->userid = $this->userInfo['id'];
  191. $this->sex = $this->userInfo['sex'] ? $this->userInfo['sex'] : 1; //未知默认男
  192. //设置cookie
  193. $arrSign['token'] = $token;
  194. $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1';
  195. $strSign = implode('&', $arrSign);
  196. Cookie::set('user_id', $uid, 3600 * 24 * 30);
  197. Cookie::set('token', $token, 3600 * 24 * 30);
  198. Cookie::set('h5Sign', md5($strSign), 3600 * 24 * 30);
  199. Cookie::set('channel_id', $this->userInfo['channel_id'], 3600 * 24 * 30);
  200. Cookie::set('agent_id', $this->userInfo['agent_id'], 3600 * 24 * 30);
  201. } else {
  202. Log::info('h5客户端登录失败:'.$userInfoResult->msg);
  203. }
  204. } else {
  205. Log::info('h5客户端登录失败:缺少参数');
  206. }
  207. }
  208. if (!$this->isLogin) {
  209. //判断是否有cookie
  210. if (Cookie::has('user_id') && Cookie::has('token')) {
  211. $userId = Cookie::get('user_id');
  212. $token = Cookie::get('token');
  213. $userInfoResult = WebUserService::instance()->setUserInfo($userId, $token);
  214. if ($userInfoResult->code == ErrorCodeConstants::SUCCESS) {
  215. $this->userInfo = WebUserService::instance()->getUserInfo()->toArray();
  216. $this->isLogin = true;
  217. $this->userid = $this->userInfo['id'];
  218. $this->sex = $this->userInfo['sex'] ? $this->userInfo['sex'] : 1; //未知默认男
  219. } else {
  220. Log::info('h5页内登录失败:'.$userInfoResult->msg);
  221. }
  222. } else {
  223. Log::info('h5页内登录失败:缺少参数');
  224. }
  225. }
  226. }
  227. }