zhanglinxin 1 年之前
父节点
当前提交
053a2acb8a

+ 5 - 5
application/api/controller/Base.php

@@ -51,13 +51,13 @@ class Base extends Controller
         $actionname = strtolower($this->request->action());
         $this->checking($modulename, $controllername, $actionname);
 
-        $this->page = input('page', 1);
-        $this->page_num = input('page_num', 20);
-        $this->off_set = $this->page * $this->page_num - $this->page_num;
+        $page = input('page');
+        $page_num = input('page_num');
+        $this->page = $page ? $page : 1;
+        $this->page_num = $page_num ? $page_num : 20;
+        $this->off_set = ($this->page * $this->page_num) - $this->page_num;
         $this->is_test = input('test', 0);// 测试用的
         $path = explode('/', $this->request->path());
-        if (input('user_id')) $this->user_id = input('user_id');
-        if (input('user')) $this->user_id = input('user');
         if (!empty($this->need_login) && in_array(end($path), $this->need_login)) $this->checkLogin();
     }
 

+ 48 - 0
application/api/controller/Goods.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\service\GoodsService;
+use hg\apidoc\annotation as Apidoc;
+
+/**
+ * @Apidoc\Title("商品")
+ * @Apidoc\Group("api")
+ * @Apidoc\Sort("21")
+ */
+class Goods extends Base
+{
+    protected $need_login = [];
+
+    public function initialize()
+    {
+        parent::initialize();
+    }
+
+    /**
+     * 商品列表
+     *
+     * @Apidoc\Method("POST")
+     * @Apidoc\Param("category_id", type="integer", require=true, desc="二级分类ID")
+     * @Apidoc\Returned("id", type="integer", desc="商品ID")
+     * @Apidoc\Returned("goods_name", type="integer", desc="商品名称")
+     * @Apidoc\Returned("is_sku", type="string", desc="规格类型:0=单规格,1=多规格")
+     * @Apidoc\Returned("weigh", type="integer", desc="权重")
+     * @Apidoc\Returned("goods_sku", type="array", desc="规格列表",
+     *     @Apidoc\Returned("id", type="integer", desc="规格ID"),
+     *     @Apidoc\Returned("goods_id", type="integer", desc="商品ID"),
+     *     @Apidoc\Returned("attr_name", type="string", desc="规格项"),
+     *     @Apidoc\Returned("attr_values_text", type="array", desc="规格值列表",
+     *         @Apidoc\Returned("id", type="integer", desc="规格值ID"),
+     *         @Apidoc\Returned("value", type="string", desc="规格值")
+     *     )
+     * )
+     */
+    public function get_list()
+    {
+        $category_id = input('category_id');
+        $data = $category_id > 0 ? GoodsService::get_list($category_id) : [];
+        $this->success('商品列表', $data);
+    }
+
+}

+ 4 - 0
application/api/controller/Goodscategory.php

@@ -12,6 +12,10 @@ use hg\apidoc\annotation as Apidoc;
  */
 class Goodscategory extends Base
 {
+    public function initialize()
+    {
+        parent::initialize();
+    }
 
     /**
      * 商品分类列表

+ 4 - 0
application/api/controller/Login.php

@@ -14,6 +14,10 @@ use hg\apidoc\annotation as Apidoc;
  */
 class Login extends Base
 {
+    public function initialize()
+    {
+        parent::initialize();
+    }
 
     /**
      * 登录

+ 0 - 47
application/common.php

@@ -301,50 +301,3 @@ function get_one_two_array($arr, $str1, $str2)
     }
     return $item;
 }
-
-//无限极分类
-function getTree($arr, $parent_id = 0, &$tree = array())
-{
-    foreach ($arr as $key => $value) {
-        if ($value['parent_id'] == $parent_id) {
-            $tree[] = $value;
-            getTree($arr, $value['id'], $tree);
-        }
-    }
-
-    return $tree;
-}
-
-if (!function_exists('get_cate_list')) {
-    //递归函数 实现无限级分类列表
-    function get_cate_list($list, $pid = 0, $level = 0)
-    {
-        static $tree = array();
-        foreach ($list as $row) {
-            if ($row['parent_id'] == $pid) {
-                $row['level'] = $level;
-                $tree[] = $row;
-                get_cate_list($list, $row['id'], $level + 1);
-            }
-        }
-        return $tree;
-    }
-}
-
-if (!function_exists('get_tree_list')) {
-    //引用方式实现 父子级树状结构
-    function get_tree_list($list)
-    {
-        //将每条数据中的id值作为其下标
-        $temp = [];
-        foreach ($list as $v) {
-            $v['son'] = [];
-            $temp[$v['id']] = $v;
-        }
-        //获取分类树
-        foreach ($temp as $k => $v) {
-            $temp[$v['pid']]['son'][] = &$temp[$v['id']];
-        }
-        return isset($temp[0]['son']) ? $temp[0]['son'] : [];
-    }
-}

+ 27 - 0
application/common/model/Goods.php

@@ -0,0 +1,27 @@
+<?php
+
+namespace app\common\model;
+
+use app\common\constant\CommonConstant;
+use think\Model;
+
+/**
+ * 商品模型
+ */
+class Goods extends Model
+{
+    // 表名
+    protected $name = 'goods';
+
+    // 追加属性
+    protected $append = [
+
+    ];
+
+
+    // 关联商品Sku (商品列表)
+    public function goodsSku()
+    {
+        return $this->hasMany(GoodsSku::class, 'goods_id', 'id');
+    }
+}

+ 28 - 0
application/common/model/GoodsSku.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace app\common\model;
+
+use app\common\constant\CommonConstant;
+use think\Model;
+
+/**
+ * 商品规格项模型
+ */
+class GoodsSku extends Model
+{
+    // 表名
+    protected $name = 'goods_sku';
+
+    // 追加属性
+    protected $append = [
+        'attr_values_text'
+    ];
+
+
+    public function getAttrValuesTextAttr($value, $data)
+    {
+        $value = $value ? $value : (isset($data['attr_values']) ? $data['attr_values'] : '');
+        return $value ? json_decode($value) : [];
+    }
+
+}

+ 21 - 0
application/common/model/GoodsSkuValue.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace app\common\model;
+
+use app\common\constant\CommonConstant;
+use think\Model;
+
+/**
+ * 商品规格值模型
+ */
+class GoodsSkuValue extends Model
+{
+    // 表名
+    protected $name = 'goods_sku_value';
+
+    // 追加属性
+    protected $append = [
+
+    ];
+
+}

+ 34 - 0
application/common/service/GoodsService.php

@@ -0,0 +1,34 @@
+<?php
+
+namespace app\common\service;
+
+use app\common\constant\CommonConstant;
+use app\common\model\Goods;
+
+/**
+ * 商品服务类
+ */
+class GoodsService
+{
+
+    /**
+     * 商品列表
+     *
+     * @param integer $category_id 二级分类ID
+     * @return array
+     **/
+    public static function get_list($category_id)
+    {
+        $list = Goods::field('status,is_deleted,create_at', true)
+            ->where('goods_category_id', $category_id)
+            ->where('status', CommonConstant::IS_WHO_1)
+            ->where('is_deleted', CommonConstant::IS_DELETED_0)
+            ->with([
+                'goodsSku'
+            ])
+            ->order('weigh desc,id desc')
+            ->select();
+        return $list;
+    }
+
+}