环境:fastadmin安装了第三方登录插件、表单可视化管理插件。/lXCGBIGhDfpLwyDu5LJ9w==
目的:如何实现前端可视化表单微信浏览自动微信登录。W29x3vBz9wlwFgItleW83A==
一:
application/index/controller/formdesign/Foreign.php做如下修改
引入:
use thinkRequest;
use thinkCache;
use appcommonlibraryAuth;
use thinkConfig;
use thinkController;
use thinkHook;
use thinkLang;
use thinkLoader;
use thinkValidate;12tXv61trhheQJZZrknoKA==
将frontend的重写,而不是直接parent::_initialize()引入
public function _initialize()PQbBGcTN0vOPfOrrd5HquQ=={
//parent::_initialize();
//移除HTML标签
$this->request->filter('trim,strip_tags,htmlspecialchars');
$modulename = $this->request->module();
$controllername = Loader::parseName($this->request->controller());
$actionname = strtolower($this->request->action());
// 如果有使用模板布局
if ($this->layout) {
$this->view->engine->layout('layout/' . $this->layout);
}
$this->auth = Auth::instance();
// token
$token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
$path = str_replace('.', '/', $controllername) . '/' . $actionname;
// 设置当前请求的URI
$this->auth->setRequestUri($path);
// 检测是否需要验证登录
if (!$this->auth->match($this->noNeedLogin)) {
//初始化
$this->auth->init($token);
//检测是否登录
if (!$this->auth->isLogin()) {
//获取当前url
Cache::set('redirecturl',Request::instance()->url(),3600); //Cache方法
Session::set('redirecturl', Request::instance()->url()); //session方法无效,在不同控制器之前无法传值。
//die(Session::pull("redirecturl"));
$this->error(__('Please login first'), 'third/connect/wechat');
}
// 判断是否需要验证权限
if (!$this->auth->match($this->noNeedRight)) {
// 判断控制器和方法判断是否有对应权限
if (!$this->auth->check($path)) {
$this->error(__('You have no permission'));
}
}
} else {
// 如果有传递token才验证是否登录状态
if ($token) {
$this->auth->init($token);
}
}
$this->view->assign('user', $this->auth->getUser());
// 语言检测
$lang = strip_tags($this->request->langset());
$site = Config::get("site");
$upload = \app\common\model\Config::upload();
// 上传信息配置后
Hook::listen("upload_config_init", $upload);
// 配置信息
$config = [
'site' => array_intersect_key($site, array_flip(['name', 'cdnurl', 'version', 'timezone', 'languages'])),
'upload' => $upload,
'modulename' => $modulename,
'controllername' => $controllername,
'actionname' => $actionname,
'jsname' => 'frontend/' . str_replace('.', '/', $controllername),
'moduleurl' => rtrim(url("/{$modulename}", '', false), '/'),
'language' => $lang
];
$config = array_merge($config, Config::get("view_replace_str"));
Config::set('upload', array_merge(Config::get('upload'), $upload));
// 配置信息后
Hook::listen("config_init", $config);
// 加载当前控制器语言包
$this->loadlang($controllername);
$this->assign('site', $site);
$this->assign('config', $config);
Formsafe::safe_gpc();
}
二:/addons/third/controller/index.php
use thinkCache;
// 注销这行代码j5VP+cLSO7hM+Qy7LB5PrQ==//$url = Session::has("redirecturl") ? Session::pull("redirecturl") : url('index/user/index');
// 改为这行代码,改为cachekWkg7MRtV5NfNmewSMG6dA==$url = !empty(Cache::get('redirecturl')) ? Cache::get('redirecturl') : url('index/user/index');
三:分析
1、以上修改可以将任何前端控制器实现微信端自动登录,而不影响系统任何其它功能。
2、tp5的session为何不能在不同的控制器之间传值?
3、以上是我的思路,欢迎留言交流。