228 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			228 lines
		
	
	
		
			6.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
declare(strict_types=1);
 | 
						|
 | 
						|
namespace BvBeJ\Handles;
 | 
						|
 | 
						|
use GuzzleHttp\Client;
 | 
						|
use GuzzleHttp\RequestOptions;
 | 
						|
use GuzzleHttp\Exception\GuzzleException;
 | 
						|
use Illuminate\Support\Facades\Log;
 | 
						|
use Ramsey\Uuid\Rfc4122\UuidV4;
 | 
						|
use Ramsey\Uuid\Uuid;
 | 
						|
 | 
						|
class BvBeJHandle
 | 
						|
{
 | 
						|
    const DATE_FORMAT = 'Y-m-d H:i:s';
 | 
						|
 | 
						|
    private static Client $client;
 | 
						|
 | 
						|
    public function __construct()
 | 
						|
    {
 | 
						|
        self::$client = new Client();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Aes加密
 | 
						|
     * @param string $data
 | 
						|
     * @param string $key
 | 
						|
     * @param string $vi
 | 
						|
     * @return string|false
 | 
						|
     */
 | 
						|
    public function AesEncrypt(string $data, string $key, string $vi): string|false
 | 
						|
    {
 | 
						|
        return base64_encode(openssl_encrypt($data, 'AES-128-CBC', $key, OPENSSL_CIPHER_AES_128_CBC, $vi));
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Aes解密
 | 
						|
     * @param string $data
 | 
						|
     * @param string $key
 | 
						|
     * @param string $vi
 | 
						|
     * @return string|false
 | 
						|
     */
 | 
						|
    public function AesDecrypt(string $data, string $key, string $vi): string|false
 | 
						|
    {
 | 
						|
        return openssl_decrypt(base64_decode($data), 'AES-128-CBC', $key, OPENSSL_CIPHER_AES_128_CBC, $vi);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param string $data
 | 
						|
     * @param bool $strict
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public function Base64UrlEncode(string $data, bool $strict = false): string
 | 
						|
    {
 | 
						|
        if ($strict) {
 | 
						|
            return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
 | 
						|
        }
 | 
						|
        return str_replace(['+', '/'], ['-', '_'], base64_encode($data));
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param string $data
 | 
						|
     * @param bool $strict
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public function Base64UrlDecode(string $data, bool $strict = false): string
 | 
						|
    {
 | 
						|
        if ($strict) {
 | 
						|
            return base64_decode(str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT));
 | 
						|
        }
 | 
						|
        return base64_decode(str_replace(['-', '_'], ['+', '/'], $data));
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param string $url
 | 
						|
     * @param array $params
 | 
						|
     * @param array $header
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public function PostJsonRequest(string $url, array $params, array $header = []): string
 | 
						|
    {
 | 
						|
        try {
 | 
						|
            $response = self::$client->post($url, [
 | 
						|
                RequestOptions::JSON => $params,
 | 
						|
                RequestOptions::HEADERS => $header
 | 
						|
            ]);
 | 
						|
            return $response->getBody()->getContents();
 | 
						|
        } catch (GuzzleException $exception) {
 | 
						|
            Log::error("SignatureRequestHttpPostByAes Error", [
 | 
						|
                'msg' => $exception->getMessage()
 | 
						|
            ]);
 | 
						|
            return '';
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 签名调用请求
 | 
						|
     *
 | 
						|
     * @param string $url
 | 
						|
     * @param $productID
 | 
						|
     * @param string $key
 | 
						|
     * @param string $vi
 | 
						|
     * @param array $params
 | 
						|
     * @return string|false
 | 
						|
     */
 | 
						|
    public function SignatureRequestHttpPostByAes(string $url, $productID, string $key, string $vi, array $params): string|false
 | 
						|
    {
 | 
						|
        $timestamp = time();
 | 
						|
        $form = array_merge($params, [
 | 
						|
            't' => $timestamp
 | 
						|
        ]);
 | 
						|
        ksort($form);
 | 
						|
        $sign = http_build_query($form);
 | 
						|
        try {
 | 
						|
            $signature = $this->AesEncrypt($sign, $key, $vi);
 | 
						|
            if ($signature === false) {
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
            $response = self::$client->post($url, [
 | 
						|
                RequestOptions::FORM_PARAMS => $form,
 | 
						|
                RequestOptions::HEADERS => [
 | 
						|
                    'Signature' => $signature,
 | 
						|
                    'Signature-Datetime' => date($this::DATE_FORMAT, $timestamp),
 | 
						|
                    'Product-ID' => $productID
 | 
						|
                ]
 | 
						|
            ]);
 | 
						|
        } catch (GuzzleException $exception) {
 | 
						|
            Log::error("SignatureRequestHttpPostByAes Error", [
 | 
						|
                'msg' => $exception->getMessage()
 | 
						|
            ]);
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
        return $response->getBody()->getContents();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 钉钉报警
 | 
						|
     *
 | 
						|
     * @param string $accessToken
 | 
						|
     * @param string $secret
 | 
						|
     * @param array $params
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public function DingTalkAlert(string $accessToken, string $secret, array $params): bool
 | 
						|
    {
 | 
						|
        try {
 | 
						|
            $timestamp = getMicroTime();
 | 
						|
            $signData = $timestamp . "\n" . $secret;
 | 
						|
            $hash_hmac = hash_hmac('sha256', $signData, $secret, true);
 | 
						|
            $sign = urlencode(base64_encode($hash_hmac));
 | 
						|
            $httpQuery = http_build_query([
 | 
						|
                'access_token' => $accessToken,
 | 
						|
                'sign' => $sign,
 | 
						|
                'timestamp' => $timestamp,
 | 
						|
            ]);
 | 
						|
            $response = self::$client->post('https://oapi.dingtalk.com/robot/send?' . $httpQuery, [
 | 
						|
                RequestOptions::JSON => $params,
 | 
						|
                RequestOptions::HEADERS => [
 | 
						|
                    'Content-Type' => 'application/json;charset=utf-8'
 | 
						|
                ]
 | 
						|
            ]);
 | 
						|
            $body = json_decode($response->getBody()->getContents(), true);
 | 
						|
            return ($body['errcode'] ?? false) === 0;
 | 
						|
        } catch (GuzzleException $exception) {
 | 
						|
            Log::error("DingTalkAlert Error", [
 | 
						|
                'msg' => $exception->getMessage()
 | 
						|
            ]);
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 有道翻译
 | 
						|
     *
 | 
						|
     * @param string $appID
 | 
						|
     * @param string $secret
 | 
						|
     * @param string $text
 | 
						|
     * @param string $lang
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    function YouDaoTextTranslator(string $appID, string $secret, string $text, string $lang): string
 | 
						|
    {
 | 
						|
        if ($lang === 'hk') {
 | 
						|
            $lang = 'zh-CHT';
 | 
						|
        }
 | 
						|
        $uuid4 = Uuid::uuid4();
 | 
						|
        $uuid = $uuid4->toString();
 | 
						|
        $timestamp = (string)now()->utc()->timestamp;
 | 
						|
        $len = mb_strlen($text);
 | 
						|
        if ($len > 20) {
 | 
						|
            $input = mb_substr($text, 0, 10) . $len . mb_substr($text, $len - 10, $len);
 | 
						|
            $signStr = $appID . $input . $uuid . $timestamp . $secret;
 | 
						|
        } else {
 | 
						|
            $signStr = $appID . $text . $uuid . $timestamp . $secret;
 | 
						|
        }
 | 
						|
        $sign = hash('sha256', $signStr);
 | 
						|
 | 
						|
        try {
 | 
						|
            $response = self::$client->post("https://openapi.youdao.com/api", [
 | 
						|
                RequestOptions::FORM_PARAMS => [
 | 
						|
                    'q' => $text,
 | 
						|
                    'from' => 'zh-CHS',
 | 
						|
                    'to' => $lang,
 | 
						|
                    'appKey' => $appID,
 | 
						|
                    'salt' => $uuid,
 | 
						|
                    'sign' => $sign,
 | 
						|
                    'signType' => "v3",
 | 
						|
                    'curtime' => $timestamp,
 | 
						|
                    'strict' => "true",
 | 
						|
                ],
 | 
						|
                RequestOptions::HEADERS => [
 | 
						|
                    'Content-Type' => 'application/x-www-form-urlencoded;charset=utf-8'
 | 
						|
                ]
 | 
						|
            ]);
 | 
						|
            $body = json_decode($response->getBody()->getContents(), true);
 | 
						|
            if (($body['errorCode'] ?? false) === '0') {
 | 
						|
                return $body['translation'][0];
 | 
						|
            }
 | 
						|
            return "";
 | 
						|
        } catch (GuzzleException $e) {
 | 
						|
            Log::error("YouDaoTextTranslator Error", [
 | 
						|
                'msg' => $e->getMessage()
 | 
						|
            ]);
 | 
						|
            return "";
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |