Solution.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. if ($data['status'] == "FAIL") { // 审核拒绝
  37. $goods = (new SaasGoods)->where("goods_id",$data['product_id'])->findOrEmpty();
  38. if ($goods->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  39. $goods->status = 4;
  40. $goods->reason = $data['reason'];
  41. $goods->save();
  42. }
  43. }
  44. if (isset($data['type']))
  45. {
  46. $msgData = json_decode($data['msg'],true);
  47. switch ($data['type'])
  48. {
  49. case "pre_create_refund": // 退款回调
  50. $order = (new SaasOrder)->where("order_sn",$msgData['order_id'])->findOrEmpty();
  51. if ($order->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  52. if ($order['status'] <> 1) return json(['err_no' => 0,"err_tips" => "success"]);
  53. $order->status = 4; // 退款中
  54. $order->refund_msg = json_encode($msgData['refund_reason']);
  55. $order->refund_apply = getDateFull();
  56. $order->save();
  57. return json(['err_no' => 0,"err_tips" => "success","data" => [
  58. "out_refund_no" => "R".$order['out_order_no'],
  59. "order_entry_schema" => [
  60. "path" => "pages/order/detail",
  61. "params" => json_encode([
  62. "order" => $order['out_order_no']
  63. ])
  64. ],
  65. "notify_url" => "https://tran.jsshuita.cn/notify/refund"
  66. ]]);
  67. break;
  68. case "pre_create_order": // 预下单
  69. echo getDateFull()."====预下单\n";
  70. print_r($msgData);
  71. if (!isset($msgData['item_order_info_list'][0]['goods_id'])) return json(['err_no' => 0,"err_tips" => "success"]);
  72. if (empty($msgData['cp_extra'])) return json(['err_no' => 0,"err_tips" => "success"]);
  73. $cp_extra = json_decode($msgData['cp_extra'],true);
  74. $skuGoods = [];
  75. if ($cp_extra['product_id'] <> $cp_extra['sku_id']) { // 多规格下单
  76. $skuGoods = (new SaasGoodsSku)->where(['product_id' => $cp_extra['product_id'],'sku_id' => $cp_extra['sku_id']])->findOrEmpty();
  77. }
  78. $goods = (new SaasGoods)->where("goods_id",$msgData['item_order_info_list'][0]['goods_id'])->findOrEmpty();
  79. if ($goods->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  80. $order = (new SaasOrder)->where("order_sn",$msgData['order_id'])->findOrEmpty();
  81. if (!$order->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  82. $orderSn = "AL".CodeExtend::uniqidDate(18).rand(100,999);
  83. $goodsImg = is_string($goods['image_list'])?json_decode($goods['image_list'],true):$goods['image_list'];
  84. $order->insertGetId([
  85. "out_order_no" => $orderSn,
  86. "order_sn" => $msgData['order_id'],
  87. "openid" => $msgData['open_id'],
  88. "goods_id" => $goods['id'],
  89. "life_goods_id" => $goods['goods_id'],
  90. "product_id" => $goods['product_id'],
  91. "sku_id" => $cp_extra['sku_id'],
  92. "number" => $msgData['goods'][0]['quantity']??1,
  93. "price" => $msgData['item_order_info_list'][0]['price']?:$goods['price'],
  94. "custom_price" => empty($skuGoods)?$goods['custom_price']:$skuGoods['custom_price'], // 补差价金额
  95. "line_price" => $goods['line_price']*100,
  96. "poi_id" => $goods['poi_id'],
  97. "img" => $goodsImg[0]['url'],
  98. "sku_name" => $cp_extra['name'],
  99. "mobile" => $msgData['phone_num']??'',
  100. "pay_money" => $msgData['total_amount']??0,
  101. ]);
  102. return json(['err_no' => 0,"err_tips" => "success","data" => [
  103. "order_entry_schema" => [
  104. "path" => "pages/order/detail",
  105. "params" => json_encode([
  106. "order" => $orderSn
  107. ])
  108. ],
  109. "out_order_no" => $orderSn,
  110. "pay_expire_seconds" => 1800,
  111. "pay_notify_url" => "https://tran.jsshuita.cn/notify/douyin"
  112. ]]);
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. return json(['err_no' => 0,"err_tips" => "success"]);
  119. } catch (\Throwable $throwable) {
  120. echo $throwable->getMessage()."\n";
  121. echo $throwable->getLine()."\n";
  122. echo $throwable->getFile()."\n";
  123. return error($throwable->getMessage());
  124. }
  125. }
  126. #[GetMapping("token")]
  127. public function setTestToken(Request $request): Response
  128. {
  129. try {
  130. $param = $this->_valid([
  131. "code.require" => ""
  132. ]);
  133. if (!is_array($param)) return error($param);
  134. $data = (new Token)->config($this->getDyConfig())->token()->getAccessTokenUser($param['code']);
  135. return success("ok",$data);
  136. } catch (\Throwable $throwable) {
  137. return error($throwable->getMessage());
  138. }
  139. }
  140. #[GetMapping("test")]
  141. public function setTest()
  142. {
  143. try {
  144. $data = (new Relation)->config($this->getDyConfig())->token()->createOrderFirst(['1865253598118939']);
  145. return success("ok",$data);
  146. } catch (\Throwable $throwable) {
  147. return error($throwable->getMessage());
  148. }
  149. }
  150. }