Solution.php 12 KB

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