ZskkDefaultApiController.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Request;
  4. /**
  5. * 后台控制器基类
  6. * 接口方法权限 必传参数 接口返回 错误抛出 通用参数处理
  7. */
  8. class ZskkDefaultApiController extends ZskkDefaultController {
  9. protected $needToken = false;
  10. protected $logName = "ZskkDefaultApiController";
  11. protected function afterSetRequest() {
  12. $this->setParams(Request::param(false));
  13. }
  14. function curlPost($url, $data) {
  15. //初使化init方法
  16. $ch = curl_init();
  17. //指定URL
  18. curl_setopt($ch, CURLOPT_URL, $url);
  19. //设定请求后返回结果
  20. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  21. //声明使用POST方式来进行发送
  22. curl_setopt($ch, CURLOPT_POST, 1);
  23. //发送什么数据呢
  24. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  25. //忽略证书
  26. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  27. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  28. //忽略header头信息
  29. curl_setopt($ch, CURLOPT_HEADER, 0);
  30. //设置超时时间
  31. // curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  32. //发送请求
  33. $output = curl_exec($ch);
  34. //关闭curl
  35. curl_close($ch);
  36. //返回数据
  37. return $output;
  38. }
  39. public function getDecryptData($data)
  40. {
  41. $key = 'zLxapoeqWYpoeqWY';
  42. $info = $this->decrypt($key,$data);
  43. if(empty($info))
  44. {
  45. return '数据解析失败';
  46. }else{
  47. $arr = json_decode(base64_decode($info),true);
  48. if(empty($arr))
  49. {
  50. return '数据解析失败,失败原因:数据加密前未进行base64转换';
  51. }
  52. return $arr;
  53. }
  54. }
  55. public static function encrypt($key, $str)
  56. {
  57. if($str) {
  58. $iv = str_repeat("\0", openssl_cipher_iv_length('SM4-CBC'));
  59. $ciphertext = openssl_encrypt($str, 'SM4-CBC', $key, OPENSSL_RAW_DATA, $iv);
  60. return base64_encode($ciphertext);
  61. }
  62. return $str;
  63. }
  64. /*
  65. * 国密4解密
  66. * str 解密字符串
  67. */
  68. public static function decrypt($key, $str)
  69. {
  70. if($str) {
  71. $iv = str_repeat("\0", openssl_cipher_iv_length('SM4-CBC'));
  72. return openssl_decrypt(base64_decode($str), 'SM4-CBC', $key, OPENSSL_RAW_DATA, $iv);
  73. }
  74. return $str;
  75. }
  76. }