1175 lines
55 KiB
PHP
1175 lines
55 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Server\H5;
|
||
|
||
use App\Facades\WechatService;
|
||
use App\Models\Server\CollageGroup;
|
||
use App\Models\Server\CollageGroupHistories;
|
||
use App\Models\Server\MEarningShare;
|
||
use App\Models\Server\MerchantService;
|
||
use Illuminate\Http\JsonResponse;
|
||
use Illuminate\Http\Request;
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\App\Comment;
|
||
use App\Models\Course\Course;
|
||
use App\Models\Course\CourseClass;
|
||
use App\Models\Course\CourseClassification;
|
||
use App\Models\Course\CourseComments;
|
||
use App\Models\Course\CourseVideo;
|
||
use App\Models\Course\UserCourses;
|
||
use App\Models\Live\Anchor;
|
||
use App\Models\Server\MerchantAccount;
|
||
use App\Models\Server\MerchantUser;
|
||
use App\Models\Server\SaasNotice;
|
||
use App\Models\Server\TouristOrder;
|
||
use App\Models\User;
|
||
use App\Models\UserCoupon;
|
||
use App\Services\UserService;
|
||
use Illuminate\Pagination\LengthAwarePaginator;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Log;
|
||
use Illuminate\Support\Facades\Redis;
|
||
|
||
|
||
class CourseController extends Controller
|
||
{
|
||
|
||
/**
|
||
* 课程列表-商户
|
||
* *
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function BusinessCourseList(Request $request)
|
||
{
|
||
try {
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$class_id = $request->class_id;
|
||
$result = Course::join('merchant_services',function ($join){
|
||
$join->on('courses.id','=','merchant_services.type_id')
|
||
->where('merchant_services.status',1)
|
||
->where('merchant_services.type','course')
|
||
->whereNull('merchant_services.deleted_at');
|
||
},null,null,'right')
|
||
->where('courses.is_show', 1)
|
||
->where('merchant_services.merchant_id', $request->merchant_id)
|
||
->withCount('videos');
|
||
//if ($request->merchant_id != 88) {
|
||
$result = $result->where('courses.type', 'business');
|
||
//}
|
||
if($class_id){
|
||
$course_ids = CourseClassification::where('class_id',$class_id)->where('is_show',1)->pluck('course_id');
|
||
$result = $result->whereIn('courses.id',$course_ids);
|
||
}
|
||
$result = $result->orderBy('merchant_services.status', 'desc')->orderBy('merchant_services.is_top', 'desc')
|
||
->orderBy('merchant_services.top_time', 'desc')
|
||
->paginate();
|
||
$time = date('Y-m-d H:i:s');
|
||
foreach ($result as $key => $value) {
|
||
$group = CollageGroup::where('type', 'course')->where('type_id', $value->id)->where('start_time', '<', $time)
|
||
->where('end_time', '>', $time)->first();
|
||
if($group){
|
||
$value->is_group = 1;
|
||
}else{
|
||
$value->is_group = 0;
|
||
}
|
||
//原价
|
||
$value->original_price = $value->charge;
|
||
$value->discount_status = 0;
|
||
if ($value->end_time_discount > now() && $value->start_time_discount < now() && $value->discount_price) {
|
||
$value->charge = $value->discount_price;
|
||
$value->discount_status = 1;
|
||
}
|
||
$value->charge = floatval($value->charge);
|
||
$value['videos_count'] = $value->videos_count;
|
||
$user_courses = UserCourses::where('course_id', $value->id)
|
||
->where('merchant_user_id', $merchant_user_id)
|
||
->where('status', 1)
|
||
->first();
|
||
$order = TouristOrder::where('type','course')->where('type_id',$value->id)->where('account_id',$merchant_user_id)
|
||
->whereIn('pay_status',[1,4])
|
||
->first();
|
||
if($user_courses || $order){
|
||
$value['paymentStatus'] = true;
|
||
}else{
|
||
$value['paymentStatus'] = false;
|
||
}
|
||
}
|
||
return $this->success('ok', $result);
|
||
}catch (\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 商户-心得点赞
|
||
* *
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function BusinessMomentCourse(Request $request)
|
||
{
|
||
try {
|
||
$User = MerchantUser::where('id', $request->merchant_user_id)->first();
|
||
$comment = $request->comment;
|
||
$reply_id = $request->reply_id ?? 0;
|
||
$commented_type = 'App\Models\User';
|
||
$commentable_id = $request->commentable_id;
|
||
$commentable_type = 'App\Models\BesinessCourse';
|
||
|
||
$result = CourseComments::where('id', $commentable_id)->where('status', 1)->first();
|
||
if (!$result)
|
||
return $this->failure('未找到该记录~');
|
||
$Comment = new Comment();
|
||
$Comment->comment = $comment;
|
||
$Comment->reply_id = $reply_id;
|
||
$Comment->commented_id = $User->id;
|
||
$Comment->commentable_id = $commentable_id;
|
||
$Comment->commented_type = $commented_type;
|
||
$Comment->commentable_type = $commentable_type;
|
||
$Comment->save();
|
||
$data = [];
|
||
$data['comment'] = $comment;
|
||
$data['reply_id'] = $reply_id;
|
||
$data['nickname'] = $User['nickname'];
|
||
$data['avatar'] = $User['pic'];
|
||
$data['created_at'] = date('Y-m-d H:i:s');
|
||
return $this->success('ok', $data);
|
||
}catch (\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 课程心得详情-商户
|
||
* *
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function BusinessCourseExperienceDetail(Request $request)
|
||
{
|
||
try {
|
||
$id = $request->id;
|
||
//$user = MerchantUser::where('id',$request->merchant_user_id)->first();
|
||
$CourseComments = CourseComments::select('id', 'user_id', 'course_id', 'content', 'photos', 'video_id',
|
||
'created_at', 'merchant_user_id')->withCount('BusinessComments')->where('id', $id)->first();
|
||
// $user = User::find($CourseComments->id);
|
||
$user = MerchantUser::find($CourseComments->merchant_user_id);
|
||
if ($CourseComments) {
|
||
$CourseComments->comments = $CourseComments->BusinessComments;
|
||
unset($CourseComments->BusinessComments);
|
||
$CourseComments->nickname = $user->nickname ?? '匿名用户';
|
||
$CourseComments->avatar = $user->pic ?? User::DefaultAvatar;
|
||
$CourseComments->title = $CourseComments->video->title ?? '';
|
||
$CourseComments->photos = json_decode($CourseComments->photos, true);
|
||
unset($CourseComments->video);
|
||
foreach ($CourseComments->comments as $key => $value) {
|
||
$otherViewer = MerchantUser::where('id', $value->commented_id)->first();
|
||
$value['nickname'] = $otherViewer['nickname'] ?? '匿名用户';
|
||
$value['avatar'] = $otherViewer['pic'] ?? User::DefaultAvatar;
|
||
}
|
||
return $this->success('ok', $CourseComments);
|
||
}
|
||
return $this->failure('获取数据失败~');
|
||
}catch (\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取课程心得-商户
|
||
* *
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function BusinessGetCourseExperience(Request $request)
|
||
{
|
||
try {
|
||
$course_id = $request->course_id;
|
||
$video_id = $request->video_id;
|
||
if ($video_id) {
|
||
$type = 'video_id';
|
||
$obj = $video_id;
|
||
} else {
|
||
$type = 'course_id';
|
||
$obj = $course_id;
|
||
}
|
||
$result = CourseComments::select('id', 'course_id', 'user_id', 'merchant_user_id', 'content', 'photos',
|
||
'open_id', 'created_at', 'video_id')->withCount('BusinessComments')
|
||
->where($type, $obj)
|
||
->where('status', 1)
|
||
->where('is_show', 1)
|
||
->orderby('id', 'desc')
|
||
->simplePaginate();
|
||
$result = $this->ComMentInfoV2($result);
|
||
return $this->success('ok', $result);
|
||
}catch (\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 上传课程心得-商户
|
||
* *
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function BusinessCourseExperience(Request $request)
|
||
{
|
||
try {
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$course_id = $request->course_id ?? 0;
|
||
$video_id = $request->video_id ?? 0;
|
||
$content = $request->input('content') ?? '';
|
||
$photos = $request->photos ?? '';
|
||
//内容安全-图片
|
||
if ($photos) {
|
||
$result = \CommonUtilsService::imageContentCecurity($photos);
|
||
if ($result && isset($result['result']) && $result['result']) {
|
||
return $this->failure('图片' . $result['result'] . ',请换一张照片');
|
||
}
|
||
}
|
||
$photos = json_encode($photos);
|
||
CourseComments::updateOrCreate(['merchant_user_id' => $merchant_user_id, 'course_id' => $course_id,
|
||
'video_id' => $video_id], ['content' => $content, 'photos' => $photos, 'status' => 1, 'type' => 'business']);
|
||
|
||
return $this->success('ok', '提交成功');
|
||
}catch (\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 课程详情-商户
|
||
* *
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function BusinessCourseDetail(Request $request)
|
||
{
|
||
try {
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$id = $request->id;
|
||
$user_coupon_id = $request->user_coupon_id;
|
||
$result = Course::withTrashed()->where('id', $id)->withCount('videos')->first();
|
||
if (!$result){
|
||
return $this->failure('课程不存在');
|
||
}
|
||
//课程目录
|
||
//原价
|
||
$merchant_user = MerchantUser::find($merchant_user_id);
|
||
$merchant_user->preview($result);
|
||
$result->original_price = $result->charge;
|
||
$anchor = Anchor::where('m_id', $result->merchant_id)->first();
|
||
if ($anchor) {
|
||
$result->teacher_name = $anchor->name;
|
||
}
|
||
//折扣价
|
||
$result->discount_status = 0;
|
||
if ($result->end_time_discount > now() && $result->start_time_discount < now() && $result->discount_price) {
|
||
$result->charge = $result->discount_price;
|
||
$result->discount_status = 1;
|
||
}
|
||
if($user_coupon_id){
|
||
$user_coupon = UserCoupon::where('id',$user_coupon_id)->with('coupon')
|
||
->first();
|
||
if($user_coupon && $user_coupon->coupon){
|
||
switch($user_coupon->coupon->coupon_type){
|
||
case 1:
|
||
$result->discount_amount = $result->charge;
|
||
$result->charge = 0.00;
|
||
break;
|
||
case 2:
|
||
$result->discount_amount = number_format( $result->charge * (1 -$user_coupon->coupon->discount_rate),2);
|
||
$result->charge = number_format( $result->charge- $result->discount_amount,2);
|
||
break;
|
||
case 3:
|
||
$temp = number_format($result->charge - $user_coupon->coupon->discount_rate,2);
|
||
if($temp < 0){
|
||
$result->discount_amount = $result->charge;
|
||
$result->charge = 0.00;
|
||
}else{
|
||
$result->discount_amount = $user_coupon->coupon->discount_rate;
|
||
$result->charge = $temp;
|
||
}
|
||
break;
|
||
default:
|
||
$result->discount_amount = 0.00;
|
||
break;
|
||
}
|
||
}
|
||
}else{
|
||
$result->discount_amount = 0.00;
|
||
}
|
||
//结业状态
|
||
$result->graduation = false;
|
||
$paymentStatus = false;
|
||
$free_study = 0;//免费学习
|
||
$result->can_play = 1;
|
||
if ($merchant_user_id) {
|
||
// $paymentStatus = UserCourses::where('course_id', $id)->where('merchant_user_id', $merchant_user_id)
|
||
// ->where('status', 1)->count() ? true : false;
|
||
$user_course = UserCourses::where('course_id', $id)->where('merchant_user_id', $merchant_user_id)
|
||
->where('status', 1)->first();
|
||
$result->can_play = $user_course->can_play ?? 1;
|
||
$paymentStatus = $user_course ? true : false;
|
||
if($result->vip_free && $merchant_user){
|
||
$is_vip = $merchant_user->isVip();//课程设置会员免费观看 并且该用户是会员
|
||
$free_study = $is_vip == 0 ? 0 : 1;
|
||
if($free_study == 1) $paymentStatus = true;
|
||
}
|
||
//空对象的布尔值是true,此处需要转化为数组
|
||
$video = json_decode($result->videos, true);
|
||
if ($paymentStatus && !empty($video)) {
|
||
$result->videos[0]['can_watch'] = 1;
|
||
}
|
||
//查看心得数量
|
||
$comment_count = CourseComments::where('merchant_user_id', $request->merchant_user_id)
|
||
->where('course_id', $id)->where('status', 1)->where('is_show', 1)->count();
|
||
if ($comment_count > 1 && $comment_count == $result->videos_count) {
|
||
//是否结业
|
||
$result->graduation = true;
|
||
}
|
||
|
||
}
|
||
//下单后用户操作流程
|
||
$result->flow = MerchantService::where('merchant_id',$result->merchant_id)
|
||
->where('type_id',$result->id)->where('type','course')
|
||
->value('flow');
|
||
//付款状态
|
||
$result->paymentStatus = $paymentStatus;
|
||
$result->free_study = $free_study;
|
||
//获取视频播放地址 getVodInfo
|
||
foreach ($result->videos as $k => $value) {
|
||
$value->Viewing_status = 0;
|
||
$value->duration = ceil($value->duration / 60);
|
||
if ($merchant_user_id) {
|
||
$key = $merchant_user_id . 'course_record' . $id . 'video_id' . $value->id;
|
||
if (Cache::has($key))
|
||
$value->Viewing_status = 1;
|
||
//如果已经有心得
|
||
$count = CourseComments::where('merchant_user_id', $request->merchant_user_id)
|
||
->where('course_id', $id)->where('status', 1)->where('video_id', $value->id)->first();
|
||
if ($count) {
|
||
$value->Viewing_status = 2;
|
||
$value->can_watch = 1;
|
||
//记录可观看状态
|
||
if (isset($result->videos[$k + 1])) {
|
||
$result->videos[$k + 1]->can_watch = 1;
|
||
}
|
||
}
|
||
}
|
||
//是否已购买此课程
|
||
$value->has_buy = TouristOrder::where('type','video')->where('type_id',$value->id)->where('account_id',$merchant_user_id)
|
||
->whereIn('pay_status',[1,4])->exists()?1:0;
|
||
if (!$value->url) {
|
||
$info = $this->getVodInfo($value->aliyun_video_id);
|
||
$value->url = $info['Mezzanine']['FileURL'];
|
||
}
|
||
if(!$paymentStatus && $value->free_play == 0){
|
||
if($value->has_buy){
|
||
|
||
}else {
|
||
$value->url = '';//未报名用户 隐藏视频地址
|
||
}
|
||
}
|
||
//后台设置不可以播放 而且 不是免费的
|
||
if (isset($user_course) && $user_course->can_play == 0 && $value->free_play != 1){
|
||
$value->url = '';
|
||
}
|
||
}
|
||
//学员信息
|
||
foreach ($result->BusinessUserCourse as $key => $value) {
|
||
$user = MerchantUser::where('openid', $value->open_id)->first();
|
||
$result->BusinessUserCourse[$key]['nickname'] = $user['nickname'] ?? '该用户已隐藏';
|
||
$result->BusinessUserCourse[$key]['avatar'] = $user['pic'] ?? User::DefaultAvatar;
|
||
}
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
$openid = 0;
|
||
if ($wechatUser)
|
||
$openid = $wechatUser->getId();
|
||
$result->user_count = UserCourses::where('course_id', $result->id)->where('status', 1)->count();
|
||
$result->pv = Redis::zincrby('coursepv', 1, $id);
|
||
$jump_url = urlencode(env('APP_URL') . '/pu/#/courseDetail/' . $id);
|
||
$url = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $request->merchant_id .
|
||
'&anchor_openid=&url=' . $jump_url . '&from_openid=' . $openid;
|
||
// $qr_code = Redis::get('CourseDetail_S'.$id);
|
||
// if(!$qr_code){
|
||
$qr_code = $this->getPreviewQrcode($url);
|
||
// Redis::setex('CourseDetail_S'.$id,60*60*24*30,$qr_code);
|
||
// $qr_code = Redis::get('CourseDetail_S'.$id);
|
||
// }
|
||
$result->share_qr_code = $qr_code;
|
||
// 增加访问记录
|
||
$user = MerchantUser::where('id', $request->merchant_user_id)->first();
|
||
if (config('app.env') == 'production') {
|
||
$user->addlog($result);
|
||
}
|
||
//下架或删除状态 0下架 1正常上架 2已删除
|
||
if ($result->deleted_at) {
|
||
$publish_state = 2;
|
||
} elseif ($result->is_show == 1) {
|
||
$publish_state = 1;
|
||
} else {
|
||
$publish_state = 0;
|
||
}
|
||
$result->publish_state = $publish_state;
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
|
||
$merchant_user = MerchantUser::where('id', $merchant_user_id)->first();
|
||
$linkmen['name'] = '';
|
||
$linkmen['mobile'] = '';
|
||
if ($merchant_user) {
|
||
$result['share_nickname'] = $merchant_user->nickname ?? '匿名用户';
|
||
$result['share_avatar'] = $merchant_user->pic ?? User::DefaultAvatar;
|
||
$linkmen['name'] = $merchant_user->nickname;
|
||
$linkmen['mobile'] = $merchant_user->mobile;
|
||
}
|
||
$result->linkmen = $linkmen;
|
||
if($request->from_openid && $request->from_openid != 'null'){
|
||
$user_service = new UserService();
|
||
$user_service->generateClientComment(0,$openid,$request->from_openid,'课程',$result->id,$result->title);
|
||
}
|
||
$order_id = null;
|
||
if($free_study == 0) {
|
||
//检查是否配置拼团内容
|
||
$time = date('Y-m-d H:i:s');
|
||
$group = CollageGroup::where('type', 'course')->where('type_id', $request->id)->where('start_time', '<', $time)
|
||
->where('end_time', '>', $time)->first();
|
||
if ($group) $group->sku = json_decode($group->sku, true);
|
||
$result->has_group = $group ? 1 : 0;//1有 0无
|
||
$result->group = $group;
|
||
if (isset($group_sku)) $result->sku = $group_sku;
|
||
if ($group) {
|
||
$result->group->need_count = $group->require_num;
|
||
$history = CollageGroupHistories::with('tOrder')->whereHas('tOrder', function ($sql) {
|
||
$sql->whereIn('pay_status', [1, 4]);
|
||
})
|
||
->where('group_id', $group->id)->where('is_initiator', 1)
|
||
->where('m_user_id', $merchant_user_id)
|
||
->orderBy('created_at', 'desc')
|
||
->first();
|
||
if ($history) {
|
||
$order_id = $history->m_order_id;
|
||
//目前参与人员
|
||
$user_ids = CollageGroupHistories::with('tOrder')
|
||
->whereHas('tOrder', function ($sql) {
|
||
$sql->whereIn('pay_status', [1, 4]);
|
||
})
|
||
->where('group_id', $group->id)->where('deadline', $history->deadline)
|
||
->pluck('m_user_id')
|
||
->toArray();
|
||
$count = count($user_ids);
|
||
$need_count = $history->group->require_num - $count;
|
||
unset($history->group);
|
||
$result->group->need_count = $need_count;
|
||
}
|
||
$result->order_id = $order_id;
|
||
}
|
||
}
|
||
$order = TouristOrder::where('type','course')->where('type_id',$id)->where('account_id',$merchant_user_id)
|
||
->whereIn('pay_status',[1,4])
|
||
->first();
|
||
if($order){
|
||
$result->have_paid = true;
|
||
}else{
|
||
$result->have_paid = false;
|
||
}
|
||
//视频缓存
|
||
if ($request->input('video_id')) {
|
||
$result->video_cache_time = Cache::get('user:'.$merchant_user_id.'course:video:'.$request->video_id.':cache');
|
||
//下一集视频id
|
||
$result->next_video_id = $this->getNextVideo($result->videos, $request->video_id);
|
||
}
|
||
//记录分享记录
|
||
if ($request->from_openid && $request->from_openid != $merchant_user->openid) {
|
||
MEarningShare::create(["user_id"=>$merchant_user->id, "type"=>Course::class, 'type_id'=>$request->id, 'from_openid'=>$request->from_openid]);
|
||
}
|
||
return $this->success('ok', $result);
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息,请稍后再试');
|
||
}
|
||
}
|
||
|
||
public function getNextVideo($videos, $video_id)
|
||
{
|
||
$next_video_id = 0;
|
||
$status = 0;
|
||
foreach ($videos as $video) {
|
||
if ($status == 1) {
|
||
$next_video_id = $video->id;
|
||
break;
|
||
}
|
||
if ($video->id == $video_id) {
|
||
$status = 1;
|
||
}
|
||
}
|
||
// $next_video_id = CourseVideo::where('course_id',$course_id)->where('id', '>', $video_id)->orderBy('id', 'asc')->limit(1)->value('id')?:0;
|
||
return $next_video_id;
|
||
}
|
||
|
||
public function cacheVideotime(Request $request, $video_id)
|
||
{
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$time = $request->input('time');
|
||
if ($time || $time === 0) {
|
||
Cache::forever('user:'.$merchant_user_id.'course:video:'.$video_id.':cache',$time);
|
||
}
|
||
return $this->success('ok');
|
||
}
|
||
|
||
/**
|
||
* 视频详情-商户
|
||
* *
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function BusinessVideoDetail(Request $request)
|
||
{
|
||
try {
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$video_id = $request->video_id;
|
||
$video = CourseVideo::select('id', 'course_id', 'title', 'thumb', 'url', 'charge')->where('id', $video_id)->first();
|
||
$video->Viewing_status = 0;
|
||
$video->duration = ceil($video->duration / 60);
|
||
$video->can_watch = 0;
|
||
if ($merchant_user_id) {
|
||
$key = $merchant_user_id . 'course_record' . $video->course_id . 'video_id' . $video_id;
|
||
if (Cache::has($key))
|
||
$video->Viewing_status = 1;
|
||
//如果已经有心得
|
||
$count = CourseComments::where('merchant_user_id', $merchant_user_id)->where('course_id', $video->course_id)->where('status', 1)->where('video_id', $video_id)->first();
|
||
if ($count) {
|
||
$video->Viewing_status = 2;
|
||
$video->can_watch = 1;
|
||
}
|
||
} else {
|
||
$video->url = null;
|
||
}
|
||
return $this->success('ok', $video);
|
||
}catch (\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 我的课程订单-商户
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function BusinessMycourse(Request $request)
|
||
{
|
||
try {
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$anchor_openid = $request->anchor_openid;
|
||
if (!$anchor_openid || $anchor_openid == 'null')
|
||
$anchor_openid = MerchantAccount::where('id', $request->merchant_id)->value('openid');
|
||
$result = UserCourses::where('merchant_user_id', $merchant_user_id)->where('status', 1);
|
||
$ids = [];
|
||
if ($request->merchant_id) {
|
||
$ids = TouristOrder::where('account_id', $merchant_user_id)->where('merchant_id', $request->merchant_id)->where('type', 'course')->whereIn('pay_status', [1, 4])->pluck('type_id');
|
||
} else {
|
||
$ids = TouristOrder::where('account_id', $merchant_user_id)->where('type', 'course')->whereIn('pay_status', [1, 4])->pluck('type_id');
|
||
}
|
||
$result = $result->wherein('course_id', $ids);
|
||
$result = $result->wherenotnull('trade_no')->simplePaginate();
|
||
foreach ($result as $key => $value) {
|
||
$state = 0;
|
||
$value->Course;
|
||
$value->video_count = CourseVideo::where('course_id', $value->Course->id)->count();
|
||
$value->learning = 0;
|
||
$comment = CourseComments::where('merchant_user_id', $merchant_user_id)->where('course_id', $value->Course->id)->where('status', 1)->orderby('video_id', 'desc')->first();
|
||
$value->title = '未知';
|
||
if (isset($comment->video))
|
||
$value->title = $comment->video->title;
|
||
$comment_count = CourseComments::where('merchant_user_id', $merchant_user_id)->where('course_id', $value->Course->id)->where('status', 1)->count();
|
||
if ($comment_count >= 1)
|
||
$value->learning = 1;
|
||
if ($comment_count >= $value->video_count)
|
||
$value->title = '已学完';
|
||
$order = TouristOrder::where('trade_no', $value->trade_no)->first();
|
||
if ($order && !$order->Praise) {
|
||
$state = 2;
|
||
} else {
|
||
$state = 3;
|
||
}
|
||
$value->pay_cash = !empty($order)?$order->price:null;
|
||
$value->state = $state;
|
||
$value->type = 'business';
|
||
}
|
||
return $this->success('ok', $result);
|
||
}catch (\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取课程学员信息-商户
|
||
* */
|
||
public function BusinessStudents(Request $request)
|
||
{
|
||
try {
|
||
$course_id = $request->course_id ?? 0;
|
||
//获取群课程
|
||
if (!$course_id) return $this->failure('课程信息有误');
|
||
$UserCourses = UserCourses::select('merchant_user_id')->where('course_id', $course_id)->where('status', 1)->groupby('merchant_user_id')->get();
|
||
$data = [];
|
||
foreach ($UserCourses as $key => $value) {
|
||
if ($value->open_id) {
|
||
$info = MerchantUser::where('id', $value->merchant_user_id)->first();
|
||
// $user['id']= $value->user->id??0;
|
||
$user['nickname'] = $info->nickname ?? '匿名用户';
|
||
$user['avatar'] = $info->pic ?? User::DefaultAvatar;
|
||
array_push($data, $user);
|
||
}
|
||
unset($value->user);
|
||
}
|
||
$page = $request->page ?? 1;
|
||
$perPage = 15;
|
||
$offset = ($page * $perPage) - $perPage;
|
||
$result = new LengthAwarePaginator(
|
||
array_slice($data, $offset, $perPage),
|
||
count($data),
|
||
$perPage,
|
||
$page,
|
||
['path' => $request->url(), 'query' => $request->query()]
|
||
);
|
||
return $this->success('ok', $result);
|
||
}catch (\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 记录播放记录-商户
|
||
* *
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function BusinessAddRecord(Request $request)
|
||
{
|
||
try {
|
||
$courses_id = $request->courses_id;
|
||
$video_id = $request->video_id;
|
||
$key = $request->merchant_user_id . 'course_record' . $courses_id . 'video_id' . $video_id;
|
||
CourseVideo::where('id', $video_id)->increment('view_num', 1);
|
||
Cache::forever($key, true);
|
||
return $this->success('ok', '记录成功');
|
||
}catch (\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 我的课程心得-商户
|
||
* *
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function BusinessMyComments(Request $request)
|
||
{
|
||
try {
|
||
$merchant_user_id = $request->merchant_user_id;
|
||
$id = $request->id;
|
||
$type = $request->type;
|
||
$result = CourseComments::select('id', 'course_id', 'user_id', 'merchant_user_id', 'content', 'photos', 'created_at', 'video_id', 'open_id')->withCount('BusinessComments')
|
||
->where('merchant_user_id', $merchant_user_id)
|
||
->where($type, $id)
|
||
->where('status', 1)
|
||
->where('is_show', 1)
|
||
->orderby('id', 'desc')
|
||
->simplePaginate();
|
||
$result = $this->ComMentInfoV2($result);
|
||
return $this->success('ok', $result);
|
||
}catch (\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @param $result
|
||
* @return mixed
|
||
*/
|
||
public function ComMentInfoV2($result)
|
||
{
|
||
try {
|
||
foreach ($result as $key => $value) {
|
||
$time = strtotime($value->created_at);
|
||
$time = date('Y/m/d H:i:s', $time);
|
||
|
||
$value->title = $value->video->title ?? '';
|
||
$value->time = $time;
|
||
unset($value->video);
|
||
$value->photos = json_decode($value->photos, true);
|
||
$user = MerchantUser::where('id', $value->merchant_user_id)->first();
|
||
$value->nickname = $user->nickname ?? '';
|
||
$value->avatar = $user->pic ?? User::DefaultAvatar;
|
||
|
||
}
|
||
return $result;
|
||
}catch (\Exception $e){
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 购买课程-商户
|
||
* *
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function BusinessBuyingCourses(Request $request)
|
||
{
|
||
try {
|
||
$trade_no = \CommonUtilsService::getTradeNO();
|
||
$buy_type = $request->buy_type ?? 'normal';//normal:单独购买 group:拼团购买
|
||
$course_id = $request->course_id;
|
||
$cash = $request->cash;
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
$merchant_user_id = $request->merchant_user_id;//用户id
|
||
$user_coupon_id = $request->user_coupons_id;
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if ($wechatUser) {
|
||
$openid = $wechatUser->getId();
|
||
} else {
|
||
$merchant_user = MerchantUser::where('id', $request->merchant_user_id)->first();
|
||
$openid = $merchant_user->openid;
|
||
}
|
||
if($user_coupon_id && $buy_type == 'group'){
|
||
return $this->failure('优惠券与团购不能同时使用');
|
||
}
|
||
DB::beginTransaction();
|
||
$course = Course::where('id', $course_id)->first();
|
||
$anchor = Anchor::where('m_id',$request->merchant_id)->first();
|
||
if (!$course) {
|
||
return $this->failure('您购买的课程不存在或已下架~');
|
||
}
|
||
if($buy_type && $buy_type == 'group'){
|
||
$time = date('Y-m-d H:i:s');
|
||
$group = CollageGroup::where('type','course')->where('type_id',$course_id)->first();
|
||
if(!$group) return $this->failure('拼团信息不存在');
|
||
$sku = json_decode($group->sku, true) ?? null;
|
||
$history_id = $request->history_id;
|
||
$history = CollageGroupHistories::where('id',$history_id)->first();
|
||
if($history){
|
||
if($history->deadline < $time) return $this->failure('你慢了一步,拼团已过有效期');
|
||
if($history->status == 1) return $this->failure('你慢了一步,该团已被他人完成');
|
||
$m_order_ids = CollageGroupHistories::where('group_id',$history->group_id)
|
||
->where('m_user_id',$merchant_user_id)->where('status',0)->where('deadline','>',$time)
|
||
->pluck('m_order_id')->toArray();
|
||
$exists = TouristOrder::whereIn('id',$m_order_ids)->whereIn('pay_status',[1,4])->first();
|
||
if($exists) return $this->failure('该活动你有尚未完成的拼团,请耐心等待');
|
||
$exists = CollageGroupHistories::with('tOrder')->whereHas('tOrder',function($sql){
|
||
$sql->whereIn('pay_status',[1,4]);
|
||
})
|
||
->where('group_id',$group->id)->where('m_user_id',$merchant_user_id)
|
||
->where('status',1)
|
||
->first();
|
||
if($exists) return $this->failure('你已成功拼团参与过该活动,无需重复参与');
|
||
if($cash != $group->price){
|
||
return $this->failure('拼团价格与设置价格不符');
|
||
}
|
||
}
|
||
}
|
||
if ($cash != $course->charge && !$course->discount_price){
|
||
if($user_coupon_id){
|
||
$user_coupon = UserCoupon::where('id',$user_coupon_id)->with('coupon')->where('status',0)
|
||
->where('m_user_id',$merchant_user_id)->where('m_id',$request->merchant_id)
|
||
->first();
|
||
if(!$user_coupon || !$user_coupon->coupon){
|
||
return $this->failure('用户优惠券不存在');
|
||
}
|
||
if($user_coupon->coupon->type != 'course' || $user_coupon->coupon->type_id != $course_id){
|
||
return $this->failure('用户优惠券类型与购买的项目不符');
|
||
}
|
||
switch($user_coupon->coupon->coupon_type){
|
||
case 1:
|
||
$amount = 0.00;
|
||
break;
|
||
case 2:
|
||
$amount = number_format( $course->charge * $user_coupon->coupon->discount_rate,2);
|
||
break;
|
||
case 3:
|
||
$temp = number_format($course->charge - $user_coupon->coupon->discount_rate,2);
|
||
if($temp < 0){
|
||
$amount = 0.00;
|
||
}else{
|
||
$amount = $temp;
|
||
}
|
||
break;
|
||
default:
|
||
$amount = $course->charge;
|
||
break;
|
||
}
|
||
if(floatval($cash) != $amount){
|
||
return $this->failure('优惠金额错误 cash =' .$cash.',amount= '.$amount );
|
||
}
|
||
}elseif($buy_type == 'group'){
|
||
if($cash != $group->price){
|
||
return $this->failure('拼团订单金额错误');
|
||
}
|
||
}else{
|
||
return $this->failure('金额错误');
|
||
}
|
||
}
|
||
$result = UserCourses::where('merchant_user_id', $merchant_user_id)->where('course_id', $course_id)
|
||
->where('status', 1)
|
||
->first();
|
||
$order = TouristOrder::where('type','course')->where('type_id',$course_id)->where('account_id',$merchant_user_id)
|
||
->whereIn('pay_status',[1,4])
|
||
->first();
|
||
if ($result || $order){
|
||
return $this->failure('您已经购买过该课程啦~');
|
||
}
|
||
$MerchantUsers = MerchantUser::where('id', $merchant_user_id)->first();
|
||
list($pay_type, $sub_mch_id)= MerchantAccount::payType($request->merchant_id);
|
||
|
||
$TouristOrder = new TouristOrder();
|
||
$TouristOrder->open_id = $openid;
|
||
$TouristOrder->price = floatval($cash);
|
||
$TouristOrder->pay_type = $pay_type;
|
||
$TouristOrder->type = 'course';
|
||
$TouristOrder->goods = isset($sku) ? $sku[0]['name'] : '';
|
||
$TouristOrder->type_id = $course_id;
|
||
$TouristOrder->trade_no = $trade_no;
|
||
$TouristOrder->desc = $course->title;
|
||
$TouristOrder->from_openid = $request->from_openid;
|
||
$TouristOrder->share_channel_id = $request->share_channel_id;
|
||
$TouristOrder->merchant_id = $request->merchant_id;
|
||
$TouristOrder->account_id = $request->merchant_user_id;
|
||
$TouristOrder->name = $request->name ?: $MerchantUsers->nickname;
|
||
$TouristOrder->area_code = $request->area_code;
|
||
$TouristOrder->alliance_id = $request->alliance_id ?? null;
|
||
$TouristOrder->group_id = isset($group) ? $group->id : null;
|
||
if ($request->has('mobile')) {
|
||
$mobile = $request->mobile;
|
||
} else {
|
||
$mobile = $MerchantUsers->mobile;
|
||
}
|
||
if($user_coupon_id){
|
||
$TouristOrder->user_coupon_id = $user_coupon_id;
|
||
}
|
||
$TouristOrder->mobile = $mobile ?: '匿名用户';
|
||
$TouristOrder->withdrawal_radio = 100;
|
||
$user_course = new UserCourses();
|
||
$user_course->user_id = 0;
|
||
$user_course->course_id = $course_id;
|
||
$user_course->type = 'business';
|
||
$user_course->trade_no = $trade_no;
|
||
$user_course->status = 0;
|
||
$user_course->open_id = $openid;
|
||
$user_course->merchant_user_id = $merchant_user_id;
|
||
$TouristOrder->save();
|
||
if($buy_type == 'group'){
|
||
if($request->history_id){
|
||
$history = CollageGroupHistories::find($request->history_id);
|
||
$deadline = $history->deadline;
|
||
$is_initiator = 0;
|
||
$CollageGroupHistories = new CollageGroupHistories();
|
||
$CollageGroupHistories->m_user_id = $TouristOrder->account_id;
|
||
$CollageGroupHistories->group_id = $TouristOrder->group_id;
|
||
$CollageGroupHistories->m_order_id = $TouristOrder->id;
|
||
$CollageGroupHistories->group_master_id = $request->history_id;
|
||
$CollageGroupHistories->is_initiator = $is_initiator;
|
||
$CollageGroupHistories->status = 0;
|
||
$CollageGroupHistories->deadline =$deadline;
|
||
$CollageGroupHistories->save();
|
||
}else{
|
||
//分享不带history_id 就是团长
|
||
$deadline = date('Y-m-d H:i:s',strtotime('+'.$group->expire_in.'hours'));
|
||
$is_initiator = 1;
|
||
$CollageGroupHistories = new CollageGroupHistories();
|
||
$CollageGroupHistories->m_user_id = $TouristOrder->account_id;
|
||
$CollageGroupHistories->group_id = $TouristOrder->group_id;
|
||
$CollageGroupHistories->m_order_id = $TouristOrder->id;
|
||
$CollageGroupHistories->is_initiator = $is_initiator;
|
||
$CollageGroupHistories->status = 0;
|
||
$CollageGroupHistories->deadline =$deadline;
|
||
$CollageGroupHistories->save();
|
||
$CollageGroupHistories->group_master_id = $CollageGroupHistories->id;
|
||
$CollageGroupHistories->save();
|
||
}
|
||
}
|
||
if ($cash > 0) {
|
||
if($buy_type == 'group') {
|
||
$callback = config('app.url') . '/api/app/callback/group/' . $trade_no;
|
||
}else{
|
||
$callback = config('app.url') . '/api/app/callback/Community/' . $trade_no;
|
||
}
|
||
$attributes = array(
|
||
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
|
||
'body' => $course->title,
|
||
'detail' => '购买课程',
|
||
'out_trade_no' => $trade_no,
|
||
'total_fee' => round($cash * 100),
|
||
'notify_url' => $callback, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
||
'openid' => $openid,
|
||
'debug' =>config('wechat.payment.debug'),
|
||
);
|
||
if($pay_type == 'partner'){
|
||
$res = WechatService::partnerPay($TouristOrder->id, $sub_mch_id, $cash, $attributes['openid'], $attributes['out_trade_no'], $attributes['notify_url'], $attributes['body']);
|
||
}else {
|
||
$res = WechatService::officialPay($attributes);
|
||
}
|
||
$TouristOrder->save();
|
||
$user_course->save();
|
||
$res['id'] = $TouristOrder->id;
|
||
DB::commit();
|
||
return $this->success('ok', $res);
|
||
} else {
|
||
$way = 'merchant';
|
||
$TouristOrder->pay_status = 1;
|
||
$TouristOrder->save();
|
||
$user_course->status = 1;
|
||
$user_course->save();
|
||
if(!empty($user_coupon)){
|
||
$user_coupon->status = 1;
|
||
$user_coupon->save();
|
||
}
|
||
//发送通知
|
||
//给用户
|
||
\CommonUtilsService::sendBuySuccessNoticeToUser($course, $TouristOrder->open_id, $TouristOrder->type,
|
||
$trade_no, $TouristOrder->price);
|
||
//给商户
|
||
$merchant_openid = $course->open_id;
|
||
if (empty($merchant_id)) $merchant_openid = Anchor::where('m_id', $TouristOrder->merchant_id)
|
||
->value('openid');
|
||
\CommonUtilsService::sendBuySuccessNoticeToBusiness($course, $merchant_openid, $way,
|
||
$TouristOrder->price, $TouristOrder->mobile, $TouristOrder->name);
|
||
//发送推荐人
|
||
if ($TouristOrder->from_openid && $TouristOrder->from_openid != 'null') {
|
||
$way = 'recommend';
|
||
\CommonUtilsService::sendBuySuccessNoticeToBusiness($course, $TouristOrder->from_openid, $way,
|
||
$TouristOrder->price, $TouristOrder->mobile, $TouristOrder->name);
|
||
}
|
||
if( $TouristOrder->merchant_id == 491 ){
|
||
//给客服脱单姐 管理员
|
||
$openids = [
|
||
"oPC_2vn6Q3M5jQpw9xAS7NNCMfjs", "oPC_2vudkf3stdiNgjA-e2n6t9_M","oPC_2vtrwOLgWHLwVEFTFsJ7N7fw","oPC_2vt7nGwKk_OFzJL70SlVuTiU","oPC_2vg_Eur-Wa_Vwnx9JiyRVn9Q
|
||
"
|
||
];
|
||
foreach ($openids as $openid) {
|
||
\CommonUtilsService::sendBuySuccessNoticeToBusiness($course, $openid,'merchant',$TouristOrder->price,
|
||
$TouristOrder->mobile, $TouristOrder->namee);
|
||
}
|
||
|
||
}
|
||
//短信通知商家
|
||
$jump_url = env('APP_URL') . '/pu_m/#/earningsRecord?merchant_id=' . $TouristOrder->merchant_id;
|
||
$short_url = \CommonUtilsService::shortUrl(env('APP_URL') . '/h5/#/jump_url?url=' . $jump_url);
|
||
$title = $course->title;
|
||
if (mb_strlen($title) > 5) {
|
||
$title = mb_substr($title, 0, 5, 'utf-8') . '...';
|
||
}
|
||
$message = '有用户下单了~ ' . $TouristOrder->name . '购买了平台的《' . $title . '》' .
|
||
$TouristOrder->price . '元,点击' . $short_url['url'] . ' 查看详情';
|
||
$this->sentMessage($anchor->mobile,$message);
|
||
$content = $TouristOrder->name . '购买了【' . $course->title . '】查看订单详情>>';
|
||
SaasNotice::addRecord($TouristOrder->merchant_id, $TouristOrder->account_id, 'order',
|
||
$TouristOrder->id, $content, 0);
|
||
$TouristOrder->save();
|
||
$user_course->save();
|
||
DB::commit();
|
||
return $this->success('ok', ['status' => 1]);
|
||
}
|
||
}catch (\Exception $e){
|
||
DB::rollback();
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
|
||
// 课程标签列表
|
||
public function courseClasses(Request $request){
|
||
$m_id = $request->merchant_id;
|
||
$course_ids = MerchantService::where('merchant_id',$m_id)->where('status',1)->where('type','course')->pluck('type_id');
|
||
$class_ids = CourseClassification::whereIn('course_id',$course_ids)->where('is_show',1)->pluck('class_id');
|
||
$class_ids = CourseClass::whereIn('id',$class_ids)->select('id','name','sort')
|
||
->orderBy('sort','desc')->orderBy('id','desc')->get();
|
||
return $this->success('ok',$class_ids);
|
||
}
|
||
|
||
//分享单个课程视频
|
||
public function shareVideo(Request $request){
|
||
try {
|
||
$merchant_user = MerchantUser::find($request->merchant_user_id);
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if ($wechatUser) {
|
||
$openid = $wechatUser->getId();
|
||
} else {
|
||
$openid = $merchant_user->openid;
|
||
}
|
||
$course_id = $request->course_id;
|
||
$video_id = $request->video_id;
|
||
$course = Course::find($course_id);
|
||
$video = CourseVideo::find($video_id);
|
||
if(empty($course) || empty($video)) return $this->failure('该课程不支持分享');
|
||
$jump_url = urlencode(env('APP_URL') . '/pu/#/courseDetailList/'.$course_id.'?videos='.$video_id);
|
||
$url = env('APP_URL') . '/api/official/live/wechat/FamilyAuth?merchant_id=' . $request->merchant_id.'&url='.$jump_url.'&from_openid='.$openid;
|
||
return $this->success('ok',$url);
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 购买课程单个视频-商户
|
||
* *
|
||
* @param Request $request
|
||
* @return JsonResponse|string
|
||
*/
|
||
public function BuyingCoursesVideo(Request $request)
|
||
{
|
||
try {
|
||
$trade_no = \CommonUtilsService::getTradeNO();
|
||
$buy_type = $request->buy_type ?? 'normal';//normal:单独购买 group:拼团购买
|
||
$course_video_id = $request->course_video_id;
|
||
$cash = $request->cash;
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
$merchant_user_id = $request->merchant_user_id;//用户id
|
||
$user_coupon_id = $request->user_coupons_id;
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if ($wechatUser) {
|
||
$openid = $wechatUser->getId();
|
||
} else {
|
||
$merchant_user = MerchantUser::where('id', $request->merchant_user_id)->first();
|
||
$openid = $merchant_user->openid;
|
||
}
|
||
DB::beginTransaction();
|
||
$video = CourseVideo::where('id', $course_video_id)->first();
|
||
if (!$video) {
|
||
return $this->failure('您购买的课程不存在或已下架~');
|
||
}
|
||
$course = Course::find($video->course_id);
|
||
$anchor = Anchor::where('m_id',$request->merchant_id)->first();
|
||
|
||
$order = TouristOrder::where('type','video')->where('type_id',$course_video_id)->where('account_id',$merchant_user_id)
|
||
->whereIn('pay_status',[1,4])
|
||
->first();
|
||
if ($order){
|
||
return $this->failure('您已经购买过该课程啦~');
|
||
}
|
||
if($video->price != $cash){
|
||
return $this->failure('订单价格不正确~');
|
||
}
|
||
$MerchantUsers = MerchantUser::where('id', $merchant_user_id)->first();
|
||
$TouristOrder = new TouristOrder();
|
||
$TouristOrder->open_id = $openid;
|
||
$TouristOrder->price = floatval($cash);
|
||
$TouristOrder->pay_type = 'wechat';
|
||
$TouristOrder->type = 'video';
|
||
$TouristOrder->linkmen = $request->linkmen?json_encode($request->linkmen):'';
|
||
$TouristOrder->goods = isset($sku) ? $sku[0]['name'] : '';
|
||
$TouristOrder->type_id = $course_video_id;
|
||
$TouristOrder->trade_no = $trade_no;
|
||
$TouristOrder->desc = $video->title;
|
||
$TouristOrder->from_openid = $request->from_openid;
|
||
$TouristOrder->share_channel_id = $request->share_channel_id;
|
||
$TouristOrder->merchant_id = $request->merchant_id;
|
||
$TouristOrder->account_id = $request->merchant_user_id;
|
||
$TouristOrder->name = $request->name ?: $MerchantUsers->nickname;
|
||
$TouristOrder->area_code = $request->area_code;
|
||
$TouristOrder->alliance_id = $request->alliance_id ?? null;
|
||
$TouristOrder->group_id = isset($group) ? $group->id : null;
|
||
if ($request->has('mobile')) {
|
||
$mobile = $request->mobile;
|
||
} else {
|
||
$mobile = $MerchantUsers->mobile;
|
||
}
|
||
if($user_coupon_id){
|
||
$TouristOrder->user_coupon_id = $user_coupon_id;
|
||
}
|
||
$TouristOrder->mobile = $mobile ?: '匿名用户';
|
||
$TouristOrder->withdrawal_radio = 100;
|
||
$TouristOrder->save();
|
||
if ($cash > 0) {
|
||
$callback = config('app.url') . '/api/app/callback/video/' . $trade_no;
|
||
$attributes = array(
|
||
'trade_type' => 'JSAPI', // JSAPI,NATIVE,APP...
|
||
'body' => $video->title,
|
||
'detail' => '购买课程',
|
||
'out_trade_no' => $trade_no,
|
||
'total_fee' => round($cash * 100),
|
||
'notify_url' => $callback, // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
||
'openid' => $openid,
|
||
'debug' =>config('wechat.payment.debug'),
|
||
);
|
||
$result = \WechatService::officialPay($attributes);
|
||
$TouristOrder->save();
|
||
$result['id'] = $TouristOrder->id;
|
||
DB::commit();
|
||
return $this->success('ok', $result);
|
||
} else {
|
||
$way = 'merchant';
|
||
$TouristOrder->pay_status = 1;
|
||
$TouristOrder->save();
|
||
if(!empty($user_coupon)){
|
||
$user_coupon->status = 1;
|
||
$user_coupon->save();
|
||
}
|
||
//发送通知
|
||
//给用户
|
||
\CommonUtilsService::sendBuySuccessNoticeToUser($course, $TouristOrder->open_id, $TouristOrder->type,
|
||
$trade_no, $TouristOrder->price);
|
||
//给商户
|
||
$merchant_openid = $course->open_id;
|
||
if (empty($merchant_id)) $merchant_openid = Anchor::where('m_id', $TouristOrder->merchant_id)
|
||
->value('openid');
|
||
\CommonUtilsService::sendBuySuccessNoticeToBusiness($course, $merchant_openid, $way,
|
||
$TouristOrder->price, $TouristOrder->mobile, $TouristOrder->name);
|
||
//发送推荐人
|
||
if ($TouristOrder->from_openid && $TouristOrder->from_openid != 'null') {
|
||
$way = 'recommend';
|
||
\CommonUtilsService::sendBuySuccessNoticeToBusiness($course, $TouristOrder->from_openid, $way,
|
||
$TouristOrder->price, $TouristOrder->mobile, $TouristOrder->name);
|
||
}
|
||
if( $TouristOrder->merchant_id == 491 ){
|
||
//给客服脱单姐 管理员
|
||
$openids = [
|
||
"oPC_2vn6Q3M5jQpw9xAS7NNCMfjs", "oPC_2vudkf3stdiNgjA-e2n6t9_M","oPC_2vtrwOLgWHLwVEFTFsJ7N7fw","oPC_2vt7nGwKk_OFzJL70SlVuTiU","oPC_2vg_Eur-Wa_Vwnx9JiyRVn9Q
|
||
"
|
||
];
|
||
foreach ($openids as $openid) {
|
||
\CommonUtilsService::sendBuySuccessNoticeToBusiness($course, $openid,'merchant',$TouristOrder->price, $TouristOrder->mobile, $TouristOrder->name);
|
||
}
|
||
|
||
}
|
||
//短信通知商家
|
||
$jump_url = env('APP_URL') . '/pu_m/#/earningsRecord?merchant_id=' . $TouristOrder->merchant_id;
|
||
$short_url = \CommonUtilsService::shortUrl(env('APP_URL') . '/h5/#/jump_url?url=' . $jump_url);
|
||
$title = $course->title;
|
||
if (mb_strlen($title) > 5) {
|
||
$title = mb_substr($title, 0, 5, 'utf-8') . '...';
|
||
}
|
||
$message = '有用户下单了~ ' . $TouristOrder->name . '购买了平台的《' . $title . '》' .
|
||
$TouristOrder->price . '元,点击' . $short_url['url'] . ' 查看详情';
|
||
$this->sentMessage($anchor->mobile,$message);
|
||
$content = $TouristOrder->name . '购买了【' . $course->title . '】查看订单详情>>';
|
||
SaasNotice::addRecord($TouristOrder->merchant_id, $TouristOrder->account_id, 'order',
|
||
$TouristOrder->id, $content, 0);
|
||
$TouristOrder->save();
|
||
DB::commit();
|
||
return $this->success('ok', ['status' => 1]);
|
||
}
|
||
}catch (\Exception $e){
|
||
DB::rollback();
|
||
$this->getError($e);
|
||
return $this->failure('服务器休息中,请稍后再试');
|
||
}
|
||
}
|
||
}
|