release
Showing
2 changed files
with
7 additions
and
37 deletions
| ... | @@ -9,6 +9,7 @@ use GuzzleHttp\Client; | ... | @@ -9,6 +9,7 @@ use GuzzleHttp\Client; |
| 9 | use Illuminate\Support\Facades\Cache; | 9 | use Illuminate\Support\Facades\Cache; |
| 10 | use Illuminate\Support\Facades\Log; | 10 | use Illuminate\Support\Facades\Log; |
| 11 | 11 | ||
| 12 | |||
| 12 | class BaseController extends Controller | 13 | class BaseController extends Controller |
| 13 | { | 14 | { |
| 14 | /** | 15 | /** |
| ... | @@ -28,7 +29,6 @@ class BaseController extends Controller | ... | @@ -28,7 +29,6 @@ class BaseController extends Controller |
| 28 | } | 29 | } |
| 29 | 30 | ||
| 30 | /**获取accessToken | 31 | /**获取accessToken |
| 31 | * | ||
| 32 | */ | 32 | */ |
| 33 | function getToken() | 33 | function getToken() |
| 34 | { | 34 | { |
| ... | @@ -50,17 +50,13 @@ class BaseController extends Controller | ... | @@ -50,17 +50,13 @@ class BaseController extends Controller |
| 50 | Log::channel('api')->error(__METHOD__, ['msg'=>$e->getMessage()]); | 50 | Log::channel('api')->error(__METHOD__, ['msg'=>$e->getMessage()]); |
| 51 | return null; | 51 | return null; |
| 52 | } | 52 | } |
| 53 | |||
| 54 | } | 53 | } |
| 55 | 54 | ||
| 56 | /** | 55 | /** |
| 57 | * 重组请求数据,返回headers和json | 56 | * 重组请求数据,返回headers |
| 58 | * @param string $uri | ||
| 59 | * @param array $reqData | ||
| 60 | * @param bool $isLocal | ||
| 61 | * @return array | 57 | * @return array |
| 62 | */ | 58 | */ |
| 63 | private function parseData(string $uri, array $params) | 59 | private function parseHeaders(array $params) |
| 64 | { | 60 | { |
| 65 | if (!$accessToken = Cache::get(CacheKeyTools::tmeAccessToken())) { | 61 | if (!$accessToken = Cache::get(CacheKeyTools::tmeAccessToken())) { |
| 66 | $accessToken = $this->getToken(); | 62 | $accessToken = $this->getToken(); |
| ... | @@ -69,8 +65,8 @@ class BaseController extends Controller | ... | @@ -69,8 +65,8 @@ class BaseController extends Controller |
| 69 | $heaers['accessToken'] = $accessToken; | 65 | $heaers['accessToken'] = $accessToken; |
| 70 | $heaers['timestamp'] = (string)date('YmdHis'); | 66 | $heaers['timestamp'] = (string)date('YmdHis'); |
| 71 | $heaers['traceId'] = (string)'1'; | 67 | $heaers['traceId'] = (string)'1'; |
| 72 | $heaers['sign'] = $this->getSign($heaers,$params); | ||
| 73 | $heaers['signMethod'] = 'md5'; | 68 | $heaers['signMethod'] = 'md5'; |
| 69 | $heaers['sign'] = $this->getSign($heaers,$params); | ||
| 74 | return $heaers; | 70 | return $heaers; |
| 75 | } | 71 | } |
| 76 | 72 | ||
| ... | @@ -89,7 +85,7 @@ class BaseController extends Controller | ... | @@ -89,7 +85,7 @@ class BaseController extends Controller |
| 89 | } else { | 85 | } else { |
| 90 | $params = $reqData; | 86 | $params = $reqData; |
| 91 | } | 87 | } |
| 92 | $headers = $this->parseData($uri, $params); | 88 | $headers = $this->parseHeaders($params); |
| 93 | switch (strtoupper($method)) { | 89 | switch (strtoupper($method)) { |
| 94 | case 'GET': | 90 | case 'GET': |
| 95 | $response = $this->client->request('GET', $url, ['query' => $params,'headers'=>$headers]); | 91 | $response = $this->client->request('GET', $url, ['query' => $params,'headers'=>$headers]); |
| ... | @@ -118,24 +114,9 @@ class BaseController extends Controller | ... | @@ -118,24 +114,9 @@ class BaseController extends Controller |
| 118 | /** | 114 | /** |
| 119 | * 获取签名和去空的参数 | 115 | * 获取签名和去空的参数 |
| 120 | * @param array $array | 116 | * @param array $array |
| 121 | * @return array | ||
| 122 | */ | 117 | */ |
| 123 | protected function getSign($heaers,$params) | 118 | protected function getSign($heaers,$params) |
| 124 | { | 119 | { |
| 125 | $params = [ | ||
| 126 | "batchId" => "8934234012312323235", | ||
| 127 | "phone" => "130****0815", | ||
| 128 | "templateId" => "M000198", | ||
| 129 | "bizType"=> 1, | ||
| 130 | "templateVariable"=> [ | ||
| 131 | "titleMap"=> ["k"=>"v"], | ||
| 132 | "contentMap"=> ["k"=>"v"], | ||
| 133 | ], | ||
| 134 | "waterId"=> "8934234012312323234", | ||
| 135 | "platformType"=>"musician", | ||
| 136 | "tenant"=>"musician", | ||
| 137 | "accountId"=>6793497 | ||
| 138 | ]; | ||
| 139 | $common_params = array_merge($params,$heaers); | 120 | $common_params = array_merge($params,$heaers); |
| 140 | ksort($common_params); | 121 | ksort($common_params); |
| 141 | $params = ''; | 122 | $params = ''; |
| ... | @@ -145,24 +126,13 @@ class BaseController extends Controller | ... | @@ -145,24 +126,13 @@ class BaseController extends Controller |
| 145 | continue; | 126 | continue; |
| 146 | } | 127 | } |
| 147 | if (is_array($v)) { | 128 | if (is_array($v)) { |
| 129 | ksort($v); | ||
| 148 | $params .= $k . json_encode($this->handleSubArrayNumber($v)); | 130 | $params .= $k . json_encode($this->handleSubArrayNumber($v)); |
| 149 | } else { | 131 | } else { |
| 150 | $params .= $k . $v; | 132 | $params .= $k . $v; |
| 151 | } | 133 | } |
| 152 | } | 134 | } |
| 153 | $result = $this->str_to_utf8('appSecret' .$this->appSecret. $params); | 135 | return strtoupper(md5($this->appSecret . $params . $this->appSecret)); |
| 154 | echo '拼装好的utf8字符串:'.$result; | ||
| 155 | |||
| 156 | $result = md5($result); | ||
| 157 | echo '摘要后的sign结果(16位):' . substr(md5($result), 8, 16); | ||
| 158 | dd($result); | ||
| 159 | return $result; | ||
| 160 | } | ||
| 161 | |||
| 162 | function str_to_utf8($str = '') { | ||
| 163 | $current_encode = mb_detect_encoding($str, array("ASCII","GB2312","GBK",'BIG5','UTF-8')); | ||
| 164 | $encoded_str = mb_convert_encoding($str, 'UTF-8', $current_encode); | ||
| 165 | return $encoded_str; | ||
| 166 | } | 136 | } |
| 167 | 137 | ||
| 168 | /** | 138 | /** | ... | ... |
This diff is collapsed.
Click to expand it.
-
Please register or sign in to post a comment