Solution.php 7.3 KB

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