bing 3 years ago
parent
commit
7ee855fbfd

+ 20 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/dao/user/RoleDao.java

@ -0,0 +1,20 @@
package com.yihu.jw.security.dao.user;
import com.yihu.jw.entity.base.role.RoleDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
/**
 * Dao - 角色
 * Created by progr1mmer on 2018/8/17.
 */
public interface RoleDao extends PagingAndSortingRepository<RoleDO, String>, JpaSpecificationExecutor<RoleDO> {
    RoleDO findByCode(String code);
    List<RoleDO> findByNameAndStatus(String name, Integer status);
    List<RoleDO> findByNameAndSaasIdAndStatus(String name, String saasId, Integer status);
}

+ 19 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/dao/user/WlyyUserAreaDao.java

@ -0,0 +1,19 @@
package com.yihu.jw.security.dao.user;
import com.yihu.jw.entity.equipment.WlyyUserAreaDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
public interface WlyyUserAreaDao extends PagingAndSortingRepository<WlyyUserAreaDO, String>, JpaSpecificationExecutor<WlyyUserAreaDO> {
    @Query("select id from WlyyUserAreaDO where userId = ?1 ")
    List<WlyyUserAreaDO> findAllByUserId(String userId);
    @Modifying
    @Query("delete from WlyyUserAreaDO p where p.userId=?1 ")
    void deleteByUserId(String userId);
}

+ 19 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/dao/user/WlyyUserEquipmentDao.java

@ -0,0 +1,19 @@
package com.yihu.jw.security.dao.user;
import com.yihu.jw.entity.equipment.WlyyUserEquipmentDO;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.List;
public interface WlyyUserEquipmentDao extends PagingAndSortingRepository<WlyyUserEquipmentDO, String>, JpaSpecificationExecutor<WlyyUserEquipmentDO> {
    @Modifying
    @Query("delete from WlyyUserEquipmentDO p where p.userId=?1 and del=1 ")
    void deleteByUserId(String userId);
    @Query("select id from WlyyUserEquipmentDO where userId = ?1 and del=1 ")
    List<WlyyUserEquipmentDO> findAllByUserId(String userId);
}

+ 74 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/oauth2/provider/endpoint/WlyyLoginEndpoint.java

@ -1876,6 +1876,80 @@ public class WlyyLoginEndpoint extends AbstractEndpoint {
        }
        return ObjEnvelop.getError("登录失败!");
    }*/
    /**
     * I健康医生端授权登录
     * @param code 医生code
     * @param client_id
     * @param login_type 3
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/oauth/checkWlyyUserLogin", method = RequestMethod.POST)
    public ObjEnvelop checkQRUserCodeLogin(String code,String client_id,String login_type,HttpSession httpSession){
        if (StringUtils.isEmpty(client_id)) {
            throw new InvalidRequestException("client_id is null");
        }
        if (StringUtils.isEmpty(code)) {
            throw new InvalidRequestException("doctor is null");
        }
        logger.info("origin doctorCode :"+code);
        KeyPair keyPair = (KeyPair) httpSession.getAttribute("privateKey");
        String doctorCode = com.yihu.jw.security.utils.RSAUtils.decryptBase64(code, keyPair);
        logger.info("after doctorCode :"+doctorCode);
        JSONObject map = null;
        try{
            map= oauthWlyyConfigService.checkWlyyUserDoctor(doctorCode);
        }catch (Exception e){
            logger.error(e);
            return ObjEnvelop.getError("授权失败");
        }
        String authCode = map.getString("authCode");
        if(!"1".equals(authCode)){
            map.remove("doctor");
            return ObjEnvelop.getError("授权失败");
        }
        ClientDetails authenticatedClient = clientDetailsService.loadClientByClientId(client_id);
        JSONObject doctor = map.getJSONObject("doctor");
        Map<String, String> parameters = new HashedMap();
        parameters.put("login_type","2");
        parameters.put("username",doctor.getString("idcard"));
        parameters.put("grant_type", "ihealthCode");
        TokenRequest tokenRequest = oAuth2RequestFactory.createTokenRequest(parameters, authenticatedClient);
        if (authenticatedClient != null) {
            oAuth2RequestValidator.validateScope(tokenRequest, authenticatedClient);
        }
        OAuth2AccessToken token = getTokenGranter().grant(tokenRequest.getGrantType(), tokenRequest);
        if (token == null) {
            throw new UnsupportedGrantTypeException("Unsupported grant type: " + tokenRequest.getGrantType());
        }
        WlyyUserSimple wlyyUserSimple = userDetailsService.authSuccess(parameters.get("username"));
        wlyyUserSimple.setAccessToken(token.getValue());
        wlyyUserSimple.setTokenType(token.getTokenType());
        wlyyUserSimple.setExpiresIn(token.getExpiresIn());
        wlyyUserSimple.setRefreshToken(token.getRefreshToken().getValue());
        wlyyUserSimple.setUser(parameters.get("username"));
        String loginType = parameters.get("login_type");
        BaseLoginLogDO baseLoginLogDO = new BaseLoginLogDO();
        userDetailsService.setRolePhth(loginType, token, wlyyUserSimple.getId(), redisTemplate);
        baseLoginLogDO.setUserId(wlyyUserSimple.getId());
        baseLoginLogDO.setCreateTime(new Date());
        String userAgent = JSONObject.toJSONString(wlyyUserSimple);
        baseLoginLogDO.setUserAgent(userAgent);
        baseLoginLogDO.setLoginType(loginType);
        baseLoginLogService.save(baseLoginLogDO);
        map.put("WlyyUserSimple",wlyyUserSimple);
        map.remove("doctor");
        return ObjEnvelop.getSuccess("success",map);
    }
    @RequestMapping(value = "/oauth/registerPatientAndLogin", method = RequestMethod.POST)
    public ObjEnvelop registerPatientAndLogin(String name, String mobile, String idcard, String ssc, String pw,String client_id,String login_type,String captcha,String openid,String wxId
    ,String geetestChallenge,String geetestValidate,String geetestSeccode )throws Exception{

+ 149 - 0
server/svr-authentication/src/main/java/com/yihu/jw/security/service/OauthWlyyConfigService.java

@ -4,6 +4,10 @@ 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.base.role.RoleDO;
import com.yihu.jw.entity.base.user.UserDO;
import com.yihu.jw.entity.equipment.WlyyUserAreaDO;
import com.yihu.jw.entity.equipment.WlyyUserEquipmentDO;
import com.yihu.jw.entity.hospital.mapping.DoctorMappingDO;
import com.yihu.jw.entity.wlyyinfo.OauthWlyyConfigDO;
import com.yihu.jw.security.dao.OauthWlyyConfigDao;
@ -11,6 +15,10 @@ 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.dao.doctor.DoctorMappingDao;
import com.yihu.jw.security.dao.iot.UserDao;
import com.yihu.jw.security.dao.user.RoleDao;
import com.yihu.jw.security.dao.user.WlyyUserAreaDao;
import com.yihu.jw.security.dao.user.WlyyUserEquipmentDao;
import com.yihu.jw.util.http.HttpClientUtil;
import com.yihu.utils.security.MD5;
import org.apache.commons.collections.map.HashedMap;
@ -49,6 +57,14 @@ public class OauthWlyyConfigService {
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private DoctorMappingDao doctorMappingDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private WlyyUserAreaDao userAreaDao;
    @Autowired
    private WlyyUserEquipmentDao userEquipmentDao;
    @Autowired
    private RoleDao roleDao;
    public JSONObject checkWlyyDoctor(String code)throws Exception{
@ -221,6 +237,139 @@ public class OauthWlyyConfigService {
    }
    public JSONObject checkWlyyUserDoctor(String doctor)throws Exception{
        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", doctor));
            //设置头部
            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);
            logger.info("checkWlyyDoctor doctorInfo :"+rs);
            JSONObject auth = JSONObject.parseObject(rs);
            Integer s = auth.getInteger("status");
            if(s == 200){
                JSONObject data = auth.getJSONObject("data");
                String authCode =  data.getString("authCode");
                if("1".equals(authCode)){
                    JSONObject doctorJson = data.getJSONObject("doctor");
                    String idcard = doctorJson.getString("idcard");
                    String mobile = doctorJson.getString("mobile");
                    if(StringUtils.isBlank(idcard)){
                        data.put("authCode","-3");
                        data.put("mes","I健康账户信息不完整,授权失败");
                        return data;
                    }
                    UserDO userDO = userDao.findByMobile(mobile);
                    if(userDO!=null){
                        return data;
                    }else{
                        //存储医生全科医生信息
                        userDO = new UserDO();
                        userDO.setSaasId("808080eb7bc87123017bf16ba0ad0046");
                        userDO.setName(doctorJson.getString("name"));
                        userDO.setIdcard(doctorJson.getString("idcard"));
                        userDO.setUsername(doctorJson.getString("mobile"));
                        userDO.setMobile(doctorJson.getString("mobile"));
                        if (doctorJson.getInteger("sex")==1){
                            userDO.setGender(UserDO.Gender.male);
                        }else if (doctorJson.getInteger("sex")==2){
                            userDO.setGender(UserDO.Gender.female);
                        }
                        //认证信息设置
                        String salt = randomString(5);
                        String pw = idcard.substring(idcard.length()-6);
                        userDO.setPassword(MD5.md5Hex(pw + "{" + salt + "}"));
                        userDO.setSalt(salt);
                        userDO.setEnabled(1);
                        userDO.setLocked(0);
                        userDO.setCreateTime(new Date());
                        userDO.setUpdateTime(new Date());
                        userDO = userDao.save(userDO);
                        //18补货员19社区药柜管理员20药柜区域管理员21药柜超级管理员
                        String expandLevel = doctorJson.getString("expandLevel");
                        RoleDO roleDO = roleDao.findOne(expandLevel);
                        userDO.setRoleId(roleDO.getId());
                        userDO = userDao.save(userDO);
                        List<WlyyUserAreaDO> userAreaDOList = userAreaDao.findAllByUserId(userDO.getId());
                        if (userAreaDOList==null||userAreaDOList.size()==0){
                            WlyyUserAreaDO userAreaDO = new  WlyyUserAreaDO();
                            userAreaDO.setUserId(userDO.getId());
                            userAreaDO.setCity(doctorJson.getString("city"));
                            userAreaDO.setCityName(doctorJson.getString("cityName"));
                            userAreaDO.setTown(doctorJson.getString("town"));
                            userAreaDO.setTownName(doctorJson.getString("townName"));
                            userAreaDO.setHospital(doctorJson.getString("hospital"));
                            userAreaDO.setHospitalName(doctorJson.getString("hospitalName"));
                            userAreaDO.setUpdateTime(new Date());
                            userAreaDO.setCreateTime(new Date());
                            userAreaDO.setDel(1);
                            userAreaDao.save(userAreaDO);
                        }
                        if (roleDO.getCode().equalsIgnoreCase("replenisher")){
                            String sql = "select d.id as id,d.equ_name as name from t_mediicine_device d where d.belong_community ='"+doctorJson.getString("hospital")+"' ";
                            List<Map<String,Object>> mapList = jdbcTemplate.queryForList(sql);
                            for (Map<String,Object> map:mapList){
                                WlyyUserEquipmentDO equipmentDO = new WlyyUserEquipmentDO();
                                String id = map.get("id").toString();
                                String name = map.get("name")==null?"":map.get("name").toString();
                                equipmentDO.setEquipmentId(id);
                                equipmentDO.setEquipmentName(name);
                                equipmentDO.setUserId(userDO.getId());
                                equipmentDO.setDel(1);
                                equipmentDO.setCityName(doctorJson.getString("cityName"));
                                equipmentDO.setCity(doctorJson.getString("city"));
                                equipmentDO.setTown(doctorJson.getString("town"));
                                equipmentDO.setTownName(doctorJson.getString("townName"));
                                equipmentDO.setHospital(doctorJson.getString("hospital"));
                                equipmentDO.setHospitalName(doctorJson.getString("hospitalName"));
                                equipmentDO.setCreateTime(new Date());
                                equipmentDO.setUpdateTime(new Date());
                                userEquipmentDao.save(equipmentDO);
                            }
                        }
                        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;
        }
    }
    /**
     * 获取全部职称