作者 邓超

1

... ... @@ -20,7 +20,7 @@ function start(){
$table->create();
// 初始时,进行一次统计
$table->set('etotal',['val'=> db()->count(\Model\email::count())]);
$table->set('etotal',['val'=> db()->count(\Model\emailSql::count())]);
// 进程管理器
$pm = new Process\Manager();
... ... @@ -30,7 +30,7 @@ function start(){
_echo("进程({$workerId})启动成功");
// 每10分钟统计一次邮箱数量
\Swoole\Timer::tick(600000,function () use (&$table){
$table->set('etotal',['val'=> db()->count(\Model\email::count())]);
$table->set('etotal',['val'=> db()->count(\Model\emailSql::count())]);
});
// 每2秒执行一次
... ...
... ... @@ -3,7 +3,8 @@
namespace Controller;
use Lib\Mail\Mail;
use Model\email;
use Model\emailSql;
use Model\listsSql;
/**
* @author:dc
... ... @@ -44,7 +45,7 @@ class Home {
Mail::login($formData['email'],$formData['password'],$formData['imap']);
// 是否存在
$id = db()->value(email::hasEmail($formData['email']));
$id = db()->value(emailSql::hasEmail($formData['email']));
$data = [
'password' => base64_encode($formData['password']),
... ... @@ -58,10 +59,10 @@ class Home {
if($id){
// 修改
$ret = db()->update(email::$table,$data,dbWhere(['id'=>$id]));
$ret = db()->update(emailSql::$table,$data,dbWhere(['id'=>$id]));
}else{
// 新增
$ret = db()->insert(email::$table,$data);
$ret = db()->insert(emailSql::$table,$data);
}
... ... @@ -76,4 +77,50 @@ class Home {
}
/**
* 邮件列表
* @author:dc
* @time 2023/2/17 14:12
*/
public function lists(){
$email = db()->first(emailSql::firstByToken(token()));
if(!$email){
app()->e('token_verify_notfound');
}
// 分页 页数
$page = app()->request('page',1,'intval');
$page = $page ? $page : 1;
$lists = db()->all(listsSql::lists($email['id'],$page));
app()->_json(listsPage($lists,100,1,30));
}
}
... ...
... ... @@ -128,7 +128,15 @@ function my_filter($value,$filter=null){
function dbWhere(array $where, string $ar = 'and'):string{
$sql = [];
foreach ($where as $f=>$v){
$sql[] = "`{$f}` = '{$v}'";
if(is_array($v)){
$v = array_map(function ($n){
return "'".addslashes($n)."'";
},$v);
$sql[] = "`{$f}` in (".implode(',',$v).")";
}else{
$sql[] = "`{$f}` = '".addslashes($v)."'";
}
}
return implode(' '.$ar.' ',$sql);
}
... ... @@ -149,10 +157,40 @@ function dbUpdate(array $data):string {
}
/**
* 获取token
* @return string
* @author:dc
* @time 2023/2/17 14:24
*/
function token():string {
$toekn = app()->request('_token_');
if(!preg_match("/^[a-z0-9]{32}$/",$toekn)){
app()->e('token_verify_error');
}
return $toekn;
}
/**
* 分页数据
* @param $item
* @param $total
* @param $page
* @param $limit
* @return array
* @author:dc
* @time 2023/2/17 15:05
*/
function listsPage($item,$total,$page,$limit){
return [
'item' => $item,
'page' => $page,
'total' => $total,
'limit' => $limit,
];
}
... ...
... ... @@ -16,4 +16,6 @@ return [
'server_error' => '服务器异常',
'login_error' => '登录失败',
'token_verify_error' => '令牌验证失败',
'token_verify_notfound' => '令牌验证失败.',
];
\ No newline at end of file
... ...
... ... @@ -120,7 +120,7 @@ class App {
// 非 Err 错误类型
$app->data = [
'message' => $app->debug ? $exception->getMessage().PHP_EOL.$exception->getTraceAsString() : __('server_error'),
'error_message' => $app->debug ? $exception->getMessage().PHP_EOL.$exception->getTraceAsString() : __('server_error'),
'status' => $exception->getCode() ? $exception->getCode() : 500,
];
}
... ... @@ -188,7 +188,7 @@ class App {
* @time 2023/2/13 10:57
*/
public function e($message,$status=400){
$this->data['message'] = __($message);
$this->data['error_message'] = __($message);
$this->data['status'] = $status;
throw new Err($message,500);
}
... ... @@ -202,9 +202,8 @@ class App {
* @time 2023/2/13 11:03
*/
public function _json($data){
$this->data['data'] = $data;
// $this->data['message'] = __($message);
throw new Err($message,200);
$this->data = $data;
throw new Err('',200);
}
... ...
... ... @@ -7,7 +7,7 @@ namespace Model;
* @time 2023/2/13 14:11
* Class email
*/
class email {
class emailSql {
public static $table = 'emails';
... ... @@ -23,6 +23,22 @@ class email {
return "select {$filed} from `".static::$table."` where `".(is_numeric($email) ? 'id' : 'email')."` = '{$email}' limit 1";
}
/**
* @param $token
* @return array
* @author:dc
* @time 2023/2/17 14:21
*/
public static function firstByToken($token):array {
return [
"select * from `".static::$table."` where `token` = ? limit 1",
[
$token
]
];
}
/**
* 统计邮箱的数量
* @return string
... ... @@ -50,30 +66,6 @@ class email {
];
}
/**
* 更新
* @param $data
* @param $where
* @return array
* @author:dc
* @time 2023/2/17 10:24
*/
public static function update($data,$where){
return [
"update `".static::$table."` set ". dbUpdate($data)
." where ".(is_numeric($where) ? '`id` = '.$where : dbWhere($where)),
$data
];
}
public function insert(){
}
... ...
<?php
namespace Model;
/**
* 邮件列表
* @author:dc
* @time 2023/2/17 14:15
* Class lists
* @package Model
*/
class listsSql {
/**
* 表
* @var string
*/
public static $table = 'lists';
/**
* 查询列表
* @param $email_id
* @param $p
* @param int $folder_id
* @return string
* @author:dc
* @time 2023/2/17 14:42
*/
public static function lists($email_id, $p, $folder_id = null){
$where = ['email_id'=>$email_id];
if($folder_id) $where['folder_id'] = $folder_id;
return "select * from `".static::$table."` where ".dbWhere($where)." order by `udate` desc limit 30 offset ".($p-1);
}
}
... ...
... ... @@ -4,5 +4,11 @@ use Controller\Home;
return [
'login' => [Home::class,'login']
/** 登录操作 @see Home::login() **/
'login' => [Home::class,'login'],
/** 邮件列表 @see Home::lists() **/
'mail/list' => [Home::class,'lists'],
];
\ No newline at end of file
... ...