This commit is contained in:
zhang zhuo 2025-12-19 11:32:01 +08:00
parent d3b1927b3e
commit b902a5d65e
8 changed files with 142 additions and 36 deletions

5
.gitignore vendored
View File

@ -14,4 +14,7 @@ vendor/
.vscode/ .vscode/
/phpstan.neon /phpstan.neon
/phpunit.xml /phpunit.xml
composer.lock composer.lock
/static/export
/static/super
/static/upload

View File

@ -836,4 +836,13 @@ class System extends Base
$res = amModel::where(['mess_id' => $param['id'], 'account_id' => $this->accountId()])->delete(); $res = amModel::where(['mess_id' => $param['id'], 'account_id' => $this->accountId()])->delete();
return $this->toAjax($res); return $this->toAjax($res);
} }
#[DeleteMapping(path: "message/clear")]
#[Auth(needAuth: false)]
public function messageClear()
{
$param = Param::only(['type' => '']);
$param['account_id'] = $this->accountId();
return $this->toAjax(amModel::clearAll($param));
}
} }

View File

@ -272,12 +272,14 @@ class Tools extends Base
} }
#[GetMapping(path: "file/export")] #[GetMapping(path: "file/export")]
#[Auth(auth: "file:export")] #[Auth(needAuth: false)]
public function messageExport() public function messageExport()
{ {
$param = Param::only(["module" => "demo", "name" => "数据导出", "params" => []]); $param = Param::only(["module" => "demo", "name" => "数据导出", "params" => []]);
$res = aModel::add([ $res = aModel::add([
'account_id' => $this->accountId(), 'account_id' => $this->accountId(),
'account_type' => $this->account()['account_type'],
'belong_id' => $this->account()['belong_id'],
'name' => $param['name'], 'name' => $param['name'],
'module' => $param['module'], 'module' => $param['module'],
'params' => json_encode($param['params'], true), 'params' => json_encode($param['params'], true),
@ -285,7 +287,31 @@ class Tools extends Base
'create_time' => date("Y-m-d H:i:s") 'create_time' => date("Y-m-d H:i:s")
]); ]);
if (!$res) return $this->error("导出任务创建失败"); if (!$res) return $this->error("导出任务创建失败");
QueueClient::push("App\Job\FileExportJob", ['messageId' => $res, 'channels' => ['websocket', 'email']]); QueueClient::push("App\Job\FileExportJob", ['attachmentId' => $res]);
return $this->success("导出任务创建成功"); return $this->success("导出任务创建成功");
} }
#[GetMapping(path: "attachment/list")]
#[Auth(needAuth: false)]
public function attachmentList()
{
$param = Param::only(["limit" => 10]);
$param['account_id'] = $this->accountId();
return $this->success("列表接口", aModel::list($param));
}
#[DeleteMapping(path: "attachment/del")]
#[Auth(needAuth: false)]
public function attachmentDel()
{
$ids = $this->request->input("ids");
return $this->toAjax(aModel::del($ids));
}
#[DeleteMapping(path: "attachment/clear")]
#[Auth(needAuth: false)]
public function attachmentClear()
{
return $this->toAjax(aModel::where("account_id", $this->accountId())->delete());
}
} }

View File

@ -2,9 +2,12 @@
namespace App\Job; namespace App\Job;
use App\Utils\Excel;
use App\Utils\QueueClient;
use Hyperf\AsyncQueue\Job; use Hyperf\AsyncQueue\Job;
use App\Model\Attachment as aModel; use App\Model\Attachment as aModel;
use App\Model\Post as pModel; use App\Model\Post as pModel;
use App\Model\Message as mModel;
class FileExportJob extends Job class FileExportJob extends Job
{ {
@ -19,21 +22,37 @@ class FileExportJob extends Job
public function handle() public function handle()
{ {
try { try {
sleep(10);
$attach = aModel::find($this->attachmentId); $attach = aModel::find($this->attachmentId);
if (empty($attach)) return false; if (empty($attach)) return false;
// 参数解析
$params = $attach->params ? json_decode($attach->params, true) : [];
// 导出类型 默认excel
$type = "excel";
switch ($attach->module) { switch ($attach->module) {
case "post_export": case "post_export":
$res = pModel::exportData($attach->params); list($header, $data) = pModel::list($attach->belong_id, $attach->account_type, $params['post_name'] ?? "", true);
$res = true; $res = Excel::exportData($attach->name, $header, $data);
break; break;
default: default:
throw new \Exception("导出模块不存在"); throw new \Exception("导出模块不存在");
} }
aModel::edit([ if (!$res) {
'attachment_id' => $this->attachmentId, return aModel::edit(['attachment_id' => $this->attachmentId, 'status' => 2, 'fail_reason' => "导出失败"]);
'status' => $res ? 1 : 2, }
'fail_reason' => $res ? "" : "导出失败" aModel::edit(['attachment_id' => $this->attachmentId, 'status' => 1, 'type' => $type, 'path' => $res]);
// 导出成功,消息通知
$data['type'] = 'system';
$id = mModel::add([
'type' => 'attach',
'title' => $attach->name . " [执行成功]",
'content' => '执行成功',
'create_id' => $attach->account_id,
]); ]);
if ($id) {
QueueClient::push("App\Job\NotifyJob", ['messageId' => $id, 'channels' => ['websocket', 'email'], 'userIds' => [$attach->account_id]]);
}
return true;
} catch (\Exception $e) { } catch (\Exception $e) {
aModel::edit([ aModel::edit([
'attachment_id' => $this->attachmentId, 'attachment_id' => $this->attachmentId,

View File

@ -45,7 +45,7 @@ class AccountMessage extends Model
->orderBy("am.read_flag") ->orderBy("am.read_flag")
->orderByDesc("am.mess_id") ->orderByDesc("am.mess_id")
->select(['am.mess_id', 'am.read_flag', 'am.create_time', 'm.title', 'm.extra']) ->select(['am.mess_id', 'am.read_flag', 'am.create_time', 'm.title', 'm.extra'])
->paginate($param['limit']); ->paginate((int)$param['limit']);
} }
public static function read(array $param) public static function read(array $param)
@ -79,4 +79,15 @@ class AccountMessage extends Model
->select(['am.mess_id', 'am.create_time', 'm.title', 'm.content', 'm.extra', 'm.type', 'a.nickname']) ->select(['am.mess_id', 'am.create_time', 'm.title', 'm.content', 'm.extra', 'm.type', 'a.nickname'])
->first(); ->first();
} }
public static function clearAll(array $param)
{
return (new self())->setTable("am")
->from("account_message as am")
->leftJoin("message as m", "m.message_id", "=", "am.message_id")
->where("am.read_flag", 1)
->where("am.account_id", $param['account_id'])
->where("m.type", $param['type'])
->delete();
}
} }

View File

@ -5,18 +5,20 @@ declare(strict_types=1);
namespace App\Model; namespace App\Model;
/** /**
* @property int $attachment_id * @property int $attachment_id
* @property int $account_id * @property int $account_type
* @property string $name * @property int $belong_id
* @property string $module * @property int $account_id
* @property string $params * @property string $name
* @property string $type * @property string $module
* @property string $path * @property string $params
* @property int $status * @property string $type
* @property string $fail_reason * @property string $path
* @property string $deleted_at * @property int $status
* @property string $create_time * @property string $fail_reason
* @property string $update_time * @property string $deleted_at
* @property string $create_time
* @property string $update_time
*/ */
class Attachment extends Model class Attachment extends Model
{ {
@ -35,5 +37,28 @@ class Attachment extends Model
/** /**
* The attributes that should be cast to native types. * The attributes that should be cast to native types.
*/ */
protected array $casts = ['attachment_id' => 'integer', 'account_id' => 'integer', 'status' => 'integer']; protected array $casts = ['attachment_id' => 'integer', 'account_id' => 'integer', 'status' => 'integer', 'account_type' => 'integer', 'belong_id' => 'integer'];
public static function list(array $param)
{
$model = self::query();
if (isset($param['account_id']) && $param['account_id'] != '') {
$model = $model->where('account_id', $param['account_id']);
}
if (isset($param['name']) && $param['name'] != '') {
$model = $model->where('name', "like", "%{$param['name']}%");
}
if (isset($param['module']) && $param['module'] != '') {
$model = $model->where('module', $param['module']);
}
if (isset($param['type']) && $param['type'] != '') {
$model = $model->where('type', $param['type']);
}
if (isset($param['status']) && $param['status'] != '') {
$model = $model->where('status', $param['status']);
}
return $model->orderByDesc("attachment_id")
->select(["attachment_id", "name", "type", "path", "status", "fail_reason", "create_time"])
->paginate((int)$param['limit']);
}
} }

View File

@ -5,15 +5,15 @@ declare(strict_types=1);
namespace App\Model; namespace App\Model;
/** /**
* @property int $post_id * @property int $post_id
* @property int $account_type * @property int $account_type
* @property int $belong_id * @property int $belong_id
* @property string $post_name * @property string $post_name
* @property int $status * @property int $status
* @property int $rank * @property int $rank
* @property string $create_time * @property string $create_time
* @property string $update_time * @property string $update_time
* @property string $deleted_at * @property string $deleted_at
*/ */
class Post extends Model class Post extends Model
{ {
@ -34,23 +34,36 @@ class Post extends Model
*/ */
protected array $casts = ['post_id' => 'integer', 'account_type' => 'integer', 'belong_id' => 'integer', 'rank' => 'integer', 'status' => 'integer']; protected array $casts = ['post_id' => 'integer', 'account_type' => 'integer', 'belong_id' => 'integer', 'rank' => 'integer', 'status' => 'integer'];
public static function list(int $belong_id, int $account_type, string $post_name) public static function list(int $belong_id, int $account_type, string $post_name, bool $export = false): array
{ {
$model = self::where('belong_id', $belong_id) $model = self::where('belong_id', $belong_id)
->where('account_type', $account_type); ->where('account_type', $account_type);
if ($post_name != '') { if ($post_name != '') {
$model = $model->where('post_name', "like", "%$post_name%"); $model = $model->where('post_name', "like", "%$post_name%");
} }
return $model->orderByDesc("rank")->orderByDesc("post_id") $model = $model->orderByDesc("rank")->orderByDesc("post_id")
->select(["post_id", "post_name", "rank", "status", "create_time"]) ->select(["post_id", "post_name", "rank", "status", "create_time"]);
->get()->toArray(); if (!$export) {
// 非导出数据
return $model->get()->toArray();
}
$header = ['岗位名称', '状态', '创建时间'];
$data = [];
foreach ($model->get() as $row) {
$data[] = [
$row->post_name,
$row->status ? '启用' : '禁用',
$row->create_time->format('Y-m-d H:i:s'),
];
}
return [$header, $data];
} }
public static function options(int $belong_id, int $account_type) public static function options(int $belong_id, int $account_type)
{ {
return self::where('belong_id', $belong_id) return self::where('belong_id', $belong_id)
->where('account_type', $account_type) ->where('account_type', $account_type)
->where("status",1) ->where("status", 1)
->orderByDesc("rank")->orderByDesc("post_id") ->orderByDesc("rank")->orderByDesc("post_id")
->select(["post_id", "post_name"]) ->select(["post_id", "post_name"])
->get()->toArray(); ->get()->toArray();

View File

@ -49,7 +49,7 @@ class Excel
$sheet->fromArray($data, '', 'A2'); $sheet->fromArray($data, '', 'A2');
$sheet->getDefaultColumnDimension()->setWidth(12); $sheet->getDefaultColumnDimension()->setWidth(12);
$date = date("Ymd"); $date = date("Ymd");
$saveDir = BASE_PATH . '/static' . "/export/{$date}/"; $saveDir = BASE_PATH . '/static' . "/export/excel/{$date}/";
if (!is_dir($saveDir)) { if (!is_dir($saveDir)) {
mkdir($saveDir, 0777, true); mkdir($saveDir, 0777, true);
} }
@ -58,7 +58,7 @@ class Excel
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save($saveFile); $writer->save($saveFile);
if (is_file($saveFile)) { if (is_file($saveFile)) {
return config("app.domain") . "xlsx/{$date}/{$name}.xlsx"; return config("app.domain") . "/export/excel/{$date}/{$name}.xlsx";
} }
return false; return false;
} }