924 lines
33 KiB
PHP
924 lines
33 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Services\CommonUtilsService;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Course\Course;
|
|
use App\Models\User;
|
|
use EasyWeChat\Factory;
|
|
use App\Models\ProfileCourtship;
|
|
use App\Models\Coupon;
|
|
use App\Models\Course\CourseVideo;
|
|
use App\Models\Course\UserCourses;
|
|
use App\Models\App\Team;
|
|
use App\Models\Course\CourseComments;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use App\Contracts\UserContract;
|
|
use App\Models\UserCoupon;
|
|
use App\Models\Live\Viewer;
|
|
use App\Repositories\Eloquent\SmsRepository as Sms;
|
|
use SimpleSoftwareIO\QrCode\Facades\QrCode;
|
|
use EasyWeChat;
|
|
|
|
class CourseController extends Controller
|
|
{
|
|
protected $userCon;
|
|
protected $sms;
|
|
protected $viewer;
|
|
protected $app;
|
|
protected $config;
|
|
|
|
public function __construct(Sms $sms, UserContract $userCon)
|
|
{
|
|
$this->config = [
|
|
'app_id' => config('wechat.official_account.new.app_id'),
|
|
'secret' => config('wechat.official_account.new.secret'),
|
|
'token' => config('wechat.official_account.new.token'),
|
|
'aes_key' => config('wechat.official_account.new.aes_key')
|
|
];
|
|
|
|
$this->app = Factory::officialAccount($this->config);
|
|
$this->sms = $sms;
|
|
$this->userCon = $userCon;
|
|
}
|
|
|
|
|
|
//群列表
|
|
public function teams(Request $request){
|
|
try {
|
|
$keyword = $request->keyword;
|
|
$teams = Team::where('type','H5')->orderBy('sort','desc')->orderBy('id','desc');
|
|
if ($keyword) {
|
|
$keyword = trim($keyword);
|
|
$teams = $teams->where('id',$keyword)->orWhere('tname','like','%'.$keyword.'%');
|
|
}
|
|
$teams = $teams->paginate();
|
|
return $this->success('ok',$teams);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取列表失败');
|
|
}
|
|
|
|
}
|
|
|
|
public function teamsOrderByDrag(Request $request){
|
|
try {
|
|
$team_id = $request->team_id;
|
|
$other_team_id = $request->other_team_id;
|
|
$team = Team::find($team_id);
|
|
$other_team = Team::find($other_team_id);
|
|
if (empty($team)) {
|
|
return $this->failure('群聊id:'.$team_id.'不存在');
|
|
}
|
|
if (empty($other_team)) {
|
|
return $this->failure('群聊id:'.$other_team_id.'不存在');
|
|
}
|
|
$sort = $team->sort;
|
|
$other_sort = $other_team->sort;
|
|
|
|
|
|
if ($team->sort == $other_team->sort && $team->id < $other_team->id) {
|
|
$team->sort = $sort + 1;
|
|
}
|
|
elseif ($team->sort == $other_team->sort && $team->id > $other_team->id) {
|
|
$other_team->sort = $other_sort + 1;
|
|
// dd($other_live->sort);
|
|
}
|
|
elseif ($team->sort != $other_team->sort) {
|
|
$team->sort = $other_sort;
|
|
$other_team->sort = $sort;
|
|
}
|
|
|
|
$team->save();
|
|
$other_team->save();
|
|
return $this->success('ok',compact('team','other_team'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('修改失败');
|
|
}
|
|
|
|
}
|
|
|
|
//群课程列表
|
|
public function courseList(Request $request){
|
|
try {
|
|
$keyword = $request->keyword;
|
|
$team_id = $request->team_id;
|
|
|
|
$courses = Course::withCount(['UserCourse','videos'])->where('type','fullink')->where('team_id',$team_id)->orderBy('is_rec','desc')->orderBy('created_at','desc');
|
|
|
|
if ($keyword) {
|
|
$keyword = trim($keyword);
|
|
$courses = $courses->where('id',$keyword)->orWhere('title','like','%'.$keyword.'%')->orWhere('seo_keywords','like','%'.$keyword.'%')->orWhere('seo_description','like','%'.$keyword.'%');
|
|
}
|
|
$courses = $courses->paginate();
|
|
$teams = [];
|
|
$team_info = Team::where('type','H5')->where('tid',$team_id)->first();
|
|
$teams []= ['tid' => $team_info->id,'tname' => $team_info->tname,'intro' => $team_info->intro,'icon' => $team_info->icon,'logo' => $team_info->logo];
|
|
return $this->success('ok',compact('courses','teams'));
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取列表失败');
|
|
}
|
|
}
|
|
|
|
|
|
//所有课程列表
|
|
public function coursesList(Request $request){
|
|
try {
|
|
$keyword = $request->keyword;
|
|
$team_id = $request->team_id;
|
|
$courses = Course::withCount(['UserCourse','videos'])->where('type','fullink')->orderBy('is_rec','desc')->orderBy('created_at','desc');
|
|
|
|
if ($keyword) {
|
|
$keyword = trim($keyword);
|
|
$courses = $courses->where('id',$keyword)->orWhere('title','like','%'.$keyword.'%')->orWhere('seo_keywords','like','%'.$keyword.'%')->orWhere('seo_description','like','%'.$keyword.'%');
|
|
}
|
|
$courses = $courses->get();
|
|
|
|
return $this->success('ok',$courses);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取列表失败');
|
|
}
|
|
}
|
|
|
|
//课程详情
|
|
public function course(Request $request ,$course_id){
|
|
try {
|
|
$course = Course::find($course_id);
|
|
if (empty($course)) return $this->failure('课程不存在');
|
|
return $this->success('ok',$course);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取详情失败');
|
|
}
|
|
|
|
}
|
|
|
|
//课程成员
|
|
public function courseMembers(Request $request ,$course_id){
|
|
try {
|
|
$keyword = $request->keyword;
|
|
$is_new = $request->is_new;
|
|
$courses= UserCourses::with('user')->whereHas('user')->where('course_id',$course_id)->where('status',1)->orderBy('created_at','desc');
|
|
|
|
$videos = CourseVideo::where('course_id',$course_id)->get();
|
|
|
|
if ($is_new && $is_new == 1) {
|
|
$courses = $courses->whereNotNull('trade_no');
|
|
}
|
|
if ($is_new && $is_new == -1) {
|
|
$courses = $courses->whereNull('trade_no');
|
|
}
|
|
|
|
if ($keyword) {
|
|
$keyword = trim($keyword);
|
|
$courses = $courses->whereHas('user',function($query) use($keyword){
|
|
$query->where('id',$keyword)->orWhere('nickname','like','%'.$keyword.'%');
|
|
});
|
|
}
|
|
$courses = $courses->paginate();
|
|
|
|
//当前课时总课时数
|
|
$total = CourseVideo::where('course_id',$course_id)->count();
|
|
|
|
$array = CourseVideo::where('course_id',$course_id)->orderBy('id','asc')->get()->toArray();
|
|
//$total = 15 array下标 0~14 push
|
|
foreach ($courses as $course) {
|
|
//交作业次数
|
|
$push = CourseComments::where('course_id',$course_id)->where('user_id',$course->user->id)->where('status',1)->count();
|
|
if (($total-$push >= 2)) {
|
|
//当前正在读的课时
|
|
$current_title = $array[$push]['title'];
|
|
$current_video_id = $array[$push]['id'];
|
|
//下一个课时
|
|
$next_title = $array[$push+1]['title'];
|
|
$next_video_id = $array[$push+1]['id'];
|
|
}
|
|
if($total-$push == 1){
|
|
$current_title = $array[$push]['title'];
|
|
$current_video_id = $array[$push]['id'];
|
|
//下一个课时
|
|
$next_title = '已完结本课程';
|
|
$next_video_id = $current_video_id;
|
|
}
|
|
if($total-$push == 0){
|
|
$current_title = '已完结本课程';
|
|
$current_video_id = 0;
|
|
//下一个课时
|
|
$next_title = '已完结本课程';
|
|
$next_video_id = 0;
|
|
}
|
|
|
|
$course->user->current_title = $current_title;
|
|
$course->user->current_video_id = $current_video_id;
|
|
$course->user->next_title = $next_title;
|
|
$course->user->next_video_id = $next_video_id;
|
|
}
|
|
|
|
return $this->success('ok',$courses);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取列表失败');
|
|
}
|
|
}
|
|
|
|
//新建课程
|
|
public function createCourse(Request $request){
|
|
try {
|
|
$course = new Course();
|
|
if ($request->aliyun_video_id) {
|
|
$result = \AliyunService::getPlayInfo($request->aliyun_video_id);
|
|
if (empty($result)) throw new \Exception("获取播放地址失败", 1);
|
|
$course->publicity_video = $result['PlayInfoList']['PlayInfo'][0]['PlayURL'];
|
|
}
|
|
|
|
if (empty($request->team_id)) return $this->failure('选择所属群');
|
|
$course->team_id = $request->team_id;
|
|
|
|
|
|
if (empty($request->title)) return $this->failure('请输入课程标题');
|
|
$course->title = $request->title;
|
|
|
|
$course->slug = $request->slug;
|
|
|
|
|
|
if (empty($request->teacher_name)) return $this->failure('请输入老师名字');
|
|
$course->teacher_name = $request->teacher_name;
|
|
|
|
if (empty($request->teacher_id)) return $this->failure('老师id不能为空');
|
|
$course->user_id = $request->teacher_id;
|
|
|
|
|
|
if (empty($request->thumb)) return $this->failure('请选择课程封面');
|
|
$course->thumb = $request->thumb;
|
|
|
|
if (empty($request->charge)) return $this->failure('请输入课程价格');
|
|
$course->charge = $request->charge;
|
|
|
|
$course->short_description = $request->short_description ? $request->short_description :'';
|
|
|
|
$course->original_desc = $request->original_desc ? $request->original_desc : '';
|
|
|
|
$course->render_desc = $request->render_desc ? $request->render_desc : '';
|
|
$course->seo_keywords = $request->seo_keywords ? $request->seo_keywords : '';
|
|
$course->seo_description = $request->seo_description ? $request->seo_description : '';
|
|
|
|
$course->discount_price = $request->discount_price;
|
|
$course->start_time_discount = $request->start_time_discount;
|
|
$course->end_time_discount = $request->end_time_discount;
|
|
$now_date = date('Y-m-d H:i:s');
|
|
if ($request->end_time_discount) {
|
|
if ($course->end_time_discount <= $now_date) {
|
|
return $this->failure('优惠结束时间应大于当前时间');
|
|
}
|
|
}
|
|
|
|
if ($request->charge && $request->discount_price){
|
|
if ($request->discount_price >= $request->charge) {
|
|
return $this->failure('课程优惠价格应小于原价');
|
|
}
|
|
}
|
|
if($request->has('lock')){
|
|
$course->lock = $request->lock;
|
|
}
|
|
|
|
if (empty($request->published_at)) return $this->failure('请选择课程上线时间');
|
|
$course->published_at = $request->published_at;
|
|
|
|
$course->is_show = $request->is_show ? $request->is_show : 1;
|
|
$course->is_rec = $request->is_rec ? $request->is_rec : 0;
|
|
$course->type = 'fullink';
|
|
$course->save();
|
|
return $this->success('ok',$course);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('添加失败');
|
|
}
|
|
}
|
|
|
|
//修改课程信息
|
|
public function updateCourse(Request $request ,$course_id){
|
|
try {
|
|
$course = Course::find($course_id);
|
|
$now_date = date('Y-m-d H:i:s');
|
|
if (empty($course)) return $this->failure('课程不存在');
|
|
|
|
if ($request->title && $course->title != $request->title) {
|
|
$course->title = $request->title;
|
|
}
|
|
if ($request->start_time_discount && $course->start_time_discount != $request->start_time_discount) {
|
|
$course->start_time_discount = $request->start_time_discount;
|
|
}
|
|
if ($request->end_time_discount && $course->end_time_discount != $request->end_time_discount) {
|
|
if ($request->end_time_discount <= $now_date) {
|
|
return $this->failure('优惠结束时间应大于当前时间');
|
|
}
|
|
$course->end_time_discount = $request->end_time_discount;
|
|
}
|
|
if ($request->discount_price && $course->discount_price != $request->discount_price) {
|
|
if ($request->discount_price >= $course->charge) {
|
|
return $this->failure('优惠价格应小于原价');
|
|
}
|
|
$course->discount_price = $request->discount_price;
|
|
}
|
|
|
|
if ($request->team_id && $course->team_id != $request->team_id) {
|
|
$course->team_id = $request->team_id;
|
|
}
|
|
|
|
if ($request->slug && $course->slug != $request->slug) {
|
|
$course->slug = $request->slug;
|
|
}
|
|
if ($request->thumb && $course->thumb != $request->thumb) {
|
|
$course->thumb = $request->thumb;
|
|
}
|
|
if (is_numeric($request->charge) && $course->charge != $request->charge) {
|
|
$course->charge = $request->charge;
|
|
}
|
|
if ($request->short_description && $course->short_description != $request->short_description) {
|
|
$course->short_description = $request->short_description;
|
|
}
|
|
if ($request->original_desc && $course->original_desc != $request->original_desc) {
|
|
$course->original_desc = $request->original_desc;
|
|
}
|
|
if ($request->teacher_name && $course->teacher_name != $request->teacher_name) {
|
|
$course->teacher_name = $request->teacher_name;
|
|
}
|
|
|
|
if ($request->teacher_id && $course->user_id != $request->teacher_id) {
|
|
$course->user_id = $request->teacher_id;
|
|
}
|
|
|
|
if ($request->render_desc && $course->render_desc != $request->render_desc) {
|
|
$course->render_desc = $request->render_desc;
|
|
}
|
|
if ($request->seo_keywords && $course->seo_keywords != $request->seo_keywords) {
|
|
$course->seo_keywords = $request->seo_keywords;
|
|
}
|
|
if ($request->seo_description && $course->seo_description != $request->seo_description) {
|
|
$course->seo_description = $request->seo_description;
|
|
}
|
|
if ($request->published_at && $course->render_desc != $request->published_at) {
|
|
$course->published_at = $request->published_at;
|
|
}
|
|
if ($request->is_show && $course->is_show != $request->is_show) {
|
|
$course->is_show = $request->is_show;
|
|
}
|
|
if (is_numeric($request->category_id) && $course->category_id != $request->category_id) {
|
|
$course->category_id = $request->category_id;
|
|
}
|
|
if ($request->seo_description && $course->seo_description != $request->seo_description) {
|
|
$course->seo_description = $request->seo_description;
|
|
}
|
|
if (is_numeric($request->is_rec) && $course->is_rec != $request->is_rec) {
|
|
$course->is_rec = $request->is_rec;
|
|
}
|
|
if($request->aliyun_video_id){ //宣传视频
|
|
$result = \AliyunService::getPlayInfo($request->aliyun_video_id);
|
|
if (empty($result)) throw new \Exception("获取播放地址失败", 1);
|
|
$play_url = $result['PlayInfoList']['PlayInfo'][0]['PlayURL'];
|
|
$course->publicity_video = $play_url;
|
|
}else{
|
|
$course->publicity_video = null;
|
|
}
|
|
if($request->has('lock')){
|
|
$course->lock = $request->lock;
|
|
}
|
|
|
|
$course->save();
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('修改失败');
|
|
}
|
|
}
|
|
|
|
//删除课程
|
|
public function removeCourse(Request $request ,$course_id){
|
|
try {
|
|
Course::where('id',$course_id)->delete();
|
|
UserCourses::where('course_id',$course_id)->delete();
|
|
CourseComments::where('course_id',$course_id)->delete();
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('删除失败');
|
|
}
|
|
|
|
}
|
|
|
|
//增加课程成员
|
|
public function addCoursesMembers(Request $request){
|
|
try {
|
|
$course_id = $request->course_id;
|
|
|
|
$user_id = $request->user_id;
|
|
if (empty($course_id)) return $this->failure('请选择课程');
|
|
if (empty($user_id)) return $this->failure('请选择成员');
|
|
|
|
$user_course = UserCourses::where('user_id',$user_id)->where('course_id',$course_id)->where('status',1)->first();
|
|
if (!empty($user_course)) return $this->failure('该用户已在此课程');
|
|
|
|
UserCourses::create([
|
|
'user_id' => $user_id,
|
|
'course_id' => $course_id,
|
|
'status' => 1,
|
|
]);
|
|
$course = Course::find($course_id);
|
|
$course->user_count = $course->user_count+1;
|
|
$course->save();
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('添加失败');
|
|
}
|
|
|
|
}
|
|
|
|
//移除课程成员
|
|
public function removeMember(Request $request){
|
|
try {
|
|
$course_id = $request->course_id;
|
|
$user_id = $request->user_id;
|
|
|
|
$video_ids = CourseVideo::where('course_id',$course_id)->pluck('id')->toArray();
|
|
|
|
if (empty($course_id)) return $this->failure('请选择课程');
|
|
if (empty($user_id)) return $this->failure('请选择成员');
|
|
|
|
if (!empty($video_ids)) {
|
|
foreach ($video_ids as $video_id) {
|
|
$key = $user_id.'course_record'.$course_id.'video_id'. $video_id;
|
|
Cache::forget($key);
|
|
}
|
|
}
|
|
|
|
UserCourses::where('course_id',$course_id)->where('user_id',$user_id)->delete();
|
|
CourseComments::where('course_id',$course_id)->where('user_id',$user_id)->delete();
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('移除失败');
|
|
}
|
|
}
|
|
|
|
|
|
//课程目录
|
|
public function courseChapters(Request $request ,$course_id){
|
|
try {
|
|
$keyword = $request->keyword;
|
|
|
|
$videos = CourseVideo::where('course_id',$course_id)->orderBy('sort','desc')->orderBy('id','asc');
|
|
if ($keyword) {
|
|
$keyword = trim($keyword);
|
|
$videos = $videos->where('title','like','%'.$keyword.'%')->orWhere('seo_keywords','like','%'.$keyword.'%');
|
|
}
|
|
$videos = $videos->paginate();
|
|
|
|
return $this->success('ok',$videos);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取列表失败');
|
|
}
|
|
}
|
|
|
|
public function coursesChapters(Request $request ,$course_id){
|
|
try {
|
|
$titles = CourseVideo::where('course_id',$course_id)->select('id','title')->get();
|
|
|
|
return $this->success('ok',$titles);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取列表失败');
|
|
}
|
|
|
|
}
|
|
|
|
//创建课程目录(含视频)
|
|
public function addCourseChapter(Request $request ,$course_id){
|
|
try {
|
|
$request->validate([
|
|
'aliyun_video_id' => 'required',
|
|
'title' => 'required',
|
|
]);
|
|
$result = \AliyunService::getPlayInfo($request->aliyun_video_id);
|
|
if (empty($result)) throw new \Exception("获取播放地址失败", 1);
|
|
$course_video = new CourseVideo();
|
|
$course_video->course_id = $course_id;
|
|
$course_video->aliyun_video_id = $request->aliyun_video_id;
|
|
$course_video->title = $request->title;
|
|
$thumb = $request->thumb;
|
|
$course_video->thumb = $request->thumb;
|
|
$course_video->view_num = 0;
|
|
$course_video->is_show = 1;
|
|
$course_video->url = $result['PlayInfoList']['PlayInfo'][0]['PlayURL'];
|
|
$course_video->duration = round($result['VideoBase']['Duration']);
|
|
$course_video->save();
|
|
return $this->success('ok',$course_video);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('创建失败');
|
|
}
|
|
|
|
}
|
|
|
|
//修改课程视频
|
|
public function updateVideo(Request $request ,$video_id){
|
|
try {
|
|
$video = CourseVideo::find($video_id);
|
|
$sort = $request->input('sort',0);
|
|
if (empty($video)) return $this->failure('视频不存在');
|
|
|
|
if ($request->aliyun_video_id && $video->aliyun_video_id != $request->aliyun_video_id) {
|
|
$result = \AliyunService::getPlayInfo($request->aliyun_video_id);
|
|
// dd($result);
|
|
if (empty($result)) throw new \Exception("获取播放地址失败", 1);
|
|
$video->aliyun_video_id = $request->aliyun_video_id;
|
|
$video->thumb = $request->thumb;
|
|
$video->view_num = 0;
|
|
$video->url = $result['PlayInfoList']['PlayInfo'][0]['PlayURL'];
|
|
$video->duration = round($result['VideoBase']['Duration']);
|
|
CourseComments::where('video_id',$video_id)->delete();
|
|
}
|
|
if ($request->title && $video->title != $request->title) {
|
|
$video->title = $request->title;
|
|
}
|
|
if($request->has('sort') && $request->sort != $video->sort){
|
|
$video->sort = $request->sort;
|
|
}
|
|
$video->save();
|
|
return $this->success('ok',$video);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('修改失败');
|
|
}
|
|
|
|
|
|
}
|
|
|
|
//移除课程目录
|
|
public function remCourseChapter(Request $request){
|
|
try {
|
|
$id = $request->id;
|
|
if (empty($id)) return $this->failure('请选择要删除的视频');
|
|
CourseVideo::where('id',$id)->delete();
|
|
CourseComments::where('video_id',$id)->delete();
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('删除失败');
|
|
}
|
|
|
|
}
|
|
|
|
//课程心得
|
|
public function courseComments(Request $request ,$course_id){
|
|
try {
|
|
$keyword = $request->keyword;
|
|
$title = $request->title;
|
|
$titles = [];
|
|
$comments = CourseComments::with('user','video')->withCount('comments')->where('status',1)->where('course_id',$course_id)->orderBy('is_show','desc')->orderBy('created_at','desc');
|
|
|
|
$titles = CourseVideo::where('course_id',$course_id)->pluck('title')->toArray();
|
|
|
|
if ($title) {
|
|
$video_id = CourseVideo::where('title',$title)->value('id');
|
|
$comments = $comments->where('video_id',$video_id);
|
|
}
|
|
|
|
if ($keyword) {
|
|
$keyword = trim($keyword);
|
|
$comments = $comments->whereHas('user',function($query) use($keyword){
|
|
$query->where('id',$keyword)
|
|
->orWhere('nickname','like','%'.$keyword.'%');
|
|
});
|
|
}
|
|
$comments = $comments->paginate();
|
|
|
|
foreach ($comments as $comment) {
|
|
$comment->likerCount = $comment->likers->count();
|
|
$comment->photos = json_decode($comment->photos, true);
|
|
unset($comment->likers);
|
|
}
|
|
|
|
return $this->success('ok',compact('titles','comments'));
|
|
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取列表失败');
|
|
}
|
|
}
|
|
|
|
//删除课程心得
|
|
public function delCourseComments(Request $request){
|
|
try {
|
|
|
|
$user_id = $request->user_id;
|
|
$course_id = $request->course_id;
|
|
$video_id = $request->video_id;
|
|
|
|
$comment_ids = $request->comment_id;
|
|
|
|
if (empty($comment_ids)) return $this->failure('请选择要删除的评论');
|
|
|
|
$key = $user_id.'course_record'.$course_id.'video_id'. $video_id;
|
|
Cache::forget($key);
|
|
|
|
CourseComments::whereIn('id',$comment_ids)->delete();
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('删除失败');
|
|
}
|
|
}
|
|
|
|
//隐藏课程心得
|
|
public function hiddenComment(Request $request){
|
|
try {
|
|
$comment_id = $request->comment_id;
|
|
|
|
$is_show = $request->is_show ? $request->is_show : 0;
|
|
if (empty($comment_id)) return $this->failure('请选择隐藏的id');
|
|
|
|
CourseComments::where('id',$comment_id)->update(['is_show' => $is_show]);
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('隐藏失败');
|
|
}
|
|
|
|
}
|
|
|
|
//解锁用户课时
|
|
public function unlockChapter(Request $request){
|
|
try {
|
|
$course_id = $request->course_id;
|
|
$user_id = $request->user_id;
|
|
$video_id = $request->video_id;
|
|
if (empty($video_id)) return $this->failure('选择要解锁的课时');
|
|
|
|
$ids = CourseVideo::where('course_id',$course_id)->pluck('id')->toArray();
|
|
|
|
$arrs = array();
|
|
foreach($ids as $id){
|
|
if($id<$video_id) $arrs[] = $id;
|
|
}
|
|
foreach ($arrs as $arr) {
|
|
CourseComments::updateOrCreate([
|
|
'user_id'=>$user_id,
|
|
'course_id'=>$course_id,
|
|
'video_id'=>$arr,
|
|
'is_show'=>0,
|
|
'status'=>1,
|
|
]);
|
|
}
|
|
|
|
return $this->success('ok');
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('解锁失败');
|
|
}
|
|
|
|
}
|
|
|
|
//新建课程优惠券
|
|
public function createCourseCoupons(Request $request,$course_id){
|
|
try {
|
|
$admin_id = auth()->id();
|
|
$course = Course::find($course_id);
|
|
if (empty($course)) return $this->failure('课程不存在');
|
|
|
|
$coupon = new Coupon();
|
|
$coupon->type = 'course';
|
|
$coupon->admin_id = $admin_id;
|
|
$coupon->type_id = $course_id;
|
|
$coupon->name = $request->name;
|
|
$coupon->desc = $request->desc;
|
|
if ($request->payment_method == 'free') {//优惠方式 free discount
|
|
$coupon->payment_method = $request->payment_method;
|
|
$coupon->preferential_amount = $course->charge;
|
|
}else {
|
|
if(is_numeric($request->preferential_amount) && $request->preferential_amount > $course->charge){
|
|
return $this->failure('优惠金额必须小于课程金额');
|
|
}
|
|
$coupon->payment_method = 'discount';
|
|
$coupon->preferential_amount = $request->preferential_amount; //优惠金额
|
|
}
|
|
|
|
$coupon->expiration_time = $request->expiration_time ? $request->expiration_time : null;
|
|
|
|
$coupon->share_user_id = $request->share_user_id;
|
|
// $coupon->total_num = $request->total_num > 0 ? $request->total_num : 0;
|
|
if (empty($request->total_num)) return $this->failure('请输入优惠券数量');
|
|
$coupon->total_num = $request->total_num;
|
|
$coupon->remain_num = $request->total_num;
|
|
$coupon->status = 1;
|
|
$coupon->save();
|
|
|
|
if ($request->payment_method == 'free'){
|
|
$charge = '免费';
|
|
}else{
|
|
$charge = '¥'.$request->preferential_amount;
|
|
}
|
|
|
|
$title = $course->title;
|
|
if (empty($request->expiration_time)) {
|
|
$expiration_time = '永久有效';
|
|
}else {
|
|
$expiration_time = $request->expiration_time;
|
|
}
|
|
|
|
//生成海报
|
|
//底图
|
|
$image1 = 'https://local-pictures.oss-cn-shenzhen.aliyuncs.com/202106/22/a7db5455ec3697802e4557522f95aeba.png';
|
|
//内图
|
|
$image2 = 'https://local-pictures.oss-cn-shenzhen.aliyuncs.com/202106/22/f759582240f7e1afa18c5772f85cf9a1.png';
|
|
//w, h, (左/右),(上/下)对应(top/bottom-left/right)
|
|
$new_image = $this->imageAddImage($image1, $image2, (375 - 40)*2, 526*2, 20*2, 90*2, 'top-left');
|
|
//优惠价格
|
|
|
|
//$image, $text, $text_size, $offset_x, $offset_y, $color='#000000', $align = 'center', $valign='top'
|
|
$new_image = $this->textAddImage($new_image,$charge,50*2,185*2,210*2,'#FFFFFF','center','top');
|
|
//课程标题
|
|
$title = '仅适用:【'.$coupon->desc.'】课程';
|
|
// dd(mb_substr($title,0,15));
|
|
$new_image = $this->textAddImage($new_image,mb_substr($title,0,18),16*2,185*2,290*2,'#FFFFFF','center','top');
|
|
if (strlen($title) > 18) {
|
|
$new_image = $this->textAddImage($new_image,mb_substr($title,18,strlen($title)),16*2,185*2,320*2,'#FFFFFF','center','top');
|
|
}
|
|
//有效期限
|
|
|
|
// $expiration_time = '有效期:永久有效';
|
|
$expiration_time = $request->expiration_time ? '有效期至:'.$request->expiration_time : '永久有效';
|
|
|
|
$new_image = $this->textAddImage($new_image,$expiration_time,16*2,180*2,360*2,'#FFFFFF','center','top');
|
|
|
|
$pic_path = storage_path('qrcode/'.time().'qrcode_coupon.png');
|
|
if(empty($coupon->qrcode)){
|
|
// $url = env('APP_URL').'/h5/#/receiveAcoupon?title='.$course->title.'&expiration_time='.$expiration_time.'&charge='.$charge;
|
|
$url = env('APP_URL').'/h5/#/receiveAcoupon?coupon_id='.$coupon->id;
|
|
|
|
//二维码图片
|
|
$qr_code2 = QrCode::size(2100)->format('png')->encoding('UTF-8')->generate($url,$pic_path);
|
|
$pic2 = $this->uploadFile($pic_path);
|
|
|
|
}
|
|
$path = time().'qrcode_coupon.png';
|
|
$qrcode_path = storage_path("qrcode/".$path);
|
|
$qrcode = $qrcode_path;
|
|
|
|
$new_image = $this->imageAddImage($new_image, $pic2, 216, 216, 450, 150, 'bottom-left');
|
|
|
|
|
|
$file_path = storage_path("qrcode/".time()."course_coupon.png");
|
|
//左上图标
|
|
$icon = 'https://local-pictures.oss-cn-shenzhen.aliyuncs.com/202106/22/41d0c99485dd9a96e7ad2331d3b207ff.png';
|
|
$new_image = $this->imageAddImage($new_image, $icon, 77*2, 28*2, 12*2, 30*2, 'top-left');
|
|
|
|
$share_user_id = $request->share_user_id;
|
|
if (!empty($share_user_id)){
|
|
$user = User::find($share_user_id);
|
|
if (!empty($user)) {
|
|
$name = $user->nickname;
|
|
$openid = $user->wechat->openid;
|
|
if ($user->circle_avatar) {
|
|
$avatar = $user->circle_avatar;
|
|
}else{
|
|
$avatar = $user->wechat->avatar2?:$user->wechat->avatar;
|
|
// $avatar = $this->circleImage($avatar, $user->id);
|
|
}
|
|
if ($user->type == 'single') {
|
|
$profile = ProfileCourtship::where('user_id', $user->id)->first();
|
|
if (empty($profile)) {
|
|
$is_complete = 0;
|
|
}else{
|
|
$is_complete = ($this->userCon->isCompleteProfileV3($user))?1:0;
|
|
}
|
|
}
|
|
}else{
|
|
$name = $request->input('name');
|
|
$avatar = $request->input('avatar');
|
|
$is_complete = 0;
|
|
}
|
|
//头像 名字
|
|
$new_image = $this->imageAddImage($new_image, $avatar, 28*2, 25*2, 100*2, 32*2, 'top-left');
|
|
$new_image = $this->textAddImage($new_image,$name,16*2,129*2,50*2,'#FFFFFF','center','left');
|
|
}
|
|
|
|
$new_image->save($file_path);
|
|
$pic = '';
|
|
if(file_exists($file_path)){
|
|
$pic = $this->uploadFile($file_path);
|
|
try{
|
|
if ($pic_path) {
|
|
unlink($pic_path);
|
|
}
|
|
unlink($file_path);
|
|
}catch(Exception $e) {
|
|
return $this->failure($e->getMessage());
|
|
}
|
|
}
|
|
$coupon->qrcode = $pic;
|
|
|
|
$coupon->save();
|
|
return $this->success('ok',$coupon);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('生成海报失败');
|
|
}
|
|
|
|
}
|
|
|
|
//优惠券领取记录列表
|
|
public function couponHistories(Request $request,$course_id){
|
|
try {
|
|
$keyword = $request->keyword;
|
|
$histories = Coupon::with('userCoupon','admin:id,name,nickname,app_avatar,circle_avatar','shareUser:id,name,nickname,app_avatar,circle_avatar','userCoupon.user:id,name,nickname,app_avatar,circle_avatar')->where('type','course')->where('type_id',$course_id);
|
|
if ($keyword) {
|
|
$keyword = trim($keyword);
|
|
$histories = $histories->whereHas('admin',function($sql) use($keyword){
|
|
$sql->where('id',$keyword)->orWhere('nickname','like','%'.$keyword.'%');
|
|
})->orWhereHas('shareUser',function($sql) use($keyword){
|
|
$sql->where('id',$keyword)->orWhere('nickname','like','%'.$keyword.'%');
|
|
});
|
|
}
|
|
$histories = $histories->paginate();
|
|
return $this->success('ok',$histories);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('获取记录列表失败');
|
|
}
|
|
|
|
}
|
|
//课程老师名字列表
|
|
public function teacherNames(){ //暂时写死
|
|
$teacher_names = ['袁大同'];
|
|
return $this->success('ok',$teacher_names);
|
|
}
|
|
|
|
//清空优惠券测试数据
|
|
public function clearCouponAndUserCoupon(){
|
|
$coupon_ids = Coupon::orderBy('id','asc')->pluck('id')->toArray();
|
|
|
|
$user_coupon_ids = UserCoupon::orderBy('id','asc')->pluck('id')->toArray();
|
|
Coupon::whereIn('id',$coupon_ids)->delete();
|
|
UserCoupon::whereIn('id',$user_coupon_ids)->delete();
|
|
return $this->success('ok');
|
|
}
|
|
|
|
//
|
|
public function changeTeacherId(Request $request,$viewer_id){
|
|
$user_id = $request->user_id;
|
|
$viewer = Viewer::where('id',$viewer_id)->update(['user_id'=>$user_id]);
|
|
return $this->success('ok',$viewer);
|
|
}
|
|
|
|
|
|
//解密阿里云视频id
|
|
public function decryptAliyunVideoId(Request $request){
|
|
try {
|
|
$videos = CourseVideo::where('course_id',1)->get();
|
|
$url = [];
|
|
foreach ($videos as $video) {
|
|
$result = \AliyunService::getPlayInfo($video->aliyun_video_id);
|
|
if (empty($result)) throw new \Exception("获取播放地址失败", 1);
|
|
$url[] = $result['PlayInfoList']['PlayInfo'][0]['PlayURL'];
|
|
}
|
|
return $this->success('ok',$url);
|
|
} catch (\Exception $e) {
|
|
$this->getError($e);
|
|
return $this->failure('创建失败');
|
|
}
|
|
}
|
|
|
|
public function updateViewerid(Request $request){
|
|
$viewer_id = $request->viewer_id;
|
|
$user_id = $request->user_id;
|
|
$viewer = Viewer::find($viewer_id);
|
|
if (empty($viewer)) return $this->failure('该直播用户不存在');
|
|
$viewer->user_id = $user_id;
|
|
$viewer->save();
|
|
return $this->success('ok');
|
|
}
|
|
|
|
//课程url http->https
|
|
public function changeVideoUrl(Request $request){
|
|
$videos = CourseVideo::pluck('url')->toArray();
|
|
foreach ($videos as $key => $video) {
|
|
if($video == ''){
|
|
continue;
|
|
}elseif(strstr($video,'https') ){
|
|
continue;
|
|
}else{
|
|
$url = str_replace('http','https',$video);
|
|
CourseVideo::where('url',$video)->update(['url'=>$url]);
|
|
}
|
|
}
|
|
return $this->success('ok');
|
|
}
|
|
}
|