Lesson.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 售课
  7. */
  8. class Lesson extends Api
  9. {
  10. // 无需登录的接口,*表示全部
  11. protected $noNeedLogin = [];
  12. // 无需鉴权的接口,*表示全部
  13. protected $noNeedRight = ['*'];
  14. //售课列表
  15. public function lists(){
  16. $list = Db::name('lesson')->where('is_show',1)->order('weigh desc')->autopage()->select();
  17. $list = list_domain_image($list,['image']);
  18. $list = $this->list_lang($list,['name','content']);
  19. $this->success(1,$list);
  20. }
  21. //售课套餐列表
  22. public function package_list(){
  23. $lesson_id = input('lesson_id',0);
  24. //套餐列表
  25. $list = Db::name('lesson_package')->alias('p')
  26. ->field('p.*,e.image')
  27. ->join('lesson e','p.lesson_id = e.id','LEFT')
  28. ->where('p.lesson_id',$lesson_id)
  29. ->where('p.is_show',1)
  30. ->order('p.weigh desc')->select();
  31. $list = list_domain_image($list,['image']);
  32. $list = $this->list_lang($list,['name','validity','activeremark']);
  33. $this->success(1,$list);
  34. }
  35. //售课套餐详情
  36. public function package_info(){
  37. $package_id = input('package_id',0);
  38. //套餐详情
  39. $list = Db::name('lesson_package')->alias('p')
  40. ->field('p.*,e.image')
  41. ->join('lesson e','p.lesson_id = e.id','LEFT')
  42. ->where('p.id',$package_id)
  43. ->order('p.weigh desc')->find();
  44. $list = info_domain_image($list,['image']);
  45. $list = $this->info_lang($list,['name','validity','activeremark']);
  46. $this->success(1,$list);
  47. }
  48. //下单购买售课套餐
  49. public function package_buy(){
  50. $package_id = input('package_id',0);
  51. $package_info = Db::name('lesson_package')->where('id',$package_id)->find();
  52. //套餐
  53. $data = [
  54. 'order_no' => createUniqueNo('P',$this->auth->id),
  55. 'user_id' => $this->auth->id,
  56. 'package_id' => $package_id,
  57. 'lesson_id' => $package_info['lesson_id'],
  58. 'sessions' => $package_info['sessions'],
  59. //'starttime' => ,
  60. 'days' => $package_info['days'],
  61. //'endtime' => ,
  62. 'price' => $package_info['price'],
  63. 'remain' => $package_info['sessions'],
  64. 'order_status'=> 0,
  65. 'paytime' => 0,
  66. 'createtime' => time(),
  67. 'updatetime' => 0,
  68. 'is_gift' => 0,
  69. ];
  70. //如果有赠品
  71. if(!empty($package_info['gift_lesson_id']) && !empty($package_info['gift_sessions'])){
  72. $gift = $data;
  73. //订单号不换了
  74. $gift['lesson_id'] = $package_info['gift_lesson_id'];
  75. $gift['sessions'] = $package_info['gift_sessions'];
  76. $gift['remain'] = $package_info['gift_sessions'];
  77. $gift['is_gift'] = 1;
  78. }
  79. //支付订单下单
  80. $pay_order = [];
  81. $pay_order['user_id'] = $data['user_id'];
  82. $pay_order['out_trade_no'] = $data['order_no'];
  83. $pay_order['order_amount'] = $data['price'];
  84. $pay_order['createtime'] = $data['createtime'];
  85. $pay_order['pay_type'] = 'hitpay';
  86. $pay_order['platform'] = 'app';
  87. $pay_order['order_status'] = 0;
  88. $pay_order['table_name'] = 'package_order';
  89. $pay_order['table_id'] = 0;
  90. $pay_order['args'] = '';
  91. //入库
  92. Db::startTrans();
  93. $order_id = Db::name('package_order')->insertGetId($data);
  94. if(!$order_id){
  95. $this->error('下单失败');
  96. Db::rollback();
  97. }
  98. $gift_order_id = Db::name('package_order')->insertGetId($gift);
  99. if(!$gift_order_id){
  100. $this->error('下单失败');
  101. Db::rollback();
  102. }
  103. $pay_order['table_id'] = $order_id;
  104. $pay_order_id = Db::name('pay_order')->insertGetId($pay_order);
  105. if(!$pay_order_id){
  106. $this->error('下单失败');
  107. Db::rollback();
  108. }
  109. Db::commit();
  110. $this->success(1);
  111. }
  112. }