3264 lines
136 KiB
PHP
3264 lines
136 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Models\ProfilePhoto;
|
||
use App\Models\AreaUser;
|
||
use App\Models\UserPreview;
|
||
use App\Services\UserService;
|
||
use Illuminate\Support\Facades\DB;
|
||
use App\Models\Live\Task;
|
||
use App\Facades\QrcodeRectService;
|
||
use App\Models\CommunityStar;
|
||
use Illuminate\Support\Facades\Auth;
|
||
use App\Models\ConsultationRecords;
|
||
use App\Models\UserMember;
|
||
use Illuminate\Container\Container as App;
|
||
use App\Models\ProfileCourtship;
|
||
use App\Repositories\Eloquent\SmsRepository as Sms;
|
||
use App\Models\Live\LiveBanner;
|
||
use App\Services\LiveAlipayService;
|
||
use App\Models\Score\CashoutLog;
|
||
use EasyWeChat\Factory;
|
||
use App\Services\IMService;
|
||
use App\Models\CommunityGroupAdmin;
|
||
use App\Models\Live\Live;
|
||
use App\Models\Live\Anchor;
|
||
use App\Models\PayOrder;
|
||
use App\Models\Live\Viewer;
|
||
use App\Models\CommunityTopic;
|
||
use Illuminate\Support\Facades\Log;
|
||
use SimpleSoftwareIO\QrCode\Facades\QrCode;
|
||
use App\Models\Order;
|
||
use Illuminate\Http\Request;
|
||
use App\Models\Community;
|
||
use App\Models\CommunityMember;
|
||
use App\Models\User;
|
||
use App\Models\MerchantUsers;
|
||
use App\Models\CommunityGroup;
|
||
use App\Models\CommunityGroupLink;
|
||
use App\Models\CommunityComplaintHistory;
|
||
use App\Models\CommunityMoment;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use App\Models\KeywordHistory;
|
||
use App\Models\HotKeyword;
|
||
use App\Models\CommunityMomentComplaint;
|
||
use Illuminate\Support\Facades\Redis;
|
||
use Symfony\Component\HttpKernel\Profiler\Profile;
|
||
use function foo\func;
|
||
use App\Jobs\DoTask;
|
||
use App\Models\AccessRecord;
|
||
use App\Models\CommunityActivity;
|
||
use App\Models\ConsultAccount;
|
||
use App\Models\MerchantAccount;
|
||
use App\Models\TouristOrder;
|
||
use App\Models\WangYiYunUser;
|
||
use App\Models\Consultation;
|
||
use App\Models\MEarning;
|
||
use App\Models\MEarningAccount;
|
||
use App\Models\MEarningTransfers;
|
||
use App\Models\MEarningwithdraws;
|
||
use App\Models\MerchantShop;
|
||
use App\Models\Message;
|
||
use App\Models\ShopRecive;
|
||
use Illuminate\Pagination\LengthAwarePaginator;
|
||
use App\Models\QAAnswer;
|
||
use App\Models\QATest;
|
||
use App\Models\QATestRecord;
|
||
use App\Models\Server\MerchantUser;
|
||
use App\Models\Server\SaasNotice;
|
||
use App\Utils\Messenger;
|
||
use Illuminate\Pagination\Paginator;
|
||
|
||
class CommunityController extends Controller
|
||
{
|
||
|
||
protected $user;
|
||
|
||
public function __construct(Request $request){
|
||
//// $this->middleware(function($request, $next){
|
||
// $wechatUser = session('wechat.oauth_user.new');
|
||
// if(empty($wechatUser)){
|
||
// if(config('app.env') == 'alpha'){
|
||
// $viewer = Viewer::where('openid', $request->openid)->first();
|
||
// }elseif (config('app.env') == 'local') {
|
||
// $viewer = Viewer::where('id', 1)->first();
|
||
// }
|
||
// $this->user = User::where('mobile', $viewer->mobile)->first();
|
||
// }else{
|
||
// $openid = $wechatUser->getId();
|
||
// $mobile = Viewer::where('openid', $openid)->value('mobile');
|
||
// $this->user = User::where('mobile', $mobile)->first();
|
||
// }
|
||
//// });
|
||
}
|
||
|
||
/**
|
||
* 社群详情
|
||
* @param Request $request [description]
|
||
* @param Community $community [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function community(Request $request, Community $community)
|
||
{
|
||
$community->increment('click_num', 1);
|
||
$members = $community->members()->with(['user:id,name,photo,type,circle_avatar,app_avatar,nickname', 'profile:id,weight,stature,city,introduction'])->whereHas('user', function ($sql) {
|
||
$sql->whereNotNull('id');
|
||
})
|
||
->where('status', 1)->orderBy('id', 'asc')->paginate();
|
||
// $members_data = [];
|
||
// foreach ($members as &$member) {
|
||
// $photo = '';
|
||
// $type = null;
|
||
// if ($member->user) {
|
||
// $photo = $member->user->photo;
|
||
// $type = $member->user->type;
|
||
// }
|
||
// $member->user_id = $member->user_id;
|
||
// $member->photo = $photo.'?x-oss-process=image/auto-orient,1/resize,m_fill,w_64,h_64/quality,q_90';
|
||
// $member->type = $type;
|
||
// if($member->profile){
|
||
// $member->weight = $member->profile->weight;
|
||
// $member->stature = $member->profile->stature;
|
||
// $member->city = $member->profile->city;
|
||
// $member->introduction = $member->profile->introduction;
|
||
// }
|
||
//// $members_data[] = $data;
|
||
// }
|
||
|
||
$community->user = $community->user()->select('id', 'name', 'photo', 'circle_avatar', 'nickname', 'app_avatar')->first();
|
||
$community->members = $members;
|
||
//是否已报名
|
||
$user = auth()->user();
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
if ($user) {
|
||
$is_applied = $community->isApplied($user->id);
|
||
$is_photo = $user->photo ? 1 : 0;
|
||
$is_owner = $community->user_id == $user->id ? 1 : 0;
|
||
} else {
|
||
$is_applied = 0;
|
||
$is_photo = 0;
|
||
$is_owner = 0;
|
||
}
|
||
$community->is_applied = $is_applied;
|
||
//是否有头像
|
||
$community->is_photo = $is_photo;
|
||
$nickname = $request->session()->get('nickname');
|
||
$avatar = $request->session()->get('avatar');
|
||
$community->nickname = $nickname;
|
||
$community->avatar = $avatar;
|
||
|
||
//群主是否正在直播
|
||
$live = '';
|
||
$live = Live::find(110);
|
||
// $owner_mobile = User::where('id', $community->user_id)->value('mobile');
|
||
// if($owner_mobile){
|
||
// $viewer_id = Viewer::where('mobile', $owner_mobile)->value('id');
|
||
// if($viewer_id){
|
||
// $anchor_id = Anchor::where('viewer_id', $viewer_id)->value('id');
|
||
// if($anchor_id){
|
||
// $live = Live::where('anchor_id', $anchor_id)->where('status', 1)->where('is_show', 1)->first();
|
||
// }
|
||
// }
|
||
// }
|
||
$community->live = $live;
|
||
|
||
//是否是群主
|
||
$community->is_owner = $is_owner;
|
||
return $this->success('ok', $community);
|
||
}
|
||
|
||
/**
|
||
* 社区群员列表
|
||
* @param Request $request [description]
|
||
* @param CommunityMember $member [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function communityMembers(Request $request, Community $community)
|
||
{
|
||
$members = $community->members()->with(['user:id,photo,name,sex,nickname,circle_avatar', 'profile:id,weight,stature,city,introduction,birthday,type'])->where('status', 1)->paginate();
|
||
foreach ($members as &$member) {
|
||
if (!empty($member->profile->birthday)) {
|
||
$member->profile->age = $this->getAge($member->profile->birthday);
|
||
}
|
||
}
|
||
return $this->success('ok', $members);
|
||
}
|
||
|
||
/**
|
||
* 申请社区成员
|
||
* @param Request $request [description]
|
||
* @param Community $community [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function applyCommunityMember(Request $request, Community $community, CommunityMember $member)
|
||
{
|
||
$user = auth()->user();
|
||
//头像是否审核过
|
||
// if (empty($user->photo)) {
|
||
// return $this->failure('申请失败,头像审核未通过,请上传个人真实照片');
|
||
// }
|
||
//判断是否申请过
|
||
$member->firstOrCreate([
|
||
'user_id' => $user->id,
|
||
'community_id' => $community->id,
|
||
'status' => 1
|
||
]);
|
||
//添加会员数
|
||
$community->increment('member_num', 1);
|
||
return $this->success('申请成功', $member);
|
||
}
|
||
|
||
public function communityGroups(Request $request, CommunityGroup $group)
|
||
{
|
||
$groups = $group->orderBy('num');
|
||
$keyword = $request->input('keyword');
|
||
if ($keyword) {
|
||
$keyword = trim($keyword);
|
||
$groups = $groups->where('title', 'like', '%' . $keyword . '%');
|
||
}
|
||
$groups = $groups->where('is_show', 1)->paginate(30);
|
||
// $nopage = $request->input('nopage', 0);
|
||
// if ($nopage) {
|
||
// $groups = $groups->get();
|
||
// }else{
|
||
// $groups = $groups->paginate(56);
|
||
// }
|
||
return $this->success('ok', $groups);
|
||
}
|
||
|
||
public function communityGroup(Request $request, CommunityGroup $group)
|
||
{
|
||
$communities = $group->groupCommunities();
|
||
$log = $group->joinCommunitiesLog();
|
||
return $this->success('ok', compact('group', 'communities', 'log'));
|
||
}
|
||
|
||
public function complaintCommunity(Request $request, Community $community)
|
||
{
|
||
$content = $request->input('content');
|
||
if (empty($content)) {
|
||
return $this->failure('请输入举报内容');
|
||
}
|
||
$history = new CommunityComplaintHistory;
|
||
$user_id = $request->id ?: 7776;
|
||
$history->user_id = $user_id;
|
||
$history->community_id = $community->id;
|
||
$history->content = $content;
|
||
$history->status = 0;
|
||
$history->save();
|
||
$community->is_hided = 1;
|
||
$community->save();
|
||
return $this->success('ok', $history);
|
||
}
|
||
|
||
public function userCommunities(Request $request, User $user, Community $community)
|
||
{
|
||
$communities = $community->where('is_hided', 0)->where('user_id', $user->id)->orderBy('id', 'desc')->paginate();
|
||
$community_count = $community->where('user_id', $user->id)->count() ?: 0;
|
||
$community_member_num = $community->where('user_id', $user->id)->sum('member_num') ?: 0;
|
||
$community_click_num = $community->where('user_id', $user->id)->sum('click_num') ?: 0;
|
||
return $this->success('ok', compact('user', 'communities', 'community_count', 'community_member_num', 'community_click_num'));
|
||
}
|
||
|
||
public function userCommunitiesV2(Request $request, Community $community)
|
||
{
|
||
try {
|
||
$user = auth()->user();
|
||
$communities = $community->where('user_id', $user->id)->orderBy('id', 'desc')->paginate(56);
|
||
$community_count = $community->where('user_id', $user->id)->count() ?: 0;
|
||
$community_member_num = $community->where('user_id', $user->id)->sum('member_num') ?: 0;
|
||
$community_click_num = $community->where('user_id', $user->id)->sum('click_num') ?: 0;
|
||
return $this->success('ok', compact('user', 'communities', 'community_count', 'community_member_num', 'community_click_num'));
|
||
} catch (\Exception $e) {
|
||
return $this->failure('获取信息失败');
|
||
}
|
||
|
||
}
|
||
|
||
public function userJoinedCommunities(Request $request, $user_id = null, CommunityMember $member, Community $community)
|
||
{
|
||
if (empty($user_id)) {
|
||
$user_id = $request->id;
|
||
}
|
||
$user = User::find($user_id);
|
||
$community_ids = $member->where('user_id', $user->id)->pluck('community_id');
|
||
$communities = $community->withCount('moment')->whereIn('id', $community_ids)->where('is_hided', 0)->paginate(56);
|
||
$community_count = $community->whereIn('id', $community_ids)->count() ?: 0;
|
||
$community_member_num = $community->whereIn('id', $community_ids)->sum('member_num') ?: 0;
|
||
$community_click_num = $community->whereIn('id', $community_ids)->sum('click_num') ?: 0;
|
||
return $this->success('ok', compact('user', 'communities', 'community_count', 'community_member_num', 'community_click_num'));
|
||
}
|
||
|
||
/**
|
||
* 我的所有社群成员
|
||
* @param Request $request [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function userCommunityMembers(Request $request, Community $community, CommunityMember $member)
|
||
{
|
||
$user = auth()->user();
|
||
$user_id = $user->id;
|
||
$community_ids = $community->where('user_id', $user_id)->pluck('id');
|
||
$members = $member->with('user')->whereHas('user', function ($sql) {
|
||
$sql->where('id', '>', 0);
|
||
})->whereIn('community_id', $community_ids)->paginate();
|
||
foreach ($members as $member) {
|
||
if (empty($member->user)) {
|
||
continue;
|
||
}
|
||
$other_user = $member->user;
|
||
if (!$user->isInvited($other_user)) {
|
||
$member->user->mobile = substr_replace($member->user->mobile, '****', 3, 4);
|
||
}
|
||
}
|
||
$community_ids = $member->where('user_id', $user_id)->pluck('community_id');
|
||
$community_count = $community->where('user_id', $user->id)->count() ?: 0;
|
||
$community_member_num = $community->where('user_id', $user->id)->sum('member_num') ?: 0;
|
||
$community_click_num = $community->where('user_id', $user->id)->sum('click_num') ?: 0;
|
||
return $this->success('ok', compact('user', 'members', 'community_count', 'community_member_num', 'community_click_num'));
|
||
}
|
||
|
||
/**
|
||
* 群主群列表
|
||
* @param Request $request [description]
|
||
* @param User $user [description]
|
||
* @param Community $community [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function groupOwnerCommunities(Request $request, User $user, Community $community)
|
||
{
|
||
$communities = $community->where('user_id', $user->id)->where('is_hided', 0)->orderBy('id', 'desc')->paginate();
|
||
return $this->success('ok', compact('user', 'communities'));
|
||
}
|
||
|
||
/**
|
||
* 创建社群
|
||
* @param Request $request [description]
|
||
* @param Community $community [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function storeCommunity(Request $request, Community $community, CommunityGroupLink $link, CommunityMember $community_member)
|
||
{
|
||
$group_id = $request->input('group_id');
|
||
if (empty($group_id)) {
|
||
return $this->failure('请选择社群分类');
|
||
}
|
||
|
||
|
||
$data['user_id'] = auth()->id();
|
||
$data['logo'] = $request->input('logo');
|
||
if (empty($data['logo'])) {
|
||
return $this->failure('请上传社群logo');
|
||
}
|
||
$data['title'] = $request->input('title');
|
||
if (empty($data['title'])) {
|
||
return $this->failure('请输入社群名称');
|
||
}
|
||
// $data['qrcode'] = $request->input('qrcode');
|
||
// if (empty($data['qrcode'])) {
|
||
// // return $this->failure("请上传群二维码");
|
||
// }
|
||
$data['wechat_qrcode'] = $request->input('wechat_qrcode');
|
||
if (empty($data['wechat_qrcode'])) {
|
||
// return $this->failure('请上传群主微信二维码');
|
||
}
|
||
$data['poster'] = $request->input('poster');
|
||
// if (empty($poster)) {
|
||
// return $this->failure('请上传社群海报');
|
||
// }
|
||
$data['poster_path'] = $request->input('poster_path');
|
||
$data['intro'] = $request->input('intro');
|
||
if (empty($data['intro'])) {
|
||
return $this->failure('请输入社群描述');
|
||
}
|
||
if (empty($request->price)) {
|
||
$data['is_free'] = 0;
|
||
$data['price'] = $request->price;
|
||
|
||
}
|
||
$community_obj = $community->create($data);
|
||
$link_obj = $link->create(['community_id' => $community_obj->id, 'group_id' => $group_id]);
|
||
//自动入群
|
||
$community_member->create([
|
||
'user_id' => $data['user_id'],
|
||
'community_id' => $community_obj->id,
|
||
'status' => 1,
|
||
]);
|
||
return $this->success('ok', $community_obj);
|
||
}
|
||
|
||
public function userCommunity(Request $request, Community $community)
|
||
{
|
||
$user_id = auth()->id();
|
||
if ($community->user_id != $user_id) {
|
||
return $this->failure('暂不能查看');
|
||
}
|
||
$links = $community->links->toArray();
|
||
$community->group_id = count($links) ? $links[0]['group_id'] : 0;
|
||
return $this->success('ok', $community);
|
||
}
|
||
|
||
/**
|
||
* 修改社群
|
||
* @param Request $request [description]
|
||
* @param Community $community [description]
|
||
* @param CommunityGroupLink $link [description]
|
||
* @return [type] [description]
|
||
*/
|
||
|
||
public function updateCommunity(Request $request, Community $community, CommunityGroupLink $link)
|
||
{
|
||
$user_id = auth()->id();
|
||
if ($community->user_id != $user_id) {
|
||
return $this->failure('暂不能修改');
|
||
}
|
||
if ($request->input('logo') && $request->logo != $community->logo) {
|
||
$community->logo = $request->logo;
|
||
}
|
||
if ($request->input('title') && $request->title != $community->title) {
|
||
$community->title = $request->title;
|
||
}
|
||
if ($request->input('qrcode') && $request->qrcode != $community->qrcode) {
|
||
$community->qrcode = $request->qrcode;
|
||
}
|
||
if ($request->input('wechat_qrcode') && $request->wechat_qrcode != $community->wechat_qrcode) {
|
||
$community->wechat_qrcode = $request->wechat_qrcode;
|
||
}
|
||
if ($request->input('poster') && $request->poster != $community->poster) {
|
||
$community->poster = $request->poster;
|
||
}
|
||
if ($request->input('poster_path') && $request->poster_path != $community->poster_path) {
|
||
$community->poster_path = $request->poster_path;
|
||
}
|
||
if ($request->input('intro') && $request->intro != $community->intro) {
|
||
$community->intro = $request->intro;
|
||
}
|
||
if ($request->input('group_id')) {
|
||
$group_id = $request->input('group_id');
|
||
$link->where('community_id', $community->id)->delete();
|
||
$link_obj = $link->create(['community_id' => $community->id, 'group_id' => $group_id]);
|
||
}
|
||
if ($request->has('price') && $request->price) {
|
||
$community->is_free = 0;
|
||
$community->price = $request->price;
|
||
}
|
||
$community->save();
|
||
return $this->success('ok');
|
||
}
|
||
|
||
public function deleteUserCommunity(Request $request, Community $community)
|
||
{
|
||
$user_id = $request->id;
|
||
if ($community->user_id != $user_id) {
|
||
return $this->failure('暂不能删除!');
|
||
}
|
||
$community->delete();
|
||
return $this->success('ok');
|
||
}
|
||
|
||
/**
|
||
* 社群首页
|
||
* @param Request $request [description]
|
||
* @param CommunityGroup $community_group [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function communityHome(Request $request, CommunityGroup $community_group, CommunityMoment $community_moment)
|
||
{
|
||
$user = auth()->user();
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
|
||
$community_home = $this->newCommunityHome($request, $community_group, $community_moment, $user);
|
||
$community_groups = $community_home['community_groups'];
|
||
$community_moments = $community_home['community_moments'];
|
||
|
||
//我加入的社群
|
||
$community_ids = CommunityMember::where('user_id', $user->id)->pluck('community_id');
|
||
$my_communites = Community::withCount('moment')->whereIn('id', $community_ids)->where('is_hided', 0)->get();
|
||
// $cache_name = 'user_'.$user->id.'community_home';
|
||
// //判断是否有缓存
|
||
// if (Cache::has($cache_name)) {
|
||
// $community_home = Cache::get($cache_name);
|
||
// $community_groups = $community_home['community_groups'];
|
||
// $community_moments = $community_home['community_moments'];
|
||
// }else{
|
||
// //社群分类
|
||
// $community_home = $this->newCommunityHome($community_group, $community_moment, $user, $cache_name);
|
||
// $community_groups = $community_home['community_groups'];
|
||
// $community_moments = $community_home['community_moments'];
|
||
// // Cache::put($cache_name, $community_home, 60);
|
||
|
||
// }
|
||
return $this->success('ok', compact('community_groups', 'community_moments', 'my_communites'));
|
||
|
||
}
|
||
|
||
/**
|
||
* 最新社群首页
|
||
* @param [type] $request [description]
|
||
* @param [type] $community_group [description]
|
||
* @param [type] $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function newCommunityHome($request, $community_group, $community_moment, $user, $cache_name = null)
|
||
{
|
||
$community_groups = $community_group->where('is_show', 1)->orderBy('num', 'desc')->get();
|
||
$community_moments = $community_moment->with(['user', 'topic'])->where('is_show', 0)->whereHas('community', function ($sql) {
|
||
$sql->where('id', '>', 0);
|
||
});
|
||
|
||
$keyword = $request->input('keyword');
|
||
if ($keyword) {
|
||
$keyword = trim($keyword);
|
||
//增加搜索记录
|
||
$this->addKeywordHistory($keyword, $user);
|
||
$community_moments = $community_moments->where('content', 'like', '%' . $keyword . '%');
|
||
}
|
||
$community_moments = $community_moments->where('is_show', 1)->orderBy('id', 'desc')->paginate();
|
||
$community_moments = $this->getCommunityMoments($user, $community_moments);
|
||
$community_home = [
|
||
'community_groups' => $community_groups,
|
||
'community_moments' => $community_moments
|
||
];
|
||
// Cache::put($cache_name, $community_home, 60);
|
||
return $community_home;
|
||
}
|
||
|
||
public function addKeywordHistory($keyword, $user)
|
||
{
|
||
if (empty($user)) {
|
||
return;
|
||
}
|
||
$history = KeywordHistory::firstOrCreate(['name' => $keyword, 'user_id' => $user->id]);
|
||
$history->updated_at = date('Y-m-d H:i:s');
|
||
$history->save();
|
||
$hot_keyword = HotKeyword::firstOrCreate(['name' => $keyword]);
|
||
$hot_keyword->increment('num', 1);
|
||
return;
|
||
}
|
||
|
||
public function getCommunityMoments($user, $community_moments)
|
||
{
|
||
foreach ($community_moments as $community_moment) {
|
||
//创建社群列表
|
||
$communities = $community_moment->communities()->where('id', '<>', $community_moment->community_id)->limit(2)->orderBy('id', 'desc')->get()->toArray();
|
||
$from_community = Community::where('id', $community_moment->community_id)->first();
|
||
array_unshift($communities, $from_community);
|
||
// dd($from_community);
|
||
$community_moment->communities = $communities;
|
||
//评论数
|
||
$community_moment->momentCommentCount = $community_moment->totalCommentsCount();
|
||
//图片
|
||
$photos = json_decode($community_moment->photos, true);
|
||
$photo_arr = [];
|
||
if (is_array($photos)) {
|
||
|
||
foreach ($photos as $photo) {
|
||
$photo = $photo . '?x-oss-process=style/scale1';
|
||
$photo_arr[] = $photo;
|
||
}
|
||
}
|
||
$community_moment->photos = $photo_arr;
|
||
if (empty($user)) {
|
||
$isLkerMoment = 0;
|
||
$favoriteCount = 0;
|
||
$is_self = 0;
|
||
$isFavoritet = 0;
|
||
} else {
|
||
$isLkerMoment = $community_moment->isLikedBy($user) ? 1 : 0;
|
||
$isFavoritet = $user->hasFavorited($community_moment) ? 1 : 0;
|
||
//是否是自己的
|
||
$is_self = $community_moment->user_id == $user->id ? 1 : 0;
|
||
}
|
||
//是否点赞
|
||
$community_moment->isLkerMoment = $isLkerMoment;
|
||
//点赞数
|
||
$community_moment->momentLikerCount = $community_moment->likers()->get()->count() ?: 0;
|
||
//是否收藏
|
||
$community_moment->is_favorite = $isFavoritet;
|
||
//收藏数
|
||
$community_moment->favoriteCount = $community_moment->favoriters()->count();
|
||
//是否是自己的
|
||
$community_moment->is_self = $is_self;
|
||
//评论列表
|
||
$comments = $this->getCommunityMomentComments($community_moment, $nopage = 1);
|
||
$community_moment->comments = $comments;
|
||
//点赞人列表
|
||
$momentLikers = $community_moment->likers->all();
|
||
$community_moment->momentLikers = $momentLikers;
|
||
}
|
||
return $community_moments;
|
||
}
|
||
|
||
/**
|
||
* 刷新最新社群首页
|
||
* @param Request $request [description]
|
||
* @param CommunityGroup $community_group [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function refreshcommunityHome(Request $request, CommunityGroup $community_group, CommunityMoment $community_moment)
|
||
{
|
||
$user = auth()->user();
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
// $cache_name = 'user_'.$user->id.'community_home';
|
||
$community_home = $this->newCommunityHome($request, $community_group, $community_moment, $user);
|
||
$community_groups = $community_home['community_groups'];
|
||
$community_moments = $community_home['community_moments'];
|
||
return $this->success('ok', compact('community_groups', 'community_moments'));
|
||
}
|
||
|
||
/**
|
||
* 社群动态列表
|
||
* @param Request $request [description]
|
||
* @param Community $community [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function communityMoments(Request $request, Community $community, CommunityMoment $community_moment)
|
||
{
|
||
$user = auth()->user();
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
if ($user) {
|
||
$is_applied = $community->isApplied($user->id);
|
||
} else {
|
||
$is_applied = 0;
|
||
}
|
||
$community->is_applied = $is_applied;
|
||
$community->increment('click_num', 1);
|
||
$community_moments = $this->newCommunityMoments($request, $community_moment, $community, $user);
|
||
// $cache_name = 'user_'.$user->id.'community_'.$community->id.'_cache';
|
||
// if (Cache::has($cache_name)) {
|
||
// $community_cache = Cache::get($cache_name);
|
||
// $community = $community_cache['community'];
|
||
// $community_moments = $community_cache['community_moments'];
|
||
// }else{
|
||
// $community_moments = $this->newCommunityMoments($request, $community_moment, $community, $user);
|
||
// $community_cache = [
|
||
// 'community'=>$community,
|
||
// 'community_moments'=>$community_moments,
|
||
// ];
|
||
// Cache::put($cache_name, $community_cache, 60);
|
||
// }
|
||
|
||
return $this->success('ok', compact('community', 'community_moments'));
|
||
}
|
||
|
||
/**
|
||
* 最新单个社群动态
|
||
* @param [type] $request [description]
|
||
* @param [type] $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function newCommunityMoments($request, $community_moment, $community, $user)
|
||
{
|
||
$community_id = $community_moment->community_id;
|
||
$community_moments = $community_moment->whereHas('community', function ($sql) use ($community_id) {
|
||
$sql->where('id', $community_id);
|
||
})->where('community_id', $community->id)->where('is_show', 1)->with('user');
|
||
$keyword = $request->input('keyword');
|
||
if ($keyword) {
|
||
$keyword = trim($keyword);
|
||
//增加搜索记录
|
||
$this->addKeywordHistory($keyword, $user);
|
||
$community_moments = $community_moments->where('content', 'like', '%' . $keyword . '%');
|
||
}
|
||
// if($request->has('topic_id') && $request->topic_id){
|
||
// $community_moments = $community_moments->where('topic_id', $request->topic_id);
|
||
// }
|
||
$community_moments = $community_moments->orderBy('id', 'desc')->paginate();
|
||
$community_moments = $this->getCommunityMoments($user, $community_moments);
|
||
return $community_moments;
|
||
}
|
||
|
||
/**
|
||
* 刷新单个社群动态
|
||
* @param Request $request [description]
|
||
* @param Community $community [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function refreshcommunityMoments(Request $request, Community $community, CommunityMoment $community_moment)
|
||
{
|
||
$user = auth()->user();
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
// $cache_name = 'user_'.$user->id.'community_'.$community->id.'_cache';
|
||
$community->increment('click_num', 1);
|
||
$community_moments = $this->newCommunityMoments($request, $community_moment, $community, $user);
|
||
// $community_cache = [
|
||
// 'community'=>$community,
|
||
// 'community_moments'=>$community_moments,
|
||
// ];
|
||
// Cache::put($cache_name, $community_cache, 60);
|
||
return $this->success('ok', compact('community', 'community_moments'));
|
||
}
|
||
|
||
/**
|
||
* 用户所加入的社群动态
|
||
* @param Request $request [description]
|
||
* @param Community $community [description]
|
||
* @param CommunityMoment $community_moments [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function userJoinCommunitiesMoments(Request $request, $user_id = null, Community $community, CommunityMoment $community_moment, CommunityMember $community_member)
|
||
{
|
||
|
||
if (empty($user_id)) {
|
||
$user = auth()->user();
|
||
} else {
|
||
$user = User::find($user_id);
|
||
}
|
||
$community_ids = $community_member->where('user_id', $user->id)->pluck('community_id');
|
||
$communities = $community->whereIn('id', $community_ids)->where('is_hided', 0)->orderBy('click_num', 'desc')->get();
|
||
$community_count = $community->whereIn('id', $community_ids)->count() ?: 0;
|
||
$community_member_num = $community->whereIn('id', $community_ids)->sum('member_num') ?: 0;
|
||
$community_click_num = $community->whereIn('id', $community_ids)->sum('click_num') ?: 0;
|
||
$mine_user = auth()->user();
|
||
if (empty($mine_user)) {
|
||
$mine_user = $this->authCheck();
|
||
}
|
||
$community_moments = $this->newUserJoinCommunitiesMoments($request, $community_moment, $community_ids, $mine_user);
|
||
//动态
|
||
// $cache_name = 'user_'.$mine_user->id.'_join_community_moments';
|
||
// if (Cache::has($cache_name)) {
|
||
// $community_moments = Cache::get($cache_name);
|
||
// }else{
|
||
// $community_moments = $this->newUserJoinCommunitiesMoments($community_moment, $community_ids, $mine_user, $cache_name);
|
||
// }
|
||
return $this->success('ok', compact('user', 'communities', 'community_count', 'community_member_num', 'community_click_num', 'community_moments'));
|
||
}
|
||
|
||
/**
|
||
* 最新用户参加社群动态
|
||
* @param [type] $community_moment [description]
|
||
* @param [type] $community_ids [description]
|
||
* @param [type] $cache_name [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function newUserJoinCommunitiesMoments($request, $community_moment, $community_ids, $user, $cache_nam = null)
|
||
{
|
||
$community_moments = $community_moment->with('user')->whereHas('community', function ($sql) {
|
||
$sql->where('id', '>', 0);
|
||
})->whereIn('community_id', $community_ids);
|
||
$keyword = $request->input('keyword');
|
||
if ($keyword) {
|
||
$keyword = trim($keyword);
|
||
//增加搜索记录
|
||
$this->addKeywordHistory($keyword, $user);
|
||
$community_moments = $community_moments->where('content', 'like', '%' . $keyword . '%');
|
||
}
|
||
$community_moments = $community_moments->orderBy('id', 'desc')->paginate();
|
||
$community_moments = $this->getCommunityMoments($user, $community_moments);
|
||
// Cache::put($cache_name, $community_moments, 60);
|
||
return $community_moments;
|
||
}
|
||
|
||
/**
|
||
* 刷新用户参加社群动态
|
||
* @param Request $request [description]
|
||
* @param Community $community [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @param User $user [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function refreshUserJoinCommunitiesMoments(Request $request, User $user, Community $community, CommunityMoment $community_moment, CommunityMember $community_member)
|
||
{
|
||
$community_ids = $community_member->where('user_id', $user->id)->pluck('community_id');
|
||
$communities = $community->whereIn('id', $community_ids)->where('is_hided', 0)->paginate(16);
|
||
$community_count = $community->whereIn('id', $community_ids)->count() ?: 0;
|
||
$community_member_num = $community->whereIn('id', $community_ids)->sum('member_num') ?: 0;
|
||
$community_click_num = $community->whereIn('id', $community_ids)->sum('click_num') ?: 0;
|
||
$mine_user = auth()->user();
|
||
if (empty($mine_user)) {
|
||
$mine_user = $this->authCheck();
|
||
}
|
||
// $cache_name = 'user_'.$mine_user->id.'_join_community_moments';
|
||
$community_moments = $this->newUserJoinCommunitiesMoments($request, $community_moment, $community_ids, $mine_user);
|
||
return $this->success('ok', compact('user', 'communities', 'community_count', 'community_member_num', 'community_click_num', 'community_moments'));
|
||
}
|
||
|
||
/**
|
||
* 用户创建社群动态
|
||
* @param Request $request [description]
|
||
* @param User $user [description]
|
||
* @param Community $community [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @param CommunityMember $community_member [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function userCreateCommunitiesMoments(Request $request, $user_id = null, Community $community, CommunityMoment $community_moment, CommunityMember $community_member)
|
||
{
|
||
if (empty($user_id)) {
|
||
$user = auth()->user();
|
||
} else {
|
||
$user = User::find($user_id);
|
||
}
|
||
if (empty($user)) {
|
||
return $this->failure('用户不存在');
|
||
}
|
||
$communities = $community->where('user_id', $user->id)->where('is_hided', 0)->orderBy('click_num', 'desc')->get();
|
||
$community_ids = $communities->pluck('id');
|
||
$community_count = $community->where('user_id', $user->id)->count() ?: 0;
|
||
$community_member_num = $community->where('user_id', $user->id)->sum('member_num') ?: 0;
|
||
$community_click_num = $community->where('user_id', $user->id)->sum('click_num') ?: 0;
|
||
// $community_ids = $community->where('user_id', $user->id)->where('is_hided', 0)->pluck('id');
|
||
$mine_user = auth()->user();
|
||
if (empty($mine_user)) {
|
||
$mine_user = $this->authCheck();
|
||
}
|
||
$community_moments = $this->newUserCreateCommunitiesMoments($request, $community_moment, $community_ids, $mine_user);
|
||
// $cache_name = 'user_'.$mine_user->id.'_create_community_moments';
|
||
// if (Cache::has($cache_name)) {
|
||
// $community_moments = Cache::get($cache_name);
|
||
// }else{
|
||
// $community_moments = $this->newUserCreateCommunitiesMoments($community_moment, $community_ids, $mine_user, $cache_name);
|
||
// }
|
||
return $this->success('ok', compact('user', 'communities', 'community_count', 'community_member_num', 'community_click_num', 'community_moments'));
|
||
}
|
||
|
||
/**
|
||
* 最新用户创建社群动态
|
||
* @param [type] $community_moment [description]
|
||
* @param [type] $community_ids [description]
|
||
* @param [type] $cache_name [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function newUserCreateCommunitiesMoments($request, $community_moment, $community_ids, $user, $cache_name = null)
|
||
{
|
||
$community_moments = $community_moment->with('user')->whereHas('community', function ($sql) {
|
||
$sql->where('id', '>', 0);
|
||
})->whereIn('community_id', $community_ids);
|
||
$keyword = $request->input('keyword');
|
||
if ($keyword) {
|
||
$keyword = trim($keyword);
|
||
//增加搜索记录
|
||
$this->addKeywordHistory($keyword, $user);
|
||
$community_moments = $community_moments->where('content', 'like', '%' . $keyword . '%');
|
||
}
|
||
$community_moments = $community_moments->orderBy('id', 'desc')->paginate();
|
||
$community_moments = $this->getCommunityMoments($user, $community_moments);
|
||
// Cache::put($cache_name, $community_moments, 60);
|
||
return $community_moments;
|
||
}
|
||
|
||
/**
|
||
* 刷新用户创建社群动态
|
||
* @param Request $request [description]
|
||
* @param User $user [description]
|
||
* @param Community $community [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @param CommunityMember $community_member [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function refreshUserCreateCommunitiesMoments(Request $request, User $user, Community $community, CommunityMoment $community_moment, CommunityMember $community_member)
|
||
{
|
||
$communities = $community->where('user_id', $user->id)->where('is_hided', 0)->orderBy('click_num', 'desc')->paginate(16);
|
||
$community_ids = $community->where('user_id', $user->id)->where('is_hided', 0)->pluck('id');
|
||
$community_count = $community->where('user_id', $user->id)->count() ?: 0;
|
||
$community_member_num = $community->where('user_id', $user->id)->sum('member_num') ?: 0;
|
||
$community_click_num = $community->where('user_id', $user->id)->sum('click_num') ?: 0;
|
||
$mine_user = auth()->user();
|
||
if (empty($mine_user)) {
|
||
$mine_user = $this->authCheck();
|
||
}
|
||
// $cache_name = 'user_'.$user->id.'_create_community_moments';
|
||
$community_moments = $this->newUserCreateCommunitiesMoments($request, $community_moment, $community_ids, $mine_user);
|
||
return $this->success('ok', compact('user', 'communities', 'community_count', 'community_member_num', 'community_click_num', 'community_moments'));
|
||
}
|
||
|
||
//社群动态列表
|
||
public function moments(Request $request)
|
||
{
|
||
$user = auth()->user();
|
||
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
$moments = CommunityMoment::with(['topic:id,name', 'community:id,title', 'user:id,name,photo,circle_avatar,app_avatar,nickname'])->whereHas('community', function ($query) {
|
||
$query->where('is_hided', 0);
|
||
})->where('community_id', '>', 0);
|
||
if ($request->has('community_id') && $request->community_id) {
|
||
$moments = $moments->where('community_id', $request->community_id);
|
||
}
|
||
if ($request->has('topic_id') && $request->topic_id) {
|
||
$moments = $moments->where('topic_id', $request->topic_id);
|
||
}
|
||
if ($request->has('user_id') && $request->user_id) {
|
||
$moments = $moments->where('user_id', $request->user_id);
|
||
}
|
||
$moments = $moments->orderBy('created_at', 'desc')->paginate();
|
||
foreach ($moments as $moment) {
|
||
if (!empty($moment->user)) {
|
||
$moment->user->photo = $moment->user->photo ?: $moment->user->circle_avatar;
|
||
$moment->user->name = !empty($moment->user->nickname) ? $moment->user->nickname : $moment->user->name;
|
||
}
|
||
}
|
||
// if(!empty($user)){
|
||
$moments = $this->getCommunityMoments($user, $moments);
|
||
// }
|
||
|
||
return $this->success('ok', $moments);
|
||
|
||
}
|
||
|
||
//我的动态列表
|
||
public function myMoments(Request $request)
|
||
{
|
||
$user = auth()->user();
|
||
$moments = CommunityMoment::with(['topic:id,name', 'community:id,title', 'user:id,name,photo,circle_avatar'])->whereHas('community', function ($query) {
|
||
$query->where('is_hided', 0);
|
||
})->where('community_id', '>', 0);
|
||
|
||
if ($request->has('keyword') && $request->keyword) {
|
||
$moments = $moments->where('content', 'like', '%' . $request->keyword . '%');
|
||
}
|
||
|
||
$moments = $moments->where('user_id', $user->id)->orderBy('created_at', 'desc')->paginate();
|
||
foreach ($moments as $moment) {
|
||
$moment->photos = json_decode($moment->photos, true);
|
||
$moment->momentCommentCount = $moment->totalCommentsCount();
|
||
$isLkerMoment = $moment->isLikedBy($user) ? 1 : 0;
|
||
$isFavoritet = $user->hasFavorited($moment) ? 1 : 0;
|
||
//是否是自己的
|
||
$is_self = $moment->user_id == $user->id ? 1 : 0;
|
||
//是否点赞
|
||
$moment->isLkerMoment = $isLkerMoment;
|
||
//点赞数
|
||
$moment->momentLikerCount = $moment->likers()->get()->count() ?: 0;
|
||
//是否收藏
|
||
$moment->is_favorite = $isFavoritet;
|
||
//收藏数
|
||
$moment->favoriteCount = $moment->favoriters()->count();
|
||
//是否是自己的
|
||
$moment->is_self = $is_self;
|
||
//评论列表
|
||
$comments = $this->getCommunityMomentComments($moment, $nopage = 1);
|
||
$moment->comments = $comments;
|
||
//点赞人列表
|
||
$momentLikers = $moment->likers->all();
|
||
$moment->momentLikers = $momentLikers;
|
||
}
|
||
return $this->success('ok', $moments);
|
||
}
|
||
|
||
/**
|
||
* 社群动态详情
|
||
* @param Request $request [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function communityMoment(Request $request, $community_moment)
|
||
{
|
||
|
||
$community_moment = CommunityMoment::find($community_moment);
|
||
if (empty($community_moment)) {
|
||
return $this->success('动态已被删除!');
|
||
}
|
||
$user = auth()->user();
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
$communities = $community_moment->communities()->limit(3)->orderBy('id', 'desc')->get();
|
||
$community_moment->communities = $communities;
|
||
$community_moment->topic = $community_moment->topic()->first();
|
||
$community_moment->community = $community_moment->community()->first();
|
||
if (!empty($user)) {
|
||
$community_moment->community->is_applid = $community_moment->community->isApplied($user->id);
|
||
} else {
|
||
$community_moment->community->is_applid = 0;
|
||
}
|
||
|
||
//评论数
|
||
$community_moment->momentCommentCount = $community_moment->totalCommentsCount();
|
||
//图片
|
||
$photos = json_decode($community_moment->photos, true);
|
||
$photo_arr = [];
|
||
foreach ($photos as $photo) {
|
||
$photo = $photo . '?x-oss-process=style/scale1';
|
||
$photo_arr[] = $photo;
|
||
}
|
||
$community_moment->photos = $photo_arr;
|
||
$moment = $community_moment;
|
||
//是否点赞
|
||
if (empty($user)) {
|
||
$isLkerMoment = 0;
|
||
$is_self = 0;
|
||
} else {
|
||
$isLkerMoment = $community_moment->isLikedBy($user) ? 1 : 0;
|
||
$is_self = $community_moment->user_id == $user->id ? 1 : 0;
|
||
}
|
||
$community_moment->isLkerMoment = $isLkerMoment;
|
||
//点赞数
|
||
$community_moment->momentLikerCount = $community_moment->likers()->get()->count() ?: 0;
|
||
//用户
|
||
$community_moment->user;
|
||
//评论列表
|
||
$comments = $this->getCommunityMomentComments($community_moment);
|
||
//点赞人列表
|
||
$momentLikers = $community_moment->likers->all();
|
||
//是否收藏
|
||
if (empty($user)) {
|
||
$community_moment->is_favorite = 0;
|
||
} else {
|
||
$community_moment->is_favorite = $user->hasFavorited($community_moment) ? 1 : 0;
|
||
}
|
||
//收藏数
|
||
$community_moment->favoriteCount = $community_moment->favoriters()->count() ?: 0;
|
||
$community_moment->is_self = $is_self;
|
||
if (!empty($user)) {
|
||
$photo = $user->photo;
|
||
} else {
|
||
$photo = '';
|
||
}
|
||
|
||
return $this->success('ok', compact('moment', 'comments', 'momentLikers', 'photo'));
|
||
}
|
||
|
||
/**
|
||
* 社群动态评论列表
|
||
* @param [type] $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function getCommunityMomentComments($community_moment, $nopage = 0)
|
||
{
|
||
if ($nopage) {
|
||
$momentComments = $community_moment->comments()->orderBy('id', 'desc')->get();
|
||
} else {
|
||
$momentComments = $community_moment->comments()->orderBy('id', 'desc')->paginate();
|
||
}
|
||
foreach ($momentComments as $momentComment) {
|
||
$user = User::find($momentComment->commented_id);
|
||
$momentComment->user = $user;
|
||
}
|
||
return $momentComments;
|
||
}
|
||
|
||
/**
|
||
* 评论社社群动态
|
||
* @param Request $request [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function commentCommunityMoment(Request $request, CommunityMoment $community_moment)
|
||
{
|
||
$comment = $request->input('comment');
|
||
if (empty($comment)) {
|
||
return $this->failure('请输入评论内容');
|
||
}
|
||
$user = auth()->user();
|
||
$user->comment($community_moment, $comment);
|
||
return $this->success('ok');
|
||
}
|
||
|
||
/**
|
||
* 点赞社群动态
|
||
* @param Request $request [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function likeCommunityMoment(Request $request, CommunityMoment $community_moment)
|
||
{
|
||
$user = auth()->user();
|
||
if ($user->hasLiked($community_moment)) {
|
||
$user->unlike($community_moment);
|
||
} else {
|
||
$user->like($community_moment);
|
||
}
|
||
return $this->success('ok');
|
||
}
|
||
|
||
/**
|
||
* 发表动态
|
||
* @param Request $request [description]
|
||
* @param Community $community [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function publishCommunityMoment(Request $request, Community $community, CommunityMoment $community_moment, CommunityGroup $community_group)
|
||
{
|
||
try {
|
||
//是否禁言
|
||
$user = auth()->user();
|
||
$banned = $user->isBanned('community_moment');
|
||
if ($banned) {
|
||
return $this->failure('您已被禁言,时间:' . $banned->start_time . '至' . $banned->end_time);
|
||
}
|
||
$content = $request->input('content');
|
||
if (empty($content)) {
|
||
return $this->failure('请输入动态内容');
|
||
}
|
||
|
||
//图片检测
|
||
$photos = $request->input('photos', []);
|
||
$img_result = \CommonUtilsService::imageContentCecurity($photos);
|
||
if ($img_result && isset($result['result']) && $img_result['result']) {
|
||
return $this->failure('图片' . $img_result['result'] . ',请换一张照片');
|
||
}
|
||
|
||
//文本检测
|
||
$arr = [$content];
|
||
$result = \CommonUtilsService::textContentCecurity($arr);
|
||
if ($result) {
|
||
return $this->failure('您填写的内容【' . $result['context'] . "】:" . $result['result'] . ",请重新输入");
|
||
}
|
||
DB::beginTransaction();
|
||
$topic_id = $request->topic_id ?: 0;
|
||
if ($community->user_id == $user->id && $request->topic) {
|
||
$topic = new CommunityTopic();
|
||
$topic->name = $request->topic;
|
||
$topic->community_id = $community->id;
|
||
$topic->save();
|
||
$topic_id = $topic->id;
|
||
}
|
||
$community_moment_obj = $community_moment->create([
|
||
'community_id' => $community->id,
|
||
'user_id' => $user->id,
|
||
'topic_id' => $topic_id,
|
||
'content' => $content,
|
||
'photos' => json_encode($photos),
|
||
]);
|
||
DB::commit();
|
||
return $this->success('ok', $community_moment_obj);
|
||
} catch (\Exception $e) {
|
||
DB::rollBack();
|
||
\Log::error($e->getMessage());
|
||
return $this->failure("发送失败");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 刷新所有的动态缓存
|
||
* @param [type] $community_moment [description]
|
||
* @param [type] $community [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function refreshMoemntCache($community_moment, $community, $community_group)
|
||
{
|
||
$user = auth()->user();
|
||
//首页动态
|
||
$home_moment_cache_name = 'user_' . $user->id . 'community_home';
|
||
$this->newCommunityHome($community_group, $community_moment, $user, $cache_name);
|
||
//社群动态
|
||
$community_moment_cache_name = 'user_' . $user->id . 'community_' . $community->id . '_cache';
|
||
$user_join_moemnt_cache_name = 'user_' . $user->id . '_join_community_moments';
|
||
$user_create_moment_cache_name = 'user_' . $user->id . '_create_community_moments';
|
||
|
||
}
|
||
|
||
/**
|
||
* 搜索历史
|
||
* @param Request $request [description]
|
||
* @param KeywordHistory $key_history [description]
|
||
* @param HotKeyword $hot_keyword [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function keywords(Request $request, KeywordHistory $keyword_history, HotKeyword $hot_keyword)
|
||
{
|
||
$keyword_histories = $keyword_history->orderBy('updated_at', 'desc')->orderBy('id', 'desc')->limit(10)->get();
|
||
$hot_keywords = $hot_keyword->orderBy('num', 'desc')->orderBy('id', 'desc')->limit(10)->get();
|
||
return $this->success('ok', compact('keyword_histories', 'hot_keywords'));
|
||
}
|
||
|
||
/**
|
||
* 举报社群动态
|
||
* @param Request $request [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function complaintCommunityMoment(Request $request, CommunityMoment $community_moment)
|
||
{
|
||
$content = $request->input('content');
|
||
if (empty($content)) {
|
||
return $this->failure('请输入举报内容');
|
||
}
|
||
$pics = $request->input('pics', []);
|
||
$user_id = auth()->id();
|
||
$complaint = new CommunityMomentComplaint();
|
||
$complaint->content = $content;
|
||
$complaint->pics = json_encode($pics);
|
||
$complaint->user_id = $user_id;
|
||
$complaint->community_moment_id = $community_moment->id;
|
||
$complaint->save();
|
||
return $this->success('ok', $complaint);
|
||
}
|
||
|
||
/**
|
||
* 搜索社群和组
|
||
* @param Request $request [description]
|
||
* @param Community $community [description]
|
||
* @param CommunityGroup $community_group [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function searchCommunityAndGroup(Request $request, Community $community, CommunityGroup $community_group)
|
||
{
|
||
$keyword = trim($request->input('keyword'));
|
||
if (empty($keyword)) {
|
||
return $this->failure('请输入搜索内容');
|
||
}
|
||
|
||
$groups = $community_group->where('title', 'like', '%' . $keyword . '%')->orderBy('num', 'desc')->get();
|
||
$communities = $community->where('title', 'like', '%' . $keyword . '%')->orderBy('click_num', 'desc')->get();
|
||
$user = auth()->user();
|
||
|
||
//增加搜索记录
|
||
$this->addKeywordHistory($keyword, $user);
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
if ($user) {
|
||
$history = KeywordHistory::firstOrCreate(['name' => $keyword, 'user_id' => $user->id]);
|
||
$history->updated_at = date('Y-m-d H:i:s');
|
||
$history->save();
|
||
}
|
||
|
||
return $this->success('ok', compact('groups', 'communities'));
|
||
}
|
||
|
||
/**
|
||
* 收藏社群动态
|
||
* @param Request $request [description]
|
||
* @param CommunityMoment $community_moment [description]
|
||
* @return [type] [description]
|
||
*/
|
||
public function favoriteCommunityMoment(Request $request, CommunityMoment $community_moment)
|
||
{
|
||
$user = auth()->user();
|
||
if ($user->hasFavorited($community_moment)) {
|
||
$user->unfavorite($community_moment);
|
||
} else {
|
||
$user->favorite($community_moment);
|
||
}
|
||
return $this->success('ok');
|
||
}
|
||
|
||
public function deleteCommunityMoment(Request $request, CommunityMoment $community_moment)
|
||
{
|
||
$user_id = auth()->id();
|
||
$moment_user_id = $community_moment->user_id;
|
||
if ($user_id != $moment_user_id) {
|
||
return $this->failure('暂不能删除!');
|
||
}
|
||
$community_moment->delete();
|
||
return $this->success('ok');
|
||
}
|
||
|
||
//创建话题
|
||
public function addTopic(Request $request)
|
||
{
|
||
if (!$request->has('name') || !$request->name) {
|
||
return $this->failure('名字不能为空');
|
||
}
|
||
|
||
try {
|
||
$topic = new CommunityTopic();
|
||
$topic->name = $request->name;
|
||
$topic->community_id = $request->community_id;
|
||
$topic->creater_id = auth()->id() ?: 0;
|
||
$topic->save();
|
||
return $this->success('ok');
|
||
} catch (\Exception $e) {
|
||
\Log::error($e->getMessage());
|
||
return $this->failure("系统错误");
|
||
}
|
||
}
|
||
|
||
//删除话题
|
||
public function delTopic(Request $request)
|
||
{
|
||
if (!$request->has('id') || !$request->id) {
|
||
return $this->failure('要删除的话题id不能为空');
|
||
}
|
||
|
||
try {
|
||
$arr = explode(',', $request->id);
|
||
CommunityTopic::whereIn('id', $arr)->delete();
|
||
return $this->success('ok');
|
||
} catch (\Exception $e) {
|
||
\Log::error($e->getMessage());
|
||
return $this->failure("删除失败");
|
||
}
|
||
}
|
||
|
||
//热门话题
|
||
public function hotTopic(Request $request, $community_id){
|
||
$topics = CommunityTopic::withCount('moment')->where('community_id', $community_id)->orderBy('click_num', 'desc')->get()->toArray();
|
||
// array_multisort(array_column($topics,'moment_count'),SORT_DESC,$topics);
|
||
// $hot_topics = array_slice($topics, 0, 4);
|
||
return $this->success('ok', $topics);
|
||
}
|
||
|
||
public function topics(Request $request, $community_id)
|
||
{
|
||
$topics = CommunityTopic::withCount('moment')->where('is_show', 1)->where('community_id', $community_id);
|
||
if ($request->has('keyword') && !$request->keyword) {
|
||
$topics = $topics->where('name', 'like', '%' . $request->keyword . '%');
|
||
}
|
||
|
||
$nopage = $request->input('nopage', 0);
|
||
if ($nopage) {
|
||
$topics = $topics->orderBy('click_num', 'desc')->get();
|
||
} else {
|
||
$topics = $topics->orderBy('click_num', 'desc')->paginate();
|
||
}
|
||
return $this->success('ok', $topics);
|
||
}
|
||
|
||
//我加入的社群
|
||
public function myCommunites(Request $request)
|
||
{
|
||
$user = auth()->user();
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
if (empty($user)) {
|
||
$community_members = CommunityMember::where('id', -1)->paginate();
|
||
return $this->success('ok', $community_members);
|
||
}
|
||
|
||
$community_members = CommunityMember::with('community')->whereHas('community', function ($sql) {
|
||
$sql->where('is_hided', 0);
|
||
})->where('user_id', $user->id)->orderBy('created_at', 'desc')->paginate();
|
||
foreach ($community_members as &$item) {
|
||
if (!empty($item->community)) {
|
||
$item->community->momont_count = CommunityMoment::where('community_id', $item->community_id)->count();
|
||
}
|
||
}
|
||
return $this->success('ok', $community_members);
|
||
}
|
||
|
||
//社群列表
|
||
public function communites(Request $request)
|
||
{
|
||
$communites = Community::withCount('moment');
|
||
if ($request->has('group_id') && $request->group_id) {
|
||
$group_id = $request->group_id;
|
||
$communites = $communites->whereHas('links', function ($query) use ($group_id) {
|
||
$query->where('group_id', $group_id);
|
||
});
|
||
}
|
||
|
||
if ($request->has('keyword') && $request->keyword) {
|
||
$communites = $communites->where('title', 'like', '%' . $request->keyword . '%');
|
||
}
|
||
$communites = $communites->orderBy('created_at', 'asc')->paginate();
|
||
return $this->success('ok', $communites);
|
||
}
|
||
|
||
//父群详情
|
||
public function newCommunityGroup(Request $request, CommunityGroup $group)
|
||
{
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if (empty($wechatUser)) {
|
||
$viewer_id = Viewer::where('openid', $request->openid)->value('id');
|
||
} else {
|
||
$viewer_id = Viewer::where('openid', $wechatUser->getId())->value('id');
|
||
}
|
||
|
||
$group->communites = $group->joinCommunitiesLog();
|
||
if ($viewer_id) {
|
||
$group->is_admin = CommunityGroupAdmin::where('viewer_id', $viewer_id)->where('group_id', $group->id)->count();
|
||
} else {
|
||
$group->is_admin = 0;
|
||
}
|
||
return $this->success('ok', $group);
|
||
|
||
}
|
||
|
||
//话题详情
|
||
public function topic(Request $request, $topic_id)
|
||
{
|
||
$user = auth()->user();
|
||
|
||
$key = 'topic' . $topic_id . '_watchAvatar';
|
||
if (!empty($user)) {
|
||
$avatar = $user->photo ?: $user->circle_avatar;
|
||
$len = Redis::llen($key);
|
||
if ($len) {
|
||
Redis::lrem($key, 1, $avatar);
|
||
Redis::lpush($key, $avatar);
|
||
redis::ltrim($key, 0, 2);
|
||
} else {
|
||
Redis::lpush($key, $avatar);
|
||
}
|
||
}
|
||
|
||
$topic = CommunityTopic::withCount('moment')->find($topic_id);
|
||
$topic->increment('click_num');
|
||
$topic->user_avatar = Redis::lrange($key, 0, -1);
|
||
|
||
return $this->success('ok', $topic);
|
||
}
|
||
|
||
//付费入群
|
||
public function payJoincommunity(Request $request, $community_id)
|
||
{
|
||
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if (empty($wechatUser)) {
|
||
$openid = $request->openid;
|
||
} else {
|
||
$openid = $wechatUser->getId();
|
||
}
|
||
$community = Community::find($community_id);
|
||
if ($community->is_free || !$community->price) {
|
||
return $this->failure('免费群');
|
||
}
|
||
|
||
$user = auth()->user();
|
||
$trade_no = \CommonUtilsService::getTradeNO();
|
||
|
||
$pay_order = new PayOrder();
|
||
$pay_order->user_id = $user->id;
|
||
$pay_order->trade_no = $trade_no;
|
||
$pay_order->pay_status = 'UNPAID';
|
||
$pay_order->cash = $community->price;
|
||
|
||
$order = new Order();
|
||
$order->user_id = $user->id;
|
||
$order->type_id = $community->id;
|
||
$order->goods = '付费入群:' . $community->title;
|
||
$order->price = $community->price;
|
||
$order->num = 1;
|
||
$order->type = 'community';
|
||
$order->trade_no = $trade_no;
|
||
|
||
|
||
//支付订单
|
||
$pay_order->save();
|
||
$order->save();
|
||
|
||
$config = [
|
||
'app_id' => 'wx36c72a00f1b2be64',
|
||
'mch_id' => '1581903621',
|
||
'key' => 'BuEjXg7ikKuV6tWKrsQq904ui13WpevT',
|
||
'notify_url' => env('APP_URL') . '/api/admin/community/pay/callback/' . $trade_no,
|
||
|
||
];
|
||
|
||
$app = Factory::payment($config);
|
||
|
||
|
||
$attributes = array(
|
||
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
|
||
'body' => '福恋社群',
|
||
'detail' => '付费入群:' . $community->title,
|
||
'out_trade_no' => $trade_no,
|
||
'total_fee' => ($community->price * 100),
|
||
'notify_url' => env('APP_URL') . '/api/admin/community/pay/callback/' . $trade_no, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
||
'openid' => $openid,
|
||
);
|
||
//生成订单
|
||
$result = $app->order->unify($attributes);
|
||
$jssdk = $app->jssdk;
|
||
|
||
$prepayId = '';
|
||
$code_url = null;
|
||
if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
|
||
$prepayId = $result['prepay_id'];
|
||
} else {
|
||
// Messenger::sendSMS('15872844805', $attributes['out_trade_no'].', 统一下单失败'.$result['return_msg']);
|
||
}
|
||
$config = $jssdk->sdkConfig($prepayId);
|
||
|
||
|
||
$data = [
|
||
'pay_order' => $pay_order,
|
||
'wx_pay' => [
|
||
'config' => $config,
|
||
'attributes' => $attributes,
|
||
'mweb_url' => isset($result['mweb_url']) ? $result['mweb_url'] : '',
|
||
]
|
||
];
|
||
return $this->success('ok', $data);
|
||
}
|
||
|
||
// 测试是否是微信
|
||
function is_weixin_visit()
|
||
{
|
||
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
|
||
return $this->success('ok', 1);
|
||
} else {
|
||
return $this->success('ok', 0);
|
||
}
|
||
}
|
||
|
||
//付费入群回调
|
||
public function joinCommunityCallback(Request $request, $trade_no)
|
||
{
|
||
$config = [
|
||
'app_id' => 'wx36c72a00f1b2be64',
|
||
'mch_id' => '1581903621',
|
||
'key' => 'BuEjXg7ikKuV6tWKrsQq904ui13WpevT',
|
||
'notify_url' => env('APP_URL') . '/api/admin/community/pay/callback/' . $trade_no,
|
||
|
||
];
|
||
$app = Factory::payment($config);
|
||
|
||
$response = $app->handlePaidNotify(function ($message, $fail) {
|
||
//\Log::info('community_pay' . json_encode($message));
|
||
$trade_no = $message['out_trade_no'];
|
||
$order = Order::where('trade_no', $trade_no)->first();
|
||
if (empty($order) || $order->pay_status == 1) {
|
||
return true;
|
||
}
|
||
|
||
if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
|
||
// 用户是否支付成功
|
||
if (array_get($message, 'result_code') === 'SUCCESS') {
|
||
try {
|
||
\DB::beginTransaction();
|
||
$order->pay_status = 'PAID';
|
||
$order->save();
|
||
|
||
PayOrder::where('trade_no', $trade_no)->update(['is_hooked' => 1]);
|
||
|
||
//入群
|
||
$community_member = new CommunityMember();
|
||
$community_member->user_id = $order->user_id;
|
||
$community_member->community_id = $order->type_id;
|
||
$community_member->status = 1;
|
||
$community_member->save();
|
||
|
||
//修改群成员数
|
||
Community::where('id', $order->type_id)->increment('member_num', 1);
|
||
|
||
\DB::commit();
|
||
} catch (\Exception $e) {
|
||
\DB::rollback();
|
||
\Log::info('community_pay_callback_error' . $e->getMessage());
|
||
}
|
||
|
||
}
|
||
} else {
|
||
return $fail('通信失败,请稍后再通知我');
|
||
}
|
||
return true;
|
||
});
|
||
|
||
return $response;
|
||
|
||
}
|
||
|
||
//完善资料/申请推荐
|
||
public function updateIno(Request $request)
|
||
{
|
||
$user = Auth()->user();
|
||
|
||
//限制请求次数
|
||
$key = $user->id . '_request_star_count';
|
||
$is_request = Redis::get($key);
|
||
if ($is_request) {
|
||
return $this->failure('请求频率过快');
|
||
}
|
||
Redis::setex($key, 2, 1);
|
||
|
||
$viewer = Viewer::where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
if ($user->type != 'single') {
|
||
return $this->failure('您不是单身');
|
||
}
|
||
$is_star = CommunityStar::where('user_id', $user->id)->where('is_audit', 1)->count();
|
||
if ($is_star && $request->signMarriageID) {
|
||
return $this->failure('您已申请过征婚');
|
||
}
|
||
|
||
$profile = ProfileCourtship::where('user_id', $user->id)->first();
|
||
if (empty($profile)) {
|
||
$profile = new ProfileCourtship();
|
||
$profile->user_id = $user->id;
|
||
}
|
||
|
||
if (!$request->has('photo') || !$request->photo) {
|
||
return $this->failure('请输入头像');
|
||
}
|
||
$user->photo = $request->photo;
|
||
// if(!$request->has('name') || !$request->name){
|
||
// return $this->failure('请输入姓名');
|
||
// }
|
||
// $user->name = $request->name;
|
||
if (!$request->has('sex') || !$request->sex) {
|
||
return $this->failure('请输入性别');
|
||
}
|
||
$user->sex = $request->sex;
|
||
if (!$request->has('stature') || !$request->stature) {
|
||
return $this->failure('请输入身高');
|
||
}
|
||
$profile->stature = $request->stature;
|
||
if (!$request->has('weight') || !$request->weight) {
|
||
return $this->failure('请输入体重');
|
||
}
|
||
$profile->weight = $request->weight;
|
||
if (!$request->has('province') || !$request->province) {
|
||
return $this->failure('请输入长居地');
|
||
}
|
||
$profile->province = $request->province;
|
||
if (!$request->has('city') || !$request->city) {
|
||
return $this->failure('请输入长居地');
|
||
}
|
||
$profile->city = $request->city;
|
||
if (!$request->has('birthday') || !$request->birthday) {
|
||
return $this->failure('请输入出生日期');
|
||
}
|
||
$profile->birthday = $request->birthday;
|
||
if (!$request->has('state') || !$request->state) {
|
||
return $this->failure('请输入单身状态');
|
||
}
|
||
$profile->state = $request->state;
|
||
if (!$request->has('degree') || !$request->degree) {
|
||
return $this->failure('请输入学历');
|
||
}
|
||
$profile->degree = $request->degree;
|
||
if (!$request->has('graduate_school') || !$request->graduate_school) {
|
||
return $this->failure('请输入毕业学校');
|
||
}
|
||
$profile->graduate_school = $request->graduate_school;
|
||
if (!$request->has('resident_province') || !$request->resident_province) {
|
||
return $this->failure('请输入籍贯');
|
||
}
|
||
$profile->resident_province = $request->resident_province;
|
||
if (!$request->has('resident_city') || !$request->resident_city) {
|
||
return $this->failure('请输入籍贯');
|
||
}
|
||
$profile->resident_city = $request->resident_city;
|
||
if (!$request->has('industry') || !$request->industry) {
|
||
return $this->failure('请输入行业');
|
||
}
|
||
$user->industry = $request->industry;
|
||
if (!$request->has('industry_sub') || !$request->industry_sub) {
|
||
return $this->failure('请输入行业');
|
||
}
|
||
$user->industry_sub = $request->industry_sub;
|
||
if (!$request->has('income') || !$request->income) {
|
||
return $this->failure('请输入年收入');
|
||
}
|
||
$profile->income = $request->income;
|
||
if (!$request->has('belief') || !$request->belief) {
|
||
return $this->failure('请输入信仰');
|
||
}
|
||
$profile->belief = $request->belief;
|
||
$user->belief = $request->belief;
|
||
if (!$request->has('wechat_id') || !$request->wechat_id) {
|
||
return $this->failure('请输入微信号');
|
||
}
|
||
$profile->wechat_id = $request->wechat_id;
|
||
|
||
if (!$request->has('introduction') || !$request->introduction) {
|
||
return $this->failure('请输入个人介绍');
|
||
}
|
||
$profile->introduction = $request->introduction;
|
||
if (!$request->has('interest_hobby') || !$request->interest_hobby) {
|
||
return $this->failure('请输入兴趣爱好');
|
||
}
|
||
$profile->interest_hobby = $request->interest_hobby;
|
||
if (!$request->has('ideal_mate') || !$request->ideal_mate) {
|
||
return $this->failure('请输入择偶标准');
|
||
}
|
||
$profile->ideal_mate = $request->ideal_mate;
|
||
|
||
//生活照
|
||
$photos = $request->photos;
|
||
$profile_phpotos = ProfilePhoto::where('user_id', $user->id)->pluck('photo')->toArray();
|
||
|
||
if (!empty(array_diff($profile_phpotos, $photos)) || !empty(array_diff($photos, $profile_phpotos))) {
|
||
ProfilePhoto::where('user_id', $user->id)->delete();
|
||
if (!empty($photos)) {
|
||
$insert_arr = [];
|
||
foreach ($photos as $item) {
|
||
$snap_arr['user_id'] = $user->id;
|
||
$snap_arr['photo'] = $item;
|
||
$snap_arr['dateline'] = date('Y-m-d');
|
||
$snap_arr['created_at'] = date('Y-m-d H:i:s');
|
||
$snap_arr['updated_at'] = date('Y-m-d H:i:s');
|
||
$insert_arr[] = $snap_arr;
|
||
}
|
||
ProfilePhoto::insert($insert_arr);
|
||
}
|
||
}
|
||
try {
|
||
DB::beginTransaction();
|
||
//是否申请社群首页推荐
|
||
if ($request->has('signMarriageID') && $request->signMarriageID == 1) {
|
||
$communityStar = CommunityStar::where('user_id', $user->id)->where('is_audit', '!=', 1)->first();
|
||
if (!empty($communityStar)) {
|
||
$communityStar->is_audit = 0;
|
||
} else {
|
||
$communityStar = new CommunityStar();
|
||
$communityStar->user_id = $user->id;
|
||
}
|
||
if ($request->has('signMarriageID') && is_numeric($request->signMarriageID)) {
|
||
$communityStar->sign_marriage_id = $request->signMarriageID;
|
||
}
|
||
|
||
|
||
//如果是通过任务申请的
|
||
if ($request->has('task_id') && $request->task_id) {
|
||
$communityStar->task_id = $request->task_id;
|
||
|
||
//完成任务
|
||
$task = Task::find($request->task_id);
|
||
if ($task && $task->the_rest > 0 && $task->num && $task->type == '征婚申请') {
|
||
$list_data['from_id'] = $request->from_id;
|
||
$list_data['task_id'] = $request->task_id;
|
||
$list_data['source_id'] = $request->task_id;
|
||
$list_data['other_viewer_id'] = $viewer->id;
|
||
DoTask::dispatch($list_data)->onQueue('love');
|
||
}
|
||
}
|
||
$communityStar->is_audit = 1;
|
||
$communityStar->audited_at = date('Y-m-d H:i:s');
|
||
$communityStar->from_id = $request->from_id ?: 0;
|
||
$communityStar->save();
|
||
}
|
||
$user->save();
|
||
$profile->save();
|
||
|
||
$viewer = Viewer::Where('user_id', $user->id)->orWhere('mobile', $user->mobile)->first();
|
||
DB::commit();
|
||
return $this->success('ok');
|
||
} catch (\Exception $e) {
|
||
DB::rollback();
|
||
$this->getError($e);
|
||
return $this->failure('操作失败,请稍后再试');
|
||
}
|
||
}
|
||
|
||
//热门推荐
|
||
public function star(Request $request)
|
||
{
|
||
$openid = $request->input('openid');
|
||
$sex = null;
|
||
$belief = null;
|
||
$user = null;
|
||
$redis_key = null;
|
||
$page = $request->input("page", 1);
|
||
if ($openid) {
|
||
$user_id = Viewer::where('openid', $openid)->value('user_id');
|
||
if ($user_id) {
|
||
$user = User::where('id', $user_id)->first();
|
||
}
|
||
}
|
||
|
||
//如果是单身用户推荐异性,介绍人推荐全部
|
||
if ($user && $user->type == 'single') {
|
||
if (!empty($user->sex)) {
|
||
$sex = $user->sex == 2 ? 1 : 2;
|
||
$redis_key = "top_view_sex_" . $sex . "_page_" . $page;
|
||
} else {
|
||
$redis_key = "top_view_sex_all_page_" . $page;
|
||
}
|
||
} else {
|
||
$redis_key = "top_view_sex_all_page_" . $page;
|
||
}
|
||
|
||
if ($user) {
|
||
$belief = $user->belief;
|
||
}
|
||
|
||
$area_id = $request->input('area_id', 1);
|
||
$area_users = json_decode(Redis::get($redis_key));
|
||
if (empty($area_users)) {
|
||
$area_users = AreaUser::with(['user:id,circle_avatar,photo,app_avatar,nickname,name,sex', 'user.profileCourtship'])
|
||
->whereHas('user', function ($query) use ($sex, $belief) {
|
||
$query->where('type', 'single')
|
||
->where('hidden_profile', 'NONE')
|
||
->select("circle_avatar", "id", "name", "photo", "app_avatar", "nickname", "sex");
|
||
if (!empty($sex)) {
|
||
$query->where('sex', $sex);
|
||
}
|
||
// if($belief){
|
||
// if($belief == '基督教'){
|
||
// $query->where('belief',$belief);
|
||
// }else{
|
||
// $query->where('belief','<>','基督教');
|
||
// }
|
||
// }
|
||
})->where('area_id', $area_id)
|
||
->where('is_audited', 1)
|
||
->where('is_show', 1)
|
||
//->orderBy('sort', 'desc')
|
||
->orderBy('id', 'desc');
|
||
|
||
|
||
if ($area_id == 1) {
|
||
$area_users = $area_users->get();
|
||
} else {
|
||
$area_users = $area_users->paginate(16);
|
||
}
|
||
foreach ($area_users as $area_user) {
|
||
$area_user->user->city = !empty($area_user->user->profileCourtship) ? $area_user->user->profileCourtship->city : '';
|
||
$area_user->user->age = !empty($area_user->user->profileCourtship) ? \CommonUtilsService::getAge($area_user->user->profileCourtship->birthday) : 0;
|
||
// $area_user->user->profileCourtship->age = !empty($area_user->user->profileCourtship->birthday) ? \CommonUtilsService::getAge($area_user->user->profileCourtship->birthday) : 0;
|
||
}
|
||
Redis::setex($redis_key, 86400, json_encode($area_users));
|
||
}
|
||
return $this->success('ok', $area_users);
|
||
}
|
||
|
||
//征婚交友
|
||
public function completeUser(Request $request)
|
||
{
|
||
$openid = $request->input('openid');
|
||
$user = null;
|
||
$other_sex = null;
|
||
$redis_key = null;
|
||
if ($openid) {
|
||
$user_id = Viewer::where('openid', $openid)->value('user_id');
|
||
if ($user_id) {
|
||
$user = User::where('id', $user_id)->first();
|
||
if($user) {
|
||
$user = $user->toArray();
|
||
}else{
|
||
return $this->failure('用户信息不存在');
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!empty($user)) {
|
||
if ($user['sex'] == 1) {
|
||
$other_sex = 2;
|
||
$redis_key = 'favorite_user_top200_sex_' . $other_sex;
|
||
} elseif ($user['sex'] == 2) {
|
||
$other_sex = 1;
|
||
$redis_key = 'favorite_user_top200_sex_' . $other_sex;
|
||
} else {
|
||
$redis_key = 'favorite_user_top200_sex_all';
|
||
}
|
||
} else {
|
||
$redis_key = 'favorite_user_top200_sex_all';
|
||
}
|
||
$users = json_decode(Redis::get($redis_key), true);
|
||
if (empty($users)) {
|
||
//查询用户最喜欢的异性
|
||
$users = UserPreview::leftJoin("users", function ($join) use ($other_sex) {
|
||
$join->on("user_previews.user_id", "=", 'users.id');
|
||
})
|
||
->where("users.hidden_profile", '=', "NONE")
|
||
->where("users.type", '=', "single")
|
||
->when(!empty($other_sex), function ($when) use ($other_sex) {
|
||
$when->where("users.sex", $other_sex);
|
||
})
|
||
->select("user_previews.user_id", DB::raw("count( ufutx_user_previews.user_id ) AS count "))
|
||
->groupBy(DB::raw('ufutx_user_previews.user_id'))
|
||
->orderBy('count', 'DESC ')
|
||
->limit(200)
|
||
->get()
|
||
->toArray();
|
||
if (!empty($users)) {
|
||
$users = array_column($users, "user_id");
|
||
Redis::setex($redis_key, 86400, json_encode($users));
|
||
}
|
||
}
|
||
|
||
//查询我查看过的用户列表
|
||
if (!empty($user)) {
|
||
$redis_key = "view_user_list_userid_" . $user['id'];
|
||
$view_user = json_decode(Redis::get($redis_key));
|
||
if (empty($view_user)) {
|
||
$view_user = UserPreview::where("preview_user_id", $user['id'])
|
||
->whereIn("user_id", $users)
|
||
->pluck('user_id')->toArray();
|
||
Redis::setex($redis_key, 3600, json_encode($view_user));
|
||
}
|
||
if (!empty($view_user)) {
|
||
$diff = array_diff($users, $view_user);
|
||
$diff = array_values($diff);
|
||
$users = array_merge($diff, $view_user);
|
||
}
|
||
}
|
||
$users = ProfileCourtship::select('id', 'user_id', 'stature', 'degree', 'city', 'birthday', 'introduction', 'post')
|
||
->with(['user:id,name,photo,is_approved,sex,age,circle_avatar,rank_id'])
|
||
->whereIn('user_id', $users)
|
||
->orderByRaw(DB::raw('FIND_IN_SET(user_id, "' . implode(",", $users) . '"' . ")"))
|
||
->paginate();
|
||
|
||
foreach ($users as $user) {
|
||
$user->user->age = $this->getAge($user->birthday);
|
||
$user->is_real_approved = $user->user->is_real_approved;
|
||
$user->city = str_replace('市', '', $user->city);
|
||
if (empty($user->user->photo) || $user->user->photo == 'https://images.ufutx.com/202007/01/e0de60525143427d4dd19515a5b387ba.png') {
|
||
$user->user->photo = $user->user->circle_avatar;
|
||
}
|
||
}
|
||
return $this->success('ok', $users);
|
||
}
|
||
|
||
//随机推荐十个征婚用户
|
||
public function randCompleteUser(Request $request)
|
||
{
|
||
$current_id = $request->input('current_id', 0);
|
||
$user_ids = CommunityStar::where('is_home', 1)->where('is_audit', 1)->where('user_id', '!=', $current_id)->pluck('user_id')->toArray();
|
||
$users = ProfileCourtship::select('id', 'user_id', 'stature', 'degree', 'city', 'birthday', 'introduction')->with(['user:id,name,photo,is_approved,sex,age,circle_avatar,rank_id,app_avatar'])->whereIn('user_id', $user_ids)->orderByRaw('RAND()')->limit(10)->get();
|
||
foreach ($users as $user) {
|
||
$user->user->age = $this->getAge($user->birthday);
|
||
if (empty($user->user->photo) || $user->user->photo == 'https://images.ufutx.com/202007/01/e0de60525143427d4dd19515a5b387ba.png') {
|
||
$user->user->photo = $user->user->circle_avatar;
|
||
}
|
||
}
|
||
return $this->success('ok', $users);
|
||
}
|
||
|
||
//个人资料
|
||
public function myProfile(Request $request)
|
||
{
|
||
$user = auth()->user();
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
if (empty($user)) {
|
||
return $this->success('ok');
|
||
}
|
||
$user->profile = $user->profileCourtship;
|
||
$profile_photos = $user->profilePhoto()->pluck('photo')->toArray();
|
||
// if(!empty($profile_photos)){
|
||
// foreach ($profile_photos as $item) {
|
||
// $photos[] = $item['photo'];
|
||
// }
|
||
if (!empty($user->profileCourtship)) {
|
||
$user->profileCourtship->photos = json_encode($profile_photos);
|
||
}
|
||
// }
|
||
return $this->success('ok', $user);
|
||
}
|
||
|
||
//用户详情
|
||
public function profile(Request $request, $user_id)
|
||
{
|
||
$my_user = auth()->user();
|
||
$age = 0;
|
||
if (empty($my_user)) {
|
||
$my_user = $this->authCheck();
|
||
}
|
||
|
||
$user = User::with(['profileCourtship', 'profilePhoto'])->where('id', $user_id)->first();
|
||
if (empty($user)) {
|
||
return $this->failure('用户不存在');
|
||
}
|
||
$key = 'user_' . $user->id . 'miniqrcode';
|
||
$mini_qrcode = Redis::get($key);
|
||
if (empty($mini_qrcode)) {
|
||
$mini_qrcode = QrcodeRectService::getQrcodeToMini($user->id);
|
||
Redis::set($key, $mini_qrcode);
|
||
}
|
||
$user->mini_qrcode = $mini_qrcode;
|
||
$user->preview_count = $user->userPreviewHistory()->count();
|
||
|
||
$profile_photos = $user->profilePhoto()->pluck('photo')->toArray();
|
||
if (!empty($user->profileCourtship)) {
|
||
$user->profileCourtship->photos = $profile_photos;
|
||
$user->profileCourtship->age = $this->getAge($user->profileCourtship->birthday);
|
||
//计算年龄
|
||
$age = $this->getAge($user->profileCourtship->birthday);
|
||
}
|
||
// if(!empty($user->profilePhoto)){
|
||
// $photos = [];
|
||
// foreach ($user->profilePhoto as $item) {
|
||
// $photos[] = $item->photo;
|
||
// }
|
||
// $user->profileCourtship->photos = json_encode($photos);
|
||
// }
|
||
|
||
if (!empty($my_user)) {
|
||
$my_user->addUserPreview($user_id);
|
||
}
|
||
$user->age = $age;
|
||
|
||
return $this->success('ok', $user);
|
||
}
|
||
|
||
public function tasks(Request $request)
|
||
{
|
||
$tasks = Task::with('live')->where('start_time', '<', date('Y-m-d H:i"s'))->where('end_time', '>', date('Y-m-d H:i"s'))->orderByRaw(DB::raw('FIELD(is_top, 1,0) asc'))->orderByRaw('created_at desc')->paginate();
|
||
foreach ($tasks as &$task) {
|
||
$task->type = empty($task->old_score) ? '' : '仅限新用户';
|
||
$task->new_score = $task->new_score * 100;
|
||
if (!empty($task->live->teacher)) {
|
||
$task->live->title = $task->live->teacher->name . '分享【' . $task->live->title . '】';
|
||
}
|
||
$task->time = $this->timetostring(strtotime($task->end_time));
|
||
if (!empty($task->live) && empty($task->url)) {
|
||
$task->url = env('APP_URL') . '/h5/#/watchLive/' . $task->live->id;
|
||
}
|
||
if (!empty($task->live) && empty($task->title)) {
|
||
$task->title = $task->live->title;
|
||
}
|
||
if (!empty($task->live) && empty($task->pic)) {
|
||
$task->pic = $task->live->poster;
|
||
}
|
||
}
|
||
|
||
return $this->success('ok', $tasks);
|
||
}
|
||
|
||
public function timetostring($unixEndTime)
|
||
{
|
||
if ($unixEndTime <= time()) { // 如果过了活动终止日期
|
||
return '0天0时0分';
|
||
}
|
||
|
||
// 使用当前日期时间到活动截至日期时间的毫秒数来计算剩余天时分
|
||
$time = $unixEndTime - time();
|
||
|
||
// $days = 0;
|
||
// if ($time >= 86400) { // 如果大于1天
|
||
// $days = (int)($time / 86400);
|
||
// $time = $time % 86400; // 计算天后剩余的毫秒数
|
||
// }
|
||
|
||
$xiaoshi = 0;
|
||
if ($time >= 3600) { // 如果大于1小时
|
||
$xiaoshi = (int)($time / 3600);
|
||
$time = $time % 3600; // 计算小时后剩余的毫秒数
|
||
}
|
||
|
||
$fen = 0;
|
||
if ($time > 60) {
|
||
$fen = (int)($time / 60);
|
||
$time = $time % 60; // 计算分钟后剩余的毫秒数
|
||
}
|
||
|
||
$second = $time;
|
||
|
||
|
||
return $xiaoshi . '时' . $fen . '分' . $second . '秒';
|
||
}
|
||
|
||
public function home(Request $request)
|
||
{
|
||
$user = auth()->user();
|
||
if (empty($user)) {
|
||
$user = $this->authCheck();
|
||
}
|
||
if (empty($user)) {
|
||
return $this->success('ok', []);
|
||
}
|
||
$has_photos = ProfilePhoto::where('user_id', $user->id)->count();
|
||
|
||
//判断资料是否完整
|
||
if (!empty($user->photo) && !empty($user->name) && !empty($user->sex) && !empty($user->profileCourtship->stature) && !empty($user->profileCourtship->weight) && !empty($user->profileCourtship->province) && !empty($user->profileCourtship->city) && !empty($user->profileCourtship->birthday) && !empty($user->profileCourtship->state) && !empty($user->profileCourtship->degree) && !empty($user->profileCourtship->graduate_school) && !empty($user->profileCourtship->resident_province) && !empty($user->profileCourtship->resident_city) && !empty($user->industry) && !empty($user->industry_sub) && !empty($user->profileCourtship->income) && !empty($user->profileCourtship->belief) && !empty($user->profileCourtship->wechat_id) && !empty($user->profileCourtship->introduction) && !empty($user->profileCourtship->ideal_mate) && !empty($has_photos)) {
|
||
$is_complete = 1;
|
||
} else {
|
||
$is_complete = 0;
|
||
}
|
||
|
||
$is_star = CommunityStar::where('user_id', $user->id)->where('is_audit', 1)->count();
|
||
$can_apply = empty($is_star) ? 1 : 0;
|
||
|
||
|
||
$viewer_id = Viewer::where('mobile', $user->mobile)->value('id');
|
||
$is_messenger = 0;
|
||
if (!empty($viewer_id)) {
|
||
$messenger = \App\Models\Live\Messenger::where('viewer_id', $viewer_id)->first();
|
||
if (!empty($messenger)) {
|
||
$is_messenger = $messenger->is_audit == 1 ? 1 : 0;
|
||
}
|
||
}
|
||
return $this->success('ok', ['can_apply' => $can_apply, 'is_complete' => $is_complete, 'is_messenger' => $is_messenger]);
|
||
}
|
||
|
||
/**举报标签 */
|
||
public function complaintLabelsV2(Request $request)
|
||
{
|
||
try {
|
||
$type = $request->input('type', 'user');
|
||
$labels = [];
|
||
if ($type == 'user') {
|
||
$labels = ['欺诈骗钱', '骚扰/广告', '辱骂/攻击', '色情/暴力', '形象照等资料虚假或假冒', '其他'];
|
||
} elseif ($type == 'mk_user') {
|
||
$labels = ['信息虚假', '小号相亲', '引导去其他平台', '在其他平台直播', '账号非本人使用', '广告', '强迫刷礼物', '恶意刷好评', '辱骂攻击', '色情骚扰', '欺诈骗钱', '涉证涉恐', '其他'];
|
||
} elseif ($type == 'moment') {
|
||
$labels = ['内容虚假', '侵权抄袭', '广告软文', '政治敏感', '色情低俗', '内容引起不适'];
|
||
}
|
||
return $this->success('ok', $labels);
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('获取信息失败');
|
||
}
|
||
}
|
||
|
||
// public function communities(Request $request){
|
||
// $communities = Community::where('is_hided', 0);
|
||
// if($request->keyword){
|
||
// $communities = $communities->where('title', 'like', '%'.$request->keyword.'%');
|
||
// }
|
||
// $communities = $communities->orderBy('id', 'desc')->paginate();
|
||
// return $this->success('ok', $communities);
|
||
// }
|
||
// 社区-训练营列表
|
||
public function activityList(Request $request)
|
||
{
|
||
if ($request->type == 'fulllink') {
|
||
$result = CommunityActivity::where('status', 1)->orderBy('id', 'desc')->where('type', 'fulllink')->paginate();
|
||
} else {//商家版
|
||
$class = $request->class ?? 'one';
|
||
if ($request->merchant_id && $request->merchant_id >= 1) {
|
||
$Account = MerchantAccount::where('id', $request->merchant_id)->first();
|
||
if (!$Account)
|
||
return $this->failure('商户信息有误。');
|
||
$id = Anchor::where('m_id', $Account->id)->value('id');
|
||
} else {
|
||
$anchor_openid = $request->anchor_openid;
|
||
if (!$anchor_openid || $anchor_openid == 'null') {
|
||
// 返回空数组
|
||
$result = CommunityActivity::where('pay_type', 'array')->paginate();
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
$id = Anchor::where('openid', $anchor_openid)->value('id');
|
||
}
|
||
if (!$id) {
|
||
// 返回空数组
|
||
$result = CommunityActivity::where('pay_type', 'array')->paginate();
|
||
return $this->success('ok', $result);
|
||
}
|
||
$result = CommunityActivity::where('status', 1)->where('anchor_id', $id)->where('type', 'business')->where('class', $class)->orderBy('id', 'desc')->paginate();
|
||
}
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 社区-训练营列表
|
||
public function BusinessActivityList(Request $request)
|
||
{
|
||
$class = $request->class ?? 'one';
|
||
$result = CommunityActivity::select('id', 'title', 'merchant_id', 'price', 'pic', 'class', 'Subtitle', 'start_time', 'apply_deadline', 'end_time')
|
||
->where('status', 1)
|
||
->where('type', 'business')
|
||
->where('is_love_show', 1)
|
||
->where('class', $class)
|
||
->orderBy('h5_top', 'desc')
|
||
->orderBy('sort', 'desc')
|
||
->orderBy('apply_deadline','desc')
|
||
->orderBy('id', 'desc')
|
||
->paginate();
|
||
$time = date('Y-m-d H:i:s');
|
||
foreach ($result as $key => $value) {
|
||
if ($value->class == 'one') {
|
||
$is_deadline = 0;
|
||
$pay = TouristOrder::where('type', 'community')->where('type_id', $value->id)->where('account_id', $value->merchant_id)->whereIn('pay_status',[1,4])->first();
|
||
if ($pay) {
|
||
$is_deadline = 1;
|
||
} elseif ($value->apply_deadline && $value->apply_deadline <= $time) {
|
||
$is_deadline = 2;
|
||
} elseif ($value->end_time && $value->end_time <= $time) {
|
||
$is_deadline = 3;
|
||
}
|
||
$value->is_deadline = $is_deadline;
|
||
}
|
||
}
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 商家简介
|
||
public function merchantInfo(Request $request)
|
||
{
|
||
$id = $request->merchant_id;
|
||
$result = Anchor::select('m_id', 'name', 'pic')->where('m_id', $id)->first();
|
||
return $this->success('ok', $result);
|
||
|
||
}
|
||
|
||
// 注册用户
|
||
public function register(Request $request)
|
||
{
|
||
$mobile = $request->mobile;
|
||
$code = $request->code;
|
||
$password = $request->password ?? $mobile;
|
||
$sms = new Sms(new App);
|
||
$result = $sms->check($mobile, $code);
|
||
if ($result)
|
||
return $this->failure($result, ['statu' => 1]);
|
||
$merchantuser = MerchantUsers::where('mobile', $mobile)->first();
|
||
if ($merchantuser) {
|
||
$merchantuser->token = $this->api_token($merchantuser);
|
||
return $this->success('ok', $merchantuser);
|
||
}
|
||
$MerchantUsers = new MerchantUsers();
|
||
$rand_str = $this->randString(8);
|
||
$MerchantUsers->mobile = $mobile;
|
||
$MerchantUsers->rand_str = $rand_str;
|
||
$MerchantUsers->password = encrypt($password);
|
||
$MerchantUsers->nickname = '用户-' . $rand_str;
|
||
$MerchantUsers->save();
|
||
// \CommonUtilsService::addNewAnchorToUser($anchor);
|
||
$MerchantUsers->token = $this->api_token($MerchantUsers);
|
||
$MerchantUsers->pic = User::DefaultAvatar;
|
||
|
||
return $this->success('ok', $MerchantUsers);
|
||
|
||
}
|
||
|
||
//创建token
|
||
public function api_token($account)
|
||
{
|
||
$token = $account->api_token;
|
||
if ($token) {
|
||
try {
|
||
$result = decrypt($token);
|
||
}catch (\Exception $e) {
|
||
$token = encrypt($account->id . '-' . $account->mobile . '-' . time() . '-' . $account->email);
|
||
$account->api_token = $token;
|
||
$account->save();
|
||
}
|
||
|
||
$time = explode('-', $result)[2];
|
||
if (time() - $time > 604800) {
|
||
$token = encrypt($account->id . '-' . $account->mobile . '-' . time() . '-' . $account->email);
|
||
$account->api_token = $token;
|
||
$account->save();
|
||
}
|
||
} else {
|
||
$token = encrypt($account->id . '-' . $account->mobile . '-' . time() . '-' . $account->email);
|
||
$account->api_token = $token;
|
||
$account->save();
|
||
}
|
||
return $token;
|
||
}
|
||
|
||
// 登录
|
||
public function Login(Request $request)
|
||
{
|
||
$mobile = $request->mobile;
|
||
if (empty($mobile)) return $this->failure('请输入手机号', ['statu' => 1]);
|
||
$account = MerchantUsers::select('id', 'openid', 'nickname', 'pic')->where('mobile', $mobile)->first();
|
||
if (!$account) return $this->failure('账户不存在,请先注册');
|
||
|
||
if ($request->code) {
|
||
$code = $request->code;
|
||
//检查验证码
|
||
$sms = new Sms(new App);
|
||
$result = $sms->check($mobile, $code);
|
||
if ($result)
|
||
return $this->failure($result, ['statu' => 1]);
|
||
} elseif ($request->password) {
|
||
$password = $request->password;
|
||
if ($password != decrypt($account->password)) return $this->failure('密码有误,请确认后再输入', ['statu' => 1]);
|
||
unset($account->password);
|
||
} else {
|
||
return $this->failure('请输入密码', ['statu' => 1]);
|
||
}
|
||
$this->api_token($account);
|
||
return $this->success('ok', $account);
|
||
|
||
}
|
||
|
||
// 社区-训练营详情
|
||
public function activityDetail(Request $request)
|
||
{
|
||
$id = $request->id;
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if ($wechatUser) {
|
||
$openid = $wechatUser->getId();
|
||
} else {
|
||
$openid = $request->openid;
|
||
}
|
||
$result = CommunityActivity::where('id', $id)->first();
|
||
if (!$result) return $this->failure('该活动已下线或不存在');
|
||
$result->increment('pv', 1);
|
||
$result->price = floatval($result->price);
|
||
$banner = LiveBanner::where('class', 'community')->where('class_id', $id)->value('icon');
|
||
$result->banner = json_decode($banner, true);
|
||
$result->sku = json_decode($result->sku, true);
|
||
$token = WangYiYunUser::where('accid', $openid)->value('token');
|
||
if (!$token && $openid) {
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
$moreInfo = $wechatUser->getOriginal();
|
||
// 创建token
|
||
$rand_str = $this->randString(10);
|
||
$data['openid'] = $openid;
|
||
$data['nickname'] = $moreInfo['nickname'] ?? '用户' . $rand_str;
|
||
$data['sex'] = 0;
|
||
$this->createWyyUser($data, $openid);
|
||
$token = WangYiYunUser::where('accid', $request->openid)->value('token');
|
||
|
||
}
|
||
$result->token = $token;
|
||
$url = env('APP_URL') . '/pu/#/activityDetails/' . $id;
|
||
$url = urlencode($url);
|
||
$url = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?from_openid=' . $openid . '&merchant_id=' . $request->merchant_id . '&url=' . $url;
|
||
$key = 'activityDetail_qrcode' . $id;
|
||
// 是否购买过
|
||
$pay_status = TouristOrder::where('open_id', $openid)->where('type', 'community')->whereIn('pay_status',[1,4])->where('type_id', $id)->exists();
|
||
$result->pay_status = $pay_status;
|
||
// if (Redis::get($key)) {
|
||
// $share_qr_code = Redis::get($key);
|
||
// } else {
|
||
$share_qr_code = $this->getPreviewQrcode($url);
|
||
// Redis::setex($key, 3600 * 60, $share_qr_code);
|
||
// }
|
||
$result->share_qr_code = $share_qr_code;
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 社区-训练营详情
|
||
public function BusinessactivityDetail(Request $request)
|
||
{
|
||
$id = $request->id;
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
$openid = null;
|
||
$merchant_user = null;
|
||
// $wechatUser = true;
|
||
if ($wechatUser) {
|
||
$openid = $wechatUser->getId();
|
||
// $openid = "orseM6bZX6g9AT3Wvg_G0ccnNGx8";
|
||
$viewer = Viewer::where('openid', $openid)->first();
|
||
$user_service = new UserService();
|
||
if ($viewer && $viewer->user_id) {
|
||
$user = User::where('id', $viewer->user_id)->first();
|
||
if ($user) {
|
||
$merchant_user = $user_service->syncSaasUser($user);
|
||
}else {
|
||
$merchant_user = $user_service->syncSaasUser(null, $openid);
|
||
}
|
||
}else {
|
||
$merchant_user = $user_service->syncSaasUser(null, $openid);
|
||
}
|
||
}
|
||
$result = CommunityActivity::withTrashed()->where('id', $id)->first();
|
||
if (!$result) return $this->failure('该活动已下线或不存在');
|
||
$result->price = floatval($result->price);
|
||
$user = AccessRecord::where('open_id', $openid)->where('account_id', $result->merchant_id)->exists();
|
||
if (!$user && $openid) {
|
||
$AccessRecord = new AccessRecord();
|
||
$AccessRecord->open_id = $openid;
|
||
$AccessRecord->account_id = $result->merchant_id;
|
||
$AccessRecord->channel = 1;
|
||
$AccessRecord->last_time = now();
|
||
$AccessRecord->save();
|
||
}
|
||
Redis::zscore('love_community_pv', $id);
|
||
$result->love_pv = Redis::zincrby('love_activity_pv', 1, $id);
|
||
$banner = LiveBanner::where('class', 'community')->where('class_id', $id)->value('icon');
|
||
$result->banner = json_decode($banner, true);
|
||
$result->sku = json_decode($result->sku, true);
|
||
|
||
if ($merchant_user) {
|
||
$accid = 10000000 + $merchant_user->id;
|
||
$token = WangYiYunUser::where('accid', $accid)->value('token');
|
||
if (!$token && $openid) {
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
$rand_str = $this->randString(10);
|
||
$nickname = '用户' . $rand_str;
|
||
if ($wechatUser) {
|
||
$moreInfo = $wechatUser->getOriginal();
|
||
$nickname = $moreInfo['nickname'] ?? $nickname;
|
||
}
|
||
// 创建token
|
||
$data['openid'] = $openid;
|
||
$data['nickname'] = $nickname;
|
||
$data['sex'] = 0;
|
||
$this->createWyyUser($data, $accid);
|
||
$token = WangYiYunUser::where('accid', $accid)->value('token');
|
||
|
||
}
|
||
$result->token = $token;
|
||
}
|
||
|
||
$url = env('APP_URL') . '/h5/#/activityDetails/' . $id;
|
||
$key = 'ActivityDetail_L' . $id;
|
||
if ($result->class == 'many') {
|
||
$url = env('APP_URL') . '/h5/#/serveDetails/' . $id;
|
||
$key = 'ServeDetail_L' . $id;
|
||
}
|
||
$url = urlencode($url);
|
||
$url = env('APP_URL') . '/api/official/live/wechat/oauth?from_openid=' . $openid . '&url=' . $url . '&merchant_id=' . $result->merchant_id;
|
||
// 是否购买过
|
||
$pay_status = false;
|
||
if ($merchant_user) {
|
||
$pay_status = TouristOrder::where('account_id', $merchant_user->id)->where('type', 'community')->whereIn('pay_status',[1,4])->where('type_id', $id)->exists();
|
||
}
|
||
$result->pay_status = $pay_status;
|
||
$reward_count = TouristOrder::where('type', 'reward_activity')->where('type_id', $id)->whereIn('pay_status',[1,4])->get()->count();
|
||
$result->reward_count = $reward_count;
|
||
$qr_code = Redis::get($key);
|
||
// if (!$qr_code) {
|
||
$qr_code = $this->getPreviewQrcode($url);
|
||
// Redis::setex($key, 60 * 60 * 24 * 7, $qr_code);
|
||
// $qr_code = Redis::get($key);
|
||
// }
|
||
// 是否领取过优惠券
|
||
$coupon = UserMember::where('type', 'community')->where('type_id', $id)->where('m_id', $request->merchant_id)->where('m_user_id', $request->merchant_user_id)->exists();
|
||
$result->share_qr_code = $qr_code;
|
||
//判断活动是否截止报名 is_deadline 0:可以报名 1:已报名 2:截止报名 3活动结束
|
||
$time = date('Y-m-d H:i:s');
|
||
if ($result->class == 'one') {
|
||
$is_deadline = 0;
|
||
if ($pay_status || $coupon) {
|
||
$is_deadline = 1;
|
||
} elseif ($result->apply_deadline && $result->apply_deadline <= $time) {
|
||
$is_deadline = 2;
|
||
} elseif ($result->end_time && $result->end_time <= $time) {
|
||
$is_deadline = 3;
|
||
}
|
||
$result->is_deadline = $is_deadline;
|
||
}
|
||
$result->start_time = $result->start_time ? date('Y-m-d H:i', strtotime($result->start_time)) : null;
|
||
$result->end_time = $result->end_time ? date('Y-m-d H:i', strtotime($result->end_time)) : null;
|
||
$result->apply_deadline = $result->apply_deadline ? date('Y-m-d H:i', strtotime($result->apply_deadline)) : null;
|
||
$type = 'community';
|
||
$key = 'love_' . $type . '_pv';
|
||
Redis::zincrby($key, 1, $id);
|
||
if(config('app.env') == 'production'){
|
||
if ($merchant_user) $merchant_user->addlog($result, 1);
|
||
}
|
||
//下架或删除状态 0下架 1正常上架 2已删除
|
||
$publish_state = 0;
|
||
if($result->deleted_at){
|
||
$publish_state = 2;
|
||
}elseif($result->status == 1){
|
||
$publish_state = 1;
|
||
}else{
|
||
$publish_state = 0;
|
||
}
|
||
$result->publish_state = $publish_state;
|
||
//联系人信息
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(!empty($wechatUser)){
|
||
$openId = $wechatUser->getId();
|
||
}else{
|
||
$openId = $request->openid;
|
||
}
|
||
$viewer = Viewer::where('openid', $openId)->first();
|
||
if($viewer){
|
||
$viewer_user = User::where('mobile', $viewer->mobile)->orWhere('id', $viewer->user_id)->first();
|
||
}
|
||
$linkmen['mobile'] = '';
|
||
$linkmen['name'] = '';
|
||
if(isset($viewer_user)){
|
||
$linkmen['mobile'] = $viewer_user->mobile;
|
||
$linkmen['name'] = $viewer_user->name;
|
||
}
|
||
$result->linkmen = $linkmen;
|
||
if($request->from_openid && $request->from_openid != 'null'){
|
||
$user_service = new UserService();
|
||
$user_service->generateClientComment(1,$openid,$request->from_openid,'服务',$result->id,$result->title);
|
||
}
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
/** 打赏订单 */
|
||
public function payReward(Request $request, $info_id)
|
||
{
|
||
try {
|
||
//用户id
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if ($wechatUser) {
|
||
$openid = $wechatUser->getId();
|
||
} else {
|
||
$openid = $request->openid;
|
||
}
|
||
if (config('app.debug')) {
|
||
$openid = 'odh7zsgI75iT8FRh0fGlSojc9PWM';
|
||
}
|
||
$merchant_user = MerchantUsers::where('openid', $openid)->first();
|
||
$type = $request->type ?? 'reward_info';
|
||
if (empty($openid)) throw new \Exception('打赏失败,未获取用户微信信息');
|
||
$merchant_id = $request->input('merchant_id');
|
||
if (empty($merchant_id)) throw new \Exception("打赏失败,获取到商家id");
|
||
$amount = $request->input('amount');
|
||
if (empty($amount)) return $this->failure("请输入打赏金额");
|
||
|
||
if (!is_numeric($amount) || (is_numeric($amount) && $amount < 0)) throw new \Exception('输入打赏金额格式错误');
|
||
$trade_no = \CommonUtilsService::getTradeNO();
|
||
$order = array(
|
||
'open_id' => $openid,
|
||
'price' => $amount,
|
||
'num' => 1,
|
||
'pay_type' => 'wechat',
|
||
'type' => $type,
|
||
'type_id' => $info_id,
|
||
'pay_status' => 0,
|
||
'trade_no' => $trade_no,
|
||
'from_openid' => $request->input('from_openid'),
|
||
'comment' => $request->input('comment'),
|
||
'merchant_id' => $merchant_id,
|
||
'account_id' => $merchant_user->id,
|
||
'name' => $merchant_user->nickname,
|
||
'mobile' => $merchant_user->mobile ?? 0,
|
||
'withdrawal_radio' => 100,
|
||
'channel' => 1,
|
||
|
||
);
|
||
$order = TouristOrder::create($order);
|
||
$callback = config('app.url') . '/api/s/h5/callback/reward/order/' . $trade_no;
|
||
$wx_pay = \WechatService::constructSWXPay($order, $openid, '用户打赏', $callback);
|
||
$order['wx_pay'] = $wx_pay;
|
||
return $this->success('ok', $order);
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('ok', $e);
|
||
}
|
||
}
|
||
|
||
public function createWyyUser($data, $accid)
|
||
{
|
||
$im_service = new IMService(env('IM_APP_KEY'), env('IM_APP_SECRET'));
|
||
$result = $im_service->createUserId($accid, $data['nickname'], $props = '{}', null);
|
||
//\Log::info('创建网易云账号');
|
||
//\Log::info($result);
|
||
if ($result['code'] == 200) {
|
||
$wyyUser = new WangYiYunUser;
|
||
$wyyUser->accid = $accid;
|
||
$wyyUser->name = $data['nickname'];
|
||
$wyyUser->gender = $data['sex'];
|
||
$wyyUser->token = $result['info']['token'];
|
||
$wyyUser->save();
|
||
} elseif ($result['code'] == 414 && $result['desc'] == 'already register') {
|
||
|
||
$result = $im_service->getUinfos([$accid]);
|
||
if ($result['code'] == 200) {
|
||
$info = $result['uinfos'][0];
|
||
$accid = '';
|
||
$name = '';
|
||
$gender = '';
|
||
$icon = '';
|
||
if (array_key_exists("accid", $info)) {
|
||
$accid = $info['accid'];
|
||
}
|
||
if (array_key_exists("name", $info)) {
|
||
$name = $info['name'];
|
||
}
|
||
if (array_key_exists("gender", $info)) {
|
||
$gender = $info['gender'];
|
||
}
|
||
if (array_key_exists("icon", $info)) {
|
||
$icon = $info['icon'];
|
||
}
|
||
$wyyUser = new WangYiYunUser;
|
||
$wyyUser->accid = $accid;
|
||
$wyyUser->name = $name;
|
||
$wyyUser->gender = $gender;
|
||
$wyyUser->icon = $icon;
|
||
$wyyUser->save();
|
||
|
||
//更新网易云token
|
||
$result = $im_service->updateUserToken($accid);
|
||
if ($result['code'] == 200) {
|
||
$wyyUser->token = $result['info']['token'];
|
||
}
|
||
$wyyUser->save();
|
||
}
|
||
}
|
||
}
|
||
|
||
public function getPreviewQrcode($scene_str)
|
||
{
|
||
$pic_path = storage_path('qrcode/'.time().$this->randString(6).'preview.png');
|
||
//二维码图片
|
||
QrCode::format('png')->margin(1)->size(600)->generate($scene_str,$pic_path);
|
||
$pic = $this->uploadFile($pic_path);
|
||
try{
|
||
if ($pic_path) {
|
||
unlink($pic_path);
|
||
}
|
||
}catch(\Exception $e) {
|
||
return $this->failure($e->getMessage());
|
||
}
|
||
return $pic;
|
||
}
|
||
|
||
// 社区-我的参与列表
|
||
public function mytouristOrder(Request $request)
|
||
{
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if ($wechatUser) {
|
||
$openid = $wechatUser->getId();
|
||
} else {
|
||
$openid = $request->openid;
|
||
}
|
||
$type = $request->type ?? 'community';
|
||
$merchant_user_id = MerchantUser::where('openid', $openid)->value('id');
|
||
$class = $request->class;
|
||
$result = TouristOrder::where('account_id', $merchant_user_id)->where('type', $type)->where('channel', '!=', 0);
|
||
if ($class) {//单次或者多次
|
||
$ids = CommunityActivity::withTrashed()->where('class', $class)/*->where('status',1)*/ ->pluck('id');
|
||
$result = $result->wherein('type_id', $ids);
|
||
}
|
||
$result = $result->whereIn('pay_status',[1,4])->orderBy('id', 'desc')->paginate();
|
||
$state = 0;
|
||
foreach ($result as $key => $value) {
|
||
$reulst = CommunityActivity::withTrashed()->where('id', $value->type_id)->first();
|
||
if (!$value->comment && !$value->Praise) {
|
||
$state = 2;//去评价
|
||
} else {
|
||
$state = 3;//查看评价
|
||
}
|
||
$value->title = $reulst->title;
|
||
$value->pic = $reulst->pic;
|
||
$value->Subtitle = $reulst->Subtitle;
|
||
$value->pay_type = $reulst->pay_type;
|
||
$value->state = $state;
|
||
if ($reulst->class == 'one') $value->type = 'activity';
|
||
if ($reulst->class == 'many') $value->type = 'service';
|
||
# code...
|
||
}
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
//商城 -- 商品列表
|
||
public function MerchantShops(Request $request)
|
||
{
|
||
$merchant_id = $request->merchant_id;
|
||
$keyword = $request->keyword;
|
||
$shops = MerchantShop::where('merchant_id', $merchant_id)->where('is_show', 1);
|
||
if ($keyword) {
|
||
$keyword = trim($keyword);
|
||
$shops = $shops->where(function ($sql) use ($keyword) {
|
||
$sql->where('title', 'like', '%' . $keyword . '%');
|
||
});
|
||
}
|
||
$shops = $shops->orderBy('id', 'desc')->paginate();
|
||
foreach ($shops as $key => $shop) {
|
||
$shop->banners = json_decode($shop->banners, true) ?? [];
|
||
$shop->sku = json_decode($shop->sku, true) ?? [];
|
||
}
|
||
return $this->success('ok', $shops);
|
||
}
|
||
|
||
//商品详情
|
||
public function shop_detail(Request $request, $id)
|
||
{
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if ($wechatUser) {
|
||
$openid = $wechatUser->getId();
|
||
} else {
|
||
$openid = $request->openid;
|
||
}
|
||
$shop = MerchantShop::where('id', $id)->first();
|
||
$shop->banners = json_decode($shop->banners, true) ?? [];
|
||
$shop->sku = json_decode($shop->sku, true) ?? [];
|
||
$shop->pic = $shop->icon;
|
||
unset($shop->icon);
|
||
$jump_url = urlencode(env('APP_URL').'/pu/#/mallDetail/'.$shop->id);
|
||
$url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$shop->merchant_id.'&url='.$jump_url.'&from_openid='.$openid;
|
||
// $share_qrcode = $this->getPreviewQrcode($url);
|
||
// $share_qrcode = Redis::get('ShopDetail_S '.$shop->id);
|
||
// if(!$share_qrcode){
|
||
$share_qr_code = $this->getPreviewQrcode($url);
|
||
$shop->share_qr_code = $share_qr_code;
|
||
Redis::zincrby('shop', 1, $id);
|
||
return $this->success('ok', $shop);
|
||
}
|
||
|
||
//订单详情 --商城
|
||
public function order_detail(Request $request, $id)
|
||
{
|
||
$order = TouristOrder::where('id', $id)->first();
|
||
$recive = ShopRecive::where('order_id', $order->id)->first();
|
||
$shop = MerchantShop::withTrashed()->where('id', $order->type_id)->first();
|
||
|
||
$recive = ShopRecive::where('order_id', $id)->first();
|
||
if ($recive->is_recived == 0) {
|
||
$state = 1; //确认收货
|
||
} elseif (!$order->comment && !$order->Praise) {
|
||
$state = 2;//去评价
|
||
} else {
|
||
$state = 3;//查看评价
|
||
}
|
||
|
||
$order->address = $recive->address;
|
||
$order->title = $shop->title;
|
||
$order->icon = $shop->icon;
|
||
if ($order->banners && $order->banners != 'null') $order->banners = json_decode($shop->banners) ?? [];
|
||
if ($order->remark && $order->remark != 'null') $order->remark = json_decode($shop->remark) ?? [];
|
||
$order->describe = $shop->describe;
|
||
$order->introduction = $shop->sub_title;
|
||
$order->sku = json_decode($shop->sku, true);
|
||
$order->state = $state;
|
||
return $this->success('ok', $order);
|
||
}
|
||
|
||
//我的购买 --商城商品
|
||
public function myShopOrder(Request $request)
|
||
{
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$merchant_id = $request->merchant_id;
|
||
$result = TouristOrder::where('account_id', $merchant_user_id)
|
||
->where('type', 'shop')
|
||
->whereIn('pay_status',[1,4])
|
||
->where('merchant_id', $merchant_id)
|
||
->orderBy('id', 'desc')
|
||
->paginate();
|
||
$state = 0;
|
||
foreach ($result as $key => $value) {
|
||
$shop = MerchantShop::withTrashed()->where('id', $value->type_id)->first();
|
||
$recive = ShopRecive::where('order_id', $value->id)->first();
|
||
if ($recive->is_recived == 0) {
|
||
$state = 1; //确认收货
|
||
} elseif (!$value->comment && !$value->Praise) {
|
||
$state = 2;//去评价
|
||
} else {
|
||
$state = 3;//查看评价
|
||
}
|
||
$value->title = $shop->title;
|
||
$value->pic = $shop->icon;
|
||
$value->Subtitle = $shop->sub_title;
|
||
$value->address = $recive->address;
|
||
$value->state = $state;
|
||
$value->order_status = $recive->order_status;
|
||
}
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
//确认收货
|
||
public function reciveShop(Request $request, $id)
|
||
{
|
||
$recive = ShopRecive::where('order_id', $id)->first();
|
||
if (!$recive) return $this->failure('订单信息有误');
|
||
$recive->is_recived = 1;
|
||
$recive->save();
|
||
return $this->success('ok');
|
||
}
|
||
|
||
// 购买社区活动
|
||
public function CommunityPayOrder(Request $request)
|
||
{
|
||
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
$merchant_user_id = $request->merchant_user_id ?? 0;
|
||
if ($wechatUser) {
|
||
$openid = $wechatUser->getId();
|
||
} else {
|
||
$openid = $request->openid;
|
||
}
|
||
$result = CommunityActivity::where('id', $request->id)->where('status', 1)->first();
|
||
if (!$result) return $this->failure('你参与的活动不存在或已下架。');
|
||
$sku_id = $request->sku_id;
|
||
if (!$sku_id && $result->pay_type == 'wechat') return $this->failure('规格参数不存在,请稍后再试。');
|
||
if ($sku_id) {
|
||
$skus = json_decode($result->sku, true);
|
||
$skus = array_column($skus, null, 'sku_id');
|
||
$sku = $skus[$sku_id];
|
||
$price = $sku['price'];
|
||
$goods = $sku['name'];
|
||
$desc = $result->title . '(' . $sku['name'] . ')';
|
||
} else {
|
||
$price = $result->price;
|
||
$goods= null;
|
||
$desc = $result->title;
|
||
}
|
||
$anchor = Anchor::where('id', $result->anchor_id)->first();
|
||
$trade_no = $this->getTradeNO();
|
||
$TouristOrder = new TouristOrder();
|
||
$TouristOrder->open_id = $openid;
|
||
$TouristOrder->price = floatval($price);
|
||
$TouristOrder->pay_type = $result->pay_type;
|
||
$TouristOrder->type = 'community';
|
||
$TouristOrder->type_id = $request->id;
|
||
$TouristOrder->trade_no = $trade_no;
|
||
$TouristOrder->goods = $goods;
|
||
$TouristOrder->desc = $desc;
|
||
$TouristOrder->from_openid = $request->from_openid;
|
||
$TouristOrder->share_channel_id = $request->share_channel_id;
|
||
$TouristOrder->merchant_id = $request->merchant_id ?? 0;
|
||
$TouristOrder->account_id = $merchant_user_id;
|
||
$TouristOrder->name = $request->name;
|
||
$TouristOrder->mobile = $request->mobile;
|
||
$TouristOrder->withdrawal_radio = $anchor->withdrawal_radio ?? 100;
|
||
if ($price == 0) {
|
||
$TouristOrder->pay_status = 1;
|
||
$TouristOrder->save();
|
||
$TouristOrder->pay_config = null;
|
||
$TouristOrder->wx_pay = [];
|
||
return $this->success('ok', $TouristOrder);
|
||
}
|
||
$TouristOrder->save();
|
||
$callback = config('app.url') . '/api/app/callback/Community/' . $trade_no;
|
||
$attributes = array(
|
||
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
|
||
'body' => '训练营',
|
||
'detail' => $result->title,
|
||
'out_trade_no' => $trade_no,
|
||
'total_fee' => round($price * 100),
|
||
'notify_url' => $callback, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
||
'openid' => $openid,
|
||
);
|
||
$result = \WechatService::officialPay($attributes);
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 商户版- 购买社区活动
|
||
public function BusinessCommunityPayOrder(Request $request)
|
||
{
|
||
$time = date('Y-m-d H:i:s');
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if ($wechatUser) {
|
||
$openid = $wechatUser->getId();
|
||
} else {
|
||
$openid = $request->openid;
|
||
}
|
||
$merchant_user = MerchantUsers::where('openid', $openid)->first();
|
||
$result = CommunityActivity::where('id', $request->id)->where('status', 1)->first();
|
||
if (!$result) return $this->failure('你参与的活动不存在或已下架。');
|
||
if ($result->class == 'one' && $result->apply_deadline) {
|
||
if ($result->apply_deadline <= $time) return $this->failure('该活动已截止报名,看看其他活动吧');
|
||
}
|
||
$sku_id = $request->sku_id;
|
||
if (!$sku_id && $result->pay_type == 'wechat') return $this->failure('规格参数不存在,请稍后再试。');
|
||
if ($sku_id) {
|
||
$skus = json_decode($result->sku, true);
|
||
$skus = array_column($skus, null, 'sku_id');
|
||
$sku = $skus[$sku_id];
|
||
$price = $sku['price'];
|
||
$goods = $sku['name'];
|
||
$desc = $result->title . '-' . $sku['name'] . '';
|
||
|
||
} else {
|
||
$price = $result->price;
|
||
$goods = null;
|
||
$desc = $result->title;
|
||
}
|
||
|
||
$anchor = Anchor::where('id', $result->anchor_id)->first();
|
||
$trade_no = $this->getRefundTradeNo();
|
||
$TouristOrder = new TouristOrder();
|
||
$TouristOrder->open_id = $openid;
|
||
$TouristOrder->price = floatval($price);
|
||
$TouristOrder->pay_type = $result->pay_type;
|
||
$TouristOrder->type = 'community';
|
||
$TouristOrder->type_id = $request->id;
|
||
$TouristOrder->trade_no = $trade_no;
|
||
$TouristOrder->goods = $goods;
|
||
$TouristOrder->desc = $desc;
|
||
$TouristOrder->channel = 1;
|
||
$TouristOrder->from_openid = $request->from_openid;
|
||
$TouristOrder->share_channel_id = $request->share_channel_id;
|
||
$TouristOrder->merchant_id = $result->merchant_id;
|
||
$TouristOrder->account_id = $merchant_user->id;
|
||
$TouristOrder->name = $request->name ?: $merchant_user->nickname;
|
||
$TouristOrder->area_code = $request->area_code;
|
||
$TouristOrder->mobile = $request->mobile ?: "未获取";
|
||
$TouristOrder->withdrawal_radio = $anchor->withdrawal_radio ?? 100;
|
||
$TouristOrder->insurance = $result->insurance ?: 0;
|
||
if ($request->insurance_info)
|
||
$TouristOrder->insurance_info = json_encode($request->insurance_info);
|
||
if ($request->linkmen)
|
||
$TouristOrder->linkmen = json_encode($request->linkmen);
|
||
if ($price == 0) {
|
||
$TouristOrder->pay_status = 1;
|
||
$TouristOrder->save();
|
||
$TouristOrder->pay_config = null;
|
||
$TouristOrder->wx_pay = [];
|
||
return $this->success('ok', $TouristOrder);
|
||
}
|
||
$TouristOrder->save();
|
||
$callback = config('app.url') . '/api/app/callback/Community/' . $trade_no;
|
||
$attributes = array(
|
||
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
|
||
'body' => '训练营',
|
||
'detail' => $result->title,
|
||
'out_trade_no' => $trade_no,
|
||
'total_fee' => round($price * 100),
|
||
'notify_url' => $callback, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
||
'openid' => $openid,
|
||
);
|
||
$result = \WechatService::officialPay($attributes);
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
//商户版 --购买商城商品
|
||
public function BusinessMerchantShop(Request $request)
|
||
{
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
$merchant_user_id = $request->merchant_user_id ?? 0;
|
||
if ($wechatUser) {
|
||
$openid = $wechatUser->getId();
|
||
} else {
|
||
$openid = MerchantUsers::where('id', $merchant_user_id)->value('openid');
|
||
}
|
||
$result = MerchantShop::where('id', $request->id)->where('is_show', 1)->first();
|
||
if (!$result) return $this->failure('你购买的商品不存在或已下架。');
|
||
$sku_id = $request->sku_id;
|
||
if (!$sku_id && $result->pay_type == 'wechat') return $this->failure('规格参数不存在,请稍后再试。');
|
||
if ($sku_id) {
|
||
$skus = json_decode($result->sku, true);
|
||
$skus = array_column($skus, null, 'sku_id');
|
||
$sku = $skus[$sku_id];
|
||
$price = $sku['price'];
|
||
$goods = $sku['name'];
|
||
$desc = $result->title . '(' . $sku['name'] . ')';
|
||
} else {
|
||
$price = $result->price;
|
||
$goods = null;
|
||
$desc = $result->title;
|
||
}
|
||
$pay_price = $request->num * $price;
|
||
// if($pay_price!=$request->price) return $this->failure('支付价格与商品价格不一致。');
|
||
$anchor = Anchor::where('id', $result->anchor_id)->first();
|
||
$trade_no = $this->getTradeNO();
|
||
$TouristOrder = new TouristOrder();
|
||
$TouristOrder->open_id = $openid;
|
||
$TouristOrder->price = floatval($pay_price);
|
||
$TouristOrder->pay_type = $result->pay_type;
|
||
$TouristOrder->type = 'shop';
|
||
$TouristOrder->num = $request->num;
|
||
$TouristOrder->type_id = $request->id;
|
||
$TouristOrder->trade_no = $trade_no;
|
||
$TouristOrder->goods = $goods;
|
||
$TouristOrder->desc = $desc;
|
||
$TouristOrder->from_openid = $request->from_openid;
|
||
$TouristOrder->share_channel_id = $request->share_channel_id;
|
||
$TouristOrder->merchant_id = $request->merchant_id ?? 0;
|
||
$TouristOrder->account_id = $merchant_user_id;
|
||
$TouristOrder->name = $request->name;
|
||
$TouristOrder->mobile = $request->mobile;
|
||
$TouristOrder->withdrawal_radio = $anchor->withdrawal_radio ?? 100;
|
||
$ShopRecive = new ShopRecive();
|
||
$ShopRecive->name = $request->name;
|
||
$ShopRecive->mobile = $request->mobile;
|
||
$ShopRecive->address = $request->address;
|
||
$ShopRecive->order_status = $request->order_status ?? 0;
|
||
$ShopRecive->account_id = $request->merchant_user_id ?? 0;
|
||
$ShopRecive->merchant_id = $result->merchant_id ?? 0;
|
||
if ($price == 0) {
|
||
$TouristOrder->pay_status = 1;
|
||
$TouristOrder->save();
|
||
$ShopRecive->order_id = $TouristOrder->id;
|
||
$ShopRecive->save();
|
||
$TouristOrder->pay_config = null;
|
||
$TouristOrder->wx_pay = [];
|
||
$TouristOrder->address = $request->address;
|
||
//发送通知
|
||
$way = 'merchant';
|
||
$res = MerchantShop::where('id', $TouristOrder->type_id)->first();
|
||
//给用户
|
||
\CommonUtilsService::sendBuySuccessNoticeToUser($res, $TouristOrder->open_id, $TouristOrder->type, $TouristOrder->trade_no, $TouristOrder->price);
|
||
//给商户
|
||
$merchant = MerchantAccount::where('id', $TouristOrder->merchant_id)->first();
|
||
$merchant_openid = $merchant->openid ?? '';
|
||
if (!$merchant_openid) {
|
||
$merchant_openid = Anchor::where('m_id', $res->merchant_id)->value('openid');
|
||
}
|
||
\CommonUtilsService::sendBuySuccessNoticeToBusiness($res, $merchant_openid, $way, $TouristOrder->price, $TouristOrder->mobile, $TouristOrder->name);
|
||
//发送推荐人
|
||
if ($TouristOrder->from_openid && $TouristOrder->from_openid != 'null') {
|
||
$way = 'recommend';
|
||
\CommonUtilsService::sendBuySuccessNoticeToBusiness($res, $TouristOrder->from_openid, $way, $TouristOrder->price, $TouristOrder->mobile, $TouristOrder->name);
|
||
}
|
||
//短信通知商家
|
||
$jump_url = env('APP_URL') . '/pu_m/#/earningsRecord?merchant_id=' . $merchant->id;
|
||
$short_url = \CommonUtilsService::shortUrl(env('APP_URL') . '/h5/#/jump_url?url=' . $jump_url);
|
||
$title = $res->title;
|
||
if (mb_strlen($title) > 5) {
|
||
$title = mb_substr($title, 0, 5, 'utf-8') . '...';
|
||
}
|
||
$message = '有用户下单了~ ' . $result->name . '购买了平台的《' . $title . '》' . $result->price . '元,点击' . $short_url['url'] . ' 查看详情';
|
||
Messenger::sendSMS($merchant->mobile, $message);
|
||
//增加记录
|
||
Message::create([
|
||
'phone' => $merchant->mobile,
|
||
'message' => $message,
|
||
'confirmed' => 1,
|
||
'code' => '购买通知',
|
||
'ip' => request() ? request()->ip() : '127.0.0.1',
|
||
]);
|
||
//插入购买通知
|
||
$content = $result->name . '购买了【' . $res->title . '】查看订单详情>>';
|
||
SaasNotice::addRecord($result->merchant_id, $result->account_id, 'order', $result->id, $content, 0);
|
||
return $this->success('ok', $TouristOrder);
|
||
}
|
||
$TouristOrder->save();
|
||
$ShopRecive->order_id = $TouristOrder->id;
|
||
$ShopRecive->save();
|
||
$callback = config('app.url') . '/api/app/callback/Community/' . $trade_no;
|
||
$attributes = array(
|
||
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
|
||
'body' => '商户商城',
|
||
'detail' => $result->title,
|
||
'out_trade_no' => $trade_no,
|
||
'total_fee' => round($price * 100) * $request->num,
|
||
'notify_url' => $callback, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
||
'openid' => $openid,
|
||
'order_id' => $TouristOrder->id,
|
||
);
|
||
$result = \WechatService::officialPay($attributes);
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 获取登录token
|
||
public function getToken(Request $request)
|
||
{
|
||
try {
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
// Log::info("授权微信信息");
|
||
if (!$wechatUser || empty($wechatUser)) {
|
||
return $this->failure('您尚未授权,请重新登录。');
|
||
}
|
||
$openid = $wechatUser->getId();
|
||
$result = MerchantUsers::where('openid', $openid)->first();
|
||
if (!$result)
|
||
return $this->failure('获取失败,请稍后再试');
|
||
$token = $this->api_token($result);
|
||
// Log::info("token:". $token);
|
||
$data['token'] = $token;
|
||
$data['openid'] = $openid;
|
||
$data['id'] = $result['id'];
|
||
return $this->success('ok', $data);
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('获取失败,请稍后再试');
|
||
}
|
||
|
||
}
|
||
|
||
// 我的收益记录
|
||
public function MyEarnings(Request $request)
|
||
{
|
||
$redis = Redis::connection('big_data');
|
||
$merchant_user_id = $request->merchant_user_id ?? 0;
|
||
$result = MEarning::where('m_user_id', $merchant_user_id)->paginate();
|
||
foreach ($result as $key => $value) {
|
||
$value->title = $value->order->desc ?? '';
|
||
$value->order_price = $value->order->price ?? 0;
|
||
$pay_user = MerchantUsers::where('id', $value->order->account_id)->first();
|
||
$value->pic = $pay_user->pic ?? User::DefaultAvatar;
|
||
$value->nickname = $pay_user->nickname ?? '匿名用户';
|
||
unset($value->order);
|
||
}
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 收益信息
|
||
public function EarningsInfo(Request $request)
|
||
{
|
||
$merchant_user_id = $request->merchant_user_id ?? 0;
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if ($wechatUser) {
|
||
$openid = $wechatUser->getId();
|
||
} else {
|
||
$openid = MerchantUsers::where('id', $merchant_user_id)->value('openid');
|
||
}
|
||
$data = [];
|
||
$data['total_price'] = MEarning::where('m_user_id', $merchant_user_id)->sum('value');
|
||
$member = 0;
|
||
if ($openid)
|
||
$member = TouristOrder::where('from_openid', $openid)->whereIn('pay_status',[1,4])->groupby('account_id')->get()->count();
|
||
$data['member'] = $member;
|
||
$data['account'] = MEarningTransfers::where('m_user_id', $merchant_user_id)->value('account') ?? '';
|
||
return $this->success('ok', $data);
|
||
}
|
||
|
||
// 提现记录
|
||
public function MyWithdraws(Request $request)
|
||
{
|
||
$merchant_user_id = $request->merchant_user_id ?? 0;
|
||
$result = MEarningwithdraws::where('m_user_id', $merchant_user_id)->paginate();
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 提现信息
|
||
public function WithdrawsInfo(Request $request)
|
||
{
|
||
$result = MEarningAccount::where('m_user_id', $request->merchant_user_id)->first();
|
||
$user = MerchantUsers::where('id', $request->merchant_user_id)->first();
|
||
$result->nickname = $user->nickname;
|
||
$result->pic = $user->pic ?? User::DefaultAvatar;
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 绑定支付账户
|
||
public function BindEarningAccounts(Request $request)
|
||
{
|
||
$merchant_id = $request->merchant_id;
|
||
$MEarningTransfers = new MEarningTransfers();
|
||
$MEarningTransfers->way = $request->way;
|
||
$MEarningTransfers->m_id = $merchant_id;
|
||
$MEarningTransfers->m_user_id = $request->merchant_user_id;
|
||
$MEarningTransfers->account = $request->account;
|
||
$MEarningTransfers->name = $request->name;
|
||
$MEarningTransfers->save();
|
||
return $this->success('ok', $MEarningTransfers);
|
||
}
|
||
|
||
// 提现
|
||
public function UserWithdrawal(Request $request)
|
||
{
|
||
$merchant_id = $request->merchant_id ?? $request->account_id;
|
||
if($merchant_id == 32) return $this->failure('提现功能维护中,请稍后再试!');
|
||
$merchant_user_id = $request->merchant_user_id ?? 0;
|
||
$account = $request->account;
|
||
$way = $request->way;
|
||
$alipay_real_name = $request->name;
|
||
$amount = $request->amount;
|
||
$data = [];
|
||
// 查询用户的提现余额是否充足
|
||
$earning_accounts = MEarningAccount::where('m_user_id', $merchant_user_id)->first();
|
||
if (!$earning_accounts) return $this->failure('您暂无提现额度');
|
||
if ($amount < 0.1) return $this->failure('提现额度有误,请重新输入');
|
||
if ($earning_accounts->balance < $amount) return $this->failure('提现额度不足,请重新输入');
|
||
$poundage = Redis::get('withdrawal_poundage') ?? 2; //提现手续费百分比
|
||
$poundage = $amount * ($poundage / 100) >= 0.01 ? $amount * ($poundage / 100) : 0.01;//收取手续费费用 不足1分 按一分处理
|
||
$actual_received = $amount - $poundage;//实际到账金额
|
||
$data['payee_account'] = $account;//支付宝账号
|
||
$data['payee_real_name'] = $alipay_real_name;//支付宝绑定姓名
|
||
$data['remark'] = '提现已到账'; //提现备注
|
||
$data['out_biz_no'] = \CommonUtilsService::getTradeNO();
|
||
$data['amount'] = $actual_received;
|
||
|
||
$ali = new LiveAlipayService();
|
||
$result = $ali->platTransferAccount($data);
|
||
if (is_array($result)) {
|
||
return $this->failure($result['msg']);
|
||
} else {//提现成功
|
||
MEarningwithdraws::create([
|
||
'm_id' => $merchant_id,
|
||
'm_user_id' => $merchant_user_id,
|
||
'way' => $way ?? 'alipay',
|
||
'value' => $amount,
|
||
'real_value' => $actual_received,
|
||
'account' => $account,
|
||
'name' => $alipay_real_name,
|
||
'trade_no' => $data['out_biz_no'],
|
||
'status' => 'finished',
|
||
]);
|
||
$earning_accounts->balance = $earning_accounts->balance - $amount;
|
||
$earning_accounts->withdrawl = $earning_accounts->withdrawl + $amount;
|
||
$earning_accounts->save();
|
||
}
|
||
return $this->success('ok');
|
||
}
|
||
|
||
// 获取问答测试列表
|
||
public function QA_testList(Request $request)
|
||
{
|
||
$merchant_id = $request->merchant_id;
|
||
$merchant_user_id = $request->merchant_user_id ?? 0;
|
||
$result = QATest::select('id', 'title', 'subtitle', 'pic', 'status', 'intro', 'created_at')->where('merchant_id', $merchant_id)->orderBy('is_recommend', 'desc')->paginate();
|
||
foreach ($result as $key => $value) {
|
||
$value->user_num = QATestRecord::where('q_a_test_id', $value->id)->groupby('merchant_user_id')->get()->count();
|
||
$value->status = QATestRecord::where('merchant_user_id', $merchant_user_id)->where('q_a_test_id', $value->id)->exists() ? 1 : 0;
|
||
}
|
||
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 获取问答测试详情
|
||
public function QA_testDetail(Request $request)
|
||
{
|
||
$merchant_id = $request->merchant_id;
|
||
$id = $request->id;
|
||
$QATest = QATest::where('id', $id)->where('merchant_id', $merchant_id)->first();
|
||
if (!$QATest) return $this->failure('测试不存在或已下架。');
|
||
$QATest->answer;
|
||
$QATest->question = json_decode($QATest->question, true);
|
||
$QATest->question_num = count($QATest->question) ?? 0;
|
||
return $this->success('ok', $QATest);
|
||
}
|
||
|
||
// 提交问答测试
|
||
public function QA_test(Request $request)
|
||
{
|
||
$merchant_id = $request->merchant_id;
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$q_a_test_id = $request->q_a_test_id;
|
||
$answer = $request->answer;
|
||
$score = 0;
|
||
foreach ($answer as $key => $value) {
|
||
$score += $value['score'];
|
||
}
|
||
$QATest = QATest::where('id', $q_a_test_id)->where('merchant_id', $merchant_id)->first();
|
||
if ($QATest->type != 'default') {
|
||
switch ($QATest->way) {
|
||
case '+':
|
||
$score = $score + $QATest->value;
|
||
break;
|
||
case '-':
|
||
$score = $score - $QATest->value;
|
||
break;
|
||
case '*':
|
||
$score = $score * $QATest->value;
|
||
break;
|
||
case '/':
|
||
$score = $score / $QATest->value;
|
||
break;
|
||
default:
|
||
$score = $score;
|
||
break;
|
||
}
|
||
|
||
}
|
||
$score = number_format($score, 2);
|
||
$q_a_answer = QAAnswer::where('min_score', '<=', $score)->where('max_score', '>=', $score)->where('q_a_test_id', $q_a_test_id)->first();
|
||
if (!$q_a_answer) {
|
||
$q_a_answer = QAAnswer::where('q_a_test_id', $q_a_test_id)->orderBY('max_score', 'desc')->first();
|
||
};
|
||
$obj = new QATestRecord();
|
||
$obj->merchant_user_id = $merchant_user_id;
|
||
$obj->q_a_test_id = $q_a_test_id;
|
||
$obj->q_a_answer_id = $q_a_answer->id;
|
||
$obj->score = $score ?? 0;
|
||
$obj->merchant_id = $merchant_id;
|
||
$obj->answer = json_encode($answer);
|
||
$obj->save();
|
||
$q_a_answer->score = $score;
|
||
return $this->success('ok', $q_a_answer);
|
||
}
|
||
|
||
// 我的问答测试
|
||
public function My_QA_test(Request $request)
|
||
{
|
||
$merchant_id = $request->merchant_id;
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$result = QATestRecord::where('merchant_user_id', $merchant_user_id)->where('merchant_id', $merchant_id)->orderBy('id', 'desc')->paginate();
|
||
foreach ($result as $key => $value) {
|
||
$value->answer = json_decode($value->answer, true);
|
||
$value->pic = $value->test->pic ?? '';
|
||
$value->title = $value->test->title ?? '测试不存在或已删除';
|
||
$value->question_num = 0;
|
||
if ($value->test) {
|
||
$value->question_num = count(json_decode($value->test->question)) ?? 0;
|
||
}
|
||
unset($value->test);
|
||
unset($value->answer);
|
||
}
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 获取问答测试详情
|
||
public function my_QA_test_Detail(Request $request)
|
||
{
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$id = $request->id;
|
||
if ($request->type == 'user') {
|
||
$Record = QATestRecord::where('id', $id)->where('merchant_user_id', $merchant_user_id)->first();
|
||
} else {
|
||
$Record = QATestRecord::where('q_a_test_id', $id)->where('merchant_user_id', $merchant_user_id)->orderBy('id', 'desc')->first();
|
||
}
|
||
$result = QAAnswer::select('title', 'content')->where('id', $Record->q_a_answer_id)->first();
|
||
$result->score = $Record->score;
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 问答测试用户
|
||
public function QA_testUser(Request $request)
|
||
{
|
||
$merchant_id = $request->merchant_id;
|
||
$id = $request->id;
|
||
$ids = QATestRecord::where('q_a_test_id', $id)->where('merchant_id', $merchant_id)->groupBy('merchant_user_id')->pluck('merchant_user_id');
|
||
$QATest = QATestRecord::select('id', 'merchant_user_id', 'q_a_test_id', 'score', 'q_a_answer_id', 'created_at')
|
||
->where('q_a_test_id', $id)
|
||
->where('merchant_id', $merchant_id)
|
||
->wherein('merchant_user_id', $ids)
|
||
->orderBy('id', 'desc')
|
||
->paginate();
|
||
foreach ($QATest as $key => $value) {
|
||
$value->nickname = $value->user->nickname ?? '匿名用户';
|
||
$value->pic = $value->user->pic ?? User::DefaultAvatar;
|
||
$value->title = $value->answer->title ?? '该测试不存在或已下架';
|
||
$value->content = $value->answer->content ?? null;
|
||
unset($value->answer);
|
||
unset($value->user);
|
||
}
|
||
return $this->success('ok', $QATest);
|
||
}
|
||
|
||
}
|