123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330 |
- <?php
- namespace app\common\controller;
- use app\common\library\Ua;
- use app\common\library\User;
- use app\main\constants\ErrorCodeConstants;
- use app\main\constants\OpenPlatformConstants;
- use app\main\constants\UrlConstants;
- use app\main\helper\ArrayHelper;
- use app\main\service\LogService;
- use app\main\service\OpenPlatformService;
- use app\main\service\UrlService;
- use app\main\service\UserService;
- use think\Config;
- use think\Cookie;
- use think\exception\HttpResponseException;
- use think\exception\ValidateException;
- use think\Lang;
- use think\Loader;
- use think\Log;
- use think\Request;
- use think\Response;
- /**
- * API控制器基类
- */
- class Api
- {
- const PAY_CALLBACK_PREFIX = 'callback';
- // const PALM_PAY_URL = 'https://pay.palmpay.cn/sdkServer/thirdpays/pay/WECHAT_SUB';
- //
- // const JOIN_PAY_URL = 'https://www.joinpay.com/trade/uniPayApi.action';
- //
- // const MIHUA_PAY_URL = 'http://paytest.mihuajinfu.com/paygateway/mbpay/order/v1';
- //
- // const JOIN_PAY_VERSION = '1.0';
- //
- // const PALM_PAY_VERSION = '3.0';
- //
- // const JOIN_PAY_TYPE = 'WEIXIN_GZH';
- /**
- * @var Request Request 实例
- */
- protected $request;
- /**
- * @var bool 验证失败是否抛出异常
- */
- protected $failException = false;
- /**
- * @var bool 是否批量验证
- */
- protected $batchValidate = false;
- /**
- * 微信内需登录的方法
- * @var array
- */
- protected $wxNeedLogin = [];
- /**
- * 浏览器需要强登录方法
- * @var array
- */
- protected $browserNeedLogin = [];
- /**
- * 前台用户系统
- * @var UserService
- */
- protected $user = null;
- /**
- * 当前url访问类型 0未识别 1后台首页 2支付页 3推广页
- * @var int
- */
- protected $urlType = 0;
- /**
- * 当前访问链接隶属于哪个渠道商/代理商
- * @var null
- */
- protected $urlAdminId = null;
- protected $urlChannelId = null;
- protected $urlAgentId = null;
- protected $sourceDomain = null;
- /**
- * @var int 当前请求时间戳
- */
- protected $time = null;
- /**
- * 支付域名全局变量
- */
- protected $currentPayHost = null;
- /**
- * 构造方法
- * @access public
- * @param Request $request Request 对象
- */
- public function __construct(Request $request = null)
- {
- LogService::info("REQUEST_TIME_START:".microtime(true));
- $this->request = is_null($request) ? Request::instance() : $request;
- if (!in_array(UrlService::instance()->getActionUrl(), UrlConstants::$ignoreCallbackInitialize)) {
- // 控制器初始化
- $this->_initialize();
- }
- }
- /**
- * 初始化操作
- * @access protected
- */
- protected function _initialize()
- {
- $this->time = $this->request->server('REQUEST_TIME');
- UserService::instance()->getRunTimeObject()->requestTime = $this->time;
- $result = UrlService::instance()->checkApiDomain();
- if($result->code != ErrorCodeConstants::SUCCESS){
- $this->error($result->msg);
- }
- $runtime = UserService::instance()->getRunTimeObject();
- $this->currentPayHost = $runtime->currentPayHost;
- $this->urlType = $runtime->urlType;
- $this->urlAdminId = $runtime->adminId;
- $this->urlAgentId = $runtime->agentId;
- $this->urlChannelId = $runtime->channelId;
- $this->sourceDomain = $runtime->sourceDomain;
- /**
- * 用户权限判断
- */
- $this->user = UserService::instance();
- if (!OpenPlatformService::instance()->getOpenPlatform()) {
- $this->error('平台配置有误');
- }
- OpenPlatformService::instance()->setSourceDomain($this->sourceDomain);
- if (Ua::isWeiXin()) { // 微信内
- //检测是否需要验证登录 && 检测是否登录
- if (UrlService::instance()->checkActionMatch(OpenPlatformConstants::WEXIN_NEED_LOGIN) && !$this->user->isLogin()) {
- $this->error('未登录');
- }
- } else { // 其他(浏览器)
- if (UrlService::instance()->checkActionMatch(OpenPlatformConstants::BROWSER_NEED_LOGIN) && !$this->user->isLogin()) {
- $this->error('未登录');
- }
- }
- }
- /**
- * 加载语言文件
- * @param string $name
- */
- protected function loadlang($name)
- {
- Lang::load(APP_PATH . $this->request->module() . '/lang/' . Lang::detect() . '/' . str_replace('.', '/', $name) . '.php');
- }
- /**
- * 操作成功返回的数据
- * @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()
- {
- return $this->error('Api not found');
- }
- /**
- * 前置操作
- * @access protected
- * @param string $method 前置操作方法名
- * @param array $options 调用参数 ['only'=>[...]] 或者 ['except'=>[...]]
- * @return void
- */
- protected function beforeAction($method, $options = [])
- {
- if (isset($options['only']))
- {
- if (is_string($options['only']))
- {
- $options['only'] = explode(',', $options['only']);
- }
- if (!in_array($this->request->action(), $options['only']))
- {
- return;
- }
- }
- elseif (isset($options['except']))
- {
- if (is_string($options['except']))
- {
- $options['except'] = explode(',', $options['except']);
- }
- if (in_array($this->request->action(), $options['except']))
- {
- return;
- }
- }
- call_user_func([$this, $method]);
- }
- /**
- * 设置验证失败后是否抛出异常
- * @access protected
- * @param bool $fail 是否抛出异常
- * @return $this
- */
- protected function validateFailException($fail = true)
- {
- $this->failException = $fail;
- return $this;
- }
- /**
- * 验证数据
- * @access protected
- * @param array $data 数据
- * @param string|array $validate 验证器名或者验证规则数组
- * @param array $message 提示信息
- * @param bool $batch 是否批量验证
- * @param mixed $callback 回调方法(闭包)
- * @return array|string|true
- * @throws ValidateException
- */
- protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
- {
- if (is_array($validate))
- {
- $v = Loader::validate();
- $v->rule($validate);
- }
- else
- {
- // 支持场景
- if (strpos($validate, '.'))
- {
- list($validate, $scene) = explode('.', $validate);
- }
- $v = Loader::validate($validate);
- !empty($scene) && $v->scene($scene);
- }
- // 批量验证
- if ($batch || $this->batchValidate)
- $v->batch(true);
- // 设置错误信息
- if (is_array($message))
- $v->message($message);
- // 使用回调验证
- if ($callback && is_callable($callback))
- {
- call_user_func_array($callback, [$v, &$data]);
- }
- if (!$v->check($data))
- {
- if ($this->failException)
- {
- throw new ValidateException($v->getError());
- }
- return $v->getError();
- }
- return true;
- }
- }
|