helper.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. function subtext($text, $length)
  31. {
  32. if(mb_strlen($text, 'utf8') > $length) {
  33. return mb_substr($text, 0, $length, 'utf8').'...';
  34. } else {
  35. return $text;
  36. }
  37. }
  38. function http($url, $param, $isCheck = true, $method = 'post')
  39. {
  40. try {
  41. $client = new \GuzzleHttp\Client();
  42. $response = $client->request($method, $url, $param);
  43. $result = json_decode($response->getBody()->getContents(), true);
  44. \Illuminate\Support\Facades\Log::debug('url:'.$url.'param:'.json_encode($param).'response:'.json_encode($result));
  45. if ($isCheck == true) {
  46. if ($result['code'] == 0) {
  47. return $result['data'];
  48. } else {
  49. return [];
  50. }
  51. } else {
  52. return $result;
  53. }
  54. } catch (\Exception $exception) {
  55. \Illuminate\Support\Facades\Log::debug('http-exception:'.$exception->getMessage());
  56. return [];
  57. }
  58. }
  59. function generateSign(array $params, $secret_key)
  60. {
  61. unset($params['sign']);
  62. // 将删除参数组中所有等值为FALSE的参数(包括:NULL, 空字符串,0, false)
  63. $params = array_filter($params);
  64. // 按照键名对参数数组进行升序排序
  65. ksort($params);
  66. // 给参数数组追加密钥,键名为 key, 值为签名配置中配置的 secret_key 的值
  67. $params['chxq_key'] = $secret_key;
  68. \Illuminate\Support\Facades\Log::debug($params);
  69. // 生成 URL-encode 之后的请求字符串
  70. $str = http_build_query($params);
  71. $str = urldecode($str);
  72. \Illuminate\Support\Facades\Log::debug($str);
  73. //$str = "address=计算机啊手机壳阿看见手机卡&address_type=1&area_id=2&area_name=西安市&city_id=2&city_name=西安市&contact_mobile
  74. //=18458881890&contact_name=刘德华&province_id=1&province_name=陕西省&uid=0&zipcode=1000000";
  75. // 将请求字符串使用MD5加密后,再转换成大写,并返回
  76. return strtoupper(MD5($str));
  77. }