Solution.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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\extra\tools\LifeWeb;
  9. use app\middleware\AuthMiddleware;
  10. use app\model\saas\SaasGoods;
  11. use app\model\saas\SaasGoodsSku;
  12. use app\model\saas\SaasLivePlanGoods;
  13. use app\model\saas\SaasLivePlanStar;
  14. use app\model\saas\SaasOrder;
  15. use app\model\saas\SaasOrderItem;
  16. use app\model\saas\SaasOrderVerify;
  17. use app\model\saas\SaasStar;
  18. use app\model\saas\SaasStore;
  19. use LinFly\Annotation\Attributes\Route\Controller;
  20. use LinFly\Annotation\Attributes\Route\GetMapping;
  21. use LinFly\Annotation\Attributes\Route\RequestMapping;
  22. use support\Request;
  23. use support\Response;
  24. use Webman\RedisQueue\Redis;
  25. use Workerman\Timer;
  26. use yzh52521\EasyHttp\Http;
  27. /**
  28. * 解决方案回传
  29. */
  30. #[Controller("/solution")]
  31. class Solution extends Base
  32. {
  33. #[RequestMapping(path: "notify")]
  34. public function getSolutionData(Request $request): Response
  35. {
  36. try {
  37. $data = $request->all();
  38. echo getDateFull()."===扩展回调\n";
  39. print_r($data);
  40. if (isset($data['operate_type'])) {
  41. if ($data['status'] == "PASS") { // 创建商品回调
  42. $goods = (new SaasGoods)->where("goods_id",$data['product_id'])->findOrEmpty();
  43. if ($goods->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  44. $goods->status = 1;
  45. $goods->save();
  46. }
  47. if ($data['status'] == "FAIL") { // 审核拒绝
  48. $goods = (new SaasGoods)->where("goods_id",$data['product_id'])->findOrEmpty();
  49. if ($goods->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  50. $goods->status = 4;
  51. $goods->reason = $data['reason'];
  52. $goods->save();
  53. }
  54. }
  55. if (isset($data['type']))
  56. {
  57. $msgData = json_decode($data['msg'],true);
  58. switch ($data['type'])
  59. {
  60. case "pre_create_refund": // 退款回调
  61. $order = (new SaasOrder)->where("order_sn",$msgData['order_id'])->findOrEmpty();
  62. if ($order->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  63. if (!in_array($order['status'],[1,6])) return json(['err_no' => 0,"err_tips" => "success"]);
  64. $order->status = 4; // 退款中
  65. $order->refund_msg = json_encode($msgData['refund_reason']);
  66. $order->refund_apply = getDateFull();
  67. $order->save();
  68. // 推送审核机制
  69. echo getDateFull()."===推送审核\n";
  70. Redis::send("audit-order",['order' => $msgData['order_id']],60);
  71. return json(['err_no' => 0,"err_tips" => "success","data" => [
  72. "out_refund_no" => "R".$order['out_order_no'],
  73. "order_entry_schema" => [
  74. "path" => "pages/order/detail",
  75. "params" => json_encode([
  76. "order" => $order['out_order_no']
  77. ])
  78. ],
  79. "notify_url" => "https://tran.jsshuita.cn/notify/refund"
  80. ]]);
  81. break;
  82. case "pre_create_order": // 预下单
  83. echo getDateFull()."====预下单\n";
  84. print_r($msgData);
  85. if (!isset($msgData['item_order_info_list'][0]['goods_id'])) return json(['err_no' => 0,"err_tips" => "success"]);
  86. if (empty($msgData['cp_extra'])) return json(['err_no' => 0,"err_tips" => "success"]);
  87. $cp_extra = json_decode($msgData['cp_extra'],true);
  88. $skuGoods = [];
  89. if ($cp_extra['product_id'] <> $cp_extra['sku_id']) { // 多规格下单
  90. $skuGoods = (new SaasGoodsSku)->where(['product_id' => $cp_extra['product_id'],'sku_id' => $cp_extra['sku_id']])->findOrEmpty();
  91. }
  92. $goods = (new SaasGoods)->where("goods_id",$msgData['item_order_info_list'][0]['goods_id'])->findOrEmpty();
  93. if ($goods->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  94. $order = (new SaasOrder)->where("order_sn",$msgData['order_id'])->findOrEmpty();
  95. if (!$order->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
  96. $orderSn = "AL".$msgData['order_id'];
  97. $goodsImg = is_string($goods['image_list'])?json_decode($goods['image_list'],true):$goods['image_list'];
  98. $order->insertGetId([
  99. "out_order_no" => $orderSn,
  100. "item_order_id" => $msgData['item_order_info_list'][0]['item_order_id']?:'0',
  101. "order_sn" => $msgData['order_id'],
  102. "openid" => $msgData['open_id'],
  103. "goods_id" => $goods['id'],
  104. "life_goods_id" => $goods['goods_id'],
  105. "product_id" => $goods['product_id'],
  106. "sku_id" => $cp_extra['sku_id'],
  107. "number" => $msgData['goods'][0]['quantity']??1,
  108. "price" => $msgData['item_order_info_list'][0]['price']?:$goods['price'],
  109. "custom_price" => empty($skuGoods)?$goods['custom_price']:$skuGoods['custom_price'], // 补差价金额
  110. "line_price" => $goods['line_price']*100,
  111. "poi_id" => $goods['poi_id'],
  112. "img" => $goodsImg[0]['url'],
  113. "sku_name" => $cp_extra['name'],
  114. "scene" => $cp_extra['scene']??'',
  115. "mobile" => $msgData['phone_num']??'',
  116. "pay_money" => $msgData['total_amount']??0,
  117. "is_show" => 0,
  118. "is_new" => 1, // 三方码订单
  119. ]);
  120. $itemData = [];
  121. foreach ($msgData['item_order_info_list'] as $key=>$val) {
  122. $itemData[$key] = [
  123. "poi_id" => $goods['poi_id'],
  124. "order_sn" => $msgData['order_id'],
  125. "item_order_id" => $val['item_order_id'],
  126. "price" => $val['price']
  127. ];
  128. }
  129. if (!empty($itemData)) {
  130. (new SaasOrderItem)->insertAll($itemData);
  131. }
  132. return json(['err_no' => 0,"err_tips" => "success","data" => [
  133. "order_entry_schema" => [
  134. "path" => "pages/order/detail",
  135. "params" => json_encode([
  136. "order" => $orderSn
  137. ])
  138. ],
  139. "out_order_no" => $orderSn,
  140. "pay_expire_seconds" => 1800,
  141. "pay_notify_url" => "https://tran.jsshuita.cn/notify/douyin",
  142. "cp_delivery_type" => 1
  143. ]]);
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. return json(['err_no' => 0,"err_tips" => "success"]);
  150. } catch (\Throwable $throwable) {
  151. echo $throwable->getMessage()."\n";
  152. echo $throwable->getLine()."\n";
  153. echo $throwable->getFile()."\n";
  154. return error($throwable->getMessage());
  155. }
  156. }
  157. #[GetMapping("token")]
  158. public function setTestToken(Request $request): Response
  159. {
  160. try {
  161. $param = $this->_valid([
  162. "code.require" => ""
  163. ]);
  164. if (!is_array($param)) return error($param);
  165. $data = (new Token)->config($this->getDyConfig())->token()->getAccessTokenUser($param['code']);
  166. return success("ok",$data);
  167. } catch (\Throwable $throwable) {
  168. return error($throwable->getMessage());
  169. }
  170. }
  171. #[GetMapping("test")]
  172. public function setTest(Request $request): Response
  173. {
  174. try {
  175. // $param = $this->_valid([
  176. // "order.require" => trans("empty.require")
  177. // ],$request->method());
  178. // if (!is_array($param)) return error($param);
  179. // Redis::send("sync-order-info",['order' => $param['order']],30);
  180. // return success("ok");
  181. // $order = (new SaasOrder)->where("order_sn",$param['order'])->findOrEmpty();
  182. // if ($order->isEmpty()) return error("数据不存在");
  183. // [$state,$resp] = (new LifeWeb)->token()->getOrderData($param['order']);
  184. // if (!$state) return error($resp);
  185. // $order->douyin_uid = $resp['douyin_uid'];
  186. // $order->life_fee = $resp['life_fee'];
  187. // $order->star_fee = $resp['star_fee'];
  188. // $order->save();
  189. // return successTrans("success.data",$resp);
  190. // $data = (new Relation)->config($this->getDyConfig())->token()->createOrderFirst(['1865253598118939']);
  191. // $data = (new SaasOrderVerify)->where("status",0)->with(['orders'])->select();
  192. // $data = (new SaasOrder)->where('status',2)->whereNull("douyin_uid")->select();
  193. $data = (new SaasOrder)->where('status','in',[1,2,3,6])->select();
  194. if ($data->isEmpty()) return error("err");
  195. $upData = [];
  196. foreach ($data as $key=>$item) {
  197. $storeRate = (new SaasStore)->where("poi_id",$item["poi_id"])->value("life_rate");
  198. if (empty($storeRate)) $storeRate = 7.6;
  199. $systemFee = ($storeRate - 6) / 100 * $item['price'];
  200. $upData[$key] = [
  201. "sys_fee" => ceil($systemFee),
  202. "id" => $item['id']
  203. ];
  204. }
  205. if (!empty($upData)) {
  206. (new SaasOrder)->saveAll(array_values($upData));
  207. }
  208. return success("ok",$upData);
  209. // $param = $this->_valid([
  210. // "order.require" => trans("empty.require")
  211. // ],$request->method());
  212. // if (!is_array($param)) return error($param);
  213. // $order = (new SaasOrder)->where("order_sn",$param['order'])->findOrEmpty();
  214. // if ($order->isEmpty()) return errorTrans("empty.data");
  215. // $douyin = (new SaasStar)->where("uid",$order['douyin_uid'])->value("unique_id");
  216. // $productRate = $this->getDyUidProduct($douyin,$order['life_goods_id']);
  217. // return success("ok",$productRate);
  218. } catch (\Throwable $throwable) {
  219. echo $throwable->getMessage()."\n";
  220. echo $throwable->getFile()."\n";
  221. echo $throwable->getLine()."\n";
  222. return error($throwable->getMessage());
  223. }
  224. }
  225. protected function getDyUidProduct(string $dyUid = "",string $goodsId = ""): array
  226. {
  227. $starPlan = (new SaasLivePlanStar)->where("unique_id",$dyUid)->column("plan_id");
  228. if (empty($starPlan)) return [];
  229. print_r($starPlan);
  230. $productIds = (new SaasLivePlanGoods)->where("plan_id","in",$starPlan)->column("goods_id,commission_rate,plan_id");
  231. if (empty($productIds)) return [];
  232. $goodsReturn = [];
  233. foreach ($productIds as $key=>$productId) {
  234. if ($productId['goods_id'] == $goodsId) {
  235. $goodsReturn[$key] = $productId;
  236. break;
  237. }
  238. }
  239. return $goodsReturn;
  240. }
  241. }