| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace app\controller\api;
- use app\extra\basic\Base;
- use app\extra\dyLife\data\OrderData;
- use app\extra\dyLife\Token;
- use app\extra\dyMini\Relation;
- use app\extra\tools\CodeExtend;
- use app\extra\tools\LifeWeb;
- use app\middleware\AuthMiddleware;
- use app\model\saas\SaasGoods;
- use app\model\saas\SaasGoodsSku;
- use app\model\saas\SaasLivePlanGoods;
- use app\model\saas\SaasLivePlanStar;
- use app\model\saas\SaasOrder;
- use app\model\saas\SaasOrderItem;
- use app\model\saas\SaasOrderVerify;
- use app\model\saas\SaasStar;
- use LinFly\Annotation\Attributes\Route\Controller;
- use LinFly\Annotation\Attributes\Route\GetMapping;
- use LinFly\Annotation\Attributes\Route\RequestMapping;
- use support\Request;
- use support\Response;
- use Webman\RedisQueue\Redis;
- use Workerman\Timer;
- use yzh52521\EasyHttp\Http;
- /**
- * 解决方案回传
- */
- #[Controller("/solution")]
- class Solution extends Base
- {
- #[RequestMapping(path: "notify")]
- public function getSolutionData(Request $request): Response
- {
- try {
- $data = $request->all();
- // echo getDateFull()."===扩展回调\n";
- // print_r($data);
- if (isset($data['operate_type'])) {
- if ($data['status'] == "PASS") { // 创建商品回调
- $goods = (new SaasGoods)->where("goods_id",$data['product_id'])->findOrEmpty();
- if ($goods->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
- $goods->status = 1;
- $goods->save();
- }
- if ($data['status'] == "FAIL") { // 审核拒绝
- $goods = (new SaasGoods)->where("goods_id",$data['product_id'])->findOrEmpty();
- if ($goods->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
- $goods->status = 4;
- $goods->reason = $data['reason'];
- $goods->save();
- }
- }
- if (isset($data['type']))
- {
- $msgData = json_decode($data['msg'],true);
- switch ($data['type'])
- {
- case "pre_create_refund": // 退款回调
- $order = (new SaasOrder)->where("order_sn",$msgData['order_id'])->findOrEmpty();
- if ($order->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
- if (!in_array($order['status'],[1,6])) return json(['err_no' => 0,"err_tips" => "success"]);
- $order->status = 4; // 退款中
- $order->refund_msg = json_encode($msgData['refund_reason']);
- $order->refund_apply = getDateFull();
- $order->save();
- return json(['err_no' => 0,"err_tips" => "success","data" => [
- "out_refund_no" => "R".$order['out_order_no'],
- "order_entry_schema" => [
- "path" => "pages/order/detail",
- "params" => json_encode([
- "order" => $order['out_order_no']
- ])
- ],
- "notify_url" => "https://tran.jsshuita.cn/notify/refund"
- ]]);
- break;
- case "pre_create_order": // 预下单
- echo getDateFull()."====预下单\n";
- print_r($msgData);
- if (!isset($msgData['item_order_info_list'][0]['goods_id'])) return json(['err_no' => 0,"err_tips" => "success"]);
- if (empty($msgData['cp_extra'])) return json(['err_no' => 0,"err_tips" => "success"]);
- $cp_extra = json_decode($msgData['cp_extra'],true);
- $skuGoods = [];
- if ($cp_extra['product_id'] <> $cp_extra['sku_id']) { // 多规格下单
- $skuGoods = (new SaasGoodsSku)->where(['product_id' => $cp_extra['product_id'],'sku_id' => $cp_extra['sku_id']])->findOrEmpty();
- }
- $goods = (new SaasGoods)->where("goods_id",$msgData['item_order_info_list'][0]['goods_id'])->findOrEmpty();
- if ($goods->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
- $order = (new SaasOrder)->where("order_sn",$msgData['order_id'])->findOrEmpty();
- if (!$order->isEmpty()) return json(['err_no' => 0,"err_tips" => "success"]);
- $orderSn = "AL".CodeExtend::uniqidDate(18).rand(100,999);
- $goodsImg = is_string($goods['image_list'])?json_decode($goods['image_list'],true):$goods['image_list'];
- $order->insertGetId([
- "out_order_no" => $orderSn,
- "order_sn" => $msgData['order_id'],
- "openid" => $msgData['open_id'],
- "goods_id" => $goods['id'],
- "life_goods_id" => $goods['goods_id'],
- "product_id" => $goods['product_id'],
- "sku_id" => $cp_extra['sku_id'],
- "number" => $msgData['goods'][0]['quantity']??1,
- "price" => $msgData['item_order_info_list'][0]['price']?:$goods['price'],
- "custom_price" => empty($skuGoods)?$goods['custom_price']:$skuGoods['custom_price'], // 补差价金额
- "line_price" => $goods['line_price']*100,
- "poi_id" => $goods['poi_id'],
- "img" => $goodsImg[0]['url'],
- "sku_name" => $cp_extra['name'],
- "scene" => $cp_extra['scene']??'',
- "mobile" => $msgData['phone_num']??'',
- "pay_money" => $msgData['total_amount']??0,
- ]);
- $itemData = [];
- foreach ($msgData['item_order_info_list'] as $key=>$val) {
- $itemData[$key] = [
- "poi_id" => $goods['poi_id'],
- "order_sn" => $msgData['order_id'],
- "item_order_id" => $val['item_order_id'],
- "price" => $val['price']
- ];
- }
- if (!empty($itemData)) {
- (new SaasOrderItem)->insertAll($itemData);
- }
- return json(['err_no' => 0,"err_tips" => "success","data" => [
- "order_entry_schema" => [
- "path" => "pages/order/detail",
- "params" => json_encode([
- "order" => $orderSn
- ])
- ],
- "out_order_no" => $orderSn,
- "pay_expire_seconds" => 1800,
- "pay_notify_url" => "https://tran.jsshuita.cn/notify/douyin"
- ]]);
- break;
- default:
- break;
- }
- }
- return json(['err_no' => 0,"err_tips" => "success"]);
- } catch (\Throwable $throwable) {
- echo $throwable->getMessage()."\n";
- echo $throwable->getLine()."\n";
- echo $throwable->getFile()."\n";
- return error($throwable->getMessage());
- }
- }
- #[GetMapping("token")]
- public function setTestToken(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "code.require" => ""
- ]);
- if (!is_array($param)) return error($param);
- $data = (new Token)->config($this->getDyConfig())->token()->getAccessTokenUser($param['code']);
- return success("ok",$data);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[GetMapping("test")]
- public function setTest(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "order.require" => trans("empty.require")
- ],$request->method());
- if (!is_array($param)) return error($param);
- Redis::send("sync-order-info",['order' => $param['order']],30);
- return success("ok");
- // $order = (new SaasOrder)->where("order_sn",$param['order'])->findOrEmpty();
- // if ($order->isEmpty()) return error("数据不存在");
- // [$state,$resp] = (new LifeWeb)->token()->getOrderData($param['order']);
- // if (!$state) return error($resp);
- // $order->douyin_uid = $resp['douyin_uid'];
- // $order->life_fee = $resp['life_fee'];
- // $order->star_fee = $resp['star_fee'];
- // $order->save();
- // return successTrans("success.data",$resp);
- // $data = (new Relation)->config($this->getDyConfig())->token()->createOrderFirst(['1865253598118939']);
- // $data = (new SaasOrderVerify)->where("status",0)->with(['orders'])->select();
- // $data = (new SaasOrder)->where('status',2)->whereNull("douyin_uid")->select();
- // $data = (new SaasOrder)->where('status','in',[1,2,3,6])->select();
- // if ($data->isEmpty()) return error("err");
- // $upData = [];
- // foreach ($data as $key=>$item) {
- // // 查询订单详情
- // [$state,$resp] = (new LifeWeb)->token()->getOrderData($item['order_sn']);
- // if (!$state) {
- // echo getDateFull()."==={$item['order_sn']}===订单获取失败\n";
- // } else {
- // echo getDateFull()."==={$item['order_sn']}===执行更新\n";
- // print_r($resp);
- // $upData[$key] = [
- // "douyin_uid" => $resp['douyin_uid'],
- // "star_fee" => $resp['star_fee'],
- // "life_fee" => $resp['life_fee'],
- // "id" => $item['id']
- // ];
- // }
- // Timer::sleep(1.5);
- // }
- // if (!empty($upData)) {
- // (new SaasOrder)->saveAll(array_values($upData));
- // }
- // return success("ok",$upData);
- // $param = $this->_valid([
- // "order.require" => trans("empty.require")
- // ],$request->method());
- // if (!is_array($param)) return error($param);
- // $order = (new SaasOrder)->where("order_sn",$param['order'])->findOrEmpty();
- // if ($order->isEmpty()) return errorTrans("empty.data");
- // $douyin = (new SaasStar)->where("uid",$order['douyin_uid'])->value("unique_id");
- // $productRate = $this->getDyUidProduct($douyin,$order['life_goods_id']);
- // return success("ok",$productRate);
- } catch (\Throwable $throwable) {
- echo $throwable->getMessage()."\n";
- echo $throwable->getFile()."\n";
- echo $throwable->getLine()."\n";
- return error($throwable->getMessage());
- }
- }
- protected function getDyUidProduct(string $dyUid = "",string $goodsId = ""): array
- {
- $starPlan = (new SaasLivePlanStar)->where("unique_id",$dyUid)->column("plan_id");
- if (empty($starPlan)) return [];
- print_r($starPlan);
- $productIds = (new SaasLivePlanGoods)->where("plan_id","in",$starPlan)->column("goods_id,commission_rate,plan_id");
- if (empty($productIds)) return [];
- $goodsReturn = [];
- foreach ($productIds as $key=>$productId) {
- if ($productId['goods_id'] == $goodsId) {
- $goodsReturn[$key] = $productId;
- break;
- }
- }
- return $goodsReturn;
- }
- }
|