123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- <?php
- namespace app\expand\controller;
- use app\common\service\HelperService;
- use PDO;
- use think\Config;
- use think\Controller;
- use think\Db;
- use think\Validate;
- /**
- * 基础授权类
- * Class BaseAuth
- * @package app\expand\controller
- */
- class BaseAuth extends Controller
- {
- public $_params = null;
- public $_apiCode = null;
- public $_sysParams = null;
- public $_redisClient = null;
- public $_oldParams = null;
- public $_mssqlProductConnect = null;
- //白名单方法列表
- public $_whiteList = [
- 'notifyJdPay',//jd
- 'notifyXcxPay',//xcx
- 'paySuccess',//wx
- 'getOpenId2Pay',//wx
- 'platformNotifyUrl',//wx
- ];
-
- public $_inWhiteList = false;//是否在白名单里面
- public $_debug = false;//是否是debug模式
- /**
- * 有2种情况
- * 1、有sign的情况,这时候他没有signKey
- * 2、有signKey的情况
- * 都需要兼容
- * @return boolean
- */
- public function __construct()
- {
- parent::__construct();
-
- $this->getInput();//获取系统/原始业务参数/解码后的业务参数
- $this->_openDebug();//debug模式是否打开
-
- //白名单方法的过滤
- if(true == $this->_filterActionWhiteList()){
- return true;
- }
- $this->_valiBaseParams();//要过基本参数校验
- $this->_valiRequireTs();//验证时间是否正确
- $this->_apiCode = $this->_sysParams['api_code'];
- }
- // /**
- // * 获取各平台密钥
- // * @param $companyCode
- // * @return mixed
- // */
- // protected function getKey2($companyCode){
- //
- // $AllKey = [
- // 'SHANGQIAO'=>[
- // 'baiDu_face_key'=>'2tSp3z72pzNcmTUm3bnavt08',
- // 'baiDu_face_secret'=>'qdOQUjSrIOsqVw0imiGkguLSnR8SOlgT',
- // 'Easemob_org_name'=>'1189180524177178',
- // 'Easemob_app_name'=>'shangqiao-vowkin-app',
- // 'Easemob_client_id'=>'YXA6b-bhsGDWEeiTZfWNdzdw7g',
- // 'Easemob_client_secret'=>'YXA6MVUX7r6EybJWKFkmmGZSrcpfDrs',
- // ],
- // 'CHENSEN'=>[
- // 'MoniFormAuth'=>1,
- // 'PinYin'=>1
- // ],
- // 'SHYL'=>[
- // 'WECHAT_APPID'=>'wx7b0f9e7a14655716',
- // 'WECHAT_APPSECRET'=>'02c2d41dbd558bd78ea0f0c960531860',
- // 'Wechat_pay_appId'=>'wxca48f8e7ad253dfc',
- // 'Wechat_pay_appsecret'=>'25f8a69f35dfb31c2bc4d5ab4784a2d6',
- // 'Wechat_pay_key'=>'1A7f7e7fbc939d3c7d25be2012e41022',
- // 'Wechat_pay_mchId'=>'1510800741',
- // ],
- // 'BAIXIONG'=>[
- // 'WECHAT_APPID'=>'wx08a4db6a54f73c6f',
- // 'WECHAT_APPSECRET'=>'8e3a6165ce46e22ea2bb278e0092e71f',
- // 'Wechat_pay_appId'=>'wxca48f8e7ad253dfc',
- // 'Wechat_pay_appsecret'=>'25f8a69f35dfb31c2bc4d5ab4784a2d6',
- // 'Wechat_pay_key'=>'1A7f7e7fbc939d3c7d25be2012e41022',
- // 'Wechat_pay_mchId'=>'1510800741',
- // ],
- // 'BOXLUNCH'=>[
- // 'WECHAT_APPID'=>'wx7b0f9e7a14655716',
- // 'WECHAT_APPSECRET'=>'02c2d41dbd558bd78ea0f0c960531860',
- // ]
- // ];
- //
- // if(!isset($AllKey[$companyCode])){
- // HelperService::returnJson(['code'=>400,'msg'=>"this company_code error($companyCode)",'data'=>[]]);
- // }
- //
- // return $AllKey[$companyCode];
- // }
- //校验基础参数
- private function _valiBaseParams(){
-
- $rule = [
- 'api_code|api调用方'=>'require|max:100',//新字段名
- 'request_ts|请求时间'=>'require|number',
- 'signKey|签名'=>'require|max:100' //1.0版本传参,2.0接口传signKey
- ];
- $validate = new Validate($rule);
- if(!$validate->check($this->_sysParams)){
- $data = $this->_debug?$this->_sysParams:[];
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>$data]);
- }
- }
-
- //白名单方法的过滤
- private function _filterActionWhiteList(){
-
- $action = $this->request->action();
- if(in_array($action, $this->_whiteList)){
- $this->_inWhiteList = true;
- return true;
- }
-
- return false;
- }
-
- //验证请求时间戳
- private function _valiRequireTs(){
- $requireTs = $this->_sysParams['request_ts']?:0;
- if(time() - $requireTs > 1800){
- HelperService::returnJson(['code'=>400,'msg'=>'签名错误(1)','data'=>[]]);
- }
-
- }
-
- //是否开启debug模式
- private function _openDebug(){
- if(isset($this->_sysParams['debug'])
- && $this->_sysParams['debug']=='xiepeng123@'){
-
- $this->_debug = true;
- }
- }
- /**
- * 获取当前url中是否包含某个字符串
- *
- * @param type $string
- * @return boolean
- */
- protected function getUrlContent($string){
- $queryString = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
- if(strpos($queryString,$string)!==false){
- return true;
- }
- }
- //获取input参数
- protected function getInput(){
-
- $this->_sysParams = $this->request->param();//系统参数
- $this->_oldParams = file_get_contents("php://input");//原始业务参数
-
- try{
- $this->_params = json_decode($this->_oldParams,true);//解码的业务参数【xml/表单解析不出来】
- }catch(\Exception $ex){
- $this->_params = $this->_oldParams;
- }
-
- $this->_setGlobalStaticParams();
- }
-
- //设置全局的静态变量,为了后续日志
- private function _setGlobalStaticParams(){
-
- HelperService::$_startExecTime = microtime(true);//请求时间
- HelperService::$_serviceParams = $this->_params;//业务参数
- HelperService::$_sysParams = $this->_sysParams;//系统参数
- }
-
- //验证sha1加密
- private function _getSignKey($apiCode,$requestTs,$signKeySalt){
-
- $md5Sign = md5(base64_encode($apiCode.$requestTs));
- $sha1Sign = strtoupper(sha1($md5Sign.$signKeySalt));
-
- if($sha1Sign == strtoupper($this->_sysParams['signKey'])){
- return true;
- }
-
- $data = [];
- if($this->_debug){
- $data = [
- 'signKey'=>$sha1Sign,
- 'salt'=>$signKeySalt
- ];
- }
-
- HelperService::returnJson(['code'=>400,'msg'=>"签名错误(3)",'data'=>$data]);
- }
-
- /**
- * 获取当前公司的配置信息
- * @param type $apiCode
- * @return array
- */
- private function _getCompanyAuth($apiCode){
- $filePath = WEB_ROOT . "./COMPANY_LIST/$apiCode/auth.php";
- if (!file_exists($filePath)) {
- HelperService::returnJson(['code' => 400, 'msg' => "this apiCode error($apiCode)", 'data' => []]);
- }
- return require_once("{$filePath}");
- }
- /**
- * 获取各平台密钥
- * @param $apiCode
- * @param $isVer 是否验证参数
- * @return mixed
- */
- protected function getKey($apiCode='CHENSEN',$isVer=true){
- $companyAuth = $this->_getCompanyAuth($apiCode);
- if(empty($companyAuth)){
- HelperService::returnJson(['code'=>400,'msg'=>"this apiAuth is empty",'data'=>[]]);
- }
-
- if(!isset($companyAuth['signKey'])){
- HelperService::returnJson(['code'=>400,'msg'=>"签名错误(2)",'data'=>[]]);
- }
- //需要验证的情况下
- if($isVer){
- $this->_getSignKey($apiCode, $this->_sysParams['request_ts'], $companyAuth['signKey']);
- }
-
- return $companyAuth;
- }
-
- /**
- * 连接远程的redis
- */
- protected function connectionRedis($select=0){
- $this->_redisClient = new \Redis();
- $this->_redisClient->connect('47.97.187.118', 6379);
- $this->_redisClient->auth('gudong-hz');
- $this->_redisClient->select($select);
- }
-
- /**
- * 创建mysql链接
- * @param type $tableName
- * @return type
- */
- protected function connectionMysql($tableName,$dbConfig='monitor'){
- $table = (string)$tableName;
- return Db::connect($dbConfig)->table($table);
- }
- /**
- * 创建product database mssql pdo连接
- * @return PDO
- */
- protected function singleProductDbConnect(){
-
- //当连接已经实例化,就不再实例化了
- if(!empty($this->_mssqlProductConnect)){
- return $this->_mssqlProductConnect;
- }
-
- $productDbConfig = Config::get('productDb');
- // Open connection
- $this->_mssqlProductConnect = Db::connect($productDbConfig);
- // Check for successful connection
- if ( $this->_mssqlProductConnect ) {
- return $this->_mssqlProductConnect;
- } else {
- die("PDO MSSQL 链接失败");
- }
- }
-
- /**
- * 专门针对于微信请求
- */
- protected function getFileContext(){
- $currentUrl = $_SERVER['REQUEST_URI'];
- $fileName = '';
- //说明是txt文件
- if(strpos($currentUrl, '.txt')>0){
- $arr = parse_url($currentUrl);
- $arr2 = pathinfo($arr['path']);
- $fileName = isset($arr2['basename'])?$arr2['basename']:"";
- $fileName?die(file_get_contents($fileName)):"";
- }
-
- return true;
- }
- }
|