BaseAuth.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. // * 获取各平台密钥
  54. // * @param $companyCode
  55. // * @return mixed
  56. // */
  57. // protected function getKey2($companyCode){
  58. //
  59. // $AllKey = [
  60. // 'SHANGQIAO'=>[
  61. // 'baiDu_face_key'=>'2tSp3z72pzNcmTUm3bnavt08',
  62. // 'baiDu_face_secret'=>'qdOQUjSrIOsqVw0imiGkguLSnR8SOlgT',
  63. // 'Easemob_org_name'=>'1189180524177178',
  64. // 'Easemob_app_name'=>'shangqiao-vowkin-app',
  65. // 'Easemob_client_id'=>'YXA6b-bhsGDWEeiTZfWNdzdw7g',
  66. // 'Easemob_client_secret'=>'YXA6MVUX7r6EybJWKFkmmGZSrcpfDrs',
  67. // ],
  68. // 'CHENSEN'=>[
  69. // 'MoniFormAuth'=>1,
  70. // 'PinYin'=>1
  71. // ],
  72. // 'SHYL'=>[
  73. // 'WECHAT_APPID'=>'wx7b0f9e7a14655716',
  74. // 'WECHAT_APPSECRET'=>'02c2d41dbd558bd78ea0f0c960531860',
  75. // 'Wechat_pay_appId'=>'wxca48f8e7ad253dfc',
  76. // 'Wechat_pay_appsecret'=>'25f8a69f35dfb31c2bc4d5ab4784a2d6',
  77. // 'Wechat_pay_key'=>'1A7f7e7fbc939d3c7d25be2012e41022',
  78. // 'Wechat_pay_mchId'=>'1510800741',
  79. // ],
  80. // 'BAIXIONG'=>[
  81. // 'WECHAT_APPID'=>'wx08a4db6a54f73c6f',
  82. // 'WECHAT_APPSECRET'=>'8e3a6165ce46e22ea2bb278e0092e71f',
  83. // 'Wechat_pay_appId'=>'wxca48f8e7ad253dfc',
  84. // 'Wechat_pay_appsecret'=>'25f8a69f35dfb31c2bc4d5ab4784a2d6',
  85. // 'Wechat_pay_key'=>'1A7f7e7fbc939d3c7d25be2012e41022',
  86. // 'Wechat_pay_mchId'=>'1510800741',
  87. // ],
  88. // 'BOXLUNCH'=>[
  89. // 'WECHAT_APPID'=>'wx7b0f9e7a14655716',
  90. // 'WECHAT_APPSECRET'=>'02c2d41dbd558bd78ea0f0c960531860',
  91. // ]
  92. // ];
  93. //
  94. // if(!isset($AllKey[$companyCode])){
  95. // HelperService::returnJson(['code'=>400,'msg'=>"this company_code error($companyCode)",'data'=>[]]);
  96. // }
  97. //
  98. // return $AllKey[$companyCode];
  99. // }
  100. //校验基础参数
  101. private function _valiBaseParams(){
  102. $rule = [
  103. 'api_code|api调用方'=>'require|max:100',//新字段名
  104. 'request_ts|请求时间'=>'require|number',
  105. 'signKey|签名'=>'require|max:100' //1.0版本传参,2.0接口传signKey
  106. ];
  107. $validate = new Validate($rule);
  108. if(!$validate->check($this->_sysParams)){
  109. $data = $this->_debug?$this->_sysParams:[];
  110. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>$data]);
  111. }
  112. }
  113. //白名单方法的过滤
  114. private function _filterActionWhiteList(){
  115. $action = $this->request->action();
  116. if(in_array($action, $this->_whiteList)){
  117. $this->_inWhiteList = true;
  118. return true;
  119. }
  120. return false;
  121. }
  122. //验证请求时间戳
  123. private function _valiRequireTs(){
  124. $requireTs = $this->_sysParams['request_ts']?:0;
  125. if(time() - $requireTs > 1800){
  126. HelperService::returnJson(['code'=>400,'msg'=>'签名错误(1)','data'=>[]]);
  127. }
  128. }
  129. //是否开启debug模式
  130. private function _openDebug(){
  131. if(isset($this->_sysParams['debug'])
  132. && $this->_sysParams['debug']=='xiepeng123@'){
  133. $this->_debug = true;
  134. }
  135. }
  136. /**
  137. * 获取当前url中是否包含某个字符串
  138. *
  139. * @param type $string
  140. * @return boolean
  141. */
  142. protected function getUrlContent($string){
  143. $queryString = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
  144. if(strpos($queryString,$string)!==false){
  145. return true;
  146. }
  147. }
  148. //获取input参数
  149. protected function getInput(){
  150. $this->_sysParams = $this->request->param();//系统参数
  151. $this->_oldParams = file_get_contents("php://input");//原始业务参数
  152. try{
  153. $this->_params = json_decode($this->_oldParams,true);//解码的业务参数【xml/表单解析不出来】
  154. }catch(\Exception $ex){
  155. $this->_params = $this->_oldParams;
  156. }
  157. $this->_setGlobalStaticParams();
  158. }
  159. //设置全局的静态变量,为了后续日志
  160. private function _setGlobalStaticParams(){
  161. HelperService::$_startExecTime = microtime(true);//请求时间
  162. HelperService::$_serviceParams = $this->_params;//业务参数
  163. HelperService::$_sysParams = $this->_sysParams;//系统参数
  164. }
  165. //验证sha1加密
  166. private function _getSignKey($apiCode,$requestTs,$signKeySalt){
  167. $md5Sign = md5(base64_encode($apiCode.$requestTs));
  168. $sha1Sign = strtoupper(sha1($md5Sign.$signKeySalt));
  169. if($sha1Sign == strtoupper($this->_sysParams['signKey'])){
  170. return true;
  171. }
  172. $data = [];
  173. if($this->_debug){
  174. $data = [
  175. 'signKey'=>$sha1Sign,
  176. 'salt'=>$signKeySalt
  177. ];
  178. }
  179. HelperService::returnJson(['code'=>400,'msg'=>"签名错误(3)",'data'=>$data]);
  180. }
  181. /**
  182. * 获取当前公司的配置信息
  183. * @param type $apiCode
  184. * @return array
  185. */
  186. private function _getCompanyAuth($apiCode){
  187. $filePath = WEB_ROOT . "./COMPANY_LIST/$apiCode/auth.php";
  188. if (!file_exists($filePath)) {
  189. HelperService::returnJson(['code' => 400, 'msg' => "this apiCode error($apiCode)", 'data' => []]);
  190. }
  191. return require_once("{$filePath}");
  192. }
  193. /**
  194. * 获取各平台密钥
  195. * @param $apiCode
  196. * @param $isVer 是否验证参数
  197. * @return mixed
  198. */
  199. protected function getKey($apiCode='CHENSEN',$isVer=true){
  200. $companyAuth = $this->_getCompanyAuth($apiCode);
  201. if(empty($companyAuth)){
  202. HelperService::returnJson(['code'=>400,'msg'=>"this apiAuth is empty",'data'=>[]]);
  203. }
  204. if(!isset($companyAuth['signKey'])){
  205. HelperService::returnJson(['code'=>400,'msg'=>"签名错误(2)",'data'=>[]]);
  206. }
  207. //需要验证的情况下
  208. if($isVer){
  209. $this->_getSignKey($apiCode, $this->_sysParams['request_ts'], $companyAuth['signKey']);
  210. }
  211. return $companyAuth;
  212. }
  213. /**
  214. * 连接远程的redis
  215. */
  216. protected function connectionRedis($select=0){
  217. $this->_redisClient = new \Redis();
  218. $this->_redisClient->connect('47.97.187.118', 6379);
  219. $this->_redisClient->auth('gudong-hz');
  220. $this->_redisClient->select($select);
  221. }
  222. /**
  223. * 创建mysql链接
  224. * @param type $tableName
  225. * @return type
  226. */
  227. protected function connectionMysql($tableName,$dbConfig='monitor'){
  228. $table = (string)$tableName;
  229. return Db::connect($dbConfig)->table($table);
  230. }
  231. /**
  232. * 创建product database mssql pdo连接
  233. * @return PDO
  234. */
  235. protected function singleProductDbConnect(){
  236. //当连接已经实例化,就不再实例化了
  237. if(!empty($this->_mssqlProductConnect)){
  238. return $this->_mssqlProductConnect;
  239. }
  240. $productDbConfig = Config::get('productDb');
  241. // Open connection
  242. $this->_mssqlProductConnect = Db::connect($productDbConfig);
  243. // Check for successful connection
  244. if ( $this->_mssqlProductConnect ) {
  245. return $this->_mssqlProductConnect;
  246. } else {
  247. die("PDO MSSQL 链接失败");
  248. }
  249. }
  250. /**
  251. * 专门针对于微信请求
  252. */
  253. protected function getFileContext(){
  254. $currentUrl = $_SERVER['REQUEST_URI'];
  255. $fileName = '';
  256. //说明是txt文件
  257. if(strpos($currentUrl, '.txt')>0){
  258. $arr = parse_url($currentUrl);
  259. $arr2 = pathinfo($arr['path']);
  260. $fileName = isset($arr2['basename'])?$arr2['basename']:"";
  261. $fileName?die(file_get_contents($fileName)):"";
  262. }
  263. return true;
  264. }
  265. }