|
@@ -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();
|
|
|
}
|
|
|
}
|