-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnkioApi.php
More file actions
executable file
·88 lines (77 loc) · 2.42 KB
/
AnkioApi.php
File metadata and controls
executable file
·88 lines (77 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/*******************************************************************************
* Copyright (c) 2022. Ankio. All Rights Reserved.
******************************************************************************/
/**
* Package: app\utils
* Class AnkioApi
* Created By ankio.
* Date : 2023/4/28
* Time : 14:35
* Description :
*/
namespace library\login;
use cleanphp\base\Config;
use cleanphp\base\Json;
use cleanphp\base\Request;
use cleanphp\file\Log;
use library\http\HttpClient;
use library\http\HttpException;
class AnkioApi
{
private static ?AnkioApi $instance = null;
public SsoConfig $config;
public function __construct()
{
$this->config = new SsoConfig(Config::getConfig("notice"));
}
static function getInstance(): ?AnkioApi
{
if (self::$instance == null) {
self::$instance = new self();
}
return self::$instance;
}
public function request($url, $data = [])
{
try {
$headers = [
'Client-Ip' => Request::getClientIP(),
'User-Agent' => Request::getHeaderValue('User-Agent') ?? 'NO UA'
];
$data['t'] = time();
$data['appid'] = $this->config->id;
$response = HttpClient::init($this->config->url)->setHeaders($headers)->post(SignUtils::sign($data, $this->config->key), 'form')->send($url);
return Json::decode($response->getBody(), true);
} catch (HttpException $e) {
Log::record("API", $e->getMessage(), Log::TYPE_ERROR);
return ['code' => 500, 'msg' => '服务器错误'];
}
}
/**
* 调用API发邮件
* @param string $mailto
* @param string $subject
* @param string $content
* @param string $fromname
* @return mixed|true
*/
static function sendMail(string $mailto, string $subject, string $content, string $fromname): mixed
{
$data = self::getInstance()->request("api/mail/send", [
"mailto" => $mailto,
"subject" => $subject,
"fromname" => $fromname,
"content" => base64_encode($content)
]);
if ($data["code"] == 200) return true;
return $data["msg"];
}
static function ai($content)
{
$data = self::getInstance()->request("api/ai/msg", [
"content" => base64_encode($content)
]);
return [$data["data"], $data["code"]];
}
}