123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- if (!function_exists('config_path')) {
-
- function config_path($path = '')
- {
- return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
- }
- }
- function generateSign(array $params, $secret_key)
- {
- \Illuminate\Support\Facades\Log::debug($params);
- unset($params['sign']);
-
-
-
- ksort($params);
-
- $params['chxq_key'] = $secret_key;
- \Illuminate\Support\Facades\Log::debug($params);
-
- $str = http_build_query($params);
- $str = urldecode($str);
- \Illuminate\Support\Facades\Log::debug($str);
-
-
-
- return strtoupper(MD5($str));
- }
- function verifySign($sign, $params, $secret_key)
- {
- if ($sign == generateSign($params, $secret_key)) {
- return true;
- } else {
- return false;
- };
- }
- function http($url, $param, $method = 'post')
- {
- try {
- $client = new \GuzzleHttp\Client();
- $response = $client->request($method, $url, $param);
- $result = json_decode($response->getBody()->getContents(), true);
- \Illuminate\Support\Facades\Log::debug('url:' . $url . 'param:' . json_encode($param) . 'response:' . json_encode($result));
- if (isset($result['code']) && $result['code'] == 0) {
- return $result['data'];
- } else {
- return [];
- }
- } catch (\Exception $exception) {
- return [];
- }
- }
- function jsonSuccess($data = [], $msg = "成功", $extra = [])
- {
- $response = array(
- 'code' => 0,
- 'msg' => $msg,
- 'data' => []
- );
- if ($data) {
- if (is_array($data)) {
-
- if (isset($data['meta'])) {
-
- $temp = array(
- 'data' => array(
- 'data' => $data['data'],
- 'pagination' => $data['meta']['pagination']
- )
- );
- if ($extra) {
- $temp['data'] = array_merge($temp['data'], $extra);
- }
- $response = array_merge($response, $temp);
- } elseif (isset($data['data'])) {
- if ($extra) {
- $data['data'] = array_merge($data['data'], $extra);
- }
- $response = array_merge($response, $data);
- } else {
- $temp = array(
- 'data' => $data
- );
- if ($extra) {
- $temp['data'] = array_merge($temp['data'], $extra);
- }
- $response = array_merge($response, $temp);
- }
- } else {
- $response['data'] = $data;
- }
- }
- return $response;
- }
- function jsonError($msg, $data = [])
- {
- $response = array(
- 'code' => 1,
- 'msg' => $msg,
- 'data' => new \stdClass()
- );
- if ($data) {
- $response['data'] = $data;
- }
- return $response;
- }
- function subtext($text, $length)
- {
-
- $text = strip_tags($text);
- $search = array(" ", " ", "\n", "\r", "\t");
- $replace = array("", "", "", "", "");
- $text = str_replace($search, $replace, $text);
- if (mb_strlen($text, 'utf8') > $length) {
- return mb_substr($text, 0, $length - 1, 'utf8') . '...';
- } else {
- return $text;
- }
- }
- function success($data = [], $msg = "成功")
- {
- $response = array(
- 'code' => 0,
- 'msg' => $msg,
- 'data' => new stdClass()
- );
- if ($data) {
- if (is_array($data)) {
-
- if (isset($data['meta'])) {
-
- $temp = array(
- 'data' => array(
- 'data' => $data['data'],
- 'pagination' => $data['meta']['pagination']
- )
- );
- $response = array_merge($response, $temp);
- } elseif (isset($data['data'])) {
- $response = $data;
- } else {
- $temp = array(
- 'data' => $data
- );
- $response = array_merge($response, $temp);
- }
- }
- }
- return $response;
- }
- function getNumber($number)
- {
- if (empty($number) || !is_numeric($number)) return (string)intval($number);
- $unit = "";
- if ($number >= 10000) {
- $leftNumber = floor($number / 10000);
- $rightNumber = bcmul(($number % 10000) / 10000, '1', 1);
- $number = floatval($leftNumber + $rightNumber);
- $unit = "w";
- } else {
- $decimals = $number > 1 ? 2 : 6;
- $number = (float)number_format($number, $decimals, '.', '');
- }
- return (string)$number . $unit;
- }
- function getClientIp()
- {
- $ip = '';
- $unknown = '';
- if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) {
-
- $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
- } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
-
- $ip = $_SERVER['REMOTE_ADDR'];
- }
-
- if (strpos($ip, ',') !== false) {
-
- $ips = explode(',', $ip);
- $ip = $ips[0];
- }
- return $ip;
- }
- function sortArrByField(&$array, $field, $desc = false)
- {
- $fieldArr = array();
- foreach ($array as $k => $v) {
- $fieldArr[$k] = $v[$field];
- }
- $sort = $desc == false ? SORT_ASC : SORT_DESC;
- array_multisort($fieldArr, $sort, $array);
- }
|