function.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. if (!function_exists('getValidatorError')) {
  12. /**
  13. * 错误验证
  14. * @param $error
  15. * @return mixed
  16. */
  17. function getValidatorError($validator)
  18. {
  19. $errors = $validator->errors()->all();
  20. return isset($errors[0]) ? $errors[0] : 1013;
  21. }
  22. }
  23. /**
  24. * 是否是手机号码
  25. */
  26. if (!function_exists('isPhone')) {
  27. function isPhone($mobile)
  28. {
  29. return preg_match("/^1[3-9][0-9]]{9}/", $mobile);
  30. }
  31. }
  32. if (!function_exists('moneyFormat')) {
  33. /**
  34. * 金钱格式化小数点
  35. * @param $money
  36. * @param int $decimal 小数点位数,默认系统配置2位
  37. * @return float
  38. */
  39. function moneyFormat($money, $decimal = null)
  40. {
  41. $formatConfig = [];
  42. $charset = !empty($formatConfig['charset']) ? trim($formatConfig['charset']) : 'utf-8';
  43. if ($decimal === null) {
  44. $decimal = isset($formatConfig['moneyDecimal']) ? intval($formatConfig['moneyDecimal']) : 2;
  45. }
  46. $money = round($money, $decimal + 1);
  47. $data = explode('.', $money);
  48. $money = isset($data[0]) ? $data[0] : 0;
  49. $float = isset($data[1]) ? $data[1] : '';
  50. $len = $float ? mb_strlen($float, $charset) : 0;
  51. $decimal = $decimal >= 0 ? intval($decimal) : 2;
  52. $num1 = isset($data[1]) ? $data[1] : '';
  53. $float = $len >= $decimal ? mb_substr($num1, 0, $decimal, $charset) : $float . str_repeat('0', $decimal - $len);
  54. $money = $money . '.' . $float;
  55. return number_format($money, $decimal, '.', '');
  56. }
  57. }
  58. if (!function_exists('paramsFormat')) {
  59. /**
  60. * 格式化请求数组参数为&连接字符串
  61. * @param array $data 参数数组
  62. * @param false $filter 是否过滤空值字段
  63. * @return string
  64. */
  65. function paramsFormat(array $data, $filter=false){
  66. $result = [];
  67. ksort($data);
  68. foreach($data as $k => $v){
  69. if($filter && !empty($v) || !$filter){
  70. $result[] = "{$k}={$v}";
  71. }
  72. }
  73. return implode('&', $result);
  74. }
  75. }
  76. if (!function_exists('xmlToArray')) {
  77. /**
  78. * xml转数组
  79. * @param $xml
  80. * @return array
  81. */
  82. function xmlToArray($xml){
  83. if(!$xml){
  84. return [];
  85. }
  86. $result = [];
  87. $xml = (array)simplexml_load_string($xml);
  88. foreach($xml as $k => $v){
  89. if(!is_object($v)){
  90. $result[$k] = $v;
  91. }else{
  92. $result[$k] = xmlToArray((array)$v);
  93. }
  94. }
  95. return $result;
  96. }
  97. }
  98. if (!function_exists('curl_api')) {
  99. /**
  100. * curl请求(POST)
  101. * @param $url 请求地址
  102. * @param array $data 请求参数
  103. * @return bool|string 返回结果
  104. * @author laravel开发员
  105. * @date 2019/6/5
  106. */
  107. function curl_api($url, $data = [], $header=[], $timeout=20)
  108. {
  109. // 初始化
  110. $ch = curl_init();
  111. // 设置post方式提交
  112. curl_setopt($ch, CURLOPT_POST, 1);
  113. // 设置头文件的信息作为数据流输出
  114. curl_setopt($ch, CURLOPT_HEADER, 0);
  115. // 超时
  116. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  117. // 是否要求返回数据
  118. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  119. // 请求头
  120. if($header){
  121. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  122. }
  123. // 设置抓取的url
  124. curl_setopt($ch, CURLOPT_URL, $url);
  125. // 提交的数据
  126. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  127. // 是否检测服务器的证书是否由正规浏览器认证过的授权CA颁发的
  128. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  129. // 执行命令
  130. $result = curl_exec($ch);
  131. // 关闭URL请求(释放句柄)
  132. curl_close($ch);
  133. return $result;
  134. }
  135. }