ClientApi.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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\main\constants\ClientApiConstants;
  10. use app\main\constants\ErrorCodeConstants;
  11. use app\main\service\ClientAppService;
  12. use app\main\service\FinancialService;
  13. use app\main\service\LogService;
  14. use app\main\service\WebUserService;
  15. use think\Request;
  16. use think\Response;
  17. use think\exception\HttpResponseException;
  18. use think\Config;
  19. class ClientApi
  20. {
  21. /**
  22. * @var int 当前请求时间戳
  23. */
  24. protected $time = null;
  25. /**
  26. * @var Request Request 实例
  27. */
  28. protected $request;
  29. protected $aCommon;
  30. protected $userInfo = null;
  31. protected $params = null;
  32. protected $debug = '';
  33. /**
  34. * 构造方法
  35. * @access public
  36. * @param Request $request Request 对象
  37. */
  38. public function __construct(Request $request = null)
  39. {
  40. $this->request = is_null($request) ? Request::instance() : $request;
  41. // 控制器初始化
  42. $this->_initialize();
  43. }
  44. protected function _initialize()
  45. {
  46. $this->debug = Config::get('client.app_debug');
  47. $strParams = $this->request->param('appParams');
  48. $this->params = json_decode($strParams, true);
  49. $commonParam = $this->request->header('common');
  50. $this->aCommon = json_decode($commonParam, true);
  51. $this->_validateParams();
  52. $this->_sign();
  53. if ($this->aCommon['ust'] == ClientApiConstants::USER_TYPE_LOGIN) {
  54. if (empty($this->aCommon['uid'])) {
  55. $this->error("公共参数uid为空");
  56. }
  57. if (empty($this->aCommon['token'])) {
  58. $this->error("公共参数token为空");
  59. }
  60. $userInfoResult = WebUserService::instance()->setUserInfo($this->aCommon['uid'], $this->aCommon['token']);
  61. if ($userInfoResult->code == ErrorCodeConstants::SUCCESS) {
  62. $this->userInfo = WebUserService::instance()->getUserInfo()->toArray();
  63. } else {
  64. $this->error($userInfoResult->msg);
  65. }
  66. }
  67. }
  68. /**
  69. * 公共参数校验
  70. */
  71. private function _validateParams()
  72. {
  73. foreach (ClientApiConstants::$generalParams as $generalParam) {
  74. if (!isset($this->aCommon[$generalParam])) {
  75. $this->error("公共参数{$generalParam}不存在");
  76. }
  77. if (strlen(trim($this->aCommon[$generalParam])) == 0) {
  78. $this->error("公共参数{$generalParam}为空");
  79. }
  80. }
  81. }
  82. /**
  83. * api签名校验
  84. */
  85. private function _sign()
  86. {
  87. if ($this->debug) {
  88. LogService::info('debug模式,跳过签名校验');
  89. return;
  90. }
  91. $originalSign = Request::instance()->header('sign');
  92. if (empty($originalSign)) {
  93. $this->error('签名错误,1');
  94. }
  95. $arrSign = [];
  96. $orgParams = [
  97. 'code' => $this->request->param('code'),
  98. 'common' => $this->request->header('common'),
  99. ];
  100. $orgParams = array_merge($orgParams, $this->params);
  101. ksort($orgParams);
  102. foreach ($orgParams as $k => $param) {
  103. $strTmp = trim($k) . '=' . trim($param);
  104. $arrSign[] = $strTmp;
  105. }
  106. $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1';
  107. $strSign = implode('&', $arrSign);
  108. $sign = md5($strSign);
  109. if ($originalSign != $sign) {
  110. $this->error('签名错误,2');
  111. }
  112. }
  113. /**
  114. * 操作成功返回的数据
  115. * @param string $msg 提示信息
  116. * @param array $data
  117. * @param array $header
  118. */
  119. protected function success($msg = '', $data = [], array $header = [])
  120. {
  121. $this->result($data, ClientApiConstants::CLIENT_API_CODE_SUCCESS, $msg, $header);
  122. }
  123. /**
  124. * 操作成功返回对象数据
  125. * @param array $data
  126. */
  127. protected function info($data = [])
  128. {
  129. $this->result($data, ClientApiConstants::CLIENT_API_CODE_SUCCESS);
  130. }
  131. /**
  132. * 操作失败返回的数据
  133. * @param string $msg 提示信息
  134. * @param int $code 错误代码
  135. * @param mixed $data 要返回的数据
  136. * @param array $header 发送的 Header 信息
  137. */
  138. protected function error(
  139. $msg = '',
  140. $code = ClientApiConstants::CLIENT_API_CODE_FAIL,
  141. $data = [],
  142. array $header = []
  143. ) {
  144. if (empty($msg)) {
  145. if (isset(ClientApiConstants::$errMsgList[$code])) {
  146. $msg = ClientApiConstants::$errMsgList[$code];
  147. }
  148. }
  149. $this->result($data, $code, $msg, $header);
  150. }
  151. /**
  152. * 返回封装后的 API 数据到客户端
  153. * @access protected
  154. * @param mixed $data 要返回的数据
  155. * @param int $code 返回的 code
  156. * @param mixed $msg 提示信息
  157. * @param array $header 发送的 Header 信息
  158. * @return void
  159. * @throws HttpResponseException
  160. */
  161. protected function result($data, $code = 0, $msg = '', array $header = [])
  162. {
  163. $result = [
  164. 'code' => $code,
  165. 'msg' => $msg,
  166. 'time' => Request::instance()->server('REQUEST_TIME'),
  167. 'data' => camelizeArr($data),
  168. ];
  169. if (empty($data)) {
  170. $result['data'] = new \ArrayObject(array());
  171. }
  172. $response = Response::create($result, 'json')->header($header);
  173. throw new HttpResponseException($response);
  174. }
  175. /**
  176. * 检查参数必填
  177. * @param string|array $paramName 参数名
  178. * @param bool $checkEmpty 是否检查空值
  179. */
  180. protected function checkParamRequired($paramName, $checkEmpty = true)
  181. {
  182. if (is_array($paramName)) {
  183. foreach ($paramName as $item) {
  184. $this->_checkParamRequired($item, $checkEmpty);
  185. }
  186. } else {
  187. $this->_checkParamRequired($paramName, $checkEmpty);
  188. }
  189. }
  190. /**
  191. * 校验接口输入参数必填
  192. * @param $paramName 参数名
  193. * @param $checkEmpty 参数值是否使用php的empty函数校验
  194. */
  195. private function _checkParamRequired($paramName, $checkEmpty)
  196. {
  197. $params = $this->params;
  198. if ($checkEmpty) {
  199. if (empty($params[$paramName])) {
  200. $this->error("参数{$paramName}不能为空");
  201. }
  202. } else {
  203. if (!isset($params[$paramName])) {
  204. $this->error("参数{$paramName}不存在");
  205. }
  206. }
  207. }
  208. /**
  209. * 校验用户登录
  210. */
  211. protected function checkUserLogin()
  212. {
  213. if (empty($this->userInfo)) {
  214. $this->error('需要登录', ClientApiConstants::CLIENT_API_CODE_NEED_LOGIN);
  215. }
  216. }
  217. /**
  218. * 格式
  219. * @return array
  220. */
  221. protected function userInfoFormat()
  222. {
  223. $this->checkUserLogin();
  224. $userInfo = $this->userInfo;
  225. $userId = $userInfo['id'];
  226. $userResult = [
  227. 'name' => $userInfo['nickname'],
  228. 'uid' => $userId,
  229. 'token' => $userInfo['openid'],
  230. 'avatar' => $userInfo['avatar'],
  231. ];
  232. $freeKandian = FinancialService::instance()->getTotalFreeKandian($userId)->data;
  233. $kandian = FinancialService::instance()->getTotalKandian($userId)->data;
  234. $signResult = ClientAppService::instance()->getTodaySign($userId);
  235. $userResult['isVip'] = $userInfo['vip_endtime'] > time();
  236. $userResult['isMarked'] = $signResult->data;
  237. $userResult['balance'] = $freeKandian + $kandian;
  238. $userResult['ch'] = $userInfo['channel_id'];
  239. $userResult['ag'] = $userInfo['agent_id'];
  240. return $userResult;
  241. }
  242. public function getParams($key)
  243. {
  244. if (array_key_exists($key, $this->params)) {
  245. return $this->params[$key];
  246. }
  247. return '';
  248. }
  249. }