Browse Source

获取c端商城相关的接口

15313670163 1 week ago
parent
commit
5c10e41aa5

+ 226 - 2
app/JsonRpc/NewsService.php

@@ -6,10 +6,13 @@ use App\Model\ArticleData;
 use App\Model\Category;
 use App\Model\Website;
 use App\Model\WebsiteCategory;
+use App\Model\Good;
 use Hyperf\DbConnection\Db;
 use Hyperf\RpcServer\Annotation\RpcService;
 use App\Tools\Result;
 use App\Model\ArticleSurvey;
+use App\Model\District;
+use App\Tools\buildMenuTree;
 #[RpcService(name: "NewsService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class NewsService implements NewsServiceInterface
 {
@@ -486,8 +489,8 @@ class NewsService implements NewsServiceInterface
             'article.copyfrom',
             'article.catid')
         ->orderBy("updated_at", "desc")
-        ->limit($data['pageSize'])
         ->offset(($data['page'] - 1) * $data['pageSize'])
+        ->limit($data['pageSize'])
         ->get()
         ->map(function ($article ) use ($data) {
             $catid = $article->catid ?? 0;
@@ -1370,6 +1373,227 @@ class NewsService implements NewsServiceInterface
            
         return Result::success($result);
     }
+    // 封装处理数据的函数
+    function processGoods($goods, $data) {
+        return $goods->map(function ($good) use ($data) {
+            $catid = $good->catid ?? 0;
+            $pinyin = '';
+            $category = WebsiteCategory::where('category_id', $catid)->where('website_id', $data['website_id'])->first();
+            if (!empty($category->pid) && $category->pid != 0) {
+                $level = json_decode($category->category_arr_id);
+                $pinyin = WebsiteCategory::whereIn('category_id', $level)
+                    ->orderByRaw('FIELD(category_id, '. implode(',', $level). ')')
+                    ->get(['aLIas_pinyin'])
+                    ->pluck('aLIas_pinyin')
+                    ->implode('/');
+            } else {
+                $pinyin = $category->aLIas_pinyin ?? '';
+            }
+            if(isset($good->city_id) &&!empty($good->city_id)){
+                $city = District::where('id', $good->city_id)->first(['name']);
+                $good->city_name = $city->name ?? '';
+            }
+            // 解析imgurl JSON并取第一条数据
+            $imgUrls = json_decode($good->imgurl, true);
+            $good->imgurl = !empty($imgUrls) ? $imgUrls[0] : null;
+            $good->pinyin = $pinyin;
+            return $good;
+        });
+    }
+    /**
+     * 获取商品模块
+     * @param array $data
+     * @return array
+     *  */
+    public function getWebsiteshop(array $data): array
+    {
+        $input['id'] = $data['id'];
+        $input['website_id'] = $data['website_id'];
+        // return Result::success($input);
+        $data = json_decode($input['id'] ?? '', true) ?? [];
+        $result = array_map(function ($item) use ($input) {
+            // 检查parent元素是否存在且不是undefined
+            if (isset($item['level']) && $item['level'] != 'undefined' && $item['level']!= "") {
+                list($Levelid, $goodStart,$goodNum) = explode(',', $item['level']);
+                $website = $input['website_id'];
+                $query = Good::where('good.status', 2)
+                    ->where('good.website_id', $website);
+                switch ($Levelid) {
+                    case 1:
+                    case 2:
+                    case 3:
+                        $goods = $query->where(function($q) use ($Levelid) {
+                            $q->whereRaw("JSON_CONTAINS(good.level, '". intval($Levelid). "') = 0")
+                              ->orWhereRaw("JSON_CONTAINS(good.level, '\"". intval($Levelid). "\"') = 0");
+                        });
+                        break;
+                    case 4:
+                        $goods = $query;
+                        break;
+                    case 5:
+                        $goods = $query->where('type_id',1);
+                        break;
+                    case 6:
+                        $goods = $query->where('type_id',2);
+                            
+                        break;
+                    default:
+                        return [];
+                }
+                $all_goods = $goods
+                    ->select('good.id', 'good.name', 'good.imgurl', 'good.description',
+                     'good.updated_at', 'good.catid','good.type_id','good.price','good.level',
+                     'good.website_id')
+                    ->orderBy('updated_at','desc')
+                    ->offset($goodStart)
+                    ->limit($goodNum)
+                    ->get();
+                $all_goods = $this->processGoods($all_goods, $input);
+            }
+            return  $all_goods;
+        }, $data);
+        return Result::success($result);
+    }
+    /**
+     * 获取商品分类
+     * @param array $data
+     * @return array
+     *  */
+    public function getWebsiteshopCat(array $data): array
+    {
+        $category = WebsiteCategory::where('website_id', $data['website_id'])
+            ->whereRaw("JSON_CONTAINS(category_arr_id, '". intval($data['id']). "') = 1")
+            ->orWhereRaw("JSON_CONTAINS(category_arr_id, '\"". intval($data['id']). "\"') = 1")
+            ->select('category_id', 'alias','aLIas_pinyin','pid','category_arr_id')
+            ->orderBy('sort')
+            ->get()
+            ->map(function ($item) use ($data) {
+                $pinyin = [];
+                $level = json_decode($item->category_arr_id);
+                // 递归获取所有父级栏目的拼音
+                if($item->pid!= 0){
+                    $pinyin = WebsiteCategory::whereIn('category_id', $level)
+                        ->orderByRaw('FIELD(category_id, '.implode(',', $level).')')
+                        ->get(['aLIas_pinyin'])
+                        ->pluck('aLIas_pinyin')
+                        ->implode('/'); 
+                }else{
+                    $pinyin = $item->aLIas_pinyin;
+                }
+                $item->aLIas_pinyin = $pinyin;
+                return $item;
+            });
+        $goods = Good::where('website_id', $data['website_id'])
+            ->where('status', 2)
+            ->select('good.id as good_id', 'name','imgurl','description','updated_at','catid','type_id','website_id')
+            ->latest('updated_at')
+            ->offset(($data['page']-1)*$data['pageSize'])
+            ->limit($data['pageSize'])
+            ->get();
+            $goods = $this->processGoods($goods, $data);
+        if(empty($category)){
+            return Result::error("栏目查询失败", 0);
+        }
+        if(empty($goods)){
+            return Result::error("商品查询失败", 0);
+        }
+        // 调用Tool中的buildMenuTree方法构建菜单树
+        $cat_tree = Result::buildMenuTree($category);
+        $result = [
+            'category' => $cat_tree,
+            'goods' => $goods,
+        ];
+        // $resul['goods'] = $goods;
+        if(empty($result)){
+            return Result::error("查询失败", 0); 
+        }
+        return Result::success($result);
+    }
+    /*
+     * 获取商品列表
+     * @param array $data
+     * @return array
+     *  */
+    public function getWebsiteshopList(array $data): array
+    {
+        // return Result::success($data);
+        $where = [
+            'status' => 2,
+            'website_id' => $data['website_id'],
+        ];
+        if((empty($data['catid']) ||!isset($data['catid'])) && (empty($data['keyword']) ||!isset($data['keyword'])) && (empty($data['city_id']) || !isset($data['city_id']))){
+            return Result::error("查询失败", 0);
+        }
+        if ((empty($data['catid']) || !isset($data['catid'])) && (!empty($data['city_id']) || isset($data['city_id']))) {
+            $category = WebsiteCategory::where('website_id', $data['website_id'])->where('pid',$data['id'])->orderBy('sort')->first(['category_id']);
+            $data['catid'] = $category->category_id ??  0;
+        }
+        if(isset($data['keyword']) &&!empty($data['keyword'])){
+           array_push($where, ['name', 'like', '%'. $data['keyword'].'%']);
+        }
+        if(isset($data['type_id']) && !empty($data['type_id'])){
+            array_push($where, ['type_id', $data['type_id']]);
+        }
+        $query = Good::where($where)
+            ->when(isset($data['catid']) &&!empty($data['catid']), function ($query) use ($data) {
+                $query->where(function($q) use ($data) {
+                    $q->WhereRaw("JSON_CONTAINS(good.cat_arr_id, '". intval($data['catid']). "') = 1")
+                    ->orWhereRaw("JSON_CONTAINS(good.cat_arr_id, '\"". intval($data['catid']). "\"') = 1");
+                });
+            })
+            ->when(isset($data['city_id']) && !empty($data['city_id']), function ($query) use ($data) {
+                $query->where(function($q) use ($data) {
+                    $q->WhereRaw("JSON_CONTAINS(good.city_arr_id, '". intval($data['city_id']). "') = 1")
+                        ->orWhereRaw("JSON_CONTAINS(good.city_arr_id, '\"". intval($data['city_id']). "\"') = 1");
+                });
+            })
+            ->select('good.id', 'good.name', 'good.imgurl', 'good.description', 'good.updated_at', 
+            'good.catid','good.type_id','good.website_id','good.cat_arr_id','good.created_at','good.city_id')
+            ->latest('updated_at');
+            // 获取 type_id 为 1 的数据
+            $result['type1'] = $this->processGoods(
+                $query->clone()
+                    ->where('type_id', 1)
+                    ->offset(($data['page'] - 1) * $data['pageSize'])
+                    ->limit($data['pageSize'])
+                    ->get(),
+                $data
+            );
+            // 获取 type_id 为 2 的数据
+            $result['type2'] = $this->processGoods(
+                $query->clone()
+                    ->where('type_id', 2)
+                    ->offset(($data['page'] - 1) * $data['pageSize'])
+                    ->limit($data['pageSize'])
+                    ->get(),
+                $data
+            );
+        if(empty($result)){
+            return Result::error("查询失败", 0); 
+        }
 
-
+        return Result::success($result);
+    }
+    /**
+     * 获取商品详情
+     * @param array $data
+     * @return array
+     *  */
+    public function getWebsiteshopInfo(array $data): array
+    {
+        $where = [
+            'good.status' => 2,
+            'good.website_id' => $data['website_id'],
+        ]; 
+        $goods = Good::where($where)
+            ->where('good.id', $data['id'])
+            ->leftJoin('website_category', 'website_category.category_id', 'good.catid')
+            ->select('good.*','website_category.alias','website_category.category_id')
+            ->first();
+        if(empty($goods)){
+            return Result::error("查询失败", 0); 
+        }
+        $goods->imgurl = json_decode($goods->imgurl, true);
+        return Result::success($goods);
+    }
 }

+ 5 - 0
app/JsonRpc/NewsServiceInterface.php

@@ -142,4 +142,9 @@ interface NewsServiceInterface
      * @return array
      */
     public function getWebsiteArticles(array $data):array;
+    /**
+     * @param array $data
+     * @return array
+     */
+    public function getWebsiteshop(array $data):array;
 }

+ 27 - 0
app/Model/District.php

@@ -0,0 +1,27 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Model;
+
+use Hyperf\DbConnection\Model\Model;
+
+/**
+ */
+class District extends Model
+{
+    /**
+     * The table associated with the model.
+     */
+    protected ?string $table = 'district';
+
+    /**
+     * The attributes that are mass assignable.
+     */
+    protected array $fillable = [];
+
+    /**
+     * The attributes that should be cast to native types.
+     */
+    protected array $casts = [];
+}

+ 27 - 0
app/Model/Good.php

@@ -0,0 +1,27 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Model;
+
+use Hyperf\DbConnection\Model\Model;
+
+/**
+ */
+class Good extends Model
+{
+    /**
+     * The table associated with the model.
+     */
+    protected ?string $table = 'good';
+
+    /**
+     * The attributes that are mass assignable.
+     */
+    protected array $fillable = [];
+
+    /**
+     * The attributes that should be cast to native types.
+     */
+    protected array $casts = [];
+}

+ 5 - 0
app/Model/WebsiteCategory.php

@@ -24,4 +24,9 @@ class WebsiteCategory extends Model
      * The attributes that should be cast to native types.
      */
     protected array $casts = [];
+    public function children()
+    {
+        return $this->hasMany(WebsiteCategory::class, 'pid', 'category_id');
+    }
+
 }

+ 22 - 0
app/Tools/Result.php

@@ -23,4 +23,26 @@ class Result
             'data' => $data,
         ];
     }
+    /**
+     * 递归查询
+     * @param $menuItems
+     * @param $parentId
+     * @return array
+     */
+    public static  function buildMenuTree($menuItems, $parentId = 0) {
+        $tree = [];
+        foreach ($menuItems as $item) {
+            if ($item['pid'] == $parentId) {
+                // 找到子菜单
+                $children = self::buildMenuTree($menuItems, $item['category_id']);
+                // 如果子菜单存在,则添加到当前菜单的children中
+                if ($children) {
+                    $item['children'] = $children;
+                }
+                // 将当前菜单添加到树中
+                $tree[] = $item;
+            }
+        }
+        return $tree;
+    }
 }