Zory 1 週間 前
コミット
1c211af51f

+ 1 - 1
app/controller/admin/Category.php

@@ -36,7 +36,7 @@ class Category extends Base
     public function getStoreCategory(Request $request): Response
     {
         try {
-            $data = $this->service->setModel()->getList($request->all(),null,true);
+            $data = $this->service->setModel()->getList($request->all(),null,false);
             $tree = DataExtend::arr2tree($data,"category_id","parent_id");
             return successTrans(100010,$tree,200);
         } catch (\Throwable $th) {

+ 26 - 1
app/controller/admin/Store.php

@@ -5,6 +5,7 @@ namespace app\controller\admin;
 use app\extra\basic\Base;
 use app\middleware\AuthMiddleware;
 use app\model\saas\SaasStore;
+use app\service\saas\GoodsService;
 use app\service\saas\StoreService;
 use DI\Attribute\Inject;
 use LinFly\Annotation\Attributes\Route\Controller;
@@ -28,6 +29,9 @@ class Store extends Base
     #[Inject]
     protected StoreService $service;
 
+    #[Inject]
+    protected GoodsService $goodsService;
+
     #[Inject]
     protected SaasStore $model;
 
@@ -41,7 +45,17 @@ class Store extends Base
     public function getStoreData(Request $request): Response
     {
         try {
-            $data = $this->service->setModel()->getList($request->all());
+            $data = $this->service->setModel()->getList($request->all(),null,true,['type'],['type' => function($query,$resp){
+                if (empty($resp['product_type'])) return [];
+                $productType = json_decode($resp['product_type'],true);
+                $typeData = [];
+                foreach ($this->goodsService->productType() as $key => $value) {
+                    if (in_array($value['key'],$productType)) {
+                        $typeData[] = $value['name'];
+                    }
+                }
+                return $typeData;
+            }]);
             return successTrans(100010,pageFormat($data),200);
         } catch (\Throwable $th) {
             return error($th->getMessage());
@@ -118,4 +132,15 @@ class Store extends Base
         }
     }
 
+    #[GetMapping("cate")]
+    public function getCategory(): Response
+    {
+        try {
+            $data = $this->goodsService->productType();
+            return successTrans("success",compact('data'));
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
 }

+ 187 - 0
app/controller/merchant/Goods.php

@@ -0,0 +1,187 @@
+<?php
+
+namespace app\controller\merchant;
+
+use app\extra\basic\Base;
+use app\extra\dyLife\data\BaseData;
+use app\extra\tools\CodeExtend;
+use app\middleware\AuthMiddleware;
+use app\model\saas\SaasGoods;
+use app\model\saas\SaasGoodsSku;
+use app\model\saas\SaasStore;
+use app\service\saas\GoodsService;
+use DI\Attribute\Inject;
+use LinFly\Annotation\Attributes\Route\Controller;
+use LinFly\Annotation\Attributes\Route\GetMapping;
+use LinFly\Annotation\Attributes\Route\Middleware;
+use LinFly\Annotation\Attributes\Route\PostMapping;
+use support\Request;
+use support\Response;
+
+
+#[Controller("/api/merchant/goods"),Middleware(AuthMiddleware::class)]
+class Goods extends Base
+{
+
+    #[Inject]
+    protected GoodsService $service;
+
+    #[Inject]
+    protected SaasGoods $model;
+
+    #[Inject]
+    protected SaasGoodsSku $skuModel;
+
+    #[Inject]
+    protected SaasStore $store;
+
+    #[GetMapping('list')]
+    public function getDataList(Request $request): Response
+    {
+        try {
+            $param = $request->all();
+            $param['poi_id'] = $request->user['store_id'];
+            $data = $this->service->setModel()->getList($request->all());
+            return successTrans(100010,pageFormat($data),200);
+        } catch (\Throwable $th) {
+            return error($th->getMessage());
+        }
+    }
+
+    /**
+     * 商品详情
+     * @param Request $request
+     * @return Response
+     */
+    #[GetMapping('detail')]
+    public function getGoodDetail(Request $request): Response
+    {
+        try {
+            $param = $this->_valid([
+                "id.require"            => trans("empty.require"),
+                "product_id.require"    => trans("empty.require"),
+            ],$request->method());
+            if (!is_array($param)) return error($param);
+            $detail = $this->model->where("id",$param["id"])->with(['skuSpecs'])->findOrEmpty();
+            if ($detail->isEmpty()) return errorTrans("empty.data");
+            if ($detail['poi_id'] <> $request->user['store_id']) return errorTrans("error.param");
+            $detail['sold_start_times'] = [(string)$detail['sold_start_time'],(string)$detail['sold_end_time']];
+            $detail['use_times'] = [(string)$detail['use_time_start'],(string)$detail['use_time_end']];
+            return successTrans('success.data',$detail->toArray());
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
+    #[PostMapping("template")]
+    public function getTemplate(Request $request): Response
+    {
+        try {
+            $param = $this->_valid([
+                "product_type.require"  => trans("empty.require"),
+                "category.require"      => trans("empty.require"),
+            ],$request->method());
+            if (!is_array($param)) return error($param);
+            $data = (new BaseData)->config([
+                "appid"     => sConf("wechat.appid"),
+                "secret"    => sConf("wechat.secret"),
+                "account"   => sConf("wechat.shop_id"),
+            ])->token()->getStoreCategoryTemplate($param['category'][2],$param['product_type']);
+            return success("ok",$data);
+        } catch (\Throwable $throwable) {
+            return error($throwable->getMessage());
+        }
+    }
+
+    #[GetMapping('type')]
+    public function getProductType(Request $request): Response
+    {
+        try {
+            $poiId = $request->user['store_id'];
+            $productType = $this->store->where("poi_id", $poiId)->value("product_type");
+            if (empty($productType)) return error("未配置");
+            $typeData = [];
+            foreach ($this->service->productType() as $key => $value) {
+                if (in_array($value['key'],json_decode($productType,true))) {
+                    $typeData[] = $value;
+                }
+            }
+            return success('ok', $typeData);
+        } catch (\Throwable $th) {
+            return error($th->getMessage());
+        }
+    }
+
+    /**
+     * 新增商品
+     * @param Request $request
+     * @return Response
+     */
+    #[PostMapping("save")]
+    public function saveGoods(Request $request): Response
+    {
+        try {
+            $param = $request->post();
+            if (empty($param['auto_renew'])) $param['auto_renew'] = 0;
+            $param['image_list'] = empty($param['image_list']) ? [] : json_encode($param['image_list']);
+            $param['detail_image_list'] = empty($param['detail_image_list']) ? [] : json_encode($param['detail_image_list']);
+            $param['environment_image_list'] = empty($param['environment_image_list']) ? [] : json_encode($param['environment_image_list']);
+            if (is_array($param['specs'])) $param['specs'] = json_encode($param['specs']);
+            if (is_array($param['category'])) {
+                $param['category_id'] = $param['category'][2]??0;
+                $param['category'] = json_encode($param['category']);
+            }
+            if (!isset($param['id'])) {
+                $param['product_id'] = strtoupper(CodeExtend::random(18,3));
+            }
+            $param['price'] = $param['price']*100;
+            $param['poi_id'] = $request->user['store_id'];
+            $skuData = [];
+            if (is_array($param['skuSpecs'])) {
+                foreach ($param['skuSpecs'] as $key => $value) {
+                    $skuData[$key] = $value;
+                    $skuData[$key]['product_id'] = $param['product_id'];
+                    $skuData[$key]['specs'] = json_encode($value['specs']);
+                }
+            }
+            unset($param['skuSpecs']);
+            $param['line_price'] = $param['line_price'] * 100;
+
+            if (!empty($skuData)) {
+                [$state,$msg] = $this->skuData($skuData);
+                if (!$state) return error($msg);
+            }
+            $state = $this->model->setAutoData($param);
+            if (!$state) return errorTrans("error.data");
+            return successTrans("success.data");
+        } catch (\Throwable $throwable) {
+            echo $throwable->getLine()."\n";
+            return error($throwable->getMessage());
+        }
+    }
+
+    /**
+     * SKU编辑
+     * @param array $skuData
+     * @return array
+     */
+    protected function skuData(array $skuData = []): array
+    {
+        try {
+            if (empty($skuData)) return [0,trans('empty.require')];
+            foreach ($skuData as $vo) {
+                $sku = $this->skuModel->where("sku_id",$vo['sku_id'])->findOrEmpty();
+                if ($sku->isEmpty()) {
+                    $sku->insertGetId($vo);
+                } else {
+                    $sku->update($vo);
+                }
+            }
+            return [1,'success'];
+        } catch (\Throwable $throwable) {
+            return [0,$throwable->getMessage()];
+        }
+    }
+
+
+}

+ 1 - 0
app/extra/basic/Model.php

@@ -20,6 +20,7 @@ class Model extends \think\Model
                 $state = $this->strict(false)->insertGetId($data);
             }
         } catch (\Throwable $throwable) {
+            echo $throwable->getMessage()."\n";
             return false;
         }
         return $state;

+ 12 - 10
app/extra/basic/Service.php

@@ -104,29 +104,31 @@ class Service
     /**
      *
      */
-    public function getList(array $param = [],$with = null,bool $all = false)
+    public function getList(array $param = [],$with = null,bool $page = true,array $append = [],$withAttr = null)
     {
         $model = $this->searchVal($param,$this->searchFilter($param));
         // 支持多种类型的 $with 参数
         if ($with !== null) {
             if (is_callable($with)) {
-                // 如果是函数,调用它
                 $with($model);
-            } elseif (is_string($with)) {
-                // 如果是字符串,直接 with
-                $model->with($with);
-            } elseif (is_array($with)) {
-                // 如果是数组,也直接 with
+            } elseif (is_string($with) || is_array($with)) {
                 $model->with($with);
             }
         }
-        if ($all) {
-            $resp = $model->select()->toArray();
-        } else {
+        // 3. 动态设置修改器(withAttr)
+        if ($withAttr !== null) {
+            $model->withAttr($withAttr);
+        }
+        if (!empty($append)) {
+            $model->append($append);
+        }
+        if ($page) {
             $resp = $model->paginate([
                 "list_rows" => $param['pageSize'],
                 "page"      => $param['page']
             ]);
+        } else {
+            $resp = $model->select()->toArray();
         }
         return $resp;
     }

+ 1 - 0
app/extra/dyLife/data/BaseData.php

@@ -43,6 +43,7 @@ class BaseData extends BasicLife
     public function getStoreCategoryTemplate(string $category_id,int $product_type = 1): array
     {
         $data = compact("category_id","product_type");
+        print_r($data);
         return $this->curlGetApi("goodlife/v1/goods/template/get/",$data);
     }
 

+ 96 - 0
app/model/saas/SaasGoods.php

@@ -0,0 +1,96 @@
+<?php
+
+namespace app\model\saas;
+
+use app\extra\basic\Model;
+use think\model\relation\HasMany;
+
+
+/**
+ * @property integer $id (主键)
+ * @property mixed $product_id 本地商品ID
+ * @property mixed $goods_id 来客商品ID
+ * @property mixed $store_id 来客id
+ * @property mixed $poi_id 
+ * @property string $product_name 
+ * @property mixed $image_list 商品头图
+ * @property mixed $category 商品ID组
+ * @property mixed $category_id 
+ * @property integer $settle_type 收款方式
+ * @property integer $show_channel 投放渠道
+ * @property integer $limit_use_rule 库存类型
+ * @property integer $auto_renew 自动延期
+ * @property integer $use_date_type 顾客可消费日期类型
+ * @property integer $day_duration 指定天数
+ * @property integer $can_no_use_date 顾客不可消费日期类型
+ * @property integer $code_source_type 券码类型
+ * @property integer $limit_rule_type 限购规则
+ * @property integer $limit_rule 限购数量
+ * @property integer $booking_date 预约时间
+ * @property integer $booking_type 预约规则
+ * @property integer $booking_unit 预约单位
+ * @property integer $rec_person_type 使用张数限制
+ * @property integer $rec_person_num_max 限制数量
+ * @property integer $mult_sku 多SKU开关
+ * @property integer $product_type 商品类型
+ * @property string $sold_start_time 售卖日期
+ * @property string $sold_end_time 售卖日期
+ * @property integer $line_price 划线价
+ * @property integer $price 销售价
+ * @property mixed $detail_image_list 辅助图
+ * @property mixed $environment_image_list 环境图
+ * @property mixed $specs 商品搭配
+ * @property integer $status 状态
+ * @property mixed $create_at
+ */
+class SaasGoods extends Model
+{
+    /**
+     * The connection name for the model.
+     *
+     * @var string|null
+     */
+    protected $connection = 'mysql';
+    
+    /**
+     * The table associated with the model.
+     *
+     * @var string
+     */
+    protected string $table = "saas_goods";
+    
+    /**
+     * The primary key associated with the table.
+     *
+     * @var string
+     */
+    protected string $primaryKey = "id";
+    
+    /**
+     * Indicates if the model should be timestamped.
+     *
+     * @var bool
+     */
+    public bool $timestamps = false;
+
+    public function setPriceAttr($value): int
+    {
+        return $value * 100;
+    }
+
+    public function getPriceAttr($value): float
+    {
+        return $value / 100;
+    }
+
+    public function getLinePriceAttr($value): float
+    {
+        return $value / 100;
+    }
+
+    public function skuSpecs(): HasMany
+    {
+        return $this->hasMany(SaasGoodsSku::class, 'product_id', 'product_id');
+    }
+
+}

+ 73 - 0
app/model/saas/SaasGoodsSku.php

@@ -0,0 +1,73 @@
+<?php
+
+namespace app\model\saas;
+
+use app\extra\basic\Model;
+
+
+/**
+ * @property integer $id (主键)
+ * @property integer $goods_id 来客商品ID
+ * @property mixed $sku_id 
+ * @property mixed $product_id 本地商品ID
+ * @property string $name 
+ * @property string $image 
+ * @property integer $line_price 
+ * @property integer $price 
+ * @property mixed $specs 
+ * @property integer $is_default 
+ * @property mixed $create_at
+ */
+class SaasGoodsSku extends Model
+{
+    /**
+     * The connection name for the model.
+     *
+     * @var string|null
+     */
+    protected $connection = 'mysql';
+    
+    /**
+     * The table associated with the model.
+     *
+     * @var string
+     */
+    protected string $table = "saas_goods_sku";
+    
+    /**
+     * The primary key associated with the table.
+     *
+     * @var string
+     */
+    protected string $primaryKey = "id";
+    
+    /**
+     * Indicates if the model should be timestamped.
+     *
+     * @var bool
+     */
+    public bool $timestamps = false;
+
+
+    public function setPriceAttr($value): int
+    {
+        return $value * 100;
+    }
+
+    public function getPriceAttr($value): float
+    {
+        return $value / 100;
+    }
+
+
+    public function setLinePriceAttr($value): int
+    {
+        return $value * 100;
+    }
+
+    public function getLinePriceAttr($value): float
+    {
+        return $value / 100;
+    }
+
+}

+ 4 - 2
app/service/saas/GoodsService.php

@@ -3,6 +3,7 @@
 namespace app\service\saas;
 
 use app\extra\basic\Service;
+use app\model\saas\SaasGoods;
 
 class GoodsService extends Service
 {
@@ -29,7 +30,7 @@ class GoodsService extends Service
      */
     public function setModel()
     {
-        $this->mode = (new SaasCategory);
+        $this->mode = (new SaasGoods);
         return $this;
     }
 
@@ -43,7 +44,8 @@ class GoodsService extends Service
     {
         $filter = [];
         !empty($param['status']) && $filter[] = ["enable", '=', $param['status']];
-        !empty($param['name']) && $filter[] = ["name", 'like', "%{$param['name']}%"];
+        !empty($param['poi_id']) && $filter[] = ["poi_id", '=', $param['poi_id']];
+        !empty($param['name']) && $filter[] = ["product_name", 'like', "%{$param['name']}%"];
         return $filter;
     }