Locked.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. // 充值状态提醒
  30. $order = ['order_id'=> $order['order_id'],'type'=>'普通解锁订单','message' => '解锁提交成功'];
  31. return $this->renderSuccess(compact('order'));
  32. }
  33. /**
  34. * 全解
  35. * @return \think\response\Json
  36. */
  37. public function submitAll()
  38. {
  39. if (getPlatform() !== 'MP-WEIXIN') {
  40. return $this->renderError('很抱歉,解锁功能暂时仅支持微信小程序端');
  41. }
  42. // 生成订单
  43. $model = new UnlockOrder();
  44. if (!$order = $model->createOrder($this->request->param(), 2)) {
  45. return $this->renderError($model->getError() ?: '解锁提交失败');
  46. }
  47. // 充值状态提醒
  48. $order = ['order_id'=> $order['order_id'],'type'=>'普通解锁订单','message' => '解锁提交成功'];
  49. return $this->renderSuccess(compact('order'));
  50. }
  51. /**
  52. * 解锁订单支付
  53. * @param int $order_id
  54. * @return \think\response\Json
  55. * @throws \app\common\exception\BaseException
  56. * @throws \cores\exception\BaseException
  57. * @throws \think\db\exception\DataNotFoundException
  58. * @throws \think\db\exception\DbException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. */
  61. public function pay(int $order_id)
  62. {
  63. // 订单信息
  64. $model = new UnlockOrder();
  65. $model = $model::getUserOrderDetail($order_id);
  66. if (!$model) {
  67. return $this->renderError($model->getError() ?: '报名订单不存在');
  68. }
  69. // 验证
  70. if(!$model->checkPay($model))
  71. {
  72. return $this->renderError($model->getError() ?: '订单支付失败');
  73. }
  74. $pilOrder = false;
  75. $userInfo = UserService::getCurrentLoginUser(true);
  76. $userId = isset($userInfo['user_id'])? $userInfo['user_id'] : 0;
  77. if($userId != $model['user_id'])
  78. {
  79. return $this->renderError('无权操作');
  80. }
  81. // 构建微信支付
  82. try {
  83. $payment = PaymentService::wechat(
  84. $model['order_id'],
  85. $model['order_no'],
  86. $model['total_price'],
  87. OrderTypeEnum::LOCKED
  88. );
  89. } catch (\Exception $exception) {
  90. $data = ['order_no'=> 'LC'.\app\common\service\Order::createOrderNo()];
  91. $model->save($data);
  92. $payment = PaymentService::wechat(
  93. $model['order_id'],
  94. $model['order_no'],
  95. $model['total_price'],
  96. OrderTypeEnum::LOCKED
  97. );
  98. }
  99. //
  100. $message = ['success' => '解锁成功', 'error' => '订单支付中'];
  101. return $this->renderSuccess(compact('payment', 'message'));
  102. }
  103. }