BaseAuth.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace app\expand\controller;
  3. use app\common\service\HelperService;
  4. use PDO;
  5. use think\Config;
  6. use think\Controller;
  7. use think\Db;
  8. use think\Validate;
  9. /**
  10. * 基础授权类
  11. * Class BaseAuth
  12. * @package app\expand\controller
  13. */
  14. class BaseAuth extends Controller
  15. {
  16. public $_params = null;
  17. public $_apiCode = null;
  18. public $_sysParams = null;
  19. public $_redisClient = null;
  20. public $_oldParams = null;
  21. public $_mssqlProductConnect = null;
  22. //白名单方法列表
  23. public $_whiteList = [
  24. 'notifyJdPay',//jd
  25. 'notifyXcxPay',//xcx
  26. 'paySuccess',//wx
  27. 'getOpenId2Pay',//wx
  28. 'platformNotifyUrl',//wx
  29. ];
  30. public $_inWhiteList = false;//是否在白名单里面
  31. public $_debug = false;//是否是debug模式
  32. /**
  33. * 有2种情况
  34. * 1、有sign的情况,这时候他没有signKey
  35. * 2、有signKey的情况
  36. * 都需要兼容
  37. * @return boolean
  38. */
  39. public function __construct()
  40. {
  41. parent::__construct();
  42. $this->getInput();//获取系统/原始业务参数/解码后的业务参数
  43. $this->_openDebug();//debug模式是否打开
  44. //白名单方法的过滤
  45. if(true == $this->_filterActionWhiteList()){
  46. return true;
  47. }
  48. $this->_valiBaseParams();//要过基本参数校验
  49. $this->_valiRequireTs();//验证时间是否正确
  50. $this->_apiCode = $this->_sysParams['api_code'];
  51. }
  52. //校验基础参数
  53. private function _valiBaseParams(){
  54. $rule = [
  55. 'api_code|api调用方'=>'require|max:100',//新字段名
  56. 'request_ts|请求时间'=>'require|number',
  57. 'signKey|签名'=>'require|max:100' //1.0版本传参,2.0接口传signKey
  58. ];
  59. $validate = new Validate($rule);
  60. if(!$validate->check($this->_sysParams)){
  61. $data = $this->_debug?$this->_sysParams:[];
  62. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>$data]);
  63. }
  64. }
  65. //白名单方法的过滤
  66. private function _filterActionWhiteList(){
  67. $action = $this->request->action();
  68. if(in_array($action, $this->_whiteList)){
  69. $this->_inWhiteList = true;
  70. return true;
  71. }
  72. return false;
  73. }
  74. //验证请求时间戳
  75. private function _valiRequireTs(){
  76. $requireTs = $this->_sysParams['request_ts']?:0;
  77. if(time() - $requireTs > 1800){
  78. HelperService::returnJson(['code'=>400,'msg'=>'签名错误(1)','data'=>[]]);
  79. }
  80. }
  81. //是否开启debug模式
  82. private function _openDebug(){
  83. if(isset($this->_sysParams['debug'])
  84. && $this->_sysParams['debug']=='xiepeng123@'){
  85. $this->_debug = true;
  86. }
  87. }
  88. /**
  89. * 获取当前url中是否包含某个字符串
  90. *
  91. * @param type $string
  92. * @return boolean
  93. */
  94. protected function getUrlContent($string){
  95. $queryString = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  96. if(strpos($queryString,$string)!==false){
  97. return true;
  98. }
  99. }
  100. //获取input参数
  101. protected function getInput(){
  102. $this->_sysParams = $this->request->param();//系统参数
  103. $this->_oldParams = file_get_contents("php://input");//原始业务参数
  104. try{
  105. $this->_params = json_decode($this->_oldParams,true);//解码的业务参数【xml/表单解析不出来】
  106. }catch(\Exception $ex){
  107. $this->_params = $this->_oldParams;
  108. }
  109. $this->_setGlobalStaticParams();
  110. }
  111. //设置全局的静态变量,为了后续日志
  112. private function _setGlobalStaticParams(){
  113. HelperService::$_startExecTime = microtime(true);//请求时间
  114. HelperService::$_serviceParams = $this->_params;//业务参数
  115. HelperService::$_sysParams = $this->_sysParams;//系统参数
  116. }
  117. //验证sha1加密
  118. private function _getSignKey($apiCode,$requestTs,$signKeySalt){
  119. $md5Sign = md5(base64_encode($apiCode.$requestTs));
  120. $sha1Sign = strtoupper(sha1($md5Sign.$signKeySalt));
  121. if($sha1Sign == strtoupper($this->_sysParams['signKey'])){
  122. return true;
  123. }
  124. $data = [];
  125. if($this->_debug){
  126. $data = [
  127. 'signKey'=>$sha1Sign,
  128. 'salt'=>$signKeySalt
  129. ];
  130. }
  131. HelperService::returnJson(['code'=>400,'msg'=>"签名错误(3)",'data'=>$data]);
  132. }
  133. /**
  134. * 获取当前公司的配置信息
  135. * @param type $apiCode
  136. * @return array
  137. */
  138. private function _getCompanyAuth($apiCode){
  139. $filePath = "./COMPANY_LIST/$apiCode/auth.php";
  140. if(!file_exists($filePath)){
  141. HelperService::returnJson(['code'=>400,'msg'=>"this apiCode error($apiCode)",'data'=>[]]);
  142. }
  143. return require_once("{$filePath}");
  144. }
  145. /**
  146. * 获取各平台密钥
  147. * @param $apiCode
  148. * @param $isVer 是否验证参数
  149. * @return mixed
  150. */
  151. protected function getKey($apiCode='CHENSEN',$isVer=true){
  152. $companyAuth = $this->_getCompanyAuth($apiCode);
  153. if(empty($companyAuth)){
  154. HelperService::returnJson(['code'=>400,'msg'=>"this apiAuth is empty",'data'=>[]]);
  155. }
  156. if(!isset($companyAuth['signKey'])){
  157. HelperService::returnJson(['code'=>400,'msg'=>"签名错误(2)",'data'=>[]]);
  158. }
  159. //需要验证的情况下
  160. if($isVer){
  161. $this->_getSignKey($apiCode, $this->_sysParams['request_ts'], $companyAuth['signKey']);
  162. }
  163. return $companyAuth;
  164. }
  165. /**
  166. * 连接远程的redis
  167. */
  168. protected function connectionRedis($select=0){
  169. $this->_redisClient = new \Redis();
  170. $this->_redisClient->connect('47.97.187.118', 6379);
  171. $this->_redisClient->auth('gudong-hz');
  172. $this->_redisClient->select($select);
  173. }
  174. /**
  175. * 创建mysql链接
  176. * @param type $tableName
  177. * @return type
  178. */
  179. protected function connectionMysql($tableName,$dbConfig='monitor'){
  180. $table = (string)$tableName;
  181. return Db::connect($dbConfig)->table($table);
  182. }
  183. /**
  184. * 创建product database mssql pdo连接
  185. * @return PDO
  186. */
  187. protected function singleProductDbConnect(){
  188. //当连接已经实例化,就不再实例化了
  189. if(!empty($this->_mssqlProductConnect)){
  190. return $this->_mssqlProductConnect;
  191. }
  192. $productDbConfig = Config::get('productDb');
  193. // Open connection
  194. $this->_mssqlProductConnect = Db::connect($productDbConfig);
  195. // Check for successful connection
  196. if ( $this->_mssqlProductConnect ) {
  197. return $this->_mssqlProductConnect;
  198. } else {
  199. die("PDO MSSQL 链接失败");
  200. }
  201. }
  202. /**
  203. * 专门针对于微信请求
  204. */
  205. protected function getFileContext(){
  206. $currentUrl = $_SERVER['REQUEST_URI'];
  207. $fileName = '';
  208. //说明是txt文件
  209. if(strpos($currentUrl, '.txt')>0){
  210. $arr = parse_url($currentUrl);
  211. $arr2 = pathinfo($arr['path']);
  212. $fileName = isset($arr2['basename'])?$arr2['basename']:"";
  213. $fileName?die(file_get_contents($fileName)):"";
  214. }
  215. return true;
  216. }
  217. }