Commit e7f95991 by 杨昕

添加回收站功能

parent eaf283f4
Pipeline #10376 passed with stages
in 45 seconds
......@@ -378,15 +378,27 @@ class MediaModel extends Eloquent
throw new \Exception('媒资不存在!');
}
if ($request->token_username != 'offcn'){
if (getAdmin()->name != 'offcn'){
throw new \Exception("您无权限操作");
}
$session = \DB::connection('mongodb')->getMongoClient()->startSession();
$session->startTransaction();
$recycleId = RecycleModel::addRecycle($id);
if (!$recycleId){
$session->abortTransaction();
throw new \Exception('回收站保存失败');
}
$flag = MediaModel::destroy($id);
if (!$flag){
LogModel::addlog(["删除媒资失败"=> $id]);
$session->abortTransaction();
throw new \Exception('删除失败');
}
$session->commitTransaction();
}catch (\Exception $exception){
......
<?php
namespace App\Model;
use App\Exceptions\DatabaseException;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class RecycleModel extends Eloquent
{
/**
* mongodb collection 名字
*/
protected $collection = 'recycle';
protected $connection = 'mongodb'; //使用mongodb
protected $primaryKey = '_id';
public static function addRecycle($obj_id,$obj_type="media"){
$recycle = RecycleModel::where([
"obj_type" => $obj_type,
"obj_id" => $obj_id,
])->first();
if (!empty($recycle)){
throw new \Exception("回收站已存在");
}
$obj_model = 'App\\Model\\'.ucfirst($obj_type).'Model';
$model = new $obj_model();
$obj = $model->find($obj_id)->toArray();
if (empty($obj)){
throw new \Exception('数据不存在');
}
$data = [
'obj_id' => $obj_id,
'obj_type' => $obj_type,
'content' => json_encode($obj),
'create_time'=> date('Y-m-d H:i:s',time())
];
$id = RecycleModel::insertGetId($data);
if (!$id){
throw new \Exception('添加回收站失败');
}
return $id;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment