Solution.php 9.0 KB

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