123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- /**
- * 添加自定义辅助函数
- */
- if ( ! function_exists('config_path'))
- {
- /**
- * Get the configuration path.
- *
- * @param string $path
- * @return string
- */
- function config_path($path = '')
- {
- return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
- }
- }
- /**
- * 参数签名校验
- * @param array $params
- * @return string
- */
- function generateSign(array $params, $secret_key)
- {
- unset($params['sign']);
- // 将删除参数组中所有等值为FALSE的参数(包括:NULL, 空字符串,0, false)
- // $params = array_filter($params);
- // 按照键名对参数数组进行升序排序
- ksort($params);
- // 给参数数组追加密钥,键名为 key, 值为签名配置中配置的 secret_key 的值
- $params['chxq_key'] = $secret_key;
- \Illuminate\Support\Facades\Log::debug($params);
- // 生成 URL-encode 之后的请求字符串
- $str = http_build_query($params);
- $str = urldecode($str);
- \Illuminate\Support\Facades\Log::debug($str);
- //$str = "address=计算机啊手机壳阿看见手机卡&address_type=1&area_id=2&area_name=西安市&city_id=2&city_name=西安市&contact_mobile
- //=18458881890&contact_name=刘德华&province_id=1&province_name=陕西省&uid=0&zipcode=1000000";
- // 将请求字符串使用MD5加密后,再转换成大写,并返回
- return strtoupper(MD5($str));
- }
- /**
- * 验证签名
- * @param $sign
- * @param $params
- * @return bool
- */
- 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'])) {
- // 更改元数据格式,全部包含在data下
- $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)
- {
- $response = array(
- 'code' => 1,
- 'msg' => $msg,
- 'data' => ""
- );
- return $response;
- }
- function subtext($text, $length)
- {
- 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'])) {
- // 更改元数据格式,全部包含在data下
- $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($num) {
- if ($num >= 100000000) {
- $num = sprintf("%.2f", $num / 100000000) . '亿';
- } elseif ($num >= 100000) {
- $num = intval($num / 10000) . '万';
- } elseif ($num >= 10000) {
- $num = sprintf("%.1f", $num / 10000) . '万';
- } else {
- $num = strval($num);
- }
- return $num;
- }
|