138 lines
3.6 KiB
PHP
138 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\MessageNotice;
|
|
use app\common\model\Post;
|
|
use app\common\model\PostLike;
|
|
use app\common\validate\PostFavoriteValidate;
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
use app\common\model\PostFavorite;
|
|
use support\Request;
|
|
|
|
class PostFavoriteService
|
|
{
|
|
|
|
use CrudServiceTrait;
|
|
|
|
/**
|
|
* 指定当前要操作的模型
|
|
*/
|
|
protected string $modelClass = PostFavorite::class;
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected $validator = PostFavoriteValidate::class;
|
|
|
|
/**
|
|
* 获取验证器
|
|
*/
|
|
public function getValidator()
|
|
{
|
|
return new $this->validator;
|
|
}
|
|
|
|
/** 分页列表 */
|
|
public function serviceSelfList(Request $request, string $scene = 'self')
|
|
{
|
|
$result = $this->cqyProcess(ucfirst($scene), $request);
|
|
if ($result === false) {
|
|
$config = $this->buildQueryConfig($request, $scene);
|
|
$limit = (int)$request->get('size', 15);
|
|
$page = (int)$request->get('current', 1);
|
|
$result = $this->logicList($config, $limit, $page);
|
|
}
|
|
return $this->postProcess(ucfirst($scene), $result);
|
|
}
|
|
|
|
/**
|
|
* 构造 CrudUtil 查询配置
|
|
*/
|
|
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
|
|
{
|
|
$p = $request->all();
|
|
|
|
$config = [];
|
|
$config['model'] = $this->modelClass;
|
|
switch ($scene) {
|
|
case 'default':
|
|
$config['order']['id'] = 'desc';
|
|
break;
|
|
case 'self':
|
|
$config['where']['user_id'] = $request->user->uid;
|
|
$config['order']['id'] = 'desc';
|
|
break;
|
|
case 'index':
|
|
$config['order']['id'] = 'desc';
|
|
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;;
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* 添加后置处理
|
|
*/
|
|
public function afterServiceStore($res)
|
|
{
|
|
$data = [];
|
|
$data['post_id'] = $res['post_id'];
|
|
$data['receiver_user_id'] = Post::where(['id' => $data['post_id']])->value('user_id');
|
|
$data['sender_user_id'] = $res['user_id'];
|
|
$data['type'] = 2;
|
|
MessageNotice::create($data);
|
|
$post = Post::where(['id' => $data['post_id']])->first();
|
|
$post['favorite'] = $post['favorite']+1;
|
|
$post->save();
|
|
}
|
|
|
|
|
|
/**
|
|
* 添加后置处理
|
|
*/
|
|
public function serviceDelete(Request $request)
|
|
{
|
|
$param = $request->all();
|
|
|
|
if (empty($param['post_id']) && empty($param['user_id'])) {
|
|
throw new \Exception("参数错误");
|
|
}
|
|
|
|
$where = [];
|
|
$where['post_id'] = $param['post_id'];
|
|
$where['user_id'] = $request->user->uid;
|
|
$count = PostFavorite::where($where)->delete();
|
|
|
|
if ($count) {
|
|
$post = Post::where(['id' => $param['post_id']])->first();
|
|
$post['favorite'] = $post['favorite']-1;
|
|
$post['favorite'] = $post['favorite']<0?0:$post['favorite'];
|
|
$post->save();
|
|
}
|
|
|
|
return $count;
|
|
}
|
|
} |