Order.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace app\controller\api;
  3. use app\extra\basic\Base;
  4. use app\extra\dyMini\Pay;
  5. use app\middleware\AuthMiddleware;
  6. use app\model\saas\SaasOrder;
  7. use app\service\saas\OrderService;
  8. use DI\Attribute\Inject;
  9. use LinFly\Annotation\Attributes\Route\Controller;
  10. use LinFly\Annotation\Attributes\Route\GetMapping;
  11. use LinFly\Annotation\Attributes\Route\Middleware;
  12. use LinFly\Annotation\Attributes\Route\PostMapping;
  13. use support\Request;
  14. use support\Response;
  15. #[Controller("/dy/order"),Middleware(AuthMiddleware::class)]
  16. class Order extends Base
  17. {
  18. #[Inject]
  19. protected SaasOrder $model;
  20. #[Inject]
  21. protected OrderService $service;
  22. #[GetMapping('list')]
  23. public function getDataList(Request $request): Response
  24. {
  25. try {
  26. $param = $request->all();
  27. if (!empty($param['size'])) {
  28. $param['pageSize'] = $param['size'];
  29. }
  30. $param['openid'] = $request->user['openid'];
  31. $data = $this->service->setModel()->getList($param);
  32. return successTrans("success.data",pageFormat($data));
  33. } catch (\Throwable $th) {
  34. return error($th->getMessage());
  35. }
  36. }
  37. #[GetMapping("confirm")]
  38. public function confirmOrder(Request $request): Response
  39. {
  40. try {
  41. $param = $this->_valid([
  42. "order.require" => trans("empty.require")
  43. ],$request->method());
  44. if (!is_array($param)) return error($param);
  45. $order = $this->model->where("order_sn",$param['order'])->with(['product','poi'])->findOrEmpty();
  46. if ($order->isEmpty()) return errorTrans("empty.data");
  47. if ($order['status'] <> 0) return errorTrans("empty.data");
  48. return $this->encode("ok",$order->toArray());
  49. } catch (\Throwable $throwable) {
  50. return error($throwable->getMessage());
  51. }
  52. }
  53. /**
  54. * 发起支付
  55. * @param Request $request
  56. * @return Response
  57. */
  58. #[PostMapping("toPay")]
  59. public function orderPay(Request $request): Response
  60. {
  61. try {
  62. $param = $this->_valid([
  63. "order.require" => trans("empty.require"),
  64. "mobile.default" => ""
  65. ],$request->method());
  66. if (!is_array($param)) return error($param);
  67. $order = $this->model->where("order_sn",$param['order'])->with(['product','poi'])->findOrEmpty();
  68. $payParam = [
  69. "order_sn" => $order['order_sn'],
  70. "total" => $order['price'],
  71. "name" => $order['product']['product_name'],
  72. "notify_url" => ""
  73. ];
  74. $byteAuthorization = (new Pay)->config([
  75. "appid" => sConf("wechat.mini_appid"),
  76. "secret" => sConf("wechat.mini_secret"),
  77. "salt" => sConf("wechat.mch_salt"),
  78. ])->createOrder($payParam);
  79. if (!empty($param['mobile'])) {
  80. $order->mobile = $param['mobile'];
  81. $order->save();
  82. }
  83. return success("ok",['pay' => $byteAuthorization]);
  84. } catch (\Throwable $throwable) {
  85. return error($throwable->getMessage());
  86. }
  87. }
  88. }