419 lines
18 KiB
PHP
419 lines
18 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Server\EnterpriseH5;
|
||
|
||
use App\Models\AnchorVideo;
|
||
use App\Models\CommunityActivity;
|
||
use App\Models\ConsultAccount;
|
||
use App\Models\Consultation;
|
||
use App\Models\Course\Course;
|
||
use App\Models\Live\Anchor;
|
||
use App\Models\MerchantInformation;
|
||
use App\Models\MerchantShop;
|
||
use App\Models\QATest;
|
||
use App\Models\Server\MerchantAccount;
|
||
use App\Models\User;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\Redis;
|
||
use App\Http\Controllers\Controller;
|
||
|
||
class MerchantController extends Controller
|
||
{
|
||
//商户详情
|
||
public function merchant(Request $request,$merchant_id) {
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
if($openId) {
|
||
$merchant = MerchantAccount::where('openid',$openId)->first();
|
||
if($merchant) {
|
||
$is_merchant = 1;
|
||
$merchant_admin_url = env('APP_URL').'/api/official/merchat/auth/login';
|
||
if($merchant->id == $merchant_id) {
|
||
$is_same_merchant = 1;
|
||
} else {
|
||
$is_same_merchant = 0;
|
||
}
|
||
} else {
|
||
$is_merchant = 0;
|
||
$merchant_admin_url = '';
|
||
$is_same_merchant = 0;
|
||
}
|
||
} else {
|
||
$is_merchant = 0;
|
||
$merchant_admin_url = '';
|
||
$is_same_merchant = 0;
|
||
}
|
||
|
||
$account = MerchantAccount::where('id',$merchant_id)->withCount(['information','service','activity','course','video','test','shop'])->first();
|
||
$anchor = Anchor::where('m_id',$account->id)->first();
|
||
$account->name = '匿名商户';
|
||
$account->avatar = User::DefaultAvatar;
|
||
if($anchor){
|
||
$anchor->pic = $anchor->pic == '' ? User::DefaultAvatar : $anchor->pic;
|
||
$account->name = $anchor->name;
|
||
}
|
||
$services = [];
|
||
if($account->information_count>0) $services[] = 'information';
|
||
if($account->service_count>0) $services[] = 'service';
|
||
if($account->activity_count>0) $services[] = 'activity';
|
||
if($account->course_count>0) $services[] = 'course';
|
||
if($account->video_count>0) $services[] = 'video';
|
||
$ids = ConsultAccount::where('merchant_id',$merchant_id)->pluck('id')->toArray();
|
||
$account->consult_count = Consultation::whereIn('consult_account_id',$ids)->count();
|
||
if($account->consult_count>0) $services[] = 'consult';
|
||
if($account->test_count>0) $services[] = 'test';
|
||
if($account->shop_count>0) $services[] = 'shop';
|
||
$account->services = $services;
|
||
$jump_url = urlencode(env('APP_URL').'/pu/#/');
|
||
|
||
$url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$merchant_id.'&url='.$jump_url.'&alliance_id='.$request->alliance_id.'&from_openid='.$openId;
|
||
$qr_codes = Redis::get('HomePage_S'.$merchant_id);
|
||
if(!$qr_codes){
|
||
$qr_code = $this->getPreviewQrcode($url);
|
||
Redis::setex('HomePage_S'.$merchant_id,60*60*24*30,$qr_code);
|
||
$qr_codes = Redis::get('HomePage_S'.$merchant_id);
|
||
}
|
||
$share_url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$merchant_id.'&url='.$jump_url.'&alliance_id='.$request->alliance_id.'&from_openid='.$openId;
|
||
|
||
return $this->success('ok',compact('account','anchor','qr_codes','share_url','is_merchant','merchant_admin_url','is_same_merchant'));
|
||
}
|
||
|
||
//单个老师的课程列表
|
||
public function merchantCourses(Request $request, $merchant_id=0)
|
||
{
|
||
try{
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$alliance_id = $request->alliance_id;
|
||
if(!$alliance_id) {
|
||
return $this->failure('联盟ID必传');
|
||
}
|
||
//课程列表
|
||
if ($merchant_id) {
|
||
//$courses = Course::with('tagged')->where('merchant_id', $merchant_id);
|
||
$courses = Course::where('merchant_id', $merchant_id)->withCount('videos');
|
||
}else{
|
||
//$courses = Course::with('tagged');
|
||
$courses = Course::where('id','>',0)->withCount('videos');
|
||
}
|
||
|
||
$keyword = $request->input('keyword');
|
||
if ($keyword){
|
||
$courses = $courses->where('title', 'like', '%'.trim($keyword).'%');
|
||
}
|
||
if(isset($request->is_love_show) && in_array($request->is_love_show,[0,1])) {
|
||
$courses = $courses->where('is_love_show', $request->is_love_show);
|
||
}
|
||
$courses = $courses->where('is_show',1)->orderBy('id','desc')->paginate();
|
||
foreach($courses as $course){
|
||
$course_tags = [];
|
||
foreach($course->tags as $tag) {
|
||
$course_tags[] = $tag->name;
|
||
}
|
||
$course->course_tags = $course_tags;
|
||
unset($course->tagged);
|
||
$jump_url = urlencode(env('APP_URL').'/pu/#/courseDetail/'.$course->id);
|
||
$course->url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$course->merchant_id.'&anchor_openid=&url='.$jump_url.'&alliance_id='.$request->alliance_id.'&from_openid='.$openId;
|
||
}
|
||
return $this->success('ok', $courses);
|
||
}catch(\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('数据错误,请联系开发人员'.$e->getMessage());
|
||
}
|
||
}
|
||
|
||
/** 老师服务列表 */
|
||
public function merchantServices(Request $request, $merchant_id=0)
|
||
{
|
||
try{
|
||
$alliance_id = $request->alliance_id;
|
||
if(!$alliance_id) {
|
||
return $this->failure('联盟ID必传');
|
||
}
|
||
$activities = $this->getCommunityActities($merchant_id, $class='many',$request);
|
||
return $this->success('ok', $activities);
|
||
}catch(\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure("获取服务列表失败,请稍后再试");
|
||
}
|
||
}
|
||
|
||
/** 老师活动列表 */
|
||
public function merchantActivities(Request $request, $merchant_id=0)
|
||
{
|
||
try{
|
||
$alliance_id = $request->alliance_id;
|
||
if(!$alliance_id) {
|
||
return $this->failure('联盟ID必传');
|
||
}
|
||
$activities = $this->getCommunityActities($merchant_id, $class='one',$request);
|
||
return $this->success('ok', $activities);
|
||
}catch(\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure("获取活动列表失败,请稍后再试");
|
||
}
|
||
}
|
||
|
||
/** 老师资讯列表 */
|
||
public function merchantInfos(Request $request, $merchant_id=0)
|
||
{
|
||
try{
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$alliance_id = $request->alliance_id;
|
||
if(!$alliance_id) {
|
||
return $this->failure('联盟ID必传');
|
||
}
|
||
if ($merchant_id) {
|
||
$infos = MerchantInformation::where('merchant_id', $merchant_id)->orderBy('id', 'desc');
|
||
}else{
|
||
$infos = MerchantInformation::orderBy('id', 'desc');
|
||
}
|
||
$is_love_show = $request->is_love_show??2;
|
||
if($is_love_show != 2){
|
||
$infos = $infos->where('is_love_show',$is_love_show);
|
||
}
|
||
$keyword = trim($request->input('keyword'));
|
||
if ($keyword) {
|
||
$infos = $infos->where(function($sql) use($keyword){
|
||
$sql->where('title', 'like', '%'.$keyword.'%')
|
||
->orWhere('subTitle', 'like', '%'.$keyword.'%');
|
||
});
|
||
|
||
}
|
||
$infos = $infos->where('status',1)->select('id', 'merchant_id', 'title', 'subTitle', 'created_at', 'pic', 'status','is_love_show','reward_status')->paginate();
|
||
foreach ($infos as $item) {
|
||
$item->pv = Redis::zscore('information',$item->id)?:0;
|
||
$jump_url = urlencode(env('APP_URL').'/pu/#/dynamicParticulars/'.$item->id);
|
||
$item->url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$merchant_id.'&anchor_openid=&url='.$jump_url.'&alliance_id='.$request->alliance_id.'&from_openid='.$openId;
|
||
}
|
||
return $this->success('ok', $infos);
|
||
}catch(\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure("获取资讯列表失败,请稍后再试");
|
||
}
|
||
}
|
||
|
||
//单个老师的视频列表
|
||
public function merchantVideos(Request $request, $merchant_id=0)
|
||
{
|
||
try{
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$alliance_id = $request->alliance_id;
|
||
if(!$alliance_id) {
|
||
return $this->failure('联盟ID必传');
|
||
}
|
||
//课程列表
|
||
if ($merchant_id) {
|
||
//$courses = Course::with('tagged')->where('merchant_id', $merchant_id);
|
||
$videos = AnchorVideo::where('m_id', $merchant_id);
|
||
}else{
|
||
//$courses = Course::with('tagged');
|
||
$videos = AnchorVideo::where('id','>',0);
|
||
}
|
||
|
||
$keyword = $request->input('keyword');
|
||
if ($keyword){
|
||
$videos = $videos->where('title', 'like', '%'.trim($keyword).'%');
|
||
}
|
||
if(isset($request->is_love_show) && in_array($request->is_love_show,[0,1])) {
|
||
$videos = $videos->where('is_love_show', $request->is_love_show);
|
||
}
|
||
$videos = $videos->where('status',1)->orderBy('id','desc')->paginate();
|
||
foreach ($videos as $key => $video) {
|
||
$video->pv = Redis::zscore('short_videopv',$video->id)?:0;
|
||
$video->type = 'video';
|
||
$jump_url = urlencode(env('APP_URL').'/pu/#/videoDetails/'.$video->id);
|
||
$video->url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$video->m_id.'&anchor_openid=&url='.$jump_url.'&alliance_id='.$request->alliance_id.'&from_openid='.$openId;
|
||
}
|
||
return $this->success('ok', $videos);
|
||
}catch(\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('数据错误,请联系开发人员'.$e->getMessage());
|
||
}
|
||
}
|
||
|
||
//商家问答
|
||
public function merchantTests(Request $request, $merchant_id=0)
|
||
{
|
||
try {
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$alliance_id = $request->alliance_id;
|
||
if(!$alliance_id) {
|
||
return $this->failure('联盟ID必传');
|
||
}
|
||
if ($merchant_id) {
|
||
$tests = QATest::with('merchant')->withCount('record')->where('merchant_id',$merchant_id)->where('status',1);
|
||
} else {
|
||
$tests = QATest::with('merchant')->withCount('record');
|
||
}
|
||
$keyword = $request->input('keyword');
|
||
if ($keyword) {
|
||
$keyword = trim($keyword);
|
||
$tests = $tests->where(function($sql) use($keyword){
|
||
$sql->where('title', 'like', '%'.$keyword.'%');
|
||
});
|
||
}
|
||
$tests = $tests->where('status',1)->orderBy('id', 'desc')->paginate();
|
||
foreach ($tests as $test) {
|
||
$test->question = json_decode($request->question);
|
||
$jump_url = urlencode(env('APP_URL').'/pu/#/questionnaire/'.$test->id);
|
||
$test->url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$test->merchant_id.'&url='.$jump_url.'&alliance_id='.$request->alliance_id.'&from_openid='.$openId;
|
||
}
|
||
return $this->success("ok", $tests);
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
$this->failure("获取测试列表失败,请稍后再试");
|
||
}
|
||
}
|
||
|
||
//商家商城
|
||
public function merchantShops(Request $request, $merchant_id=0)
|
||
{
|
||
try {
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$alliance_id = $request->alliance_id;
|
||
if(!$alliance_id) {
|
||
return $this->failure('联盟ID必传');
|
||
}
|
||
$keyword = $request->keyword;
|
||
$is_show = $request->is_show??2;
|
||
if ($merchant_id) {
|
||
$shops = MerchantShop::with('merchant')->where('merchant_id',$merchant_id);
|
||
} else {
|
||
$shops = MerchantShop::with('merchant')->where('title','<>','【预售】JOHN英文原版2021品牌杂志(虚拟商品)');
|
||
}
|
||
|
||
if($is_show != 2){
|
||
$shops = $shops->where('is_show',$is_show);
|
||
}
|
||
if($keyword){
|
||
$shops = $shops->where('title','like','%'.$keyword.'%');
|
||
}
|
||
$shops = $shops->where('is_show',1)->paginate();
|
||
foreach ($shops as $shop) {
|
||
$jump_url = urlencode(env('APP_URL').'/pu/#/mallDetail/'.$shop->id);
|
||
$shop->url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$shop->merchant_id.'&url='.$jump_url.'&alliance_id='.$request->alliance_id.'&from_openid='.$openId;
|
||
}
|
||
return $this->success('ok', $shops);
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('获取商品列表失败,请稍后再试');
|
||
}
|
||
}
|
||
|
||
//商家咨询
|
||
public function merchantConsults(Request $request, $merchant_id=0)
|
||
{
|
||
try{
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
$alliance_id = $request->alliance_id;
|
||
if(!$alliance_id) {
|
||
return $this->failure('联盟ID必传');
|
||
}
|
||
if ($merchant_id) {
|
||
//咨询师id
|
||
$consult_account_ids = ConsultAccount::where('merchant_id', $merchant_id)->pluck('id');
|
||
$consults = Consultation::whereIn('consult_account_id', $consult_account_ids)->orderBy('id', 'desc');
|
||
}else{
|
||
$consults = Consultation::orderBy('id', 'desc');
|
||
}
|
||
$keyword = request()->input('keyword');
|
||
if($keyword){
|
||
$keyword = trim($keyword);
|
||
$consults = $consults->where(function($sql) use($keyword){
|
||
$sql->where('title','like','%'.$keyword.'%');
|
||
});
|
||
}
|
||
$consults = $consults->where('status',1)->with('teacher.merchant')->paginate();
|
||
foreach ($consults as $consult) {
|
||
$merchant_id = ConsultAccount::where('id',$consult->consult_account_id)->value('merchant_id');
|
||
$jump_url = urlencode(env('APP_URL').'/pu/#/consultingDetail/'.$consult->id);
|
||
$consult->url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$merchant_id.'&url='.$jump_url.'&alliance_id='.$request->alliance_id.'&from_openid='.$openId;
|
||
}
|
||
return $this->success('ok', $consults);
|
||
}catch(\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure("获取服务列表失败,请稍后再试");
|
||
}
|
||
}
|
||
|
||
public function getCommunityActities($merchant_id, $class,$request)
|
||
{
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if(empty( $wechatUser)){
|
||
$openId = $request->openid;
|
||
}else{
|
||
$openId = $wechatUser->getId();
|
||
}
|
||
if ($merchant_id) {
|
||
$activities = CommunityActivity::with('banners:class_id,icon')->where('merchant_id', $merchant_id);
|
||
}else{
|
||
$activities = CommunityActivity::with('banners:class_id,icon')->whereNotIn('title',['如何创建和管理服务','如何创建和管理活动']);
|
||
}
|
||
if(isset($request->is_love_show)&&$request->is_love_show!=2){
|
||
$activities = $activities->where('is_love_show',$request->is_love_show);
|
||
}
|
||
if($request->sortName=='timeSort'){
|
||
$sortType = $request->sortType??'desc';
|
||
$activities = $activities->orderBy('created_at', $sortType);
|
||
}
|
||
if($request->sortName=='hotSort'){
|
||
$sortType = $request->sortType??'desc';
|
||
$activities = $activities->orderBy('pv', $sortType);
|
||
}
|
||
$activities = $activities->where('status',1)->orderBy('id', 'desc');
|
||
$keyword = request()->input('keyword');
|
||
if($keyword){
|
||
$keyword = trim($keyword);
|
||
$activities = $activities->where(function($sql) use($keyword){
|
||
$sql->where('title','like','%'.$keyword.'%')
|
||
->orWhere('id','like','%'.$keyword.'%');
|
||
});
|
||
}
|
||
$activities = $activities->where('class', $class)->paginate();
|
||
foreach ($activities as $key => $activity) {
|
||
if($activity->class == 'one'){
|
||
$jump_url = urlencode(env('APP_URL').'/pu/#/activityDetails/'.$activity->id);
|
||
}else{
|
||
$jump_url = urlencode(env('APP_URL').'/pu/#/serveDetails/'.$activity->id);
|
||
}
|
||
$activity->url = env('APP_URL').'/api/official/live/wechat/FamilyAuth?merchant_id='.$activity->merchant_id.'&url='.$jump_url.'&alliance_id='.$request->alliance_id.'&from_openid='.$openId;
|
||
|
||
}
|
||
return $activities;
|
||
}
|
||
}
|