520 lines
20 KiB
PHP
520 lines
20 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller;
|
||
|
||
use app\lib\AuthApi;
|
||
use app\model\UsersManager;
|
||
use app\util\ReturnCode;
|
||
use think\Db;
|
||
use think\Cache;
|
||
use think\facade\Log;
|
||
|
||
class User extends Base
|
||
{
|
||
|
||
/**
|
||
* 注册一个新用户
|
||
*
|
||
* @return [返回码,成功数据]
|
||
*/
|
||
public function createUser()
|
||
{
|
||
$info = [];
|
||
$info["user_name"] = $this->request->post('user_name', '', 'trim');
|
||
$info["user_phone"] = $this->request->post('user_phone', '', 'trim');
|
||
|
||
$ret = (new UsersManager())->createUser( $info );
|
||
|
||
if( UsersManager::$RET_CODE_SUCCESS == $ret[0])
|
||
{
|
||
return $this->buildSuccess("注册用户成功!");
|
||
}
|
||
else
|
||
{
|
||
// dump($ret);
|
||
return $this->buildFailed("注册用户出错,错误码:".$ret[0]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 以标签的形式删除一个用户(不实际删除,只做标记)
|
||
*
|
||
* @return void
|
||
*/
|
||
public function delUserWithTag()
|
||
{
|
||
$user_id = $this->request->post('user_id', '', 'trim');
|
||
|
||
$ret = (new UsersManager())->delUserWithTag($user_id);
|
||
if( UsersManager::$RET_CODE_SUCCESS == $ret)
|
||
{
|
||
return $this->buildSuccess("注册用户成功!");
|
||
}
|
||
else
|
||
{
|
||
dump($ret);
|
||
return $this->buildFailed("注册用户出错,错误码:");
|
||
}
|
||
|
||
}
|
||
|
||
|
||
|
||
//获取用户列表
|
||
public function getUserList()
|
||
{
|
||
$userList = cache('userList');
|
||
if (!$userList ) {
|
||
$Auth = new AuthApi();
|
||
$user_list = $Auth->getUserList();
|
||
$ret = json_decode($user_list,true);
|
||
if($ret['code'] == 200){
|
||
cache('userList',$ret['data'],1800);
|
||
return $this->buildSuccess($ret['data']);
|
||
}else {
|
||
dump($ret);
|
||
return $this->buildFailed("获取用户列表出错:");
|
||
}
|
||
}
|
||
$userTemp = [];
|
||
foreach ($userList as $key=>$value){
|
||
if ($value['status']!=1){
|
||
$userTemp[] = $value;
|
||
}
|
||
}
|
||
$userList = $userTemp;
|
||
return $this->buildSuccess($userList);
|
||
}
|
||
|
||
|
||
|
||
//登录接口-
|
||
public function SignIn()
|
||
{
|
||
|
||
$user_name = $this->request->post('username', '', 'trim');
|
||
$password = $this->request->post('password', '', 'trim');
|
||
$version = $this->request->post('version', '', 'trim');
|
||
if(!$user_name){
|
||
return $this->buildFailed("请输入用户名");
|
||
}
|
||
if(!$password){
|
||
return $this->buildFailed("请输入密码");
|
||
}
|
||
//llz
|
||
//if(!$version){
|
||
// return $this->buildFailed("请输入版本号");
|
||
//}
|
||
$data['grant_type'] = "password";
|
||
$data['username'] = $user_name;
|
||
$data['password'] = $password;
|
||
$Auth = new AuthApi();
|
||
|
||
//获取token
|
||
$auth_list = $Auth->getToken($data);
|
||
Log::debug('---------------result of getToken-----------');
|
||
log::debug($auth_list);
|
||
if($auth_list == "-4001"){
|
||
return $this->buildFailed("缺少请求参数");
|
||
}else if($auth_list == ReturnCode::AUTH_TOKEN ){
|
||
return $this->buildFailed("获取token失败");
|
||
}else{
|
||
|
||
$auth_list = json_decode($auth_list,true);
|
||
|
||
if($auth_list['code'] == '200'){
|
||
if(!empty($auth_list['Error'])) {
|
||
return $this->buildFailed($auth_list['Error']);
|
||
}
|
||
$this->token = $auth_list['token'];
|
||
}else{
|
||
return $this->buildFailed($auth_list);
|
||
}
|
||
|
||
}
|
||
|
||
$user['access_token'] = $this->token;
|
||
$user['systemCode'] = "NCE";
|
||
$user['version'] = $version;
|
||
$arr_header[] = "Content-Type: application/json; charset=utf-8";
|
||
$arr_header[] = "Authorization: Bearer " . $this->token;
|
||
//获取用户权限
|
||
$userinfo = $Auth->userinfo($user,$arr_header);
|
||
|
||
if($userinfo == "-4003"){
|
||
return $this->buildFailed("缺少token参数");
|
||
}else if($userinfo == "-4004"){
|
||
return $this->buildFailed("获取用户权限失败");
|
||
}
|
||
$userinfo = json_decode($userinfo,true);
|
||
|
||
$info = array();
|
||
if($userinfo['code'] == 200){
|
||
$info['apiAuth'] = $this->token;
|
||
$info['refresh_token'] = $this->token;
|
||
$info['loginTime'] = time();
|
||
$info['userinfo']['user_id'] = $userinfo['data']['oid'];
|
||
$info['userinfo']['user_name'] = $userinfo['data']['nickname'];
|
||
$info['userinfo']['user_phone'] = $userinfo['data']['mobile'];
|
||
$info['userinfo']['department_id'] = $userinfo['data']['odepid'];
|
||
$info['userinfo']['department_name'] = $userinfo['data']['depname'];
|
||
$info['userinfo']['sex'] = $userinfo['data']['sex'];
|
||
$info['userinfo']['email'] = $userinfo['data']['email'];
|
||
$info['userinfo']['mobile'] = $userinfo['data']['mobile'];
|
||
// $info['perFlags'] = array_column($userinfo['data']['perFlags'],"flag");
|
||
$info['perFlags'] = $userinfo['data']['perFlags'];
|
||
$info['admmenu'] = $userinfo['data']['admmenu'];
|
||
$info['menu'] = $userinfo['data']['menu'];
|
||
// 所有下属包括自己的id
|
||
$info['userinfo']['user_ids'] = $userinfo['data']['ids'];
|
||
// 用户角色编码
|
||
$info['userinfo']['roleCode'] = $userinfo['data']['roleCode'];
|
||
$options = [
|
||
// 缓存类型为File
|
||
'type' => 'File',
|
||
// 缓存有效期为永久有效
|
||
'expire' => 0,
|
||
// 指定缓存目录
|
||
'path' => APP_PATH . 'runtime/cache/',
|
||
];
|
||
cache('Login:' . $info['userinfo']['user_id'], json_encode($info['userinfo']),$options);
|
||
cache('userinfo'.$info['userinfo']['user_id'], $info['userinfo'],$options);
|
||
cache('info'.$info['userinfo']['user_id'], $info,$options);
|
||
cache('token'.$info['userinfo']['user_id'], $this->token,$options);
|
||
|
||
// 将登录用户的登录ip写入登录日志表
|
||
// strcasecmp 比较两个字符,不区分大小写。返回0,>0,<0。
|
||
if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
|
||
$ip = getenv('HTTP_CLIENT_IP');
|
||
} elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
|
||
$ip = getenv('HTTP_X_FORWARDED_FOR');
|
||
} elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
|
||
$ip = getenv('REMOTE_ADDR');
|
||
} elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
|
||
$ip = $_SERVER['REMOTE_ADDR'];
|
||
} else {
|
||
$ip = '';
|
||
}
|
||
$client_ip = preg_match( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
|
||
$login_ins_data = [
|
||
'user_no' => $user_name,
|
||
'user_name'=>$info['userinfo']['user_name'],
|
||
'client_ip' => $client_ip,
|
||
'login_time' => date('Y-m-d H:i:s')
|
||
];
|
||
Db::name('user_login')->insert($login_ins_data);
|
||
|
||
return $this->buildSuccess($info);
|
||
|
||
}
|
||
return $this->buildFailed("获取用户权限失败");
|
||
}
|
||
|
||
|
||
/**
|
||
* 退出登录
|
||
*
|
||
*/
|
||
public function Logout(){
|
||
//获取用户权限
|
||
$Auth = new AuthApi();
|
||
$token = $this->token;
|
||
|
||
$lo = $Auth->Logout(array("access_token"=>$token));
|
||
$lo = json_decode($lo,true);
|
||
if($lo['code'] == 0000){
|
||
$userId = $this->request->header('userId');
|
||
cache('token'.$userId,null);
|
||
cache('userinfo'.$userId,null);
|
||
cache('info'.$userId,null);
|
||
cache('Login:' . $userId, null);
|
||
return $this->buildSuccess([], '退出成功');
|
||
}else{
|
||
return $this->buildFailed('退出失败');
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 刷新token
|
||
*
|
||
*/
|
||
public function Refresh(){
|
||
//获取用户权限
|
||
$Auth = new AuthApi();
|
||
$refresh_token = $this->request->post('refresh_token', '', 'trim');
|
||
if(empty($refresh_token)){
|
||
return $this->buildFailed('缺少refresh_token参数');
|
||
}
|
||
$userid = $this->request->header('userId');
|
||
$auth_list = $Auth->RefreshgetToken($userid,$refresh_token);
|
||
|
||
$auth_list = json_decode($auth_list,true);
|
||
if($auth_list['code'] == 0000){
|
||
cache('token'.$this->userInfo['user_id'], $auth_list['data'], config('apiBusiness.ONLINE_TIME'));
|
||
$auth_list['data']['apiAuth'] = $auth_list['data']['access_token'];
|
||
$auth_list['data']['loginTime'] = time();
|
||
|
||
$userInfo = cache('Login:' . $userid);
|
||
$userInfo = json_decode($userInfo, true);
|
||
if (!$userInfo || !isset($userInfo['user_id'])) {
|
||
return $this->buildFailed('-1', '缺少ApiAuth!');
|
||
}
|
||
//重置ApiAuth
|
||
cache('Login:' . $userid, null);
|
||
cache('Login:' . $this->userInfo['user_id'], json_encode($userInfo), config('apiBusiness.ONLINE_TIME'));
|
||
return $this->buildSuccess($auth_list['data'], '刷新成功');
|
||
}else{
|
||
return $this->buildFailed('刷新失败');
|
||
}
|
||
|
||
}
|
||
|
||
/**
|
||
* 循环修改字段
|
||
*/
|
||
public function LoopModify($data){
|
||
if(!empty($data)){
|
||
foreach ($data as $k => $v){
|
||
if(!empty($v['children'])){
|
||
$data[$k]['children'] = $this->LoopModify($v['children']);
|
||
}
|
||
}
|
||
}
|
||
return $data;
|
||
}
|
||
|
||
/**
|
||
* 获取角色用户
|
||
* @param string $role_code
|
||
* @return \think\Response
|
||
*/
|
||
public function getRole() {
|
||
if(empty($this->userInfo)){
|
||
return $this->buildFailed("获取角色失败,因用户信息为空,请重新登录","",ReturnCode::AUTH_ERROR);
|
||
}
|
||
$role_code = $this->request->post('roleCode', '', 'trim');
|
||
if (empty($role_code)) {
|
||
return $this->buildFailed("角色标识不能为空");
|
||
}
|
||
$Auth = new AuthApi();
|
||
$token = $this->token;
|
||
$arr_header[] = "Content-Type: application/json; charset=utf-8";
|
||
$arr_header[] = "Authorization: Bearer " . $this->token;
|
||
|
||
$data = [
|
||
'roleCode' => $role_code,
|
||
'access_token' => $token
|
||
];
|
||
$result = $Auth->getRole($data,$arr_header);
|
||
$result = json_decode($result,true);
|
||
if ($result['code'] <= -1) {
|
||
return $this->buildFailed($result['msg'],"角色代码:".$role_code."失败.来源:User:getRole()",ReturnCode::AUTH_ERROR);
|
||
}
|
||
|
||
if (empty($result['data'] ) and count($result['data']) == 0) {
|
||
return $this->buildFailed($result,"角色代码:".$role_code."获取数据失败.来源:User:getRole()",ReturnCode::AUTH_ERROR);
|
||
}
|
||
|
||
|
||
if (empty($result['data'])) {
|
||
return $this->buildFailed($result,"失败",ReturnCode::AUTH_ERROR);
|
||
}
|
||
|
||
|
||
return $this->buildSuccess($result['data'], '成功');
|
||
}
|
||
|
||
/**
|
||
* 获取二维码
|
||
*/
|
||
public function qrcode() {
|
||
if ($this->request->isPost()) {
|
||
$nonceStr = rand(100000,999999);
|
||
$timeStamp = time();
|
||
$sign = md5(config('apiBusiness.SCAN_AUTH_KEY').$timeStamp.$nonceStr);
|
||
// 向数据表pg_qr_code_info插入一条二维码的信息记录
|
||
$ins_data = [
|
||
'sign' => $sign,
|
||
'timestamp' => $timeStamp,
|
||
'nonce' => $nonceStr,
|
||
'qr_code_status' => 0,
|
||
'user_id' => 0,
|
||
'expire_time' => intval($timeStamp+config('apiBusiness.SCAN_EXPIRETIME')),
|
||
'status' => 1,
|
||
'create_time' => time()
|
||
];
|
||
$ins_result = Db::name('qr_code_info')->insert($ins_data);
|
||
if (!$ins_result) {
|
||
return $this->buildFailed('请求失败');
|
||
}
|
||
$return = [
|
||
'return_url' => 'Scan/changeQrCodeStatus',
|
||
'timestamp' => $timeStamp,
|
||
'sign' => $sign,
|
||
'nonce' => $nonceStr,
|
||
'system' => 'CSPG'
|
||
];
|
||
return $this->buildSuccess($return, '请求成功');
|
||
}
|
||
}
|
||
|
||
public function qrCodeLogin()
|
||
{
|
||
$sign = input('post.sign');
|
||
$nonceStr = input('post.nonce');
|
||
$timeStamp = input('post.timestamp', 0);
|
||
// $version = input('post.version');
|
||
$currentTime = time();
|
||
if (empty($sign) || empty($nonceStr)) {
|
||
return $this->buildFailed('参数错误');
|
||
}
|
||
//校验二维码的有效性
|
||
if (md5(config('apiBusiness.SCAN_AUTH_KEY').$timeStamp.$nonceStr) != $sign) {
|
||
return $this->buildFailed('无效二维码', ['qrCodeStatus' => "4", 'desc' => '无效二维码~'], ReturnCode::PARAM_INVALID);
|
||
}
|
||
// 校验数据库表pg_qr_code_info是否存在该二维码记录
|
||
$qr_code_result = Db::name('qr_code_info')
|
||
->where([
|
||
'sign' => $sign,
|
||
'timestamp' => $timeStamp,
|
||
'nonce' => $nonceStr,
|
||
'status' => 1
|
||
])
|
||
->find();
|
||
if (!$qr_code_result) {
|
||
return $this->buildFailed('无效二维码', ['qrCodeStatus' => 4, 'desc' => '无效二维码~'], ReturnCode::PARAM_INVALID);
|
||
}
|
||
// 校验二维码有效期
|
||
if ($qr_code_result['expire_time'] < $currentTime) {
|
||
return $this->buildSuccess(['qrCodeStatus' => 3, 'desc' => '二维码已失效']);
|
||
}
|
||
// 校验二维码状态
|
||
if ($qr_code_result['qr_code_status'] == 1) {
|
||
return $this->buildSuccess(['qrCodeStatus' => $qr_code_result['qr_code_status'], 'desc' => '扫描成功']);
|
||
}
|
||
if ($qr_code_result['qr_code_status'] == 0) {
|
||
return $this->buildSuccess(['qrCodeStatus' => $qr_code_result['qr_code_status'], 'desc' => '等待扫描']);
|
||
}
|
||
if ($qr_code_result['qr_code_status'] != 2 || !$qr_code_result['user_id']) {
|
||
return $this->buildFailed('无效二维码', ['qrCodeStatus' => 4, 'desc' => '无效二维码~'], ReturnCode::PARAM_INVALID);
|
||
}
|
||
|
||
$userId = $qr_code_result['user_id'];
|
||
$Auth = new AuthApi();
|
||
$data['user_name'] = $userId;
|
||
//获取token
|
||
$auth_list = $Auth->getQrCodeToken($data);
|
||
if ($auth_list == "-4001") {
|
||
return $this->buildFailed("缺少请求参数");
|
||
} elseif ($auth_list == "-4002") {
|
||
return $this->buildFailed("获取token失败");
|
||
} else {
|
||
$auth_list = json_decode($auth_list,true);
|
||
if ($auth_list['code'] == '0000') {
|
||
if (array_key_exists("refresh_token",$auth_list['data'])) {
|
||
$this->token = $auth_list['data'];
|
||
}
|
||
} else {
|
||
return $this->buildFailed($auth_list['message']);
|
||
}
|
||
|
||
}
|
||
|
||
$user['systemCode'] = "NCE";
|
||
$user['access_token'] = $this->token['access_token'];
|
||
// $user['version'] = $version;
|
||
$user['version'] = $qr_code_result['version'];
|
||
//获取用户权限
|
||
$user_info = $Auth->userinfo($user);
|
||
if($user_info == "-4003"){
|
||
return $this->buildFailed("缺少token参数");
|
||
}else if($user_info == "-4004"){
|
||
return $this->buildFailed("获取用户权限失败");
|
||
}
|
||
$user_info = json_decode($user_info,true);
|
||
$info = array();
|
||
if($user_info['code'] == 0000){
|
||
$info['apiAuth'] = $this->token['access_token'];
|
||
$info['refresh_token'] = $this->token['refresh_token'];
|
||
$info['loginTime'] = time();
|
||
$info['userinfo']['user_id'] = $user_info['data']['oid'];
|
||
$info['userinfo']['user_name'] = $user_info['data']['name'];
|
||
$info['userinfo']['user_phone'] = $user_info['data']['mobile'];
|
||
$info['userinfo']['department_id'] = $user_info['data']['odepid'];
|
||
$info['userinfo']['department_name'] = $user_info['data']['depName'];
|
||
$info['perFlags'] = $user_info['data']['perFlags'];
|
||
$info['menu'] = $user_info['data']['menuResponses'];
|
||
// 所有下属包括自己的id
|
||
$info['userinfo']['user_ids'] = $user_info['data']['ids'];
|
||
// 用户角色编码
|
||
$info['userinfo']['roleCode'] = $user_info['data']['rolesCodes'];
|
||
$info['qrCodeStatus'] = 2;
|
||
$info['desc'] = '登录成功';
|
||
|
||
cache('Login:' . $info['userinfo']['user_id'], json_encode($info['userinfo']), config('apiBusiness.ONLINE_TIME'));
|
||
cache('userinfo'.$info['userinfo']['user_id'], $info['userinfo'], config('apiBusiness.ONLINE_TIME'));
|
||
cache('info'.$info['userinfo']['user_id'], $info, config('apiBusiness.ONLINE_TIME'));
|
||
cache('token'.$info['userinfo']['user_id'], $this->token, config('apiBusiness.ONLINE_TIME'));
|
||
return $this->buildSuccess($info);
|
||
} else {
|
||
return $this->buildFailed("扫码登录失败");
|
||
}
|
||
}
|
||
|
||
public function destroyQrcode(){
|
||
$sign = input('post.sign');
|
||
$nonceStr = input('post.nonce');
|
||
$timeStamp = input('post.timestamp',0);
|
||
if (md5(config('apiBusiness.SCAN_AUTH_KEY').$timeStamp.$nonceStr) != $sign) {
|
||
return $this->buildFailed('无效二维码', ['qrCodeStatus' => "4", 'desc' => '无效二维码~'], ReturnCode::PARAM_INVALID);
|
||
}
|
||
// 校验数据库表pg_qr_code_info是否存在该二维码记录
|
||
$qr_code_result = Db::name('qr_code_info')
|
||
->where([
|
||
'sign' => $sign,
|
||
'timestamp' => $timeStamp,
|
||
'nonce' => $nonceStr
|
||
])
|
||
->find();
|
||
if (!$qr_code_result) {
|
||
return $this->buildFailed('无效二维码', ['qrCodeStatus' => "4", 'desc' => '无效二维码~'], ReturnCode::PARAM_INVALID);
|
||
}
|
||
// 二维码软删除
|
||
$del_result = Db::name('qr_code_info')->where(['id'=>$qr_code_result['id']])->data(['status'=>0])->update();
|
||
if ($del_result) {
|
||
return $this->buildSuccess('', '请求成功');
|
||
} else {
|
||
return $this->buildFailed('请求失败');
|
||
}
|
||
}
|
||
|
||
public function updatePwd()
|
||
{
|
||
$old_pwd = $this->request->post('old_pwd', '', 'trim');
|
||
$new_pwd = $this->request->post('new_pwd', '', 'trim');
|
||
$confirm_pwd = $this->request->post('confirm_pwd', '', 'trim');
|
||
if (empty($old_pwd) || empty($new_pwd) || empty($confirm_pwd)) {
|
||
return $this->buildFailed('参数错误');
|
||
}
|
||
if ($new_pwd != $confirm_pwd) {
|
||
return $this->buildFailed('两次密码输入不一致');
|
||
}
|
||
$Auth = new AuthApi();
|
||
$arr_header[] = "Content-Type: application/json; charset=utf-8";
|
||
$arr_header[] = "Authorization: Bearer " . $this->token;
|
||
$data = [
|
||
'oldPassword' => $old_pwd,
|
||
'newPassword' => $new_pwd,
|
||
'access_token' => $this->token
|
||
];
|
||
$result = $Auth->updatePwd($data, $arr_header);
|
||
$result = json_decode($result,true);
|
||
if ($result['code'] <= -1) {
|
||
return $this->buildFailed($result['msg'],"修改密码失败.来源:User:updatePwd()",ReturnCode::AUTH_ERROR);
|
||
}
|
||
return $this->buildSuccess([], '修改密码成功');
|
||
}
|
||
|
||
}
|