Using easywechat to log in to wechat
public function __construct ( Request $request = null ){
parent::__construct();
$this->config = [
'app_id' => 'wx11ca*****98df5',
'secret' => 'f860284********2d732d0beae9',
//Specifies the type of the result returned by the API call: array (default) / collection / object / raw / custom class name
// 'response_type' => 'array',
'oauth' => [
'scopes' => ['snsapi_userinfo'],
'callback' => '/wx_callback',
],
];
}
//Login operation
public function login(){
if(session('wechat_user')){
$this->redirect('/index2',302);
}
$app = Factory::officialAccount($this->config);
//Not logged in, jump to the login interface
if (!session('wechat_user')) {
$oauth = $app->oauth;
return $oauth->redirect();
}
}
//Save login information
public function wx_callback(){
$code = input('code','');
$app = Factory::officialAccount($this->config);
$user = $app->oauth->user()->toArray();
session('wechat_user',$user);
$user_data['openid'] = $user['id'];
$user_data['name'] = $user['name'];
$user_data['pic'] = $user['avatar'];
$user_data['ctime'] = time();
$user_data['ip'] = request()->ip();
$user_data['sex'] = $user['original']['sex'];
$user_data['address'] = $user['original']['country'].'-'.$user['original']['province'].'='.$user['original']['city'];
$is = Db::name('user')->where(['openid'=>$user_data['openid']])->find();
if(!$is){
$res = Db::name('user')->insert($user_data);
$is = Db::name('user')->where(['openid'=>$user_data['openid']])->find();
}
if($is){
$this->redirect('/index2',302);
}else{
echo 'err';
}
}
//Home page
public function index2(){
if(!session('wechat_user')){
$this->redirect('/login',302);
}
$seo['title'] = config()['web']['site_title'];
$seo['keywords'] = config()['web']['site_keywords'];
$seo['description'] = config()['web']['site_description'];
$this->assign('seo', $seo);
$app = Factory::officialAccount($this->config);
$this->assign('app', $app);
return view($this->tpl);
}
Problems arising
The following information appears on the open page
Find the following directory to hide or delete HTML
Free use of high value background management system###Zifeng background management system###It can be directly installed on the pagoda panel
Welcome to my official account: the wonderful world of Zi Feng, exclusive learning resources and daily dry cargo push.
If you are interested in my other topics, please go directly to my personal blog:www.wangmingchang.com 。