Course.php
14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<?php
namespace app\mobile\controller;
use think\Validate;
use think\Db;
use app\common\controller\Api;
use app\mobile\model\CourseBanner;
use app\mobile\model\CourseCatalog;
use app\mobile\model\CourseAppraise;
use app\mobile\model\CourseSpec;
use app\mobile\model\CourseOrder;
use app\mobile\model\Company;
use app\mobile\model\CompanyUser;
use addons\epay\library\Service;
/**
* 课程接口
* @ApiWeigh (93)
*/
class Course extends Api
{
protected $noNeedLogin = ['banner','index','info','catalog','appraiseList'];
protected $noNeedRight = ['*'];
public function _initialize()
{
parent::_initialize();
$this->model = model('app\mobile\model\Course');
}
/**
* @ApiTitle (课程-轮播图)
* @ApiSummary (课程-轮播图)
* @ApiMethod (POST)
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599907184",
"data": [{
"id": 1, // 轮播图ID
"image": "https://img02.mockplus.cn/idoc/sketch/2020-08-06/006324c3-e5d3-4206-be48-52a7c7468e74.7C1FC40C-CED5-4B08-9506-3E48A2732B2C.png" // 图片地址
}]
})
*/
public function banner()
{
$list = CourseBanner::order('createtime desc')->field('id,image')->select();
$this->success('成功',$list);
}
/**
* @ApiTitle (课程-首页)
* @ApiSummary (课程-首页)
* @ApiMethod (POST)
* @ApiParams (name="forum_category_id", type="inter", required=false, description="话题分类ID")
* @ApiParams (name="order", type="inter", required=false, description="排序方式:0=综合排序,1=按学习人数,2=价格从低到高,3=价格从高到低")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599907522",
"data": {
"total": 1, //数据总条数
"list": [{
"id": 1, //课程ID
"title": "测试课程", //课程标题
"cover": "", //课程封面图
"current_price": "50.00", //课程现价
"original_price": "100.00", //课程原价
"study_num": 3 //学习人数
}]
}
})
*/
public function index()
{
$order = $this->request->param('order',0);
$page = $this->request->param('page', 1, 'intval');
$page_num = $this->request->param('page_num', 10, 'intval');
switch ($order) {
case 0:
$order = ['is_top' => 'desc','top_time' => 'desc'];
break;
case 1:
$order = ['study_num' => 'desc'];
break;
case 2:
$order = ['current_price' => 'asc'];
break;
case 3:
$order = ['current_price' => 'desc'];
break;
}
$data = $this->model
->field("
id,
title,
cover,
current_price,
original_price,
(study_num_virtual + study_num_real) study_num
")
->order($order)
->paginate($page_num,false,['page'=>$page])
->toArray();
$this->success('成功',['total'=>$data['total'],'list'=>$data['data']]);
}
/**
* @ApiTitle (课程详情)
* @ApiSummary (课程详情)
* @ApiMethod (POST)
*
* @ApiParams (name="course_id", type="int", required=true, description="课程ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599907645",
"data": {
"id": 1, //课程ID
"title": "测试课程", //课程标题
"cover": "", //封面图
"current_price": "50.00", //现价
"original_price": "100.00", //原价
"content": null, //内容
"is_top": "0", //是否置顶:0=否,1=是
"top_time": null, //置顶时间
"study_num_virtual": 1, //虚拟学习人数
"study_num_real": 2, //真实学习人数
"createtime": null,
"updatetime": null,
"video": "" //一节课视频地址
}
})
*/
public function info()
{
$course_id = $this->request->param('course_id');
empty($course_id) && $this->error('缺少必要参数');
$info = $this->model->get($course_id);
// 课程视频
// 第一节目录
$catalog_parent = CourseCatalog::where('course_id',$course_id)
->where('pid',0)
->order('weigh asc')
->find();
// 第一节目录下的第一课程
$catalog_child = CourseCatalog::where('course_id',$course_id)
->where('pid',$catalog_parent['id'])
->order('weigh asc')
->find();
$video = '';
if(!empty($catalog_parent) && !empty($catalog_child)){
$video = $catalog_child['video'];
}
$info['video'] = $video;
$this->success('成功',$info);
}
/**
* @ApiTitle (课程详情-目录)
* @ApiSummary (课程详情-目录)
* @ApiMethod (POST)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="course_id", type="int", required=true, description="课程ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599908189",
"data": [{
"id": 1, //一级目录ID
"name": "第一章",
"childlist": [{ //二级目录列表
"id": 2, //二级目录ID
"name": "第一章-第一节", //二级目录名称
"video": "https://vd2.bdstatic.com/mda-jkptk0q9euab5v41/sc/mda-jkptk0q9euab5v41.mp4?auth_key=1599909914-0-0-1b5b778ac7883d30cf78883ff8884b7e&bcevod_channel=searchbox_feed&pd=1&pt=3" //视频地址
}]
}]
})
*/
public function catalog()
{
$course_id = $this->request->param('course_id');
empty($course_id) && $this->error('缺少必要参数');
$list = CourseCatalog::where('course_id',$course_id)
->where('pid',0)
->field('id,name')
->order('weigh asc')
->select();
foreach ($list as &$v) {
$childlist = CourseCatalog::where('pid',$v['id'])
->where('course_id',$course_id)
->order('weigh asc')
->select();
foreach ($childlist as $val) {
$val->visible(['id','name','video']);
}
$v['childlist'] = $childlist;
}
$this->success('成功',$list);
}
/**
* @ApiTitle (课程详情-评价)
* @ApiSummary (课程详情-评价)
* @ApiMethod (POST)
*
* @ApiParams (name="course_id", type="int", required=true, description="课程ID")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599908791",
"data": {
"total": 1, //数据总条数
"list": [{
"id": 1, //评价ID
"star": 5, //评价星数
"content": "keyi ", //评价内容
"createtime": "1970.01.01 08:00", //评价时间
"user": { //用户信息
"nickname": "", //真实姓名
"image": "http://qizhibang.brotop.cn/uploads/20200814/FkGifOc2vGqdgoZ68cDFAv5HWKqh.jpg" //头像
}
}]
}
})
*/
public function appraiseList()
{
$course_id = $this->request->param('course_id');
$page = $this->request->param('page', 1, 'intval');
$page_num = $this->request->param('page_num', 10, 'intval');
empty($course_id) && $this->error('缺少必要参数');
$data = CourseAppraise::with(['user'])
->where('course_id',$course_id)
->order('createtime desc')
->paginate($page_num,false,['page'=>$page])
->each(function($v){
$v->visible(['id','star','content','createtime','user']);
$v->createtime = date('Y.m.d H:i',$v['createtime']);
$v->getRelation('user')->visible(['user_id','image','nickname']);
})->toArray();
$this->success('成功',['total'=>$data['total'],'list'=>$data['data']]);
}
/**
* @ApiTitle (选择规格)
* @ApiSummary (选择规格)
* @ApiMethod (POST)
*
* @ApiParams (name="course_id", type="int", required=true, description="课程ID")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599739316",
"data": [{
"id": 1, //规格ID
"course_id": 1, //课程ID
"name": "基础版", //套餐名称
"current_price": "500.00", //当前价格
"original_price": "1000.00", //原价
"people_num": 20, //限制人数
"is_top": "0", //是否顶配:0=否,1=是
"createtime": null,
"updatetime": null,
"is_pay": 1 //是否可以购买:0=否,1=是
}]
})
*/
public function spec()
{
$company = Company::get(['user_id'=>$this->auth->id]);
$course_id = $this->request->param('course_id');
empty($course_id) && $this->error('缺少必要参数');
$info = $this->model->get($course_id);
empty($info) && $this->error('课程信息不存在');
$list = CourseSpec::where('course_id',$course_id)->select();
foreach ($list as &$v) {
// 是否可购买
$people_num = CompanyUser::where('company_id',$company['id'])
->where('status','1')
->count();
if($v['is_top'] == '1'){
$v['is_pay'] = 1;
}else{
$v['is_pay'] = $v['people_num'] < $people_num ? 0 : 1;
}
// 显示价格
$order = CourseOrder::where('company_id',$company['id'])
->where('course_id',$course_id)
->where('pay_status','1')
->order('people_num desc')
->find();
$v['current_price'] = !empty($order) ? $v['current_price']-$order['total_price'] : $v['current_price'];
}
$this->success('成功',$list);
}
/**
* @ApiTitle (套餐说明)
* @ApiSummary (套餐说明)
* @ApiMethod (POST)
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599017563",
"data": "套餐说明" //套餐说明内容
})
*/
public function spec_intro()
{
$content = Db::name('mobile_config')->where('id',1)->value('course_spec_intro');
$this->success('成功', $content);
}
/**
* @ApiTitle (购买预览)
* @ApiSummary (购买预览)
* @ApiMethod (POST)
*
* @ApiParams (name="course_id", type="int", required=true, description="课程ID")
* @ApiParams (name="spec_id", type="int", required=true, description="课程规格ID")
* @ApiParams (name="score_switch", type="int", description="积分开关:0=关,1=开")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599046220",
"data": {
"id": 1, //试卷ID
"title": "测试试卷", //试卷标题
"year": 2015, //年费(单位:年)
"time": 100, //答题时间(单位:分)
"pass_score": 80, //合格分数
"description": "这个还行", //试卷描述
"do_num": 10, //回答人数
"full_score": 100 //试卷分数(单位:分)
}
})
*/
public function payView()
{
$param = $this->request->param();
if(!$order = $this->model->payView($this->auth->getUser(),$param)){
$this->error($this->model->getError(),null,$this->model->getCode());
}
$this->success(__('成功'),$order);
}
/**
* @ApiTitle (购买)
* @ApiSummary (购买)
* @ApiMethod (POST)
*
* @ApiParams (name="course_id", type="int", required=true, description="课程ID")
* @ApiParams (name="spec_id", type="int", required=true, description="课程规格ID")
* @ApiParams (name="score_switch", type="int", description="积分开关:0=关,1=开")
* @ApiParams (name="pay_type", type="string", required=true, description="支付方式:wechat=微信,alipay=支付宝")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1599046220",
"data": {
"id": 1, //试卷ID
"title": "测试试卷", //试卷标题
"year": 2015, //年费(单位:年)
"time": 100, //答题时间(单位:分)
"pass_score": 80, //合格分数
"description": "这个还行", //试卷描述
"do_num": 10, //回答人数
"full_score": 100 //试卷分数(单位:分)
}
})
*/
public function pay()
{
$param = $this->request->param();
if(!$order = $this->model->payView($this->auth->getUser(),$param)){
$this->error($this->model->getError(),null,$this->model->getCode());
}
if (!$param['pay_type'] || !in_array($param['pay_type'], ['alipay', 'wechat'])) {
$this->error("请选择支付方式");
}
// 创建订单
$model = new CourseOrder;
$model->add($this->auth->getUser(), $order, $param['pay_type']);
//回调链接
$notifyurl = $this->request->root(true) . '/mobile/course/notifyx/paytype/' . $param['pay_type'];
$payment = Service::submitOrder($model['pay_price'], $model['order_sn'], $param['pay_type'], '课程', $notifyurl, null, 'app');
$this->success('成功',$payment);
}
/**
* 支付成功
*/
public function notifyx()
{
$paytype = $this->request->param('paytype');
$pay = \addons\epay\library\Service::checkNotify($paytype);
if (!$pay) {
echo '签名错误';
return;
}
$data = $pay->verify();
try {
$payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
$out_trade_no = $data['out_trade_no'];
// 处理订单逻辑
$order = CourseOrder::get(['order_sn'=>$out_trade_no,'pay_price'=>$payamount,'pay_type'=>$paytype]);
if($order && $order['pay_status'] != '1'){
$order->save(['pay_status'=>'1','pay_time'=>time()]);
}
} catch (Exception $e) {
}
echo $pay->success();
}
}