functions.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * Here is your custom functions.
  4. */
  5. use support\Response;
  6. if (!function_exists('http_post')) {
  7. function http_post($url, $params)
  8. {
  9. $ch = curl_init();
  10. $this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
  11. curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
  12. curl_setopt($ch, CURLOPT_URL, $url);
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  14. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  15. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  16. curl_setopt($ch, CURLOPT_POST, 1);
  17. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  18. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
  19. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  20. $output = curl_exec($ch);
  21. curl_close($ch);
  22. return $output;
  23. }
  24. }
  25. if (!function_exists('http_post_json')) {
  26. function http_post_json($url, $params)
  27. {
  28. try {
  29. $ch = curl_init();
  30. $this_header = array('Content-Type: application/json;charset=UTF-8');
  31. curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
  32. curl_setopt($ch, CURLOPT_URL, $url);
  33. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  34. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  35. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  36. curl_setopt($ch, CURLOPT_POST, 1);
  37. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
  38. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
  39. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  40. $output = curl_exec($ch);
  41. curl_close($ch);
  42. // 写日志
  43. if (is_json($output)) {
  44. \support\Log::info('模拟请求:', json_decode($output, true));
  45. } else {
  46. \support\Log::info('模拟请求,返回异常:', ['msg' => (string)$output]);
  47. }
  48. return $output;
  49. } catch (\Exception $e) {
  50. \support\Log::info('模拟请求失败:', ['msg' => $e->getMessage()]);
  51. }
  52. }
  53. }
  54. if (!function_exists('month_12')) {
  55. function month_12()
  56. {
  57. $months = [];
  58. for ($i = 0; $i < 12; $i++) {
  59. $months[$i] = date('Y/m', strtotime(date('Y-m-01') . "-" . $i . "month"));
  60. }
  61. return $months;
  62. }
  63. }
  64. /**
  65. * @Desc 管理员操作日志
  66. * @Author Gorden
  67. * @Date 2024/3/29 17:07
  68. *
  69. * adminId = 1001 计划任务
  70. *
  71. *
  72. * @param $name
  73. * @param $operation
  74. * @return void
  75. */
  76. if (!function_exists('_syslog')) {
  77. function _syslog($name, $operation, $operationData = false, $requestParams = false, $adminId = false)
  78. {
  79. try {
  80. $adminId = $adminId ? $adminId : \Tinywan\Jwt\JwtToken::getCurrentId();
  81. } catch (\Exception $e) {
  82. $adminId = 1001;
  83. }
  84. $logAdminId = $adminId;
  85. $model = new \app\model\SysLog();
  86. $model->log_admin_id = $logAdminId;
  87. $model->log_name = $name;
  88. $model->log_route = \request() && \request()->uri() ? \request()->uri() : (\request() && \request()->route && \request()->route->getPath() ? \request()->route->getPath() : '');
  89. $model->log_operation = $operation;
  90. $model->log_ip = \request() && \request()->getRealIp() ? \request()->getRealIp() : '0.0.0.0';
  91. $model->log_request_params = $requestParams ? json_encode($requestParams) : ($adminId == 1001 ? '[]' : json_encode(\request()->all()));
  92. $model->log_operation_data = $operationData ? json_encode($operationData) : null;
  93. $model->save();
  94. }
  95. }
  96. if (!function_exists('json_throw')) {
  97. function json_throw($code, $message, $data = '', $options = JSON_UNESCAPED_UNICODE)
  98. {
  99. $return = [
  100. 'code' => $code,
  101. 'message' => $message,
  102. 'data' => $data,
  103. ];
  104. return new \support\Response(200, ['Content-Type' => 'application/json'], json_encode($return, $options));
  105. }
  106. }
  107. if (!function_exists('json_success')) {
  108. function json_success($message, $data = '', $options = JSON_UNESCAPED_UNICODE)
  109. {
  110. $return = [
  111. 'code' => 200,
  112. 'message' => $message,
  113. 'data' => $data,
  114. ];
  115. return new Response(200, ['Content-Type' => 'application/json'], json_encode($return, $options));
  116. }
  117. }
  118. if (!function_exists('json_fail')) {
  119. function json_fail($message, $options = JSON_UNESCAPED_UNICODE)
  120. {
  121. $return = [
  122. 'code' => 0,
  123. 'message' => $message,
  124. 'data' => ''
  125. ];
  126. return new Response(200, ['Content-Type' => 'application/json'], json_encode($return, $options));
  127. }
  128. }
  129. if (!function_exists('format_string')) {
  130. function format_string($string)
  131. {
  132. return htmlspecialchars(strip_tags($string));
  133. }
  134. }
  135. if (!function_exists('random_string')) {
  136. function random_string($length, $type = 'all')
  137. {
  138. $string = 'abcdefghijklmnopqrstuvwxyz';
  139. $stringUp = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  140. $number = '0123456789';
  141. switch ($type) {
  142. case 'all':
  143. $result = $string . $number;
  144. break;
  145. case 'string':
  146. $result = $string;
  147. break;
  148. case 'up':
  149. $result = $stringUp . $number;
  150. break;
  151. case 'number':
  152. $result = $number;
  153. break;
  154. default:
  155. $result = '';
  156. break;
  157. }
  158. $return = '';
  159. $totalLength = strlen($result);
  160. for ($i = 0; $i < $length; $i++) {
  161. $return .= $result[mt_rand(0, $totalLength - 1)];
  162. }
  163. return $return;
  164. }
  165. }
  166. if (!function_exists('is_json')) {
  167. /**
  168. * @Desc 验证是否是json数据
  169. * @Author Gorden
  170. * @Date 2024/2/22 15:37
  171. *
  172. * @param $string
  173. * @return bool
  174. */
  175. function is_json($string)
  176. {
  177. if (!is_string($string)) {
  178. return false;
  179. }
  180. json_decode($string);
  181. if (json_last_error() === JSON_ERROR_NONE) {
  182. return true;
  183. }
  184. return false;
  185. }
  186. }
  187. if (!function_exists('chinese_week')) {
  188. /**
  189. * @Desc 周几-汉字
  190. * @Author Gorden
  191. * @Date 2024/3/5 17:29
  192. *
  193. * @param $week
  194. * @return string
  195. */
  196. function chinese_week($week)
  197. {
  198. $weekArray = ['日', '一', '二', '三', '四', '五', '六'];
  199. return '周' . $weekArray[$week];
  200. }
  201. }
  202. if (!function_exists('hexToRgb')) {
  203. /**
  204. * @Desc 颜色值转RGB
  205. * @Author Gorden
  206. * @Date 2024/5/15 17:22
  207. *
  208. * @param $hexColor
  209. * @return mixed
  210. */
  211. function hexToRgb($hexColor, $type = "string")
  212. {
  213. // 使用substr函数去掉前缀的'#'
  214. $hexColor = ltrim($hexColor, '#');
  215. // 使用hexdec函数将十六进制转换为十进制
  216. $red = hexdec(substr($hexColor, 0, 2));
  217. $green = hexdec(substr($hexColor, 2, 2));
  218. $blue = hexdec(substr($hexColor, 4, 2));
  219. if ($type == "string") {
  220. // rgb(46, 209, 153)
  221. return "rgb(" . $red . ", " . $green . ", " . $blue . ")";
  222. }
  223. return array('red' => $red, 'green' => $green, 'blue' => $blue);
  224. }
  225. }
  226. if (!function_exists('rgbToHex')) {
  227. function rgbToHex($rgb)
  228. {
  229. if (substr($rgb, 0, 3) == 'rgb') {
  230. $rgb = str_replace("rgb(", '', $rgb);
  231. $rgb = str_replace(")", "", $rgb);
  232. [$red, $green, $blue] = explode(',', $rgb);
  233. $hexRed = dechex($red);
  234. $hexGreen = dechex($green);
  235. $hexBlue = dechex($blue);
  236. // 如果颜色分量不足两位,前面补零
  237. $hexRed = strlen($hexRed) == 1 ? '0' . $hexRed : $hexRed;
  238. $hexGreen = strlen($hexGreen) == 1 ? '0' . $hexGreen : $hexGreen;
  239. $hexBlue = strlen($hexBlue) == 1 ? '0' . $hexBlue : $hexBlue;
  240. return "#" . $hexRed . $hexGreen . $hexBlue;
  241. }
  242. return "";
  243. }
  244. }