178 lines
6.1 KiB
PHP
178 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace addons\shopro\controller\zy;
|
|
|
|
use think\Db;
|
|
use think\Exception;
|
|
use app\admin\model\zy\Club;
|
|
use app\admin\model\zy\Menber;
|
|
use think\exception\PDOException;
|
|
use app\admin\model\zy\circle\Likes;
|
|
use think\exception\ValidateException;
|
|
use app\admin\model\zy\circle\Circle as CircleModel;
|
|
use app\admin\model\zy\circle\Comment;
|
|
use app\admin\model\zy\link\Relation;
|
|
|
|
class Circle extends Base
|
|
{
|
|
|
|
public function index()
|
|
{
|
|
$params = $this->request->param();
|
|
|
|
$sub = CircleModel::alias('c')
|
|
->join([Likes::$tableName => 'l'], 'c.id = l.circle_id', 'LEFT')
|
|
->field("c.*,JSON_ARRAYAGG(JSON_OBJECT(
|
|
'id', l.id,
|
|
'user_id', l.user_id,
|
|
'nickname', l.nickname,
|
|
'avatar', l.avatar,
|
|
'gender', l.gender
|
|
)) AS likes")->group('c.id')->buildSql();
|
|
$query = Comment::alias('m')
|
|
->join([$sub => 'c'], 'c.id = m.circle_id', 'RIGHT')
|
|
->field("c.*,
|
|
JSON_ARRAYAGG(JSON_OBJECT(
|
|
'id', m.id,
|
|
'pid', m.pid,
|
|
'puser_id', m.puser_id,
|
|
'pnickname', m.pnickname,
|
|
'user_id', m.user_id,
|
|
'nickname', m.nickname,
|
|
'avatar', m.avatar,
|
|
'gender', m.gender,
|
|
'content', m.content,
|
|
'create_time', m.create_time
|
|
)) AS comment")->group('c.id');
|
|
if (isset($params['club_id'])) {
|
|
$query->where('c.club_id', $params['club_id']);
|
|
}
|
|
if (isset($params['user_id'])) {
|
|
$query->where('c.user_id', $params['user_id']);
|
|
}
|
|
if (isset($params['friend'])) {
|
|
$friend = Relation::where('user_id', $this->auth->id)->where('status', 'IN', explode(',', $params['friend']))->column('target_id');
|
|
$query->where('c.user_id', 'IN', $friend);
|
|
}
|
|
$res = $query->paginate($params['pageSize'] ?? 10);
|
|
$list = $res->items();
|
|
foreach ($list as &$r) {
|
|
$r['likes'] = json_decode($r['likes'], true);
|
|
if (count($r['likes']) == 1 && empty($r['likes'][0]['id'])) $r['likes'] = [];
|
|
$r['comment'] = buildTree(json_decode($r['comment'], true));
|
|
}
|
|
$this->success('Success', ['list' => $list, 'count' => $res->total()]);
|
|
}
|
|
|
|
public function view()
|
|
{
|
|
$model = CircleModel::get($this->request->param('id'));
|
|
if (empty($model)) {
|
|
$this->error(__('No rows were found'));
|
|
}
|
|
$this->success('Success', $model);
|
|
}
|
|
|
|
public function add()
|
|
{
|
|
$params = $this->request->param();
|
|
if (empty($params['content'])) {
|
|
$this->error('内容不能为空');
|
|
}
|
|
$user = auth_user();
|
|
$club = Menber::alias('m')->join([Club::$tableName => 'c'], 'm.club_id=c.id')
|
|
->field('c.name')
|
|
->where(['m.club_id' => $params['club_id'], 'm.user_id' => $user['id']])
|
|
->where('m.role', '>', 0)->find();
|
|
if (empty($club)) {
|
|
$this->error('你无权在此俱乐部发布');
|
|
}
|
|
$params['user_id'] = $user['id'];
|
|
$params['nickname'] = $user['nickname'];
|
|
$params['avatar'] = $user['avatar'];
|
|
$params['gender'] = $user['gender'];
|
|
$params['club_name'] = $club['name'];
|
|
$params['status'] = 1;
|
|
Db::startTrans();
|
|
try {
|
|
$result = (new CircleModel)->allowField(true)->save($params);
|
|
Db::commit();
|
|
} catch (ValidateException | PDOException | Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($result === false) {
|
|
$this->error('操作失败');
|
|
}
|
|
$this->success('Success');
|
|
}
|
|
|
|
// 点赞
|
|
public function like()
|
|
{
|
|
$params = $this->request->param();
|
|
$club = CircleModel::get($params['circle_id']);
|
|
if (empty($club)) {
|
|
$this->error('数据不存在');
|
|
}
|
|
$user = auth_user();
|
|
Db::startTrans();
|
|
try {
|
|
$like = Likes::get(['circle_id' => $params['circle_id'], 'user_id' => $user['id']]);
|
|
if (empty($like)) { // 点赞
|
|
(new Likes)->allowField(true)->save([
|
|
'circle_id' => $params['circle_id'],
|
|
'user_id' => $user['id'],
|
|
'nickname' => $user['nickname'],
|
|
'avatar' => $user['avatar'],
|
|
'gender' => $user['gender'],
|
|
]);
|
|
} else { // 取消点赞
|
|
$like->delete();
|
|
}
|
|
Db::commit();
|
|
} catch (ValidateException | PDOException | Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
$this->success('Success');
|
|
}
|
|
|
|
// 评论
|
|
public function comment()
|
|
{
|
|
$params = $this->request->param();
|
|
$club = CircleModel::get($params['circle_id']);
|
|
if (empty($club)) {
|
|
$this->error('数据不存在');
|
|
}
|
|
if (empty($params['content'])) {
|
|
$this->error('内容不能为空');
|
|
}
|
|
$pcomment = Comment::get($params['pid']);
|
|
if (!empty($params['pid']) && empty($pcomment)) {
|
|
$this->error('回复的评论不存在');
|
|
}
|
|
$user = auth_user();
|
|
Db::startTrans();
|
|
try {
|
|
(new Comment)->allowField(true)->save([
|
|
'circle_id' => $params['circle_id'],
|
|
'pid' => $params['pid'],
|
|
'puser_id' => $pcomment['user_id'] ?? 0,
|
|
'pnickname' => $pcomment['nickname'] ?? '',
|
|
'user_id' => $user['id'],
|
|
'nickname' => $user['nickname'],
|
|
'avatar' => $user['avatar'],
|
|
'gender' => $user['gender'],
|
|
'content' => $params['content'],
|
|
]);
|
|
Db::commit();
|
|
} catch (ValidateException | PDOException | Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
$this->success('Success');
|
|
}
|
|
}
|