BaseAuth.php 9.4 KB

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