Solution.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace app\controller\api;
  3. use app\extra\basic\Base;
  4. use app\extra\dyLife\Token;
  5. use app\extra\dyMini\Relation;
  6. use app\extra\tools\CodeExtend;
  7. use app\middleware\AuthMiddleware;
  8. use app\model\saas\SaasGoods;
  9. use app\model\saas\SaasOrder;
  10. use LinFly\Annotation\Attributes\Route\Controller;
  11. use LinFly\Annotation\Attributes\Route\GetMapping;
  12. use LinFly\Annotation\Attributes\Route\RequestMapping;
  13. use support\Request;
  14. use support\Response;
  15. /**
  16. * 解决方案回传
  17. */
  18. #[Controller("/solution")]
  19. class Solution extends Base
  20. {
  21. #[RequestMapping(path: "notify")]
  22. public function getSolutionData(Request $request): Response
  23. {
  24. try {
  25. $data = $request->all();
  26. echo getDateFull()."===扩展回调\n";
  27. print_r($data);
  28. if (isset($data['operate_type'])) {
  29. if ($data['status'] == "PASS") { // 创建商品回调
  30. $goods = (new SaasGoods)->where("goods_id",$data['product_id'])->findOrEmpty();
  31. if ($goods->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  32. $goods->status = 1;
  33. $goods->save();
  34. }
  35. }
  36. if (isset($data['type']))
  37. {
  38. $msgData = json_decode($data['msg'],true);
  39. switch ($data['type'])
  40. {
  41. case "pre_create_refund": // 退款回调
  42. $order = (new SaasOrder)->where("order_sn",$msgData['order_id'])->findOrEmpty();
  43. if ($order->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  44. if ($order['status'] <> 1) return json(['err_no' => 0,"err_tips" => "success"]);
  45. $order->status = 4; // 退款中
  46. $order->refund_msg = json_encode($msgData['refund_reason']);
  47. $order->refund_apply = getDateFull();
  48. $order->save();
  49. return json(['err_no' => 0,"err_tips" => "success","data" => [
  50. "out_refund_no" => "R".$order['out_order_no'],
  51. "order_entry_schema" => [
  52. "path" => "pages/order/detail",
  53. "params" => json_encode([
  54. "order" => $order['out_order_no']
  55. ])
  56. ],
  57. "notify_url" => "https://tran.jsshuita.cn/notify/refund"
  58. ]]);
  59. break;
  60. case "pre_create_order": // 预下单
  61. if (!isset($msgData['item_order_info_list'][0]['goods_id'])) return json(['err_no' => 0,"err_tips" => "success"]);
  62. $goods = (new SaasGoods)->where("goods_id",$msgData['item_order_info_list'][0]['goods_id'])->findOrEmpty();
  63. if ($goods->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  64. $order = (new SaasOrder)->where("order_sn",$msgData['order_id'])->findOrEmpty();
  65. if (!$order->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  66. $orderSn = "AL".CodeExtend::uniqidDate(18).rand(100,999);
  67. $goodsImg = is_string($goods['image_list'])?json_decode($goods['image_list'],true):$goods['image_list'];
  68. $order->insertGetId([
  69. "out_order_no" => $orderSn,
  70. "order_sn" => $msgData['order_id'],
  71. "openid" => $msgData['open_id'],
  72. "goods_id" => $goods['id'],
  73. "life_goods_id" => $goods['goods_id'],
  74. "product_id" => $goods['product_id'],
  75. "sku_id" => "",
  76. "number" => $msgData['goods'][0]['quantity']??1,
  77. "price" => $msgData['item_order_info_list'][0]['price']?:$goods['price'],
  78. "line_price" => $goods['line_price']*100,
  79. "poi_id" => $goods['poi_id'],
  80. "img" => $goodsImg[0]['url'],
  81. "sku_name" => "",
  82. "mobile" => $msgData['phone_num']??'',
  83. "pay_money" => $msgData['total_amount']??0,
  84. ]);
  85. return json(['err_no' => 0,"err_tips" => "success","data" => [
  86. "order_entry_schema" => [
  87. "path" => "pages/order/detail",
  88. "params" => json_encode([
  89. "order" => $orderSn
  90. ])
  91. ],
  92. "out_order_no" => $orderSn,
  93. "pay_expire_seconds" => 1800,
  94. "pay_notify_url" => "https://tran.jsshuita.cn/notify/douyin"
  95. ]]);
  96. break;
  97. default:
  98. break;
  99. }
  100. }
  101. return json(['err_no' => 0,"err_tips" => "success"]);
  102. } catch (\Throwable $throwable) {
  103. echo $throwable->getMessage()."\n";
  104. echo $throwable->getLine()."\n";
  105. echo $throwable->getFile()."\n";
  106. return error($throwable->getMessage());
  107. }
  108. }
  109. #[GetMapping("token")]
  110. public function setTestToken(Request $request): Response
  111. {
  112. try {
  113. $param = $this->_valid([
  114. "code.require" => ""
  115. ]);
  116. if (!is_array($param)) return error($param);
  117. $data = (new Token)->config($this->getDyConfig())->token()->getAccessTokenUser($param['code']);
  118. return success("ok",$data);
  119. } catch (\Throwable $throwable) {
  120. return error($throwable->getMessage());
  121. }
  122. }
  123. #[GetMapping("test")]
  124. public function setTest()
  125. {
  126. try {
  127. $data = (new Relation)->config($this->getDyConfig())->token()->createOrderFirst(['1865253598118939']);
  128. return success("ok",$data);
  129. } catch (\Throwable $throwable) {
  130. return error($throwable->getMessage());
  131. }
  132. }
  133. }