Files
wsa_server/controllers/Weixin.php
T
2026-09-14 16:32:07 +08:00

144 lines
4.1 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use EasyWeChat\Foundation\Application as OfficialAccount;
/**
* DNA检测系统微信授权入口首页
* @author luoxuancai
*/
class Weixin extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->model('customer_model');
}
protected function getApp(){
$conf = $this->config->item('weixin_uah');
$config = [
'app_id' => $conf['AppId'],
'secret' => $conf['AppSecert'],
'token' => $conf['Token'],
'aes_key' => $conf['EncodingAESKey'],
'response_type' => 'array',
];
return new OfficialAccount($config);
}
// 发起授权页
public function index()
{
$to_url = $this->input->get('url');
if(empty($to_url)){
die('Url is empty.');
}
//支持静默授权
$scope = !empty($this->input->get('is_base'))?'snsapi_base':'snsapi_userinfo';
//如已登录,则不跳转
if(!empty($this->session->{Customer_model::SESSION_KEY})){
Header("HTTP/1.1 303 See Other");
Header("Location: $to_url");
die;
}
//未登录,则跳转授权,处理会员数据之后再进行一次跳转
$callback_url = base_url().strtolower(__CLASS__).'/callback';
$callback_url.='?url='.urlencode($to_url);
$app = $this->getApp();
$response = $app->oauth->scopes([$scope])->redirect($callback_url);
$response->send();
}
//授权回调页
public function callback()
{
$to_url = $this->input->get('url');
$app = $this->getApp();
try{
$user = $app->oauth->user();
}catch (Exception $e){
die('Error:'.$e->getMessage());
}
if(!$user){
die('User info empty.');
}
if($user->original['scope'] == 'snsapi_base') {
$this->session->{Customer_model::SESSION_KEY_WX_OPEN_ID} = $user->original['openid'];
}else{
$ret = $this->customer_model->weixin_users($user->original);
if(!$ret){
die('Database connect failed.');
}
$this->session->{Customer_model::SESSION_KEY} = $ret;
}
Header("HTTP/1.1 303 See Other");
Header("Location: $to_url");
}
// 直接设置session user id,在微信外登录
public function test()
{
$this->session->{Customer_model::SESSION_KEY} = 1;
die('dfdf');
//$this->session->{Customer_model::SESSION_KEY_WX_OPEN_ID} = 'ddd';
//echo $this->session->{Customer_model::SESSION_KEY_WX_OPEN_ID};die;
}
public function test2()
{
unset($this->session->{Customer_model::SESSION_KEY});
//$this->session->{Customer_model::SESSION_KEY} = 26;
// $this->session->{Customer_model::SESSION_KEY} = 'aadd';
// if(!empty($this->session->{Customer_model::SESSION_KEY})){
// die('222');
// }
}
public function test_get()
{
var_dump($this->session->{Customer_model::SESSION_KEY});
}
//判断是介入还是用户 只有第一次介入的时候才会返回echostr
public function check_sign(){
//这个echostr呢 只有说验证的时候才会echo 如果是验证过之后这个echostr是不存在的字段了
$echoStr = $_GET["echostr"];
if ($this->checkSignature()) {
echo $echoStr;
//如果你不知道是否验证成功 你可以先echo echostr 然后再写一个东西
exit;
}
}
//验证微信开发者模式接入是否成功
private function checkSignature(){
//signature 是微信传过来的 类似于签名的东西
$signature = $_GET["signature"];
//微信发过来的东西
$timestamp = $_GET["timestamp"];
//微信传过来的值 什么用我不知道...
$nonce = $_GET["nonce"];
//定义你在微信公众号开发者模式里面定义的token
$token = $this->Token;
//三个变量 按照字典排序 形成一个数组
$tmpArr = array(
$token,
$timestamp,
$nonce
);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
//哈希加密 在laravel里面是Hash::
$tmpStr = sha1($tmpStr);
//按照微信的套路 给你一个signature没用是不可能的 这里就用得上了
if ($tmpStr == $signature) {
return true; //一定要返回 不要echo 负责微信拿不到返回值导致不通过
} else {
return false;
}
}// checkSignature end
}