Browse Source

分类创建时唯一性判断

Home 4 years ago
parent
commit
5f39b7ac98
1 changed files with 14 additions and 2 deletions
  1. 14 2
      app/common/business/Category.php

+ 14 - 2
app/common/business/Category.php

@@ -17,12 +17,24 @@ class Category
     /**
      * 新增一条分类
      * @param array $data
-     * @return int
+     * @return mixed
+     * @throws \think\Exception
      */
     public function add(array $data)
     {
         $data['status'] = config('status.mysql.table_normal');
-        $this->categoryObj->save($data);
+
+        $name = $data['name'];
+        $count = $this->categoryObj->where('name',$name)->count();
+        if ($count > 0) {
+            throw new \think\Exception('分类名已经存在');
+        }
+
+        try {
+            $this->categoryObj->save($data);
+        } catch (\Exception $e) {
+            throw new \think\Exception('服务内部异常');
+        }
         return $this->categoryObj->getLastInsID();
     }
 }