love_php/app/Services/MomentService.php
2026-04-02 09:20:51 +08:00

1635 lines
65 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Services;
use App\Models\RankHistory;
//阿里云SDK
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use App\Models\Rbac\RoleUser;
use App\Models\TestUser;
use Illuminate\Support\Facades\Auth;
use AlibabaCloud\Client\Request\Request;
use App\Jobs\SendEasySms;
use App\Jobs\SendIMPushMsg;
use App\Models\App\UserVote;
use App\Models\App\Vote;
use App\Models\App\Comment;
use App\Repositories\Eloquent\SmsRepository as Sms;
use App\Models\UnlikeMomentLog;
use App\Contracts\MomentContract;
use App\Models\User;
use App\Models\BannedHistory;
use App\Models\Moment;
use App\Models\ProfileCourtship;
use App\Models\ProfileMarriage;
use App\Models\Wechat;
use App\Models\CoinLog;
use App\Models\MomentTopic;
use App\Models\ComplaintHistory;
use App\Services\CommonUtilsService;
use App\Services\WechatService;
use App\Services\UserService;
use App\Http\Response\ResponseJson;
use App\Jobs\SendTemplateMsg;
use App\Models\Followable;
use App\Models\LinkingBlacklist;
use App\Models\Notice;
use App\Models\VerifyLog;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
class MomentService implements MomentContract
{
use ResponseJson;
protected $sms;
protected $CommonUtilsService;
protected $userCon;
public function __construct(Sms $sms)
{
$this->sms = $sms;
$this->commonUtil = new CommonUtilsService();
$this->userCon = new \App\Services\UserService();
}
/**
* 动态列表
* @return [type] [description]
*/
public function moments($limit = 15, $type = 'user')
{
$moments = Moment::with([
'topic' => function ($query) {
$query->where('is_show', 1);
},
'vote:id,title',
'user'
])
->whereHas('user', function ($q) {
$q->where('hidden_profile', 'NONE');
})->orderBy('is_top', 'desc');
;
// if(request()->input('order_comment')){
// $comments = \DB::select("SELECT * from ufutx_moments as m LEFT JOIN (SELECT commentable_id as moment_id, COUNT(*) as comment_count FROM `ufutx_comments` WHERE commentable_type = 'App\\Models\\Moment' GROUP BY commentable_id ) as c on c.moment_id = m.id ORDER BY `c`.`comment_count` DESC");
// }
$topic_id = request()->input('topic_id', 0);
$is_hot = request()->input('is_hot');
$user_id = request()->input('user_id');
if ($topic_id) {
$moments = $moments->where('topic_id', $topic_id);
}
if ($type == 'tourist') {
$arrs = [616, 58859, 161, 12441, 3480];
$moments = $moments->wherein('user_id', $arrs);
}
// //小程序屏蔽视频动态
// $prefix = (request()->route()->getAction())['prefix'];
// if ($prefix == 'api') {
// // $vote_moment_id = Moment::where('vote_id', '!=', 0)->pluck('id')->toArray();
// // $moments = $moments->whereNotIn('id', $vote_moment_id);
// $moments = $moments->where('vote_id', 0);
// }
$result = strstr(request()->route()->uri(), 'api/admin/moments');
if ($result) {//后台
$moments = $moments->orderBy('is_top', 'desc');
$belief = request()->input('belief');
if (!empty($belief)) {
$moments = $moments->whereHas('user', function ($sql) use ($belief) {
$sql->where('hidden_profile', 'NONE');
if ($belief == '基督教') {
$sql->where('belief', $belief);
} else {
$sql->where('belief', '<>', '基督教');
}
});
}
$is_audited = request()->input('is_audited');
if (is_numeric($is_audited)) {
$moments = $moments->where('is_audited', $is_audited);
}
$is_show = request()->input('is_show', 1);
if (is_numeric($is_show)) {
$moments = $moments->where('is_show', $is_show);
}
} else {//app、小程序
//过滤已校验出敏感词的动态id
$key = 'sensitive_moment';
if (Cache::has($key)) {
$source_ids = Cache::get($key);
} else {
$source_ids = VerifyLog::where('source_type', 'moment')->pluck('source_id')->toArray();
Cache::add($key, $source_ids, 60 * 5);
}
$moments = $moments->whereNotIn('id', $source_ids);
$moments = $moments->where(function ($sql) {
$sql->where('is_audited', 1)
->orWhere(function ($query) {
$query->where('user_id', auth()->id())->where('is_audited', 0);
});
})->where(function ($query) {
$query->where('is_show', 1)
->orWhere(function ($query) {
$query->where('user_id', auth()->id())->where('is_show', 0);
});
});
$moments = $moments->doesntHave('unlinkLogs');
}
$keyword = request()->input('keyword');
if ($keyword) {
$moments = $moments->where(function ($sql) use ($keyword) {
$sql->where('content', 'like', '%' . $keyword . '%')->orWhere('id', $keyword)
->orWhereHas('user', function ($query) use ($keyword) {
$query->where('nickname', 'like', '%' . $keyword . '%')
->orWhere('id', $keyword)
->orWhere('mobile', 'like', '%' . $keyword . '%');
});
});
}
if (is_numeric($is_hot) && $is_hot) {
$moments = $moments->where('is_hot', $is_hot);
}
if (is_numeric($user_id) && $user_id) {
$moments = $moments->where('user_id', $user_id);
}
$is_app_route = strstr(request()->route()->uri(), 'api/app/moments');
if ($is_app_route) {
$last_id = clone $moments;
$last_id = $last_id->where('created_at', '<', '2021-01-01 00:00:00')
->orderBy('id', 'desc')
->limit(1)
->value('id');
$moments = $moments->orderBy('id', 'desc')->paginate($limit);
foreach ($moments->items() as $item) {
if ($item->id == $last_id)
$item->last_id = 1;
$item = $this->momentDetail($item);
if (!empty($item->user) && !empty($item->user->mobile)) {
unset($item->user->mobile);
}
}
} else {
$moments = $moments->orderBy('id', 'desc')->paginate($limit);
foreach ($moments->items() as $item) {
$item = $this->momentDetail($item, $type);
}
}
return $moments;
}
/**
* 动态列表
* @return [type] [description]
*/
public function momentsV2($limit = 15, $type = 'user', $belief = null)
{
$moments = Moment::with([
'topic' => function ($query) {
$query->where('is_show', 1);
},
'vote:id,title',
'user'
])
->whereHas('user', function ($q) use ($belief) {
$q->where('hidden_profile', 'NONE');
if ($belief) {
if ($belief == '基督教') {
$q->where('belief', $belief);
} else {
$q->where('belief', '<>', '基督教');
}
}
})
->orderBy('is_top', 'desc');
$topic_id = request()->input('topic_id', 0);
$is_hot = request()->input('is_hot');
$user_id = request()->input('user_id');
$versions = request()->input('versions');
if ($topic_id) {
$moments = $moments->where('topic_id', $topic_id);
}
if ($type == 'tourist') {
$arrs = [616, 58859, 161, 12441, 3480];
$moments = $moments->wherein('user_id', $arrs);
}
$result = strstr(request()->route()->uri(), 'api/admin/moments');
if ($result) {//后台
$is_audited = request()->input('is_audited');
if (is_numeric($is_audited)) {
$moments = $moments->where('is_audited', $is_audited);
}
$is_show = request()->input('is_show');
if (is_numeric($is_show)) {
$moments = $moments->where('is_show', $is_show);
}
} else {//app、小程序
//过滤已校验出敏感词的动态id
$key = 'sensitive_moment';
if (Cache::has($key)) {
$source_ids = Cache::get($key);
} else {
$source_ids = VerifyLog::where('source_type', 'moment')->pluck('source_id')->toArray();
Cache::add($key, $source_ids, 60 * 5);
}
$moments = $moments->whereNotIn('id', $source_ids)->where(function ($sql) {
$sql->where('is_audited', 1)
->orWhere(function ($query) {
$query->where('user_id', auth()->id())->where('is_audited', 0);
});
})->where(function ($query) {
$query->where('is_show', 1)
->orWhere(function ($query) {
$query->where('user_id', auth()->id())->where('is_show', 0);
});
});
$moments = $moments->doesntHave('unlinkLogs');
}
$keyword = request()->input('keyword');
if ($keyword) {
$moments = $moments->where(function ($sql) use ($keyword) {
$sql->where('content', 'like', '%' . $keyword . '%')
->orWhereHas('user', function ($query) use ($keyword) {
$query->where('nickname', 'like', '%' . $keyword . '%')
->orWhere('mobile', 'like', '%' . $keyword . '%');
});
});
}
if (is_numeric($is_hot) && $is_hot) {
$moments = $moments->where('is_hot', $is_hot);
}
if (is_numeric($user_id) && $user_id) {
$moments = $moments->where('user_id', $user_id);
}
$is_app_route = strstr(request()->route()->uri(), 'api/app/moments');
if ($is_app_route) {
$last_id = clone $moments;
$last_id = $last_id->where('created_at', '<', '2021-01-01 00:00:00')
->orderBy('id', 'desc')
->limit(1)
->value('id');
$moments = $moments->orderBy('id', 'desc')->paginate($limit);
foreach ($moments->items() as $item) {
if ($item->id == $last_id)
$item->last_id = 1;
$item = $this->momentDetail($item);
}
} else {
$moments = $moments->orderBy('id', 'desc')->paginate($limit);
foreach ($moments->items() as $item) {
$item = $this->momentDetail($item, $type);
}
}
return $moments;
}
/**
* 动态列表 按评论降序
* @return [type] [description]
*/
public function momentsV3($limit = 15, $type = 'user')
{
$page = request()->input('page', 1);//第几页
$size = request()->input('size', 15);//每页数量
$sql = "SELECT * from ufutx_moments as m LEFT JOIN (SELECT commentable_id as moment_id, COUNT(*) as comment_count FROM `ufutx_comments` WHERE commentable_type = 'App\\\\Models\\\\Moment' GROUP BY commentable_id ) as `c` on c.moment_id = m.id ORDER BY c.comment_count DESC limit " . ($page - 1) * $size . ',15';
$results = DB::select($sql);
$q = "SELECT * from ufutx_moments as m LEFT JOIN (SELECT followable_id as moment_id, COUNT(*) as like_count FROM `ufutx_followables` WHERE followable_type = 'App\\\\Models\\\\Moment' GROUP BY followable_id ) as `f` on f.moment_id = m.id ORDER BY f.like_count DESC";
$querys = DB::select($q);
foreach ($results as $result) {
$user = User::where('id', $result->user_id)->select('id', 'nickname', 'app_avatar', 'circle_avatar', 'mobile')->first();
$result->user = $user;
$result->photos = json_decode($result->photos, true);
$result->like_count = 0;
foreach ($querys as $query) {
if ($result->moment_id == $query->moment_id) {
$result->like_count = $query->like_count;
}
}
}
return $results;
}
/**
* 动态列表 按点赞降序
* @return [type] [description]
*/
public function momentsV4($limit = 15, $type = 'user')
{
$page = request()->input('page', 1);//第几页
$size = request()->input('size', 15);//每页数量
$sql = "SELECT * from ufutx_moments as m LEFT JOIN (SELECT commentable_id as moment_id, COUNT(*) as comment_count FROM `ufutx_comments` WHERE commentable_type = 'App\\\\Models\\\\Moment' GROUP BY commentable_id ) as `c` on c.moment_id = m.id ORDER BY c.comment_count DESC";
$results = DB::select($sql);
$q = "SELECT * from ufutx_moments as m LEFT JOIN (SELECT followable_id as moment_id, COUNT(*) as like_count FROM `ufutx_followables` WHERE followable_type = 'App\\\\Models\\\\Moment' GROUP BY followable_id ) as `f` on f.moment_id = m.id ORDER BY f.like_count DESC limit " . ($page - 1) * $size . ',15';
$querys = DB::select($q);
foreach ($querys as $query) {
$user = User::where('id', $query->user_id)->select('id', 'nickname', 'app_avatar', 'circle_avatar', 'mobile')->first();
$query->user = $user;
$query->photos = json_decode($query->photos, true);
$query->comment_count = 0;
foreach ($results as $result) {
if ($query->moment_id == $result->moment_id) {
$query->comment_count = $result->comment_count;
}
}
}
return $querys;
}
/**
* 发表动态
*/
public function addMoment($user_id, $channel = 'APP')
{
try {
//查看是否被禁言
$date = date('Y-m-d H:i:s');
$history = BannedHistory::where('user_id', $user_id)->where('type', 'moment')->first();
if (!empty($history)) {
if ($history->end_time >= $date && $history->start_time <= $date) {
return ['code' => 1, 'msg' => '你被禁止发动态至' . $history->end_time];
}
}
$content = request()->input('content');
//敏感词汇过滤
$result = \CommonUtilsService::textContentCecurity([$content]);
if ($result && $result['result']) {
return ['code' => 1, 'msg' => '您填写的内容【' . $result['context'] . "】:" . $result['result'] . ",请重新输入"];
}
$photos = request()->input('photos') ?: [];
$aliyun_video_id = request()->input('aliyun_video_id') == 'null' ? '' : request()->input('aliyun_video_id');
$topic_id = request()->input('topic_id') ?: 0;
$location_longitude = request()->input('location_longitude');
$location_latitude = request()->input('location_latitude');
$address = request()->input('address');
$is_anonymised = request()->input('is_anonymised', 0);
$vote_id = request()->input('vote_id', 0);
$is_hot = request()->input('is_hot') ?: 0;
if ($is_anonymised)
return ['code' => 1, 'msg' => '暂不支持匿名发表'];
//获取视频信息
if (!empty($aliyun_video_id)) {
$result = $this->getVodInfo($aliyun_video_id);
if (is_array($result)) {
$video_height = $result['Mezzanine']['Height'];
$video_width = $result['Mezzanine']['Width'];
}
}
$moment = auth()->user()->moments()->create([
'user_id' => $user_id,
'content' => $content,
'photos' => json_encode($photos),
'aliyun_video_id' => $aliyun_video_id,
'video_height' => !empty($video_height) ? $video_height : 0,
'video_width' => !empty($video_width) ? $video_width : 0,
'topic_id' => $topic_id,
'location_latitude' => $location_latitude,
'location_longitude' => $location_longitude,
'address' => $address,
'is_anonymised' => $is_anonymised,
'vote_id' => $vote_id,
'channel' => $channel,
'is_hot' => $is_hot,
'is_audited' => 1,
]);
return $moment;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
return;
}
/**发表动态 领取会员 */
public function addMomentV2($user, $channel = 'APP')
{
try {
//code 1 成功 2 失败 3 成功但是***
$res_data = ['code' => 1, 'msg' => '异常'];
//查看是否被禁言
$date = date('Y-m-d H:i:s');
$history = BannedHistory::where('user_id', $user->id)->where('type', 'moment')->first();
if (!empty($history)) {
if ($history->end_time >= $date && $history->start_time <= $date) {
return ['code' => 1, 'msg' => '你被禁止发动态至' . $history->end_time];
}
}
if ($user->is_photo_audited != 1) {
return ['code' => 1, 'msg' => "头像未通过审核,禁止发布动态"];
}
if ($user->is_real_approved != 1) {
return ['code' => 1, 'msg' => "真人认证未通过审核,禁止发布动态"];
}
$content = request()->input('content');
$photos = request()->input('photos') ?: [];
if ($user->id != 69938) { //排除小天使账号
//敏感词汇过滤
$result = \CommonUtilsService::checkoutTextArray([$content]);
if ($result['code'] == 1) {
return ['code' => 1, 'msg' => $result['cause']];
}
if (count($photos)) {
$result = \CommonUtilsService::imageContentCecurity($photos);
if ($result && isset($result['result']))
return ['code' => 1, 'msg' => $result['result']];
;
}
}
$aliyun_video_id = request()->input('aliyun_video_id') == 'null' ? '' : request()->input('aliyun_video_id');
$topic_id = request()->input('topic_id') ?: 0;
$location_longitude = request()->input('location_longitude');
$location_latitude = request()->input('location_latitude');
$address = request()->input('address');
$is_anonymised = request()->input('is_anonymised', 0);
$vote_id = request()->input('vote_id', 0);
$is_hot = request()->input('is_hot') ?: 0;
if ($is_anonymised)
return ['code' => 1, 'msg' => '暂不支持匿名发表'];
$aliyun_video_state = 0;
//获取视频信息
if (!empty($aliyun_video_id)) {
$result = $this->getVodInfo($aliyun_video_id);
if (is_array($result)) {
$video_height = $result['Mezzanine']['Height'];
$video_width = $result['Mezzanine']['Width'];
}
$aliyun_video_state = 1;
}
$data['code'] = 0;
$data['msg'] = '';
$data['data'] = ['moment' => '', 'give_hint' => 0];
if ($topic_id > 0) {
$momentTopic = MomentTopic::with('momentTopicsActivities')
->where('id', $topic_id)->first();
if (!$momentTopic) {
return ['code' => 1, 'msg' => '话题不存在'];
}
if ($momentTopic->momentTopicsActivities) {
$presentTime = time();
if (
strlen($content) >= $momentTopic->momentTopicsActivities->tc_moment_content_count
&& (count($photos) >= $momentTopic->momentTopicsActivities->tc_moment_photos_count || $aliyun_video_state == 1)
&& $presentTime >= strtotime($momentTopic->momentTopicsActivities->start_time)
&& $presentTime <= strtotime($momentTopic->momentTopicsActivities->end_time)
) {
if ($momentTopic->momentTopicsActivities->award_member_days >= 1) {
$other_type = RankHistory::TOPICACTIVITIESPARTGIVE . '_' . $topic_id;
$superCount = $user->getSuperCount($user->id, RankHistory::TOPICACTIVITIESPARTGIVE, $other_type);
// dd($superCount);
if ($superCount == 0) {
$data['data']['give_hint'] = 1;
$user->addSuperRank($momentTopic->momentTopicsActivities->award_member_days, $month = 0, $type = RankHistory::TOPICACTIVITIESPARTGIVE, $other_type);
}
}
}
}
}
$moment = auth()->user()->moments()->create([
'user_id' => $user->id,
'content' => $content,
'photos' => json_encode($photos),
'aliyun_video_id' => $aliyun_video_id,
'video_height' => !empty($video_height) ? $video_height : 0,
'video_width' => !empty($video_width) ? $video_width : 0,
'topic_id' => $topic_id,
'location_latitude' => $location_latitude,
'location_longitude' => $location_longitude,
'address' => $address,
'is_anonymised' => $is_anonymised,
'vote_id' => $vote_id,
'channel' => $channel,
'is_hot' => $is_hot,
'is_audited' => 1,
]);
$data['data']['moment'] = $moment;
//给关注的人发送公众号消息推送
$targe_user_ids = Followable::where('followable_id', $user->id)->where('relation', 'follow')->pluck('user_id')->toArray();
foreach ($targe_user_ids as $key => $targe_user_id) {
if ($user->id == 59505)
break;
if ($targe_user_id == $user->id)
continue;
$targe_user = User::find($targe_user_id);
if (empty($targe_user))
continue;
$param = $this->getAddMomentParam($user, $targe_user);//user被关注 targe_user主动关注接收通知
if (empty($param))
continue;
SendTemplateMsg::dispatch($param)->onQueue('template_message');
}
return $data;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
return;
}
public function getAddMomentParam($user, $targe_user)
{
$openid = $targe_user->wechat ? $targe_user->wechat->official_openid : null;
if (empty($openid)) {
$openid = $targe_user->viewer ? $targe_user->viewer->openid : null;
}
//关注时间
$foll = Followable::where('user_id', $targe_user->id)->where('followable_id', $user->id)->where('relation', 'follow')->first();
$foll_time = $foll->created_at->toDateTimeString();
if ($openid) {
$param["touser"] = $openid;
$param['template_id'] = config('wechat.tpls.follow_user_add_moment');
$param['url'] = env('APP_URL') . '/h5';
$param['miniprogram'] = [
'appid' => config('wechat.mini_program.app_id'),
'pagepath' => 'pages/home/PersonalDynamic?id=' . $user->id,
];
$param['data'] = [
"first" => '您关注的会员发布动态啦!',
'keyword1' => $user->nickname,
'keyword2' => !empty($foll_time) ? date('Y-m-d H:i', strtotime($foll_time)) : date('Y-m-d H:i'),//关注时间
'remark' => '记得跟Ta互动一下哦'
];
return $param;
}
return [];
}
public function sendMomentNotice($type, $moment, $user, $moment_user)
{
if ($type == 'like') {
$first = '您的动态被人点赞了!';
} elseif ($type == 'comment') {
$first = '您的动态被人评论了!';
} elseif ($type == 'reply_comment') {
$first = '您的动态被人回复了!';
} else {
return;
}
//公众号通知
$param["touser"] = $moment_user->wechat ? $moment_user->wechat->official_openid : null;
$param['template_id'] = config('wechat.tpls.follow_user_add_moment');
$param['url'] = env('APP_URL') . '/h5';
$param['miniprogram'] = [
'appid' => config('wechat.mini_program.app_id'),
'pagepath' => 'pages/dynamic/feedDetail?id=' . $moment->id,
];
$param['data'] = [
"first" => $first,
'keyword1' => $user->nickname,
'keyword2' => date("Y-m-d H:i:s"),//关注时间
'remark' => '记得跟Ta互动一下哦'
];
SendTemplateMsg::dispatch($param)->onQueue('template_message');
}
/**
* 动态详情
* @param [type] $moment_id [description]
* @return [type] [description]
*/
public function moment($moment_id)
{
try {
$moment = Moment::with('user.profileCourtship', 'topic', 'user:id,sex,nickname,photo,app_avatar,mobile,belief', 'vote:id,title')->where('id', $moment_id)->first();
if (empty($moment))
return ['code' => 1, 'msg' => '该动态已被删除'];
$moment = $this->momentDetail($moment);
return $moment;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function momentDetail($moment, $type = 'user')
{
try {
//点赞数
$momentLikerCount = $this->momentLikerCount($moment);
$moment->momentLikerCount = $momentLikerCount;
//评论数
$momentCommentCount = $this->momentCommentCount($moment);
$moment->momentCommentCount = $momentCommentCount;
//视频
$play_url = '';
$cover_url = '';
if ($moment->aliyun_video_id) {
$result = \AliyunService::getPlayInfo($moment->aliyun_video_id, $play_domain = 'vod-mp.ufutx.com');
if (empty($result))
throw new \Exception("获取播放地址失败", 1);
$play_url = $result['PlayInfoList']['PlayInfo'][0]['PlayURL'];
$cover_url = $result['VideoBase']['CoverURL'] ?? '';
}
$moment->cover_url = $cover_url;
$moment->play_url = $play_url;
unset($moment->user);
$moment->isLkerMoment = 0;
//是否点赞
if ($type != 'tourist') {
$moment->isLkerMoment = $this->isLkerMoment($moment);
}
//是否是自己的动态
$moment->is_self = auth()->id() == $moment->user_id ? 1 : 0;
$user = $moment->user;
if (!empty($user)) {
$moment->user->age = \App\Facades\CommonUtilsService::getAge($user->profileCourtship ? $user->profileCourtship->birthday : null);
$moment->user->photo = $user->userAvatar();
} else {
$moment->user->age = null;
$moment->user->photo = null;
}
if ($user && $user->profileCourtship) {
$moment->user->city = $user->profileCourtship->city;
$moment->user->stature = $user->profileCourtship->stature;
$moment->user->degree = $user->profileCourtship->degree;
$moment->user->year = substr($user->profileCourtship->birthday, 2, 2) . '年';
} else {
$moment->user->city = null;
$moment->user->stature = null;
$moment->user->degree = null;
$moment->user->year = null;
}
$photo_arr = [];
$photos = json_decode($moment->photos, true);
if (is_array($photos) && count($photos)) {
$moment->original_photos = json_decode($moment->photos);
foreach ($photos as $photo) {
if (!is_array($photo)) {
$result = strstr($photo, '?');
$photo = $result ? $photo . '&x-oss-process=image/auto-orient,1/quality,q_90/watermark,image_d2F0ZXIucG5nP3gtb3NzLXByb2Nlc3M9aW1hZ2UvcmVzaXplLFBfMjA,x_10,y_10' : $photo . '?x-oss-process=image/auto-orient,1/quality,q_90/watermark,image_d2F0ZXIucG5nP3gtb3NzLXByb2Nlc3M9aW1hZ2UvcmVzaXplLFBfMjA,x_10,y_10';
$photo_arr[] = $photo;
}
}
$moment->photos = $photo_arr;
} else {
$moment->photos = $photos;
}
unset($moment->user->mobile, $moment->user->mobile);
//工作人员标签
$moment->work_user_tag = TestUser::where('status', 0)->where('user_id', $moment->user_id)->value('tag');
return $moment;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function momentDetailV2($moment, $user)
{
$moment->u_photo = ($moment->is_photo_audited == -1 ? 'https://image.fulllinkai.com/202207/04/1fa9a906b956efa26852fb685a845fff.png' : $moment->u_photo) ?: $moment->u_app_avatar;
$photos = [];
$moment->original_photos = json_decode($moment->photos, true);
if (is_array($moment->original_photos) && count($moment->original_photos)) {
foreach ($moment->original_photos as $photo) {
$result = strstr($photo, '?');
// $photo = $result?$photo.'&x-oss-process=style/scale1':$photo.'?x-oss-process=style/scale1';
$photo = $result ? $photo . '&x-oss-process=image/auto-orient,1/quality,q_90/watermark,image_d2F0ZXIucG5nP3gtb3NzLXByb2Nlc3M9aW1hZ2UvcmVzaXplLFBfMjA,x_10,y_10' : $photo . '?x-oss-process=image/auto-orient,1/quality,q_90/watermark,image_d2F0ZXIucG5nP3gtb3NzLXByb2Nlc3M9aW1hZ2UvcmVzaXplLFBfMjA,x_10,y_10';
$photos[] = $photo;
}
}
$moment->photos = $photos;
$moment->u_age = \CommonUtilsService::getAge($moment->u_birthday) ?: 0;
$moment->u_year = substr($moment->u_birthday, 2, 2) . '年';
$moment->is_self = $user->id == $moment->user_id ? 1 : 0;
//点赞数 评论数 是否点赞
$moment->comment_count = DB::table('comments')->where('commentable_id', $moment->id)->where('commentable_type', 'App\\Models\\Moment')->groupBy('commentable_id')->count();
$moment->like_count = DB::table('followables')->where('followable_id', $moment->id)->where('followable_type', 'App\\Models\\Moment')->where('relation', 'like')->groupBy('followable_id')->count();
$moment->is_like = DB::table('followables')->where('user_id', $user->id)->where('followable_id', $moment->id)->where('followable_type', 'App\\Models\\Moment')->where('relation', 'like')->exists();
//视频
$moment->cover_url = '';
$moment->play_url = '';
if ($moment->aliyun_video_id) {
$result = \AliyunService::getPlayInfo($moment->aliyun_video_id);
if (!empty($result)) {
$moment->play_url = $result['PlayInfoList']['PlayInfo'][0]['PlayURL'];
$moment->cover_url = $result['VideoBase']['CoverURL'] ?? '';
}
}
//工作人员标签
$moment->work_user_tag = TestUser::where('status', 0)->where('user_id', $moment->user_id)->value('tag');
return $moment;
}
/**
* 删除动态
* @param [type] $moment_id [description]
* @return [type] [description]
*/
public function deleteMoment($moment_id)
{
try {
$moment = Moment::find($moment_id);
if (empty($moment))
return ['code' => 1, 'msg' => '该动态已被删除'];
$is_moment_admin = RoleUser::where('role_id', 21)->where('user_id', auth()->id())->count() ? 1 : 0;
if ($moment->user_id != auth()->id() && $is_moment_admin != 1)
return ['code' => 1, 'msg' => '暂无权限删除该动态'];
$moment->delete();
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
/**
* 点赞动态
* @param [type] $user_id [description]
* @param [type] $moment_id [description]
* @return [type] [description]
*/
public function likeMoment($moment_id)
{
try {
$user = auth()->user();
$moment = Moment::find($moment_id);
if (empty($moment))
return ['code' => 1, 'msg' => '该动态已被删除'];
$black_ids = LinkingBlacklist::where('user_id', $moment->user_id)->pluck('other_user_id')->toArray();
if (in_array($user->id, $black_ids))
return ['code' => 1, 'msg' => '你已被该用户拉黑,无法点赞'];
if ($user->hasLiked($moment)) {
//取消点赞
$user->unlike($moment);
//更新缓存
$moment->updateCacheMomentLikeNum('SUB');
return ['code' => 0, 'isLkerMoment' => false];
} else {
//点赞
$user->like($moment);
$moment->updateCacheMomentLikeNum('ADD');
if ($user->id != $moment->user_id) {
//通知
$moment_user_id = $moment->user_id;
$content = $user->nickname . '点赞了你的动态';
$userService = new UserService($this->sms);
$userService->sendNotice($moment_user_id, $user->id, 'moment', $content, $content, $moment_id);
// $result = $this->sendAttachMsg($moment_id, 'moment', $content, $user->id, $moment->user_id, $user->app_avatar);
SendIMPushMsg::dispatch($moment_id, 'moment', $content, $user->id, $moment->user_id, $user->app_avatar)->onQueue('love');
//公众号通知
$this->sendMomentNotice('like', $moment, $user, $moment->user);
}
return ['code' => 0, 'isLkerMoment' => true];
}
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function sendAttachMsg($type_id, $type, $content, $from, $to, $image)
{
$body = "点击查看>>";
//发送评论自定义系统消息
$im_service = new IMService();
$attach = ["myattach" => $content, $type . '_id' => $type_id];
$payload = [
"apsField" => [
"alert" => [
"title" => $content,
"body" => $body,
],
"mutable-content" => 1
],
"hwField" => [
"click_action" => [
"type" => 1,
"intent" => "intent://com.huawei.codelabpush/deeplink?id=" . $type_id . "&customType=" . $type . "#Intent;scheme=pushscheme;launchFlags=0x4000000;end",
],
"title" => $content,
"body" => $body,
],
"oppoField" => [
"click_action_type" => 1,
"click_action_activity" => "com.oppo.codelabpush.intent.action.test",
"action_parameters" => [
"id" => $type_id,
"customType" => "moment",
],
],
"apnsText" => $body,
"pushTitle" => $content,
"customType" => $type,
"content" => $body,
"media_image" => $image,
"media_type" => 'image',
"id" => $type_id,
"type" => $type,
'passThrough' => 1,
];
$result = $im_service->sendAttachMsg($from, 0, $to, json_encode($attach), $content, $payload);
return true;
}
/**
* 是否点赞动态
* @param [type] $user_id [description]
* @param [type] $moment_id [description]
* @return boolean [description]
*/
public function isLkerMoment($moment)
{
return Auth::guard('api')->user()->hasLiked($moment);
}
/**
* 动态点赞者
* @param [type] $moment_id [description]
* @return [type] [description]
*/
public function momentLikerCount($moment)
{
$count = $moment->likers->count();
return $count;
}
/**
* 动态点赞者
* @param [type] $moment_id [description]
* @return [type] [description]
*/
public function momentLikers($moment_id)
{
try {
$moment = Moment::find($moment_id);
if (empty($moment))
return ['code' => 1, 'msg' => '该动态已被删除'];
$users = $moment->likers()->orderBy('followables.created_at', 'desc')->select('id', 'nickname', 'sex', 'photo', 'app_avatar', 'type', 'is_photo_audited')->get();
$moment->updateCacheMomentLikeNumV2(count($users));
foreach ($users as $user) {
$user->photo = $user->userAvatar();
$user->like_time = $user->pivot->created_at->toDateTimeString();
unset($user->pivot, $user->app_avatar);
}
return $users;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
/**
* 评论动态
* @param [type] $request [description]
* @param [type] $user_id [description]
* @param [type] $moment_id [description]
* @return [type] [description]
*/
public function commentMoment($moment_id)
{
try {
$user = auth()->user();
$moment = Moment::find($moment_id);
if (empty($moment))
return ['code' => 1, 'msg' => '该动态已删除'];
$comment = request()->input('comment');
$reply_id = request()->input('reply_id');
$result = $this->userCon->isBlacklist($user->id, $moment->user_id);
if ($result) {
if ($result == 1)
return ['code' => 1, 'msg' => '您已经被对方拉入黑名单'];
if ($result == 2)
return ['code' => 1, 'msg' => '您已经将对方拉入黑名单'];
}
$black_ids = LinkingBlacklist::where('user_id', $moment->user_id)->pluck('other_user_id')->toArray();
if (in_array($user->id, $black_ids))
return ['code' => 1, 'msg' => '你已被该用户拉黑,无法评论'];
//未完善资料
$res = $this->userCon->isCompleteProfile($user->id);
if (empty($res))
return ['code' => 1, 'msg' => '评论失败,请先完善资料'];
$comment = $user->comment($moment, $comment);
if ($user->id != $moment->user_id) {
//通知
$moment_user_id = $moment->user_id;
$content = $user->nickname . '评论了你的动态';
$userService = new UserService($this->sms);
$userService->sendNotice($moment_user_id, $user->id, 'moment', $content, $content, $moment_id);
$rec_user_id = $moment->user_id;
//发送评论自定义系统消息
// $result = $this->sendAttachMsg($moment_id, 'moment', $content, $user->id, $moment->user_id, $user->app_avatar);
SendIMPushMsg::dispatch($moment_id, 'moment', $content, $user->id, $rec_user_id, $user->app_avatar)->onQueue('love');
$this->sendMomentNotice('comment', $moment, $user, $moment->user);
}
if ($reply_id) {
//通知
$content = $user->nickname . '回复了你的评论';
$userService = new UserService($this->sms);
$userService->sendNotice($reply_id, $user->id, 'moment', $content, $content, $moment_id);
$rec_user_id = $reply_id;
//发送评论自定义系统消息
// $result = $this->sendAttachMsg($moment_id, 'moment', $content, $user->id, $reply_id, $user->app_avatar);
SendIMPushMsg::dispatch($moment_id, 'moment', $content, $user->id, $rec_user_id, $user->app_avatar)->onQueue('love');
if ($user->id != $moment->user_id) {
$this->sendMomentNotice('reply_comment', $moment, $user, $moment->user);
}
}
$moment->updateCacheMomentCommentNum('ADD');
return $comment;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
/**
* 动态评论列表
* @param [type] $moment_id [description]
* @return [type] [description]
*/
public function momentComments($moment_id, $limit = 0, $nopage = 0)
{
$moment = Moment::find($moment_id);
$comments = $moment->comments();
if ($nopage) {
$comments = $comments->limit($limit)->orderBy('id', 'desc')->get();
} else {
$comments = $comments->orderBy('id', 'desc')->paginate();
}
foreach ($comments as $comment) {
$comment->user = User::where('id', $comment->commented_id)->select('id', 'nickname', 'app_avatar', 'photo', 'sex')->first();
if ($comment->user) {
$comment->user->photo = $comment->user->userAvatar();
}
}
return $comments;
}
/**
* 动态评论数量
* @param [type] $moment_id [description]
* @return [type] [description]
*/
public function momentCommentCount($moment)
{
$count = $moment->comments()->count();
return $count;
}
/**
* 用户动态列表
* @param [type] $user_id [description]
* @return [type] [description]
*/
public function userMoments($user_id)
{
$moments = Moment::where('user_id', $user_id)->with('user.profileCourtship', 'topic', 'user', 'vote:id,title')->orderBy('id', 'desc')->paginate();
foreach ($moments as $moment) {
$this->momentDetail($moment);
}
return $moments;
}
/**
* 话题列表
* @return [type] [description]
*/
public function topics($request, $type = 'user')
{
try {
$unlike_moment_id = [];
if ($type == 'user') {
$user_id = Auth::guard('api')->user()->id;
//不喜欢的动态id
$unlike_moment_id = UnlikeMomentLog::where('user_id', $user_id)->pluck('moment_id')->toArray();
}
//没有用户的动态id
$has_no_user_moment_id = Moment::doesntHave('user')->pluck('id')->toArray();
$topics = MomentTopic::where('is_show', 1)
->with([
'momentTopicsActivities' => function ($q) {
$date = date('Y-m-d H:i:s');
$q->where('start_time', '<=', $date);
$q->where('end_time', '>=', $date);
}
])
->orderBy('sort', 'desc')->withCount([
'moment' => function ($query) use ($unlike_moment_id, $has_no_user_moment_id) {
$query->where(function ($sql) use ($unlike_moment_id, $has_no_user_moment_id) {
$sql->where('is_audited', 1)->whereNotIn('id', $unlike_moment_id)->whereNotIn('id', $has_no_user_moment_id);
})
->orWhere(function ($sql) {
$sql->where('user_id', auth()->id())->where('is_audited', 0);
});
}
]);
$date = date('Y-m-d H:i:s');
$topics = $topics->where('start_time', '<=', $date);
$topics = $topics->where('end_time', '>', $date);
if ($request->keyword) {
$topics = $topics->where('name', 'like', '%' . $request->keyword . '%')->orderBy('is_hot', 'desc');
}
if ($request->is_hot) {
$topics = $topics->where('is_hot', 1);
}
//比较版本号
// $ios_version = $request->header('app-version', '1.0.0');
// $and_version = $request->header('version-name', '1.0.0');
// if(!$this->contrastVersion($ios_version, '1.3.8') && !$this->contrastVersion($and_version, '1.3.13')){
// $topics = $topics->orderBy('id', 'desc')->get();
// }else{
// $topics = $topics->orderBy('id', 'desc')->paginate();
// }
if ($request->input('nopage')) {
$topics = $topics->orderBy('sort', 'desc')->get();
} else {
$topics = $topics->orderBy('sort', 'desc')->paginate();
}
return $topics;
} catch (\Exception $e) {
return false;
}
}
/**
* 话题列表
* @return [type] [description]
*/
public function topicsV2($request, $type = 'user')
{
try {
$user_id = null;
$unlike_moment_id = [];
$belief = null;
if ($type == 'user') {
$user = Auth::guard('api')->user();
$user_id = $user->id;
$belief = $user->belief;
//不喜欢的动态id
// $unlike_moment_id = UnlikeMomentLog::where('user_id', $user_id)->pluck('moment_id')->toArray();
}
$topics = MomentTopic::where('is_show', 1)->orderBy('sort', 'desc')
->with([
'momentTopicsActivities' => function ($q) {
$date = date('Y-m-d H:i:s');
$q->where('start_time', '<=', $date);
$q->where('end_time', '>=', $date);
}
])
->withCount([
'moment' => function ($query) use ($unlike_moment_id, $belief, $user_id) {
$query->whereHas('user', function ($q) use ($belief) {
$q->where('hidden_profile', 'NONE');
if ($belief) {
if ($belief == '基督教') {
$q->where('belief', $belief);
} else {
$q->where('belief', '<>', '基督教');
}
}
});
$query->where(function ($sql) use ($unlike_moment_id, $belief) {
$sql->where('is_audited', 1);
// ->whereNotIn('id', $unlike_moment_id);
})->orWhere(function ($sql) {
$sql->where('user_id', auth()->id())->where('is_audited', 0);
});
if ($user_id) {
$query->whereDoesntHave("unlikeMoments", function ($sql) use ($user_id) {
$sql->where("user_id", $user_id);
});
}
}
]);
$date = date('Y-m-d H:i:s');
$topics = $topics->where('start_time', '<=', $date);
$topics = $topics->where('end_time', '>', $date);
if ($request->keyword) {
$topics = $topics->where('name', 'like', '%' . $request->keyword . '%')->orderBy('is_hot', 'desc');
}
if ($request->is_hot) {
$topics = $topics->where('is_hot', 1);
}
if ($request->input('nopage')) {
$topics = $topics->orderBy('id', 'desc')->get();
} else {
$topics = $topics->orderBy('id', 'desc')->paginate();
}
return $topics;
} catch (\Exception $e) {
return false;
}
}
//比较版本号
public function contrastVersion($version, $other_version)
{
$version = explode('.', $version);
$other_version = explode('.', $other_version);
if ($version[0] > $other_version[0]) {
return true;
}
if ($version[0] < $other_version[0]) {
return false;
}
if ($version[0] == $other_version[0]) {
if ($version[1] > $other_version[1]) {
return true;
}
if ($version[1] < $other_version[1]) {
return false;
}
if ($version[1] == $other_version[1]) {
if ($version[2] > $other_version[2]) {
return true;
} else {
return false;
}
}
}
}
/**
* 举报动态
* @param [type] $moment_id [description]
* @return [type] [description]
*/
public function complaintMoment($moment_id)
{
try {
$moment = Moment::find($moment_id);
$label = request()->input('label');
$content = request()->input('content');
$photos = json_encode(request()->input('photos', []));
if (empty($label))
return ['code' => 1, 'msg' => '请选择举报类型'];
if (empty($moment))
return ['code' => 1, 'msg' => '要举报的动态不存在'];
if (request()->input('photos', []) == [] || empty(request()->input('photos', []))) {
return $this->failure('请提供举报截图');
}
ComplaintHistory::create([
'user_id' => auth()->id(),
'complaint_id' => $moment_id,
'type' => 'moment',
'content' => $content,
'label' => $label,
'photos' => $photos
]);
//通知人
$user = auth()->user();
$mobiles = Redis::smembers('repot_notice_mobile');
$param['message'] = $user->nickname . ' 举报了动态:' . $moment->content;
foreach ($mobiles as $mobile) {
$param['mobile'] = $mobile;
SendEasySms::dispatch($param)->onQueue('love');
}
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function auditedMoment($moment_id)
{
try {
\DB::beginTransaction();
$moment = Moment::find($moment_id);
$is_audited = request()->input('is_audited', 0);
$reason = request()->input('reason');
$moment->is_audited = $is_audited;
$moment->reason = $reason;
$moment->save();
$result = $this->sendMomentCoin($moment);
if (empty($result))
throw new \Exception("赠送福币失败", 1);
// if($moment->topic_id){
// Topic::where('id', $moment->topic_id)->increment('quote_num');
// }
\DB::commit();
return $moment;
} catch (\Exception $e) {
\DB::rollback();
$this->getError($e);
return false;
}
}
public function sendMomentCoin($moment)
{
try {
$start_time = date('Y-m-d 00:00:00');
$end_time = $moment->created_at->toDateTimeString();
//是否是当天首次发布
$count = Moment::where('user_id', $moment->user_id)->where(function ($sql) {
$sql->whereNotNull('aliyun_video_id')->orWhere('photos', '<>', '[]');
})->whereBetween('created_at', [$start_time, $end_time])->count();
if ($count != 1)
return true;
//审核通过,赠送福币
$user = User::find($moment->user_id);
if (empty($user))
throw new \Exception("没有发布者", 1);
$coin = 0;
if (count(json_decode($moment->photos, true))) {
$coin = 2;
} elseif ($moment->aliyun_video_id) {
$coin = 4;
}
$log = CoinLog::where([
'user_id' => $user->id,
'type' => 'RECMOMENT',
'type_id' => $moment->id,
])->first();
if ($coin && $moment->is_audited == 1 && empty($log)) {
$user->updateCoinInfo('add', $coin, 'other');
$user->addCoinLog('RECMOMENT', $moment->id, $coin);
//通知
}
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function unlikeMoment($moment_id)
{
try {
$user = auth()->user();
$user->unlikeMomentLogs()->create([
'moment_id' => $moment_id,
]);
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
//评论详情
public function comments($request)
{
$user = auth()->user();
switch ($request->type) {
case 'moment':
$type = 'App\Models\Moment';
break;
case 'vote':
$type = 'App\Models\App\Vote';
break;
default:
$type = 'App\Models\Moment';
break;
}
if ($request->nopage) {
$comments = Comment::select('id', 'commentable_id', 'commentable_type', 'commented_id', 'reply_id', 'commented_type', 'comment', 'created_at', 'updated_at')->where('commentable_id', $request->id)->where('commentable_type', $type)->where('commented_type', 'App\Models\User')->orderBy('id', 'desc')->get();
} else {
$comments = Comment::select('id', 'commentable_id', 'commentable_type', 'commented_id', 'reply_id', 'commented_type', 'comment', 'created_at', 'updated_at')->where('commentable_id', $request->id)->where('commentable_type', $type)->where('commented_type', 'App\Models\User')->orderBy('id', 'desc')->paginate();
}
foreach ($comments as $comment) {
if (!empty($comment)) {
//是不是本人评论
$comment->publish = $comment->commented_id == $user->id ? 1 : 0;
$comment->user = User::select('id', 'nickname', 'app_avatar', 'photo', 'circle_avatar', 'sex', 'type', 'is_photo_audited')->where('id', $comment->commented_id)->first();
$comment->replyer = User::select('id', 'nickname', 'app_avatar', 'photo', 'circle_avatar', 'sex', 'type', 'is_photo_audited')->where('id', $comment->reply_id)->first();
$comment->likerCount = $this->modelLikeCount($comment);
$comment->isLker = $this->isLkerModel($comment);
}
}
return $comments;
}
//点赞者
public function liker($request)
{
switch ($request->type) {
case 'moment':
$result = $this->momentLikers($request->id);
break;
case 'vote':
$vote = Vote::find($request->id);
if (empty($vote))
throw new \Exception("投票不存在", 1);
$result = $this->modelLikers($vote);
break;
default:
$result = $this->momentLikers($request->id);
break;
}
return $result;
}
//带投票的动态
public function momentVotes($request)
{
$user_id = auth()->id();
$user_vote_id = UserVote::where('user_id', $user_id)->pluck('vote_id')->toArray();
$votes = Vote::with('option')->whereNotIn('id', $user_vote_id)->where('is_show', 1)->orderByRaw("RAND()")->limit(3)->get()->toArray();
//手动分页
$limit = 15 - count($votes);
$moments = $this->moments($limit);
$moments = $moments->toArray();
foreach ($moments['data'] as &$val) {
$val['type'] = 'moment';
}
//动态里面插入投票
foreach ($votes as $key => $vote) {
$vote = $this->vote($vote['id']);
$vote = [$vote];
switch ($key) {
case 0:
if ($moments['to'] > 1) {
array_splice($moments['data'], 2, 0, $vote);
}
break;
case 1:
if ($moments['to'] > 6) {
array_splice($moments['data'], 8, 0, $vote);
}
break;
case 2:
if ($moments['to'] > 13) {
array_splice($moments['data'], 15, 0, $vote);
}
break;
default:
break;
}
}
return $moments;
}
//投票
public function vote($vote_id)
{
$user_id = auth()->id();
$vote = Vote::with('option')->where('id', $vote_id)->first();
//点赞数
$vote->momentLikerCount = $this->modelLikeCount($vote);
//评论数
$vote->momentCommentCount = $this->modelCommentCount($vote);
//是否点赞
$vote->isLkerMoment = $this->isLkerModel($vote);
//总投票数
$vote_user_count = UserVote::where('vote_id', $vote_id)->count();
//是否投票过
$has_vote = UserVote::where('vote_id', $vote_id)->where('user_id', $user_id)->first();
$vote->is_vote = !empty($has_vote) ? 1 : 0;
$vote->type = 'vote';
if (!empty($has_vote)) {
foreach ($vote->option as &$val) {
$count = UserVote::where('option_id', $val->id)->count();
$val->rate = sprintf("%.3f", $count / $vote_user_count);
$val->count = $count;
$val->checked = $val->id == $has_vote->option_id ? 1 : 0;
}
}
return $vote;
}
//某个对象的点赞数
public function modelLikeCount($model)
{
$count = $model->likers ? $model->likers->count() : 0;
return $count;
}
public function modelCommentCount($model)
{
$count = $model->comments()->count();
return $count;
}
public function isLkerModel($model)
{
return auth()->user()->hasLiked($model);
}
//点赞对象
public function likeModel($model)
{
try {
$user = auth()->user();
if ($user->hasLiked($model)) {
//取消点赞
$user->unlike($model);
return ['code' => 0, 'isLker' => false];
} else {
//点赞
$user->like($model);
//消息推送
switch ($model->commentable_type) {
case 'App\Models\App\Vote':
$notice_model = Vote::find($model->commentable_id);
break;
case 'App\Models\Moment':
$notice_model = Moment::find($model->commentable_id);
$black_ids = LinkingBlacklist::where('user_id', $notice_model->user_id)->pluck('other_user_id')->toArray();
if (in_array($user->id, $black_ids))
return ['code' => 1, 'msg' => '你已被该用户拉黑,无法点赞'];
break;
default:
break;
}
if (!empty($notice_model)) {
$this->interactNotice($model, $notice_model, 'like');
}
return ['code' => 0, 'isLker' => true];
}
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
//评论对象
public function commentModel($model)
{
try {
$user = auth()->user();
$comment = request()->input('comment');
$reply_id = request()->input('reply_id');
$comment = $user->comment($model, $comment);
if ($reply_id) {
$this->interactNotice($comment, $model, 'reply');
}
return $comment;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
//对象点赞者
public function modelLikers($model)
{
try {
$users = $model->likers()->orderBy('followables.created_at', 'desc')->select('id', 'nickname', 'sex', 'photo', 'app_avatar')->get();
foreach ($users as $user) {
$user->photo = $user->userAvatar();
$user->like_time = $user->pivot->created_at->toDateTimeString();
unset($user->pivot, $user->app_avatar);
}
return $users;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
//评论互动通知
public function interactNotice($model, $notice_model, $type)
{
$user = auth()->user();
$table = str_replace('s', '', $notice_model->getTable());
switch ($type) {
case 'like':
$type = '点赞';
if ($user->id != $model->commented_id) {
//通知
$content = $user->nickname . $type . '了你的评论';
$userService = new UserService($this->sms);
$userService->sendNotice($model->commented_id, $user->id, $table, $content, $content, $model->commentable_id);
//发送评论自定义系统消息
// $result = $this->sendAttachMsg($model->commentable_id, $table, $content, $user->id, $model->commented_id, $user->app_avatar);
SendIMPushMsg::dispatch($model->commentable_id, $table, $content, $user->id, $model->commented_id, $user->app_avatar)->onQueue('love');
}
break;
case 'reply':
$type = '回复';
$reply_id = request()->input('reply_id');
if ($user->id != $reply_id) {
//通知
$content = $user->nickname . $type . '了你的评论';
$userService = new UserService($this->sms);
$userService->sendNotice($reply_id, $user->id, $table, $content, $content, $model->commentable_id);
//发送评论自定义系统消息
// $result = $this->sendAttachMsg($model->commentable_id, $table, $content, $user->id, $reply_id, $user->app_avatar);
SendIMPushMsg::dispatch($model->commentable_id, $table, $content, $user->id, $reply_id, $user->app_avatar)->onQueue('love');
}
break;
default:
break;
}
return true;
}
//获取阿里云点播视频信息
public function getVodInfo($video_id)
{
$access_key_id = config('aliyun.aliyun_vod_access_key_id');
$access_key_secret = config('aliyun.aliyun_vod_access_key_secret');
AlibabaCloud::accessKeyClient($access_key_id, $access_key_secret)
->regionId('cn-shanghai')
->asDefaultClient();
try {
$result = AlibabaCloud::rpc()
->product('vod')
// ->scheme('https') // https | http
->version('2017-03-21')
->action('GetMezzanineInfo')
->method('POST')
->host('vod.cn-shanghai.aliyuncs.com')
->options([
'query' => [
'RegionId' => "cn-shanghai",
'VideoId' => $video_id,
'AdditionType' => "video",
],
])
->request();
return $result->toArray();
} catch (ClientException $e) {
$this->getError($e);
return false;
} catch (ServerException $e) {
$this->getError($e);
return false;
}
}
public function sendNotice($user_id, $send_user_id, $type, $content, $message = '', $type_id = 0)
{
$data = [
'user_id' => $user_id,
'send_user_id' => $send_user_id,
'type' => $type,
'type_id' => $type_id,
'content' => $content,
'message' => $message,
'status' => 0,
];
$notice = Notice::where('user_id', $user_id)->where('send_user_id', $send_user_id)->where('type', $type)->where('type_id', $type_id)->orderBy('id', 'desc')->first();
if ($notice && $type == 'follow') {
$notice->update($data);
} else {
$notice = Notice::create($data);
}
return $notice;
}
/**根据动态话题的ID判断是否存在话题活动如果存在则领取会员天数 */
public function participateMomentToplicActivities($moment)
{
$topic_id = $moment->topic_id;
}
}