wesmiler 2 недель назад
Родитель
Сommit
6e1d6937ba

+ 114 - 0
app/Http/Controllers/Admin/StoreCategoryController.php

@@ -0,0 +1,114 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Http\Controllers\Admin;
+
+use App\Services\Common\StoreCategoryService;
+
+/**
+ * 商家分类管理-控制器
+ * @author laravel开发员
+ * @since 2020/11/11
+ * @package App\Http\Controllers
+ */
+class StoreCategoryController extends Backend
+{
+    /**
+     * 构造函数
+     * @author laravel开发员
+     * @since 2020/11/11
+     */
+    public function __construct()
+    {
+        parent::__construct();
+        $this->service = new StoreCategoryService();
+    }
+
+    /**
+     * 获取列表
+     * @return array
+     */
+    public function index()
+    {
+        $pageSize = request()->get('limit', 15);
+        $params = request()->all();
+        $params['store_id'] = $this->storeId;
+        return $this->service->getDataList($params, $pageSize);
+    }
+
+    /**
+     * 获取详情
+     * @return array
+     */
+    public function info()
+    {
+        return $this->service->info();
+    }
+
+    /**
+     * 添加或编辑
+     * @return array
+     */
+    public function edit()
+    {
+        if ($this->service->edit()) {
+            return message($this->service->getError() ?: '操作成功', true);
+        } else {
+            return message($this->service->getError() ?: '操作失败', false);
+        }
+    }
+
+    /**
+     * 设置状态
+     * @return array
+     */
+    public function status()
+    {
+        $params = request()->post();
+        if ($this->service->setStatus($params)) {
+            return message($this->service->getError(), true);
+        } else {
+            return message($this->service->getError(), false);
+        }
+    }
+
+    /**
+     * 删除
+     * @return array
+     */
+    public function delete()
+    {
+        return $this->service->delete();
+    }
+
+    /**
+     * 批量删除
+     * @return array
+     */
+    public function deleteAll()
+    {
+        $params = request()->post();
+        if ($this->service->deleteAll($params)) {
+            return message($this->service->getError(), true);
+        } else {
+            return message($this->service->getError(), false);
+        }
+    }
+
+    /**
+     * 选项列表(用于下拉选择)
+     * @return array
+     */
+    public function options()
+    {
+        return $this->service->options();
+    }
+}

+ 244 - 0
app/Services/Common/StoreCategoryService.php

@@ -0,0 +1,244 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Services\Common;
+
+use App\Models\StoreCategoryModel;
+use App\Services\BaseService;
+
+/**
+ * 商家分类管理-服务类
+ * @author laravel开发员
+ * @since 2020/11/11
+ * @package App\Services\Common
+ */
+class StoreCategoryService extends BaseService
+{
+    /**
+     * 构造函数
+     * @author laravel开发员
+     * @since 2020/11/11
+     */
+    public function __construct()
+    {
+        $this->model = new StoreCategoryModel();
+    }
+
+
+    /**
+     * 获取数据列表
+     * @param array $params
+     * @param int $pageSize
+     * @return array
+     */
+    public function getDataList($params, $pageSize = 15)
+    {
+        // 分页查询
+        $list = $this->model->orderBy('sort', 'desc')
+            ->where('pid', 0)
+            ->where(function ($query) use ($params) {
+                if (isset($params['store_id']) && $params['store_id'] > 0) {
+                    $query->where('store_id', $params['store_id']);
+                }
+                if (isset($params['name']) && $params['name']) {
+                    $query->where('name', 'like', "%{$params['name']}%");
+                }
+                if (isset($params['status']) && $params['status'] !== null && $params['status'] !== '') {
+                    $query->where('status', intval($params['status']));
+                }
+            })
+            ->orderBy('id', 'asc')
+            ->paginate($pageSize > 0 ? $pageSize : 9999999);
+
+        $list = $list->toArray();
+
+        // 格式化数据
+        if (isset($list['data']) && !empty($list['data'])) {
+            foreach ($list['data'] as &$val) {
+                $val['create_time'] = $val['create_time'] ? datetime($val['create_time'], 'Y-m-d H:i:s') : '';
+            }
+        }
+
+        //返回结果
+        return [
+            'msg' => '操作成功',
+            'code' => 0,
+            'data' => $list['data'],
+            'count' => $list['total']
+        ];
+    }
+
+    /**
+     * 获取分类详情
+     * @return array
+     * @since 2020/11/11
+     * @author laravel开发员
+     */
+    public function info()
+    {
+        // 记录ID
+        $id = request()->input("id", 0);
+        $info = [];
+        if ($id) {
+            $info = $this->model->getInfo($id);
+            if ($info && isset($info['create_time'])) {
+                $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d H:i:s') : '';
+            }
+        }
+        return message(MESSAGE_OK, true, $info);
+    }
+
+    /**
+     * 添加或编辑
+     * @return array
+     * @since 2020/11/11
+     * @author laravel开发员
+     */
+    public function edit()
+    {
+        // 参数
+        $param = request()->all();
+
+        // 验证分类名称
+        if (empty($param['name'])) {
+            $this->error = '分类名称不能为空';
+            return false;
+        }
+
+        // 强制设置 pid=0,忽略层级关系,只保留一级分类
+        $param['pid'] = 0;
+
+        // 设置默认值
+        if (!isset($param['status'])) {
+            $param['status'] = 1; // 默认有效
+        }
+        if (!isset($param['sort'])) {
+            $param['sort'] = 0; // 默认排序为0
+        }
+        if (!isset($param['remark'])) {
+            $param['remark'] = ''; // 默认备注为空
+        }
+
+        // 保存数据
+        $result = $this->model->edit($param);
+        if (!$result) {
+            $this->error = '操作失败';
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * 设置状态
+     * @param array $params
+     * @return bool
+     */
+    public function setStatus($params)
+    {
+        $id = isset($params['id']) ? intval($params['id']) : 0;
+        $status = isset($params['status']) ? intval($params['status']) : 1;
+
+        if (!$id) {
+            $this->error = '分类ID不能为空';
+            return false;
+        }
+
+        $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
+        if (!$info) {
+            $this->error = '分类信息不存在';
+            return false;
+        }
+
+        // 更新状态
+        $this->model->where('id', $id)->update([
+            'status' => $status,
+            'update_time' => time()
+        ]);
+
+        $this->error = $status == 1 ? '启用成功' : '禁用成功';
+        return true;
+    }
+
+    /**
+     * 删除(重写父类方法)
+     * @return array
+     */
+    public function delete()
+    {
+        // 参数
+        $param = request()->all();
+        // 记录ID
+        $id = getter($param, "id");
+        if (empty($id)) {
+            return message("分类ID不能为空", false);
+        }
+
+        $info = $this->model->where(['id' => $id, 'mark' => 1])->first();
+        if (!$info) {
+            return message("分类信息不存在", false);
+        }
+
+        // 忽略 pid,不需要检查子分类(因为只有一级分类)
+
+        // 删除(软删除)
+        $this->model->where('id', $id)->update([
+            'mark' => 0,
+            'update_time' => time()
+        ]);
+
+        return message("删除成功", true);
+    }
+
+    /**
+     * 批量删除
+     * @param array $params
+     * @return bool
+     */
+    public function deleteAll($params)
+    {
+        $ids = isset($params['ids']) ? $params['ids'] : [];
+
+        if (empty($ids) || !is_array($ids)) {
+            $this->error = '请选择要删除的分类';
+            return false;
+        }
+
+        // 忽略 pid,不需要检查子分类(因为只有一级分类)
+
+        // 批量删除(软删除)
+        $this->model->whereIn('id', $ids)->update([
+            'mark' => 0,
+            'update_time' => time()
+        ]);
+
+        $this->error = '删除成功';
+        return true;
+    }
+
+    /**
+     * 获取分类选项(用于下拉选择)
+     * 只返回一级分类(pid=0),忽略层级关系
+     * @return array
+     */
+    public function options()
+    {
+        // 只查询一级分类(pid=0)
+        $list = $this->model->where(['mark' => 1, 'status' => 1, 'pid' => 0])
+            ->orderBy('sort', 'desc')
+            ->orderBy('id', 'asc')
+            ->select(['id', 'name'])
+            ->get()
+            ->toArray();
+
+        return message("操作成功", true, $list);
+    }
+}