helper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * 添加自定义辅助函数
  4. */
  5. if ( ! function_exists('config_path'))
  6. {
  7. /**
  8. * Get the configuration path.
  9. *
  10. * @param string $path
  11. * @return string
  12. */
  13. function config_path($path = '')
  14. {
  15. return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
  16. }
  17. }
  18. if (! function_exists('public_path')) {
  19. /**
  20. * Get the path to the public folder.
  21. *
  22. * @param string $path
  23. * @return string
  24. */
  25. function public_path($path = '')
  26. {
  27. return app()->basePath() . '/public' . ($path ? '/' . $path : $path);
  28. }
  29. }
  30. /**
  31. * 参数签名校验
  32. * @param array $params
  33. * @return string
  34. */
  35. function generateSign(array $params, $secret_key)
  36. {
  37. unset($params['sign']);
  38. // 将删除参数组中所有等值为FALSE的参数(包括:NULL, 空字符串,0, false)
  39. // $params = array_filter($params);
  40. // 按照键名对参数数组进行升序排序
  41. ksort($params);
  42. // 给参数数组追加密钥,键名为 key, 值为签名配置中配置的 secret_key 的值
  43. $params['chxq_key'] = $secret_key;
  44. // 生成 URL-encode 之后的请求字符串
  45. $str = http_build_query($params);
  46. $str = urldecode($str);
  47. // 将请求字符串使用MD5加密后,再转换成大写,并返回
  48. return strtoupper(MD5($str));
  49. }
  50. /**
  51. * 验证签名
  52. * @param $sign
  53. * @param $params
  54. * @return bool
  55. */
  56. function verifySign($sign, $params, $secret_key)
  57. {
  58. if ($sign == generateSign($params, $secret_key)) {
  59. return true;
  60. } else {
  61. return false;
  62. };
  63. }
  64. function jsonSuccess($data = [], $msg = "成功")
  65. {
  66. $response = array(
  67. 'code' => 0,
  68. 'msg' => $msg,
  69. 'data' => []
  70. );
  71. if ($data) {
  72. if (is_array($data)) {
  73. //带有分页格式转换
  74. if (isset($data['meta'])) {
  75. // 更改元数据格式,全部包含在data下
  76. $temp = array(
  77. 'data' => array(
  78. 'data' => $data['data'],
  79. 'pagination' => $data['meta']['pagination']
  80. )
  81. );
  82. $response = array_merge($response, $temp);
  83. } elseif(isset($data['data'])) {
  84. $response = array_merge($response, $data);
  85. }else{
  86. $temp = array(
  87. 'data' => $data
  88. );
  89. $response = array_merge($response, $temp);
  90. }
  91. } else {
  92. $response['data'] = $data;
  93. }
  94. }
  95. return $response;
  96. }
  97. function jsonError($msg)
  98. {
  99. $response = array(
  100. 'code' => 1,
  101. 'msg' => $msg,
  102. 'data' => ""
  103. );
  104. return $response;
  105. }
  106. function http($url, $param, $method = 'post')
  107. {
  108. try {
  109. $client = new \GuzzleHttp\Client();
  110. $response = $client->request($method, $url, $param);
  111. $result = json_decode($response->getBody()->getContents(), true);
  112. \Illuminate\Support\Facades\Log::debug('url:'.$url.'param:'.json_encode($param).'response:'.json_encode($result));
  113. if (isset($result['code']) && $result['code'] == 0) {
  114. return $result['data'];
  115. } else {
  116. return [];
  117. }
  118. } catch (\Exception $exception) {
  119. return [];
  120. }
  121. }
  122. function subtext($text, $length)
  123. {
  124. if(mb_strlen($text, 'utf8') > $length) {
  125. return mb_substr($text, 0, $length - 1, 'utf8').'...';
  126. } else {
  127. return $text;
  128. }
  129. }
  130. function filter_Emoji($str)
  131. {
  132. $str = preg_replace_callback( //执行一个正则表达式搜索并且使用一个回调进行替换
  133. '/./u',
  134. function (array $match) {
  135. return strlen($match[0]) >= 4 ? '☆' : $match[0];
  136. },
  137. $str);
  138. return $str;
  139. }