Solution.php 8.3 KB

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