123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- /**
- * Created by PhpStorm.
- * User: lytian
- * Date: 2019/4/13
- * Time: 11:44
- */
- namespace app\common\controller;
- use app\common\constants\ErrorCodeConstants;
- use app\common\library\Redis;
- use app\common\service\LogService;
- use app\main\constants\ClientApiConstants;
- use app\main\service\WebUserService;
- use think\Config;
- use think\Cookie;
- use think\Env;
- use think\exception\HttpResponseException;
- use think\Log;
- use think\Request;
- use think\Response;
- class ClientWebApi
- {
- /**
- * @var int 当前请求时间戳
- */
- protected $time = null;
- /**
- * @var Request Request 实例
- */
- protected $request;
- protected $aCommon;
- protected $ajaxParams;
- protected $params = null;
- protected $debug = '';
- 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->_initialize();
- }
- protected function _initialize()
- {
- // Cookie::set('web_ust', ['uid' => 60007, 'token' => 'oiYYI1l0kANcDG6Ti8B7Tjr45xbU'], 3600 * 24 * 30);
- $this->debug = Config::get('client.app_debug');
- $this->time = $this->request->server('REQUEST_TIME');
- $commonParam = $this->request->header('common');
- $ajaxParams = $this->request->header('AjaxParams');
- $this->ajaxParams = json_decode($ajaxParams, true);
- $this->aCommon = json_decode($commonParam, true);
- $this->params = $this->request->param();
- $this->checkLogin();
- $this->redis = Redis::instance();
- }
- /**
- * 检测登陆
- */
- private function checkLogin()
- {
- 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页内登录失败:缺少参数');
- }
- }
- }
- /**
- * header签名校验
- */
- public function checkSign()
- {
- if ($this->debug) {
- LogService::info('debug模式,跳过签名校验');
- return true;
- }
- $originalSign = $this->aCommon['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;
- }
- /**
- * 操作成功返回的数据
- * @param string $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param string $type 输出类型
- * @param array $header 发送的 Header 信息
- */
- protected function success($msg = '', $data = '', $type = 'json', array $header = [])
- {
- $this->result($data, 1, $msg, $type, $header);
- }
- /**
- * 操作失败返回的数据
- * @param string $msg 提示信息
- * @param mixed $data 要返回的数据
- * @param string $type 输出类型
- * @param array $header 发送的 Header 信息
- */
- protected function error($msg = '', $data = '', $type = 'json', array $header = [])
- {
- $this->result($data, 0, $msg, $type, $header);
- }
- /**
- * 返回封装后的 API 数据到客户端
- * @access protected
- * @param mixed $data 要返回的数据
- * @param int $code 返回的 code
- * @param mixed $msg 提示信息
- * @param string $type 返回数据格式
- * @param array $header 发送的 Header 信息
- * @return void
- * @throws HttpResponseException
- */
- protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
- {
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'time' => Request::instance()->server('REQUEST_TIME'),
- 'data' => $data,
- ];
- $type = $type ?: $this->getResponseType();
- $response = Response::create($result, $type)->header($header);
- throw new HttpResponseException($response);
- }
- /**
- * 未找到请求的接口
- */
- public function _empty()
- {
- $this->error('Api not found');
- }
- /**
- * 获取当前的 response 输出类型
- * @access protected
- * @return string
- */
- protected function getResponseType()
- {
- return Request::instance()->isAjax()
- ? Config::get('default_ajax_return')
- : Config::get('default_return_type');
- }
- }
|