UniversityCourse.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use app\common\controller\Api;
  5. use app\common\model\PayOrderModel;
  6. use app\common\model\UniversityCourseModel;
  7. use app\common\model\UniversityEventModel;
  8. use app\common\model\Wallet;
  9. use app\utils\CurlUtil;
  10. use think\Db;
  11. /**
  12. * 老年大学 活动板块
  13. */
  14. class UniversityCourse extends Api
  15. {
  16. protected $noNeedLogin = [''];
  17. protected $noNeedRight = ['*'];
  18. // 活动列表
  19. public function list()
  20. {
  21. $user_id = $this->auth->id;
  22. $list = UniversityCourseModel::with([
  23. 'apply' => function ($query) use ($user_id) {
  24. $query->field('id,course_id,user_id')->where('user_id', $user_id)->where('status', 1);
  25. }
  26. ])
  27. ->field('id,name,image')
  28. ->where('status', 1)
  29. ->order('weigh desc,id desc')
  30. ->autopage()
  31. ->select();
  32. foreach ($list as $k => $v) {
  33. $list[$k]['apply'] = !empty($v['apply']) ? 1 : 0;
  34. }
  35. return $this->success('success', $list);
  36. }
  37. // 活动详情
  38. public function info()
  39. {
  40. $params = $this->request->param();
  41. if (empty($params['course_id'])) {
  42. return $this->error('参数缺失');
  43. }
  44. $user_id = $this->auth->id;
  45. $info = UniversityCourseModel::with([
  46. 'apply' => function ($query) use ($user_id) {
  47. $query->field('id,course_id,user_id')->where('user_id', $user_id)->where('status', 1);
  48. }
  49. ])
  50. ->field('id,name,image,images,content')
  51. ->where('id', $params['course_id'])
  52. ->where('status', 1)
  53. ->find();
  54. $info['apply'] = !empty($info['apply']) ? 1 : 0;
  55. return $this->success('success', $info);
  56. }
  57. public function apply()
  58. {
  59. $params = $this->request->param();
  60. if (empty($params['course_id'])) {
  61. return $this->error('参数缺失');
  62. }
  63. if (empty($params['name'])) {
  64. return $this->error('报名信息姓名不能为空');
  65. }
  66. if (empty($params['phone'])) {
  67. return $this->error('报名信息手机号不能为空');
  68. }
  69. $user_id = $this->auth->id;
  70. $info = UniversityCourseModel::with([
  71. 'apply' => function ($query) use ($user_id) {
  72. $query->field('id,course_id,user_id')->where('user_id', $user_id)->where('status', 1);
  73. }
  74. ])
  75. ->field('id,name,image,images,content')
  76. ->where('id', $params['course_id'])
  77. ->where('status', 1)
  78. ->find();
  79. if (!$info) {
  80. return $this->error('课程不存在');
  81. }
  82. if (!empty($info['apply'])) {
  83. return $this->error('您已报过名了');
  84. }
  85. $nowTime = time();
  86. // 开始报名
  87. $data = [
  88. 'user_id' => $user_id,
  89. 'course_id' => $params['course_id'],
  90. 'order_no' => createUniqueNo('C', $user_id),
  91. 'name' => $params['name'],
  92. 'phone' => $params['phone'],
  93. 'status' => 1,
  94. 'create_time' => $nowTime
  95. ];
  96. Db::startTrans();
  97. if (!$apply_id = Db::name('university_course_apply')->insertGetId($data)) {
  98. Db::rollback();
  99. return $this->error('订单创建失败');
  100. }
  101. // TODO 抵扣券
  102. Db::commit();
  103. return $this->success('报名成功');
  104. }
  105. }