Files
annnj-company 130c1026c4 first commit
2026-04-17 18:29:53 +08:00

468 lines
15 KiB
PHP

<?php
namespace app\lib;
use app\util\ReturnCode;
use think\facade\Env;
use \Exception;
/**
* 权限系统接口
*/
class AuthApi
{
//调用信息
// const USERNAME = 'auth';
const USERNAME = 'nce';
// const PASSWORD = 'YlzXZbm4&5';
const PASSWORD = 'NRXpT@HfvH';
const GRANT_TYPE = 'refresh_token';
// 接口IP
//const AUTH_URL = 'http://106.52.70.137:8080/';
// const AUTH_URL = 'http://111.230.221.71:8001/';
// const AUTH_URL = 'http://129.204.123.130:8001/'; // uat
private $auth_url;
//接口地址
const API_TOKEN = 'auth/oauth/token'; //获取token接口/刷新token接口
const API_TOKEN1 = 'oauth/token'; //获取token接口/刷新token接口
const API_DEL_TOKEN = '/logout'; //注销token接口
const API_USER_AUTH = 'auth/api/user/per'; //获取用户权限
const API_USER_ROLE = 'auth/api/user/role'; //获取角色用户
const API_USER_LIST = 'auth/api/user/list'; //获取用户列表
const API_DEPART_ROLE = 'auth/api/user/depart/users'; //根据部门id获取用户信息
const API_BUSINESS_DEPARTMENT = 'auth/api/user/depart'; //根据业务部id获取业务部下属所有分部信息
const API_MANY_USER_ROLE = 'auth/api/user/roles'; //获取多角色用户
const API_QR_CODE = 'auth/user/token'; // 扫码登录获取token接口
const API_UPDATE_PWD = 'system/user/profile/updatePwd'; // 扫码登录获取token接口
// const API_TOKEN = 'CLOUD-AUTH/oauth/token'; //获取token接口/刷新token接口
// const API_TOKEN1 = 'oauth/token'; //获取token接口/刷新token接口
// const API_DEL_TOKEN = 'CLOUD-AUTH/api/exit'; //注销token接口
// const API_USER_AUTH = 'CLOUD-AUTH/api/user/per'; //获取用户权限
// const API_USER_ROLE = 'CLOUD-AUTH/api/user/role'; //获取角色用户
// const API_DEPART_ROLE = 'CLOUD-AUTH/api/user/depart/users'; //根据部门id获取用户信息
// const API_BUSINESS_DEPARTMENT = 'CLOUD-AUTH/api/user/depart'; //根据业务部id获取业务部下属所有分部信息
// const API_MANY_USER_ROLE = 'CLOUD-AUTH/api/user/roles'; //获取多角色用户
// const API_QR_CODE = 'CLOUD-AUTH/user/token'; // 扫码登录获取token接口
public function __construct(){
$this->auth_url = Env::get('auth.auth_url');
}
/**
* GET 请求
* @param string $url
*/
private function http_get($url,$param,$arr_header=array(),$id=''){
$oCurl = curl_init();
if (is_string($param)) {
$strPOST = $param;
} else {
$aPOST = array();
foreach ($param as $key => $val) {
$aPOST[] = $key . "=" . $val;
}
$strPOST = join("&", $aPOST);
}
$url = $url."?".$strPOST;
if (stripos($url, "https://") !== false) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
if(!empty($arr_header)){
$arr_header[] = "Content-Type: application/json; charset=utf-8";
}
curl_setopt($oCurl, CURLOPT_HTTPHEADER, $arr_header);
$sContent = curl_exec($oCurl);
if (curl_errno($oCurl)){
throw new Exception(curl_error($oCurl),0);
}else{
$httpStatusCode = curl_getinfo($oCurl, CURLINFO_HTTP_CODE);
if (200 !== $httpStatusCode){
throw new Exception($arr_header[1],$httpStatusCode);
}
}
curl_close($oCurl);
return $sContent;
}
/**
* POST 请求
* @param string $url
* @param array $param
* @param boolean $post_file 是否文件上传
* @param boolean $time 超时时间,单位秒
* @param boolean $id 爬虫日志表id
* @param boolean $arr_header http请求头
* @return string content
*/
private function http_post($url, $param,$user_name,$password, $post_file = false, $time = 30,$id='',$arr_header=array()){
$oCurl = curl_init();
if (stripos($url, "https://") !== false) {
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
if(stripos($url,"pycredit")!== false){
curl_setopt($oCurl, CURLOPT_SSLCERT, APP_PATH . 'extra' . DS . self::PYCERT_NAME); //pem
curl_setopt($oCurl, CURLOPT_SSLCERTPASSWD, '123456');
curl_setopt($oCurl, CURLOPT_SSLKEY, APP_PATH . 'extra' . DS . self::PYCERT_NAME); //pem
curl_setopt($oCurl, CURLOPT_SSLKEYPASSWD, '123456'); //pem
}
}
if (is_string($param) || $post_file) {
$strPOST = $param;
} else {
$aPOST = array();
foreach ($param as $key => $val) {
$aPOST[] = $key . "=" . $val;
}
$strPOST = join("&", $aPOST);
}
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_POST, true);
curl_setopt($oCurl, CURLOPT_TIMEOUT, $time); //只需要设置一个秒的数量就可以,超时时间
curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
// curl_setopt($oCurl, CURLOPT_HTTPHEADER, $arr_header);
curl_setopt($oCurl, CURLOPT_USERPWD, "{$user_name}:{$password}");
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
}
/**
* DELETE 请求
* @param string $url
* @param array $param
* @param boolean $user_name AUTH账号
* @param boolean $password AUTH密码
* @return string content
*/
function http_delete($url,$param,$user_name,$password) {
$oCurl = curl_init();
if (is_string($param)) {
$strPOST = $param;
} else {
$aPOST = array();
foreach ($param as $key => $val) {
$aPOST[] = $key . "=" . $val;
}
$strPOST = join("&", $aPOST);
}
$url = $url."?".$strPOST;
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_CUSTOMREQUEST, 'DELETE');
//设置头
curl_setopt($oCurl, CURLOPT_USERPWD, "{$user_name}:{$password}");
curl_setopt($oCurl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36');
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);//SSL认证。
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
}
/**
* PUT 请求
* @param string $url
* @param array $param
* @param boolean $user_name AUTH账号
* @param boolean $password AUTH密码
* @return string content
*/
function http_put($url,$param,$user_name,$password, $arr_header) {
$oCurl = curl_init();
if (is_string($param)) {
$strPOST = $param;
} else {
$aPOST = array();
foreach ($param as $key => $val) {
$aPOST[] = $key . "=" . $val;
}
$strPOST = join("&", $aPOST);
}
$url = $url."?".$strPOST;
curl_setopt($oCurl, CURLOPT_URL, $url);
curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($oCurl, CURLOPT_CUSTOMREQUEST, 'PUT');
//设置头
curl_setopt($oCurl, CURLOPT_HTTPHEADER, $arr_header);
// curl_setopt($oCurl, CURLOPT_USERPWD, "{$user_name}:{$password}");
curl_setopt($oCurl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36');
curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, false);//SSL认证。
$sContent = curl_exec($oCurl);
$aStatus = curl_getinfo($oCurl);
curl_close($oCurl);
if (intval($aStatus["http_code"]) == 200) {
return $sContent;
} else {
return false;
}
}
/**
* getToken
* @Author
* @DateTime
* @version 1.0
* @param data 查询条件json
* @return string 查询结果json字符串
*/
public function getToken($data){
if (empty($data)) {
return ReturnCode::AUTH_PARAMETER;
}
$apiFrom = self::API_TOKEN;
$url = $this->auth_url . $apiFrom;
$user_name = self::USERNAME;
$password = self::PASSWORD;
$res = $this->http_post($url, $data,$user_name,$password, false, 120);
if ($res) {
return $res;
} else {
return ReturnCode::AUTH_TOKEN;
}
}
public function getQrCodeToken($data){
if (empty($data)) {
return ReturnCode::AUTH_PARAMETER;
}
$apiFrom = self::API_QR_CODE;
$url = $this->auth_url . $apiFrom;
$user_name = self::USERNAME;
$password = self::PASSWORD;
$res = $this->http_post($url, $data,$user_name,$password, false, 120);
if ($res) {
return $res;
} else {
return ReturnCode::AUTH_TOKEN;
}
}
/**
* userinfo
* @Author
* @DateTime
* @version 1.0
* @param data 查询条件
* @return string 查询结果json字符串
*/
public function userinfo($data,$arr_header)
{
if (empty($data)) {
return ReturnCode::AUTH_USER_TOKEN;
}
$apiFrom = self::API_USER_AUTH;
$url = $this->auth_url . $apiFrom;
$res = $this->http_get($url,$data,$arr_header);
if ($res) {
return $res;
} else {
return ReturnCode::AUTH_USER;
}
}
/**
* Logout
* @Author llz
* @DateTime 2022-10-10
* @version 1.0
* @param
* @return
*/
public function Logout($data)
{
if (empty($data)) {
return ReturnCode::AUTH_USER_TOKEN;
}
$apiFrom = self::API_DEL_TOKEN;
$url = $this->auth_url . $apiFrom;
$user_name = self::USERNAME;
$password = self::PASSWORD;
$res = $this->http_delete($url,$data,$user_name,$password);
if ($res) {
return $res;
} else {
return ReturnCode::AUTH_USER;
}
}
/**
* userinfo
* @Author
* @DateTime
* @version 1.0
* @param data 查询条件
* @return string 查询结果json字符串
*/
public function RefreshgetToken($userid,$refresh_token)
{
if (empty($refresh_token)) {
return ReturnCode::AUTH_PARAMETER;
}
$apiFrom = self::API_TOKEN;
$userInfo = cache('userinfo'.$userid);
$url = $this->auth_url . $apiFrom."?grant_type=".self::GRANT_TYPE."&refresh_token=".$refresh_token;
$user_name = $userInfo['user_name'];
$password = self::PASSWORD;
$res = $this->http_post($url, array(),$user_name,$password, false, 120);
if ($res) {
return $res;
} else {
return ReturnCode::AUTH_TOKEN;
}
}
public function getRole($data,$arr_header) {
if (empty($data)) {
return ReturnCode::AUTH_PARAMETER;
}
$apiFrom = self::API_USER_ROLE;
$url = $this->auth_url . $apiFrom;
$res = $this->http_get($url,$data);
if ($res) {
return $res;
} else {
return ReturnCode::AUTH_ROLE;
}
}
/**
* 根据部门id获取用户列表
* @param [type] $data [description]
* @return [type] [description]
*/
public function getUsersListById($data) {
if (empty($data)) {
return ReturnCode::AUTH_PARAMETER;
}
$apiFrom = self::API_DEPART_ROLE;
$url = $this->auth_url . $apiFrom;
$res = $this->http_get($url,$data);
if ($res) {
return $res;
} else {
return ReturnCode::AUTH_ROLE;
}
}
/**
* 根据业务部id获取业务部下面所有部门信息
* @param $data
* @return array|int
*/
public function getBusinessDepartment($data) {
if (empty($data)) {
return ReturnCode::AUTH_PARAMETER;
}
$apiFrom = self::API_BUSINESS_DEPARTMENT;
$url = $this->auth_url . $apiFrom;
$res = $this->http_get($url,$data);
if ($res) {
return $res;
} else {
return ReturnCode::AUTH_ROLE;
}
}
/**
* getUser
* @Author
* @DateTime
* @version 1.0
* @param data 查询条件json
* @return string 查询结果json字符串
*/
public function getUser($data){
if (empty($data)) {
return ReturnCode::AUTH_PARAMETER;
}
$apiFrom = self::API_MANY_USER_ROLE;
$url = $this->auth_url . $apiFrom;
$data['systemCode'] = 'NCE';
$res = $this->http_get($url,$data);
if ($res) {
return $res;
} else {
return ReturnCode::AUTH_TOKEN;
}
}
/**
* getUserList
* @Author llz
* @DateTime 2023-3-09
* @version 1.0
* @return string 查询结果json字符串
*/
public function getUserList(){
$url = $this->auth_url . self::API_USER_LIST;
$res = $this->http_get($url,[]);
if ($res) {
return $res;
} else {
return ReturnCode::DB_READ_ERROR;
}
}
/**
* updatePwd
* @Author llz
* @DateTime 2023-3-09
* @version 1.0
* @return string 查询结果json字符串
*/
public function updatePwd(array $data, array $arr_header): string
{
if (empty($data)) {
return ReturnCode::AUTH_PARAMETER;
}
$apiFrom = self::API_UPDATE_PWD;
$url = $this->auth_url . $apiFrom;
$res = $this->http_put($url, $data, self::USERNAME, self::PASSWORD, $arr_header);
if ($res) {
return $res;
} else {
return ReturnCode::AUTH_USER;
}
}
}