Locked.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\UnlockOrder;
  4. use app\api\service\Payment as PaymentService;
  5. use app\api\service\User as UserService;
  6. use app\common\enum\OrderType as OrderTypeEnum;
  7. use app\common\enum\book\order\PayType as OrderPayTypeEnum;
  8. /**
  9. * 解锁用户控制器
  10. * Class Locked
  11. * @package app\api\controller
  12. */
  13. class Locked extends Controller
  14. {
  15. /**
  16. * 解锁单个用户
  17. * @return \think\response\Json
  18. */
  19. public function submit()
  20. {
  21. if (getPlatform() !== 'MP-WEIXIN') {
  22. return $this->renderError('很抱歉,解锁功能暂时仅支持微信小程序端');
  23. }
  24. // 生成订单
  25. $model = new UnlockOrder();
  26. if (!$order = $model->createOrder($this->request->param(), 1)) {
  27. return $this->renderError($model->getError() ?: '解锁提交失败');
  28. }
  29. $payment = PaymentService::wechat(
  30. $order['order_id'],
  31. $order['order_no'],
  32. $order['total_price'],
  33. OrderTypeEnum::LOCKED
  34. );
  35. // 充值状态提醒
  36. $order = ['order_id'=> $order['order_id'],'type'=>'普通解锁订单','message' => '解锁提交成功'];
  37. return $this->renderSuccess(compact('payment','order'));
  38. }
  39. /**
  40. * 全解
  41. * @return \think\response\Json
  42. */
  43. public function submitAll()
  44. {
  45. if (getPlatform() !== 'MP-WEIXIN') {
  46. return $this->renderError('很抱歉,解锁功能暂时仅支持微信小程序端');
  47. }
  48. // 生成订单
  49. $model = new UnlockOrder();
  50. if (!$order = $model->createOrder($this->request->param(), 2)) {
  51. return $this->renderError($model->getError() ?: '解锁提交失败');
  52. }
  53. $payment = PaymentService::wechat(
  54. $order['order_id'],
  55. $order['order_no'],
  56. $order['total_price'],
  57. OrderTypeEnum::LOCKED
  58. );
  59. // 充值状态提醒
  60. $order = ['order_id'=> $order['order_id'],'type'=>'普通解锁订单','message' => '解锁提交成功'];
  61. return $this->renderSuccess(compact('payment','order'));
  62. }
  63. /**
  64. * 解锁订单支付
  65. * @param int $order_id
  66. * @return \think\response\Json
  67. * @throws \app\common\exception\BaseException
  68. * @throws \cores\exception\BaseException
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function pay(int $order_id)
  74. {
  75. // 订单信息
  76. $model = new UnlockOrder();
  77. $model = $model::getUserOrderDetail($order_id);
  78. if (!$model) {
  79. return $this->renderError($model->getError() ?: '报名订单不存在');
  80. }
  81. // 验证
  82. if(!$model->checkPay($model))
  83. {
  84. return $this->renderError($model->getError() ?: '订单支付失败');
  85. }
  86. $pilOrder = false;
  87. $userInfo = UserService::getCurrentLoginUser(true);
  88. $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0;
  89. if($userId != $model['user_id'])
  90. {
  91. return $this->renderError('无权操作');
  92. }
  93. // 构建微信支付
  94. try {
  95. $payment = PaymentService::wechat(
  96. $model['order_id'],
  97. $model['order_no'],
  98. $model['total_price'],
  99. OrderTypeEnum::LOCKED
  100. );
  101. } catch (\Exception $exception) {
  102. $data = ['order_no'=> 'LC'.\app\common\service\Order::createOrderNo()];
  103. $model->save($data);
  104. $payment = PaymentService::wechat(
  105. $model['order_id'],
  106. $model['order_no'],
  107. $model['total_price'],
  108. OrderTypeEnum::LOCKED
  109. );
  110. }
  111. //
  112. $message = ['success' => '解锁成功', 'error' => '订单支付中'];
  113. return $this->renderSuccess(compact('payment', 'message'));
  114. }
  115. }