作者 邓超

1

... ... @@ -5,6 +5,7 @@
"license": "MIT",
"require": {
"php": "^8.0.2",
"ext-pdo": "*",
"phpmailer/phpmailer": "^6.7"
},
"require-dev": {
... ...
<?php
error_reporting(E_ERROR | E_NOTICE);
// 开启4个进程
define('WORKER_NUM',4);
// 开启100个协程
... ...
... ... @@ -21,64 +21,63 @@ class Home {
// $mail,$password,$imap,$smtp
$formData = app()->request(['email','password','imap','smtp']);
if(empty($formData['email']) || !preg_match("",$formData['email'])){
if(empty($formData['email']) || !preg_match("//",$formData['email'])){
app()->e('email_verify_error');
}
$validator = validator($formData,[
'email' => ['required','email'],
'password' => ['required','min:8','max:32'],
'imap' => ['required'],
'smtp' => ['required'],
],[
]);
if($validator->fails()){
return res()
->message($validator->errors()->first())
->status(400)
->toJson();
if(empty($formData['password'])){
app()->e('password_verify_error');
}
// host
$model = Email::_first($formData['email']);
if(!$model){
$model = new Email();
$model->email = $formData['email'];
if(empty($formData['imap'])){
app()->e('imap_verify_error');
}
$model->imap = $formData['imap'];
$model->smtp = $formData['smtp'];
$model->status = Email::STATUS_ACTIVE;
$model->password = @base64_encode($formData['password']);
try {
Mail::login($model->email,$model->password,$model->imap);
}catch (\Throwable $e){
return res()
->message($e->getMessage())
->status(400)
->toJson();
if(empty($formData['smtp'])){
app()->e('smtp_verify_error');
}
// 登录成功了,密码验证字段通过
$model->pwd_error = 0;
// 保存好邮箱
$model->save();
// 设置上id,方便后面使用
Mail::$client[$model->email]->setId($model->id);
// 开始同步文件夹
// $folder = Mail::syncFolder($model->email);
return res()
->data([
'token' => token_en($model->id.','.$model->email.','.time())
])
->toJson();
$data = db()->first(\Model\email::first($formData['email']));
app()->_json($data);
// // host
// $model = Email::_first($formData['email']);
// if(!$model){
// $model = new Email();
// $model->email = $formData['email'];
// }
//
// $model->imap = $formData['imap'];
// $model->smtp = $formData['smtp'];
// $model->status = Email::STATUS_ACTIVE;
// $model->password = @base64_encode($formData['password']);
//
// try {
// Mail::login($model->email,$model->password,$model->imap);
// }catch (\Throwable $e){
// return res()
// ->message($e->getMessage())
// ->status(400)
// ->toJson();
// }
//
// // 登录成功了,密码验证字段通过
// $model->pwd_error = 0;
// // 保存好邮箱
// $model->save();
//
// // 设置上id,方便后面使用
// Mail::$client[$model->email]->setId($model->id);
//
// // 开始同步文件夹
//// $folder = Mail::syncFolder($model->email);
//
// return res()
// ->data([
// 'token' => token_en($model->id.','.$model->email.','.time())
// ])
// ->toJson();
}
... ...
<?php
echo co::getCid();
\ No newline at end of file
... ...
... ... @@ -3,31 +3,29 @@
/**
* redis 驱动
* @return RedisPool
* @return \Lib\RedisPool
* @author:dc
* @time 2023/2/13 9:44
*/
function redis():\RedisPool {
function redis():\Lib\RedisPool {
return \RedisPool::instance(co::getCid());
return \Lib\RedisPool::instance(co::getCid());
}
/**
* 操作db
* @return DbPool
* @return \Lib\DbPool
* @author:dc
* @time 2023/2/13 9:43
* @time 2023/2/13 14:15
*/
function db():DbPool{
function db():\Lib\DbPool{
return DbPool::instance(co::getCid());
return \Lib\DbPool::instance(co::getCid());
}
function model($model){
}
... ... @@ -40,7 +38,7 @@ function model($model){
function logs($message,$filename=null){
@file_put_contents(
$filename ? $filename : LOG_PATH.'/error.log',
print_r($message,true),
print_r($message,true).PHP_EOL,
FILE_APPEND
);
}
... ...
... ... @@ -9,4 +9,9 @@ return [
'route_not_found' => '页面不存在',
'email_verify_error' => '邮箱验证失败',
'password_verify_error' => '密码必须',
'imap_verify_error' => 'imap服务器地址错误',
'smtp_verify_error' => 'smtp服务器地址错误',
'server_error' => '服务器异常',
];
\ No newline at end of file
... ...
... ... @@ -2,6 +2,12 @@
namespace Lib;
/**
* @author:dc
* @time 2023/2/13 15:07
* Class App
* @package Lib
*/
class App {
/**
... ... @@ -32,6 +38,13 @@ class App {
*/
private array $request = [];
/**
* TODO:: 如果debug打开,错误时会返回错误消息到前端
* @var bool
*/
public bool $debug = true;
/**
* 输出到前端的数据
* @var array
... ... @@ -101,10 +114,17 @@ class App {
// 记录日志
$filename = LOG_PATH.'/'.$app->nowDate().'.log';
logs(
$exception->getMessage().PHP_EOL.$exception->getTraceAsString(),
$app->nowDateTime().' '.$exception->getMessage().PHP_EOL.$exception->getTraceAsString(),
$filename
);
// 非 Err 错误类型
$app->data = [
'message' => $app->debug ? $exception->getMessage().PHP_EOL.$exception->getTraceAsString() : __('server_error'),
'status' => $exception->getCode() ? $exception->getCode() : 500,
];
}
}
}
... ... @@ -124,6 +144,8 @@ class App {
if (is_string($name)){
return $this->request[$name]??$default;
}
if(is_array($name)){
$data = [];
foreach ($this->request as $key=>$value){
if(in_array($key,$name)){
... ... @@ -132,6 +154,8 @@ class App {
}
return $data;
}
return null;
}
/**
... ...
<?php
namespace Lib;
/**
* db 池
* @author:dc
* @time 2023/2/13 15:03
* Class DbPool
* @package Lib
*/
class DbPool {
/**
* @var DbPool[]
* @var \Lib\DbPool[]
*/
static $instance = [];
... ... @@ -22,6 +31,18 @@ class DbPool {
return $this->client;
}
/**
* 表
* @var string
*/
private string $table;
/**
* 查询条件
* @var string|array
*/
private string | array $where;
public function __construct()
{
... ... @@ -33,7 +54,7 @@ class DbPool {
[
\PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
\PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'",
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
]
);
}catch (\PDOException $e){
... ... @@ -43,17 +64,64 @@ class DbPool {
}
public function query($sql){
/**
* 表
* @param $table
* @return $this
* @author:dc
* @time 2023/2/13 14:36
*/
public function table($table){
$this->table = $table;
return $this;
}
/**
* 条件
* @param string|array $where
* @return $this
* @author:dc
* @time 2023/2/13 14:38
*/
public function where(string|array $where){
$this->where = $where;
return $this;
}
/**
* @param $sql
* @param null $params
* @return false|\PDOStatement
* @author:dc
* @time 2023/2/13 14:41
*/
private function query($sql,$params=null){
}
public function get($sql){
}
public function first($sql){
/**
* 查询一条数据
* @param string|array $sql
* @return mixed|null
* @author:dc
* @time 2023/2/13 14:54
*/
public function first(string|array $sql){
$query = $this->client->prepare(is_array($sql) ? $sql[0] : $sql);
if($query->execute(is_array($sql) ? $sql[1] : null)){
return $query->fetch();
}
return null;
}
... ... @@ -68,9 +136,9 @@ class DbPool {
* @author:dc
* @time 2023/2/13 9:39
*/
public static function instance($cid){
public static function instance($cid=0){
if(empty(static::$instance[$cid])){
static::$instance[$cid] = new \RedisPool();
static::$instance[$cid] = new DbPool();
}
return static::$instance[$cid];
}
... ...
<?php
namespace Lib;
/**
* redis 链接池
* @author:dc
... ... @@ -9,7 +11,7 @@
class RedisPool {
/**
* @var RedisPool[]
* @var \Lib\RedisPool[]
*/
static $instance = [];
... ... @@ -168,13 +170,13 @@ class RedisPool {
/**
* @param $cid
* @return RedisPool
* @return \Lib\RedisPool
* @author:dc
* @time 2023/2/13 9:38
*/
public static function instance($cid){
if(empty(static::$instance[$cid])){
static::$instance[$cid] = new \RedisPool();
static::$instance[$cid] = new \Lib\RedisPool();
}
return static::$instance[$cid];
}
... ...
<?php
namespace Model;
/**
* @author:dc
* @time 2023/2/13 14:11
* Class email
*/
class email {
/**
* 通过email查询
* @param $email
* @return array
* @author:dc
* @time 2023/2/13 14:50
*/
public static function first($email){
return [
"select * from `emails` where `email` = ? limit 1",
[
$email
]
];
}
}
... ...