浏览代码

fix api分页数据重复

magicblack 5 月之前
父节点
当前提交
15358abb2b
共有 1 个文件被更改,包括 12 次插入2 次删除
  1. 12 2
      application/common/model/Base.php

+ 12 - 2
application/common/model/Base.php

@@ -39,12 +39,22 @@ class Base extends Model
 
     public function getListByCond($offset, $limit, $cond, $orderby = '', $fields = "*", $transform = false)
     {
-        $orderby = $orderby ?: ($this->primaryId . " DESC");
+        $offset = max(0, (int)$offset);
+        $limit = max(1, (int)$limit);
+
+        if (empty($orderby)) {
+            $orderby = $this->primaryId . " DESC";
+        } else {
+            if (strpos($orderby, $this->primaryId) === false) {
+                $orderby .= ", " . $this->primaryId . " DESC";
+            }
+        }
+
         $query_object = $this;
         if ($this->readFromMaster === true) {
             $query_object = $query_object->master();
         }
-        $list = $query_object->where($cond)->field($fields)->order($orderby)->limit($offset . ',' . $limit)->select();
+        $list = $query_object->where($cond)->field($fields)->order($orderby)->limit($offset, $limit)->select();
         if (!$list) {
             return [];
         }