ClientWebApi.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: lytian
  5. * Date: 2019/4/13
  6. * Time: 11:44
  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\constants\ClientApiConstants;
  13. use app\main\service\WebUserService;
  14. use think\Config;
  15. use think\Cookie;
  16. use think\Env;
  17. use think\exception\HttpResponseException;
  18. use think\Log;
  19. use think\Request;
  20. use think\Response;
  21. class ClientWebApi
  22. {
  23. /**
  24. * @var int 当前请求时间戳
  25. */
  26. protected $time = null;
  27. /**
  28. * @var Request Request 实例
  29. */
  30. protected $request;
  31. protected $aCommon;
  32. protected $ajaxParams;
  33. protected $params = null;
  34. protected $debug = '';
  35. protected $isLogin = false;
  36. //用户基本信息
  37. protected $userInfo = null;
  38. //用户ID
  39. protected $userid = null;
  40. //用户性别 默认为1 男性
  41. protected $sex = 1;
  42. /**
  43. * @var Redis
  44. */
  45. protected $redis = null;
  46. /**
  47. * 构造方法
  48. * @access public
  49. * @param Request $request Request 对象
  50. */
  51. public function __construct(Request $request = null)
  52. {
  53. $this->request = is_null($request) ? Request::instance() : $request;
  54. $this->_initialize();
  55. }
  56. protected function _initialize()
  57. {
  58. // Cookie::set('web_ust', ['uid' => 60007, 'token' => 'oiYYI1l0kANcDG6Ti8B7Tjr45xbU'], 3600 * 24 * 30);
  59. $this->debug = Config::get('client.app_debug');
  60. $this->time = $this->request->server('REQUEST_TIME');
  61. $commonParam = $this->request->header('common');
  62. $ajaxParams = $this->request->header('AjaxParams');
  63. $this->ajaxParams = json_decode($ajaxParams, true);
  64. $this->aCommon = json_decode($commonParam, true);
  65. $this->params = $this->request->param();
  66. $this->checkLogin();
  67. $this->redis = Redis::instance();
  68. }
  69. /**
  70. * 检测登陆
  71. */
  72. private function checkLogin()
  73. {
  74. if (!$this->isLogin) {
  75. //判断是否有cookie
  76. if (Cookie::has('user_id') && Cookie::has('token')) {
  77. $userId = Cookie::get('user_id');
  78. $token = Cookie::get('token');
  79. $userInfoResult = WebUserService::instance()->setUserInfo($userId, $token);
  80. if ($userInfoResult->code == ErrorCodeConstants::SUCCESS) {
  81. $this->userInfo = WebUserService::instance()->getUserInfo()->toArray();
  82. $this->isLogin = true;
  83. $this->userid = $this->userInfo['id'];
  84. $this->sex = $this->userInfo['sex'] ? $this->userInfo['sex'] : 1; //未知默认男
  85. } else {
  86. Log::info('h5页内登录失败:'.$userInfoResult->msg);
  87. }
  88. } else {
  89. Log::info('h5页内登录失败:缺少参数');
  90. }
  91. }
  92. }
  93. /**
  94. * header签名校验
  95. */
  96. public function checkSign()
  97. {
  98. if ($this->debug) {
  99. LogService::info('debug模式,跳过签名校验');
  100. return true;
  101. }
  102. $originalSign = $this->aCommon['sign'] ?? '';
  103. if (empty($originalSign)) {
  104. LogService::error('签名错误, 缺少sign参数');
  105. return false;
  106. }
  107. $arrSign = [];
  108. ksort($this->aCommon);
  109. foreach ($this->aCommon as $k => $param) {
  110. $strTmp = trim($k) . '=' . trim($param);
  111. $arrSign[] = $strTmp;
  112. }
  113. $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1';
  114. $strSign = implode('&', $arrSign);
  115. $sign = md5($strSign);
  116. if ($originalSign != $sign) {
  117. LogService::error('签名错误,验证失败');
  118. return false;
  119. }
  120. return true;
  121. }
  122. /**
  123. * 操作成功返回的数据
  124. * @param string $msg 提示信息
  125. * @param mixed $data 要返回的数据
  126. * @param string $type 输出类型
  127. * @param array $header 发送的 Header 信息
  128. */
  129. protected function success($msg = '', $data = '', $type = 'json', array $header = [])
  130. {
  131. $this->result($data, 1, $msg, $type, $header);
  132. }
  133. /**
  134. * 操作失败返回的数据
  135. * @param string $msg 提示信息
  136. * @param mixed $data 要返回的数据
  137. * @param string $type 输出类型
  138. * @param array $header 发送的 Header 信息
  139. */
  140. protected function error($msg = '', $data = '', $type = 'json', array $header = [])
  141. {
  142. $this->result($data, 0, $msg, $type, $header);
  143. }
  144. /**
  145. * 返回封装后的 API 数据到客户端
  146. * @access protected
  147. * @param mixed $data 要返回的数据
  148. * @param int $code 返回的 code
  149. * @param mixed $msg 提示信息
  150. * @param string $type 返回数据格式
  151. * @param array $header 发送的 Header 信息
  152. * @return void
  153. * @throws HttpResponseException
  154. */
  155. protected function result($data, $code = 0, $msg = '', $type = '', array $header = [])
  156. {
  157. $result = [
  158. 'code' => $code,
  159. 'msg' => $msg,
  160. 'time' => Request::instance()->server('REQUEST_TIME'),
  161. 'data' => $data,
  162. ];
  163. $type = $type ?: $this->getResponseType();
  164. $response = Response::create($result, $type)->header($header);
  165. throw new HttpResponseException($response);
  166. }
  167. /**
  168. * 未找到请求的接口
  169. */
  170. public function _empty()
  171. {
  172. $this->error('Api not found');
  173. }
  174. /**
  175. * 获取当前的 response 输出类型
  176. * @access protected
  177. * @return string
  178. */
  179. protected function getResponseType()
  180. {
  181. return Request::instance()->isAjax()
  182. ? Config::get('default_ajax_return')
  183. : Config::get('default_return_type');
  184. }
  185. }