zory 2 долоо хоног өмнө
parent
commit
45a65ddbe1

+ 29 - 0
app/controller/admin/Store.php

@@ -3,6 +3,8 @@
 namespace app\controller\admin;
 
 use app\extra\basic\Base;
+use app\extra\douyin\Account;
+use app\extra\douyin\Client;
 use app\extra\service\saas\StoreService;
 use app\extra\tools\CodeExtend;
 use app\middleware\AuthMiddleware;
@@ -45,6 +47,33 @@ class Store extends Base
     }
 
 
+    /**
+     * 设置path
+     * @param Request $request
+     * @return Response
+     */
+    #[Route(path: "auth",methods: "post")]
+    public function setAuth(Request $request): Response
+    {
+        try {
+            $param = $this->_valid([
+                "id.require"    => trans("empty.require"),
+            ],"post");
+            if (!is_array($param)) return error($param);
+            $store = $this->model->where("id",$param['id'])->findOrEmpty();
+            if ($store->isEmpty()) return errorTrans("empty.data");
+            // 设置path
+            $resp = (new Client)->config($this->getDyConfig())->token()->setMiniPath($store['store_id']);
+            if ($resp['err_no'] <> 0) return error($resp['err_msg']);
+            $resp = (new Account)->config($this->getDyConfig())->token()->setWhiteSetting($store['store_id'],1,true);
+            if ($resp['err_no'] <> 0) return error($resp['err_msg']);
+            return successTrans("success.data");
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
+
     /**
      * 新增/编辑代理
      * @param Request $request

+ 14 - 2
app/controller/mini/Auth.php

@@ -3,10 +3,13 @@
 namespace app\controller\mini;
 
 use app\extra\basic\Base;
+use app\extra\douyin\Client;
 use app\extra\douyin\Crypt;
 use app\extra\tools\CodeExtend;
 use app\model\saas\SaasMember;
 use app\model\saas\SaasOrderLife;
+use app\model\saas\SaasOrderLog;
+use app\model\saas\SaasStore;
 use LinFly\Annotation\Route\Controller;
 use LinFly\Annotation\Route\Route;
 use support\Request;
@@ -40,7 +43,8 @@ class Auth extends Base
     {
         try {
             $param = $request->post();
-            $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['code']);
+            print_r($param);
+            $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['auth']);
             if (!isset($sessionKey['openid'])) return error("获取数据失败");
             $openId = $sessionKey['openid'];
             $userIn = [
@@ -54,9 +58,17 @@ class Auth extends Base
             if ($user->isEmpty()) {
                 $userIn['id'] = $user->insertGetId($userIn);
             }
+            // 同步已经下单的订单
+            if (!empty($param['account'])) {
+                $this->asyncDyOrder($param['account'],$openId);
+            }
             return successTrans("success.data",[
                 "user"      => get_object_vars((new AuthMode)->guard("member")->login($userIn)),
-                "coupon"    => (new SaasOrderLife)->where("open_id",$openId)->where("status",1)->select()
+                "coupon"    => (new SaasOrderLife)->with(['goods' => function($query){
+                    $query->field("out_id,product_img");
+                }])->append(['expire_at'])->withAttr(['expire_at' => function($data,$resp){
+                    return date("Y-m-d",strtotime($resp['expire_at']));
+                }])->where("open_id",$openId)->where("status",1)->select()
             ]);
         } catch (\Throwable $throwable) {
             echo getDateFull()."==授权登陆===".$throwable->getMessage()."\n";

+ 19 - 9
app/controller/mini/Order.php

@@ -38,12 +38,9 @@ class Order extends Base
     public function getLifeOrder(Request $request): Response
     {
         try {
-            $param = $this->_valid([
-                "order.require" => "请求参数错误",
-                "code.require"  => "请求参数错误"
-            ],'post');
-            if (!is_array($param)) return error($param);
-            $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['code']);
+            $param = $request->post();
+            print_r($param);
+            $sessionKey = (new Crypt)->config($this->getDyConfig())->getSessionKey($param['auth']);
             if (empty($sessionKey)) return error("授权登录失败");
             if (!isset($sessionKey['openid'])) return error("获取数据失败");
             $openId = $sessionKey['openid'];
@@ -58,7 +55,15 @@ class Order extends Base
             if ($user->isEmpty()) {
                 $userIn['id'] = $user->insertGetId($userIn);
             }
-            $order = (new SaasOrderLife)->where("order_id",$param['order'])->where("open_id",$openId)->findOrEmpty();
+            // 同步已经下单的订单
+            if (!empty($param['account'])) {
+                $this->asyncDyOrder($param['account'],$openId);
+            }
+            $order = (new SaasOrderLife)->where("open_id",$openId)->with(['goods' => function($query){
+                $query->field("out_id,product_img");
+            }])->append(['expire_at'])->withAttr(['expire_at' => function($data,$resp){
+                return date("Y-m-d",strtotime($resp['expire_at']));
+            }])->findOrEmpty();
             if ($order->isEmpty()) return error("订单尚未同步");
             return successTrans("success.data",[
                 "user"      => get_object_vars((new AuthMode)->guard("member")->login($userIn)),
@@ -81,13 +86,18 @@ class Order extends Base
         try {
             $param = $this->_valid([
                 "order.require"     => "请求参数错误",
-                "location.require"  => "请求参数错误"
+                "location.require"  => "请求参数错误",
+                "keyword.default"   => ""
             ],'post');
             if (!is_array($param)) return error($param);
             $order = (new SaasOrderLife)->where("order_id",$param['order'])->where("open_id",$request->user['open_id'])->findOrEmpty();
             if ($order->isEmpty()) return error("订单尚未同步");
             $location = explode(",",$param['location']);
-            $resp = store_region($location[1],$location[0],2,"store_id={$order['store_id']} and agent_id={$order['agent_id']}");
+            $where = "store_id={$order['store_id']} and agent_id={$order['agent_id']}";
+            if (!empty($param['keyword'])) {
+                $where .= " and poi_name like '%".$param['keyword']."%'";
+            }
+            $resp = store_region($location[1],$location[0],2,$where);
             foreach ($resp as $key=>$val) {
                 $resp[$key] = $val;
                 $resp[$key]['poi_id'] = (string) $val['poi_id'];

+ 2 - 2
app/controller/mini/Test.php

@@ -21,8 +21,8 @@ class Test extends Base
         try {
             $uuid = ["228287898988984"];
 //            $resp = (new \app\extra\douyin\Account())->config($this->getDyConfig())->token()->setWhiteSetting("7513378475235919883",1,true);
-            $resp = (new Client)->config($this->getDyConfig())->token()->setMiniPath("7513378475235919883","1090045011490588984");
-//            $resp = (new Client)->config($this->getDyConfig())->token()->queryOrder("7513378475235919883","_000SDaHqDYY9GGnyrpYfp4vIPHD8qs32XCx");
+//            $resp = (new Client)->config($this->getDyConfig())->token()->setMiniPath("7513378475235919883");
+            $resp = (new Client)->config($this->getDyConfig())->token()->queryOrder("7513378475235919883","_000SDaHqDYY9GGnyrpYfp4vIPHD8qs32XCx");
             print_r($resp);
             return success("ok");
         } catch (\Throwable $throwable) {

+ 54 - 0
app/extra/basic/Base.php

@@ -2,7 +2,11 @@
 
 namespace app\extra\basic;
 
+use app\extra\douyin\Client;
 use app\extra\tools\CodeExtend;
+use app\model\saas\SaasOrderLife;
+use app\model\saas\SaasOrderLog;
+use app\model\saas\SaasStore;
 use app\model\system\SystemUser;
 use Overtrue\EasySms\Strategies\OrderStrategy;
 use think\Validate;
@@ -33,6 +37,56 @@ class Base
         return ["appid" => sConf('dy.appid'),'secret' => sConf('dy.secret')];
     }
 
+
+    /**
+     * 同步订单-实时
+     * @param string $account
+     * @param string $openId
+     * @return void
+     */
+    protected function asyncDyOrder(string $account = "",string $openId = "")
+    {
+        // 同步已经下单的订单
+        if (!empty($account)) {
+            $resp = (new Client)->config($this->getDyConfig())->token()->queryOrder($account,$openId);
+            $store = (new SaasStore)->where("store_id",$account)->findOrEmpty();
+            if (!empty($resp['data']['orders'])) {
+                $orderData = $orderLog = [];
+                foreach ($resp['data']['orders'] as $key=>$val) {
+                    if ($val['can_use']) {
+                        $orderEx = (new SaasOrderLife)->where("order_id",$val['order_id'])->findOrEmpty();
+                        $certificates = $val['certificates'][0];
+                        if ($orderEx->isEmpty() && !isset($certificates['lock_info_extra'])) {
+                            $orderData[$key] = [
+                                "open_id"   => $openId,
+                                "agent_id"  => $store['agent_id']??'',
+                                "store_id"  => $store['store_id']??'',
+                                "order_id"  => $val['order_id'],
+                                "pay_amount"    => $certificates['amount']['pay_amount'],
+                                "order_amount"  => $certificates['amount']['original_amount'],
+                                "expire_at"     => date("Y-m-d H:i:s",$certificates['expire_time']),
+                                "out_id"        => $certificates['sku_info']['sku_id'],
+                                "product_name"  => $certificates['sku_info']['title'],
+                                "groupon_type"  => $certificates['sku_info']['groupon_type'],
+                                "start_time"    => date("Y-m-d H:i:s",$certificates['start_time']),
+                                "status"        => 1
+                            ];
+                            $orderLog[$key] = [
+                                "order_id"  => $val['order_id'],
+                                "title"     => "发起订单",
+                                "remark"    => "下单并完成支付"
+                            ];
+                        }
+                    }
+                }
+                if (!empty($orderData)) {
+                    (new SaasOrderLife)->insertAll(array_values($orderData));
+                    (new SaasOrderLog)->insertAll(array_values($orderLog));
+                }
+            }
+        }
+    }
+
     /**
      * 写入新用户
      * @param array $param

+ 3 - 3
app/extra/douyin/Client.php

@@ -20,15 +20,15 @@ class Client extends Base
             "bind_biz_type"     => 0,
             "path_data_list"    => [
                 [
-                    "path"      => "pages/order/detail?order={$order}",
+                    "path"      => "pages/index/index?account={$account}",
                     "path_type" => 1
                 ],
                 [
-                    "path"      => "pages/order/detail?order={$order}",
+                    "path"      => "pages/index/index?account={$account}",
                     "path_type" => 2
                 ],
                 [
-                    "path"      => "pages/order/detail?order={$order}",
+                    "path"      => "pages/index/index?account={$account}",
                     "path_type" => 3
                 ]
             ]