|
@@ -7,11 +7,11 @@ use app\common\model\mysql\Category as CategoryModel;
|
|
|
|
|
|
class Category
|
|
|
{
|
|
|
- public $categoryObj = null;
|
|
|
+ public $model = null;
|
|
|
|
|
|
public function __construct()
|
|
|
{
|
|
|
- $this->categoryObj = new CategoryModel();
|
|
|
+ $this->model = new CategoryModel();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -25,17 +25,17 @@ class Category
|
|
|
$data['status'] = config('status.mysql.table_normal');
|
|
|
|
|
|
$name = $data['name'];
|
|
|
- $count = $this->categoryObj->where('name', $name)->count();
|
|
|
+ $count = $this->model->where('name', $name)->count();
|
|
|
if ($count > 0) {
|
|
|
throw new \think\Exception('分类名已经存在');
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- $this->categoryObj->save($data);
|
|
|
+ $this->model->save($data);
|
|
|
} catch (\Exception $e) {
|
|
|
throw new \think\Exception('服务内部异常');
|
|
|
}
|
|
|
- return $this->categoryObj->getLastInsID();
|
|
|
+ return $this->model->id;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -48,7 +48,7 @@ class Category
|
|
|
public function getNormalCategory()
|
|
|
{
|
|
|
$field = "id, name, pid";
|
|
|
- $categorys = $this->categoryObj->getNormalCategory($field);
|
|
|
+ $categorys = $this->model->getNormalCategory($field);
|
|
|
if (!$categorys) {
|
|
|
return [];
|
|
|
}
|
|
@@ -65,7 +65,7 @@ class Category
|
|
|
*/
|
|
|
public function getLists($data, $num)
|
|
|
{
|
|
|
- $list = $this->categoryObj->getLists($data, $num);
|
|
|
+ $list = $this->model->getLists($data, $num);
|
|
|
if (!$list) {
|
|
|
return [];
|
|
|
}
|
|
@@ -73,4 +73,49 @@ class Category
|
|
|
$result['render'] = $list->render();
|
|
|
return $result;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据id获取某一条记录
|
|
|
+ * @param $id
|
|
|
+ * @return array|\think\Model|null
|
|
|
+ * @throws \think\db\exception\DataNotFoundException
|
|
|
+ * @throws \think\db\exception\DbException
|
|
|
+ * @throws \think\db\exception\ModelNotFoundException
|
|
|
+ */
|
|
|
+ public function getById($id)
|
|
|
+ {
|
|
|
+ $result = $this->model->find($id);
|
|
|
+ if (empty($result)) {
|
|
|
+ return [];
|
|
|
+ }
|
|
|
+ $result = $result->toArray();
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改分类表排序的值
|
|
|
+ * @param $id
|
|
|
+ * @param $listorder
|
|
|
+ * @return bool
|
|
|
+ * @throws \think\Exception
|
|
|
+ */
|
|
|
+ public function listorder($id, $listorder)
|
|
|
+ {
|
|
|
+ $res = $this->getById($id);
|
|
|
+ if (!$res) {
|
|
|
+ throw new \think\Exception('不存在该条记录');
|
|
|
+ }
|
|
|
+ $data = [
|
|
|
+ 'listorder' => $listorder
|
|
|
+ ];
|
|
|
+
|
|
|
+ try {
|
|
|
+ $res = $this->model->updateById($id, $data);
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ // 记录日志
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $res;
|
|
|
+ }
|
|
|
}
|