diff --git a/app/common.php b/app/common.php index bb33fceb..5da6be9b 100755 --- a/app/common.php +++ b/app/common.php @@ -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 ($field == 'path') { - if (!empty($picture['url'])) { - $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'; + 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'; + } else { + $picture['path'] = $picture['path'] ? $basePath . $picture['path'] : $basePath . '/static/images/default.png'; + } } + return empty($field) ? $picture : $picture[$field]; } - return empty($field) ? $picture : $picture[$field]; -} - -/** - * 获取文档封面图片 - * @param int $cover_id - * @param string $field - * @return 完整的数据 或者 指定的$field字段值 - * @author huajie - */ -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]; } \ No newline at end of file diff --git a/app/controller/front/Base.php b/app/controller/front/Base.php index 51c21266..d8f0e1d6 100644 --- a/app/controller/front/Base.php +++ b/app/controller/front/Base.php @@ -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); diff --git a/app/controller/front/Content.php b/app/controller/front/Content.php index 6c24aaef..809f7078 100644 --- a/app/controller/front/Content.php +++ b/app/controller/front/Content.php @@ -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(); - $pmap = [ - ['category_id', '=', $detail['category_id']], - ['id', '<', $param['id']] - ]; + if (isset($detail['category_id']) && $detail['category_id']) { + $pmap = [ + ['category_id', '=', $detail['category_id']], + ['id', '<', $param['id']] + ]; + $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(); - $nmap = [ - ['category_id', '=', $detail['category_id']], - ['id', '>', $param['id']] - ]; $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']); } } diff --git a/app/controller/user/Base.php b/app/controller/user/Base.php index 59cd688e..2bec6f7b 100644 --- a/app/controller/user/Base.php +++ b/app/controller/user/Base.php @@ -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); diff --git a/extend/com/Sent.php b/extend/com/Sent.php index d2021ef9..82efdaad 100644 --- a/extend/com/Sent.php +++ b/extend/com/Sent.php @@ -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'; diff --git a/route/app.php b/route/app.php index 8520f2e0..e7f9dc69 100755 --- a/route/app.php +++ b/route/app.php @@ -9,16 +9,40 @@ // | Author: liu21st // +---------------------------------------------------------------------- use think\facade\Route; +use think\facade\Cache; use app\model\Model; +use app\model\Rewrite; -$model = Model::where('status', '>', 0)->field(['id', 'name'])->select()->toArray(); -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']]); - Route::rule($value['name'] . '/list/:id', 'front.content/lists')->append(['name'=>$value['name'], 'model_id' => $value['id']]); - 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']]); +$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']]); + Route::rule($value['name'] . '/list/:id', 'front.content/lists')->append(['name'=>$value['name'], 'model_id' => $value['id']]); + 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');