初始上传

This commit is contained in:
2026-04-04 17:27:12 +08:00
parent 4d80d28eb4
commit b7e11774ee
11191 changed files with 1588469 additions and 0 deletions

56
extend/api/ApiProtocol.php Executable file
View File

@@ -0,0 +1,56 @@
<?php
namespace extend\api;
class ApiProtocol {
const APP_ID_KEY = 'app_key';
const METHOD_KEY = 'method';
const TIMESTAMP_KEY = 'timestamp';
const FORMAT_KEY = 'format';
const VERSION_KEY = 'version';
const SIGN_KEY = 'sign';
const SIGN_METHOD_KEY = 'sign_method';
const TOKEN_KEY = 'access_token';
const ALLOWED_DEVIATE_SECONDS = 600;
const ERR_SYSTEM = -1;
const ERR_INVALID_APP_ID = 40001;
const ERR_INVALID_APP = 40002;
const ERR_INVALID_TIMESTAMP = 40003;
const ERR_EMPTY_SIGNATURE = 40004;
const ERR_INVALID_SIGNATURE = 40005;
const ERR_INVALID_METHOD_NAME = 40006;
const ERR_INVALID_METHOD = 40007;
const ERR_INVALID_TEAM = 40008;
const ERR_PARAMETER = 41000;
const ERR_LOGIC = 50000;
public static function sign($appSecret, $params, $method = 'md5') {
if (!is_array($params)) $params = array();
ksort($params);
$text = '';
foreach ($params as $k => $v) {
$text .= $k . $v;
}
return self::hash($method, $appSecret . $text . $appSecret);
}
private static function hash($method, $text) {
switch ($method) {
case 'md5':
default:
$signature = md5($text);
break;
}
return $signature;
}
public static function allowed_sign_methods() {
return array('md5');
}
public static function allowed_format() {
return array('json');
}
}

125
extend/api/CenterClient.php Executable file
View File

@@ -0,0 +1,125 @@
<?php
namespace extend\api;
/**
* 授权中心
* Class CenterClient
* @package extend\api
*/
class CenterClient
{
// 请求地址
private $request_url = "http://localhost/niucenter/?s=/niucenter/api/";
// 平台标识
private $name = "NIUCLOUD";
private $app_key = "b61d312d32976df698ca34d098d23a9d";
private $public_key;
private $private_key;
private $key_len;
// 参数
private $params = array();
public function __construct()
{
$this->initRSA();
$this->params['params'] = [
'name' => $this->name,
'app_key' => $this->app_key
];
}
/**
* POST请求
* @param $method
* @param array $params
* @return bool|false|mixed|string
*/
public function post($method, $params = [])
{
$this->request_url .= $method;
$publicEncrypt = $this->publicEncrypt(json_encode($params));
$this->params['params']['public_encrypt'] = $publicEncrypt;
$res = HttpClient::post($this->request_url, $this->params);
if (!empty($res)) {
//对返回数据进行解密,解密需要一些时间,暂时去掉
// $decrypted = $this->publicDecrypt($res);
// if(!empty($decrypted)){
// $res = json_decode($decrypted, true);
// }else{
// $res = json_decode($res, true);
// }
$res = json_decode($res, true);
return $res;
} else {
return json_encode(error("接口发生错误"));
}
}
/**
* 初始化公钥 长度
*/
private function initRSA()
{
$public_key = ADDON_APP_PATH . '/Niushop/rsa_public_key.pem';
$public_key_content = file_get_contents($public_key);
$this->public_key = openssl_pkey_get_public($public_key_content);
$this->key_len = openssl_pkey_get_details($this->public_key)['bits'];
}
/*
* 公钥加密
*/
public function publicEncrypt($data)
{
$encrypted = '';
$part_len = $this->key_len / 8 - 11;
$parts = str_split($data, $part_len);
//分段加密
foreach ($parts as $part) {
$encrypted_temp = '';
openssl_public_encrypt($part, $encrypted_temp, $this->public_key);
$encrypted .= $encrypted_temp;
}
return $this->url_safe_base64_encode($encrypted);
}
/*
* 公钥解密
*/
public function publicDecrypt($encrypted)
{
$decrypted = "";
$part_len = $this->key_len / 8;
$base64_decoded = $this->url_safe_base64_decode($encrypted);
$parts = str_split($base64_decoded, $part_len);
foreach ($parts as $part) {
$decrypted_temp = '';
openssl_public_decrypt($part, $decrypted_temp, $this->public_key);
$decrypted .= $decrypted_temp;
}
return $decrypted;
}
function url_safe_base64_encode($data)
{
return str_replace(array( '+', '/', '=' ), array( '-', '_', '' ), base64_encode($data));
}
function url_safe_base64_decode($data)
{
$base_64 = str_replace(array( '-', '_' ), array( '+', '/' ), $data);
return base64_decode($base_64);
}
}

108
extend/api/HttpClient.php Executable file
View File

@@ -0,0 +1,108 @@
<?php
namespace extend\api;
class HttpClient
{
private static $boundary = '';
public static function get($url, $params)
{
$url = $url . '?' . http_build_query($params);
return self::http($url, 'GET');
}
public static function post($url, $params, $files = array())
{
$headers = array();
if (!$files) {
$body = http_build_query($params);
} else {
$body = self::build_http_query_multi($params, $files);
$headers[] = "Content-Type: multipart/form-data; boundary=" . self::$boundary;
}
return self::http($url, 'POST', $body, $headers);
}
private static function http($url, $method, $postfields = NULL, $headers = array())
{
$ci = curl_init();
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($ci, CURLOPT_TIMEOUT, 100);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_ENCODING, "");
curl_setopt($ci, CURLOPT_HEADER, FALSE);
switch ($method) {
case 'POST':
curl_setopt($ci, CURLOPT_POST, TRUE);
if (!empty($postfields)) {
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
}
break;
}
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
//TODO 只有本地使用 外网不用设置
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$response = curl_exec($ci);
$httpCode = curl_getinfo($ci, CURLINFO_HTTP_CODE);
$httpInfo = curl_getinfo($ci);
curl_close($ci);
return $response;
}
private static function build_http_query_multi($params, $files)
{
if (!$params) return '';
$pairs = array();
self::$boundary = $boundary = uniqid('------------------');
$MPboundary = '--' . $boundary;
$endMPboundary = $MPboundary . '--';
$multipartbody = '';
foreach ($params as $key => $value) {
$multipartbody .= $MPboundary . "\r\n";
$multipartbody .= 'content-disposition: form-data; name="' . $key . "\"\r\n\r\n";
$multipartbody .= $value . "\r\n";
}
foreach ($files as $key => $value) {
if (!$value) {
continue;
}
if (is_array($value)) {
$url = $value['url'];
if (isset($value['name'])) {
$filename = $value['name'];
} else {
$parts = explode('?', basename($value['url']));
$filename = $parts[0];
}
$field = isset($value['field']) ? $value['field'] : $key;
} else {
$url = $value;
$parts = explode('?', basename($url));
$filename = $parts[0];
$field = $key;
}
$content = file_get_contents($url);
$multipartbody .= $MPboundary . "\r\n";
$multipartbody .= 'Content-Disposition: form-data; name="' . $field . '"; filename="' . $filename . '"' . "\r\n";
$multipartbody .= "Content-Type: image/unknown\r\n\r\n";
$multipartbody .= $content . "\r\n";
}
$multipartbody .= $endMPboundary;
return $multipartbody;
}
}

102
extend/api/SignClient.php Executable file
View File

@@ -0,0 +1,102 @@
<?php
namespace extend\api;
class SignClient
{
private $version = '1.0';
private $request_url;
// private static $request_url = 'http://demo.niucloud.com/?s=/api/';
private $app_key;
private $app_secret;
private $format = 'json';
private $sign_method = 'md5';
public function __construct($app_key, $app_secret)
{
if ('' == $app_key || '' == $app_secret) throw new \Exception('app_key 和 app_secret 不能为空');
$this->app_key = $app_key;
$this->app_secret = $app_secret;
// $this->request_url = DOMAIN . '/?s=/api/';
$this->request_url = API_URL . '/?s=/api/';
}
public function get($method, $api_version, $params = array())
{
return $this->parse_response(
HttpClient::get($this->url($method, $api_version), $this->build_request_params($method, $params))
);
}
public function post($method, $params = array(), $files = array(), $api_version = '1.0')
{
$this->version = $api_version;
return $this->parse_response(
HttpClient::post($this->url($method, $api_version), $this->build_request_params($method, $params), $files)
);
}
public function url($method, $api_version = '1.0')
{
$method = 'index/index/method/' . $method . '/version/' . $api_version;
$url = $this->request_url . $method;
return $url;
}
public function set_format($format)
{
if (!in_array($format, ApiProtocol::allowed_format()))
throw new \Exception('设置的数据格式错误');
$this->format = $format;
return $this;
}
public function set_sign_method($method)
{
if (!in_array($method, ApiProtocol::allowed_sign_methods()))
throw new \Exception('设置的签名方法错误');
$this->sign_method = $method;
return $this;
}
private function parse_response($response_data)
{
$data = json_decode($response_data, true);
if (null === $data) throw new \Exception('response invalid, data: ' . $response_data);
return $data;
}
private function build_request_params($method, $api_params)
{
if (!is_array($api_params)) $api_params = array();
if ($this->app_key) {
}
$pairs = $this->get_common_params($method);
foreach ($api_params as $k => $v) {
if (isset($pairs[ $k ])) throw new \Exception('参数名冲突');
$pairs[ $k ] = $v;
}
$pairs[ ApiProtocol::SIGN_KEY ] = ApiProtocol::sign($this->app_secret, $pairs, $this->sign_method);
return $pairs;
}
private function get_common_params($method)
{
$params = array();
$params[ ApiProtocol::APP_ID_KEY ] = $this->app_key;
$params[ ApiProtocol::METHOD_KEY ] = $method;
$params[ ApiProtocol::TIMESTAMP_KEY ] = date('Y-m-d H:i:s');
$params[ ApiProtocol::FORMAT_KEY ] = $this->format;
$params[ ApiProtocol::SIGN_METHOD_KEY ] = $this->sign_method;
$params[ ApiProtocol::VERSION_KEY ] = $this->version;
return $params;
}
}

98
extend/api/WebClient.php Executable file
View File

@@ -0,0 +1,98 @@
<?php
namespace extend\api;
use think\Log;
class WebClient
{
private $version = '1.0';
private $request_url;
private $app_key;
private $app_secret;
private $format = 'json';
private $sign_method = 'md5';
public function __construct($app_key, $app_secret)
{
if ('' == $app_key || '' == $app_secret) throw new \Exception('app_key 和 app_secret 不能为空');
$this->app_key = $app_key;
$this->app_secret = $app_secret;
$this->request_url = 'http://localhost/ncweb/auth.php';
}
public function get($method, $params = array(), $api_version = '1.0')
{
return $this->parse_response(
HttpClient::get($this->url($method, $api_version), $this->build_request_params($method, $params))
);
}
public function post($method, $params = array(), $files = array(), $api_version = '1.0')
{
$this->version = $api_version;
return $this->parse_response(
HttpClient::post($this->url($method, $api_version), $this->build_request_params($method, $params), $files)
);
}
public function url($method, $api_version = '1.0')
{
$url = $this->request_url;
return $url;
}
public function set_format($format)
{
if (!in_array($format, ApiProtocol::allowed_format()))
throw new \Exception('设置的数据格式错误');
$this->format = $format;
return $this;
}
public function set_sign_method($method)
{
if (!in_array($method, ApiProtocol::allowed_sign_methods()))
throw new \Exception('设置的签名方法错误');
$this->sign_method = $method;
return $this;
}
private function parse_response($response_data)
{
$data = json_decode($response_data, true);
return $data;
}
private function build_request_params($method, $api_params)
{
if (!is_array($api_params)) $api_params = array();
$pairs = $this->get_common_params($method);
foreach ($api_params as $k => $v) {
if (isset($pairs[ $k ])) throw new \Exception('参数名冲突');
$pairs[ $k ] = $v;
}
$pairs[ ApiProtocol::SIGN_KEY ] = ApiProtocol::sign($this->app_secret, $pairs, $this->sign_method);
return $pairs;
}
private function get_common_params($method)
{
$params = array();
$params[ ApiProtocol::APP_ID_KEY ] = $this->app_key;
$params[ ApiProtocol::METHOD_KEY ] = $method;
$params[ ApiProtocol::TIMESTAMP_KEY ] = date('Y-m-d H:i:s');
$params[ ApiProtocol::FORMAT_KEY ] = $this->format;
$params[ ApiProtocol::SIGN_METHOD_KEY ] = $this->sign_method;
$params[ ApiProtocol::VERSION_KEY ] = $this->version;
return $params;
}
}