1.ca扫码登录认证

master
51976 3 years ago
parent 7ef787ecf8
commit 07aebd5c12

@ -1,18 +1,21 @@
package com.manage.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.manage.encrypt.Base64;
import com.manage.encrypt.MD5;
import com.manage.entity.Power_Dept;
import com.manage.entity.Power_Log;
import com.manage.entity.Power_Menu;
import com.manage.entity.Power_User;
import com.manage.service.LogService;
import com.manage.service.Power_DeptService;
import com.manage.service.Power_MenuService;
import com.manage.service.Power_UserService;
import com.manage.service.cache.Cache;
import com.manage.service.cache.CacheManager;
import com.manage.util.DateUtils;
import com.manage.util.Msg;
import com.manage.util.*;
import com.manage.vo.Power_UserVo;
import com.manage.vo.User_Dept_Menu;
import org.apache.commons.lang3.StringUtils;
@ -22,6 +25,8 @@ import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
@ -29,13 +34,15 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
/**
@ -58,6 +65,9 @@ public class methodForEmrMedical {
@Autowired
private Power_DeptService power_deptService;
@Autowired
private LogService logService;
@Value("${EMRMEDICALRECORD_IP}")
private String ip;
@ -99,11 +109,12 @@ public class methodForEmrMedical {
@RequestMapping(value = "getErweima")
@ResponseBody
public JSONObject getErweima(String responseType, String clientId, HttpServletRequest request){
public JSONObject getErweima(String responseType, String clientId, Boolean selfSign, HttpServletRequest request){
// 获得Http客户端
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建Get请求
HttpGet httpGet = new HttpGet("https://test.51trust.com/gateway/oauth/authorize?responseType=code&clientId=2017070411003376");
HttpGet httpGet = new HttpGet("http://10.162.0.60:9999/gateway/oauth/authorize?responseType=" + responseType + "&clientId=" + clientId + "&selfSign=" + selfSign);
//HttpGet httpGet = new HttpGet("http://test.51trust.com/gateway/oauth/authorize?responseType=" + responseType + "&clientId=" + clientId + "&selfSign=" + selfSign);
// 请求头
httpGet.setHeader("Content-Type", "application/json;charset=utf8");
// 响应模型
@ -115,8 +126,6 @@ public class methodForEmrMedical {
response = httpClient.execute(httpGet);
// 从响应模型中获取响应实体
responseEntity = response.getEntity();
System.out.println("responseEntity::::"+responseEntity);
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
jsonObject = JSONObject.parseObject(EntityUtils.toString(responseEntity));
String contents = jsonObject.getString("contents");
@ -149,24 +158,23 @@ public class methodForEmrMedical {
@RequestMapping(value = "getUser")
@ResponseBody
public JSONObject getUser(String requestId, HttpServletRequest request){
// 获得Http客户端
public JSONObject getUser(String requestId, String clientId, HttpServletRequest request){
// 获得Http客户端"138275ab833e42d9qd6b4weab3y4027c8a3"
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建Get请求
HttpGet httpGet = new HttpGet("https://test.51trust.com/gateway/oauth/tokeninfo?clientId=2017070411003376&requestId=" + requestId);
//HttpGet httpGet = new HttpGet("https://test.51trust.com/gateway/oauth/tokeninfo?clientId=" + clientId + "&requestId=" + requestId);
HttpGet httpGet = new HttpGet("http://10.162.0.60:9999/gateway/oauth/tokeninfo?clientId="+ clientId +"&requestId=" + requestId);
// 请求头
httpGet.setHeader("Content-Type", "application/json;charset=utf8");
// 响应模型
CloseableHttpResponse response = null;
HttpEntity responseEntity=null;
HttpEntity responseEntity = null;
JSONObject jsonObject = null;
try {
// 由客户端执行(发送)Get请求
response = httpClient.execute(httpGet);
// 从响应模型中获取响应实体
responseEntity = response.getEntity();
System.out.println("responseEntity::::"+responseEntity);
System.out.println("响应状态为:" + response.getStatusLine());
if (responseEntity != null) {
jsonObject = JSONObject.parseObject(EntityUtils.toString(responseEntity));
String contents = jsonObject.getString("contents");
@ -197,6 +205,203 @@ public class methodForEmrMedical {
//return jsonObject;
}
@RequestMapping(value = "smlogin")
@ResponseBody
public HashMap<String, Object> smlogin(String openId, String clientId) throws Exception {
HashMap<String, Object> map = new HashMap<>();
try {
System.out.println("--------------------------进入smlogin");
Map map1 = getUserTwo(openId, clientId);
// Map map1 = (Map) JSON.parse(dataStr);
System.out.println(map1);
String userName = map1.get("employeeNumber").toString();
Power_UserVo powerUser = new Power_UserVo();
powerUser.setUserName(userName);
powerUser.setUserPwd("000000");
Power_UserVo user = powerUserService.findPowerUserByUserNameAndUserPwd(powerUser);
//添加进操作日志
Power_Log log = new Power_Log();
if( user != null){
//如处于登录状态,先清除缓存
//CacheManager.removeCacheByObject(user);
//记住
//MyCookieUtil.remember(request, response);
//清除用户登录错误次数缓存
CacheManager.clearOnly(powerUser.getUserName());
//存session密码置空
//是否记住密码功能
//MyCookieUtil.remember(request, response);
//设置token缓存
String date = String.valueOf(DateUtils.getDate());
String token = UUID.randomUUID().toString();
//查询归属医院
/* long start5 = System.currentTimeMillis();
Power_User_Dict powerUserDict = powerUserDictMapper.selectDictIdByUserId(user.getUserId());
long end5 = System.currentTimeMillis();
System.out.println("查询医院时间="+(end5-start5)/1000.0+"s");
user.setDictId(powerUserDict.getDictId());*/
//科室id科室名
ActionScopeUtils.setSessionAttribute("token",token,Integer.valueOf(String.valueOf(TOKEN_EXPIRE_TIME))/1000);
//设置用户登录次数缓存
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
CacheManager.addloginUserCount(fmt.format(new Date()),user.getUserName());
CacheManager.addExcCount("noExc");
List<Power_Menu> list = null;
List<User_Dept_Menu> menuList = new ArrayList<>();
Set<String> menus = new LinkedHashSet<>();
if (user.getRoleId().equals(0) || user.getRoleId().equals(-100)) {
list = powerMenuService.queryAllPowerMenu(null,user.getRoleId());
} else {
list = powerMenuService.selectUserAndRoleMenuListPower(user.getUserId(),null);
}
if(null != list && !list.isEmpty()){
for (Power_Menu power_menu : list) {
User_Dept_Menu deptMenu = new User_Dept_Menu();
String menuUrl = power_menu.getMenuUrl();
if (StringUtils.isNotBlank(menuUrl)) {
BeanUtils.copyProperties(power_menu, deptMenu);
deptMenu.setMethodParent(power_menu.getParentId());
menuList.add(deptMenu);
}
if (StringUtils.isNotBlank(power_menu.getMethod())) {
menus.add(power_menu.getMenuUrl());
}
}
}
user.setMenuList(menuList);
user.setMenus(menus);
//设置科室
StringBuilder power_depts = new StringBuilder();
List<Power_Dept> power_depts1 = power_deptService.selectByPrimaryKeys(user.getDeptId());
for(int j=0;j<power_depts1.size();j++){
if(j<power_depts1.size()-1){
power_depts.append(power_depts1.get(j).getDeptName()).append(",");
}else{
power_depts.append(power_depts1.get(j).getDeptName());
}
}
user.setRemark(power_depts.toString());
//设置进缓存
CacheManager.putCache(token,new Cache(date,user,TOKEN_EXPIRE_TIME));
ActionScopeUtils.setSessionAttribute("CURRENT_USER",user,Integer.valueOf(String.valueOf(TOKEN_EXPIRE_TIME))/1000);
//String url = "http://localhost:8080/power/gatewayPage";
//String url = "http://10.6.1.127:9001/power/gatewayPage";
String url = "http://10.6.1.127:8080/power/gatewayPage";
map.put("url", url);
map.put("isSuccess", true);
map.put("employeeNumber", map1.get("employeeNumber"));
map.put("title", map1.get("title"));
map.put("userName", map1.get("userName"));
map.put("openId", map1.get("openId"));
//return "redirect:gatewayPage";
return map;
}else{
//登录失败
// Integer wrongNum = 1;
// Cache cache = CacheManager.getCacheInfo(powerUser.getUserName());
// if(cache != null){
// //缓存中错误次数
// Integer currentNum = (Integer)cache.getValue();
// //叠加1
// wrongNum += currentNum;
// }
//先清除后添加缓存
// CacheManager.clearOnly(powerUser.getUserName());
// CacheManager.putCache(powerUser.getUserName(),new Cache(powerUser.getUserName(),wrongNum));
// log.setCreater(powerUser.getUserName());
// log.setLogTitle("登录");
// log.setLogContent("用户密码错误");
//log.setRemark("已错误【"+wrongNum+"】次");
//logService.insert(log);
//request.setAttribute("msg", "用户名或密码不正确");
//String url = "http://localhost:8080/power/login";
//map.put("url", url);
map.put("isSuccess", false);
map.put("msg", "用户不存在");
map.put("userName", userName);
map.put("employeeNumber", map1.get("employeeNumber"));
map.put("title", map1.get("title"));
map.put("userName", map1.get("userName"));
map.put("openId", map1.get("openId"));
}
}catch (Exception e){
ExceptionPrintUtil.printException(e);
e.printStackTrace();
CacheManager.addExcCount("exc");
}
//return "loginDir/login";
return map;
}
public Map getUserTwo(String openId, String clientId) {
System.out.println("------------------------进入getUserTwo");
// 获得Http客户端
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
// 创建Post请求10.162.0.60:9999
//HttpPost httpPost = new HttpPost("https://test.51trust.com/gateway/doctor/synDoctorSearch");
HttpPost httpPost = new HttpPost("http://10.162.0.60:9999/gateway/doctor/synDoctorSearch");
//json格式转换
JSONObject obj = new JSONObject();
JSONObject head = new JSONObject();
JSONObject body = new JSONObject();
head.put("clientId", clientId);
body.put("openId", openId);
obj.put("head", head);
obj.put("body", body);
String jsonString = JSON.toJSONString(obj);
StringEntity entity = new StringEntity(jsonString, "UTF-8");
// post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "application/json;charset=utf8");
// 响应模型
CloseableHttpResponse response = null;
HttpEntity responseEntity = null;
String dataStr = "";
Map map = null;
try {
System.out.println("---------------------开始调用接口发送请求");
// 由客户端执行(发送)Post请求
response = httpClient.execute(httpPost);
// 从响应模型中获取响应实体
responseEntity = response.getEntity();
System.out.println("-----------------------------从响应模型中获取响应实体");
if (responseEntity != null) {
JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(responseEntity));
System.out.println("获取到的json数据--------------------" + jsonObject);
Object data = jsonObject.get("data");
dataStr = data.toString();
map = (Map) JSON.parse(dataStr);
System.out.println("转换的map数据--------------------------" + map);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 释放资源
if (httpClient != null) {
httpClient.close();
}
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return map;
}
/*
*

@ -66,7 +66,7 @@
</div>
<div class="inputDiv">
<label><input type="checkbox" name="rememberMe" id="rememberMe" value="yes" style="vertical-align:middle; margin-top:0;" <%=checked%>>记住密码</label>
<%-- <a href="#" id="smdl" style="float: right; margin-right: 10px">扫码登录</a>--%>
<a href="#" id="smdl" style="float: right; margin-right: 10px">扫码登录</a>
</div>
<%--<a href="#" id="forgetPwd" style="float: right;">忘记密码了?</a>--%>
<div class="inputDiv">

@ -33,7 +33,12 @@ $(function(){
$.ajax({
type: "post",
url : "methodForEmrMedical/getErweima",
data: {responseType:"code", clientId:"2019111514014789"},
data: {
responseType:"code",
//clientId:"2017070411003376",
clientId:"2019111514014789",
selfSign: "true"
},
success : function(data) {
var requestId = "";
$.each(data,function (index, obj){
@ -49,38 +54,62 @@ $(function(){
zhdl.style.display = "none";
return false;
});
//test(requestId);
getUser(requestId);
polling(requestId);
},
})
});
var i = 0;
function test(requestId) {
while (i == 0) {
getUser(requestId);
}
function polling(requestId, delay = 1000) {
return new Promise((resolve, reject) =>{
$.ajax({
type: "post",
url : "methodForEmrMedical/getUser",
data: {
requestId: requestId,
//clientId:"2017070411003376"
clientId:"2019111514014789"
},
success: function (data) {
$.each(data,function (index, obj){
//轮询查询接口
if (obj === '未授权') { //是否继续轮询
setTimeout(() => {
resolve(polling(requestId, delay));
}, delay)
} else {
smlogin(obj);
resolve(obj);
}
return false;
})
}
})
})
}
function getUser(requestId) {
function smlogin(user) {
$.ajax({
type: "post",
url : "methodForEmrMedical/getUser",
data: {requestId: requestId },
success : function(data) {
$.each(data,function (index, obj){
test(requestId);
if (obj != "未授权"){
alert(obj.userName)
i = 1;
}
return false;
});
url : "methodForEmrMedical/smlogin",
data: {
openId: user.openId,
//clientId:"2017070411003376"
clientId:"2019111514014789"
},
success: function (data) {
if (data.isSuccess){
window.location.href = data.url;
}else {
//window.location.href = data.url;
window.location.reload();
alert(data.msg);
}
}
})
}
});
$('body').keydown(function () {

Loading…
Cancel
Save