helper.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. // 生成 URL-encode 之后的请求字符串
  33. $str = http_build_query($params);
  34. $str = urldecode($str);
  35. // 将请求字符串使用MD5加密后,再转换成大写,并返回
  36. return strtoupper(MD5($str));
  37. }
  38. /**
  39. * 验证签名
  40. * @param $sign
  41. * @param $params
  42. * @return bool
  43. */
  44. function verifySign($sign, $params, $secret_key)
  45. {
  46. if ($sign == generateSign($params, $secret_key)) {
  47. return true;
  48. } else {
  49. return false;
  50. };
  51. }
  52. function jsonSuccess($data = [], $msg = "成功")
  53. {
  54. $response = array(
  55. 'code' => 0,
  56. 'msg' => $msg,
  57. 'data' => []
  58. );
  59. if ($data) {
  60. if (is_array($data)) {
  61. //带有分页格式转换
  62. if (isset($data['meta'])) {
  63. // 更改元数据格式,全部包含在data下
  64. $temp = array(
  65. 'data' => array(
  66. 'data' => $data['data'],
  67. 'pagination' => $data['meta']['pagination']
  68. )
  69. );
  70. $response = array_merge($response, $temp);
  71. } elseif(isset($data['data'])) {
  72. $response = array_merge($response, $data);
  73. }else{
  74. $temp = array(
  75. 'data' => $data
  76. );
  77. $response = array_merge($response, $temp);
  78. }
  79. } else {
  80. $response['data'] = $data;
  81. }
  82. }
  83. return $response;
  84. }
  85. function jsonError($msg)
  86. {
  87. $response = array(
  88. 'code' => 1,
  89. 'msg' => $msg,
  90. 'data' => ""
  91. );
  92. return $response;
  93. }