2024-08-09 22:16:39 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\controller;
|
|
|
|
|
|
|
|
use support\Request;
|
|
|
|
use support\Db;
|
|
|
|
use yzh52521\EasyHttp\Http;
|
|
|
|
use yzh52521\EasyHttp\Response;
|
|
|
|
use yzh52521\EasyHttp\RequestException;
|
|
|
|
|
|
|
|
class LayAuth
|
|
|
|
{
|
|
|
|
public function index(Request $request,$appid)
|
|
|
|
{
|
|
|
|
$appquery= Db::table('App')->where('ID', $appid);
|
|
|
|
if($appquery->doesntExist()){
|
|
|
|
return view('404');
|
|
|
|
}
|
|
|
|
$app=$appquery->first();
|
|
|
|
$provider= Db::table('Provider')->where('ID', $app->provider)->first();
|
2024-08-11 17:48:45 +08:00
|
|
|
$redirecturl='https://'.getenv('weburl').'/auth/lay/'.$appid.'/callback';
|
|
|
|
return view('auth', ['app'=>$app,'provider'=>$provider,'redirecturl'=>$redirecturl]);
|
2024-08-09 22:16:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function callback(Request $request,$appid,$gateway)
|
|
|
|
{
|
|
|
|
$appquery= Db::table('App')->where('ID', $appid);
|
|
|
|
if($appquery->doesntExist()){
|
|
|
|
return view('404');
|
|
|
|
}
|
|
|
|
$app=$appquery->first();
|
|
|
|
$provider= Db::table('Provider')->where('ID', $app->provider)->first();
|
2024-08-11 17:48:45 +08:00
|
|
|
$redirecturl='https://'.getenv('weburl').'/auth/lay/'.$appid.'/callback';
|
2024-08-09 22:16:39 +08:00
|
|
|
switch ($gateway) {
|
|
|
|
case "qywx":
|
|
|
|
$code = $request->input('code','null');
|
|
|
|
if($code=='null'){
|
2024-08-11 17:48:45 +08:00
|
|
|
return view('auth', ['app'=>$app,'provider'=>$provider,'special'=>'登陆信息无效','redirecturl'=>$redirecturl]);
|
2024-08-09 22:16:39 +08:00
|
|
|
}
|
|
|
|
$tokenfile=base_path().'/token/qywx/innerQYWX.token';
|
|
|
|
if(file_exists($tokenfile)){
|
|
|
|
$tokencontent=json_decode(file_get_contents($tokenfile));
|
|
|
|
$fulltoken=$tokencontent->token;
|
|
|
|
$ddl=$tokencontent->ddl;
|
|
|
|
if($ddl-time()<180){
|
|
|
|
$reapply=true;
|
|
|
|
}else{
|
|
|
|
$reapply=false;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
$reapply=true;
|
|
|
|
}
|
|
|
|
if($reapply==true){
|
|
|
|
$apply=$response = Http::get('https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.getenv('appid').'&corpsecret='.getenv('token'))->json();
|
|
|
|
$fulltoken=$apply->access_token;
|
|
|
|
$ddl=time()+$apply->expires_in;
|
|
|
|
$file=fopen($tokenfile,"w");
|
|
|
|
fwrite($file, json_encode(array('token'=>$fulltoken,'ddl'=>$ddl)));
|
|
|
|
fclose($file);
|
|
|
|
}
|
|
|
|
$lookup= Http::get('https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token='.$fulltoken.'&code='.$code)->json();
|
|
|
|
if($lookup->errcode!=0){
|
2024-08-11 17:48:45 +08:00
|
|
|
return view('auth', ['app'=>$app,'provider'=>$provider,'special'=>'登陆信息无效','redirecturl'=>$redirecturl]);
|
2024-08-09 22:16:39 +08:00
|
|
|
}else{
|
|
|
|
$userid=$lookup->userid;
|
|
|
|
$userinfo=Http::get('https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token='.$fulltoken.'&userid='.$userid)->json();
|
|
|
|
$username=$userinfo->name;
|
|
|
|
$userposition=$userinfo->position;
|
|
|
|
$WT=json_encode(['id'=>$userid,'name'=>$username,'position'=>$userposition,'time'=>time()]);
|
|
|
|
$key = getenv('aeskey');
|
|
|
|
$iv = getenv('aesiv');
|
|
|
|
$WT = encryptAES($WT, $key, $iv);
|
|
|
|
$dest=$app->redirect;
|
|
|
|
return view('success', ['app'=>$app,'provider'=>$provider,'dest'=>$dest,'userinfo'=>$userinfo])->cookie('WT', $WT,time()+9600,'/','.laysense.cn');
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2024-08-11 17:48:45 +08:00
|
|
|
return view('auth', ['app'=>$app,'provider'=>$provider,'special'=>'验证方式无效或不存在','redirecturl'=>$redirecturl]);
|
2024-08-09 22:16:39 +08:00
|
|
|
}
|
|
|
|
|
2024-08-11 17:48:45 +08:00
|
|
|
return view('auth', ['app'=>$app,'provider'=>$provider,'special'=>'验证方式无效或不存在','redirecturl'=>$redirecturl]);
|
2024-08-09 22:16:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function check(Request $request,$appid)
|
|
|
|
{
|
|
|
|
return json(['code' => 0, 'msg' => 'ok']);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|