共计 1805 个字符,预计需要花费 5 分钟才能阅读完成。
应用 easyWeChat 进行微信网页的受权登录
public function __construct (Request $request = null){parent::__construct();
$this->config = [
'app_id' => 'wx11ca*****98df5',
'secret' => 'f860284********2d732d0beae9',
// 指定 API 调用返回后果的类型:array(default)/collection/object/raw/ 自定义类名
// 'response_type' => 'array',
'oauth' => ['scopes' => ['snsapi_userinfo'],
'callback' => '/wx_callback',
],
];
}
// 登录操作
public function login(){if(session('wechat_user')){$this->redirect('/index2',302);
}
$app = Factory::officialAccount($this->config);
// 未登录, 跳转到登录界面
if (!session('wechat_user')) {
$oauth = $app->oauth;
return $oauth->redirect();}
}
// 保留登录信息
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';}
}
// 首页
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);
}
呈现的问题
关上的页面呈现如下信息
找到如下目录, 能够暗藏或者删除 html
高颜值后盾管理系统收费应用 ### 子枫后盾管理系统 ###,可在宝塔面板间接装置
欢送关注我的公众号:子枫的微妙世界,取得独家整顿的学习资源和日常干货推送。
如果您对我的其余专题内容感兴趣,中转我的集体博客:www.wangmingchang.com。
正文完