Solution.php 9.6 KB

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