You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

600 lines
23 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* 课程
*/
namespace App\Http\Controllers\Mobile;
use App\Helpers\ResponseCode;
use App\Models\AccompanyOrder;
use App\Models\AppointmentTotalLog;
use App\Models\Config;
use App\Models\Course;
use App\Models\CourseAppointmentTotal;
use App\Models\CourseContent;
use App\Models\CourseSign;
use App\Models\Notice;
use App\Models\Order;
use App\Models\User;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator;
class CourseController extends CommonController
{
/**
* @OA\Get(
* path="/api/mobile/course/notices",
* tags={"小程序-课程"},
* summary="通知公告",
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function notices()
{
$all = request()->all();
$list = Notice::with('image')->where(function ($query) use ($all) {
})->orderBy('id')->paginate($all['page_size'] ?? 20);
return $this->success($list);
}
/**
* @OA\Get(
* path="/api/mobile/course/course",
* tags={"小程序-课程"},
* summary="课程列表",
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
* @OA\Parameter(name="sort_name", in="query", @OA\Schema(type="string"), required=false, description="排序字段名字"),
* @OA\Parameter(name="sort_type", in="query", @OA\Schema(type="string"), required=false, description="排序类型"),
* @OA\Parameter(name="type", in="query", @OA\Schema(type="integer"), description="课程类型"),
* @OA\Parameter(name="status", in="query", @OA\Schema(type="integer"), description="状态"),
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function course()
{
$all = request()->all();
$list = Course::with('image', 'typeDetail')->withCount('courseSigns')->where(function ($query) use ($all) {
if (isset($all['type'])) {
$query->where('type', $all['type']);
}
if (isset($all['status'])) {
$query->where('status', $all['status']);
}
})->whereIn('sign_status', [10, 40])
->where('is_virtual', 0)
->orderBy('sign_status', 'asc')
->orderBy('start_date', 'desc')
->paginate($all['page_size'] ?? 20);
return $this->success($list);
}
/**
* @OA\Get(
* path="/api/mobile/course/course-detail",
* tags={"小程序-课程"},
* summary="课程详情",
* @OA\Parameter(name="course_id", in="query", @OA\Schema(type="string"), required=false, description="课程id"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function courseDetail()
{
$all = \request()->all();
$messages = [
'course_id.required' => '课程id必填',
];
$validator = Validator::make($all, [
'course_id' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$detail = Course::with('qunImage', 'typeDetail', 'courseForms', 'teacher')
->withCount(['courseSigns as my_user' => function ($query) {
$query->where('user_id', $this->getUserId());
}])->find($all['course_id']);
return $this->success($detail);
}
/**
* @OA\Get(
* path="/api/mobile/course/course-detail-pc",
* tags={"小程序-课程"},
* summary="课程详情PC",
* @OA\Parameter(name="course_id", in="query", @OA\Schema(type="string"), required=false, description="课程id"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function courseDetailPc()
{
$all = \request()->all();
$messages = [
'course_id.required' => '课程id必填',
];
$validator = Validator::make($all, [
'course_id' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$detail = Course::with('typeDetail', 'courseForms', 'teacher')->find($all['course_id']);
return $this->success($detail);
}
/**
* @OA\Post(
* path="/api/mobile/course/sign",
* tags={"小程序-课程"},
* summary="报名",
* @OA\Parameter(name="course_id", in="query", @OA\Schema(type="string"), required=false, description="课程id"),
* @OA\Parameter(name="is_change", in="query", @OA\Schema(type="string"), required=false, description="用户数据是否变化0否1是"),
* @OA\Parameter(name="change_data", in="query", @OA\Schema(type="string"), required=false, description="变化的数组,键值对形式的一维数组"),
* @OA\Parameter(name="data", in="query", @OA\Schema(type="string"), required=false, description="自定义数据的数据二维数组。包括键名namefieldvalue"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function sign()
{
$all = \request()->all();
$messages = [
'course_id.required' => '课程id必填',
];
$validator = Validator::make($all, [
'course_id' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$sign = CourseSign::where('course_id', $all['course_id'])
->where('user_id', $this->getUserId())
->first();
if ($sign) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '请勿重复报名']);
}
$course = Course::find($all['course_id']);
// 报名时间判断
if (!empty($course->sign_start_date)) {
if (time() < strtotime($course->sign_start_date)) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '报名时间未到']);
}
}
if (!empty($course->sign_end_date)) {
if (time() > strtotime($course->sign_end_date) + 86400) {
return $this->fail([ResponseCode::ERROR_BUSINESS, '报名已结束']);
}
}
$result = CourseSign::create([
'is_change' => $all['is_change'] ?? 0,
'course_id' => $all['course_id'],
'user_id' => $this->getUserId(),
'data' => $all['data'] ?? [],
'change_data' => $all['change_data'] ?? [],
'no' => getBatchNo(),
'money' => $course->price,
'title' => $course->name
]);
// 发短信
$smsSign = Config::getValueByKey('sms_sign');
if ($course->is_fee) {
$content = "{$smsSign}您已提交【{$course->name}】报名申请,请等待审核结果。";
} else {
$content = "{$smsSign}您已提交【{$course->name}】报名申请,请等待审核结果通知。";
}
ymSms($this->getUser()->mobile, $content);
return $this->success($result);
}
/**
* @OA\Get(
* path="/api/mobile/course/my-course",
* tags={"小程序-课程"},
* summary="我的课程",
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function myCourse()
{
$list = Course::with('typeDetail')->with(['courseSigns' => function ($query) {
$query->where('user_id', $this->getUserId());
}])->whereHas('courseSigns', function ($query) {
$query->where('user_id', $this->getUserId());
})->where('is_virtual', 0)->orderBy('id', 'desc')->paginate($all['page_size'] ?? 20);
return $this->success(compact('list'));
}
/**
* @OA\Get(
* path="/api/mobile/course/my-course-content",
* tags={"小程序-课程"},
* summary="我的课表",
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function myCourseContent()
{
$list = CourseContent::with('course.typeDetail', 'teacher')->whereHas('course', function ($query) {
$query->where('course_status', '!=', 40)->where('course_content_status', 1);
$query->whereHas('courseSigns', function ($query) {
$query->where('user_id', $this->getUserId())->where('status', 1)->where(function ($q) {
$q->where('fee_status', 1)->orWhere(function ($q) {
$q->whereHas('course', function ($qry) {
$qry->where('is_fee', 0);
});
});
});
});
})->orderBy('date')->get();
return $this->success(compact('list'));
}
/**
* @OA\Get(
* path="/api/mobile/course/get-sign",
* tags={"小程序-课程"},
* summary="课程报名详情",
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function getSign()
{
$all = \request()->all();
$messages = [
'course_id.required' => '课程id必填',
];
$validator = Validator::make($all, [
'course_id' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$detail = CourseSign::with('course.typeDetail', 'user')
->where('user_id', $this->getUserId())->where('course_id', $all['course_id'])
->first();
return $this->success(compact('detail'));
}
/**
* @OA\Post(
* path="/api/mobile/course/update-sign",
* tags={"小程序-课程"},
* summary="更新报名信息",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=false, description="报名id"),
* @OA\Parameter(name="fee_file_ids", in="query", @OA\Schema(type="string"), required=false, description="文件id数组"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function updateSign()
{
$all = \request()->all();
$messages = [
'id.required' => '报名id必填',
];
$validator = Validator::make($all, [
'id' => 'required',
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$signModel = CourseSign::find($all['id']);
$original = $signModel->getOriginal();
$signModel->fill($all);
$signModel->save();
if ($original['status'] == 1 && $signModel->status != 1) {
// 审核通过到其他状态
// 减少预约次数
CourseAppointmentTotal::reduce($signModel->user_id, $signModel->id);
}
return $this->success(compact('signModel'));
}
/**
* @OA\Get(
* path="/api/mobile/course/contents",
* tags={"小程序-课程"},
* summary="课表",
* @OA\Parameter(name="course_id", in="query", @OA\Schema(type="string"), required=false, description="课程id"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function contents()
{
$all = \request()->all();
$messages = [
'course_id.required' => '课程id必填',
];
$validator = Validator::make($all, [
'course_id' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$list = CourseContent::with('teacher', 'course')
->whereHas('course', function ($query) {
$query->where('course_content_status', 1);
})->where('course_id', $all['course_id'])
->orderBy('date', 'asc')
->get();
return $this->success(compact('list'));
}
/**
* @OA\Get(
* path="/api/mobile/course/user-list",
* tags={"小程序-课程"},
* summary="校友库和本班通讯录",
* @OA\Parameter(name="type", in="query", @OA\Schema(type="string"), required=false, description="类型1校友库2本班通讯录"),
* @OA\Parameter(name="course_id", in="query", @OA\Schema(type="string"), required=false, description="课程id"),
* @OA\Parameter(name="name", in="query", @OA\Schema(type="string"), required=false, description="名字"),
* @OA\Parameter(name="company_business", in="query", @OA\Schema(type="string"), required=false, description="主营业务"),
* @OA\Parameter(name="company_name", in="query", @OA\Schema(type="string"), required=false, description="公司名字"),
* @OA\Parameter(name="company_position", in="query", @OA\Schema(type="string"), required=false, description="职务"),
* @OA\Parameter(name="company_area", in="query", @OA\Schema(type="string"), required=false, description="所属区域"),
* @OA\Parameter(name="company_type", in="query", @OA\Schema(type="string"), required=false, description="企业性质"),
* @OA\Parameter(name="company_industry", in="query", @OA\Schema(type="string"), required=false, description="所属行业"),
* @OA\Parameter(name="letter", in="query", @OA\Schema(type="string"), required=false, description="首字母"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function userList()
{
$all = \request()->all();
$messages = [
'type.required' => '类型必填',
];
$validator = Validator::make($all, [
'type' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$list = User::whereHas('courseSigns', function ($query) use ($all) {
if (isset($all['course_id'])) {
$query->where('course_id', $all['course_id']);
}
if ($all['type'] == 2) {
$query->where('status', 1);
}
})->with(['courseSigns' => function ($query) use ($all) {
$query->where('status', 1)->whereHas('course', function ($q) {
$q->where('is_fee', 1);
})->with('course.teacher', 'course.typeDetail')
->orderByRaw("FIELD(fee_status, 1, 0, 2,3)");
if (isset($all['course_id'])) {
$query->where('course_id', $all['course_id']);
}
}])->where(function ($query) use ($all) {
if ($all['type'] == 1) {
$query->where('is_schoolmate', 1);
}
if (isset($all['name'])) {
$query->where('name', 'like', '%' . $all['name'] . '%');
}
if (isset($all['company_business'])) {
$query->where('company_business', 'like', '%' . $all['company_business'] . '%');
}
if (isset($all['company_name'])) {
$query->where('company_name', 'like', '%' . $all['company_name'] . '%');
}
if (isset($all['company_position'])) {
$query->where('company_position', $all['company_position']);
}
if (isset($all['company_area'])) {
$query->where('company_area', 'like', '%' . $all['company_area'] . '%');
}
if (isset($all['company_type'])) {
$company_type = explode(',', $all['company_type']);
$query->where(function ($q) use ($company_type) {
foreach ($company_type as $v) {
$q->orWhereRaw('FIND_IN_SET(?, company_type)', [$v]);
}
});
}
if (isset($all['company_industry'])) {
$company_industry = explode(',', $all['company_industry']);
$query->where(function ($q) use ($company_industry) {
foreach ($company_industry as $v) {
$q->orWhereRaw('FIND_IN_SET(?, company_industry)', [$v]);
}
});
}
if (isset($all['letter'])) {
$query->where('letter', $all['letter']);
}
});
if (isset($all['type']) && $all['type'] == 2) {
$list = $list->orderBy('letter')
->paginate(10);
} else {
$list = $list->orderBy('letter')->paginate(20);
}
$teacher = [];
if (isset($all['type']) && $all['type'] == 2) {
$course = Course::find($all['course_id']);
$teacher = $course['teacher_detail'];
}
return $this->success(compact('list', 'teacher'));
}
/**
* @OA\Get(
* path="/api/mobile/course/news",
* tags={"小程序-课程"},
* summary="文章列表",
* @OA\Parameter(name="page_size", in="query", @OA\Schema(type="string"), required=false, description="每页显示的条数"),
* @OA\Parameter(name="page", in="query", @OA\Schema(type="string"), required=false, description="页码"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function news()
{
$page = request('page', 1);
$page_size = request('page_size', 8);
$url = config('app.new_api') . "/e/extend/news.php?page={$page}&pagesize={$page_size}";
$content = file_get_contents($url);
$content = json_decode($content, true);
$pattern = '/\/d\/file\//';
$replacement = config('app.new_api') . '/d/file/';
foreach ($content['rows'] as &$item) {
$item['titlepic'] = preg_replace($pattern, $replacement, $item['titlepic']);
}
return $this->success($content);
}
/**
* @OA\Get(
* path="/api/mobile/course/news-detail",
* tags={"小程序-课程"},
* summary="文章详情",
* @OA\Parameter(name="id", in="query", @OA\Schema(type="string"), required=false, description="id"),
* @OA\Response(
* response=200,
* description="操作成功"
* )
* )
*/
public function newsDetail()
{
$all = \request()->all();
$messages = [
'id.required' => 'id必填',
];
$validator = Validator::make($all, [
'id' => 'required'
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$url = config('app.new_api') . "/e/extend/news_detail.php?id={$all['id']}";
$detail = file_get_contents($url);
$detail = json_decode($detail, true);
$detail = $detail['rows'];
// 正则表达式替换
// 使用正则表达式替换图片路径
$pattern = '/\/d\/file\//';
$replacement = config('app.new_api') . '/d/file/';
$detail['newstext'] = preg_replace($pattern, $replacement, $detail['newstext']);
return $this->success($detail);
}
/**
* @OA\Get(
* path="/api/mobile/course/pay",
* tags={"小程序-课程"},
* summary="获取支付参数",
* @OA\Parameter(name="no", in="query", @OA\Schema(type="string"), required=false, description="no"),
* @OA\Parameter(name="token", in="query", @OA\Schema(type="string"), required=true, description="token"),
* @OA\Response(
* response="200",
* description="暂无"
* )
* )
*/
public function pay()
{
$all = \request()->all();
$messages = [
'no.required' => 'no必填',
];
$validator = Validator::make($all, [
'no' => 'required',
], $messages);
if ($validator->fails()) {
return $this->fail([ResponseCode::ERROR_PARAMETER, implode(',', $validator->errors()->all())]);
}
$courseSign = CourseSign::where('no', $all['no'])->first();
if (!$courseSign) {
return $this->fail([ResponseCode::ERROR_PARAMETER, '订单不存在']);
}
$course = Course::find($courseSign->course_id);
if ($course->fee_type != 2) {
return $this->fail([ResponseCode::ERROR_PARAMETER, '支付类型错误']);
}
// 下单
$config = [
'app_id' => config('app.applet_appid'),
'mch_id' => config('app.applet_mch_id'),
'key' => config('app.applet_key')
];
$app = Factory::payment($config);
$result = $app->order->unify([
'trade_type' => 'NATIVE',
'product_id' => $courseSign->no,
'body' => $courseSign->title,
'out_trade_no' => $courseSign->no,
'total_fee' => $courseSign->money * 100,
'notify_url' => getDomain() . '/api/mobile/course/pay_callback',
]);
return $this->success(compact('result'));
}
public function payCallback()
{
$config = [
'app_id' => config('app.applet_appid'),
'mch_id' => config('app.applet_mch_id'),
'key' => config('app.applet_key')
];
// 获取支付参数
$app = Factory::payment($config);
$response = $app->handlePaidNotify(function ($message, $fail) {
if ($message['return_code'] === 'SUCCESS') {
if ($message['result_code'] === 'SUCCESS') {
$order = CourseSign::where('no', $message['out_trade_no'])->first();
if ($order) {
$order->fee_status = 1;
$order->save();
}
return true;
}
} else {
return $fail('通信失败,请稍后再通知我');
}
return true; // 返回处理完成
});
$response->send();
}
}