123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- <?php
- declare(strict_types=1);
- namespace App\Master\Framework\Extend;
- use App\Utils\LogUtil;
- use Hyperf\Coroutine\Coroutine;
- class BaseTask{
- //日志板块
- protected string $LOG_MODULE = 'BaseTask';
- protected string $message = 'error';
- protected mixed $data = [];
- public function __construct()
- {
- //日志统一写入
- LogUtil::getInstance('Task/');//设置日志存入通道
- Coroutine::defer(function () {
- LogUtil::close();//协程结束后统一写入
- });
- }
- public function execute(): bool
- {
- LogUtil::info('开始处理', $this->LOG_MODULE, 'do');
- // 根据参数处理具体逻辑
- // 通过具体参数获取模型等
- // 这里的逻辑会在 ConsumerProcess 进程中执行
- $res = $this->do();
- LogUtil::info('处理结果', $this->LOG_MODULE, 'do', [
- 'code' => $res,
- 'message' => $this->getMessage(),
- 'data' => $this->getData(),
- ]);
- return $res;
- }
- /**
- * 开始工作
- * @return true
- */
- protected function do(): bool
- {
- // 业务代码
- return $this->success('执行成功');
- }
- /**
- * 返回成功结果
- * @param string $message
- * @param mixed $data
- * @return bool
- */
- protected function success(string $message = 'success', mixed $data = []): bool
- {
- $this->message = $message;
- $this->data = $data;
- return true;
- }
- /**
- * 返回失败结果
- * @param string $message
- * @param mixed $data
- * @return bool
- */
- protected function error(string $message = 'error', mixed $data = []): bool
- {
- $this->message = $message;
- $this->data = $data;
- return false;
- }
- /**
- * 获取成功数据
- * @return mixed
- */
- public function getData(): mixed
- {
- return $this->data;
- }
- /**
- * 获取消息
- * @return string
- */
- public function getMessage(): string
- {
- return $this->message;
- }
- }
|