helper.php 2.1 KB

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