123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683 |
- <?php
- namespace App\Http\Controllers\Api\Repositories;
- use App\Jobs\AttachIpAddressJob;
- use App\Jobs\Search\ElasticSearchBatchSyncJob;
- use App\Jobs\SubscribeMessageJob;
- use App\Models\Posts\WxComment;
- use App\Models\Posts\WxPostsFile;
- use App\Models\Posts\WxPostsPayContent;
- use App\Models\Shop\WxShopGoods;
- use App\Models\Posts\WxPostsSound;
- use App\Models\Posts\WxPostsField;
- use App\Wen\Utils\CircleUtils;
- use App\Wen\Utils\FieldUtils;
- use App\Wen\Utils\FileUtils;
- use App\Wen\Utils\ImageUtils;
- use App\Wen\Utils\PostUtils;
- use App\Wen\Utils\SearchUtils;
- use App\Wen\Utils\StrUtils;
- use App\Wen\Utils\UserUtils;
- use App\Wen\Utils\Utils;
- use App\Models\Posts\WxCollect;
- use App\Models\Posts\WxLike;
- use App\Models\Posts\WxPost;
- use App\Models\Posts\WxPost as Model;
- use App\Models\Posts\WxPostsAddress;
- use App\Models\Posts\WxPostsImg;
- use App\Models\Posts\WxPostsTag;
- use App\Models\Posts\WxPostsVideo;
- use App\Models\Posts\WxPostsVote;
- use App\Models\Posts\WxTag;
- use App\Models\Circle\WxCircle;
- use App\Models\WxExceptional;
- use App\Models\WxNotice;
- use App\Models\Posts\WxPostsGoods;
- use App\Models\WxSyncRelation;
- use App\Models\User\WxUser;
- use App\Models\User\WxUserFollow;
- use Illuminate\Support\Carbon;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use App\Wen\Utils\Settings;
- use Illuminate\Support\Facades\Redis;
- class PostsRepositores
- {
- /**
- * Model.
- *
- * @var string
- */
- protected $eloquentClass = Model::class;
- public static function add($uid, $is_examine, $posts_title, $contents, $circle_id, $tags, $goods, $images, $video, $address, $videoThumbUrl, $video_width, $video_height, $votes = [], $files = [], $sounds = [], $pay_content_id = 0, $device = '', $refer_id = 0, $contact_phone = '', $sph_obj = null, $useds = [], $bureaus = [], $is_anonymous = 0, $voter_id = 0,$photo_url='',$is_draft)
- {
- DB::beginTransaction();
- $user = UserUtils::get_cached_user($uid);
- $circle = null;
- if(_empty_($refer_id) && $circle_id){
- $circle = WxCircle::where('id', $circle_id)->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 = '<mini-icon name="mini-icon mini-fenxiang2"></mini-icon> '.$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, '笔记被转发', '「<a href="/pages/user/user?id='.$user['id'].'">'.$user['user_name'].'</a>」' . '转发了您的笔记。', 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'], '您创建的圈子「' . '<a href="/pages/circle/list?id='.$circle['id'].'">'.$circle['circle_name'].'</a>' . '」有一篇新的内容等待您去审核哦,请务必在24小时内进行处理。');
- }else{
- //给对应圈子圈主发送新内容信息
- UserUtils::assistant_notice($circle['user_id'], '您创建的圈子「' . '<a href="/pages/circle/list?id='.$circle['id'].'">'.$circle['circle_name'].'</a>' . '」有一篇新的内容发布,您要好好把关哦。');
- }
- }
- 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, '收到了一个喜欢', '您的笔记收到了「' . '<a href="/pages/user/user?id='.$user['id'].'">'.$user['user_name'].'</a>' . '」的喜欢。', 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, '收到了一个收藏', '您的笔记被「' . '<a href="/pages/user/user?id='.$user['id'].'">'.$user['user_name'].'</a>' . '」收藏啦!', 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;
- });
- }
- }
|