Certificate.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Gold
  5. * Date: 2024/1/24
  6. * Time: 8:41
  7. */
  8. namespace app\api\controller;
  9. use app\admin\model\books\Books; //教材
  10. use app\admin\model\books\Certificate as CertificateModel;
  11. use app\common\controller\Api;
  12. class Certificate extends Api
  13. {
  14. protected $noNeedLogin = [];
  15. protected $noNeedRight = [];
  16. /**
  17. * 我的体验卡
  18. *
  19. */
  20. public function getMyCertificate(){
  21. $data = input();
  22. if(!isset($data['page']) || $data['page'] == '' || $data['page'] == null){
  23. $page = 1;
  24. }else{
  25. $page = $data['page'];
  26. }
  27. if(!isset($data['limit']) || $data['limit'] == '' || $data['limit'] == null){
  28. $limit = 20;
  29. }else{
  30. $limit = $data['limit'];
  31. }
  32. $where = [];
  33. if(isset($data['is_expire']) && $data['is_expire'] !== '' && $data['is_expire'] !== null){
  34. $where['is_expire'] = $data['is_expire'];
  35. }
  36. $uid = $this->auth->id;
  37. $CertificateModel = new CertificateModel();
  38. $list = $CertificateModel->where($where)->where('is_deleted',1)->with(['goods'=>function($query){
  39. $query->field('id,title,image');
  40. }])->where('user_id',$uid)->order('id','desc')->paginate([
  41. 'list_rows'=> $limit,
  42. 'page' => $page,
  43. ]);
  44. $this->success('操作成功',$list);
  45. }
  46. /**
  47. * 使用体验卡
  48. *
  49. */
  50. public function useCertificate(){
  51. $data = input();
  52. $uid = $this->auth->id;
  53. $date = date('Y-m-d H:i:s');
  54. // $date_end = date("Y-m-d H:i:s", strtotime("3 day"));
  55. $CertificateModel = new CertificateModel();
  56. $find = $CertificateModel->where('user_id',$uid)->where('id',$data['id'])->where('is_deleted',1)->find();
  57. $date_end = date("Y-m-d H:i:s", strtotime($find['free_day']." day"));
  58. if(!$find){
  59. $this->error('体验卡不存在');
  60. }
  61. if($find['effective_time'] < $date){
  62. $this->error('体验卡已过期');
  63. }
  64. if($find['status'] == 1){
  65. $this->error('该体验卡已使用');
  66. }
  67. $arr = [
  68. 'free_start_time' => $date,
  69. 'free_end_time' => $date_end,
  70. 'status' => 1
  71. ];
  72. if($CertificateModel->where('id',$data['id'])->update($arr)){
  73. $this->success('操作成功');
  74. }
  75. }
  76. }