helper.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * 添加自定义辅助函数
  4. */
  5. if (!function_exists('config_path')) {
  6. /**
  7. * Get the configuration path.
  8. *
  9. * @param string $path
  10. * @return string
  11. */
  12. function config_path($path = '')
  13. {
  14. return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
  15. }
  16. }
  17. /**
  18. * 参数签名校验
  19. * @param array $params
  20. * @return string
  21. */
  22. function generateSign(array $params, $secret_key)
  23. {
  24. \Illuminate\Support\Facades\Log::debug($params);
  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. \Illuminate\Support\Facades\Log::debug('url:' . $url . 'param:' . json_encode($param) . 'response:' . json_encode($result));
  63. if (isset($result['code']) && $result['code'] == 0) {
  64. return $result['data'];
  65. } else {
  66. return [];
  67. }
  68. } catch (\Exception $exception) {
  69. return [];
  70. }
  71. }
  72. function jsonSuccess($data = [], $msg = "成功", $extra = [])
  73. {
  74. $response = array(
  75. 'code' => 0,
  76. 'msg' => $msg,
  77. 'data' => []
  78. );
  79. if ($data) {
  80. if (is_array($data)) {
  81. //带有分页格式转换
  82. if (isset($data['meta'])) {
  83. // 更改元数据格式,全部包含在data下
  84. $temp = array(
  85. 'data' => array(
  86. 'data' => $data['data'],
  87. 'pagination' => $data['meta']['pagination']
  88. )
  89. );
  90. if ($extra) {
  91. $temp['data'] = array_merge($temp['data'], $extra);
  92. }
  93. $response = array_merge($response, $temp);
  94. } elseif (isset($data['data'])) {
  95. if ($extra) {
  96. $data['data'] = array_merge($data['data'], $extra);
  97. }
  98. $response = array_merge($response, $data);
  99. } else {
  100. $temp = array(
  101. 'data' => $data
  102. );
  103. if ($extra) {
  104. $temp['data'] = array_merge($temp['data'], $extra);
  105. }
  106. $response = array_merge($response, $temp);
  107. }
  108. } else {
  109. $response['data'] = $data;
  110. }
  111. }
  112. return $response;
  113. }
  114. function jsonError($msg, $data = [])
  115. {
  116. $response = array(
  117. 'code' => 1,
  118. 'msg' => $msg,
  119. 'data' => new \stdClass()
  120. );
  121. if ($data) {
  122. $response['data'] = $data;
  123. }
  124. return $response;
  125. }
  126. function subtext($text, $length)
  127. {
  128. //去除标签 空格 换行等空白字符
  129. $text = strip_tags($text);
  130. $search = array(" ", " ", "\n", "\r", "\t");
  131. $replace = array("", "", "", "", "");
  132. $text = str_replace($search, $replace, $text);
  133. if (mb_strlen($text, 'utf8') > $length) {
  134. return mb_substr($text, 0, $length - 1, 'utf8') . '...';
  135. } else {
  136. return $text;
  137. }
  138. }
  139. function success($data = [], $msg = "成功")
  140. {
  141. $response = array(
  142. 'code' => 0,
  143. 'msg' => $msg,
  144. 'data' => new stdClass()
  145. );
  146. if ($data) {
  147. if (is_array($data)) {
  148. //带有分页格式转换
  149. if (isset($data['meta'])) {
  150. // 更改元数据格式,全部包含在data下
  151. $temp = array(
  152. 'data' => array(
  153. 'data' => $data['data'],
  154. 'pagination' => $data['meta']['pagination']
  155. )
  156. );
  157. $response = array_merge($response, $temp);
  158. } elseif (isset($data['data'])) {
  159. $response = $data;
  160. } else {
  161. $temp = array(
  162. 'data' => $data
  163. );
  164. $response = array_merge($response, $temp);
  165. }
  166. }
  167. }
  168. return $response;
  169. }
  170. //转换数字
  171. function getNumber($number)
  172. {
  173. if (empty($number) || !is_numeric($number)) return (string)intval($number);
  174. $unit = "";
  175. if ($number >= 10000) {
  176. $leftNumber = floor($number / 10000);
  177. $rightNumber = bcmul(($number % 10000) / 10000, '1', 1);
  178. $number = floatval($leftNumber + $rightNumber);
  179. $unit = "w";
  180. } else {
  181. $decimals = $number > 1 ? 2 : 6;
  182. $number = (float)number_format($number, $decimals, '.', '');
  183. }
  184. return (string)$number . $unit;
  185. }
  186. function getClientIp()
  187. {
  188. $ip = '';
  189. $unknown = '';
  190. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) {
  191. // 使用透明代理、欺骗性代理的情况
  192. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  193. } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
  194. // 没有代理、使用普通匿名代理和高匿代理的情况
  195. $ip = $_SERVER['REMOTE_ADDR'];
  196. }
  197. // 处理多层代理的情况
  198. if (strpos($ip, ',') !== false) {
  199. // 输出第一个IP
  200. $ips = explode(',', $ip);
  201. $ip = $ips[0];
  202. }
  203. return $ip;
  204. }
  205. /**
  206. * 多维数组按单字段排序
  207. * @param $array
  208. * @param $field
  209. * @param bool $desc
  210. */
  211. function sortArrByField(&$array, $field, $desc = false)
  212. {
  213. $fieldArr = array();
  214. foreach ($array as $k => $v) {
  215. $fieldArr[$k] = $v[$field];
  216. }
  217. $sort = $desc == false ? SORT_ASC : SORT_DESC;
  218. array_multisort($fieldArr, $sort, $array);
  219. }