|  | @ -0,0 +1,154 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.security.service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorHospitalDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.base.doctor.BaseDoctorRoleDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.wlyyinfo.OauthWlyyConfigDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.security.dao.OauthWlyyConfigDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.security.dao.doctor.BaseDoctorDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.security.dao.doctor.BaseDoctorHospitalDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.security.dao.doctor.BaseDoctorRoleDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.security.utils.HttpClientUtil;
 | 
	
		
			
				|  |  | import com.yihu.utils.security.MD5;
 | 
	
		
			
				|  |  | import org.apache.commons.collections.map.HashedMap;
 | 
	
		
			
				|  |  | import org.apache.http.NameValuePair;
 | 
	
		
			
				|  |  | import org.apache.http.message.BasicNameValuePair;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by Trick on 2019/8/6.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | @Transactional
 | 
	
		
			
				|  |  | public class OauthWlyyConfigService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static final Logger logger = LoggerFactory.getLogger(OauthWlyyConfigService.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HttpClientUtil httpClientUtil;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private OauthWlyyConfigDao oauthWlyyConfigDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorDao baseDoctorDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorHospitalDao baseDoctorHospitalDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private BaseDoctorRoleDao baseDoctorRoleDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public JSONObject checkWlyyDoctor(String code){
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         OauthWlyyConfigDO oauthWlyyConfigDO = oauthWlyyConfigDao.findOne("wlyyConfig");
 | 
	
		
			
				|  |  |         //token获取accesstoken
 | 
	
		
			
				|  |  |         List<NameValuePair> params = new ArrayList<>();
 | 
	
		
			
				|  |  |         params.add(new BasicNameValuePair("appid", oauthWlyyConfigDO.getAppId()));
 | 
	
		
			
				|  |  |         params.add(new BasicNameValuePair("appSecret", oauthWlyyConfigDO.getAppSecret()));
 | 
	
		
			
				|  |  |         String res = httpClientUtil.post(oauthWlyyConfigDO.getTokenUrl(),params,"UTF-8");
 | 
	
		
			
				|  |  |         String token = null;
 | 
	
		
			
				|  |  |         JSONObject rsjson = JSONObject.parseObject(res);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         logger.info("checkWlyyDoctor token :"+rsjson.toString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         Integer status = rsjson.getInteger("status");
 | 
	
		
			
				|  |  |         if(status==10000){
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             //设置入参
 | 
	
		
			
				|  |  |             List<NameValuePair> p = new ArrayList<>();
 | 
	
		
			
				|  |  |             p.add(new BasicNameValuePair("doctor", oauthWlyyConfigDO.getAppId()));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             //设置头部
 | 
	
		
			
				|  |  |             token = rsjson.getJSONObject("result").getString("accesstoken");
 | 
	
		
			
				|  |  |             Map<String,Object> headerMap = new HashedMap();
 | 
	
		
			
				|  |  |             headerMap.put("accesstoken",token);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             String rs = httpClientUtil.headerPost(oauthWlyyConfigDO.getUrl(),p,"UTF-8",headerMap);
 | 
	
		
			
				|  |  |             JSONObject auth = JSONObject.parseObject(rs);
 | 
	
		
			
				|  |  |             Integer s = auth.getInteger("status");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             logger.info("checkWlyyDoctor doctorInfo :"+s.toString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             if(s == 200){
 | 
	
		
			
				|  |  |                 JSONObject data = rsjson.getJSONObject("data");
 | 
	
		
			
				|  |  |                 String authCode =  data.getString("authCode");
 | 
	
		
			
				|  |  |                 if("1".equals(authCode)){
 | 
	
		
			
				|  |  |                     JSONObject doctorJson = data.getJSONObject("doctor");
 | 
	
		
			
				|  |  |                     String idcard = doctorJson.getString("idcard");
 | 
	
		
			
				|  |  |                     BaseDoctorDO doctorDO = baseDoctorDao.findByIdcard(idcard);
 | 
	
		
			
				|  |  |                     if(doctorDO!=null){
 | 
	
		
			
				|  |  |                         return data;
 | 
	
		
			
				|  |  |                     }else{
 | 
	
		
			
				|  |  |                         //存储医生全科医生信息
 | 
	
		
			
				|  |  |                         BaseDoctorDO doctor = new BaseDoctorDO();
 | 
	
		
			
				|  |  |                         doctor.setId(doctorJson.getString("code"));
 | 
	
		
			
				|  |  |                         doctor.setName(doctorJson.getString("name"));
 | 
	
		
			
				|  |  |                         doctor.setIdcard(doctorJson.getString("idcard"));
 | 
	
		
			
				|  |  |                         doctor.setMobile(doctorJson.getString("mobile"));
 | 
	
		
			
				|  |  |                         doctor.setSex(doctorJson.getInteger("sex"));
 | 
	
		
			
				|  |  |                         doctor.setBirthday(doctorJson.getDate("birthday"));
 | 
	
		
			
				|  |  |                         doctor.setProvinceCode(doctorJson.getString("province"));
 | 
	
		
			
				|  |  |                         doctor.setProvinceName(doctorJson.getString("provinceName"));
 | 
	
		
			
				|  |  |                         doctor.setCityCode(doctorJson.getString("city"));
 | 
	
		
			
				|  |  |                         doctor.setCityName(doctorJson.getString("cityName"));
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                         //认证信息设置
 | 
	
		
			
				|  |  |                         String salt = randomString(5);
 | 
	
		
			
				|  |  |                         String pw = idcard.substring(idcard.length()-6);
 | 
	
		
			
				|  |  |                         MD5.md5Hex(pw + "{" + salt + "}");
 | 
	
		
			
				|  |  |                         doctor.setDel("1");
 | 
	
		
			
				|  |  |                         doctor.setEnabled(1);
 | 
	
		
			
				|  |  |                         doctor.setLocked(0);
 | 
	
		
			
				|  |  |                         doctor.setCreateTime(new Date());
 | 
	
		
			
				|  |  |                         baseDoctorDao.save(doctor);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                         //机构信息
 | 
	
		
			
				|  |  |                         BaseDoctorHospitalDO hospitalDO = new BaseDoctorHospitalDO();
 | 
	
		
			
				|  |  |                         hospitalDO.setDoctorCode(doctorJson.getString("code"));
 | 
	
		
			
				|  |  |                         hospitalDO.setOrgCode(doctorJson.getString("hospital"));
 | 
	
		
			
				|  |  |                         hospitalDO.setOrgName(doctorJson.getString("hospitalName"));
 | 
	
		
			
				|  |  |                         hospitalDO.setDel("1");
 | 
	
		
			
				|  |  |                         baseDoctorHospitalDao.save(hospitalDO);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                         BaseDoctorRoleDO role = new BaseDoctorRoleDO();
 | 
	
		
			
				|  |  |                         role.setDoctorCode(doctorJson.getString("code"));
 | 
	
		
			
				|  |  |                         role.setRoleCode("generalDoctor");
 | 
	
		
			
				|  |  |                         baseDoctorRoleDao.save(role);
 | 
	
		
			
				|  |  |                         return data;
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 }else{
 | 
	
		
			
				|  |  |                     return data;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }else{
 | 
	
		
			
				|  |  |                 //请求异常
 | 
	
		
			
				|  |  |                 JSONObject data = new JSONObject();
 | 
	
		
			
				|  |  |                 data.put("authCode","-2");
 | 
	
		
			
				|  |  |                 return data;
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }else {
 | 
	
		
			
				|  |  |             //请求异常
 | 
	
		
			
				|  |  |             JSONObject data = new JSONObject();
 | 
	
		
			
				|  |  |             data.put("authCode","-2");
 | 
	
		
			
				|  |  |             return data;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String randomString(int length) {
 | 
	
		
			
				|  |  |         String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 | 
	
		
			
				|  |  |         StringBuffer buffer = new StringBuffer();
 | 
	
		
			
				|  |  |         Random random = new Random();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         for(int i = 0; i < length; ++i) {
 | 
	
		
			
				|  |  |             int pos = random.nextInt(str.length());
 | 
	
		
			
				|  |  |             buffer.append(str.charAt(pos));
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         return buffer.toString();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |