helper.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. return [];
  56. }
  57. }
  58. function generateSign(array $params, $secret_key)
  59. {
  60. unset($params['sign']);
  61. // 将删除参数组中所有等值为FALSE的参数(包括:NULL, 空字符串,0, false)
  62. $params = array_filter($params);
  63. // 按照键名对参数数组进行升序排序
  64. ksort($params);
  65. // 给参数数组追加密钥,键名为 key, 值为签名配置中配置的 secret_key 的值
  66. $params['chxq_key'] = $secret_key;
  67. \Illuminate\Support\Facades\Log::debug($params);
  68. // 生成 URL-encode 之后的请求字符串
  69. $str = http_build_query($params);
  70. $str = urldecode($str);
  71. \Illuminate\Support\Facades\Log::debug($str);
  72. //$str = "address=计算机啊手机壳阿看见手机卡&address_type=1&area_id=2&area_name=西安市&city_id=2&city_name=西安市&contact_mobile
  73. //=18458881890&contact_name=刘德华&province_id=1&province_name=陕西省&uid=0&zipcode=1000000";
  74. // 将请求字符串使用MD5加密后,再转换成大写,并返回
  75. return strtoupper(MD5($str));
  76. }