diff --git a/src/Base/DzqCache.php b/src/Base/DzqCache.php index d8b5c73e9994c2b01d10b1ad11102717c0775118..eb6f7f541fe9907b6dd6cb013818176561e2295d 100644 --- a/src/Base/DzqCache.php +++ b/src/Base/DzqCache.php @@ -19,7 +19,6 @@ namespace Discuz\Base; use App\Common\CacheKey; -use Illuminate\Database\Eloquent\Collection; class DzqCache { @@ -65,28 +64,6 @@ class DzqCache return true; } - public static function hSet($key, $hashKey, $value) - { - $data = self::get($key); - $data[$hashKey] = $value; - return self::set($key, $data); - } - - public static function hMSet($key, array $value, $indexField = null, $mutiColumn = false, $indexes = [], $defaultValue = []) - { - list($result) = self::hMSetResult($value, $indexField, $mutiColumn, $indexes, $defaultValue); - if (isset(CacheKey::$fileStore[$key])) { - self::putFragmentFileStore($key, $result); - } else { - $data = self::get($key); - foreach ($result as $k => $v) { - $data[$k] = $v; - } - self::set($key, $data); - } - return $result; - } - /** * @desc 获取一个key下的数据 * @param $key @@ -152,34 +129,38 @@ class DzqCache * @param $key * @param $hashKeys * @param callable|null $callBack - * @param null $indexField 数据转换的字段 + * @param null $index 数据转换的字段 * @param bool $mutiColumn 每个字段下的数据是否保留多个 * @param bool $autoCache * @return bool|array */ - public static function hMGet($key, $hashKeys, callable $callBack = null, $indexField = null, $mutiColumn = false, $autoCache = true) + public static function hMGet($key, $hashKeys, callable $callBack = null, $index = null, $mutiColumn = false, $autoCache = true) { if (isset(CacheKey::$fileStore[$key])) { $ret = self::getFragmentFileStore($key, $hashKeys); } else { $data = self::get($key); - $ret = false; - if ($data && self::CACHE_SWICH) { - foreach ($hashKeys as $hashKey) { - if (array_key_exists($hashKey, $data)) { - if (!empty($data[$hashKey])) { - $ret[$hashKey] = $data[$hashKey]; + $ret = []; + if(!empty($hashKeys)){ + if ($data && self::CACHE_SWICH) { + foreach ($hashKeys as $hashKey) { + if (array_key_exists($hashKey, $data)) { + if (!empty($data[$hashKey])) { + $ret[$hashKey] = $data[$hashKey]; + } + } else { + $ret = false; + break; } - } else { - $ret = false; - break; } + }else{ + $ret = false; } } } if ($ret === false && !empty($callBack)) { $ret = $callBack($hashKeys); - list($resultWithNull, $resultNotNull) = self::hMSetResult($ret, $indexField, $mutiColumn, $hashKeys, null); + list($resultWithNull, $resultNotNull) = self::hMSetResult($ret, $index, $mutiColumn, $hashKeys, null); if ($autoCache) { if (isset(CacheKey::$fileStore[$key])) { self::putFragmentFileStore($key, $resultWithNull); @@ -196,41 +177,6 @@ class DzqCache return $ret; } - /** - * @desc 附件数据获取 - * todo 后续去除模型传递,废除该方法 - * @param $key - * @param array $hashKeys - * @param callable|null $callBack - * @return bool|Collection - */ - public static function hMGetCollection($key, array $hashKeys, callable $callBack = null) - { - $data = self::getAppCache($key, $hasCache, $cacheData); - $ret = false; - if ($data && self::CACHE_SWICH) { - $ret = new Collection(); - foreach ($hashKeys as $hashKey) { - if (!empty($data[$hashKey])) { - $ret->put($hashKey, $data[$hashKey]); - } else { - $ret = false; - break; - } - } - } - if (($ret === false || !$data) && !empty($callBack)) { - $ret = $callBack($hashKeys); - !$data && $data = new Collection(); - foreach ($ret as $k => $v) { - $data->put($k, $v); - } - $hasCache && self::setAppCache($key, $data, $cacheData); - self::set($key, $data); - } - return $ret; - } - /** * @desc 帖子列表二级缓存和预加载 * @param $key diff --git a/src/Base/DzqController.php b/src/Base/DzqController.php index 9c09ca7182fbd98bddd1fa3f8ad2f7ed2e4cbac2..3f160a0aa937f880ef5f85ea1e3fb05eaf07c5dc 100644 --- a/src/Base/DzqController.php +++ b/src/Base/DzqController.php @@ -69,8 +69,8 @@ abstract class DzqController implements RequestHandlerInterface $this->dzqLogInit(); //临时处理管理端接口权限 $path = $this->request->getUri()->getPath(); - if(stristr($path,'backAdmin') && !stristr($path,'backAdmin/login')){ - if(!$this->user->isAdmin()){ + if (stristr($path, 'backAdmin') && !stristr($path, 'backAdmin/login')) { + if (!$this->user->isAdmin()) { throw new PermissionDeniedException('没有权限'); } } @@ -131,6 +131,7 @@ abstract class DzqController implements RequestHandlerInterface } } } + private function setParamsAlias() { $this->saveParamsAlias = []; @@ -141,18 +142,19 @@ abstract class DzqController implements RequestHandlerInterface } } } + /* * 接口入参 */ - public function inPut($name='', $checkValid = true) + public function inPut($name = '', $checkValid = true) { - if(!empty($this->saveParamsAlias[$name])){ + if (!empty($this->saveParamsAlias[$name])) { return $this->saveParamsAlias[$name]; } - if(empty($name)){ - if($this->parseBody instanceof \Illuminate\Support\Collection){ + if (empty($name)) { + if ($this->parseBody instanceof \Illuminate\Support\Collection) { return $this->parseBody->merge($this->queryParams)->all(); - }else{ + } else { return $this->queryParams; } } @@ -231,13 +233,13 @@ abstract class DzqController implements RequestHandlerInterface ]; } - public function preloadPaginiation($pageCount, $perPage, \Illuminate\Database\Eloquent\Builder $builder) + public function preloadPaginiation($currentPage, $pageCount, $perPage, \Illuminate\Database\Eloquent\Builder $builder) { $perPage = $perPage >= 1 ? intval($perPage) : 20; $perPageMax = 50; $perPage > $perPageMax && $perPage = $perPageMax; $count = $builder->count(); - $builder = $builder->offset(0)->limit($pageCount * $perPage)->get(); + $builder = $builder->offset(($currentPage-1)*$perPage)->limit($pageCount * $perPage)->get(); $builder = $builder->toArray(); $url = $this->request->getUri(); $port = $url->getPort(); @@ -245,16 +247,17 @@ abstract class DzqController implements RequestHandlerInterface parse_str($url->getQuery(), $query); unset($query['preload']); $ret = []; - $currentPage = 1; $totalCount = $count; $totalPage = $count % $perPage == 0 ? $count / $perPage : intval($count / $perPage) + 1; - while ($currentPage <= $pageCount) { + $limitPage = $currentPage + $pageCount; + $offset = 0; + while ($currentPage < $limitPage) { $queryFirst = $queryNext = $queryPre = $query; - $queryFirst['page'] = 1; + $queryFirst['page'] = $currentPage; $queryNext['page'] = $currentPage + 1; $queryPre['page'] = $currentPage <= 1 ? 1 : $currentPage - 1; $path = $url->getScheme() . '://' . $url->getHost() . $port . $url->getPath() . '?'; - $pageData = (array)array_slice($builder, ($currentPage - 1) * $perPage, $perPage); + $pageData = (array)array_slice($builder, $offset * $perPage, $perPage); $ret[$currentPage] = [ 'pageData' => $pageData, 'currentPage' => $currentPage, @@ -269,6 +272,7 @@ abstract class DzqController implements RequestHandlerInterface if (empty($pageData)) { break; } + $offset++; $currentPage++; } return $ret; diff --git a/src/Common/Utils.php b/src/Common/Utils.php index 3b942ac05785f8d9022514082098ad0d985963d4..532ed6d55200bb920dad5812fbdef1766279cc02 100644 --- a/src/Common/Utils.php +++ b/src/Common/Utils.php @@ -362,4 +362,17 @@ class Utils } return true; } + + /** + * @desc 正整数 + * @param $number + * @return bool + */ + public static function isPositiveInteger($number) + { + if ($number > 0 && round($number, 0) == $number) { + return true; + } + return false; + } }