Studyplan.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\controller;
  12. use app\admin\model\study\Plan;
  13. use app\admin\model\study\PlanSteps;
  14. use app\admin\model\study\PlanItems;
  15. use app\wap\model\special\Special as SpecialModel;
  16. use app\wap\model\store\StoreOrder;
  17. use service\JsonService;
  18. use think\Url;
  19. /**商品控制器
  20. * Class Store
  21. * @package app\wap\controller
  22. */
  23. class Studyplan extends AuthController{
  24. /*
  25. * 白名单
  26. * */
  27. public static function WhiteList(){
  28. return [
  29. 'index',
  30. 'detail',
  31. ];
  32. }
  33. public function index($page = 1, $limit = 20, $op = ''){
  34. if ($op == 'list') {
  35. $model = Plan::alias('p')->where('p.is_del', 0)->page((int)$page, (int)$limit);
  36. $model = $model->join('PlanBuy pb', 'pb.uid = ' . $this->uid .' and pb.planid = p.id ', 'left');
  37. $orderList = $model->order('p.shelf_time DESC')->field('p.*, pb.id as isbuy')->select();
  38. $orderList = count($orderList) > 0 ? $orderList->toArray() : [];
  39. return JsonService::successful($orderList);
  40. }
  41. //微信支付
  42. $user_level = !$this->uid ? 0 : $this->userInfo;
  43. $this->assign('is_member', isset($user_level['level']) ? $user_level['level'] : 0);
  44. return $this->fetch();
  45. }
  46. public function details($id = 0) {
  47. if (!$id) $this->failed('缺少参数,无法访问', Url::build('index/index'));
  48. $planinfo = Plan::get($id);
  49. if (!$planinfo) $this->failed('参数错误,无法访问', Url::build('index/index'));
  50. $planinfo['introduction'] = htmlspecialchars_decode($planinfo['introduction']);
  51. $steps = PlanSteps::where('is_del', 0)->where('pid', $id)->select();
  52. $stepids = [];
  53. foreach($steps as $v){
  54. $stepids[] = $v['id'];
  55. }
  56. $itemsList = [];
  57. $join = [
  58. ['special s', 's.id = it.cid and s.is_del = 0 and s.status = 1']
  59. ];
  60. $items = PlanItems::alias('it')->field('s.*, it.stepsid,it.pid')
  61. ->join($join)
  62. ->where('pid', $id)
  63. ->where('stepsid', 'in', $stepids)
  64. ->select();
  65. foreach ($items as $v) {
  66. $v['count'] = SpecialModel::numberChapters($v['type'], $v['id']);
  67. $itemsList[$v['stepsid']][] = $v->toArray();
  68. }
  69. //微信支付相关代码
  70. $user_level = !$this->uid ? 0 : $this->userInfo;
  71. $this->assign('is_member', isset($user_level['level']) ? $user_level['level'] : 0);
  72. $this->assign('steps', json_encode($steps));
  73. $this->assign('itemsList', json_encode($itemsList));
  74. $this->assign('planinfo', json_encode($planinfo));
  75. return $this->fetch();
  76. }
  77. /**
  78. * 创建专题支付订单
  79. * @param int $special_id 专题id
  80. * @param int $pay_type 购买类型 1=礼物,2=普通购买,3=开团或者拼团
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. * @throws \think\exception\DbException
  84. */
  85. public function create_order($special_id, $pay_type_num, $payType, $pinkId = 0, $total_num = 1, $link_pay_uid = 0, $from = 'weixin')
  86. {
  87. if (!$special_id) return JsonService::fail('缺少购买参数');
  88. if ($pay_type_num == -1) return JsonService::fail('选择购买方式');
  89. $Plan = Plan::where('is_del', 0)->where('is_show', 1)->find($special_id);
  90. if (!$Plan) return JsonService::status('ORDER_ERROR', '购买的学习计划不存在');
  91. $order = StoreOrder::createPlanOrder($Plan, $pinkId, $pay_type_num, $this->uid, $payType, $link_pay_uid, $total_num);
  92. $orderId = $order['order_id'];
  93. $info = compact('orderId');
  94. if ($orderId) {
  95. $orderInfo = StoreOrder::where('order_id', $orderId)->find();
  96. if (!$orderInfo || !isset($orderInfo['paid'])) return JsonService::status('pay_error', '支付订单不存在!');
  97. if ($orderInfo['paid']) return JsonService::status('pay_error', '支付已支付!');
  98. if (bcsub((float)$orderInfo['pay_price'], 0, 2) <= 0) {
  99. if (StoreOrder::jsPayPlanPrice($orderId, $this->uid))
  100. return JsonService::status('success', '支付成功', $info);
  101. else
  102. return JsonService::status('pay_error', StoreOrder::getErrorInfo());
  103. } else {
  104. switch ($payType) {
  105. case 'weixin':
  106. try {
  107. if ($from == 'weixinh5') {
  108. $jsConfig = StoreOrder::h5PlanPay($orderId);
  109. } else {
  110. $jsConfig = StoreOrder::jsPlanPay($orderId);
  111. }
  112. } catch (\Exception $e) {
  113. return JsonService::status('pay_error', $e->getMessage(), $info);
  114. }
  115. $info['jsConfig'] = $jsConfig;
  116. if ($from == 'weixinh5') {
  117. return JsonService::status('wechat_h5_pay', '订单创建成功', $info);
  118. } else {
  119. return JsonService::status('wechat_pay', '订单创建成功', $info);
  120. }
  121. break;
  122. case 'yue':
  123. if (StoreOrder::yuePlanPay($orderId, $this->uid))
  124. return JsonService::status('success', '余额支付成功', $info);
  125. else
  126. return JsonService::status('pay_error', StoreOrder::getErrorInfo());
  127. break;
  128. case 'zhifubao':
  129. $info['pay_price'] = $orderInfo['pay_price'];
  130. $info['orderName'] = '购买学习计划';
  131. return JsonService::status('zhifubao_pay', '订单创建成功', base64_encode(json_encode($info)));
  132. break;
  133. }
  134. }
  135. } else {
  136. return JsonService::fail(StoreOrder::getErrorInfo('订单生成失败!'));
  137. }
  138. }
  139. }