624 lines
27 KiB
PHP
624 lines
27 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Models\AccessRecord;
|
||
use Illuminate\Http\Request;
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\App\Comment;
|
||
use App\Models\Course\Course;
|
||
use App\Models\Course\CourseComments;
|
||
use App\Models\Course\CourseVideo;
|
||
use App\Models\Course\UserCourses;
|
||
use App\Models\Live\Anchor;
|
||
use App\Models\Message;
|
||
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\Services\UserService;
|
||
use App\Utils\Messenger;
|
||
use Illuminate\Pagination\LengthAwarePaginator;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use Illuminate\Support\Facades\Log;
|
||
use Illuminate\Support\Facades\Redis;
|
||
|
||
class BusinessCourseController extends Controller
|
||
{
|
||
//
|
||
|
||
// 课程列表-商户
|
||
public function BusinessCourseList(Request $request)
|
||
{
|
||
if (\Auth::guard('api')->check()) {
|
||
$userId = \Auth::guard('api')->user()->id;
|
||
}
|
||
$result = Course::where('is_show',1)->where('is_love_show',1)->withCount('videos');
|
||
//if($request->merchant_id != 88){
|
||
$result = $result->where('type','business');
|
||
//}
|
||
$result= $result->orderBy('sort','desc')->Paginate();
|
||
foreach ($result as $key => $value) {
|
||
//原价
|
||
$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;
|
||
if(isset($userId))
|
||
$value['paymentStatus'] = UserCourses::where('course_id',$value->id)->where('user_id',$userId)->where('status',1)->count()?true:false;
|
||
|
||
}
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 商户-心得点赞
|
||
public function BusinessMomentCourse(Request $request)
|
||
{
|
||
$user = auth()->user();
|
||
$comment = $request->comment;
|
||
$reply_id = $request->reply_id??0;
|
||
$commented_type ='App\Models\User';
|
||
$commentable_id = $request->commentable_id;
|
||
$commentable_type = 'App\Models\Course';
|
||
|
||
$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;
|
||
$user = User::where('id',$user->id)->select('nickname','photo')->first();
|
||
$data['nickname']= $user['nickname'];
|
||
$data['avatar']= $user['photo'];
|
||
$data['created_at']= date('Y-m-d H:i:s');
|
||
return $this->success('ok',$data);
|
||
}
|
||
|
||
// 课程心得详情-商户
|
||
public function BusinessCourseExperienceDetail(Request $request)
|
||
{
|
||
$user = auth()->user();
|
||
$id = $request->id;
|
||
$CourseComments = CourseComments::select('id','user_id','course_id','content','photos','video_id','created_at')->withCount(['comments','BusinessComments'])->where('id',$id)->first();
|
||
|
||
if($CourseComments){
|
||
$user = User::where('id',$CourseComments->user_id)->first();
|
||
$CourseComments->isLker = $user->hasLiked($CourseComments) ?true:false;
|
||
$CourseComments->business_comments_count = $CourseComments->comments_count+$CourseComments->business_comments_count;
|
||
$CourseComments->business_comments = $CourseComments->BusinessComments;
|
||
// $CourseComments->comments = array_merge((array)$CourseComments->comments,(array)$CourseComments->business_comments);
|
||
// unset($CourseComments->business_comments);
|
||
$CourseComments->nickname = $user->nickname??'匿名用户';
|
||
$CourseComments->avatar = $user->photo;
|
||
$CourseComments->title = $CourseComments->video->title??'';
|
||
$CourseComments->photos = json_decode($CourseComments->photos,true);
|
||
unset( $CourseComments->video);
|
||
unset( $CourseComments->user);
|
||
$likers = $CourseComments->likers;
|
||
$liker = [];
|
||
$CourseComments->likerCount =$CourseComments->likers->count();
|
||
foreach ($likers as $k => $v) {
|
||
$liker[$k]['id'] = $v->id;
|
||
$liker[$k]['nickname'] = $v->nickname;
|
||
$liker[$k]['avatar'] = $v->avatar;
|
||
}
|
||
$CourseComments->liker = $liker;
|
||
unset($CourseComments->likers);
|
||
foreach ($CourseComments->comments as $key => $value) {
|
||
$user = User::select('id','photo','app_avatar','circle_avatar','nickname')->where('id',$value['commented_id'])->first();
|
||
$value['nickname'] = $user['nickname'] ?? '该用户已隐藏';
|
||
$value['avatar'] = $user['photo'] ?? User::DefaultAvatar;
|
||
}
|
||
foreach ($CourseComments->business_comments as $key => $value) {
|
||
$user = User::select('id','photo','app_avatar','circle_avatar','nickname')->where('id',$value['commented_id'])->first();
|
||
$value['nickname'] = $user['nickname'] ?? '该用户已隐藏';
|
||
$value['avatar'] = $user['photo'] ?? User::DefaultAvatar;
|
||
$CourseComments->comments[] = $value;
|
||
}
|
||
|
||
return $this->success('ok',$CourseComments);
|
||
|
||
}
|
||
return $this->failure('获取数据失败~');
|
||
}
|
||
|
||
public function ComMentInfo($result,$user)
|
||
{
|
||
foreach ($result as $key => $value) {
|
||
$time = strtotime($value->created_at);
|
||
$time = date('Y/m/d H:i:s',$time);
|
||
|
||
$value->likerCount =$value->likers->count();
|
||
$value->title =$value->video->title??'';
|
||
$value->time = $time;
|
||
$likers = $value->likers;
|
||
$liker = [];
|
||
foreach ($likers as $k => $v) {
|
||
$liker[$k]['id'] = $v->id;
|
||
$liker[$k]['nickname'] = $v->nickname;
|
||
$liker[$k]['avatar'] = $v->avatar;
|
||
}
|
||
$value->liker = $liker;
|
||
$value->business_comments_count = $value->comments_count+$value->business_comments_count;
|
||
unset($value->likers);
|
||
unset($value->video);
|
||
$value->photos = json_decode($value->photos,true);
|
||
$author = User::where('id',$value['user_id'])->first();
|
||
$value->nickname = $author->nickname??'';
|
||
$value->avatar = $author->avatar??'';
|
||
$value->isLker = false;
|
||
if($user)
|
||
$value->isLker = $user->hasLiked($value)?true:false;
|
||
|
||
}
|
||
return $result;
|
||
}
|
||
|
||
//获取课程心得-商户
|
||
public function BusinessGetCourseExperience(Request $request)
|
||
{
|
||
$user = $this->authCheck();
|
||
$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','content','photos','open_id','created_at','video_id')->withCount(['comments','BusinessComments'])
|
||
->where($type,$obj)
|
||
->where('status',1)
|
||
->where('is_show',1)
|
||
->orderby('id','desc')
|
||
->simplePaginate();
|
||
$result = $this->ComMentInfo($result,$user);
|
||
return $this->success('ok',$result);
|
||
}
|
||
|
||
//上传课程心得-商户
|
||
public function BusinessCourseExperience(Request $request)
|
||
{
|
||
$user = auth()->user();
|
||
$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(['user_id'=>$user->id,'course_id'=>$course_id,'video_id'=>$video_id],['content'=>$content,'photos'=>$photos,'status'=>1,'type'=>'business']);
|
||
|
||
return $this->success('ok','提交成功');
|
||
}
|
||
|
||
// 课程详情-商户
|
||
public function BusinessCourseDetail(Request $request)
|
||
{
|
||
try {
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if($wechatUser){
|
||
$openid = $wechatUser->getId();
|
||
}else{
|
||
$openid = $request->openid;
|
||
}
|
||
if (\Auth::guard('api')->check()) {
|
||
$user = \Auth::guard('api')->user();
|
||
}
|
||
if (\Auth::guard('api')->check()) {
|
||
$userId = \Auth::guard('api')->user()->id;
|
||
}
|
||
$id = $request->id;
|
||
$result = Course::withTrashed()->where('id',$id)->withCount('videos')->first();
|
||
//课程目录
|
||
// if(!$result)
|
||
// return $this->failure('该课程不存在或已经下架~');
|
||
//原价
|
||
$result->original_price = $result->charge;
|
||
//折扣价
|
||
$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;
|
||
}
|
||
//课程数量
|
||
$result->videos_count = $result->videos_count;
|
||
//结业状态
|
||
$result->graduation = false;
|
||
$paymentStatus = false;
|
||
$merchant_user = MerchantUser::where('openid',$openid)->first();
|
||
if(isset($user)){
|
||
$paymentStatus = UserCourses::where('course_id',$id)->where('user_id',$user->id)->where('status',1)->count()?true:false;
|
||
//空对象的布尔值是true,此处需要转化为数组
|
||
$video = json_decode($result->videos,true);
|
||
if($paymentStatus&&!empty($video)){
|
||
$result->videos[0]['can_watch']=1;
|
||
}
|
||
//查看心得数量
|
||
$comment_count = CourseComments::where('user_id',$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;
|
||
}
|
||
} elseif($merchant_user) {
|
||
$paymentStatus = UserCourses::where('course_id',$id)->Where('merchant_user_id', $merchant_user->id)->where('status',1)->count()?true:false;
|
||
//空对象的布尔值是true,此处需要转化为数组
|
||
$video = json_decode($result->videos,true);
|
||
if($paymentStatus&&!empty($video)){
|
||
$result->videos[0]['can_watch']=1;
|
||
}
|
||
//查看心得数量
|
||
$comment_count = CourseComments::Where('merchant_user_id', $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->paymentStatus = $paymentStatus;
|
||
//获取视频播放地址 getVodInfo
|
||
foreach ( $result->videos as $k => $value) {
|
||
$value->Viewing_status = 0;
|
||
$value->duration = ceil($value->duration/60);
|
||
if(isset($user)){
|
||
$key = $user->id.'course_record'.$id.'video_id'. $value->id;
|
||
if (Cache::has($key))
|
||
$value->Viewing_status = 1;
|
||
//如果已经有心得
|
||
$count = CourseComments::where('user_id',$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;
|
||
}
|
||
}
|
||
}
|
||
if(!$value->url){
|
||
$info = $this->getVodInfo($value->aliyun_video_id);
|
||
$value->url = $info['Mezzanine']['FileURL'];
|
||
}
|
||
|
||
}
|
||
|
||
//学员信息
|
||
// foreach ($result->BusinessUserCourse as $key => $value) {
|
||
//// $user = MerchantUser::where('id',$value->merchant_user_id)->first();
|
||
//// $result->BusinessUserCourse[$key]['nickname'] = $user['nickname']??'该用户已隐藏';
|
||
//// $result->BusinessUserCourse[$key]['avatar'] = $user['pic']??User::DefaultAvatar;
|
||
//// }
|
||
//学员信息
|
||
foreach ($result->UserCourse as $key => $value) {
|
||
$user = User::select('id','nickname','photo')->where('id',$value['user_id'])->first();
|
||
$result->UserCourse[$key]['nickname'] = $user['nickname'] ?? '该用户已隐藏';
|
||
$result->UserCourse[$key]['avatar'] = $user['photo'] ?? User::DefaultAvatar;
|
||
}
|
||
$result->business_user_course = $result->UserCourse;
|
||
unset($result->UserCourse);
|
||
$merchantUser = MerchantUser::where('openid',$openid)->first();
|
||
if(!$merchantUser){
|
||
$rand_str = $this->randString(8);
|
||
$merchantUser = new MerchantUser();
|
||
$merchantUser->openid = $openid;
|
||
$merchantUser->rand_str = $rand_str;
|
||
$merchantUser->pic = User::DefaultAvatar;
|
||
$merchantUser->nickname = '用户'.$rand_str;
|
||
$merchantUser->save();
|
||
}
|
||
$user = AccessRecord::where('open_id',$openid)->where('account_id',$result->merchant_id)->exists();
|
||
if(!$user){
|
||
$accessRecord = new AccessRecord();
|
||
$accessRecord->open_id = $openid;
|
||
$accessRecord->account_id = $result->merchant_id;
|
||
$accessRecord->channel = 1;
|
||
$accessRecord->last_time = now();
|
||
$accessRecord->save();
|
||
}
|
||
$result->user_count = UserCourses::where('course_id',$id)->where('status',1)->count();
|
||
$result->pv = Redis::zincrby('love_course_pv', 1 ,$id);
|
||
$jump_url = urlencode(env('APP_URL').'/h5/#/syncCourseDetail/'.$id);
|
||
$url = env('APP_URL').'/api/official/live/wechat/oauth?from_openid='.$openid.'&merchant_id='.$result->merchant_id.'&url='.$jump_url;
|
||
$qr_code = Redis::get('CourseDetail_L'.$id);
|
||
// if(!$qr_code){
|
||
$qr_code = $this->getPreviewQrcode($url);
|
||
// Redis::setex('CourseDetail_L'.$id,60*60*24*30,$qr_code);
|
||
// $qr_code = Redis::get('CourseDetail_L'.$id);
|
||
// }
|
||
$result->share_qr_code = $qr_code;
|
||
//下架或删除状态 0下架 1正常上架 2已删除
|
||
$publish_state = 0;
|
||
if($result->deleted_at){
|
||
$publish_state = 2;
|
||
}elseif($result->is_show == 1){
|
||
$publish_state = 1;
|
||
}else{
|
||
$publish_state = 0;
|
||
}
|
||
$result->publish_state = $publish_state;
|
||
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);
|
||
} catch (\Exception $e) {
|
||
return $this->failure('fail',['file' => $e->getFile(),'line' => $e->getLine()]);
|
||
}
|
||
}
|
||
|
||
//视频详情-商户
|
||
public function BusinessVideoDetail(Request $request)
|
||
{
|
||
if (\Auth::guard('api')->check()) {
|
||
$user = \Auth::guard('api')->user();
|
||
}
|
||
if(!isset($request->video_id)) {
|
||
if($request->type == 'video_id' && isset($request->id)) {
|
||
$video_id = $request->id;
|
||
} else {
|
||
return $this->fail('缺少参数,video_id没有上传');
|
||
}
|
||
} else {
|
||
$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(isset($user)){
|
||
$key = $user->id.'course_record'.$video->course_id.'video_id'. $video_id;
|
||
if (Cache::has($key))
|
||
$video->Viewing_status = 1;
|
||
//如果已经有心得
|
||
$count = CourseComments::where('user_id',$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);
|
||
}
|
||
|
||
//我的课程订单-商户
|
||
public function BusinessMycourse(Request $request)
|
||
{
|
||
$user_id = auth()->user()->id;
|
||
$result = UserCourses::where('user_id',$user_id)->where('status',1)->wherenotnull('trade_no')->simplePaginate();
|
||
foreach ($result as $key => $value) {
|
||
$value->Course ;
|
||
$value->video_count = CourseVideo::where('course_id',$value->Course->id)->count();
|
||
$value->learning = 0;
|
||
$comment = CourseComments::where('user_id',$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('user_id',$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 = '已学完';
|
||
}
|
||
return $this->success('ok', $result);
|
||
}
|
||
|
||
// 获取课程学员信息-商户
|
||
public function BusinessStudents(Request $request)
|
||
{
|
||
$course_id = $request->course_id??0;
|
||
//获取群课程
|
||
if(!$course_id) return $this->failure('课程信息有误');
|
||
$UserCourses = UserCourses::select('user_id')->where('course_id',$course_id)->where('status',1)->groupby('user_id')->get();
|
||
$data = [];
|
||
foreach ($UserCourses as $key => $value) {
|
||
if($value->user){
|
||
$user['id']= $value->user->id??0;
|
||
$user['nickname'] = $value->user->nickname??'匿名用户';
|
||
$user['avatar'] = $value->user->avatar??'';
|
||
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);
|
||
}
|
||
|
||
//记录播放记录-商户
|
||
public function BusinessAddRecord(Request $request)
|
||
{
|
||
$user_id = auth()->user()->id;
|
||
$courses_id = $request->courses_id;
|
||
$video_id = $request->video_id;
|
||
$key = $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', '记录成功');
|
||
}
|
||
|
||
//我的课程心得-商户
|
||
|
||
public function BusinessMyComments(Request $request)
|
||
{
|
||
$user = auth()->user();
|
||
|
||
$id = $request->id;
|
||
$type = $request->type;
|
||
$result = CourseComments::select('id','course_id','user_id','content','photos','created_at','video_id','open_id')->withCount(['comments','BusinessComments'])
|
||
->where('user_id',$user->id)
|
||
->where($type,$id)
|
||
->where('status',1)
|
||
->where('is_show',1)
|
||
->orderby('id','desc')
|
||
->simplePaginate();
|
||
$result = $this->ComMentInfo($result,$user);
|
||
return $this->success('ok',$result);
|
||
}
|
||
public function ComMentInfoV2($result)
|
||
{
|
||
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;
|
||
}
|
||
|
||
|
||
// 购买课程-商户
|
||
|
||
public function BusinessBuyingCourses(Request $request)
|
||
{
|
||
$user_id = auth()->user()->id;
|
||
$wechatUser = session('wechat.oauth_user.new');
|
||
if($wechatUser){
|
||
$openid = $wechatUser->getId();
|
||
}else{
|
||
$openid = $request->openid;
|
||
}
|
||
$merchant_user = MerchantUser::where('openid',$openid)->first();
|
||
$trade_no = \CommonUtilsService::getTradeNO();
|
||
$course_id = $request->course_id;
|
||
$cash = $request->cash;
|
||
$course = Course::where('id',$course_id)->first();
|
||
$anchor = Anchor::where('openid',$course->open_id)->first();
|
||
if(!$course)
|
||
return $this->failure('您购买的课程不存在或已下架~');
|
||
if($cash !=$course->charge&&!$course->discount_price)
|
||
return $this->failure('金额错误');
|
||
$result = UserCourses::where('user_id',$user_id)->where('course_id',$course_id)->where('status',1)->first();
|
||
if($result)
|
||
return $this->failure('您已经购买过该课程啦~');
|
||
$price = $course->charge;
|
||
$user = User::select('nickname','mobile','photo')->where('id',$user_id)->first();
|
||
$TouristOrder = new TouristOrder();
|
||
$TouristOrder->open_id = $openid;
|
||
$TouristOrder->price = floatval($price);
|
||
$TouristOrder->pay_type = 'wechat';
|
||
$TouristOrder->type = 'course';
|
||
$TouristOrder->type_id = $course_id;
|
||
$TouristOrder->trade_no = $trade_no;
|
||
$TouristOrder->desc = $course->title;
|
||
$TouristOrder->from_openid = $request->from_openid;
|
||
$TouristOrder->merchant_id = $course->merchant_id;
|
||
$TouristOrder->account_id = $merchant_user ? $merchant_user->id : 0;
|
||
$TouristOrder->share_channel_id = $request->share_channel_id;
|
||
$TouristOrder->name = $user->nickname??'匿名用户';
|
||
$TouristOrder->mobile = $user->mobile??'匿名用户';
|
||
$TouristOrder->withdrawal_radio = 100;
|
||
$TouristOrder->channel = 1;
|
||
$user_course = new UserCourses();
|
||
$user_course->user_id = $user_id;
|
||
$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 ? $merchant_user->id : 0;
|
||
try {
|
||
//开启事务
|
||
\DB::beginTransaction();
|
||
$TouristOrder->save();
|
||
$user_course->save();
|
||
\DB::commit();
|
||
|
||
} catch (\Exception $e) {
|
||
//回滚事务
|
||
\DB::rollback();
|
||
return $this->failure($e);
|
||
\Log::error($e->getMessage().';line-'.$e->getLine());
|
||
}
|
||
if ($price>0) {
|
||
|
||
$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,
|
||
);
|
||
$result = \WechatService::officialPay($attributes);
|
||
}else{
|
||
$way = 'merchant';
|
||
$TouristOrder->pay_status = 1;
|
||
$TouristOrder->save();
|
||
$user_course->status = 1;
|
||
$user_course->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);
|
||
}
|
||
//短信通知商家
|
||
$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'].' 查看详情';
|
||
Messenger::sendSMS($anchor->mobile, $message);
|
||
//增加记录
|
||
Message::create([
|
||
'phone' => $anchor->mobile,
|
||
'message' => $message,
|
||
'confirmed' => 1,
|
||
'code' => '购买通知',
|
||
'ip' => request() ? request()->ip() : '127.0.0.1',
|
||
]);
|
||
$content = $TouristOrder->name.'购买了【'.$course->title.'】查看订单详情>>';
|
||
SaasNotice::addRecord($TouristOrder->merchant_id,$TouristOrder->account_id,'order',$TouristOrder->id,$content,0);
|
||
return $this->success('ok',['status'=>1]);
|
||
}
|
||
return $this->success('ok', $result);
|
||
}
|
||
}
|