作者 邓超

1

... ... @@ -200,7 +200,10 @@ class Home extends Base {
}
}
// 移动到的文件夹
$folder = app()->request('folder');
$to_folder = app()->request('folder');
if(empty($to_folder)){
app()->e('folder_move_error');
}
$data = db()->all(listsSql::first(dbWhere(['id'=>$mail_ids,'email_id'=>array_column($emails,'id')]),'`id`,`uid`,`email_id`,`folder_id`'));
if($data){
... ... @@ -218,6 +221,9 @@ class Home extends Base {
}
foreach ($uids as $eid=>$arr){
// 查询需要移动的文件夹
$to_origin_folder = db()->first(folderSql::first(['email_id'=>$eid,'folder'=>$to_folder]));
if($to_origin_folder){
foreach ($arr as $fid=>$uid){
// 查询目录
$folder = db()->first(folderSql::first($fid));
... ... @@ -226,16 +232,14 @@ class Home extends Base {
$mailInstance = new Mail($emails[$eid]['email'],base64_decode($emails[$eid]['password']),$emails[$eid]['imap']);
if($mailInstance->login()){
$mailInstance->move();
// TODO:: 这个过程无法保证原子性。没办法
// 先复制
$ret = $mailInstance->move(array_column($uid,'uid'),$folder['origin_folder'],$to_origin_folder['origin_folder']);
if($ret){
$uret = db()->update(listsSql::$table,['deleted'=>1],dbWhere(['id'=>array_column($uid,'id')]));
}
$mailInstance = null;
// 更新数据
// db()->update(listsSql::$table,[
// $d => $fv
// ],dbWhere([
// 'id' => array_column($uid,'id')
// ]));
}
}
... ... @@ -243,6 +247,9 @@ class Home extends Base {
}
}
}
}
... ...
... ... @@ -143,7 +143,10 @@ function dbWhere(array $where, string $ar = 'and'):string{
foreach ($where as $f=>$v){
if(is_array($v)){
$v = array_map(function ($n){
if (is_string($n)){
return "'".addslashes($n)."'";
}
return $n;
},$v);
if(count($v)===1){
// 只有一个值时就是 =
... ... @@ -153,7 +156,7 @@ function dbWhere(array $where, string $ar = 'and'):string{
}
}else{
$sql[] = "`{$f}` = '".addslashes($v)."'";
$sql[] = "`{$f}` = '". (is_string($v) ? addslashes($v): $v) ."'";
}
}
... ... @@ -318,6 +321,14 @@ function folderAlias($folder){
'Deleted Messages' => '回收站',
];
foreach ($folder_map as $key=>$name){
if(strtolower($folder) == strtolower($key)){
return $name;
}
}
return $folder_map[$folder]??$folder;
}
... ...
... ... @@ -32,6 +32,7 @@ return [
'folder_delete_exist_child' => '无法删除带有子文件夹的目录',
'folder_delete_exist_mail' => '无法删除存在邮件的目录',
'folder_delete_error' => '文件夹删除失败',
'folder_move_error' => '请选择目标文件夹',
'sync_request_param_error' => '同步请求参数异常',
... ...
... ... @@ -28,12 +28,24 @@ class DbPool {
* @time 2023/2/13 9:12
*/
public function getClient(){
if(!$this->client){
$this->connect();
}
return $this->client;
}
public function __construct()
{
$this->connect();
}
/**
* 连接sql
* @author:dc
* @time 2023/3/23 9:14
*/
public function connect(){
try {
$this->client = new \PDO(
'mysql:charset=utf8mb4;dbname='.DB_DATABASE.';host='.DB_HOST.';port='.DB_PORT,
... ... @@ -46,9 +58,8 @@ class DbPool {
]
);
}catch (\PDOException $e){
logs($e->getMessage());
}
}
... ... @@ -70,7 +81,7 @@ class DbPool {
$timer = microtime(true);
}
$query = $this->client->prepare($sql);
$query = $this->getClient()->prepare($sql);
$ret = $query->execute($params);
... ... @@ -165,7 +176,7 @@ class DbPool {
$query = $this->query([$sql,$data]);
if($query){
return $this->client->lastInsertId();
return $this->getClient()->lastInsertId();
}
return 0;
... ... @@ -266,7 +277,7 @@ class DbPool {
* @time 2023/2/17 11:35
*/
public function transaction(){
$this->client->beginTransaction();
$this->getClient()->beginTransaction();
}
/**
... ... @@ -275,7 +286,7 @@ class DbPool {
* @time 2023/2/17 11:35
*/
public function rollBack(){
$this->client->rollBack();
$this->getClient()->rollBack();
}
/**
... ... @@ -284,7 +295,7 @@ class DbPool {
* @time 2023/2/17 11:35
*/
public function commit(){
$this->client->commit();
$this->getClient()->commit();
}
... ...
... ... @@ -494,12 +494,66 @@ class Imap {
}
/**
* 复制
* @param array $uids
* @param string $folder
* @return bool
* @throws \Exception
* @author:dc
* @time 2023/3/22 16:35
*/
public function copy(array $uids,string $folder){
$uids = implode(',',$uids);
$res = $this->request("UID COPY {$uids} \"{$folder}\"");
if ($res[0] == 'ok'){
return true;
}
throw new \Exception("copy error:".end($res[1]));
}
/**
* 移动邮件
* @param array $uids
* @param string $folder
* @return bool
* @throws \Exception
* @author:dc
* @time 2023/3/21 14:55
* @time 2023/3/22 18:06
*/
public function move(){
public function move(array $uids,string $folder){
$uids = implode(',',$uids);
$res = $this->request("UID MOVE {$uids} \"{$folder}\"");
if ($res[0] == 'ok'){
return true;
}
throw new \Exception("move error:".end($res[1]));
}
/**
* 删除邮件
* @param array $uids
* @author:dc
* @time 2023/3/22 17:51
*/
public function delete(array $uids){
// $uids = implode(',',$uids);
//
// $res = $this->request("UID DELETE {$uids} ");
//
// if ($res[0] == 'ok'){
// return true;
// }
//
// throw new \Exception("delete error:".end($res[1]));
}
... ...
... ... @@ -328,23 +328,58 @@ class Mail {
}
/**
* 移动邮件,
* 复制
* @param $uids
* @param $folder
* @param $to_folder
* @return bool
* @throws \Exception
* @author:dc
* @time 2023/3/21 14:54
* @time 2023/3/22 16:38
*/
public function copy($uids,$folder,$to_folder){
// 选择目录
$status = $this->client->selectFolder($folder);
return $this->client->copy($uids,$to_folder);
}
/**
* 移动邮件
* @param $uids
* @param $folder
* @param $to_folder
* @return bool
* @throws \Exception
* @author:dc
* @time 2023/3/22 18:06
*/
public function move($uids,$folder,$to_folder){
// 选择目录
$status = $this->client->selectFolder($folder);
return $this->client->folderRename($uids,[Imap::FLAGS_DELETED],$recycle ? '+' : '-',true);
return $this->client->move($uids,$to_folder);
}
// /**
// * 删除
// * @param $uids
// * @param $folder
// * @return bool
// * @throws \Exception
// * @author:dc
// * @time 2023/3/22 17:52
// */
// public function delete($uids,$folder){
// // 选择目录
// $status = $this->client->selectFolder($folder);
//
// return $this->client->delete($uids);
// }
... ...
... ... @@ -43,9 +43,9 @@ class folderSql {
* @author:dc
* @time 2023/3/14 11:49
*/
public static function first(array|string $where):string {
public static function first(array|string $where,$filed = '*'):string {
$where = is_numeric($where) ? ['id'=>$where] : $where;
return "select * from `".self::$table."` where ".dbWhere($where);
return "select {$filed} from `".self::$table."` where ".dbWhere($where)." limit 1";
}
... ...