优化分页

This commit is contained in:
tao 2022-01-07 14:45:35 +08:00
parent 83ca563d1c
commit 4042239db9
29 changed files with 366 additions and 345 deletions

View File

@ -3,8 +3,8 @@
> TaoLer是一个简单迅捷的轻论坛系统适用于个人或组织区域型信息交流发布平台。
* 官网https://www.aieok.com
* 版本TaoLer 1.8.18
* 日期2021.12.30
* 版本TaoLer 1.8.20
* 日期2022.1.7
webman版新架构已适配90%

View File

@ -191,12 +191,10 @@ class Article extends Model
* @param string $ename 分类英文名
* @param string $type all\top\hot\jie 分类类型
* @param int $page 页面
* @param string $url
* @param string $suffix
* @return mixed|\think\Paginator
* @throws \think\db\exception\DbException
*/
public function getCateList(string $ename, string $type, int $page, string $url, string $suffix)
public function getCateList(string $ename, string $type, int $page = 1)
{
$where = [];
$cateId = Cate::where('ename',$ename)->value('id');
@ -224,8 +222,7 @@ class Article extends Model
])->withCount(['comments'])->where(['status'=>1,'jie'=>1])->where($where)->order(['is_top'=>'desc','create_time'=>'desc'])
->paginate([
'list_rows' => 15,
'page' => $page,
'path' =>$url.'[PAGE]'.$suffix
'page' => $page
])->toArray();
break;
@ -240,8 +237,7 @@ class Article extends Model
])->withCount(['comments'])->where('status',1)->where($where)->where('is_hot',1)->order(['is_top'=>'desc','create_time'=>'desc'])
->paginate([
'list_rows' => 15,
'page' => $page,
'path' =>$url.'[PAGE]'.$suffix
'page' => $page
])->toArray();
break;
@ -256,8 +252,7 @@ class Article extends Model
])->withCount(['comments'])->where('status',1)->where($where)->where('is_top',1)->order(['is_top'=>'desc','create_time'=>'desc'])
->paginate([
'list_rows' => 15,
'page' => $page,
'path' =>$url.'[PAGE]'.$suffix
'page' => $page
])->toArray();
break;
@ -272,8 +267,7 @@ class Article extends Model
])->withCount(['comments'])->where('status',1)->where($where)->where('jie',0)->order(['is_top'=>'desc','create_time'=>'desc'])
->paginate([
'list_rows' => 15,
'page' => $page,
'path' =>$url.'[PAGE]'.$suffix
'page' => $page
])->toArray();
break;
@ -288,8 +282,7 @@ class Article extends Model
])->withCount(['comments'])->where('status',1)->where($where)->order(['is_top'=>'desc','create_time'=>'desc'])
->paginate([
'list_rows' => 15,
'page' => $page,
'path' =>$url.'[PAGE]'.$suffix
'page' => $page
])->toArray();
break;
}

View File

@ -40,7 +40,7 @@ class Comment extends Model
{
$res = Cache::get('reply');
if(!$res){
$user = User::withCount('comments')->order(['comments_count'=>'desc','last_login_time'=>'desc'])->limit($num)->select();
$user = User::withCount('comments')->order(['comments_count'=>'desc','last_login_time'=>'desc'])->limit($num)->select()->toArray();
if($user)
{
$res['status'] = 0;

View File

@ -29,54 +29,22 @@ class Article extends BaseController
// 抛出 HTTP 异常
throw new \think\exception\HttpException(404, '请求异常');
}
$page = Request::param('page') ? Request::param('page') : 1;
//获取分类ID
$ename = Request::param('ename');
//动态参数
$ename = Request::param('ename') ?? 'all';
$type = Request::param('type') ?? 'all';
$page = Request::param('page') ? Request::param('page') : 1;
$tpl = Db::name('cate')->where('ename',$ename)->value('detpl');
//分页伪静态
//$str = Request::baseUrl(); //不带参数在url
$path = Request::pathinfo();
$str = '/'.app('http')->getName().'/'.$path;
//halt($str);
$patterns = "/\d+/"; //数字正则
$p = preg_match($patterns,$str,$arr); //正则查询页码出现在位置
//分页url
$url = url('cate_page',['ename'=>$ename,'type'=>$type,'page'=>$page]);
//返回最后/前面的字符串
$path = substr($url,0,strrpos($url,"/"));
//检测route配置中是否设置了伪静态后缀
$suffix = Config::get('route.url_html_suffix') ? '.'.Config::get('route.url_html_suffix') : '/';
if(Config::get('route.url_html_suffix')){
//伪静态有后缀
if(isset($arr[0])){
$page = $arr[0];
$url = strstr($str,$arr[0],true);
} else {
$page = 1;
$url = strstr($str,'.html',true);
}
} else {
//伪静态后缀false
if(isset($arr[0])){
$page = $arr[0];
$url = strstr($str,$arr[0],true);
} else {
$page = 1;
$url = $str.'/';
}
}
//分类列表
$article = new ArticleModel();
$artList = $article->getCateList($ename,$type,$page,$url,$suffix);
//$count = $artList->total();
$artList = $article->getCateList($ename,$type,$page);
// 热议文章
$artHot = $article->getArtHot(10);
//广告
$ad = new Slider();
//分类图片
@ -84,12 +52,12 @@ class Article extends BaseController
//分类钻展赞助
$ad_comm = $ad->getSliderList(6);
View::assign(['type'=>$type,'artList'=>$artList,'artHot'=>$artHot,'ad_cateImg'=>$ad_cateImg,'ad_comm'=>$ad_comm,'jspage'=>'jie','url'=>$url]);
View::assign(['type'=>$type,'artList'=>$artList,'artHot'=>$artHot,'ad_cateImg'=>$ad_cateImg,'ad_comm'=>$ad_comm,'jspage'=>'jie','ename'=>$ename,'path'=>$path]);
return View::fetch('article/'.$tpl.'/cate');
}
//文章详情页
public function detail($id)
public function detail()
{
$id = input('id');
$artStu = Db::name('article')->field('id')->where(['status'=>1,'delete_time'=>0])->find($id);
@ -114,8 +82,7 @@ class Article extends BaseController
$pv = Db::name('article')->field('pv')->where('id',$id)->value('pv');
//评论
$comment = new Comment;
$comments = $comment->getComment($id, $page);
$comments = $this->getComments($id, $page);
// 热议文章
$artHot = $article->getArtHot(10);
@ -130,6 +97,13 @@ class Article extends BaseController
return View::fetch('article/'.$tpl.'/detail');
}
//评论内容
public function getComments($id, $page)
{
$comment = new Comment;
return $comment->getComment($id, $page);
}
//文章评论
public function comment()
{

View File

@ -14,17 +14,19 @@ Route::get('captcha/[:config]','\\think\\captcha\\CaptchaController@index');
Route::rule('/', 'index'); // 首页访问路由
Route::group(function () {
Route::get('jie/:id', 'article/detail');
//Route::get('column/<ename>','article/cate');
//Route::get('column/<ename>/<type>','article/cate');
Route::get('column/<ename>/[:type]/[:page]','article/cate')
->pattern([
'ename' => '\w+',
'page' => '\d+',
]);
Route::get('column/<ename>$','article/cate');
Route::get('column/<ename>/<type>$', 'article/cate')->name('cate_type');
Route::rule('column/<ename>/<type>/<page>', 'article/cate')->name('cate_page');
Route::rule('add','Article/add');
Route::rule('edit/[:id]','Article/edit');
//Route::rule('del/:id','article/delete');
});
})->pattern([
'ename' => '\w+',
'type' => '\w+',
'page' => '\d+',
'id' => '\d+',
]);
Route::group(function () {
Route::rule('u/:id', 'user/home');
});

47
composer.lock generated
View File

@ -826,7 +826,7 @@
},
{
"name": "topthink/framework",
"version": "v6.0.10",
"version": "v6.0.11",
"source": {
"type": "git",
"url": "https://github.com/top-think/framework.git",
@ -893,7 +893,7 @@
],
"support": {
"issues": "https://github.com/top-think/framework/issues",
"source": "https://github.com/top-think/framework/tree/v6.0.10"
"source": "https://github.com/top-think/framework/tree/v6.0.11"
},
"time": "2021-12-31T09:14:28+00:00"
},
@ -1064,16 +1064,16 @@
},
{
"name": "topthink/think-orm",
"version": "v2.0.47",
"version": "v2.0.50",
"source": {
"type": "git",
"url": "https://github.com/top-think/think-orm.git",
"reference": "e69151fba9dd21f86e392a0ae208825904d6d49a"
"reference": "091ad5e023c15fcce4ceaea2f3814bdf71045cde"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/top-think/think-orm/zipball/e69151fba9dd21f86e392a0ae208825904d6d49a",
"reference": "e69151fba9dd21f86e392a0ae208825904d6d49a",
"url": "https://api.github.com/repos/top-think/think-orm/zipball/091ad5e023c15fcce4ceaea2f3814bdf71045cde",
"reference": "091ad5e023c15fcce4ceaea2f3814bdf71045cde",
"shasum": "",
"mirrors": [
{
@ -1119,9 +1119,9 @@
],
"support": {
"issues": "https://github.com/top-think/think-orm/issues",
"source": "https://github.com/top-think/think-orm/tree/v2.0.47"
"source": "https://github.com/top-think/think-orm/tree/v2.0.50"
},
"time": "2021-12-31T06:12:13+00:00"
"time": "2022-01-06T02:48:16+00:00"
},
{
"name": "topthink/think-template",
@ -1272,16 +1272,16 @@
"packages-dev": [
{
"name": "symfony/polyfill-mbstring",
"version": "v1.23.1",
"version": "v1.24.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "9174a3d80210dca8daa7f31fec659150bbeabfc6"
"reference": "0abb51d2f102e00a4eefcf46ba7fec406d245825"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/9174a3d80210dca8daa7f31fec659150bbeabfc6",
"reference": "9174a3d80210dca8daa7f31fec659150bbeabfc6",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/0abb51d2f102e00a4eefcf46ba7fec406d245825",
"reference": "0abb51d2f102e00a4eefcf46ba7fec406d245825",
"shasum": "",
"mirrors": [
{
@ -1293,6 +1293,9 @@
"require": {
"php": ">=7.1"
},
"provide": {
"ext-mbstring": "*"
},
"suggest": {
"ext-mbstring": "For best performance"
},
@ -1338,7 +1341,7 @@
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.23.1"
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.24.0"
},
"funding": [
{
@ -1354,11 +1357,11 @@
"type": "tidelift"
}
],
"time": "2021-05-27T12:26:48+00:00"
"time": "2021-11-30T18:21:41+00:00"
},
{
"name": "symfony/polyfill-php72",
"version": "v1.23.0",
"version": "v1.24.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php72.git",
@ -1420,7 +1423,7 @@
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-php72/tree/v1.23.0"
"source": "https://github.com/symfony/polyfill-php72/tree/v1.24.0"
},
"funding": [
{
@ -1440,16 +1443,16 @@
},
{
"name": "symfony/polyfill-php80",
"version": "v1.23.1",
"version": "v1.24.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php80.git",
"reference": "1100343ed1a92e3a38f9ae122fc0eb21602547be"
"reference": "57b712b08eddb97c762a8caa32c84e037892d2e9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/1100343ed1a92e3a38f9ae122fc0eb21602547be",
"reference": "1100343ed1a92e3a38f9ae122fc0eb21602547be",
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/57b712b08eddb97c762a8caa32c84e037892d2e9",
"reference": "57b712b08eddb97c762a8caa32c84e037892d2e9",
"shasum": "",
"mirrors": [
{
@ -1509,7 +1512,7 @@
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-php80/tree/v1.23.1"
"source": "https://github.com/symfony/polyfill-php80/tree/v1.24.0"
},
"funding": [
{
@ -1525,7 +1528,7 @@
"type": "tidelift"
}
],
"time": "2021-07-28T13:41:28+00:00"
"time": "2021-09-13T13:58:33+00:00"
},
{
"name": "symfony/var-dumper",

View File

@ -7,7 +7,7 @@ return [
//应用名,此项不可更改
'appname' => 'TaoLer',
//版本配置
'version' => '1.8.17',
'version' => '1.8.20',
//加盐
'salt' => 'taoler',
//数据库备份目录

View File

@ -722,17 +722,17 @@
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.23.1",
"version_normalized": "1.23.1.0",
"version": "v1.24.0",
"version_normalized": "1.24.0.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "9174a3d80210dca8daa7f31fec659150bbeabfc6"
"reference": "0abb51d2f102e00a4eefcf46ba7fec406d245825"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/9174a3d80210dca8daa7f31fec659150bbeabfc6",
"reference": "9174a3d80210dca8daa7f31fec659150bbeabfc6",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/0abb51d2f102e00a4eefcf46ba7fec406d245825",
"reference": "0abb51d2f102e00a4eefcf46ba7fec406d245825",
"shasum": "",
"mirrors": [
{
@ -744,10 +744,13 @@
"require": {
"php": ">=7.1"
},
"provide": {
"ext-mbstring": "*"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"time": "2021-05-27T12:26:48+00:00",
"time": "2021-11-30T18:21:41+00:00",
"type": "library",
"extra": {
"branch-alias": {
@ -791,7 +794,7 @@
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.23.1"
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.24.0"
},
"funding": [
{
@ -811,8 +814,8 @@
},
{
"name": "symfony/polyfill-php72",
"version": "v1.23.0",
"version_normalized": "1.23.0.0",
"version": "v1.24.0",
"version_normalized": "1.24.0.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php72.git",
@ -876,7 +879,7 @@
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-php72/tree/v1.23.0"
"source": "https://github.com/symfony/polyfill-php72/tree/v1.24.0"
},
"funding": [
{
@ -896,17 +899,17 @@
},
{
"name": "symfony/polyfill-php80",
"version": "v1.23.1",
"version_normalized": "1.23.1.0",
"version": "v1.24.0",
"version_normalized": "1.24.0.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php80.git",
"reference": "1100343ed1a92e3a38f9ae122fc0eb21602547be"
"reference": "57b712b08eddb97c762a8caa32c84e037892d2e9"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/1100343ed1a92e3a38f9ae122fc0eb21602547be",
"reference": "1100343ed1a92e3a38f9ae122fc0eb21602547be",
"url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/57b712b08eddb97c762a8caa32c84e037892d2e9",
"reference": "57b712b08eddb97c762a8caa32c84e037892d2e9",
"shasum": "",
"mirrors": [
{
@ -918,7 +921,7 @@
"require": {
"php": ">=7.1"
},
"time": "2021-07-28T13:41:28+00:00",
"time": "2021-09-13T13:58:33+00:00",
"type": "library",
"extra": {
"branch-alias": {
@ -968,7 +971,7 @@
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-php80/tree/v1.23.1"
"source": "https://github.com/symfony/polyfill-php80/tree/v1.24.0"
},
"funding": [
{
@ -1209,8 +1212,8 @@
},
{
"name": "topthink/framework",
"version": "v6.0.10",
"version_normalized": "6.0.10.0",
"version": "v6.0.11",
"version_normalized": "6.0.11.0",
"source": {
"type": "git",
"url": "https://github.com/top-think/framework.git",
@ -1279,7 +1282,7 @@
],
"support": {
"issues": "https://github.com/top-think/framework/issues",
"source": "https://github.com/top-think/framework/tree/v6.0.10"
"source": "https://github.com/top-think/framework/tree/v6.0.11"
},
"install-path": "../topthink/framework"
},
@ -1451,17 +1454,17 @@
},
{
"name": "topthink/think-orm",
"version": "v2.0.47",
"version_normalized": "2.0.47.0",
"version": "v2.0.50",
"version_normalized": "2.0.50.0",
"source": {
"type": "git",
"url": "https://github.com/top-think/think-orm.git",
"reference": "e69151fba9dd21f86e392a0ae208825904d6d49a"
"reference": "091ad5e023c15fcce4ceaea2f3814bdf71045cde"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/top-think/think-orm/zipball/e69151fba9dd21f86e392a0ae208825904d6d49a",
"reference": "e69151fba9dd21f86e392a0ae208825904d6d49a",
"url": "https://api.github.com/repos/top-think/think-orm/zipball/091ad5e023c15fcce4ceaea2f3814bdf71045cde",
"reference": "091ad5e023c15fcce4ceaea2f3814bdf71045cde",
"shasum": "",
"mirrors": [
{
@ -1481,7 +1484,7 @@
"require-dev": {
"phpunit/phpunit": "^7|^8|^9.5"
},
"time": "2021-12-31T06:12:13+00:00",
"time": "2022-01-06T02:48:16+00:00",
"type": "library",
"installation-source": "dist",
"autoload": {
@ -1509,7 +1512,7 @@
],
"support": {
"issues": "https://github.com/top-think/think-orm/issues",
"source": "https://github.com/top-think/think-orm/tree/v2.0.47"
"source": "https://github.com/top-think/think-orm/tree/v2.0.50"
},
"install-path": "../topthink/think-orm"
},

View File

@ -5,7 +5,7 @@
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => 'f35436abc461b29e7056f63f10302c84aeb2bfbc',
'reference' => '83ca563d1c45da13aef6d32918bfb64b5cc2a935',
'name' => 'taoser/taoler',
'dev' => true,
),
@ -110,17 +110,17 @@
'dev_requirement' => false,
),
'symfony/polyfill-mbstring' => array(
'pretty_version' => 'v1.23.1',
'version' => '1.23.1.0',
'pretty_version' => 'v1.24.0',
'version' => '1.24.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../symfony/polyfill-mbstring',
'aliases' => array(),
'reference' => '9174a3d80210dca8daa7f31fec659150bbeabfc6',
'reference' => '0abb51d2f102e00a4eefcf46ba7fec406d245825',
'dev_requirement' => true,
),
'symfony/polyfill-php72' => array(
'pretty_version' => 'v1.23.0',
'version' => '1.23.0.0',
'pretty_version' => 'v1.24.0',
'version' => '1.24.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../symfony/polyfill-php72',
'aliases' => array(),
@ -128,12 +128,12 @@
'dev_requirement' => true,
),
'symfony/polyfill-php80' => array(
'pretty_version' => 'v1.23.1',
'version' => '1.23.1.0',
'pretty_version' => 'v1.24.0',
'version' => '1.24.0.0',
'type' => 'library',
'install_path' => __DIR__ . '/../symfony/polyfill-php80',
'aliases' => array(),
'reference' => '1100343ed1a92e3a38f9ae122fc0eb21602547be',
'reference' => '57b712b08eddb97c762a8caa32c84e037892d2e9',
'dev_requirement' => true,
),
'symfony/var-dumper' => array(
@ -151,7 +151,7 @@
'type' => 'project',
'install_path' => __DIR__ . '/../../',
'aliases' => array(),
'reference' => 'f35436abc461b29e7056f63f10302c84aeb2bfbc',
'reference' => '83ca563d1c45da13aef6d32918bfb64b5cc2a935',
'dev_requirement' => false,
),
'taoser/think-addons' => array(
@ -173,8 +173,8 @@
'dev_requirement' => false,
),
'topthink/framework' => array(
'pretty_version' => 'v6.0.10',
'version' => '6.0.10.0',
'pretty_version' => 'v6.0.11',
'version' => '6.0.11.0',
'type' => 'library',
'install_path' => __DIR__ . '/../topthink/framework',
'aliases' => array(),
@ -209,12 +209,12 @@
'dev_requirement' => false,
),
'topthink/think-orm' => array(
'pretty_version' => 'v2.0.47',
'version' => '2.0.47.0',
'pretty_version' => 'v2.0.50',
'version' => '2.0.50.0',
'type' => 'library',
'install_path' => __DIR__ . '/../topthink/think-orm',
'aliases' => array(),
'reference' => 'e69151fba9dd21f86e392a0ae208825904d6d49a',
'reference' => '091ad5e023c15fcce4ceaea2f3814bdf71045cde',
'dev_requirement' => false,
),
'topthink/think-template' => array(

2
vendor/services.php vendored
View File

@ -1,5 +1,5 @@
<?php
// This file is automatically generated at:2022-01-02 21:09:33
// This file is automatically generated at:2022-01-07 14:45:18
declare (strict_types = 1);
return array (
0 => 'taoser\\addons\\Service',

View File

@ -80,7 +80,7 @@ final class Mbstring
public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
{
if (\is_array($fromEncoding) || false !== strpos($fromEncoding, ',')) {
if (\is_array($fromEncoding) || ($fromEncoding !== null && false !== strpos($fromEncoding, ','))) {
$fromEncoding = self::mb_detect_encoding($s, $fromEncoding);
} else {
$fromEncoding = self::getEncoding($fromEncoding);
@ -602,6 +602,9 @@ final class Mbstring
if (80000 > \PHP_VERSION_ID) {
return false;
}
if (\is_int($c) || 'long' === $c || 'entity' === $c) {
return false;
}
throw new \ValueError('Argument #1 ($substitute_character) must be "none", "long", "entity" or a valid codepoint');
}

View File

@ -18,6 +18,9 @@
"require": {
"php": ">=7.1"
},
"provide": {
"ext-mbstring": "*"
},
"autoload": {
"psr-4": { "Symfony\\Polyfill\\Mbstring\\": "" },
"files": [ "bootstrap.php" ]

View File

@ -1,5 +1,7 @@
<?php
class UnhandledMatchError extends Error
{
if (\PHP_VERSION_ID < 80000) {
class UnhandledMatchError extends Error
{
}
}

View File

@ -1,5 +1,7 @@
<?php
class ValueError extends Error
{
if (\PHP_VERSION_ID < 80000) {
class ValueError extends Error
{
}
}

View File

@ -243,29 +243,6 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
}
}
$this->filter(function ($result, $options) {
// 关联查询
if (!empty($options['relation'])) {
$result->relationQuery($options['relation'], $options['with_relation_attr']);
}
// 预载入查询
if (empty($options['is_resultSet']) && !empty($options['with'])) {
$result->eagerlyResult($result, $options['with'], $options['with_relation_attr'], false, $options['with_cache'] ?? false);
}
// JOIN预载入查询
if (empty($options['is_resultSet']) && !empty($options['with_join'])) {
$result->eagerlyResult($result, $options['with_join'], $options['with_relation_attr'], true, $options['with_cache'] ?? false);
}
// 关联统计
if (!empty($options['with_count'])) {
foreach ($options['with_count'] as $val) {
$result->relationCount($this, (array) $val[0], $val[1], $val[2], false);
}
}
});
// 执行初始化操作
$this->initialize();
}
@ -308,11 +285,6 @@ abstract class Model implements JsonSerializable, ArrayAccess, Arrayable, Jsonab
$model->setUpdateWhere($where);
// 查询数据处理
foreach ($this->filter as $filter) {
call_user_func_array($filter, [$model, $options]);
}
$model->trigger('AfterRead');
return $model;

View File

@ -106,12 +106,6 @@ abstract class BaseQuery
$name = Str::snake(substr($method, 5));
array_unshift($args, $name);
return call_user_func_array([$this, 'where'], $args);
} elseif ($this->model && in_array($method, ['hidden', 'visible', 'append'])) {
// 调用模型类方法
$this->model->filter(function ($model, $options) use ($method, $args) {
call_user_func_array([$model, $method], $args);
});
return $this;
} elseif ($this->model && method_exists($this->model, 'scope' . $method)) {
// 动态调用命名范围
$method = 'scope' . $method;
@ -284,9 +278,11 @@ abstract class BaseQuery
public function column($field, string $key = ''): array
{
$result = $this->connection->column($this, $field, $key);
if (count($result) != count($result, 1)) {
$this->resultSet($result, false);
}
return $result;
}
@ -1158,7 +1154,7 @@ abstract class BaseQuery
if (!empty($this->model)) {
// 返回模型对象
$this->resultToModel($result, $this->options);
$this->resultToModel($result);
} else {
$this->result($result);
}
@ -1187,7 +1183,7 @@ abstract class BaseQuery
$this->parseView($options);
}
foreach (['data', 'order', 'join', 'union', 'filter', 'json'] as $name) {
foreach (['data', 'order', 'join', 'union', 'filter', 'json', 'with_attr', 'with_relation_attr'] as $name) {
if (!isset($options[$name])) {
$options[$name] = [];
}
@ -1197,7 +1193,7 @@ abstract class BaseQuery
$options['strict'] = $this->connection->getConfig('fields_strict');
}
foreach (['master', 'lock', 'fetch_sql', 'array', 'distinct', 'procedure'] as $name) {
foreach (['master', 'lock', 'fetch_sql', 'array', 'distinct', 'procedure', 'with_cache'] as $name) {
if (!isset($options[$name])) {
$options[$name] = false;
}

View File

@ -630,7 +630,7 @@ class Mongo extends BaseQuery
$options['table'] = $this->getTable();
}
foreach (['where', 'data'] as $name) {
foreach (['where', 'data', 'projection', 'filter', 'json', 'with_attr', 'with_relation_attr'] as $name) {
if (!isset($options[$name])) {
$options[$name] = [];
}
@ -649,10 +649,6 @@ class Mongo extends BaseQuery
$options['modifiers'] = $modifiers;
}
if (!isset($options['projection'])) {
$options['projection'] = [];
}
if (!isset($options['typeMap'])) {
$options['typeMap'] = $this->getConfig('type_map');
}

View File

@ -51,6 +51,45 @@ trait ModelRelationQuery
return $this->model;
}
/**
* 设置需要隐藏的输出属性
* @access public
* @param array $hidden 属性列表
* @return $this
*/
public function hidden(array $hidden = [])
{
$this->options['hidden'] = $hidden;
return $this;
}
/**
* 设置需要输出的属性
* @access public
* @param array $visible
* @return $this
*/
public function visible(array $visible = [])
{
$this->options['visible'] = $visible;
return $this;
}
/**
* 设置需要附加的输出属性
* @access public
* @param array $append 属性列表
* @return $this
*/
public function append(array $append = [])
{
$this->options['append'] = $append;
return $this;
}
/**
* 添加查询范围
* @access public
@ -94,10 +133,11 @@ trait ModelRelationQuery
*/
public function relation(array $relation)
{
if (!empty($relation)) {
$this->options['relation'] = $relation;
if (empty($this->model) || empty($relation)) {
return $this;
}
$this->options['relation'] = $relation;
return $this;
}
@ -139,21 +179,33 @@ trait ModelRelationQuery
/**
* 设置数据字段获取器
* @access public
* @param string|array $name 字段名
* @param callable $callback 闭包获取器
* @param string|array $name 字段名
* @param callable $callback 闭包获取器
* @return $this
*/
public function withAttr($name, callable $callback = null)
{
if (is_array($name)) {
$this->options['with_attr'] = $name;
} else {
$this->options['with_attr'][$name] = $callback;
foreach ($name as $key => $val) {
$this->withAttr($key, $val);
}
return $this;
}
return $this->filter(function ($result) {
return $this->getResultAttr($result, $this->options['with_attr']);
}, 'with_attr');
$this->options['with_attr'][$name] = $callback;
if (strpos($name, '.')) {
[$relation, $field] = explode('.', $name);
if (!empty($this->options['json']) && in_array($relation, $this->options['json'])) {
} else {
$this->options['with_relation_attr'][$relation][$field] = $callback;
unset($this->options['with_attr'][$name]);
}
}
return $this;
}
/**
@ -164,10 +216,11 @@ trait ModelRelationQuery
*/
public function with($with)
{
if (!empty($with)) {
$this->options['with'] = (array) $with;
if (empty($this->model) || empty($with)) {
return $this;
}
$this->options['with'] = (array) $with;
return $this;
}
@ -180,7 +233,7 @@ trait ModelRelationQuery
*/
public function withJoin($with, string $joinType = '')
{
if (empty($with)) {
if (empty($this->model) || empty($with)) {
return $this;
}
@ -212,7 +265,6 @@ trait ModelRelationQuery
}
$this->via();
$this->options['with_join'] = $with;
return $this;
@ -229,16 +281,20 @@ trait ModelRelationQuery
*/
protected function withAggregate($relations, string $aggregate = 'count', $field = '*', bool $subQuery = true)
{
if (!$subQuery) {
$this->options['with_count'][] = [$relations, $aggregate, $field];
} else {
if (!isset($this->options['field'])) {
$this->field('*');
}
$this->model->relationCount($this, (array) $relations, $aggregate, $field, true);
if (empty($this->model)) {
return $this;
}
if (!$subQuery) {
$this->options['with_aggregate'][] = [(array) $relations, $aggregate, $field];
return $this;
}
if (!isset($this->options['field'])) {
$this->field('*');
}
$this->model->relationCount($this, (array) $relations, $aggregate, $field, true);
return $this;
}
@ -253,6 +309,10 @@ trait ModelRelationQuery
*/
public function withCache($relation = true, $key = true, $expire = null, string $tag = null)
{
if (empty($this->model)) {
return $this;
}
if (false === $relation || false === $key || !$this->getConnection()->getCache()) {
return $this;
}
@ -372,6 +432,32 @@ trait ModelRelationQuery
return $this->model->hasWhere($relation, $where, $fields, $joinType, $this);
}
/**
* JSON字段数据转换
* @access protected
* @param array $result 查询数据
* @return void
*/
protected function jsonModelResult(array &$result): void
{
$withAttr = $this->options['with_attr'];
foreach ($this->options['json'] as $name) {
if (!isset($result[$name])) {
continue;
}
$jsonData = json_decode($result[$name], true);
if (isset($withAttr[$name])) {
foreach ($withAttr[$name] as $key => $closure) {
$jsonData[$key] = $closure($jsonData[$key] ?? null, $jsonData);
}
}
$result[$name] = !$this->options['json_assoc'] ? (object) $jsonData : $jsonData;
}
}
/**
* 查询数据转换为模型数据集对象
* @access protected
@ -384,20 +470,24 @@ trait ModelRelationQuery
return $this->model->toCollection();
}
$withRelationAttr = $this->getWithRelationAttr();
$this->options['is_resultSet'] = true;
foreach ($resultSet as $key => &$result) {
// 数据转换为模型对象
$this->resultToModel($result, $this->options, true);
$this->resultToModel($result);
}
if (!empty($this->options['with'])) {
// 预载入
$result->eagerlyResultSet($resultSet, $this->options['with'], $withRelationAttr, false, $this->options['with_cache'] ?? false);
}
if (!empty($this->options['with_join'])) {
// 预载入
$result->eagerlyResultSet($resultSet, $this->options['with_join'], $withRelationAttr, true, $this->options['with_cache'] ?? false);
foreach (['with', 'with_join'] as $with) {
// 关联预载入
if (!empty($this->options[$with])) {
$result->eagerlyResultSet(
$resultSet,
$this->options[$with],
$this->options['with_relation_attr'],
'with_join' == $with ? true : false,
$this->options['with_cache'] ?? false
);
}
}
// 模型数据集转换
@ -405,55 +495,68 @@ trait ModelRelationQuery
}
/**
* 检查动态获取器
* 查询数据转换为模型对象
* @access protected
* @return array
* @param array $result 查询数据
* @return void
*/
protected function getWithRelationAttr(): array
protected function resultToModel(array &$result): void
{
if (isset($this->options['with_relation_attr'])) {
return $this->options['with_relation_attr'];
// JSON数据处理
if (!empty($this->options['json'])) {
$this->jsonModelResult($result);
}
$withRelationAttr = [];
if (!empty($this->options['with_attr'])) {
foreach ($this->options['with_attr'] as $name => $val) {
if (strpos($name, '.')) {
[$relation, $field] = explode('.', $name);
$result = $this->model->newInstance(
$result,
!empty($this->options['is_resultSet']) ? null : $this->getModelUpdateCondition($this->options),
$this->options
);
$withRelationAttr[$relation][$field] = $val;
unset($this->options['with_attr'][$name]);
// 模型数据处理
foreach ($this->options['filter'] as $filter) {
call_user_func_array($filter, [$result, $this->options]);
}
// 关联查询
if (!empty($this->options['relation'])) {
$result->relationQuery($this->options['relation'], $this->options['with_relation_attr']);
}
// 关联预载入查询
if (empty($this->options['is_resultSet'])) {
foreach (['with', 'with_join'] as $with) {
if (!empty($this->options[$with])) {
$result->eagerlyResult(
$this->options[$with],
$this->options['with_relation_attr'],
'with_join' == $with ? true : false,
$this->options['with_cache'] ?? false
);
}
}
}
$this->options['with_relation_attr'] = $withRelationAttr;
return $withRelationAttr;
}
/**
* 查询数据转换为模型对象
* @access protected
* @param array $result 查询数据
* @param array $options 查询参数
* @param bool $resultSet 是否为数据集查询
* @return void
*/
protected function resultToModel(array &$result, array $options = [], bool $resultSet = false): void
{
$options['with_relation_attr'] = $this->getWithRelationAttr();
$options['is_resultSet'] = $resultSet;
// JSON 数据处理
if (!empty($options['json'])) {
$this->jsonResult($result, $options['json'], $options['json_assoc'], $options['with_relation_attr']);
// 关联统计查询
if (!empty($this->options['with_aggregate'])) {
foreach ($this->options['with_aggregate'] as $val) {
$result->relationCount($this, $val[0], $val[1], $val[2], false);
}
}
foreach ($this->options['filter'] as $filter) {
$result = call_user_func($filter, $result);
// 动态获取器
if (!empty($this->options['with_attr'])) {
$result->withAttr($this->options['with_attr']);
}
$result = $this->model->newInstance($result, $resultSet ? null : $this->getModelUpdateCondition($options), $options);
foreach (['hidden', 'visible', 'append'] as $name) {
if (!empty($this->options[$name])) {
$result->$name($this->options[$name]);
}
}
// 刷新原始数据
$result->refreshOrigin();
}
/**

View File

@ -27,7 +27,7 @@ use think\Model;
trait ResultOperation
{
/**
* 设置数据处理
* 设置数据处理(支持模型)
* @access public
* @param callable $filter 数据处理Callable
* @param string $index 索引(唯一)
@ -75,12 +75,19 @@ trait ResultOperation
*/
protected function result(array &$result): void
{
// JSON数据处理
if (!empty($this->options['json'])) {
$this->jsonResult($result, $this->options['json'], true);
$this->jsonResult($result);
}
// 查询数据处理
foreach ($this->options['filter'] as $filter) {
$result = call_user_func($filter, $result);
$result = call_user_func_array($filter, [$result, $this->options]);
}
// 获取器
if (!empty($this->options['with_attr'])) {
$this->getResultAttr($result, $this->options['with_attr']);
}
}
@ -108,9 +115,9 @@ trait ResultOperation
* @access protected
* @param array $result 查询数据
* @param array $withAttr 字段获取器
* @return array
* @return void
*/
protected function getResultAttr(array $result, array $withAttr = []): array
protected function getResultAttr(array &$result, array $withAttr = []): void
{
foreach ($withAttr as $name => $closure) {
$name = Str::snake($name);
@ -126,8 +133,6 @@ trait ResultOperation
$result[$name] = $closure($result[$name] ?? null, $result);
}
}
return $result;
}
/**
@ -161,30 +166,17 @@ trait ResultOperation
/**
* JSON字段数据转换
* @access protected
* @param array $result 查询数据
* @param array $json JSON字段
* @param bool $assoc 是否转换为数组
* @param array $withRelationAttr 关联获取器
* @param array $result 查询数据
* @return void
*/
protected function jsonResult(array &$result, array $json = [], bool $assoc = false, array $withRelationAttr = []): void
protected function jsonResult(array &$result): void
{
foreach ($json as $name) {
foreach ($this->options['json'] as $name) {
if (!isset($result[$name])) {
continue;
}
$result[$name] = json_decode($result[$name], true);
if (isset($withRelationAttr[$name])) {
foreach ($withRelationAttr[$name] as $key => $closure) {
$result[$name][$key] = $closure($result[$name][$key] ?? null, $result[$name]);
}
}
if (!$assoc) {
$result[$name] = (object) $result[$name];
}
}
}

View File

@ -106,12 +106,6 @@ trait Attribute
*/
private $withAttr = [];
/**
* 数据处理
* @var array
*/
private $filter = [];
/**
* 获取模型对象的主键
* @access public
@ -183,24 +177,6 @@ trait Attribute
return $this;
}
/**
* 设置模型数据处理
* @access public
* @param callable $filter 数据处理Callable
* @param string $index 索引(唯一)
* @return $this
*/
public function filter(callable $filter, string $index = null)
{
if ($index) {
$this->filter[$index] = $filter;
} else {
$this->filter[] = $filter;
}
return $this;
}
/**
* 获取实际的字段名
* @access protected
@ -274,6 +250,17 @@ trait Attribute
return $this;
}
/**
* 刷新对象原始数据(为当前数据)
* @access public
* @return $this
*/
public function refreshOrigin()
{
$this->origin = $this->data;
return $this;
}
/**
* 获取对象原始数据 如果不存在指定字段返回null
* @access public
@ -555,6 +542,10 @@ trait Attribute
*/
protected function getJsonValue($name, $value)
{
if (is_null($value)) {
return $value;
}
foreach ($this->withAttr[$name] as $key => $closure) {
if ($this->jsonAssoc) {
$value[$key] = $closure($value[$key], $value);

View File

@ -291,14 +291,13 @@ trait RelationShip
/**
* 预载入关联查询 返回模型对象
* @access public
* @param Model $result 数据对象
* @param array $relations 关联
* @param array $withRelationAttr 关联获取器
* @param bool $join 是否为JOIN方式
* @param mixed $cache 关联缓存
* @return void
*/
public function eagerlyResult(Model $result, array $relations, array $withRelationAttr = [], bool $join = false, $cache = false): void
public function eagerlyResult(array $relations, array $withRelationAttr = [], bool $join = false, $cache = false): void
{
foreach ($relations as $key => $relation) {
$subRelation = [];
@ -333,7 +332,7 @@ trait RelationShip
$relationCache = $cache[$relationName] ?? [];
}
$relationResult->eagerlyResult($result, $relationName, $subRelation, $closure, $relationCache, $join);
$relationResult->eagerlyResult($this, $relationName, $subRelation, $closure, $relationCache, $join);
}
}

View File

@ -11,9 +11,9 @@
<div class="fly-panel" style="margin-bottom: 0;">
<div class="layui-tab-card">
<ul class="layui-tab-title">
<li {if condition="$type eq 'all'" } class="layui-this" {/if}><a href="{:url('article/cate',['ename' => $Request.param.ename ?:'all','type' => 'all'])} " >{:lang('all')}</a></li>
<li {if condition="$type eq 'jie'" } class="layui-this" {/if}><a href="{:url('article/cate',['ename' => $Request.param.ename ?:'all','type' => 'jie'])} ">{:lang('end')}</a></li>
<li {if condition="$type eq 'wait'" } class="layui-this" {/if}><a href="{:url('article/cate',['ename' => $Request.param.ename ?:'all' ,'type' => 'wait'])}">{:lang('未解决')}</a> </li>
<li {if condition="$type eq 'all'" } class="layui-this" {/if}><a href="{:url('cate_type',['ename' => $Request.param.ename,'type' => 'all'])} " >{:lang('all')}</a></li>
<li {if condition="$type eq 'jie'" } class="layui-this" {/if}><a href="{:url('cate_type',['ename' => $Request.param.ename,'type' => 'jie'])} ">{:lang('end')}</a></li>
<li {if condition="$type eq 'wait'" } class="layui-this" {/if}><a href="{:url('cate_type',['ename' => $Request.param.ename,'type' => 'wait',])}">{:lang('未解决')}</a> </li>
</ul>
</div>
<ul class="question-list">
@ -90,6 +90,7 @@
<script>
layui.use(['laypage'], function(){
var laypage = layui.laypage;
var $ = layui.jquery;
//执行一个laypage实例
laypage.render({
@ -100,17 +101,13 @@
//获取起始页
,jump: function(obj, first){
//obj包含了当前分页的所有参数比如
//console.log(obj.curr); //得到当前页,以便向服务端请求对应页的数据。
//console.log(obj.limit); //得到每页显示的条数
//首次不执行
if(!first){
//do something
//window.location.href= url+obj.curr+'&limit='+obj.limit+'#collection'; //跳转
window.location.href = '{$url}'+obj.curr+'.html'; //跳转
}
}
var page = obj.curr;
location.href ="{$path}/"+page+'.html';
}
}
});
});
</script>

View File

@ -294,10 +294,9 @@ layui.use(['fly', 'face','colorpicker','plyr', 'laypage'], function(){
//首次不执行
if(!first){
var page = obj.curr;
console.log(page);
var url = "{:url('article/detail',['id'=>$article.id])}";
var url = "{:url('article/detail',['id'=>$article.id])}";
var id = "{$article.id}";
$.post("{:url('article/detail')}",{"page":page,"id":id},function(){
$.post("{:url('article/detail')}",{"id":id,"page":page},function(){
window.location.href = url + '?page=' + page + '#flyReply';
});
}

View File

@ -100,15 +100,10 @@
,curr : "{$artList['current_page']}"
//获取起始页
,jump: function(obj, first){
//obj包含了当前分页的所有参数比如
console.log(obj.curr); //得到当前页,以便向服务端请求对应页的数据。
//console.log(obj.limit); //得到每页显示的条数
//首次不执行
if(!first){
//do something
//window.location.href= url+obj.curr+'&limit='+obj.limit+'#collection'; //跳转
//var page = obj.curr ? obj.curr : 1;
window.location.href = '{$url}'+ obj.curr +'.html'; //跳转
window.location.href = '{$path}/'+ obj.curr +'.html'; //跳转
}
}
});

View File

@ -85,24 +85,19 @@
//执行一个laypage实例
laypage.render({
elem: 'pages' //注意,这里的 test1 是 ID不用加 # 号
,count: "{$artList['total']}" //数据总数,从服务端得到
,limit: "{$artList['per_page']}"
,curr : "{$artList['current_page']}"
//获取起始页
,jump: function(obj, first){
//obj包含了当前分页的所有参数比如
//console.log(obj.curr); //得到当前页,以便向服务端请求对应页的数据。
//console.log(obj.limit); //得到每页显示的条数
//首次不执行
if(!first){
//do something
//window.location.href= url+obj.curr+'&limit='+obj.limit+'#collection'; //跳转
window.location.href = '{$url}'+obj.curr+'.html'; //跳转
}
}
elem: 'pages' //注意,这里的 test1 是 ID不用加 # 号
,count: "{$artList['total']}" //数据总数,从服务端得到
,limit: "{$artList['per_page']}"
,curr : "{$artList['current_page']}"
//获取起始页
,jump: function(obj, first){
//首次不执行
if(!first){
window.location.href = '{$path}/'+obj.curr+'.html'; //跳转
}
}
});
});
</script>

View File

@ -271,8 +271,9 @@ layui.use(['fly', 'face','colorpicker','plyr', 'laypage'], function(){
//首次不执行
if(!first){
var page = obj.curr;
var url = "{:url('article/detail',['id'=>$article.id])}"
$.post(url,{"page":page},function(){
var url = "{:url('article/detail',['id'=>$article.id])}";
var id = "{$article.id}";
$.post("{:url('article/detail')}",{"id":id,"page":page},function(){
window.location.href = url + '?page=' + page + '#flyReply';
});
}

View File

@ -35,7 +35,7 @@
</ul>
<div style="text-align: center">
<div class="laypage-main">
<a href="{:url('article/cate',['ename'=>'all','page'=>1])}" class="laypage-next">{:lang('more post')}</a>
<a href="{:url('article/cate',['ename'=>'all'])}" class="laypage-next">{:lang('more post')}</a>
</div>
</div>
</div>

View File

@ -37,7 +37,7 @@
layui.cache.user = {
username: "{$user.name??'游客'}"
,uid: "{$user.id ?? -1}"
,avatar: "{$user['user_img'] ?? '/static/res/images/avatar/00.jpg'}"
,avatar: '/static/res/images/avatar/00.jpg'
,experience: "{$user.point ?? ''}"
,sex: "{$user.sex ? '女':'男'}"
};

View File

@ -1,11 +1,11 @@
<div class="fly-panel-title fly-filter">
<a href="{:url('article/cate',['ename' => $Request.param.ename ?:'all' ,'type' => 'all'])} " {if condition="$type eq 'all'" } class="layui-this" {/if} >{:lang('all')}</a>
<a href="{:url('cate_type',['ename' => $Request.param.ename,'type' => 'all'])} " {if condition="$type eq 'all'" } class="layui-this" {/if} >{:lang('all')}</a>
<span class="fly-mid"></span>
<a href="{:url('article/cate',['ename' => $Request.param.ename ?:'all','type' => 'jie'])} " {if condition="$type eq 'jie'" } class="layui-this" {/if} >{:lang('end')}</a>
<a href="{:url('cate_type',['ename' => $Request.param.ename,'type' => 'jie'])} " {if condition="$type eq 'jie'" } class="layui-this" {/if} >{:lang('end')}</a>
<span class="fly-mid"></span>
<a href="{:url('article/cate',['ename' => $Request.param.ename ?:'all','type' => 'top'])} " {if condition="$type eq 'top'" } class="layui-this" {/if} >{:lang('top')}</a>
<a href="{:url('cate_type',['ename' => $Request.param.ename,'type' => 'top'])} " {if condition="$type eq 'top'" } class="layui-this" {/if} >{:lang('top')}</a>
<span class="fly-mid"></span>
<a href="{:url('article/cate',['ename' => $Request.param.ename ?:'all','type' => 'hot'])} " {if condition="$type eq 'hot'" } class="layui-this" {/if} >{:lang('hot')}</a>
<a href="{:url('cate_type',['ename' => $Request.param.ename,'type' => 'hot'])} " {if condition="$type eq 'hot'" } class="layui-this" {/if} >{:lang('hot')}</a>
<!--span class="fly-filter-right layui-hide-xs"><a href="" class="layui-this">按最新</a>
<span class="fly-mid"></span><a href="">按热议</a></span-->