Browse Source

商家管理

牟新芬 4 years ago
parent
commit
9a05f929d5

+ 69 - 0
app/model/system/Merchant.php

@@ -0,0 +1,69 @@
+<?php
+
+namespace app\model\system;
+
+use library\traits\ModelTrait;
+use library\basic\BaseModel;
+
+/**
+ * TODO 商家Model
+ * Class Merchant
+ * @package app\model\system
+ */
+class Merchant extends BaseModel
+{
+	/**
+     * 数据表主键
+     * @var string
+     */
+    protected $pk = 'id';
+
+    /**
+     * 模型名称
+     * @var string
+     */
+    protected $name = 'merchant';
+
+    use ModelTrait;
+
+    /**
+     * 商家列表
+     * @param $where
+     * @return array
+     */
+    public static function systemPage($where)
+    {
+        $model = self::setWhere($where);
+        $count = $model->count();
+        $list = $model->page((int)$where['page'], (int)$where['limit'])
+            ->select()
+            ->each(function ($item) {
+                $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
+            });
+        return compact('count', 'list');
+    }
+
+    /**
+     * 设置商家 where 条件
+     * @param $where
+     * @param null $model
+     * @return mixed
+     */
+    public static function setWhere($where, $model = null)
+    {
+        $model = $model === null ? new self() : $model;
+        $model = $model->alias('c');
+        $model = $model->field('c.*');
+        if (isset($where['name']) && $where['name'] != '') $model = $model->where('c.id|c.name', 'LIKE', "%$where[name]%");
+        return $model->order('c.id desc')->where('c.is_del', 0);
+    }
+
+    /**
+     * 详情
+     */
+    public static function getOne($id)
+    {
+        $info = self::get($id);
+        return $info;
+    }
+}

+ 47 - 48
app/system/controller/v1/Category.php

@@ -23,79 +23,78 @@ class Category extends  BaseController{
      * 基本设置
      */
     public function list(Request $request){
-    [$cate_name,$pid,$is_show] = UtilService::getMore([
-        ['cate_name',''],
-        ['pid','0'],
-        ['is_show','0']
-    ],$request,true);
-    $menuMenu = new CategoryModel();
-    $menus = $menuMenu->getArMenu($cate_name,$pid,$is_show);
-    return app('json')->success($menus);
-}
+        [$cate_name,$pid,$is_show] = UtilService::getMore([
+            ['cate_name',''],
+            ['pid','0'],
+            ['is_show','0']
+        ],$request,true);
+        $menuMenu = new CategoryModel();
+        $menus = $menuMenu->getArMenu($cate_name,$pid,$is_show);
+        return app('json')->success($menus);
+    }
 
     /**
      * 系统分类显示 | 关闭
      */
     public function status(Request $request) {
-    [$id,$is_show] = UtilService::getMore([
-        ['id','0','empty','参数错误'],
-        ['is_show','0']
-    ],$request,true);
-    $bool = (new CategoryModel())->setStatus($id,$is_show);
-    if($bool) {
-        return app('json')->success('操作成功');
-    } else {
-        return app('json')->fail('提交失败');
+        [$id,$is_show] = UtilService::getMore([
+            ['id','0','empty','参数错误'],
+            ['is_show','0']
+        ],$request,true);
+        $bool = (new CategoryModel())->setStatus($id,$is_show);
+        if($bool) {
+            return app('json')->success('操作成功');
+        } else {
+            return app('json')->fail('提交失败');
+        }
     }
-}
-
 
     /**
      * 获取树级分类
      */
     public function treeList() {
-    $menuAr = (new CategoryModel())->order("sort","desc")->select()->toArray();
-    $data = sort_list_tier($menuAr, '顶级', 'pid', 'id');
-    return app('json')->success($data);
-}
+        $menuAr = (new CategoryModel())->order("sort","desc")->select()->toArray();
+        $data = sort_list_tier($menuAr, '顶级', 'pid', 'id');
+        return app('json')->success($data);
+    }
 
     /**
      * 获取详情栏目数据
      */
     public function info(Request $request){
-    [$id] = UtilService::getMore([
-        ['id',0,'empty','参数错误']
-    ],$request,true);
-    $data = (new CategoryModel())->find(compact('id'))->toArray();
-    return app('json')->success($data);
-}
+        [$id] = UtilService::getMore([
+            ['id',0,'empty','参数错误']
+        ],$request,true);
+        $data = (new CategoryModel())->find(compact('id'))->toArray();
+        return app('json')->success($data);
+    }
 
     /**
      * 保存数据
      */
     public function save(Request $request){
-    $post = UtilService::getMore([
-        ['cate_name','','empty','商品分类'],
-        ['pid','0'],
-        ['sort','0'],
-        ['pic',''],
-        ['is_show','0'],
-        ['id','0']
-    ],$request);
-    (new CategoryModel())->saveMenu($post);
-    return app('json')->success("数据保存成功");
-}
+        $post = UtilService::getMore([
+            ['cate_name','','empty','商品分类'],
+            ['pid','0'],
+            ['sort','0'],
+            ['pic',''],
+            ['is_show','0'],
+            ['id','0']
+        ],$request);
+        (new CategoryModel())->saveMenu($post);
+        return app('json')->success("数据保存成功");
+    }
 
     /**
      * 栏目删除
      * @param Request $request
      */
     public function del(Request $request) {
-    [$id] = UtilService::getMore([
-        ['id',0,'empty','参数错误']
-    ],$request,true);
-    $bool =   (new CategoryModel())->delMenu($id);
-    return app('json')->success("栏目删除成功");
-}
+        [$id] = UtilService::getMore([
+            ['id',0,'empty','参数错误']
+        ],$request,true);
+        $bool =   (new CategoryModel())->delMenu($id);
+        return app('json')->success("栏目删除成功");
+    }
 
-}
+}

+ 115 - 0
app/system/controller/v1/Merchant.php

@@ -0,0 +1,115 @@
+<?php
+
+namespace app\system\controller\v1;
+
+use app\BaseController;
+use library\services\UtilService;
+use app\model\system\Merchant as MerchantModel;
+
+/**
+ * 商家管理
+ * Class Merchant
+ * @package app\system\controller\v1
+ */
+class Merchant extends BaseController
+{
+
+	/**
+     * 显示资源列表
+     *
+     * @return \think\Response
+     */
+    public function index()
+    {
+        $where = UtilService::getMore([
+            ['page', 1],
+            ['limit', 20],
+            ['name', '']
+        ]);
+        $list = MerchantModel::systemPage($where);
+        return app('json')->success($list);
+    }
+
+    /**
+     * 获取所有显示商家(不分页)
+     */
+    public function list()
+    {
+        $list = MerchantModel::where('is_del', 0)->where('status', 1)->select()->toArray();
+        return app('json')->success($list);
+    }
+
+    /**
+     * 详情
+     * @param $id
+     * @return mixed
+     */
+    public function read($id)
+    {
+        $info = MerchantModel::getOne($id);
+        return app('json')->success(compact('info'));
+    }
+
+    /**
+     * 保存新建的资源
+     * @param int $id
+     */
+    public function save($id = 0)
+    {
+        $data = UtilService::getMore([
+            ['name','','empty','公司名称不能为空'],
+            'main',
+            'principal',
+            'phone',
+            ['amount', 0],
+            ['issuing', 0],
+            ['packet', 0],
+            ['hot', 0],
+            'advantage'
+        ]);
+        if ($id) {
+            $record = MerchantModel::get($id);
+            if (!$record) return app('json')->fail('数据不存在!');
+            MerchantModel::where('id',$id)->save($data);
+            return app('json')->success('编辑成功!');
+        } else {
+            $data['add_time'] = time();
+            $id = MerchantModel::insertGetId($data);
+            MerchantModel::where('id',$id)->save($data);
+            return app('json')->success('添加商家成功!');
+        }
+    }
+
+    /**
+     * 删除指定资源
+     *
+     * @param int $id
+     * @return \think\Response
+     */
+    public function delete($id)
+    {
+        if (!$id) return app('json')->fail('数据不存在');
+        $record = MerchantModel::get($id);
+        if (!$record) return app('json')->fail('数据不存在!');
+        if ($record['is_del']) return app('json')->fail('已删除!');
+        $data['is_del'] = 1;
+        if (!MerchantModel::where('id',$id)->save($data))
+            return app('json')->fail(MerchantModel::getErrorInfo('删除失败,请稍候再试!'));
+        else
+            return app('json')->success('删除成功!');
+    }
+
+    /**
+     * 修改状态
+     * @param $id
+     * @param $status
+     * @return mixed
+     */
+    public function set_status($id, $status)
+    {
+        if ($status == '' || $id == 0) return app('json')->fail('参数错误');
+        MerchantModel::where(['id' => $id])->update(['status' => $status]);
+        return app('json')->success($status == 0 ? '关闭成功' : '开启成功');
+    }
+
+}

+ 2 - 0
app/system/controller/v1/Product.php

@@ -222,6 +222,7 @@ class Product extends BaseController
         $data = UtilService::getMore([
             'store_name',
             'cate_id',
+            'factory_id',
             'code',
             'keyword',
             'store_info',
@@ -252,6 +253,7 @@ class Product extends BaseController
         $attr = $data['items'];
         unset($data['items'], $data['video'], $data['attrs']);
         if (!$data['cate_id']) return app('json')->fail('请选择商品分类');
+        if (!$data['factory_id']) return app('json')->fail('请选择供货商家');
         if (!$data['store_name']) return app('json')->fail('请输入商品名称');
         if (empty($data['image'])) return app('json')->fail('请上传商品图片');
         if (count($data['slider_image']) < 1) return app('json')->fail('请上传商品轮播图');

+ 22 - 0
app/system/route/merchant.php

@@ -0,0 +1,22 @@
+<?php
+
+use think\facade\Route;
+
+Route::group('merchant', function () {
+    //商家列表
+    Route::rule('list', 'v1.merchant/index');
+    //所有显示商家
+    Route::rule('all_list', 'v1.merchant/list');
+    //商家详情
+    Route::rule('info/:id', 'v1.merchant/read');
+    //保存新增或编辑
+    Route::rule('save/:id', 'v1.merchant/save');
+    //删除
+    Route::rule('del/:id', 'v1.merchant/delete');
+    //修改状态
+    Route::rule('set_status/:id/:status', 'v1.merchant/set_status');
+
+})->middleware([
+    \app\system\middleware\AllowOriginMiddleware::class,
+    \app\system\middleware\AdminAuthTokenMiddleware::class
+]);