1、完善前端模板功能

2、完善伪静态规则设置
This commit is contained in:
2020-04-17 16:46:32 +08:00
parent ce9b63dda1
commit 83ce571a3f
6 changed files with 135 additions and 57 deletions

View File

@@ -83,6 +83,40 @@ function get_client_ip($type = 0, $adv = false) {
return $ip[$type];
}
/**
* 字符串截取,支持中文和其他编码
* @static
* @access public
* @param string $str 需要转换的字符串
* @param string $start 开始位置
* @param string $length 截取长度
* @param string $charset 编码格式
* @param string $suffix 截断显示字符
* @return string
*/
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true) {
if (function_exists("mb_substr")) {
$slice = mb_substr($str, $start, $length, $charset);
} elseif (function_exists('iconv_substr')) {
$slice = iconv_substr($str, $start, $length, $charset);
if (false === $slice) {
$slice = '';
}
} else {
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("", array_slice($match[0], $start, $length));
}
if (strlen($slice) == strlen($str)) {
return $slice;
} else {
return $suffix ? $slice . '...' : $slice;
}
}
/**
* 根据用户ID获取用户名
* @param integer $uid 用户ID
@@ -202,36 +236,20 @@ function get_attach($id, $field = null) {
if (empty($id)) {
return $basePath . '/static/images/default.png';
}
$picture = \think\facade\Db::name('Attach')->where(array('id' => $id))->find();
if (false !== strpos($id, ",")) {
$map[] = ['id', 'IN', explode(",", $id)];
$picture = \think\facade\Db::name('Attach')->where($map)->column("*", "id");
return $picture;
}else{
$map[] = ['id', '=', $id];
$picture = \think\facade\Db::name('Attach')->where($map)->find();
if ($field == 'path') {
if (!empty($picture['url'])) {
$picture['path'] = $picture['url'] ? $$basePath . $picture['url'] : $$basePath . '/static/images/default.png';
$picture['path'] = $picture['url'] ? $basePath . $picture['url'] : $basePath . '/static/images/default.png';
} else {
$picture['path'] = $picture['path'] ? $$basePath . $picture['path'] : $$basePath . '/static/images/default.png';
$picture['path'] = $picture['path'] ? $basePath . $picture['path'] : $basePath . '/static/images/default.png';
}
}
return empty($field) ? $picture : $picture[$field];
}
/**
* 获取文档封面图片
* @param int $cover_id
* @param string $field
* @return 完整的数据 或者 指定的$field字段值
* @author huajie <banhuajie@163.com>
*/
function get_cover($cover_id, $field = null) {
if (empty($cover_id)) {
return BASE_PATH . '/static/images/default.png';
}
$base_path = "";
$picture = \think\facade\Db::name('Picture')->where(array('status' => 1, 'id' => $cover_id))->find();
if ($field == 'path') {
if (!empty($picture['url'])) {
$picture['path'] = $picture['url'] ? $base_path . $picture['url'] : $base_path . '/static/images/default.png';
} else {
$picture['path'] = $picture['path'] ? $base_path . $picture['path'] : $base_path . '/static/images/default.png';
}
}
return empty($field) ? $picture : $picture[$field];
}

View File

@@ -33,6 +33,15 @@ class Base extends BaseC {
$this->tpl_config['view_depr'] = '/';
$this->tpl_config['view_dir_name'] = 'addons' . DIRECTORY_SEPARATOR . $this->request->param('addon') . DIRECTORY_SEPARATOR . 'view';
}
$template_path = str_replace("public", "", $this->tpl_config['view_dir_name']);
$this->tpl_config['tpl_replace_string'] = [
'__static__' => '/static',
'__img__' => $template_path . DIRECTORY_SEPARATOR . 'static/images',
'__css__' => $template_path . DIRECTORY_SEPARATOR . 'static/css',
'__js__' => $template_path . DIRECTORY_SEPARATOR . 'static/js',
'__plugins__' => '/static/plugins',
'__public__' => $template_path . DIRECTORY_SEPARATOR . 'static',
];
View::config($this->tpl_config);
View::assign($this->data);

View File

@@ -35,13 +35,16 @@ class Content extends Base {
$category = Category::where('model_id', $this->modelInfo['id'])->column("*", "id");
$list = $this->model->where($map)->order($order)->paginate($this->request->pageConfig);
$teamplate = 'front@content/' . $this->modelInfo['name'] . '/index';
$this->data = [
'model' => $this->modelInfo,
'category' => $category,
'list' => $list,
'page' => $list->render()
];
return $this->fetch();
return $this->fetch($teamplate);
}
/**
@@ -65,6 +68,15 @@ class Content extends Base {
}
$list = $this->model->where($map)->order($order)->paginate($this->request->pageConfig);
//当前栏目
$cate = $category[$param['id']];
if (isset($cate['template_lists']) && $cate['template_lists']) {
$teamplate = 'front@content/' . $this->modelInfo['name'] . '/' . $cate['template_lists'];
} else {
$teamplate = 'front@content/' . $this->modelInfo['name'] . '/list';
}
$this->data = [
'model' => $this->modelInfo,
'id' => (int) $param['id'],
@@ -72,7 +84,7 @@ class Content extends Base {
'list' => $list,
'page' => $list->render()
];
return $this->fetch();
return $this->fetch($teamplate);
}
/**
@@ -86,24 +98,39 @@ class Content extends Base {
$map[] = ['id', "=", $param['id']];
$detail = $this->model->where($map)->find();
if (isset($detail['category_id']) && $detail['category_id']) {
$pmap = [
['category_id', '=', $detail['category_id']],
['id', '<', $param['id']]
];
$prev = Db::name(ucfirst($this->modelInfo['name']))->where($pmap)->order('id desc')->find();
$nmap = [
['category_id', '=', $detail['category_id']],
['id', '>', $param['id']]
];
}else{
$pmap = [
['id', '<', $param['id']]
];
$nmap = [
['id', '>', $param['id']]
];
}
$prev = Db::name(ucfirst($this->modelInfo['name']))->where($pmap)->order('id desc')->find();
$next = Db::name(ucfirst($this->modelInfo['name']))->where($nmap)->order('id asc')->find();
if (isset($detail['template_detail']) && $detail['template_detail']) {
$teamplate = 'front@content/' . $this->modelInfo['name'] . '/' . $detail['template_detail'];
} else {
$teamplate = 'front@content/' . $this->modelInfo['name'] . '/detail';
}
$this->data = [
'model' => $this->modelInfo,
'info' => $detail,
'prev' => $prev,
'next' => $next
];
return $this->fetch();
return $this->fetch($teamplate);
}
/**
@@ -133,7 +160,7 @@ class Content extends Base {
}
protected function setModel(){
$this->modelInfo = Model::where('name', $this->request->param('name'))->find()->append(['grid_list', 'attr_group'])->toArray();
$this->modelInfo = Model::where('id', $this->request->param('model_id'))->find()->append(['grid_list', 'attr_group'])->toArray();
$this->model = Db::name($this->modelInfo['name']);
}
}

View File

@@ -42,11 +42,11 @@ class Base extends BaseC {
if ($this->isMobile() && $config['mobile_themes']) {
$mobile_themes = $config['mobile_themes'] ? $config['mobile_themes'] . DIRECTORY_SEPARATOR : "";
$this->tpl_config['view_dir_name'] = 'public' . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $mobile_themes;
if (!is_dir($this->app->getRootPath() . $this->tpl_config['view_dir_name'])) {
if (!file_exists($this->app->getRootPath() . $this->tpl_config['view_dir_name'])) {
$this->tpl_config['view_dir_name'] = 'public' . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . $pc_themes;
}
}
if(!is_dir($this->app->getRootPath() . $this->tpl_config['view_dir_name'])){
if(!file_exists($this->app->getRootPath() . $this->tpl_config['view_dir_name'] . DIRECTORY_SEPARATOR . 'user')){
$this->tpl_config['view_dir_name'] = 'public' . DIRECTORY_SEPARATOR . 'template' . DIRECTORY_SEPARATOR . 'default';
}
if ($template == '') {
@@ -55,11 +55,11 @@ class Base extends BaseC {
$template_path = str_replace("public", "", $this->tpl_config['view_dir_name']);
$this->tpl_config['tpl_replace_string'] = [
'__static__' => '/static',
'__img__' => $template_path . 'static/images',
'__css__' => $template_path . 'static/css',
'__js__' => $template_path . 'static/js',
'__img__' => $template_path . DIRECTORY_SEPARATOR . 'static/images',
'__css__' => $template_path . DIRECTORY_SEPARATOR . 'static/css',
'__js__' => $template_path . DIRECTORY_SEPARATOR . 'static/js',
'__plugins__' => '/static/plugins',
'__public__' => $template_path . 'static',
'__public__' => $template_path . DIRECTORY_SEPARATOR . 'static',
];
View::config($this->tpl_config);

View File

@@ -36,9 +36,9 @@ class Sent extends \think\template\TagLib {
public function tagdoc($tag, $content){
$model = !empty($tag['model']) ? $tag['model']:'';
$cid = (!empty($tag['cid']) && is_integer($tag['limit'])) ? $tag['cid']:'0';
$cid = isset($tag['cid']) ? (int) $tag['cid'] : 20;
$field = empty($tag['field']) ? '*' : $tag['field'];
$limit = (!empty($tag['limit']) && is_integer($tag['limit'])) ? $tag['limit'] : 20;
$limit = isset($tag['limit']) ? (int) $tag['limit'] : 20;
$order = empty($tag['order']) ? 'id desc' : $tag['order'];
$name = isset($tag['name']) ? $tag['name'] : 'item';

View File

@@ -9,10 +9,18 @@
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
use think\facade\Route;
use think\facade\Cache;
use app\model\Model;
use app\model\Rewrite;
$model = Cache::get('model_list');
if (!$model) {
$model = Model::where('status', '>', 0)->field(['id', 'name'])->select()->toArray();
Cache::set('model_list', $model);
}
if (!empty($model)) {
foreach ($model as $value) {
Route::rule('/admin/' . $value['name'] . '/:function', 'admin.content/:function')->append(['name'=>$value['name'], 'model_id' => $value['id']]);
Route::rule($value['name'] . '/index', 'front.content/index')->append(['name'=>$value['name'], 'model_id' => $value['id']]);
@@ -20,6 +28,22 @@ foreach ($model as $value) {
Route::rule($value['name'] . '/detail-:id', 'front.content/detail')->append(['name'=>$value['name'], 'model_id' => $value['id']]);
Route::rule('/user/' . $value['name'] . '/:function', 'user.content/:function')->append(['name'=>$value['name'], 'model_id' => $value['id']]);
}
}
$rewrite = Cache::get('rewrite_list');
if (!$rewrite) {
$rewrite = Rewrite::select()->toArray();
Cache::set('rewrite_list', $rewrite);
}
if (!empty($rewrite)) {
foreach ($rewrite as $key => $value) {
$url = parse_url($value['url']);
$param = [];
parse_str($url['query'], $param);
Route::rule($value['rule'], $url['path'])->append($param);
}
}
Route::rule('/', 'front.Index/index');
Route::rule('search', 'front.Content/search');