From c0f7fe512f91e25dfd83ee7f7a88ec54c315cc87 Mon Sep 17 00:00:00 2001 From: taoser Date: Sun, 1 Nov 2020 18:13:05 +0800 Subject: [PATCH] language --- app/admin/view/auth_group/role.html | 2 +- app/common/lib/Msg.php | 57 ++++++++++++++++++++++++++++ app/index/controller/Article.php | 11 +++++- app/index/controller/Index.php | 22 ++++++++++- app/index/controller/Lang.php | 32 ++++++++++++++++ app/index/lang/en-us.php | 36 ++++++++++++++++++ app/index/lang/zh-cn.php | 39 +++++++++++++++++++ app/index/lang/zh-tw.php | 9 +++++ app/middleware.php | 2 +- app/middleware/Auth.php | 49 +++++++++++------------- config/lang.php | 2 +- view/taoler/index/index/index.html | 19 ++++++++-- view/taoler/index/public/column.html | 10 ++--- view/taoler/index/public/filter.html | 8 ++-- view/taoler/index/public/flink.html | 2 +- view/taoler/index/public/header.html | 31 +++++++++------ view/taoler/index/public/js.html | 4 +- 17 files changed, 277 insertions(+), 58 deletions(-) create mode 100644 app/common/lib/Msg.php create mode 100644 app/index/controller/Lang.php create mode 100644 app/index/lang/en-us.php create mode 100644 app/index/lang/zh-cn.php create mode 100644 app/index/lang/zh-tw.php diff --git a/app/admin/view/auth_group/role.html b/app/admin/view/auth_group/role.html index f0b077d..b605017 100644 --- a/app/admin/view/auth_group/role.html +++ b/app/admin/view/auth_group/role.html @@ -71,7 +71,7 @@ //搜索角色 form.on('select(LAY-user-adminrole-type)', function(data){ - console.log(data); + $.ajax({ type:"post", url:"{:url('admin/AuthGroup/list')}", diff --git a/app/common/lib/Msg.php b/app/common/lib/Msg.php new file mode 100644 index 0000000..0705fb7 --- /dev/null +++ b/app/common/lib/Msg.php @@ -0,0 +1,57 @@ + 1, + 'error' => 0, + + ]; + + foreach($res as $k => $v){ + if($k == $strCode){ + return $res = $v; + } + } + //return $res; + } + + public static function getMsg($strMsg){ + //状态配置 + $res = [ + 'add_success' => Lang::get('add success'), + 'add_error' => Lang::get('add error'), + 'edit_success' => Lang::get('edit success'), + 'edit_error' => Lang::get('edit error'), + + ]; + + foreach($res as $k => $v){ + if($k == $strMsg){ + return $res = $v; + } + } + //$res; + } + + public static function show($strCode,$strMsg,$url) + { + $res = [ + 'code' => self::getCode($strCode), + 'msg' => self::getMsg($strMsg), + 'url' => $url, + ]; + + return json($res); + } + + +} diff --git a/app/index/controller/Article.php b/app/index/controller/Article.php index c99ff30..5e75ff8 100644 --- a/app/index/controller/Article.php +++ b/app/index/controller/Article.php @@ -14,6 +14,8 @@ use taoler\com\Message; use app\common\model\Cate; use app\common\model\User; use app\common\model\Collection; +use think\facade\Lang; +use app\common\lib\Msg; class Article extends BaseController { @@ -250,7 +252,9 @@ class Article extends BaseController $link = (string) url('article/detail',['id'=> $aid]); //清除文章tag缓存 Cache::tag('tagArtDetail')->clear(); - return json(['code'=>1,'msg'=>'发布成功','url'=> $link]); + + //return json(['code'=>1,'msg'=>'发布成功','url'=> $link]); + return json(['code'=>Msg::getCode('success'),'msg'=>Msg::getMsg('add_success'),'url'=> $link]); } else { $this->error($result); } @@ -322,7 +326,10 @@ class Article extends BaseController //删除缓存显示编辑后内容 Cache::delete('article_'.$id); $link = (string) url('article/detail',['id'=> $id]); - return json(['code'=>0,'msg'=>'修改成功','url'=> $link]); + //return json(['code'=>0,'msg'=>'修改成功','url'=> $link]); + return Msg::show('error','edit_success',$link); + + } else { $this->error($result); } diff --git a/app/index/controller/Index.php b/app/index/controller/Index.php index 8f4b252..fc09cf2 100644 --- a/app/index/controller/Index.php +++ b/app/index/controller/Index.php @@ -10,6 +10,7 @@ use app\common\model\Article; use app\common\model\User; use app\common\model\Cate; use app\common\model\Comment; +use think\facade\Cookie; class Index extends BaseController { @@ -121,7 +122,6 @@ class Index extends BaseController //搜索功能 public function search() { - //$this->showUser(); //全局查询条件 $map = []; //所有的查询条件封装到数组中 //条件1: @@ -162,5 +162,25 @@ class Index extends BaseController return redirect((string) url('user/home',['id'=>$u['id']])); } + + public function select() + { + $lang = input('language'); + //dump($lang); + switch ($lang) { + case 'cn': + Cookie::set('think_lang','zh-cn'); + break; + case 'en': + Cookie::set('think_lang','en-us'); + break; + case 'tw': + Cookie::set('think_lang','zh-tw'); + + break; + default: + break; + } + } } diff --git a/app/index/controller/Lang.php b/app/index/controller/Lang.php new file mode 100644 index 0000000..db42227 --- /dev/null +++ b/app/index/controller/Lang.php @@ -0,0 +1,32 @@ + 'index', + 'home_page' => 'home page', + 'user_center' => 'center', + 'set_info' => 'set info', + 'my_message' => 'message', + 'my_page' => 'my page', + + 'login' => 'login', + 'logout' => 'logout', + 'register' => 'register', + 'discuss' => 'discuss', + 'case' => 'case', + 'timeline' => 'timeline', + + //帖子 + 'add_post' => 'add post', + 'my_collection' => 'my collection', + 'all' => 'all', + 'over' => 'over', + 'hot' => 'hot', + 'top' => 'top', + 'go_sign' => 'go sign', + 'more_post' => 'more post', + 'friendly_link' => 'friendly link', + + + //message + 'add success' => 'add success!', + 'add error' => 'add error', + 'edit success' => 'articel edit success', + 'edit error' => 'articel edit error', +]; \ No newline at end of file diff --git a/app/index/lang/zh-cn.php b/app/index/lang/zh-cn.php new file mode 100644 index 0000000..223ee36 --- /dev/null +++ b/app/index/lang/zh-cn.php @@ -0,0 +1,39 @@ + 'index', + 'home_page' => '首页', + 'user_center' => '用户中心', + 'set_info' => '设置', + 'my_message' => '我的消息', + 'my_page' => '我的主页', + + 'login' => '登录', + 'logout' => '退出', + 'register' => '注册', + 'discuss' => '讨论', + 'case' => '案例', + 'timeline' => '框架日志', + + //帖子 + 'add_post' => '添加帖子', + 'my_collection' => '我的收藏', + 'all' => '综合', + 'over' => '已结', + 'hot' => '热贴', + 'top' => '精贴', + 'go_sign' => '去签到', + 'more_post' => '更多帖子', + 'friendly_link' => '友情链接', + + + + + 'hello thinkphp' => '欢迎使用ThinkPHP', + 'data type error' => '数据类型错误', + 'add success' => '添加成功!', + 'add error' => '添加失败', + 'edit success' => '修改成功', + 'edit error' => '修改失败', +]; \ No newline at end of file diff --git a/app/index/lang/zh-tw.php b/app/index/lang/zh-tw.php new file mode 100644 index 0000000..212a3ca --- /dev/null +++ b/app/index/lang/zh-tw.php @@ -0,0 +1,9 @@ + '数据类型错误', + 'add success' => '添加成功', + 'add error' => '添加失敗', + 'edit success' => '修改成功', + 'edit error' => '修改失敗', +]; \ No newline at end of file diff --git a/app/middleware.php b/app/middleware.php index c8d321e..fa33fae 100644 --- a/app/middleware.php +++ b/app/middleware.php @@ -4,7 +4,7 @@ return [ // 全局请求缓存 // \think\middleware\CheckRequestCache::class, // 多语言加载 - // \think\middleware\LoadLangPack::class, + \think\middleware\LoadLangPack::class, // Session初始化 \think\middleware\SessionInit::class ]; diff --git a/app/middleware/Auth.php b/app/middleware/Auth.php index 637f87d..e91e824 100644 --- a/app/middleware/Auth.php +++ b/app/middleware/Auth.php @@ -17,35 +17,32 @@ class Auth */ public function handle($request, \Closure $next) { - $response = $next($request); - //后置中间件获取应用控制器方法 - $app = app('http')->getName(); - $controller = $request->controller(); - $action = $request->action(); + $path = app('http')->getName().'/'.stristr($request->pathinfo(),".html",true); - //登陆后检验权限 - if (Session::has('admin_id')) { - // 排除权限 - $not_check = ['admin/Login/index','admin/Index/index','admin/Index/home','admin/Set/info','admin/Set/password','admin/User/logout']; + //没有登录及当前非登录页重定向登录页 + if(!Session::has('admin_id') && $path !== 'admin/login/index' && !stristr($request->pathinfo(),"captcha.html") ) + { + return redirect((string) url('admin/login/index')); + } + + //登陆后无法访问登录页 + if(Session::has('admin_id') && $path == 'admin/login/index'){ + return redirect((string) url('admin/index/index')); + } + + // 排除公共权限 + $not_check = ['admin/login/index','admin/index/index','admin/index/home','admin/Set/info','admin/Set/password','admin/Admin/logout','admin/captcha']; - if (!in_array($app . '/' . $controller . '/' . $action, $not_check)) { - $auth = new UserAuth(); - $admin_id = Session::get('admin_id'); //登录用户的id + if (!in_array($path, $not_check)) { + $auth = new UserAuth(); + $admin_id = Session::get('admin_id'); //登录用户的id - if (!$auth->check($app . '/' . $controller . '/' . $action, $admin_id) && $admin_id != 1) { - //return json(['code'=>-1,'msg'=>'没有权限!']); - return view('public/auth'); - } + if (!$auth->check($path, $admin_id) && $admin_id != 1) { + return view('public/auth'); + //return json(['status'=>-1,'msg'=>'没有权限!']); + //return response(""); } - - } else { - //排除登录页和验证码及退出登录后被重定向 - if($controller !== 'Login' && !stristr($request->pathinfo(),"captcha.html") && $action !== 'logout') - { - //非登录重定向 - return redirect((string) url('admin/login/index')); - } - } - return $response; + } + return $next($request); } } diff --git a/config/lang.php b/config/lang.php index e3b4986..c386937 100644 --- a/config/lang.php +++ b/config/lang.php @@ -7,7 +7,7 @@ return [ // 默认语言 'default_lang' => env('lang.default_lang', 'zh-cn'), // 允许的语言列表 - 'allow_lang_list' => [], + 'allow_lang_list' => ['zh-cn', 'en-us', 'zh-tw'], // 多语言自动侦测变量名 'detect_var' => 'lang', // 是否使用Cookie记录 diff --git a/view/taoler/index/index/index.html b/view/taoler/index/index/index.html index 914ee50..2b0e3b0 100644 --- a/view/taoler/index/index/index.html +++ b/view/taoler/index/index/index.html @@ -17,8 +17,8 @@
@@ -101,10 +101,12 @@ {block name="script"}