helper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. }
  71. function jsonSuccess($data = [], $msg = "成功")
  72. {
  73. $response = array(
  74. 'code' => 0,
  75. 'msg' => $msg,
  76. 'data' => new stdClass()
  77. );
  78. if ($data) {
  79. if (is_array($data)) {
  80. //带有分页格式转换
  81. if (isset($data['meta'])) {
  82. // 更改元数据格式,全部包含在data下
  83. $temp = array(
  84. 'data' => array(
  85. 'data' => $data['data'],
  86. 'pagination' => $data['meta']['pagination']
  87. )
  88. );
  89. $response = array_merge($response, $temp);
  90. } elseif(isset($data['data'])) {
  91. $response = array_merge($response, $data);
  92. }else{
  93. $temp = array(
  94. 'data' => $data
  95. );
  96. $response = array_merge($response, $temp);
  97. }
  98. } else {
  99. $response['data'] = $data;
  100. }
  101. }
  102. return $response;
  103. }
  104. function jsonError($msg)
  105. {
  106. $response = array(
  107. 'code' => 1,
  108. 'msg' => $msg,
  109. 'data' => ""
  110. );
  111. return $response;
  112. }
  113. function subtext($text, $length)
  114. {
  115. if(mb_strlen($text, 'utf8') > $length) {
  116. return mb_substr($text, 0, $length - 1, 'utf8').'...';
  117. } else {
  118. return $text;
  119. }
  120. }