Merge branch 'feature/article' into develop
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
namespace app\apis\controller;
|
||||
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use app\apis\model\ArticleModel;
|
||||
use app\apis\service\ArticleService;
|
||||
use app\apis\validate\ArticleValidator;
|
||||
use app\mzl\common\controller\BaseController;
|
||||
|
||||
class ArticleController extends BaseController
|
||||
{
|
||||
|
||||
/**
|
||||
* 不需要验证token的接口
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedJwt = [];
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected mixed $validator = ArticleValidator::class;
|
||||
|
||||
/**
|
||||
* 返回业务 Service 实例
|
||||
*/
|
||||
protected function getService()
|
||||
{
|
||||
return new ArticleService(new ArticleModel());
|
||||
}
|
||||
|
||||
/**
|
||||
* 额外字段处理 (根据场景处理)
|
||||
* @param array $item
|
||||
* @param string $scene
|
||||
* @return array
|
||||
*/
|
||||
protected function postProcessItem(array $item, string $scene): array
|
||||
{
|
||||
|
||||
if ($scene === 'default' || $scene === 'show') {
|
||||
$item['publish_time'] = date('Y-m-d H:i:s', $item['publish_time']);
|
||||
|
||||
$item['cover_full'] = '';
|
||||
if ($item['cover']) {
|
||||
$item['cover_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($item['cover']);
|
||||
}
|
||||
foreach ($item['comment'] as &$comment) {
|
||||
|
||||
if ($comment['avatar']) {
|
||||
$comment['avatar_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($comment['avatar']);
|
||||
}
|
||||
}
|
||||
unset($comment);
|
||||
}
|
||||
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数预处理 (根据场景处理)
|
||||
* @param Request $request
|
||||
* @param string $scene
|
||||
* @return array
|
||||
*/
|
||||
protected function prepareParams(Request $request, string $scene): array
|
||||
{
|
||||
$params = parent::prepareParams($request, $scene);
|
||||
|
||||
if ($scene === 'default') {
|
||||
$params['filters'] = [];
|
||||
$params['filters'][] = ['field' => 'status', 'operator' => '=', 'value' => 1];
|
||||
$params['filters'][] = ['field' => 'locale', 'operator' => '=', 'value' => locale()];
|
||||
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
* @param Request $request 请求对象
|
||||
* @return Response 响应对象
|
||||
*/
|
||||
public function index(Request $request, string $scene = 'default'): Response
|
||||
{
|
||||
try {
|
||||
return parent::index($request, $scene);
|
||||
} catch (\Throwable $th) {
|
||||
return $this->errorResponse($th->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function detail(Request $request, int $id, string $scene = 'show'): Response
|
||||
{
|
||||
try {
|
||||
$request->setGet(['id'=>$id]);
|
||||
return parent::one($request, $scene);
|
||||
} catch (\Throwable $th) {
|
||||
return $this->errorResponse($th->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace app\apis\controller;
|
||||
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use app\apis\model\CommentModel;
|
||||
use app\apis\service\CommentService;
|
||||
use app\apis\validate\CommentValidator;
|
||||
use app\mzl\common\controller\BaseController;
|
||||
|
||||
class CommentController extends BaseController
|
||||
{
|
||||
|
||||
/**
|
||||
* 不需要验证token的接口
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedJwt = [];
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected mixed $validator = CommentValidator::class;
|
||||
|
||||
/**
|
||||
* 返回业务 Service 实例
|
||||
*/
|
||||
protected function getService()
|
||||
{
|
||||
return new CommentService(new CommentModel());
|
||||
}
|
||||
|
||||
/**
|
||||
* 额外字段处理 (根据场景处理)
|
||||
* @param array $item
|
||||
* @param string $scene
|
||||
* @return array
|
||||
*/
|
||||
protected function postProcessItem(array $item, string $scene): array
|
||||
{
|
||||
|
||||
if ($scene === 'default') {
|
||||
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数预处理 (根据场景处理)
|
||||
* @param Request $request
|
||||
* @param string $scene
|
||||
* @return array
|
||||
*/
|
||||
protected function prepareParams(Request $request, string $scene): array
|
||||
{
|
||||
$params = parent::prepareParams($request, $scene);
|
||||
|
||||
if ($scene === 'default') {
|
||||
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
* @param Request $request 请求对象
|
||||
* @return Response 响应对象
|
||||
*/
|
||||
public function index(Request $request, string $scene = 'default'): Response
|
||||
{
|
||||
try {
|
||||
return parent::index($request, $scene);
|
||||
} catch (\Throwable $th) {
|
||||
return $this->errorResponse($th->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace app\apis\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
/**
|
||||
* ArticleModel
|
||||
*/
|
||||
class ArticleModel extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'hs_article';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
/**
|
||||
* 模型的数据字段的保存格式。
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at', 'deleted_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace app\apis\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
/**
|
||||
* CommentModel
|
||||
*/
|
||||
class CommentModel extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'hs_comment';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
/**
|
||||
* 模型的数据字段的保存格式。
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'article_id', 'content', 'avatar', 'name', 'created_at', 'updated_at', 'deleted_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace app\apis\service;
|
||||
|
||||
use app\apis\model\ArticleModel;
|
||||
use app\mzl\common\services\BaseService;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class ArticleService extends BaseService
|
||||
{
|
||||
/**
|
||||
* 定义模型
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getModel(): string
|
||||
{
|
||||
return ArticleModel::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 多场景配置
|
||||
*/
|
||||
public function queryScenes(): array
|
||||
{
|
||||
return [
|
||||
'default' => function ($params) {
|
||||
return [
|
||||
'filters' => $params['filters'],
|
||||
'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'],
|
||||
'relations' => [
|
||||
'creater' => ['id', 'nickname',],
|
||||
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
|
||||
// 'translation' => ['model_id', 'name'],
|
||||
// 'brand.translation' => ['brand_id', 'name']
|
||||
],
|
||||
'sorts' => [
|
||||
['field' => 'is_top', 'direction' => 'desc'],
|
||||
['field' => 'sort', 'direction' => 'desc'],
|
||||
['field' => 'publish_time', 'direction' => 'desc'],
|
||||
],
|
||||
'pagination' => [
|
||||
'page' => $params['page'] ?? 1,
|
||||
'per_page' => $params['per_page'] ?? 10,
|
||||
]
|
||||
];
|
||||
},
|
||||
'show' => function ($params) {
|
||||
return [
|
||||
'filters' => [
|
||||
['field' => 'status', 'operator' => '=', 'value' => '1'],
|
||||
['field' => 'id', 'operator' => '=', 'value' => $params['id']]
|
||||
],
|
||||
'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'],
|
||||
'relations' => [
|
||||
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
|
||||
// 'translation' => ['model_id', 'name']
|
||||
],
|
||||
];
|
||||
}
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace app\apis\service;
|
||||
|
||||
use app\apis\model\CommentModel;
|
||||
use app\mzl\common\services\BaseService;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class CommentService extends BaseService
|
||||
{
|
||||
/**
|
||||
* 定义模型
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getModel(): string
|
||||
{
|
||||
return CommentModel::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 多场景配置
|
||||
*/
|
||||
public function queryScenes(): array
|
||||
{
|
||||
return [
|
||||
// 'default' => function ($params) {
|
||||
// $keyword = $params['keyword'] ?? '';
|
||||
// return [
|
||||
// 'filters' => [
|
||||
// ['field' => '***', 'operator' => '=', 'value' => '***']
|
||||
// ],
|
||||
// 'fields' => ['id'],
|
||||
// 'relations' => [],
|
||||
// 'custom' => function(Builder $q) use ($keyword) {
|
||||
// // 示例
|
||||
// if ($keyword) {
|
||||
// $q->where(function($q2) use($keyword) {
|
||||
// $q2->whereHas('***', function($q3) use($keyword) {
|
||||
// $q3->where('***', 'like', "%{$keyword}%");
|
||||
// })->orWhereHas('***', function($q4) use($keyword) {
|
||||
// $q4->where('***', 'like', "%{$keyword}%");
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
// },
|
||||
// 'sorts' => [
|
||||
// ['field' => 'created_at', 'direction' => 'desc']
|
||||
// ],
|
||||
// 'pagination' => [
|
||||
// 'page' => $params['page'] ?? 1,
|
||||
// 'per_page' => $params['per_page'] ?? 10,
|
||||
// ]
|
||||
// ];
|
||||
// }
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace app\apis\validate;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class ArticleValidator extends Validate
|
||||
{
|
||||
// 规则
|
||||
protected $rule = [
|
||||
|
||||
];
|
||||
|
||||
// 提示
|
||||
protected $message = [
|
||||
|
||||
];
|
||||
|
||||
// 场景
|
||||
protected $scene = [
|
||||
'store' => [
|
||||
|
||||
],
|
||||
|
||||
'update' => [
|
||||
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace app\apis\validate;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class CommentValidator extends Validate
|
||||
{
|
||||
// 规则
|
||||
protected $rule = [
|
||||
|
||||
];
|
||||
|
||||
// 提示
|
||||
protected $message = [
|
||||
|
||||
];
|
||||
|
||||
// 场景
|
||||
protected $scene = [
|
||||
'store' => [
|
||||
|
||||
],
|
||||
|
||||
'update' => [
|
||||
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -46,7 +46,13 @@ class ArticleController extends BaseController
|
||||
if ($item['cover']) {
|
||||
$item['cover_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($item['cover']);
|
||||
}
|
||||
foreach ($item['comment'] as &$comment) {
|
||||
|
||||
if ($comment['avatar']) {
|
||||
$comment['avatar_full'] = (new \app\mzl\modules\upload\service\OssService())->generatePrivateUrl($comment['avatar']);
|
||||
}
|
||||
}
|
||||
unset($comment);
|
||||
}
|
||||
|
||||
return $item;
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace app\console\controller;
|
||||
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use app\console\model\CommentModel;
|
||||
use app\console\service\CommentService;
|
||||
use app\console\validate\CommentValidator;
|
||||
use app\mzl\common\controller\BaseController;
|
||||
|
||||
class CommentController extends BaseController
|
||||
{
|
||||
|
||||
/**
|
||||
* 不需要验证token的接口
|
||||
* @var array
|
||||
*/
|
||||
protected $noNeedJwt = [];
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected mixed $validator = CommentValidator::class;
|
||||
|
||||
/**
|
||||
* 返回业务 Service 实例
|
||||
*/
|
||||
protected function getService()
|
||||
{
|
||||
return new CommentService(new CommentModel());
|
||||
}
|
||||
|
||||
/**
|
||||
* 额外字段处理 (根据场景处理)
|
||||
* @param array $item
|
||||
* @param string $scene
|
||||
* @return array
|
||||
*/
|
||||
protected function postProcessItem(array $item, string $scene): array
|
||||
{
|
||||
|
||||
if ($scene === 'default') {
|
||||
|
||||
}
|
||||
|
||||
return $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* 参数预处理 (根据场景处理)
|
||||
* @param Request $request
|
||||
* @param string $scene
|
||||
* @return array
|
||||
*/
|
||||
protected function prepareParams(Request $request, string $scene): array
|
||||
{
|
||||
$params = parent::prepareParams($request, $scene);
|
||||
|
||||
if ($scene === 'default') {
|
||||
|
||||
}
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
* @param Request $request 请求对象
|
||||
* @return Response 响应对象
|
||||
*/
|
||||
public function index(Request $request, string $scene = 'default'): Response
|
||||
{
|
||||
try {
|
||||
return parent::index($request, $scene);
|
||||
} catch (\Throwable $th) {
|
||||
return $this->errorResponse($th->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,11 @@ class ArticleModel extends Model
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
public function comment()
|
||||
{
|
||||
return $this->hasMany(CommentModel::class, 'article_id', 'id');
|
||||
}
|
||||
|
||||
public function creater()
|
||||
{
|
||||
return $this->belongsTo(AdminsModel::class, 'user_id','id');
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace app\console\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
/**
|
||||
* CommentModel
|
||||
*/
|
||||
class CommentAvatarModel extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'hs_comment_avatar';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
/**
|
||||
* 模型的数据字段的保存格式。
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = [];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
namespace app\console\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
/**
|
||||
* CommentModel
|
||||
*/
|
||||
class CommentModel extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'hs_comment';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
/**
|
||||
* 模型的数据字段的保存格式。
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'article_id', 'content', 'avatar', 'name', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -34,6 +34,7 @@ class ArticleService extends BaseService
|
||||
'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'],
|
||||
'relations' => [
|
||||
'creater' => ['id', 'nickname',],
|
||||
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
|
||||
// 'translation' => ['model_id', 'name'],
|
||||
// 'brand.translation' => ['brand_id', 'name']
|
||||
],
|
||||
@@ -56,6 +57,7 @@ class ArticleService extends BaseService
|
||||
],
|
||||
'fields' => ['id', 'locale', 'title', 'subtitle', 'cover', 'content', 'user_id', 'is_top', 'sort', 'status', 'publish_time', 'created_at', 'updated_at'],
|
||||
'relations' => [
|
||||
'comment' => ['id', 'article_id', 'content', 'avatar', 'name',],
|
||||
// 'translation' => ['model_id', 'name']
|
||||
],
|
||||
];
|
||||
@@ -71,6 +73,8 @@ class ArticleService extends BaseService
|
||||
}else{
|
||||
$params['publish_time'] = time();
|
||||
}
|
||||
$num = $params['num'] ?? 0;
|
||||
unset($params['num']);
|
||||
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
@@ -79,6 +83,10 @@ class ArticleService extends BaseService
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
|
||||
if($num > 0){
|
||||
CommentService::createComment($info->id,$params['content'],$num);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Throwable $th) {
|
||||
@@ -91,6 +99,8 @@ class ArticleService extends BaseService
|
||||
{
|
||||
$params['locale'] = locale();
|
||||
$params['publish_time'] = strtotime($params['publish_time']);
|
||||
$num = $params['num'] ?? 0;
|
||||
unset($params['num']);
|
||||
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
@@ -99,6 +109,9 @@ class ArticleService extends BaseService
|
||||
throw new BusinessException('提交失败');
|
||||
}
|
||||
|
||||
if($num > 0){
|
||||
CommentService::createComment($id,$params['content'],$num);
|
||||
}
|
||||
Db::commit();
|
||||
return true;
|
||||
} catch (\Throwable $th) {
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
|
||||
namespace app\console\service;
|
||||
|
||||
use app\console\model\CommentModel;
|
||||
use app\mzl\common\org\XunfeiSpark;
|
||||
use app\mzl\common\services\BaseService;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class CommentService extends BaseService
|
||||
{
|
||||
/**
|
||||
* 定义模型
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getModel(): string
|
||||
{
|
||||
return CommentModel::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* 多场景配置
|
||||
*/
|
||||
public function queryScenes(): array
|
||||
{
|
||||
return [
|
||||
// 'default' => function ($params) {
|
||||
// $keyword = $params['keyword'] ?? '';
|
||||
// return [
|
||||
// 'filters' => [
|
||||
// ['field' => '***', 'operator' => '=', 'value' => '***']
|
||||
// ],
|
||||
// 'fields' => ['id'],
|
||||
// 'relations' => [],
|
||||
// 'custom' => function(Builder $q) use ($keyword) {
|
||||
// // 示例
|
||||
// if ($keyword) {
|
||||
// $q->where(function($q2) use($keyword) {
|
||||
// $q2->whereHas('***', function($q3) use($keyword) {
|
||||
// $q3->where('***', 'like', "%{$keyword}%");
|
||||
// })->orWhereHas('***', function($q4) use($keyword) {
|
||||
// $q4->where('***', 'like', "%{$keyword}%");
|
||||
// });
|
||||
// });
|
||||
// }
|
||||
// },
|
||||
// 'sorts' => [
|
||||
// ['field' => 'created_at', 'direction' => 'desc']
|
||||
// ],
|
||||
// 'pagination' => [
|
||||
// 'page' => $params['page'] ?? 1,
|
||||
// 'per_page' => $params['per_page'] ?? 10,
|
||||
// ]
|
||||
// ];
|
||||
// }
|
||||
];
|
||||
}
|
||||
|
||||
public static function createComment($articleId, $articleContent, $num): array
|
||||
{
|
||||
|
||||
for ($i = 1; $i <= $num; $i++) {
|
||||
// 生成模拟用户
|
||||
$user = \app\mzl\common\utils\ForeignUserGenerator::generateRandomName();
|
||||
$user['avatar'] = \app\mzl\common\utils\ForeignUserGenerator::generateAvatar();
|
||||
|
||||
// 调用讯飞星火生成评论内容
|
||||
$param = [['role' => 'user', 'content' => $articleContent]];
|
||||
$commentContent = \app\mzl\common\org\XunfeiSpark::comment($param);
|
||||
|
||||
// 存入数据库
|
||||
$comment = new \app\console\model\CommentModel();
|
||||
$comment->content = $commentContent;
|
||||
$comment->article_id = $articleId;
|
||||
$comment->name = $user['name'] ?? 'Anonymous user';
|
||||
$comment->avatar = $user['avatar'] ?? '';
|
||||
$comment->save();
|
||||
}
|
||||
|
||||
return $comment->toArray();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace app\console\validate;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class CommentValidator extends Validate
|
||||
{
|
||||
// 规则
|
||||
protected $rule = [
|
||||
|
||||
];
|
||||
|
||||
// 提示
|
||||
protected $message = [
|
||||
|
||||
];
|
||||
|
||||
// 场景
|
||||
protected $scene = [
|
||||
'store' => [
|
||||
|
||||
],
|
||||
|
||||
'update' => [
|
||||
|
||||
]
|
||||
];
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace app\mzl\common\utils;
|
||||
|
||||
use app\console\model\CommentAvatarModel;
|
||||
|
||||
class ForeignUserGenerator
|
||||
{
|
||||
// 阿拉伯男性名字(迪拜)
|
||||
@@ -187,83 +189,14 @@ class ForeignUserGenerator
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成真人风格头像(使用本地服务)
|
||||
*
|
||||
* 组合使用以下服务:
|
||||
* 1. 随机用户API获取基础头像
|
||||
* 2. 使用ImageMagick进行本地处理增强真实感
|
||||
* 从hs_comment_avatar随机获取头像
|
||||
*/
|
||||
public static function generateAvatar(string $gender = 'random', string $ethnicity = 'random'): string
|
||||
public static function generateAvatar(): string
|
||||
{
|
||||
// 获取基础头像
|
||||
$baseAvatar = self::getRandomUserAvatar();
|
||||
|
||||
if (!$baseAvatar) {
|
||||
// 如果API不可用,使用备用方法
|
||||
return self::generateFallbackAvatar($gender, $ethnicity);
|
||||
}
|
||||
|
||||
// 增强头像真实感
|
||||
return self::enhanceAvatar($baseAvatar);
|
||||
$avatar = CommentAvatarModel::query()->inRandomOrder()->value('avatar');
|
||||
return $avatar ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 从随机用户API获取基础头像
|
||||
*/
|
||||
private static function getRandomUserAvatar(): ?string
|
||||
{
|
||||
$nationalities = ['gb', 'fr', 'de', 'es', 'ir', 'tr', 'us', 'ca'];
|
||||
$nationality = $nationalities[array_rand($nationalities)];
|
||||
|
||||
$url = "https://randomuser.me/api/?nat={$nationality}&inc=picture";
|
||||
|
||||
try {
|
||||
$response = file_get_contents($url);
|
||||
$data = json_decode($response, true);
|
||||
|
||||
if (isset($data['results'][0]['picture']['large'])) {
|
||||
return $data['results'][0]['picture']['large'];
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
// 记录错误或忽略
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增强头像真实感
|
||||
*/
|
||||
private static function enhanceAvatar(string $imageUrl): string
|
||||
{
|
||||
// 下载图像
|
||||
$imageData = file_get_contents($imageUrl);
|
||||
$tempInput = tempnam(sys_get_temp_dir(), 'avatar') . '.jpg';
|
||||
file_put_contents($tempInput, $imageData);
|
||||
|
||||
// 创建输出文件名
|
||||
$outputFile = 'avatars/' . uniqid('enhanced_') . '.jpg';
|
||||
|
||||
// 清理临时文件
|
||||
unlink($tempInput);
|
||||
|
||||
return $outputFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 备用头像生成方法
|
||||
*/
|
||||
private static function generateFallbackAvatar(string $gender, string $ethnicity): string
|
||||
{
|
||||
// 使用ThisPersonDoesNotExist服务
|
||||
$url = 'https://thispersondoesnotexist.com/';
|
||||
$imageData = file_get_contents($url);
|
||||
|
||||
$outputFile = 'avatars/' . uniqid('fallback_') . '.jpg';
|
||||
file_put_contents($outputFile, $imageData);
|
||||
|
||||
return $outputFile;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量生成用户数据
|
||||
|
||||
@@ -54,6 +54,10 @@ Route::group('/apis', function() {
|
||||
Route::any('/vehicle/new/sku/list', [\app\apis\controller\VehicleNewSkusController::class, 'index']);
|
||||
// 能源列表
|
||||
Route::any('/energy/preset', [\app\console\controller\EnergyController::class, 'presets']);
|
||||
|
||||
//文章列表
|
||||
Route::any('/article/list', [\app\console\controller\ArticleController::class, 'index']);
|
||||
Route::any('/article/{id}/detail', [\app\console\controller\ArticleController::class, 'detail']);
|
||||
});
|
||||
// ====== ====== ====== 网站 ====== ====== ======
|
||||
|
||||
|
||||
Reference in New Issue
Block a user