first();
}else{
$circle_id = null;
}
if($sph_obj && is_array($sph_obj) && isset($sph_obj['type'])){
}else{
$sph_obj = null;
}
try {
$addressId = 0;
if (!_empty_($address) && _array_key($address, 'address_name', '')) {
// 创建地址
$addr = new WxPostsAddress([
'address_name' => $address['address_name'],
'address_detailed' => $address['address_detailed'],
'latitude' => $address['latitude'],
'longitude' => $address['longitude']
]);
$addr->save();
$addressId = $addr->id;
}
global $__MINI_GLOBAL_IP__,$__MINI_GLOBAL_TENANT_ID__;
$timestamp = current_time();
// 创建笔记
$contents = trim($contents);
$post_conetnt = _mini_phone( _mini_emoji( _mini_aite_replace( $contents ) ) );
$post = new Model([
'posts_content' => $post_conetnt,
'user_id' => $uid,
'pay_content_id' => $pay_content_id > 0 ? $pay_content_id : 0,
'circle_id' => ($circle_id && $circle_id > 0) ? $circle_id : 0,
'circle_name' => ($circle && !_empty_(_array_key($circle, 'circle_name', null))) ? $circle['circle_name'] : '广场',
'address_id' => _empty_($addressId) ? 0 : $addressId,
'tags_id' => '0',
'weight' => $timestamp,
'last_reply_at' => $timestamp,
'is_draft' => $is_draft,
]);
// print_r($post);exit;
if(!_empty_($posts_title)){
$post->posts_title = $posts_title;
}
$post->is_examine = $is_examine;
if(!_empty_($refer_id)){
$post->refer_id = $refer_id;
}
if($voter_id > 0){
$post->voter_id = $voter_id;
}
if($device){
$post->device = $device;
}
if($__MINI_GLOBAL_IP__){
$post->ip = $__MINI_GLOBAL_IP__;
}
if($__MINI_GLOBAL_TENANT_ID__ > 0){
$post->tenant_id = $__MINI_GLOBAL_TENANT_ID__;
}
if($sph_obj){
$post->is_wechat_sph = true;
}
if(!_empty_($useds)){
$post->useds = implode(',', $useds);
}
if($contact_phone){
$post->has_fields = 1;
}
if($is_anonymous == 1){
$post->is_anonymous = 1;
}
$post->save();
$postId = $post->id;
if($contact_phone){
$post_field = new WxPostsField();
$post_field->post_id = $postId;
$post_field->contact_phone = $contact_phone;
$post_field->save();
}
if($circle_id > 0){
Redis::sadd('realtime:others:set', json_encode([$circle_id, 2, 10]));
}
Redis::sadd('realtime:others:set', json_encode([$uid, 4, 10]));
if($pay_content_id > 0){
$pay_r = WxPostsPayContent::where('id', $pay_content_id)->update(['post_id'=>$postId]);
if(!$pay_r){
WxPostsPayContent::where('id', $pay_content_id)->forceDelete();
DB::rollBack();
return 0;
}
}
if($sph_obj){
update_post_meta($postId, 'wechat_sph', $sph_obj, 'j');
}
// 图片组
if (!_empty_($images)) {
$postImage = [];
foreach ($images as $k => $v) {
if(_empty_(_array_key($v, 'url', ''))){
continue;
}
$tmp_images = [];
$tmp_images['posts_id'] = $postId;
$tmp_images['user_id'] = $uid;
$tmp_images['img_url'] = $v['url'];
if(!StrUtils::endsWith($v['url'] ?? '', '.gif')){
$img_info = ImageUtils::get_url_img_info($v['url']);
if($img_info){
$tmp_images['width'] = $img_info['width'];
$tmp_images['height'] = $img_info['height'];
}
}else{
$tmp_images['width'] = 0;
$tmp_images['height'] = 0;
}
$postImage[] = $tmp_images;
}
if($postImage){
$postImg = new WxPostsImg();
$postImg->addAll($postImage);
}
}
// print_r($photo_url);exit;
// print_r('123');exit;
if (!empty($photo_url)) {
// print_r('123');exit;
$postImages = new WxPostsImg();
$post_img['posts_id'] =$postId;
$post_img['user_id'] =$uid;
$post_img['img_url'] =$photo_url;
$postImages->posts_id = $postId;
$postImages->user_id = $uid;
$postImages->img_url = $photo_url;
// $postImages->add($post_img);
$postImages->save();
}
if(!_empty_($refer_id)){
//转发评论
$berefer_post = WxPost::find($refer_id);
if($berefer_post){
$CommentModel = new WxComment();
$CommentModel->posts_id = $berefer_post->id;
$CommentModel->posts_user_id = $berefer_post->user_id;
$CommentModel->user_id = $uid;
$CommentModel->user_name = $user['user_name'];
$CommentModel->user_avatar = $user['user_avatar'];
$CommentModel->comment_content = ' '.$post_conetnt;
$CommentModel->comment_img_url = '';
$CommentModel->is_sticky = 0;
$CommentModel->comment_state = 1;
if($device){
$CommentModel->device = $device;
}
if($__MINI_GLOBAL_IP__){
$CommentModel->ip = $__MINI_GLOBAL_IP__;
}
$comment_r = $CommentModel->save();
if($berefer_post->user_id != $uid){
UserUtils::add_user_notice(2007, $berefer_post->user_id, '笔记被转发', '「'.$user['user_name'].'」' . '转发了您的笔记。', 102, $berefer_post->id);
}
$comment_count = WxCommentRepositores::commentCount($berefer_post->id) ?: 0;//评论数量
$weight = WxPost::where('id', $berefer_post->id)->value('weight');
$weight += ( Utils::getDecayCoefficient($comment_count, 5) * 1000);
if($weight > time()){
$weight = time();
}
WxPost::withoutSyncingToSearch(function () use ($berefer_post, $weight) {
WxPost::where('id', $berefer_post->id)->update(['last_reply_at' => current_time(), 'weight' => timetostr($weight)]);
});
Cache::forget('post:commentCount:'.$berefer_post->id);
}
}
// 话题
if (!_empty_($tags)) {
$postTag = [];
$tagsIds = [];
foreach ($tags as $k => $v) {
$postTag[$k]['posts_id'] = $postId;
$postTag[$k]['tags_id'] = $v['id'];
$tagsIds[] = $v['id'];
Redis::sadd('realtime:others:set', json_encode([$v['id'], 5, 10]));
}
$tags = new WxPostsTag();
$tags->addAll($postTag);
WxTag::whereIn('id', $tagsIds)->increment('tags_number');
if($circle_id > 0){
CircleUtils::add_circle_tags($circle_id, $tagsIds);
}
}
// 商品
if (!_empty_($goods)) {
$postGoods = [];
foreach ($goods as $k => $v) {
$postGoods[$k]['posts_id'] = $postId;
$postGoods[$k]['goods_id'] = $v['id'];
Redis::sadd('realtime:others:set', json_encode([$v['id'], 3, 10]));
}
$goods = new WxPostsGoods();
$goods->addAll($postGoods);
}
// 视频组
if (!_empty_($video)) {
$postVideoModel = new WxPostsVideo();
$postVideoModel->user_id = $uid;
$postVideoModel->posts_id = $postId;
$postVideoModel->video_url = $video;
$postVideoModel->video_thumb_url = $videoThumbUrl;
$postVideoModel->video_width = $video_width;
$postVideoModel->video_height = $video_height;
$attachment = FileUtils::get_url_attach($video);
if($attachment->state == 2){
// 状态(0正常,1用户删除,2管理员下架, 3审核中 4冻结中 5已清理)
$postVideoModel->posts_video_state = 5;
}else if($attachment->state == 3 || $attachment->state == 4){
$postVideoModel->posts_video_state = 4;
}else if(in_array($attachment->cos_review, [3,4,5])){
$postVideoModel->posts_video_state = 3;
}
$postVideoModel->save();
}
// 投票
if(!_empty_($votes)){
$vote = new WxPostsVote();
$vote->post_id = $postId;
foreach ([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20] as $num){
if(_array_key($votes, $num - 1, '')){
$vote['option'.$num] = _array_key($votes, $num - 1, '');
}
}
$vote->save();
}
if(!_empty_($files)){
foreach ($files as $file){
if($file['name'] && $file['url']){
$file_model = new WxPostsFile();
$file_model->name = $file['name'];
$file_model->url = $file['url'];
$file_model->size = _array_key($file, 'size', 0);
$file_model->post_id = $postId;
$file_model->save();
}
}
}
if(!_empty_($sounds)){
foreach ($sounds as $sound){
$sound_model = new WxPostsSound();
$sound_model->name = $sound['name'];
$sound_model->time = $sound['time'];
$sound_model->url = $sound['url'];
$sound_model->post_id = $postId;
$sound_model->save();
}
}
if(!_empty_($circle)){
$pay_res = CircleUtils::user_can_publish($user, $circle, true);
if($pay_res['code'] == 0){
DB::rollBack();
return 0;
}
}
if($circle_id && $circle_id > 0){
WxCircle::withoutSyncingToSearch(function () use ($circle_id){
WxCircle::where('id', $circle_id)->update(['last_publish_at' => current_time()]);
});
}
Redis::sadd('realtime:post:set', $postId);
if($__MINI_GLOBAL_IP__){
AttachIpAddressJob::dispatch(1, $postId)->delay(2);
}
DB::commit();
return $postId;
} catch (\Exception $e) {
DB::rollBack();
_logger_(__file__, __line__, $e->getMessage());
// _logger_(__file__, __line__, $e->getTrace());
return 0;
}
}
/**
* @param $uid 'uid'
* @param $contents 'content'
* @param $circle_id 'id'
* @param $tag_ids [id1,id2,id3]
* @param $good_ids [id1,id2,id3]
* @param $image_urls 'url1,url2,url3'
* @param $video_url 'url'
* @param $address_id 'id'
* @return bool|string
*/
public static function admin_add($domain, $uid, $title, $contents, $circle_id, $tag_ids, $good_ids, $image_urls, $video_url, $address_id, $external_id = '', $date = '', $tenant_id = 0)
{
DB::beginTransaction();
try {
if($domain){
$post_id = WxSyncRelationRepositories::wx_post_id($domain, $external_id);
if($post_id){
return $post_id;
}
}
$timestamp = current_time();
// 圈子名称
if($circle_id > 0){
$circle = WxCircle::where('id', $circle_id)->first();
if(_empty_($circle)){
_logger_(__file__, __line__, '同步失败,不存在ID为'.$circle_id.'的圈子');
return 0;
}
}else{
$circle['circle_name'] = '广场';
}
$is_examine = 1;
$contents = trim($contents);
// 创建笔记
$post = new Model([
'posts_title' => $title ?: '',
'posts_content' =>_mini_phone( _mini_emoji( _mini_aite_replace($contents) ) ),
'user_id' => $uid,
'circle_id' => $circle_id,
'circle_name' => $circle['circle_name'],
'address_id' => _empty_($address_id) ? 0 : $address_id,
'tags_id' => 0,
'device' => ['app', 'mp'][(int)mini_rand(0,1)],
'is_examine'=>1,
'weight' => $timestamp,
'last_reply_at' => $timestamp
]);
if($date){
$post->updated_at = strtotime($date);
$post->created_at = strtotime($date);
$post->weight = $date;
$post->last_reply_at = $date;
}
if($tenant_id > 0){
$post->tenant_id = $tenant_id;
}
$post->save();
$postId = $post->id;
Redis::sadd('realtime:others:set', json_encode([$circle_id, 2, 10]));
Redis::sadd('realtime:others:set', json_encode([$uid, 4, 10]));
// 视频组
if (!_empty_($video_url)) {
$videoThumbUrl = null;
if(!_empty_($image_urls)){
$videoThumbUrl = explode(',', $image_urls)[0];
}
$postVideoModel = new WxPostsVideo();
$postVideoModel->user_id = $uid;
$postVideoModel->posts_id = $postId;
$postVideoModel->video_url = stripslashes($video_url);
$postVideoModel->video_thumb_url = $videoThumbUrl;
$postVideoModel->video_width = null;
$postVideoModel->video_height = null;
$postVideoModel->save();
}else{
// 图片组
if (!_empty_($image_urls)) {
$postImage = [];
$k = 0;
$image_urls = explode(',', $image_urls);
foreach ($image_urls as $img_url) {
if(_empty_($img_url)){
continue;
}
$postImage[$k]['posts_id'] = $postId;
$postImage[$k]['user_id'] = $uid;
$postImage[$k]['img_url'] = $img_url;
if(Cache::has($img_url)){
$img_info = json_decode(Cache::get($img_url), true);
$postImage[$k]['width'] = $img_info['width'];
$postImage[$k]['height'] = $img_info['height'];
Cache::forget($img_url);
}
$k ++;
}
if($postImage){
$postImg = new WxPostsImg();
$postImg->addAll($postImage);
}
}
}
// 话题
if (!_empty_($tag_ids)) {
$postTag = [];
$tagsIds = [];
$k = 0;
foreach ($tag_ids as $tag_id) {
if(_empty_($tag_id)){
continue;
}
$postTag[$k]['posts_id'] = $postId;
$postTag[$k]['tags_id'] = $tag_id;
$tagsIds[] = $tag_id;
Redis::sadd('realtime:others:set', json_encode([$tag_id, 5, 10]));
$k++;
}
if($postTag){
$tags = new WxPostsTag();
$tags->addAll($postTag);
WxTag::whereIn('id', $tagsIds)->increment('tags_number');
if($circle_id > 0){
CircleUtils::add_circle_tags($circle_id, $tagsIds);
}
}
}
// 商品
if (!_empty_($good_ids)) {
$postGoods = [];
$k = 0;
foreach ($good_ids as $good_id) {
if(_empty_($good_id)){
continue;
}
$postGoods[$k]['posts_id'] = $postId;
$postGoods[$k]['goods_id'] = $good_id;
Redis::sadd('realtime:others:set', json_encode([$good_id, 3, 10]));
$k ++;
}
if($postGoods){
$goods = new WxPostsGoods();
$goods->addAll($postGoods);
}
}
// 圈主
// 待审核
if($circle_id > 0){
if($is_examine == 0){
//给对应圈子发送待审核信息
UserUtils::assistant_notice($circle['user_id'], '您创建的圈子「' . ''.$circle['circle_name'].'' . '」有一篇新的内容等待您去审核哦,请务必在24小时内进行处理。');
}else{
//给对应圈子圈主发送新内容信息
UserUtils::assistant_notice($circle['user_id'], '您创建的圈子「' . ''.$circle['circle_name'].'' . '」有一篇新的内容发布,您要好好把关哦。');
}
}
if($external_id && $domain){
$sync = new WxSyncRelation();
$sync->domain = $domain;
$sync->type = 'post';
$sync->inner_id = $postId;
$sync->external_id = $external_id;
$sync->status = 1;
$sync->save();
}
Redis::sadd('realtime:post:set', $postId);
DB::commit();
return $postId;
} catch (\Exception $e) {
DB::rollBack();
_logger_(__file__, __line__, $e->getMessage());
return 0;
}
}
/**
* 推荐的笔记列表
* @param $uid
* @param int $limit
* @return mixed
*/
public static function list($uid, $limit = 10, $is_waterfall = false, $city = '')
{
global $__MINI_GLOBAL_TENANT_ID__,$__MINI_GLOBAL_DEVICE__,$__MINI_GLOBAL_MODE__;
$sorts = Settings::get('app_index_list_sort', ['weight', 'created_at', 'id'], true);
$not_list_circles = explode(',', Settings::get('app_index_not_list_circles', '', true));
$not_like_post_set = get_user_meta($uid, 'feedback:notlike:post:set', 'j');
$not_like_user_set = get_user_meta($uid, 'feedback:notlike:user:set', 'j');
if(_empty_($city) || $city == '全国'){
$city = '';
}
$data = (new Model())
->where('is_examine', 1)
->where('posts_state', 0)
->orderBy('is_sticky', 'desc');
$data = $data->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__);
if(!_empty_($not_list_circles)){
$data = $data->whereNotIn('circle_id', $not_list_circles);
}
if(!_empty_($not_like_user_set)){
$data = $data->whereNotIn('user_id', $not_like_user_set);
}
if(!_empty_($not_like_post_set)){
$data = $data->whereNotIn('id', $not_like_post_set);
}
// 控制视频隐藏
if($__MINI_GLOBAL_DEVICE__ != 'mp'){
global $__MINI_GLOBAL_FRONT_VERSION__;
// todo: 临时
if($__MINI_GLOBAL_FRONT_VERSION__ >= '1.2.2.24'){
}else{
$data = $data->where('is_wechat_sph', 0);
}
}
if($__MINI_GLOBAL_MODE__ == 'examine'){
$data = $data->where('post_type', '<>', 'video');
}
if($city){
if(StrUtils::endsWith($city, '市')){
$data = $data->where('city', $city);
}else{
$data = $data->where('city', $city.'市');
}
}
if($sorts){
foreach ($sorts as $sort_){
if(StrUtils::startsWith($sort_, 'rand')){
if(StrUtils::startsWith($sort_, 'rand_new')){
$number = explode('_', $sort_)[2];
$the_base_id = Cache::remember('posts:new:'.$number, 3600 * 5, function () use ($number){
$base_id = WxPost::where('is_examine', 1)->where('posts_state', 0)->orderBy('id', 'desc')->skip($number)->value('id');
return $base_id > 0 ? $base_id : 0;
});
$data = $data->where('id', '>=', $the_base_id)->inRandomOrder();
break;
}else{
$days = explode('_', $sort_)[1];
$data = $data->where('created_at', '>=', Carbon::now()->subDays($days))->inRandomOrder();
break;
}
}
$data = $data->orderBy($sort_, 'desc');
}
}else{
$data = $data->orderBy('id', 'desc');
}
$data = $data->simplePaginate($limit, FieldUtils::postInfoColums());
if($is_waterfall){
PostUtils::reset_loop();
$data->map(function ($v, $k) use ($uid){
return PostUtils::WaterfallProcess($k, $v, 1);
});
}else{
$data = self::postsParame($data, $uid,1);//文章详情
}
return $data;
}
/**
* 我关注的笔记列表
* @param $uid
* @param int $limit
* @return mixed
*/
public static function followPosts($uid, $limit = 10, $is_waterfall = false)
{
$userFollowIds = WxUserFollow::where('user_id', $uid)->pluck('user_follow_id');
global $__MINI_GLOBAL_TENANT_ID__;
$query = WxPost::where('is_examine', 1)
->where('posts_state', 0)
->whereIn('user_id', $userFollowIds)->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__);
// 控制视频隐藏
global $__MINI_GLOBAL_DEVICE__,$__MINI_GLOBAL_MODE__;
if($__MINI_GLOBAL_DEVICE__ != 'mp'){
global $__MINI_GLOBAL_FRONT_VERSION__;
// todo: 临时
if($__MINI_GLOBAL_FRONT_VERSION__ >= '1.2.2.24'){
}else {
$query = $query->where('is_wechat_sph', 0);
}
}
if($__MINI_GLOBAL_MODE__ == 'examine'){
$query = $query->where('post_type', '<>', 'video');
}
$data = $query->orderBy('id', 'desc')
->simplePaginate($limit, FieldUtils::postInfoColums());
if($is_waterfall){
PostUtils::reset_loop();
$data->map(function ($v, $k) use ($uid){
return PostUtils::WaterfallProcess($k, $v, 1);
});
}else{
$data = self::postsParame($data, $uid, 1);//文章详情
}
return $data;
}
/**
* 热榜的笔记列表
* @param $uid
* @param int $limit
* @return mixed
*/
public static function hotPosts($uid, $limit = 10, $is_waterfall = false, $is_mutisite = false)
{
global $__MINI_GLOBAL_DEVICE__,$__MINI_GLOBAL_MODE__, $__MINI_GLOBAL_TENANT_ID__;
$query = (new Model())
->where('is_examine', 1)
->where('posts_state', 0);
if(!$is_mutisite){
$query = $query->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__);
}
// 控制视频隐藏
if($__MINI_GLOBAL_DEVICE__ != 'mp'){
global $__MINI_GLOBAL_FRONT_VERSION__;
// todo: 临时
if($__MINI_GLOBAL_FRONT_VERSION__ >= '1.2.2.24'){
}else {
$query = $query->where('is_wechat_sph', 0);
}
}
if($__MINI_GLOBAL_MODE__ == 'examine'){
$query = $query->where('post_type', '<>', 'video');
}
if(true){
$data = $query->orderBy('degree', 'desc')->orderBy('created_at', 'desc')
->simplePaginate($limit, FieldUtils::postInfoColums());
}else{
$data = $query->withCount('like as like_count')
->orderBy('like_count', 'desc')
->simplePaginate($limit, FieldUtils::postInfoColums());
}
if($data){
if($is_mutisite){
PostUtils::$force_show_multi_site = true;
}
if($is_waterfall){
PostUtils::reset_loop();
$data->map(function ($v, $k) use ($uid){
return PostUtils::WaterfallProcess($k, $v, 1);
});
}else{
$data = self::postsParame($data, $uid, 1);//文章详情
}
return $data;
}
return null;
}
public static function getPostTags($post_id){
if(_empty_($post_id)){
return null;
}
return Cache::remember('get:post:tags:'.$post_id, 3600, function () use ($post_id){
$tagsIds = WxPostsTag::where('posts_id', $post_id)->pluck('tags_id');//话题ID列表
if($tagsIds){
return WxTag::whereIn('id', $tagsIds)->get(['tags_name', 'id']);//话题列表
}
return null;
});
}
public static function getPostGoods($post_id){
if(_empty_($post_id)){
return null;
}
return Cache::remember('get:post:goods:'.$post_id, 3600, function () use ($post_id){
$goodsIds = WxPostsGoods::where([['posts_id','=', $post_id],['type', '=', 0]])->pluck('goods_id');//商品ID列表
if($goodsIds){
return WxShopGoods::whereIn('id', $goodsIds)->get(['id', 'pic', 'name', 'credit_type', 'intro', 'price', 'vip_price', 'tk_platform_id', 'buys']);//商品列表
}
return null;
});
}
public static function getPostFiles($post_id){
if(_empty_($post_id)){
return null;
}
return Cache::remember('get:post:files:'.$post_id, 3600, function () use ($post_id){
return WxPostsFile::where('post_id', $post_id)->get();
});
}
public static function getPostSounds($post_id){
if(_empty_($post_id)){
return null;
}
return Cache::remember('get:post:sounds:'.$post_id, 3600, function () use ($post_id){
return WxPostsSound::where('post_id', $post_id)->get();
});
}
/**
* 板块推荐的笔记列表
* @param $uid
* @param int $limit
* @return mixed
*/
public static function getListByPlate($uid, $plate_id, $limit = 6)
{
global $__MINI_GLOBAL_DEVICE__,$__MINI_GLOBAL_MODE__;
$circleIds = WxCircle::where('plate_id', $plate_id)->pluck('id');
$query = (new Model())
->where('is_examine', 1)
->where('posts_state', 0);
// 控制视频隐藏
if($__MINI_GLOBAL_DEVICE__ != 'mp'){
global $__MINI_GLOBAL_FRONT_VERSION__;
// todo: 临时
if($__MINI_GLOBAL_FRONT_VERSION__ >= '1.2.2.24'){
}else {
$query = $query->where('is_wechat_sph', 0);
}
}
if($__MINI_GLOBAL_MODE__ == 'examine'){
$query = $query->where('post_type', '<>', 'video');
}
$data = $query->whereIn('circle_id', $circleIds)
->orderBy('id', 'desc')
->simplePaginate($limit);
$data = self::postsParame($data, $uid, 1);//文章详情
return $data;
}
/**
* 点赞
*/
public static function addLike($uid, $posts_id)
{
$like_count = PostsRepositores::likeCont($posts_id);
$posts_user_id = WxPost::where('id', $posts_id)->pluck('user_id')->first();
$isExists = WxLike::where('posts_user_id', $posts_user_id)
->where('posts_id', $posts_id)
->where('user_id', $uid)
->exists();
if (!$isExists) {
// 点赞
$wxLike = new WxLike();
$wxLike->posts_user_id = $posts_user_id;
$wxLike->posts_id = $posts_id;
$wxLike->user_id = $uid;
$wxLike->save();
$weight = WxPost::where('id', $posts_id)->value('weight');
$weight += ( Utils::getDecayCoefficient($like_count, 10) * 400);
if($weight > time()){
$weight = time();
}
WxPost::withoutSyncingToSearch(function () use ($posts_id, $weight){
WxPost::where('id', $posts_id)->update(['weight'=>timetostr($weight)]);
});
$circle_id = WxPost::where('id', $posts_id)->value('circle_id');
$circle_user_id = WxCircle::where('id', $circle_id)->value('user_id');
Redis::sadd('realtime:others:set', json_encode([$circle_id, 2, 1]));
Redis::sadd('realtime:others:set', json_encode([$posts_user_id, 4, 1]));
if($circle_user_id){
Redis::sadd('realtime:others:set', json_encode([$circle_user_id, 4, 1]));
}
$tagsIds = WxPostsTag::where('posts_id', $posts_id)->pluck('tags_id');//话题ID列表
if($tagsIds){
foreach ($tagsIds as $tag_id){
Redis::sadd('realtime:others:set', json_encode([$tag_id, 5, 1]));
}
}
$user = UserUtils::get_cached_user($uid);
if($user['id'] != $posts_user_id){
UserUtils::add_user_notice(2003, $posts_user_id, '收到了一个喜欢', '您的笔记收到了「' . ''.$user['user_name'].'' . '」的喜欢。', 101, $posts_id);
}
UserUtils::add_user_experience($user['id'], 3);
SubscribeMessageJob::dispatch('beliked', $wxLike->id);
} else {
$weight = WxPost::where('id', $posts_id)->value('weight');
$weight -= ( Utils::getDecayCoefficient($like_count, 10) * 400);
if($weight > time()){
$weight = time();
}
WxPost::withoutSyncingToSearch(function () use ($posts_id, $weight){
WxPost::where('id', $posts_id)->update(['weight'=>timetostr($weight)]);
});
// 取消点赞
(new WxLike())->where('posts_user_id', $posts_user_id)
->where('posts_id', $posts_id)
->where('user_id', $uid)
->delete();
(new WxNotice())->where('posts_id', $posts_id)
->where('user_id', $posts_user_id)
->where('notice_type', 2)
->delete();
}
update_user_visit($uid, 1, $posts_id);
Cache::forget('posts:likeCont:'.$posts_id);
Cache::forget('user:beliked:count:'.$posts_user_id);
Cache::forget($uid.':like:post:'.$posts_id);
Redis::sadd('realtime:post:set', $posts_id);
}
/**
* 收藏
*/
public static function addCollect($uid, $posts_id)
{
$posts_user_id = WxPost::where('id', $posts_id)->value('user_id');
$isExists = WxCollect::where('posts_user_id', $posts_user_id)
->where('posts_id', $posts_id)
->where('user_id', $uid)
->exists();
if (!$isExists) {
// 收藏
$wxLike = new WxCollect();
$wxLike->posts_user_id = $posts_user_id;
$wxLike->posts_id = $posts_id;
$wxLike->user_id = $uid;
$wxLike->save();
$user = UserUtils::get_cached_user($uid);
$circle_id = WxPost::where('id', $posts_id)->value('circle_id');
$circle_user_id = WxCircle::where('id', $circle_id)->value('user_id');
Redis::sadd('realtime:others:set', json_encode([$circle_id, 2, 1]));
Redis::sadd('realtime:others:set', json_encode([$posts_user_id, 4, 1]));
if($circle_user_id){
Redis::sadd('realtime:others:set', json_encode([$circle_user_id, 4, 1]));
}
$tagsIds = WxPostsTag::where('posts_id', $posts_id)->pluck('tags_id');//话题ID列表
if($tagsIds){
foreach ($tagsIds as $tag_id){
Redis::sadd('realtime:others:set', json_encode([$tag_id, 5, 1]));
}
}
if($posts_user_id != $user['id']){
UserUtils::add_user_notice(2006, $posts_user_id, '收到了一个收藏', '您的笔记被「' . ''.$user['user_name'].'' . '」收藏啦!', 101, $posts_id);
UserUtils::add_user_experience($user['id'], 4);
}
} else {
// 取消收藏
(new WxCollect())->where('posts_user_id', $posts_user_id)
->where('posts_id', $posts_id)
->where('user_id', $uid)
->delete();
(new WxNotice())->where('posts_id', $posts_id)
->where('user_id', $posts_user_id)
->where('notice_type', 3)
->delete();
}
Redis::sadd('realtime:post:set', $posts_id);
Cache::forget('user:becollected:count:'.$posts_user_id);
Cache::forget('post:becollected:count:'.$posts_id);
Cache::forget($uid.':collect:post:'.$posts_id);
}
public static function addBlur($posts_id){
$post = WxPost::find($posts_id);
if($post){
if($post->is_blur){
$post->is_blur = 0;
return $post->save();
}else{
$post->is_blur = 1;
return $post->save();
}
}
return false;
}
public static function addSticky($scene, $posts_id){
if($scene == 1 || $scene == 10 || $scene == 9){
$post = WxPost::find($posts_id);
if($post){
if($post->is_sticky){
return WxPost::where('id', $posts_id)->update([
'is_sticky' => 0
]);
}else{
return WxPost::where('id', $posts_id)->update([
'is_sticky' => 1
]);
}
}
}else if($scene == 5 || $scene == 8){
$user_id = WxPost::where('id', $posts_id)->value('user_id');
if($user_id){
$sticky_post = get_user_meta($user_id, 'sticky', 'n');
if($sticky_post == $posts_id){
// 取消置顶
$posts_id = 0;
}
if(update_user_meta($user_id, 'sticky', $posts_id, 'n')){
return true;
}
}
}else if( $scene == 6 || $scene == 60001 ){
$circle_id = WxPost::where('id', $posts_id)->value('circle_id');
if($circle_id){
if($scene == 60001){
$sticky_posts = _empty_default_(get_circle_meta($circle_id, 'sticky_notice_list', 'j'), []);
$sticky_posts = array_values($sticky_posts);
$flag = -1;
if($sticky_posts){
_logger_(__file__, __line__, $sticky_posts);
for ($i = 0; $i < count($sticky_posts); $i++){
if($sticky_posts[$i] == $posts_id){
$flag = 1;
unset($sticky_posts[$i]);
break;
}
}
}
if($flag == -1){
$sticky_posts[] = $posts_id;
}
$sticky_posts = array_unique($sticky_posts);
if(update_circle_meta($circle_id, 'sticky_notice_list', $sticky_posts, 'j')){
return true;
}
}else{
$sticky_post = get_circle_meta($circle_id, 'sticky', 'n');
if($sticky_post == $posts_id){
// 取消置顶
$posts_id = 0;
}
if(update_circle_meta($circle_id, 'sticky', $posts_id, 'n')){
return true;
}
}
}
}
return false;
}
/**
* 关注
* @param $uid
* @param $user_follow_id
*/
public static function followUser($uid, $user_follow_id)
{
$isExists = WxUserFollow::where('user_follow_id', $user_follow_id)
->where('user_id', $uid)
->exists();
if (!$isExists) {
// 不能关注自己
if ($user_follow_id != $uid) {
// 关注
$wxLike = new WxUserFollow();
$wxLike->user_follow_id = $user_follow_id;
$wxLike->user_id = $uid;
$wxLike->save();
}
} else {
// 取消关注
(new WxUserFollow())->where('user_follow_id', $user_follow_id)
->where('user_id', $uid)
->delete();
}
}
/**
* 删除笔记
*/
public static function deletePosts($posts_id, $posts_state = 1)
{
DB::beginTransaction();
try {
// 删除文章(真删)
// $addressId = WxPost::where('id', $posts_id)->pluck('address_id')->first();
// WxPost::destroy($posts_id);
// WxPostsAddress::where('id', $addressId)->delete();
// WxPostsImg::where('posts_id', $posts_id)->delete();
// WxPostsVideo::where('posts_id', $posts_id)->delete();
// WxPostsTag::where('posts_id', $posts_id)->delete();
// 删除文章(假删)
$post = WxPost::find($posts_id);
if($post){
if($post->circle_id && $post->circle_id > 0){
$sticky_post1 = get_circle_meta($post->circle_id, 'sticky', 'n');
if($sticky_post1 == $posts_id){
update_circle_meta($post->circle_id, 'sticky', 0, 'n');
}
$sticky_posts2 = _empty_default_(get_circle_meta($post->circle_id, 'sticky_notice_list', 'j'), []);
$sticky_posts2 = array_values($sticky_posts2);
$flag = -1;
if($sticky_posts2){
for ($i = 0; $i < count($sticky_posts2); $i++){
if($sticky_posts2[$i] == $posts_id){
$flag = 1;
unset($sticky_posts2[$i]);
break;
}
}
}
update_circle_meta($post->circle_id, 'sticky_notice_list', $sticky_posts2, 'j');
}
// 主页置顶
$sticky_post3 = get_user_meta($post->user_id, 'sticky', 'n');
if($sticky_post3 == $posts_id){
update_user_meta($post->user_id, 'sticky', 0, 'n');
}
}
WxPost::where('id', $posts_id)->update(['posts_state' => $posts_state]);
Db::commit();
return true;
} catch (\Exception $e) {
DB::rollBack();
_logger_(__file__, __line__, $e->getMessage());
return false;
}
}
/**
* 0:我发布的,1: 我收藏的;2:我喜欢的 3:我充电的
* @param $uid
* @param int $limit
* @return mixed
*/
public static function myPosts($uid, $limit = 10, $type = 0, $orderBy = 'id', $order = 'desc')
{
if ($type == 'post') {
return self::mySendPosts($uid, $limit);
} elseif ($type == 'collected') {
return self::myCollectPosts($uid, $limit);
} elseif ($type == 'liked') {
return self::myLikePosts($uid, $limit);
} elseif ($type == 'exceptionaled') {
return self::myExceptionalPosts($uid, $limit);
} elseif ($type == 'voted') {
return self::myVotePosts($uid, $limit);
} elseif ($type == 'shop') {
return self::myShopGoods($uid, $limit, $orderBy, $order);
}
}
public static function myShopGoods($uid, $limit = 10, $orderBy = 'id', $order = 'desc'){
$shop_id = WxUser::where('id', $uid)->value('shop_id');
if(UserUtils::is_mini_supder_admin($uid)){
$data = WxShopGoods::whereIn('shop_id', [$shop_id, 0])->where('state', 1)->orderBy($orderBy, $order)->paginate($limit);
}else{
$data = WxShopGoods::where('shop_id', $shop_id)->where('state', 1)->orderBy($orderBy, $order)->paginate($limit);
}
if($data){
return $data;
}
return null;
}
/** 我发起的投票的帖子
* @param $uid
* @param int $limit
* @return mixed
*/
public static function myVotePosts($uid, $limit = 10){
$votedPostIdsSubQuery = DB::table('wx_posts_vote')
->where('user_id', $uid)
->select('post_id');
$data = WxPost::whereIn('id', $votedPostIdsSubQuery)
->paginate($limit);
if($data){
$data = self::postsParame($data, $uid,5);//文章详情
return $data;
}
return null;
}
/**
* 我发的笔记
* @return mixed
*/
public static function mySendPosts($uid, $limit = 10)
{
global $__MINI_GLOBAL_CURRENT_USER_ID__;
global $__MINI_GLOBAL_DEVICE__,$__MINI_GLOBAL_MODE__,$__MINI_GLOBAL_SCENE__;
$query = (new Model())
->withCount('like as like_count');
$uid_ = $uid;
if($uid < 0){
$uid_ = $uid;
$uid = _abs($uid);
$query = $query->where('is_anonymous', 1);
}else{
if($uid == $__MINI_GLOBAL_CURRENT_USER_ID__ && $__MINI_GLOBAL_SCENE__ == 5){
}else{
$query = $query->where('is_anonymous', 0);
}
}
if($uid == $__MINI_GLOBAL_CURRENT_USER_ID__){
if($__MINI_GLOBAL_SCENE__ == 8){
$query = $query->where('is_examine', 1);
}
}else{
$query = $query->where('is_examine', 1);
}
$is_waterfall = Settings::get('app_user_list_style', 0) == 1;
$user_setting_style = mini_current_user(2, 'diy_scene_8_post_list_style');
if($user_setting_style){
if($user_setting_style != 1){
if($user_setting_style == 3){
$is_waterfall = true;
}else{
$is_waterfall = false;
}
}
}
$sticky_post = get_user_meta($uid, 'sticky', 'n');
if($sticky_post){
$query = $query->where('id', '<>', $sticky_post);
}
$query = $query->where('user_id', $uid);
if($__MINI_GLOBAL_SCENE__ == 5 && $uid == $__MINI_GLOBAL_CURRENT_USER_ID__){
$query = $query->whereIn('posts_state', [0, 2]);
}else{
$query = $query->where('posts_state', 0);
}
// 控制视频隐藏
if($__MINI_GLOBAL_DEVICE__ != 'mp'){
global $__MINI_GLOBAL_FRONT_VERSION__;
// todo: 临时
if($__MINI_GLOBAL_FRONT_VERSION__ >= '1.2.2.24'){
}else {
$query = $query->where('is_wechat_sph', 0);
}
}
if($__MINI_GLOBAL_MODE__ == 'examine'){
$query = $query->where('post_type', '<>', 'video');
}
$data = $query->orderBy('id', 'desc')->paginate($limit);
if($is_waterfall){
PostUtils::reset_loop();
$data->map(function ($v, $k) use ($uid){
return PostUtils::WaterfallProcess($k, $v, 5);
});
}else {
$data = PostsRepositores::postsParame($data, $uid,5);
}
if($uid_ > 0 && request()->page == 1 && $sticky_post){
$data2 = WxPost::where('id', $sticky_post)->whereIn('posts_state', [0, 2])->get();
if($data2){
if($is_waterfall){
PostUtils::reset_loop();
$data2->map(function ($v, $k) use ($uid){
return PostUtils::WaterfallProcess($k, $v,5);
});
}else {
$data2 = PostsRepositores::postsParame($data2, $uid,5);
}
$data = json_decode($data->toJson(), true);
$data['data'] = $data2->concat($data['data']);
}
}
return $data;
}
/**
* 我收藏的文章列表
* @param $uid
* @param int $limit
* @return mixed
*/
public static function myCollectPosts($uid, $limit = 10)
{
$postsIds = WxCollect::where('user_id', $uid)->pluck('posts_id');
$data = (new Model())
->where('is_examine', 1)
->where('posts_state', 0)
->whereIn('id', $postsIds)
->orderBy('id', 'desc')
->paginate($limit);
$data = self::postsParame($data, $uid, 5);//文章详情
return $data;
}
/**
* 我喜欢的
* @return mixed
*/
public static function myLikePosts($uid, $limit = 10)
{
$postsIds = WxLike::where('user_id', $uid)->pluck('posts_id');
$data = (new Model())
->where('is_examine', 1)
->where('posts_state', 0)
->whereIn('id', $postsIds)
->paginate($limit);
$data = self::postsParame($data, $uid, 5);//文章详情
return $data;
}
/**
* 我充电的
* @return mixed
*/
public static function myExceptionalPosts($uid, $limit = 10)
{
$postsIds = WxExceptional::where('user_id', $uid)->orderBy('id', 'desc')->pluck('posts_id')->toArray();
$data = (new Model())
->where('is_examine', 1)
->where('posts_state', 0)
->whereIn('id', $postsIds)->orderBy(DB::raw('FIND_IN_SET(id, "' . implode(",", $postsIds) . '"' . ")"))
->paginate($limit);
$data = self::postsParame($data, $uid, 5);//文章详情
return $data;
}
/**
* 根据圈子ID获取最新和最热的笔记
* @param $circle_id
* @param $type 0:最新,1:最热
*/
public static function postsByCircleId($circle_id, $type, $limit, $uid, $page, $tags, $sort, $city = '', $is_tenant = false)
{
$is_waterfall = CircleUtils::is_list_waterfall($circle_id);
global $__MINI_GLOBAL_DEVICE__,$__MINI_GLOBAL_MODE__,$__MINI_GLOBAL_TENANT_ID__;
if ($type == 0) {
$sorts = Settings::get('app_circle_list_sort', ['weight', 'created_at', 'id'], true);
$sticky_post = get_circle_meta($circle_id, 'sticky', 'n');
$sticky_notice_posts = get_circle_meta($circle_id, 'sticky_notice_list', 'j');
$data = (new Model())
->where('is_examine', 1)
->where('posts_state', 0)
->where('circle_id', $circle_id);
if($is_tenant){
$data = $data->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__);
}
if($sticky_post){
$data = $data->Where('id', '<>', $sticky_post);
}
if($city){
if(StrUtils::endsWith($city, '市')){
$data = $data->where('city', $city);
}else{
$data = $data->where('city', $city.'市');
}
}
// 控制视频隐藏
if($__MINI_GLOBAL_DEVICE__ != 'mp'){
global $__MINI_GLOBAL_FRONT_VERSION__;
// todo: 临时
if($__MINI_GLOBAL_FRONT_VERSION__ >= '1.2.2.24'){
}else {
$data = $data->where('is_wechat_sph', 0);
}
}
if($__MINI_GLOBAL_MODE__ == 'examine'){
$data = $data->where('post_type', '<>', 'video');
}
$not_like_post_set = get_user_meta($uid, 'feedback:notlike:post:set', 'j');
$not_like_user_set = get_user_meta($uid, 'feedback:notlike:user:set', 'j');
if(!_empty_($not_like_user_set)){
$data = $data->whereNotIn('user_id', $not_like_user_set);
}
if(!_empty_($not_like_post_set)){
$data = $data->whereNotIn('id', $not_like_post_set);
}
if(!_empty_($sticky_notice_posts)){
$data = $data->whereNotIn('id', $sticky_notice_posts);
}
if($tags && is_array($tags)){
$data = $data->whereIn('id', function ($query) use ($tags) {
$query->select('posts_id')
->from('wx_posts_tags')
->whereIn('tags_id', $tags);
});
}
//
// if($sticky_post){
// $data = $data->orderByRaw(DB::raw("CASE WHEN id = $sticky_post then 1 else 0 end"), 'desc');
// }
if($sort == 1){
$data = $data->orderBy('last_reply_at', 'desc')->orderBy('id', 'desc');
}else if($sort == 2){
$data = $data->orderBy('degree', 'desc')->orderBy('id', 'desc');
}else{
if($sorts){
foreach ($sorts as $sort_){
$data = $data->orderBy($sort_, 'desc');
}
}else{
$data = $data->orderBy('id', 'desc');
}
}
$data = $data->simplePaginate($limit);
if($is_tenant){
PostUtils::$force_show_multi_site = false;
}
if($is_waterfall){
PostUtils::reset_loop();
$data->map(function ($v, $k) use ($uid){
return PostUtils::WaterfallProcess($k, $v, 6);
});
}else {
$data = self::postsParame($data, $uid,6);//文章详情
}
if($page == 1 && $sticky_post){
$data2 = WxPost::where('id', $sticky_post)->where('is_examine', 1)->where('posts_state', 0)->get();
if($data2){
if($is_waterfall){
PostUtils::reset_loop();
$data2->map(function ($v, $k) use ($uid){
return PostUtils::WaterfallProcess($k, $v,6);
});
}else {
$data2 = self::postsParame($data2, $uid, 6);//文章详情
}
$data = json_decode($data->toJson(), true);
$data['data'] = $data2->concat($data['data']);
}
}
return $data;
}elseif ($type == 999) {
// 验证权限
if(UserUtils::is_mini_admin($uid) || UserUtils::is_circle_admin($circle_id, $uid)){
// 审核中的笔记
$data = (new Model())
->where('is_examine', 0)
->where('circle_id', $circle_id)
->orderBy('id', 'desc')
->simplePaginate($limit);
if($is_waterfall){
PostUtils::reset_loop();
$data->map(function ($v, $k) use ($uid){
return PostUtils::WaterfallProcess($k, $v, 6);
});
}else {
$data = self::postsParame($data, $uid, 6);//文章详情
}
return $data;
}
return null;
}
}
/**
* 搜索
* @param $keyword
* @param int $uid
* @param int $limit
* @return mixed
*/
public static function searchPosts($keyword, $uid = 0, $limit = 10, $is_tenant = false)
{
global $__MINI_GLOBAL_TENANT_ID__;
WxSearchRepositores::record($uid, $keyword);
$query = (new Model())
->where('is_examine', 1)
->where('posts_state', 0);
if($is_tenant){
$query = $query->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__);
}
$data = $query->where(function ($query) use ($keyword) {
if(is_numeric($keyword)){
$query->orWhere('id', $keyword)
->orWhere('posts_content', 'like', '%' . $keyword . '%');
}else{
$query->orWhere('posts_content', 'like', '%' . $keyword . '%');
}
})->orderBy('id', 'desc')->simplePaginate($limit);
if($is_tenant){
PostUtils::$force_show_multi_site = false;
}
$data = self::postsParame($data, $uid, 10);//文章详情
return $data;
}
public static function relevantPosts($post_id, $uid = 0, $limit = 10)
{
$result = [];
if(SearchUtils::is_use_es()){
try{
global $__MINI_GLOBAL_TENANT_ID__;
$keyword = str_replace('?', '?', PostUtils::get_the_title('', $post_id));
$data = WxPost::search($keyword)->where('is_examine', 1)->where('posts_state', 0)->where('tenant_id', $__MINI_GLOBAL_TENANT_ID__)->simplePaginate($limit);
PostUtils::reset_loop();
$data->map(function ($v, $k) use ($uid){
return PostUtils::WaterfallProcess($k, $v,90001);
});
$result = json_decode($data->toJson(), true);
$data_ = [];
if($result['data']){
foreach ($result['data'] as $obj){
if($obj['id'] != $post_id && $obj['is_examine'] == 1 && $obj['posts_state'] == 0 && $obj['tenant_id'] == $__MINI_GLOBAL_TENANT_ID__){
$data_[] = $obj;
}
}
}
$result['data'] = $data_;
}catch (\Exception $e){
if(strpos($e->getMessage(), '404 Not Found') !== false){
ElasticSearchBatchSyncJob::dispatch(0, 1000, 'post');
}else{
_logger_(__file__, __line__, $e->getMessage());
}
}
}
return $result;
}
/**
* 商品晒单
* @return mixed
*/
public static function goodsBaskPosts($uid, $gid)
{
$postsIds = WxPostsGoods::where('goods_id', $gid)->pluck('posts_id');//笔记ID列表
$data = (new Model())
->where('is_examine', 1)
->where('posts_state', 0)
->whereIn('id', $postsIds)
->paginate(6);
$data = self::postsParame($data, $uid);//文章详情
return $data;
}
// $style样式位置
public static function postsParame($data, $uid, $scene = 0, $actree = null)
{
if($data){
PostUtils::reset_loop();
$data->map(function ($v, $k) use ($uid, $scene, $actree) {
return PostUtils::defaultProcess($k, $v, $scene, $actree);
});
$data->append(['format_time', 'is_ellipsis', 'is_content_beyond', 'ip_address']);
return $data;
}
return null;
}
/**
* 获取笔记喜欢数量
*/
public static function likeCont($postId)
{
return (int)(Cache::remember('posts:likeCont:'.$postId, 36000, function () use ($postId){
return WxLike::where(['posts_id' => $postId])->count();
}));
}
/**
* 获取笔记充电总数
*/
public static function exceptionalCont($postId)
{
return (int)Cache::remember('exceptional:cont:'.$postId, 3600, function () use ($postId){
return WxExceptional::where(['posts_id' => $postId])->count();
});
}
/**
* 获取笔记充电列表(只显示头像)
*/
public static function exceptionalList($postId)
{
if(_empty_($postId)){
return null;
}
return Cache::remember('post:exceptional:list:'.$postId, 3600, function () use ($postId){
$ids = WxExceptional::where('posts_id', $postId)
->groupBy('user_id')
->pluck('user_id');//充电用户ID列表
if($ids){
return WxUser::whereIn('id', $ids)->limit(6)->get(['user_avatar']);
}
return null;
});
}
}