Przeglądaj źródła

8-10分类状态修改与删除开发

Home 4 lat temu
rodzic
commit
9a7222e89c

+ 23 - 0
app/admin/controller/Category.php

@@ -5,8 +5,10 @@ namespace app\admin\controller;
 
 use app\admin\validate\Category as CategoryValidate;
 use app\common\business\Category as CategoryBusiness;
+use app\common\lib\Status as StatusLib;
 use think\facade\View;
 
+
 class Category extends AdminBase
 {
     /**
@@ -97,4 +99,25 @@ class Category extends AdminBase
             return show(config('status.error'), '排序失败');
         }
     }
+
+    public function status()
+    {
+        $id = input('param.id', 0, 'intval');
+        $status = input('param.status', 0, 'intval');
+
+        if (!$id || !in_array($status, StatusLib::getTableStatus())) {
+            return show(config('status.error'), '参数错误');
+        }
+
+        try {
+            $res = (new CategoryBusiness())->status($id, $status);
+        } catch (\Exception $e) {
+            return show(config('status.error'), $e->getMessage());
+        }
+        if ($res) {
+            return show(config('status.success'), '状态更新成功');
+        } else {
+            return show(config('status.error'), '状态更新失败');
+        }
+    }
 }

+ 18 - 18
app/admin/view/category/index.html

@@ -83,11 +83,10 @@
                     <td>
                         {$vo.operate_user}
                     </td>
-                    <td data-id="1"><input type="checkbox" {if $vo.status== 1} checked {/if} name="status"
-                        lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">
+                    <td data-id="{$vo.id}"><input type="checkbox" {if $vo.status == 1} checked {/if} name="status" lay-skin="switch" lay-filter="switchStatus" lay-text="ON|OFF">
                     </td>
                     <td>
-                        <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete del-child" data-ptype="1"
+                        <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete delete" data-ptype="1"
                            lay-event="delete" data-id="{$vo.id}">删除</a>
                         <a href="{:url('index', ['pid'=>$vo.id])}">获取子栏目</a>
                     </td>
@@ -145,15 +144,17 @@
         // });
 
         //监听状态 更改
-        form.on('switch(switchTest)', function (obj) {
-            console.log(obj.elem.checked, '改变状态')
-
+        form.on('switch(switchStatus)', function (obj) {
             let id = obj.othis.parent().attr('data-id');
             let status = obj.elem.checked ? 1 : 0;
             $.ajax({
-                url: '{:url("admin/change")}?id=' + id + '&status=' + status,
+                url: '{:url("category/status")}?id=' + id + '&status=' + status,
                 success: (res) => {
-
+                    if (res.status == 1) {
+                        window.location.reload();
+                    } else {
+                        layer.msg("更改失败")
+                    }
                 }
             });
             return false;
@@ -185,19 +186,18 @@
         }
 
         // 删除二级分类
-        $('.del-child').on('click', function () {
+        $('.delete').on('click', function () {
             let ptype = $(this).attr('data-ptype'); // fu
             let id = $(this).attr('data-id'); // fu
-            let msg = '';
-            if (ptype == 1) { // 等级类目
-                msg = '一';
-            } else if (ptype == 2) {
-                msg = '二';
-            }
-            layObj.box(`是否删除${msg}级分类`, () => {
-                let url = '{:url("admin/del")}?id=' + id
+
+            layObj.box(`是否删除当前分类`, () => {
+                let url = '{:url("category/status")}?id=' + id + '&status=99';
                 layObj.get(url, (res) => {
-                    $(this).parent().remove()
+                    if (res.status == 1) {
+                        window.location.reload();
+                    } else {
+                        layer.msg("删除失败")
+                    }
                 })
 
             })

+ 36 - 1
app/common/business/Category.php

@@ -96,8 +96,11 @@ class Category
      * 修改分类表排序的值
      * @param $id
      * @param $listorder
-     * @return bool
+     * @return array|bool|\think\Model|null
      * @throws \think\Exception
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
      */
     public function listorder($id, $listorder)
     {
@@ -118,4 +121,36 @@ class Category
 
         return $res;
     }
+
+    /**
+     * 分类状态值修改
+     * @param $id
+     * @param $status
+     * @return array|bool|\think\Model|null
+     * @throws \think\Exception
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function status($id, $status)
+    {
+        $res = $this->getById($id);
+        if (!$res) {
+            throw new \think\Exception('不存在该条记录');
+        }
+        if ($res['status'] == $status) {
+            throw new \think\Exception('状态修改前后无区别');
+        }
+
+        $data = [
+            'status' => (int)$status
+        ];
+        try {
+            $res = $this->model->updateById($id, $data);
+        } catch (\Exception $e) {
+            // 记录日志
+            return false;
+        }
+        return $res;
+    }
 }

+ 14 - 0
app/common/lib/Status.php

@@ -0,0 +1,14 @@
+<?php
+
+
+namespace app\common\lib;
+
+
+class Status
+{
+    public static function getTableStatus()
+    {
+        $mysqlStatus = config('status.mysql');
+        return array_values($mysqlStatus);
+    }
+}