functions.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Here is your custom functions.
  4. */
  5. if (!function_exists('http_post')) {
  6. function http_post($url, $params)
  7. {
  8. $ch = curl_init();
  9. $this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
  10. curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
  11. curl_setopt($ch, CURLOPT_URL, $url);
  12. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  13. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  14. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  15. curl_setopt($ch, CURLOPT_POST, 1);
  16. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  17. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
  18. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  19. $output = curl_exec($ch);
  20. curl_close($ch);
  21. return $output;
  22. }
  23. }
  24. if (!function_exists('http_post_json')) {
  25. function http_post_json($url, $params)
  26. {
  27. try {
  28. $ch = curl_init();
  29. $this_header = array('Content-Type: application/json;charset=UTF-8');
  30. curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
  31. curl_setopt($ch, CURLOPT_URL, $url);
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  33. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  34. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  35. curl_setopt($ch, CURLOPT_POST, 1);
  36. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
  37. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
  38. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  39. $output = curl_exec($ch);
  40. curl_close($ch);
  41. // 写日志
  42. if (is_json($output)) {
  43. \support\Log::info('模拟请求:', json_decode($output, true));
  44. } else {
  45. \support\Log::info('模拟请求,返回异常:', ['msg' => (string)$output]);
  46. }
  47. return $output;
  48. } catch (\Exception $e) {
  49. \support\Log::info('模拟请求失败:', ['msg' => $e->getMessage()]);
  50. }
  51. }
  52. }
  53. if (!function_exists('month_12')) {
  54. function month_12()
  55. {
  56. $months = [];
  57. for ($i = 0; $i < 12; $i++) {
  58. $months[$i] = date('Y/m', strtotime(date('Y-m-01') . "-" . $i . "month"));
  59. }
  60. return $months;
  61. }
  62. }
  63. /**
  64. * @Desc 管理员操作日志
  65. * @Author Gorden
  66. * @Date 2024/3/29 17:07
  67. *
  68. * adminId = 1001 计划任务
  69. *
  70. *
  71. * @param $name
  72. * @param $operation
  73. * @return void
  74. */
  75. if (!function_exists('_syslog')) {
  76. function _syslog($name, $operation, $operationData = false, $requestParams = false, $adminId = false)
  77. {
  78. try {
  79. $adminId = $adminId ? $adminId : \Tinywan\Jwt\JwtToken::getCurrentId();
  80. } catch (\Exception $e) {
  81. $adminId = 1001;
  82. }
  83. $logAdminId = $adminId;
  84. $model = new \app\model\SysLog();
  85. $model->log_admin_id = $logAdminId;
  86. $model->log_name = $name;
  87. $model->log_route = \request() && \request()->uri() ? \request()->uri() : (\request() && \request()->route && \request()->route->getPath() ? \request()->route->getPath() : '');
  88. $model->log_operation = $operation;
  89. $model->log_ip = \request() && \request()->getRealIp() ? \request()->getRealIp() : '0.0.0.0';
  90. $model->log_request_params = $requestParams ? json_encode($requestParams) : ($adminId == 1001 ? '[]' : json_encode(\request()->all()));
  91. $model->log_operation_data = $operationData ? json_encode($operationData) : null;
  92. $model->save();
  93. }
  94. }
  95. if (!function_exists('json_throw')) {
  96. function json_throw($code, $message, $data = '', $options = JSON_UNESCAPED_UNICODE)
  97. {
  98. $return = [
  99. 'code' => $code,
  100. 'message' => $message,
  101. 'data' => $data,
  102. ];
  103. return new \support\Response(200, ['Content-Type' => 'application/json'], json_encode($return, $options));
  104. }
  105. }