123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- /**
- * Created by PhpStorm.
- * User: lts
- * Date: 2019-04-01
- * Time: 11:48
- */
- namespace app\common\controller;
- use app\main\constants\ClientApiConstants;
- use app\main\constants\ErrorCodeConstants;
- use app\main\service\ClientAppService;
- use app\main\service\FinancialService;
- use app\main\service\LogService;
- use app\main\service\WebUserService;
- use think\Request;
- use think\Response;
- use think\exception\HttpResponseException;
- use think\Config;
- class ClientApi
- {
- /**
- * @var int 当前请求时间戳
- */
- protected $time = null;
- /**
- * @var Request Request 实例
- */
- protected $request;
- protected $aCommon;
- protected $userInfo = null;
- protected $params = null;
- protected $debug = '';
- /**
- * 构造方法
- * @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()
- {
- $this->debug = Config::get('client.app_debug');
- $strParams = $this->request->param('appParams');
- $this->params = json_decode($strParams, true);
- $commonParam = $this->request->header('common');
- $this->aCommon = json_decode($commonParam, true);
- $this->_validateParams();
- $this->_sign();
- if ($this->aCommon['ust'] == ClientApiConstants::USER_TYPE_LOGIN) {
- if (empty($this->aCommon['uid'])) {
- $this->error("公共参数uid为空");
- }
- if (empty($this->aCommon['token'])) {
- $this->error("公共参数token为空");
- }
- $userInfoResult = WebUserService::instance()->setUserInfo($this->aCommon['uid'], $this->aCommon['token']);
- if ($userInfoResult->code == ErrorCodeConstants::SUCCESS) {
- $this->userInfo = WebUserService::instance()->getUserInfo()->toArray();
- } else {
- $this->error($userInfoResult->msg);
- }
- }
- }
- /**
- * 公共参数校验
- */
- private function _validateParams()
- {
- foreach (ClientApiConstants::$generalParams as $generalParam) {
- if (!isset($this->aCommon[$generalParam])) {
- $this->error("公共参数{$generalParam}不存在");
- }
- if (strlen(trim($this->aCommon[$generalParam])) == 0) {
- $this->error("公共参数{$generalParam}为空");
- }
- }
- }
- /**
- * api签名校验
- */
- private function _sign()
- {
- if ($this->debug) {
- LogService::info('debug模式,跳过签名校验');
- return;
- }
- $originalSign = Request::instance()->header('sign');
- if (empty($originalSign)) {
- $this->error('签名错误,1');
- }
- $arrSign = [];
- $orgParams = [
- 'code' => $this->request->param('code'),
- 'common' => $this->request->header('common'),
- ];
- $orgParams = array_merge($orgParams, $this->params);
- ksort($orgParams);
- foreach ($orgParams as $k => $param) {
- $strTmp = trim($k) . '=' . trim($param);
- $arrSign[] = $strTmp;
- }
- $arrSign[] = 'key=ddbc9169242b479da867eb24efb735d1';
- $strSign = implode('&', $arrSign);
- $sign = md5($strSign);
- if ($originalSign != $sign) {
- $this->error('签名错误,2');
- }
- }
- /**
- * 操作成功返回的数据
- * @param string $msg 提示信息
- * @param array $data
- * @param array $header
- */
- protected function success($msg = '', $data = [], array $header = [])
- {
- $this->result($data, ClientApiConstants::CLIENT_API_CODE_SUCCESS, $msg, $header);
- }
- /**
- * 操作成功返回对象数据
- * @param array $data
- */
- protected function info($data = [])
- {
- $this->result($data, ClientApiConstants::CLIENT_API_CODE_SUCCESS);
- }
- /**
- * 操作失败返回的数据
- * @param string $msg 提示信息
- * @param int $code 错误代码
- * @param mixed $data 要返回的数据
- * @param array $header 发送的 Header 信息
- */
- protected function error(
- $msg = '',
- $code = ClientApiConstants::CLIENT_API_CODE_FAIL,
- $data = [],
- array $header = []
- ) {
- if (empty($msg)) {
- if (isset(ClientApiConstants::$errMsgList[$code])) {
- $msg = ClientApiConstants::$errMsgList[$code];
- }
- }
- $this->result($data, $code, $msg, $header);
- }
- /**
- * 返回封装后的 API 数据到客户端
- * @access protected
- * @param mixed $data 要返回的数据
- * @param int $code 返回的 code
- * @param mixed $msg 提示信息
- * @param array $header 发送的 Header 信息
- * @return void
- * @throws HttpResponseException
- */
- protected function result($data, $code = 0, $msg = '', array $header = [])
- {
- $result = [
- 'code' => $code,
- 'msg' => $msg,
- 'time' => Request::instance()->server('REQUEST_TIME'),
- 'data' => camelizeArr($data),
- ];
- if (empty($data)) {
- $result['data'] = new \ArrayObject(array());
- }
- $response = Response::create($result, 'json')->header($header);
- throw new HttpResponseException($response);
- }
- /**
- * 检查参数必填
- * @param string|array $paramName 参数名
- * @param bool $checkEmpty 是否检查空值
- */
- protected function checkParamRequired($paramName, $checkEmpty = true)
- {
- if (is_array($paramName)) {
- foreach ($paramName as $item) {
- $this->_checkParamRequired($item, $checkEmpty);
- }
- } else {
- $this->_checkParamRequired($paramName, $checkEmpty);
- }
- }
- /**
- * 校验接口输入参数必填
- * @param $paramName 参数名
- * @param $checkEmpty 参数值是否使用php的empty函数校验
- */
- private function _checkParamRequired($paramName, $checkEmpty)
- {
- $params = $this->params;
- if ($checkEmpty) {
- if (empty($params[$paramName])) {
- $this->error("参数{$paramName}不能为空");
- }
- } else {
- if (!isset($params[$paramName])) {
- $this->error("参数{$paramName}不存在");
- }
- }
- }
- /**
- * 校验用户登录
- */
- protected function checkUserLogin()
- {
- if (empty($this->userInfo)) {
- $this->error('需要登录', ClientApiConstants::CLIENT_API_CODE_NEED_LOGIN);
- }
- }
- /**
- * 格式
- * @return array
- */
- protected function userInfoFormat()
- {
- $this->checkUserLogin();
- $userInfo = $this->userInfo;
- $userId = $userInfo['id'];
- $userResult = [
- 'name' => $userInfo['nickname'],
- 'uid' => $userId,
- 'token' => $userInfo['openid'],
- 'avatar' => $userInfo['avatar'],
- ];
- $freeKandian = FinancialService::instance()->getTotalFreeKandian($userId)->data;
- $kandian = FinancialService::instance()->getTotalKandian($userId)->data;
- $signResult = ClientAppService::instance()->getTodaySign($userId);
- $userResult['isVip'] = $userInfo['vip_endtime'] > time();
- $userResult['isMarked'] = $signResult->data;
- $userResult['balance'] = $freeKandian + $kandian;
- $userResult['ch'] = $userInfo['channel_id'];
- $userResult['ag'] = $userInfo['agent_id'];
- return $userResult;
- }
- public function getParams($key)
- {
- if (array_key_exists($key, $this->params)) {
- return $this->params[$key];
- }
- return '';
- }
- }
|