Files
bsh_info/app/common/service/PostService.php
T
2025-12-24 16:57:35 +08:00

242 lines
7.0 KiB
PHP

<?php
namespace app\common\service;
use app\common\model\PostCategory;
use app\common\model\PostMedia;
use app\common\model\Sku;
use app\common\model\User;
use app\common\model\UserStaff;
use app\common\validate\PostValidate;
use app\common\traits\crud\CrudServiceTrait;
use app\common\model\Post;
use support\exception\BusinessException;
use support\Redis;
use support\Request;
class PostService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = Post::class;
/**
* 验证器
*/
protected $validator = PostValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
/**
* 构造 CrudUtil 查询配置
*/
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
{
$p = $request->all();
$config = [];
$config['model'] = $this->modelClass;
switch ($scene) {
case 'default':
$config['where']['status'] = 1;
$config['order']['sort'] = 'desc';
$config['order']['id'] = 'desc';
break;
case 'index':
if (isset($p['category_id']) && $p['category_id']) {
$config['where']['category_id'] = $p['category_id'];
}
if (isset($p['scene']) && $p['scene']=='self') {
if(!empty($request->user) && !empty($request->user->uid)){
$config['where']['user_id'] = $request->user->uid;
}else{
$config['where']['user_id'] = -1;
}
}
if(isset($p['year']) && $p['year']){
$from = strtotime($p['year'].'-01-01');
$to = strtotime($p['year'].'-12-31 23:59:59');
$config['where']['created_at'] = [
'between', [$from, $to]
];
}
if (isset($p['keyword']) && $p['keyword']) {
$config['where']['content'] = ['like', '%'.$p['keyword'].'%'];
}
$config['where']['status'] = 1;
$config['order']['sort'] = 'desc';
if (empty($p['order'])) {
$p['order']='desc';
}
if (isset($p['sort_by']) && $p['sort_by'] == 'favorite') {
$config['order']['favorite'] = $p['order'];
} elseif (isset($p['sort_by']) && $p['sort_by'] == 'browse') {
$config['order']['browse'] = $p['order'];
} else {
$config['order']['id'] = $p['order'];
}
break;
case 'show':
$config['where']['id'] = $p['id'];
$config['where']['status'] = 1;
break;
case 'store':
break;
case 'update':
$config['where']['id'] = $p['id'];
break;
case 'destroy':
$config['where']['id'] = $p['id'];
break;
}
return $config;
}
/**
* 添加前置处理
*/
public function beforeServiceStore($data, Request $request)
{
$data['user_id'] = $request->user->uid;;
$user = User::where('id', $data['user_id'])->first();
if($user->is_release == 0){
$staff = UserStaff::where('name',$user->name)->where('sn',$user->sn)->first();
if(!empty($staff)){
$user->is_release = 1;
$user->save();
}else{
throw new BusinessException('无发布权限');
}
}
$media = serialize($data['media']);
redis::set('PostMedia_'.$data['user_id'],$media,30);
unset($data['media']);
$category = PostCategory::where('id',$data['category_id'])->first();
$models = explode(',',$data['model']);
foreach ($models as $key => $model) {
$code = (new SkuService)->generateUniqueCode();
Sku::firstOrCreate(['model' => $model], ['category_id' => $data['category_id'],
'product_name' => $category['name'],'code' => $code]);
}
return $data;
}
/**
* 添加后置处理
*/
public function afterServiceStore($res)
{
$media = redis::get('PostMedia_'.$res['user_id']);
$media = unserialize($media);
if(!is_array($media)){
$media = explode(',',$media);
}
foreach ($media as $v) {
PostMedia::create([
'post_id' => $res->id,
'media_type' => $this->getMediaTypeByUrl($v),
'url' => $v,
]);
}
}
/**
* 添加前置处理
*/
public function beforeServiceUpdate($data, Request $request)
{
$data['user_id'] = $request->user->uid;;
$media = serialize($data['media']);
redis::set('PostMedia_'.$data['user_id'],$media,30);
unset($data['media']);
return $data;
}
/**
* 添加后置处理
*/
public function afterServiceUpdate($res)
{
PostMedia::where('post_id', $res->id)->delete();
$media = redis::get('PostMedia_'.$res['user_id']);
$media = unserialize($media);
foreach ($media as $v) {
PostMedia::create([
'post_id' => $res->id,
'media_type' => $this->getMediaTypeByUrl($v['url']),
'url' => $v['url'],
]);
}
}
/**
* 添加前置处理
*/
public function beforeServiceShow($data, Request $request)
{
$post = Post::find($data['id']);
if(!$post){
$post->browse = $post->browse+1;
$post->save();
}
return $data;
}
function getMediaTypeByUrl(string $url): int
{
// 提取后缀
$ext = strtolower(pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION));
// 图片类型
$imageExt = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp'];
// 视频类型
$videoExt = ['mp4', 'mov', 'avi', 'wmv', 'flv', 'mkv'];
if (in_array($ext, $imageExt)) {
return 1; // 图片
}
if (in_array($ext, $videoExt)) {
return 2; // 视频
}
return 0; // 未知类型
} /**
* 添加后置处理
*/
public function serviceDelete(Request $request,$scene = 'admin')
{
$param = $request->all();
if (empty($param['id'])) {
throw new \Exception("参数错误");
}
$where = [];
$where['id'] = $param['id'];
$post = Post::where($where)->first();
if($scene == 'mini' && $post['user_id'] != $request->user->uid){
throw new \Exception("只能删除本人发布内容");
}
$count = $post->delete();
return $count;
}
}