User.php
32.0 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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
<?php
namespace app\api\controller;
use app\admin\model\Set;
use app\common\controller\Api;
use function fast\e;
use think\Cache;
use think\Db;
use think\Session;
use think\Config;
use app\api\controller\Common;
use app\common\controller\Backend;
use EasyWeChat\Message\Image;
use Exception;
use fast\Auth;
use fast\Random;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use Endroid\QrCode\QrCode;
use think\exception\PDOException;
/**
* 我的资料
*/
class User extends Api
{
protected $noNeedLogin = ['video_list'];
protected $noNeedRight = ['*'];
// 允许批量修改字段
protected $multiFields = 'view_status';
/**
* @ApiTitle (我的资料)
* @ApiSummary (我的资料)
* @ApiMethod (POST)
* @ApiRoute (/api/user/index)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"id"://ID
"username"://用户名
"nickname"://姓名
"sex"://性别1男0女
"card"://身份证号
"work_address"://工作地点
"phone"://手机号
"image"://头像
}
})
*/
public function index()
{
$user_id = $this->auth->id;
$data = Db::name('user')->where('id',$user_id)->find();
if(empty($data['nickname'])){
$data['nickname'] = '';
}
if(empty($data['image'])){
$data['image'] = '';
$data['yuan_image'] = '';
}else{
$data['yuan_image'] = $data['image'];
$data['image'] = cdnurl($data['image'],true);
}
if(empty($data['card'])){
$data['card'] = '';
}
if(empty($data['mobile'])){
$data['phone'] = '';
}
if(empty($data['work_address'])){
$data['work_address'] = '';
}
$this->success('success',$data);
}
/**
* @ApiTitle (修改我的资料)
* @ApiSummary (修改我的资料)
* @ApiMethod (POST)
* @ApiRoute (/api/user/edit_info)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="nickname", type="string", required=true, description="姓名")
* @ApiParams (name="sex", type="int", required=true, description="性别1男0女")
* @ApiParams (name="card", type="string", required=true, description="身份证号")
* @ApiParams (name="work_address", type="string", required=true, description="工作单位")
* @ApiParams (name="phone", type="string", required=true, description="手机号")
* @ApiParams (name="code", type="string", required=true, description="验证码")
* @ApiParams (name="image", type="string", required=true, description="照片")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
}
})
*/
public function edit_info()
{
$user_id = $this->auth->id;
$nickname = $this->request->param('nickname');
$sex = $this->request->param('sex');
$card = $this->request->param('card');
$work_address = $this->request->param('work_address');
$phone = $this->request->param('phone');
$code = $this->request->param('code');
$image = $this->request->param('image');
if(!empty($phone)){
// $have_mobile = Db::name('user')->where('mobile', $phone)->where('id', '<>', $user_id)->find();
// if($have_mobile){
// $this->error('手机号已存在');
// }
if(empty($code)){
$this->error('验证码不能为空');
}
// $yuan_code = Cache::get($phone);
// if($code != $yuan_code){
// $this->error('验证码错误');
// }
}
$data = Db::name('user')->where('id',$user_id)->find();
$params = [];
if($sex){
$params['sex'] = $sex;
}
if($phone){
$params['mobile'] = $phone;
$params['phone'] = $phone;
}
if(empty($data['image']) && $image){
$params['image'] = $image;
}
if($params){
Db::name('user')
->where('id',$user_id)
->update($params);
}
$this->success('success');
}
/**
* @ApiTitle (培训课程列表)
* @ApiSummary (培训课程列表)
* @ApiMethod (POST)
* @ApiRoute (/api/user/video_list)
*
* @ApiParams (name="id", type="int", required=true, description="学习系统id")
* @ApiParams (name="type", type="int", required=true, description="类型1或者空是全部2必修3选修")
* @ApiParams (name="page", type="inter", required=false, description="当前页(默认1)")
* @ApiParams (name="pageNum", type="inter", required=false, description="每页显示数据个数(默认10)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"total_num"://总时长
"info":[
"id"://ID
"study_id"://学习系统id
"is_major"://1主修2选修
"tag"://标签
"name"://课程名称
"video_file"://视频地址
"video_image"://视频首帧图
"duration"://总时长/秒
"class_hour"://学时
"content"://内容详情
"teacher"://讲师
]
}
})
*/
public function video_list()
{
$page = $this->request->param('page', 1, 'intval');
$pageNum = $this->request->param('pageNum', 10, 'intval');
$id = $this->request->param('id');
if(empty($id)){
$this->error('缺少必要参数');
}
$type = $this->request->param('type');
if($type == 1 || empty($type)){
true;
}elseif ($type == 2){
$where['is_major'] = 1;
}elseif ($type == 3){
$where['is_major'] = 2;
}
$where['study_id'] = $id;
$data['total_num'] = Db::name('classes')
->where($where)
->count();
$data['info'] = Db::name('classes')
->field('createtime,updatetime',true)
->where($where)
->page($page,$pageNum)
->order('id desc')
->select();
foreach ($data['info'] as &$v){
$v['video_file'] = cdnurl($v['video_file'],true);
// $video_info = json_decode(file_get_contents($v['video_file'] . '?avinfo'), true);
$v['video_image'] = cdnurl($v['video_image'],true);
}
$this->success('success',$data);
}
/**
* @ApiTitle (培训课程详情)
* @ApiSummary (培训课程详情)
* @ApiMethod (POST)
* @ApiRoute (/api/user/video_detail)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="int", required=true, description="培训课程id")
*
* @ApiReturn({
"code": 1,
"msg": "success",
"time": "1618221189",
"data": {
"id": 391, //ID
"study_id": 20, //学习系统id
"is_major": 1, //1主修2选修
"tag": "安全员继续教育", //标签
"name": "01-建设工程安全生产法律制度-1", //课程名称
"video_image": "http://qizhibang.brotop.cn/uploads/20210304/FiebeoL5youcbR0DPZcli5_FhfGQ.JPG", //视频首帧图
"video_file": "http://qizhibang.brotop.cn/uploads/20210304/lljynpl-YhcK_jJs8RgteX0ZazAh.mp4", //视频地址
"duration": "3737", //总时长/秒
"class_hour": "2", //学时
"teacher": "韩晓冬", //讲师
"content": "01-建设工程安全生产法律制度-1", //内容详情
"createtime": 1614848450,
"updatetime": 1614850419,
"expirationtime": "2021-12-31 23:59:59", //截止日期
"bofangtime": 3738, //播放时长/秒
"is_end": 1, //是否结束1已看完2未看完
"is_join": 2, //是否已经加入学习计划1未加入2已加入
"min": 63, //总时长/分
"is_fast_switch": 0 //是否可以快进0否1是
}
})
*/
public function video_detail()
{
$user_id = $this->auth->id;
$id = $this->request->param('id');
if(empty($id)){
$this->error('缺少必要参数');
}
$user_info = Db::name('user')->where('id',$user_id)->find();
// if(empty($user_info['image']) || empty($user_info['mobile'])){
// $this->error('请先完善个人信息');
// }
if(empty($user_info['image'])){
$this->error('请先完善个人头像信息');
}
$data = Db::name('classes')
->alias('a')
->join('study b','a.study_id = b.id')
->where('a.id',$id)
->field('a.*,b.expirationtime')
->find();
// 查看用户是否有权限
$third_study = Db::name('third_study')->where('third_id',$user_id)->where('study_id',$data['study_id'])->find();
if(empty($third_study)){
$this->error('您没有该场考试的权限,无法观看视频');
}else{
$third_study['periodtime'] < time() && $this->error('学习期限已过,无法观看视频');
}
// 判断用户查看视频状态
$admin_option = Set::get(1);
if($admin_option['only_switch'] == 1 && $this->auth->view_status == 1) {
$this->error('当前用户正在观看视频,无法查看其他视频');
}
//查看播放秒数
$class_detail = Db::name('class_detail')
->where('user_id',$user_id)
->where('class_id',$id)
->find();
if(empty($class_detail)){
$res['user_id'] = $user_id;
$res['class_id'] = $id;
$res['duration'] = 0;
$res['createtime'] = time();
Db::name('class_detail')->insert($res);
$data['bofangtime'] = 0;
$data['is_end'] = 2;
}else{
$data['bofangtime'] = $class_detail['duration'];
$data['is_end'] = $class_detail['is_end'];
}
$info = Db::name('study_class')
->where('third_id',$user_id)
->where('study_id',$data['study_id'])
->where('class_id',$id)
->find();
if(empty($info)){
$data['is_join'] = 1;
}else{
$data['is_join'] = 2;
}
//视频地址以及首帧图
$data['video_file'] = cdnurl($data['video_file'],true);
// $video_info = json_decode(file_get_contents($data['video_file'] . '?avinfo'), true);
$data['video_image'] = cdnurl($data['video_image'],true);
$data['expirationtime'] = date('Y-m-d H:i:s',$data['expirationtime']);
$data['min'] = ceil($data['duration']/60);
// 视频快进开关
$data['is_fast_switch'] = Db::name('set')->value('fast_switch');
// 自动暂停
$data['auto_pause'] = Db::name('set')->field('stopnum,switch')->find();
$this->success('success',$data);
}
/**
* @ApiTitle (记录用户播放状态)
* @ApiSummary (记录用户播放状态)
* @ApiMethod (POST)
* @ApiRoute (/api/user/change_view_status)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="view_status", type="int", required=true, description="观看视频状态:0=结束观看,1=开始观看")
* @ApiParams (name="view_id", type="int", required=true, description="培训课程id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001"
})
*/
public function change_view_status()
{
$user_id = $this->auth->id;
$view_status = $this->request->param('view_status',0);
$id = $this->request->param('view_id');
if(empty($id)){
$this->error('缺少必要参数');
}
$user_info = Db::name('user')->where('id',$user_id)->find();
if(empty($user_info['image'])){
$this->error('请先完善个人头像信息');
}
$data = Db::name('classes')
->alias('a')
->join('study b','a.study_id = b.id')
->where('a.id',$id)
->field('a.*,b.expirationtime')
->find();
// 查看用户是否有权限
$third_study = Db::name('third_study')->where('third_id',$user_id)->where('study_id',$data['study_id'])->find();
if(empty($third_study)){
$this->error('您没有该场考试的权限,无法观看视频');
}else{
$third_study['periodtime'] < time() && $this->error('学习期限已过,无法观看视频');
}
$user_update = [
'view_status' => $view_status,
'view_id' => $view_status == 1 ? $id : 0
];
if($view_status == 0 || $id != $this->auth->view_id) {
// 修改用户观看视频状态
$res = \app\common\model\User::update($user_update,['id'=>$user_id]);
if(!$res) $this->error('修改失败');
}
$this->success('修改成功');
}
/**
* @ApiTitle (记录播放时间)
* @ApiSummary (记录播放时间)
* @ApiMethod (POST)
* @ApiRoute (/api/user/bofang)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="int", required=true, description="培训课程id")
* @ApiParams (name="duration", type="int", required=true, description="播放时间")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
}
})
*/
public function bofang()
{
$user_id = $this->auth->id;
$id = $this->request->param('id');
$duration = $this->request->param('duration');
if(empty($id) || empty($duration)){
$this->error('缺少必要参数');
}
$class = Db::name('classes')
->where('id',$id)
->field('id,duration,study_id')
->find();
if($duration >= $class['duration']){
$is_end = 1;
$study_class = Db::name('study_class')
->where('third_id',$user_id)
->where('study_id',$class['study_id'])
->where('class_id',$id)
->find();
if(!empty($study_class)){
Db::name('study_class')
->where('third_id',$user_id)
->where('study_id',$class['study_id'])
->where('class_id',$id)
->update(['status'=>2,'playtime'=>$duration]);
}
}else{
$is_end = 2;
$study_class = Db::name('study_class')
->where('third_id',$user_id)
->where('study_id',$class['study_id'])
->where('class_id',$id)
->find();
if(!empty($study_class)){
Db::name('study_class')
->where('third_id',$user_id)
->where('study_id',$class['study_id'])
->where('class_id',$id)
->update(['playtime'=>$duration]);
}
}
$data = Db::name('class_detail')
->where('user_id',$user_id)
->where('class_id',$id)
->update(['duration'=>$duration,'is_end'=>$is_end]);
if($data === false){
$this->error('失败');
}else{
$this->success('成功');
}
}
/**
* @ApiTitle (加入学习计划)
* @ApiSummary (加入学习计划)
* @ApiMethod (POST)
* @ApiRoute (/api/user/join)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="int", required=true, description="培训课程id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
}
})
*/
public function join()
{
$info['third_id'] = $this->auth->id;
$info['class_id'] = $this->request->param('id');
if(empty($info['class_id'])){
$this->error('缺少必要参数');
}
$data = Db::name('classes')
->where('id',$info['class_id'])
->field('study_id,duration')
->find();
$info['study_id'] = $data['study_id'];
//查询用户是否已经播放完毕该视频
$video = Db::name('class_detail')
->where('user_id',$info['third_id'])
->where('class_id',$info['class_id'])
->find();
if(empty($video)){
$info['playtime'] = 0;
}else{
if($video['is_end'] == 2){
$info['playtime'] = $video['duration'];
}else{
$info['playtime'] = $data['duration'];
$info['status'] = 2;
}
}
$info['createtime'] = time();
$is_have = Db::name('study_class')
->where('third_id',$info['third_id'])
->where('study_id',$info['study_id'])
->where('class_id',$info['class_id'])
->find();
if(empty($is_have)){
$arr = Db::name('study_class')->insertGetId($info);
if(empty($arr)){
$this->error('添加失败');
}else{
$this->success('添加成功');
}
}else{
$this->error('您已经添加过了');
}
}
/**
* @ApiTitle (加入学习计划-批量)
* @ApiSummary (加入学习计划-批量)
* @ApiMethod (POST)
* @ApiRoute (/api/user/joinBatch)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="ids", type="string", required=true, description="培训课程id集合(多个id用英文逗号隔开)")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": null
})
*/
public function joinBatch()
{
$ids = $this->request->param('ids');
empty($ids) && $this->error('缺少必要参数');
$list = Db::name('classes')->where('id','in',$ids)->field('id,study_id,duration')->select();
count($list) <= 0 && $this->error('传入参数不合法');
$data = [];
$third_id = $this->auth->id;
$time = time();
foreach($list as $v){
$is_have = Db::name('study_class')
->where('third_id',$third_id)
->where('study_id',$v['study_id'])
->where('class_id',$v['id'])
->find();
if(empty($is_have)){
$add_data = [
'third_id' => $third_id,
'study_id' => $v['study_id'],
'class_id' => $v['id'],
'createtime' => $time
];
//查询用户是否已经播放完毕该视频
$video = Db::name('class_detail')
->where('user_id',$third_id)
->where('class_id',$v['id'])
->find();
if(empty($video)){
$add_data['playtime'] = 0;
}else{
if($video['is_end'] == 2){
$add_data['playtime'] = $video['duration'];
}else{
$add_data['playtime'] = $v['duration'];
$add_data['status'] = 2;
}
}
$data[] = $add_data;
}
}
if(!empty($data)){
Db::name('study_class')->insertAll($data);
}
$this->success('添加成功');
}
/**
* @ApiTitle (移除学习计划)
* @ApiSummary (移除学习计划)
* @ApiMethod (POST)
* @ApiRoute (/api/user/del_join)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="int", required=true, description="培训课程id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
}
})
*/
public function del_join()
{
$user_id = $this->auth->id;
$id = $this->request->param('id');
if(empty($id)){
$this->error('缺少必要参数');
}
$data = Db::name('classes')
->where('id',$id)
->value('study_id');
$arr = Db::name('study_class')
->where('third_id',$user_id)
->where('study_id',$data)
->where('class_id',$id)
->delete();
if(empty($arr)){
$this->error('移除失败');
}else{
$this->success('移除成功');
}
}
/**
* @ApiTitle (我的考试)
* @ApiSummary (我的考试)
* @ApiMethod (POST)
* @ApiRoute (/api/user/myexam)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="int", required=true, description="学习系统id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"exam":[
"id"://id,
"examname"://考试名称
"class_hour"://所需课时
"finish_hour"://完成课时
"image"://图片
"proportion"://比例
"expirationtime"://截止日期
"is_xian"://是否显示1显示2不显示
],
"info": [
{
"name": //课程名称,
"class_hour": //课时,
"is_major": //1主修2选修,
"duration": //时长,
"status": //状态1学习中2已完成,
"playtime": //已播放的秒数,
"proportion": //比例
},
]
}
})
*/
public function myexam()
{
$qiniu = get_addon_config('qiniu')['cdnurl'];
$user_id = $this->auth->id;
$id = $this->request->param('id');
if(empty($id)){
$this->error('缺少必要参数');
}
// 查看用户是否有权限
$third_study = Db::name('third_study')->where('third_id',$user_id)->where('study_id',$id)->find();
if(empty($third_study)){
$this->error('您没有该场考试的权限');
}else{
$third_study['periodtime'] < time() && $this->error('学习期限已过');
}
$data['exam'] = Db::name('study')
->where('id',$id)
->find();
$data['exam']['image'] = cdnurl($data['exam']['image'],true);
$finish_hour = Db::name('study_class')
->alias('a')
->join('classes b','a.class_id = b.id')
->where('a.third_id',$user_id)
->where('a.study_id',$id)
->where('a.status',2)
->field('sum(b.class_hour) as finish_hour')
->find();
if(empty($finish_hour['finish_hour'])){
$data['exam']['finish_hour'] = 0;
}else{
$data['exam']['finish_hour'] = $finish_hour['finish_hour'];
}
//查询出所有该科目的必修课
$class_ids = Db::name('classes')->where('study_id',$id)->where('is_major',1)->column('id');
$is_end = Db::name('study_class')
->where('third_id',$user_id)
->where('study_id',$id)
->whereIn('class_id',$class_ids)
->where('status',1)
->find();
//查看用户该科目是否已经考过3次了
$exam_times = Db::name('third_exam')
->where('user_id',$user_id)
->where('study_id',$id)
->whereTime('createtime','month')
->count();
//该用户绑定该科目的有效期
$third_study = Db::name('third_study')->where('third_id',$user_id)->where('study_id',$id)->find();
if($third_study['periodtime'] < time() || $data['exam']['expirationtime']<time() || $data['exam']['finish_hour'] < $data['exam']['class_hour'] || !empty($is_end) || $exam_times >= 3){
$data['exam']['is_xian'] = 2;
$third_exam = Db::name('third_exam')
->where('user_id',$user_id)
->where('study_id',$id)
->order('exam_num desc')
->find();
}else{
$study = Db::name('study')->where('id',$id)->find();
$where_exam = [
'user_id' => $user_id,
'study_id' => $id,
'score' => ['egt',$study['pass_mark']]
];
$third_exam = Db::name('third_exam')
->where($where_exam)
->whereTime('createtime','year')
->find();
$data['exam']['is_xian'] = $third_exam ? 2 : 1;
}
// $data['exam']['expirationtime'] = date('Y-m-d H:i:s',$data['exam']['expirationtime']);
$data['exam']['expirationtime'] = date('Y-m-d H:i:s',$third_study['periodtime']);
unset($data['exam']['updatetime']);
unset($data['exam']['createtime']);
$a = $data['exam']['class_hour'] == 0 ? 0 : floor($data['exam']['finish_hour']/$data['exam']['class_hour']*100);
if($a > 100){
$a = 100;
}
$data['exam']['proportion'] = $a.'%';
$data['info'] = Db::name('study_class')
->alias('a')
->join('classes b','a.class_id = b.id')
->where('a.third_id',$user_id)
->where('a.study_id',$id)
->field('b.id,a.id as study_class_id,b.name,b.class_hour,b.is_major,b.duration,a.status,a.playtime,b.video_image')
->select();
foreach ($data['info'] as &$v){
if($v['status'] == 1){
$proportion = $v['duration'] > 0 ? floor($v['playtime']/$v['duration']*100) : 0;
$v['proportion'] = ($proportion >= 100 ? 99 : $proportion).'%';
}else{
$v['proportion'] = '100%';
}
$v['duration'] = ceil($v['duration']/60);
$v['video_image'] = cdnurl($v['video_image'],true);
}
$data['third'] = $third_exam;
$this->success('success',$data);
}
/**
* @ApiTitle (查看证书)
* @ApiSummary (查看证书)
* @ApiMethod (POST)
* @ApiRoute (/api/user/certificate)
*
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="id", type="int", required=true, description="考试记录id")
*
* @ApiReturn({
"code": 1,
"msg": "成功",
"time": "1571492001",
"data": {
"qrcodeurl": //证书图片,
"score": //分数
}
})
*/
public function certificate(){
$user_id = $this->auth->id;
$id = $this->request->param('id');
if(empty($id)){
$this->error('缺少必要参数');
}
//查询该课程的详细信息
$data = Db::name('third_exam')
->alias('a')
->join('study b','a.study_id = b.id')
->join('user c','a.user_id = c.id')
->where('a.id',$id)
->field('a.id,a.score,a.user_id,a.study_id,a.createtime,b.yinimage,b.workname,c.studynum,b.class_hour,c.image,c.card,c.nickname')
->find();
// 考试不合格,无法下载证书
if($data['score'] < 60){
$this->error('考试不合格,无法下载证书');
}
$year = date('Y',$data['createtime']);
$data['createtime'] = date('Y年m月d日',$data['createtime']);
if(empty($data['image'])){
$this->error('请先上传用户头像');
}
//证书编号
$text = 'TJAG'.$year.$data['studynum'].$id;
// 将编号存入数据库
$res = Db::name('third_exam')->where('id',$id)->update(['certificate_sn'=>$text]);
if($res === false){
$this->error('网络要挤爆啦,请稍后重试吧');
}
//将用户的头像保存到本地
$dir_user = ROOT_PATH . 'public' . DS . 'uploads/user';
if (!file_exists($dir_user)){
mkdir($dir_user,0777,true);
}
$qiniu_url = cdnurl($data['image'],true);
$image_name = $user_id.'.png';
$furl = $dir_user . DS .$image_name;
$a = file_get_contents($qiniu_url);
file_put_contents($furl,$a);
//将考试的印章图片保存到本地
$dir_qrcode = ROOT_PATH . 'public' . DS . 'uploads/qrcode';
if (!file_exists($dir_qrcode)){
mkdir($dir_qrcode,0777,true);
}
$yinimage = cdnurl($data['yinimage'],true);
$imagename = $data['study_id'].'.png';
$yinfurl = $dir_qrcode . DS .$imagename;
$b = file_get_contents($yinimage);
file_put_contents($yinfurl,$b);
//用户头像
$user_image = \think\Image::open(ROOT_PATH."public/uploads/user/$image_name");
$user_image->thumb(184,220,\think\Image::THUMB_CENTER)->save(ROOT_PATH."public/uploads/user/$image_name");
//印象头像
$yin_image = \think\Image::open(ROOT_PATH."public/uploads/qrcode/$imagename");
$yin_image->thumb(248,242,\think\Image::THUMB_CENTER)->save(ROOT_PATH."public/uploads/qrcode/$imagename");
$file_path = "$id.png";
$image = \think\Image::open(ROOT_PATH."public/assets/img/yuan.jpg");
$path_ttf = ROOT_PATH.'public/assets/fonts/PingFang.ttf';
//培训单位盖章
$text1 = '培训单位盖章';
$image->water(ROOT_PATH."public/uploads/qrcode/$imagename",[1200,602],100);
$image->water(ROOT_PATH."public/uploads/user/$image_name",[300,430],100);
$image->text($data['card'],$path_ttf,25,'#000000',[300,778]);
$image->text($text,$path_ttf,25,'#000000',[300,868]);
// $image->text($data['nickname'],$path_ttf,30,'#000000',[850,364]);
// $image->text($data['nickname'],$path_ttf,30,'#000000',[851,364]);
// $image->text($year,$path_ttf,30,'#000000',[1121,364]);
// $image->text($year,$path_ttf,30,'#000000',[1122,364]);
// $image->text($data['workname'],$path_ttf,30,'#000000',[1325,362]);
// $image->text($data['workname'],$path_ttf,30,'#000000',[1326,362]);
// $image->text($data['class_hour'],$path_ttf,30,'#000000',[1071,442]);
// $image->text($data['class_hour'],$path_ttf,30,'#000000',[1072,442]);
$str = " {$data['nickname']} 完成 {$year} 年度 {$data['workname']} 培训, {$data['class_hour']} 学时,经考核成绩合格,特发此证!";
$str = str_split_unicode($str,36);
for ($i=0; $i < count($str); $i++) {
$image->text($str[$i],$path_ttf,32,'#000000',[774,364+($i*70)]);
$image->text($str[$i],$path_ttf,32,'#000000',[775,364+($i*70)]);
}
$image->text($data['createtime'],$path_ttf,30,'#000000',[1121,832]);
$image->text($data['createtime'],$path_ttf,30,'#000000',[1122,832]);
$image->text($text1,$path_ttf,30,'#000000',[1187,772]);
$image->text($text1,$path_ttf,30,'#000000',[1188,772]);
$image->save(ROOT_PATH . 'public' . DS . 'uploads' . DS . $file_path,'png',100);
// 证书地址
$answer['qrcodeurl'] = request()->domain().'/uploads/'.$id.'.png?v='.time();
$answer['score'] = $data['score'];
$this->success('success',$answer);
}
public function get_video_first_image($video_url,$video_info){
if(empty($video_info['streams'][0]['width'])) {
$width = $video_info['streams'][1]['width'];
$height = $video_info['streams'][1]['height'];
} else {
$width = $video_info['streams'][0]['width'];
$height = $video_info['streams'][0]['height'];
}
return $video_url.'?vframe/jpg/offset/1/w/'.$width.'/h/'.$height;
}
}