love_php/app/Http/Controllers/Server/H5/HomeController.php
2026-04-02 09:20:51 +08:00

352 lines
16 KiB
PHP

<?php
namespace App\Http\Controllers\Server\H5;
use App\Http\Response\ResponseJson;
use App\Models\SaasBanner;
use App\Models\Server\CommunityActivity;
use App\Models\Configs;
use App\Models\Consultation;
use App\Models\Course\Course;
use App\Models\MerchantShop;
use App\Models\QATest;
use App\Models\Server\EvaluateList;
use App\Models\Server\MerchantAccount;
use App\Models\Server\MerchantAdminLinks;
use App\Models\Server\MerchantAdmins;
use App\Models\Server\MerchantEvaluate;
use App\Models\Server\MerchantInfo;
use App\Models\Server\MerchantMembers;
use App\Models\Server\MerchantRecommendArea;
use App\Models\Server\MerchantServiceStatus;
use App\Models\Server\MerchantUser;
use App\Models\Server\ServiceOrderRole;
use App\Models\Server\ServiceRoleAdmin;
use App\Models\Server\TouristOrder;
use App\Models\User;
use App\Server\ServiceRole;
use Illuminate\Contracts\View\Factory;
use Illuminate\Foundation\Application;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use Illuminate\View\View;
use function foo\func;
class HomeController extends Controller
{
use ResponseJson;
/**
* 轮播推荐
* @param Request $request
* @return Factory|Application|JsonResponse|View
*/
public function carouselRecommend(Request $request)
{
try {
//单身用户
$single_users = [];
//轮播图
$carousel_photots = [];
//是否是福恋
$merchant_id = $request->merchant_id;
$m_user_id = $request->merchant_user_id;
$carousel_photots = SaasBanner::where('merchant_id',$merchant_id)
->select('id','pic','path','path_type')
->get();
foreach ($carousel_photots as $key => $carousel_photot) {
if($carousel_photot->path_type == 'h5') continue;
$key = 'carousel_photot_m_user_id'.$m_user_id.'--'.$carousel_photot->id;
$path_arr = parse_url($carousel_photot->path);
$path = $path_arr['path'];
$query = !empty($path_arr['query']) ? $path_arr['query'] : null;
if(Redis::get($key)){
$carousel_photot->path = Redis::get($key);
}else{
$wecharLink = \CommonUtilsService::getWecharLink($path,$query);
Redis::setex($key,60*60*24,$wecharLink);
$carousel_photot->path = $wecharLink;
}
}
$make_firend = MerchantInfo::where('m_id', $merchant_id)->value('make_friends');
if ($make_firend) {
$users = MerchantUser::whereNotNull('pic')->select('id', 'user_id', 'pic', 'nickname', 'created_at')
->whereHas('singleUser', function ($sql) use($merchant_id){
$sql->where('m_id', $merchant_id);
})
->orderByRaw("RAND()")->limit(4)->get();
foreach ($users as $user){
$single_users[] = $user->pic;
}
}
//轮播订单
$carousel_orders = TouristOrder::with('user:id,nickname,pic', 'activity:id')
->where('merchant_id', $merchant_id)
->where('pay_status', '>', 0)
->whereNotNull('desc')
->limit(10)
->select('id', 'pay_status', 'name', 'merchant_id', 'account_id', 'type','type_id', 'desc')
->orderBy('id', 'desc')
->get();
foreach ($carousel_orders as $order)
{
$name = $order->name?:$order->user->nickname;
$name = $name?mb_substr($name, 0,1,'utf-8'):'';
$text = $name.'**购买了 '.$order->desc;
$order->text = $text;
if($order->type == 'community' && $order->activity) $order->type = 'activity';
if($order->type == 'community' && empty($order->activity)) $order->type = 'activity';
if ($order->type == 'evaluate') {
$order->type_id = $order->merchant_evaluate?$order->merchant_evaluate->test_item_id:0;
}
unset($order->activity, $order->merchant_evaluate);
}
$account = MerchantAccount::find($merchant_id);
$show_services = [];
if (!empty($account)){
$show_services = $account->servicesStatus();
}
//各开启服务状态
return $this->success('ok', compact('single_users', 'carousel_orders', 'carousel_photots','show_services'));
} catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息,请稍后再试');
}
}
public function areaRecommend(Request $request){
try {
$merchant_id = $request->merchant_id;
//top
$top_service = MerchantRecommendArea::where('m_id', $merchant_id)->where('area_type', 'top')
->where('is_show', 1)->orderBy('id', 'desc')->first();
if ($top_service) {
$top_service = $this->areaRecommendDetail($top_service);
}
//middle
$middle_services = MerchantRecommendArea::where('m_id', $merchant_id)->where('area_type', 'middle')
->where('is_show', 1)->orderBy('id', 'asc')->get();
foreach ($middle_services as &$service) {
$this->areaRecommendDetail($service);
}
//below
$below_left_services = MerchantRecommendArea::where('m_id', $merchant_id)->where('area_type', 'below_left')
->where('is_show', 1)->orderBy('id', 'asc')->get();
foreach ($below_left_services as &$service) {
$this->areaRecommendDetail($service);
}
$below_right_services = [];
if ($merchant_id == 491) {
$below_right_services = MerchantRecommendArea::where('m_id', $merchant_id)->
where('area_type', 'below_right')->where('is_show', 1)->orderBy('id', 'asc')->get();
foreach ($below_right_services as &$service) {
$this->areaRecommendDetail($service);
}
}
foreach ($below_left_services as $key =>$val){
if($val->type == 'evaluate'){
$test_item_id = MerchantEvaluate::where('id',$val->type_id)
->where('merchant_id',$request->merchant_id)
->value('test_item_id');
$val->evaluate_type = EvaluateList::where('test_item_id',$test_item_id)
->value('type');
}
}
return $this->success('ok', compact('top_service', 'middle_services',
'below_left_services', 'below_right_services'));
}catch (\Exception $e) {
$this->getError($e);
return $this->failure('服务器休息,请稍后再试');
}
}
public function areaRecommendDetail($service){
if ($service->type == 'activity' || $service->type == 'service') {
$obj = \DB::table('community_activities as a')->where('id', $service->type_id)->selectRaw('id, title, pic, pv, price')->first();
}elseif($service->type == 'consult') {
$obj = \DB::table('consultations as c')->where('id', $service->type_id)->selectRaw('id, title, pic,pv,price ')->first();
$obj->pay_orders_count = TouristOrder::where('type','consult')->where('type_id',$service->type_id)->whereIn('pay_status',[1,4])->whereNotNull('Praise')->count();
$obj->pv = Redis::zscore('consulation', $obj->id)?:0;
}elseif ($service->type == 'course') {
$obj = \DB::table('courses as c')->where('id', $service->type_id)->selectRaw('id, title,thumb as pic, pv')->first();
}elseif ($service->type == 'evaluate') {
$orders = $this->ordersQuery($service);
$obj = \DB::table('merchant_evaluates as me')->leftJoinSub($orders, 'ufutx_o', function($join){
$join->on('me.id', 'o.type_id');
})->where('id', $service->type_id)->selectRaw('ufutx_me.id as id, pv, title,image as pic,price, ufutx_o.pay_orders_count')->first();
}elseif ($service->type == 'shop') {
$obj = \DB::table('merchant_shops as ms')->where('id', $service->type_id)->selectRaw('id, title,icon as pic, pv')->first();
}elseif ($service->type == 'test') {
$obj = \DB::table('q_a_tests as qat')->where('id', $service->type_id)->selectRaw('id, title, pic, pv')->first();
}
if($obj) {
$obj->pay_orders_count = $obj->pay_orders_count??0;
$obj->pv = $obj->pv??0;
}
$service->detail = $obj;
return $service;
}
public function ordersQuery($service)
{
if ($service->type == 'activity' || $service->type == 'service') {
$orders = \DB::table('tourist_orders')->where('type', 'community')->where('type_id', $service->type_id)->where('pay_status', '>', 0)->selectRaw('type_id, count(*) as pay_orders_count');
}else {
$orders = \DB::table('tourist_orders')->where('type', $service->type)->where('type_id', $service->type_id)->where('pay_status', '>', 0)->selectRaw('type_id, count(*) as pay_orders_count, id as order_id');
}
return $orders;
}
public function informationsRecommend(Request $request)
{
try {
$merchant_id = $request->merchant_id;
$infos = \DB::table('merchant_informations as i')->leftJoin('merchant_tags as t', 't.id', '=', 'i.tag_id')->where('merchant_id', $merchant_id)->whereNull('deleted_at')->where('status',1)->selectRaw('ufutx_i.id as id, merchant_id as m_id,title, ufutx_i.pic,ufutx_i.top_time,ufutx_i.created_at,"infor" as type, tag_id, ufutx_t.name as tag_name, ufutx_i.pv');
$videos = \DB::table('anchor_videos')->where('m_id', $merchant_id)->where('status',1)->whereNull('deleted_at')->union($infos)->selectRaw('id, m_id, title, cover as pic,top_time,created_at, "video" as type, 0 as tag_id, ""as tag_name, pv')->orderBy('top_time', 'desc')->orderBY('created_at', 'desc')->paginate();
foreach ($videos as $video) {
if ($video->type == "infor" && $video->tag_name == '心动计划') $video->type = 'love';
}
return $this->success('ok', $videos);
} catch (\Exception $e) {
$this->getError($e);
return $this->success("ok");
}
}
/**
* 商家成员详情
*/
public function getMemberDetail(Request $request)
{
try {
$merchant_id = $request->merchant_id;
$member_id = $request->member_id;
if (config('app.debug')) {
if (!$merchant_id || !$member_id) {
$this->failure('参数不全');
}
}
$data = MerchantMembers::where('id', $member_id)->where('status',1)
->where('mch_id', $merchant_id)
->select('id', 'mch_id', 'name', 'designation', 'avatar', 'introduction', 'created_at', 'admin_id', 'status')
->first();
if (!empty($data) && !empty($data['admin_id'])) {
$data['mobile'] = MerchantAdmins::where('id', $data['admin_id'])
->value('mobile');
$data['user_role'] = MerchantAdminLinks::where('mch_id', $merchant_id)
->where('admin_id', $data['admin_id'])
->value('user_role');
} else {
$data['user_role'] = 1;
}
return $this->success('处理成功', $data);
} catch (\Exception $e) {
return $this->failure('处理失败');
}
}
/**
* 商家成员列表
*/
public function getMemberList(Request $request){
$merchant_id = $request->merchant_id;
$keyword = $request->keyword;
$nopage = $request->nopage;
$data = MerchantMembers::where('status',1)->where('mch_id',$merchant_id)
->select('id', 'mch_id', 'name', 'designation', 'avatar', 'introduction', 'created_at', 'admin_id', 'status');
if($keyword){
$data = $data->where(function ($query) use ($keyword,$merchant_id){
$query->where(function ($sub_query_1) use($keyword,$merchant_id){
$sub_query_1->where('mch_id', $merchant_id)
->Where('name','like',"%$keyword%");
})
->orWhere(function ($sub_query_2) use($keyword,$merchant_id){
$sub_query_2->where('mch_id', $merchant_id)
->Where('designation','like',"%$keyword%");
})
->orWhere(function ($sub_query_3) use($keyword,$merchant_id){
$sub_query_3->where('mch_id', $merchant_id)
->Where('introduction','like',"%$keyword%");
});
});
}
if($nopage){
$data = $data->orderBy('id','desc')->get();
}else{
$data = $data->orderBy('id','desc')->paginate();
}
return $this->success('ok',$data);
}
/**
* 根据openid查询商家信息,用于绑定商家角色
*/
public function getMerchantByOpenid(Request $request){
$openid = $request->openid;
$order_id = $request->order_id;
$merchant_id = $request->merchant_id;
$role_id = $request->role_id;
$merchant = MerchantAccount::with('anchorV2')->where('id',$merchant_id)
->select('id','mobile','share_title','share_icon')
->first();
if(!$merchant){
return $this->failure('商家不存在');
}
$merchant = $merchant->toArray();
if($merchant['anchor_v2']){
$merchant['mobile'] = $merchant['mobile']??($merchant['anchor_v2']['mobile']);
$merchant['pic'] = $merchant['anchor_v2']['pic'] ?? User::DefaultAvatar;
$merchant['name'] = $merchant['anchor_v2']['name'] ?? $merchant['share_title'];
}else{
$merchant['mobile'] = $merchant['mobile']??null;
$merchant['pic'] = $merchant['share_icon'] ?? User::DefaultAvatar;
$merchant['name'] = $merchant['share_title'] ?? null;
}
unset($merchant['anchor_v2']);
$invite_merchant = MerchantAccount::with('anchor')->where('openid',$openid)
->select('id','mobile','share_title','share_icon','openid')
->first();
if($invite_merchant){
if($invite_merchant->anchor){
$invite_merchant = $invite_merchant->toArray();
$invite_merchant['mobile'] = $invite_merchant['mobile']??($invite_merchant['anchor']['mobile']);
$invite_merchant['pic'] = $invite_merchant['anchor']['pic'] ?? User::DefaultAvatar;
$invite_merchant['name'] = $invite_merchant['anchor']['name'] ?? $invite_merchant['share_title'];
}else{
$invite_merchant['mobile'] = $invite_merchant['mobile']??null;
$invite_merchant['pic'] = $invite_merchant['share_icon'] ?? User::DefaultAvatar;
$invite_merchant['name'] = $invite_merchant['share_title'] ?? null;
}
}
unset($invite_merchant['anchor']);
$order = TouristOrder::where('id',$order_id)->where('merchant_id',$merchant_id)
->whereIn('pay_status',[1,4])
->select('price','goods','type','type_id')
->first();
if(!$order){
return $this->failure('订单不存在');
}
$service = null;
switch ($order->type){
case 'community':
$service = CommunityActivity::where('id',$order->type_id)->select('id','title','pic')->first();
break;
default:
break;
}
if(!$service){
return $this->failure('服务不存在');
}
$role = ServiceOrderRole::where('order_id',$order_id)->where('id',$role_id)
->first();
if(!$role){
return $this->failure('角色不存在');
}
return $this->success('ok',compact('merchant','order','invite_merchant','service','role'));
}
}