|
|
|
@ -4,6 +4,7 @@ namespace App\Http\Controllers\API;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
|
|
|
|
use Illuminate\Http\Exceptions\HttpResponseException;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
@ -28,16 +29,17 @@ class XCXApiController extends Controller
|
|
|
|
|
|
|
|
|
|
|
|
public static function Post($url_code, $data)
|
|
|
|
public static function Post($url_code, $data)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
$url_address = self::Api($url_code);
|
|
|
|
$url_address = self::Api($url_code);
|
|
|
|
self::RequestLog(self::$baseUrl.$url_address, $data, $url_code, '小程序接口');
|
|
|
|
self::RequestLog(self::$baseUrl . $url_address, $data, $url_code, '小程序接口');
|
|
|
|
$timestamp = time();
|
|
|
|
$timestamp = time();
|
|
|
|
$nonce = md5(uniqid(rand(), true));
|
|
|
|
$nonce = md5(uniqid(rand(), true));
|
|
|
|
$base64Signature = self::Sign( $url_address, $data, $nonce, $timestamp);
|
|
|
|
$base64Signature = self::Sign($url_address, $data, $nonce, $timestamp);
|
|
|
|
$response = Http::withHeaders([
|
|
|
|
$response = Http::withHeaders([
|
|
|
|
'Authorization' => self::BuildAuthorization($nonce, $timestamp, $base64Signature)
|
|
|
|
'Authorization' => self::BuildAuthorization($nonce, $timestamp, $base64Signature)
|
|
|
|
])->post(self::$baseUrl.$url_address, $data);
|
|
|
|
])->post(self::$baseUrl . $url_address, $data);
|
|
|
|
|
|
|
|
|
|
|
|
// dd($response);
|
|
|
|
// dd(self::BuildAuthorization($nonce, $timestamp, $base64Signature));
|
|
|
|
if ($response->successful()) {
|
|
|
|
if ($response->successful()) {
|
|
|
|
// 处理成功的响应
|
|
|
|
// 处理成功的响应
|
|
|
|
$res_string = json_encode($response->json(), JSON_UNESCAPED_UNICODE);
|
|
|
|
$res_string = json_encode($response->json(), JSON_UNESCAPED_UNICODE);
|
|
|
|
@ -48,11 +50,21 @@ class XCXApiController extends Controller
|
|
|
|
if ($str_size > 10) $save_res = '{"data":"Row size too large"}';
|
|
|
|
if ($str_size > 10) $save_res = '{"data":"Row size too large"}';
|
|
|
|
self::$request->response_data = $save_res;
|
|
|
|
self::$request->response_data = $save_res;
|
|
|
|
self::$request->save();
|
|
|
|
self::$request->save();
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$res = json_decode($res_string, true);
|
|
|
|
|
|
|
|
if (!$res['success']) {
|
|
|
|
|
|
|
|
throw new HttpResponseException(\Yz::echoError1("小程序接口提示:" . $res['message']));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
|
|
'code' => $res['code'],
|
|
|
|
|
|
|
|
'message' => $res['message'],
|
|
|
|
|
|
|
|
'data' => $res['result']
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// 处理失败的响应
|
|
|
|
// 处理失败的响应
|
|
|
|
self::$request->response_data = "请求失败";
|
|
|
|
self::$request->response_data = "请求小程序接口失败";
|
|
|
|
self::$request->save();
|
|
|
|
self::$request->save();
|
|
|
|
|
|
|
|
throw new HttpResponseException(\Yz::echoError1("请求小程序接口失败"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -105,7 +117,7 @@ class XCXApiController extends Controller
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//计算签名和 Authorization
|
|
|
|
//计算签名和 Authorization
|
|
|
|
public static function Sign($url, $data, $nonce, $timestamp,$is_urlencode=false)
|
|
|
|
public static function Sign($url, $data, $nonce, $timestamp, $is_urlencode = false)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
$body = self::buildSortedQueryString($data);
|
|
|
|
$body = self::buildSortedQueryString($data);
|
|
|
|
@ -122,18 +134,18 @@ class XCXApiController extends Controller
|
|
|
|
$url . "\n" .
|
|
|
|
$url . "\n" .
|
|
|
|
$timestamp . "\n" .
|
|
|
|
$timestamp . "\n" .
|
|
|
|
$nonce . "\n" .
|
|
|
|
$nonce . "\n" .
|
|
|
|
$body;
|
|
|
|
urldecode($body) . "\n";
|
|
|
|
//dd($SignStr);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 使用私钥进行 RSA 签名
|
|
|
|
// 使用私钥进行 RSA 签名
|
|
|
|
openssl_sign($SignStr, $signature, $private_key, OPENSSL_ALGO_SHA256);
|
|
|
|
openssl_sign($SignStr, $signature, $private_key, OPENSSL_ALGO_SHA256);
|
|
|
|
|
|
|
|
|
|
|
|
if($is_urlencode){
|
|
|
|
if ($is_urlencode) {
|
|
|
|
$signature= urlencode($signature);
|
|
|
|
$signature = urlencode($signature);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 对签名结果进行 Base64 编码
|
|
|
|
// 对签名结果进行 Base64 编码
|
|
|
|
$base64Signature = base64_encode($signature);
|
|
|
|
$base64Signature = base64_encode($signature);
|
|
|
|
// dd($base64Signature);
|
|
|
|
// dd($SignStr,$base64Signature);
|
|
|
|
return $base64Signature;
|
|
|
|
return $base64Signature;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -146,5 +158,21 @@ class XCXApiController extends Controller
|
|
|
|
return self::$signType . ' ' . $signInfo;
|
|
|
|
return self::$signType . ' ' . $signInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//解密
|
|
|
|
|
|
|
|
public static function XCXDecode($str)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
$private_key = Storage::get('keys/private_key.pem');
|
|
|
|
|
|
|
|
$str = "mZd4Nds9jyKxGfjId+eQAQ3WBMZbjEZg4Plq2Qz62W+AZvcDCAjkOL8VRAxNWXTwsrCNkMf07tMoY1NPzPgob00SfOJwVeM7SxqwH4PWKqkm5+Rg4g+eciepst66ToPI2ArgZutnb3XlcOqF8Mzlvz53GgDe/6VFQDaFATj1DOrEMyPgF85FY18lHWH5HKC1ctOMC/FTwTISJ0QPMSpYiBu8hWr4mTkYzY5nFkxv+M4q30IeAvJocCuIFnEa8t8iU6cvixe0HJofLug7fJJb8PEwPhJRMIocME1knETKszhI7YgSBj9RlBkwzLhtG2RCCZbHiK4UOK7HrvthXyJI/A==";
|
|
|
|
|
|
|
|
$str = base64_decode($str);
|
|
|
|
|
|
|
|
// dd($str);
|
|
|
|
|
|
|
|
if (openssl_private_decrypt($str, $decryptedData, $private_key, OPENSSL_PKCS1_OAEP_PADDING)) {
|
|
|
|
|
|
|
|
dd($decryptedData);
|
|
|
|
|
|
|
|
return $decryptedData;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw new HttpResponseException(\Yz::echoError1("解密数据失败"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|