Browse Source

Merge branch 'dev' of http://192.168.1.220:10080/Amoy2/wlyy2.0 into dev

liuwenbin 6 years ago
parent
commit
14bedee4de

+ 46 - 0
business/base-service/src/main/java/com/yihu/jw/security/dao/TokenDao.java

@ -0,0 +1,46 @@
package com.yihu.jw.security.dao;
import com.yihu.jw.entity.base.security.Token;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
public interface TokenDao extends PagingAndSortingRepository<Token, String> {
	
	@Modifying
	@Query("delete Token a where a.user = ?1")
	int deleteByUser(String user);
	@Modifying
	@Query("delete Token a where a.user = ?1 and platform=?2")
	int deleteByUserAndPlatform(String user, Integer platform);
	
	@Modifying
	@Query("delete Token a where a.token = ?1")
	int deleteByToken(String token);
	
//	@Query("select a from Token a where a.user = ?1 and a.platform = ?2 and a.del = '1'")
//	Token findByUser(String user, int platform);
	
//	@Query("select a from Token a where a.user = ?1 and a.del = '1'")
//	Token findByUser(String user);
	
//	@Query("select count(1) from Token a where a.user = ?1 and a.del = '1'")
//	int countByUser(String user);
	
	@Query("select a from Token a where a.token = ?1 and a.del = '1'")
	Token findByToken(String token);
	
	@Query("select a from Token a where a.user = ?1 and a.platform = ?2 and a.del = '1'")
	Token findByPatient(String patient, int platform);
	
	@Modifying
	@Query("delete Token a where a.user = ?1 and a.platform = 3 and a.del = '1'")
	int deleteWxTokenByPatient(String patient);
	@Query("select a from Token a where a.imei = ?1 and a.platform = 3")
	Page<Token> findByOpenid(String openid, Pageable pageable);
}

+ 136 - 0
business/base-service/src/main/java/com/yihu/jw/security/service/TokenService.java

@ -0,0 +1,136 @@
package com.yihu.jw.security.service;
import com.yihu.jw.entity.base.security.Token;
import com.yihu.jw.security.dao.TokenDao;
import com.yihu.jw.util.date.DateUtil;
import com.yihu.jw.util.security.MD5;
import com.yihu.jw.utils.SystemData;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.UUID;
@Component
@Transactional(rollbackFor = Exception.class)
public class TokenService extends BaseJpaService<Token, TokenDao> {
	@Autowired
	public TokenDao tokenDao;
	/**
	 * 生成token
	 * @param user 用户标识
	 * @param imei 手机IMEI码
	 * @param platform 1患者,2医生,3患者微信公众号,4pc端医生
	 * @return
	 * @throws Exception
	 */
	public Token newToken(String user, String imei, int platform) throws Exception {
		Date czrq = new Date();
		String tokenStr = platform + imei + System.currentTimeMillis();
		// MD5加密
		tokenStr = MD5.GetMD5Code(tokenStr);
//		Token token = tokenDao.findByPatient(user, platform);
//		if (token == null) {
//			token = new Token();
//		}
		Token token = new Token();
		token.setDel("1");
		token.setPlatform(platform);
		token.setImei(imei);
		// 30天的有限期
		token.setTimeout(DateUtil.strToDate(DateUtil.getNextDay(DateUtil.getStringDate(DateUtil.YYYY_MM_DD), 30), DateUtil.YYYY_MM_DD));
		token.setToken(tokenStr);
		token.setUser(user);
		token.setCzrq(czrq);
		// 先删除防止重复
		tokenDao.deleteByUserAndPlatform(user,platform);
		// 添加新的token
		token = tokenDao.save(token);
		if (token == null) {
			throw new Exception("Token生成失败");
		}
		// 更新token缓存
		if (platform == 3) {
			SystemData.patientTokens.put(user, token);
		} else if (platform == 2) {
			SystemData.doctorTokens.put(user, token);
		}else if(platform == 4){
			SystemData.doctorPCTokens.put(user,token);
		}else if(platform == 5){
			SystemData.doctorWXTokens.put(user,token);
		}
		return token;
	}
	public Token newTxToken(String user, String openid) throws Exception {
		Date czrq = new Date();
		String tokenStr = "";
		if(!StringUtils.isBlank(openid)){
			tokenStr = 3 + openid + System.currentTimeMillis();
		}else{
			tokenStr = 3 +""+ System.currentTimeMillis();
		}
		// MD5加密
		tokenStr = MD5.GetMD5Code(tokenStr);
		Token token = new Token();
		token.setDel("1");
		token.setPlatform(3);
		if(StringUtils.isBlank(openid)){
			token.setImei(UUID.randomUUID().toString().replace("-",""));
		}else{
			token.setImei(openid);
		}
		// 360天的有限期
		token.setTimeout(DateUtil.strToDate(DateUtil.getNextDay(DateUtil.getStringDate(DateUtil.YYYY_MM_DD), 360), DateUtil.YYYY_MM_DD));
		token.setToken(tokenStr);
		token.setUser(user);
		token.setCzrq(czrq);
		// 先删除,防止重复
		tokenDao.deleteByUser(user);
		// 添加新的token
		token = tokenDao.save(token);
		if (token == null) {
			throw new Exception("Token生成失败");
		}
		// 更新token缓存
		SystemData.patientTokens.put(user, token);
		return token;
	}
	/**
	 * 删除token
	 * @param uid 用户code
	 * @return
	 * @throws Exception
	 */
	public void delToken(int platform, String uid) throws Exception {
		// 删除老的token
		tokenDao.deleteByUser(uid);
		// 更新token缓存
		if (platform == 3) {
			SystemData.patientTokens.remove(uid);
		} else if (platform == 2) {
			SystemData.doctorTokens.remove(uid);
		}
	}
	/**
	 * 查询患者的微信token标识
	 * @param patient
	 */
	public Token findWxToken(String patient) {
		return tokenDao.findByPatient(patient, 3);
	}
	/**
	 * 查询doctor的微信token标识
	 * @param user
	 */
	public Token findDoctorToken(String user) {
		return tokenDao.findByPatient(user, 4);
	}
}

+ 19 - 0
business/base-service/src/main/java/com/yihu/jw/utils/SystemData.java

@ -0,0 +1,19 @@
package com.yihu.jw.utils;
import com.yihu.jw.entity.base.security.Token;
import java.util.HashMap;
import java.util.Map;
public class SystemData {
	// 医生app验证信息
	public static Map<String, Token> doctorTokens = new HashMap<String, Token>();  // 2
	// 医生pc端验证信息 取药系统 对外系统
	public static Map<String, Token> doctorPCTokens = new HashMap<String, Token>(); // 4
	// 患者验证信息
	public static Map<String, Token> patientTokens = new HashMap<String, Token>(); // 3
	// 医生验证信息
	public static Map<String, Token> doctorWXTokens = new HashMap<String, Token>(); // 5
}

+ 7 - 0
business/sms-service/src/main/java/com/yihu/jw/sms/dao/BaseSmsDao.java

@ -1,7 +1,11 @@
package com.yihu.jw.sms.dao;
import com.yihu.jw.entity.base.sms.SmsDO;
import com.yihu.jw.entity.base.sms.SmsTemplateDO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import java.util.Date;
@ -13,4 +17,7 @@ import java.util.Date;
public interface BaseSmsDao extends PagingAndSortingRepository<SmsDO, String>, JpaSpecificationExecutor<SmsDO> {
    boolean existsByCaptchaAndDeadlineBefore(String captcha, Date now);
    @Query("select a from SmsDO a where a.mobile = ?1 and a.captcha = ?2 and a.type = ?3")
    Page<SmsDO> findByCaptcha(String mobile, String captcha, SmsTemplateDO.Type type, Pageable pageRequest);
}

+ 41 - 0
business/sms-service/src/main/java/com/yihu/jw/sms/service/BaseSmsService.java

@ -1,9 +1,13 @@
package com.yihu.jw.sms.service;
import com.yihu.jw.entity.base.sms.SmsDO;
import com.yihu.jw.entity.base.sms.SmsTemplateDO;
import com.yihu.jw.sms.dao.BaseSmsDao;
import com.yihu.mysql.query.BaseJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import java.util.Date;
@ -26,4 +30,41 @@ public class BaseSmsService extends BaseJpaService<SmsDO, BaseSmsDao>{
    public Boolean verifyCaptcha(String captcha){
        return baseSmsDao.existsByCaptchaAndDeadlineBefore(captcha,new Date());
    }
    /**
     * 验证码校验
     *
     * @param mobile  手机号
     * @param type    type 消息类型:1微信端注册,2微信端找回密码,3医生端找回密码,4患者登录,5医生登录 7用户变更手机号 8用户新手机号验证 9用户绑定手机号
     * @param captcha 验证码
     * @return -1验证码过期,-1验证码错误,0验证码无效,1验证通过
     */
    public int check(String mobile, int type, String captcha) {
        // 根据手机号和验证码查询对应的短信信息
        // 排序
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        // 分页信息
        PageRequest pageRequest = new PageRequest(0, 1, sort);
        SmsTemplateDO.Type value = SmsTemplateDO.valueOf(type);
        Page<SmsDO> page = baseSmsDao.findByCaptcha(mobile, captcha, value, pageRequest);
        SmsDO sms = null;
        for (SmsDO temp : page) {
            if (temp != null) {
                sms = temp;
                break;
            }
        }
        // 验证码校验
        if (sms == null) {
            // 验证码错误
            return -1;
        } else if (type != sms.getType().ordinal()){
            // 验证码无效,也视为错误
            return -1;
        } else if (sms.getDeadline().before(new Date())) {
            // 验证码过期
            return -2;
        }
        return 1;
    }
}

+ 81 - 0
common/common-entity/src/main/java/com/yihu/jw/entity/base/security/Token.java

@ -0,0 +1,81 @@
package com.yihu.jw.entity.base.security;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.yihu.jw.entity.UuidIdentityEntity;
import javax.persistence.Entity;
import javax.persistence.Table;
import java.util.Date;
@Entity
@Table(name = "wlyy_token")
public class Token extends UuidIdentityEntity {
	private static final long serialVersionUID = -3297644428262254694L;
	private String user;
	private Integer platform;   // 1患者端,2医生端app,3微信公众号wechat,4医生端pc
	private Date timeout;
	private String imei;
	private String token;
	private Date czrq;
	private String del;
	public String getUser() {
		return user;
	}
	public void setUser(String user) {
		this.user = user;
	}
	public Integer getPlatform() {
		return platform;
	}
	public void setPlatform(Integer platform) {
		this.platform = platform;
	}
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Date getTimeout() {
		return timeout;
	}
	public void setTimeout(Date timeout) {
		this.timeout = timeout;
	}
	public String getImei() {
		return imei;
	}
	public void setImei(String imei) {
		this.imei = imei;
	}
	public String getToken() {
		return token;
	}
	public void setToken(String token) {
		this.token = token;
	}
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+08:00")
	public Date getCzrq() {
		return czrq;
	}
	public void setCzrq(Date czrq) {
		this.czrq = czrq;
	}
	public String getDel() {
		return del;
	}
	public void setDel(String del) {
		this.del = del;
	}
}

+ 15 - 0
common/common-entity/src/main/java/com/yihu/jw/entity/base/sms/SmsTemplateDO.java

@ -28,6 +28,21 @@ public class SmsTemplateDO extends UuidIdentityEntityWithOperator {
        updateMobile
    }
    public static Type valueOf(int value) {
        switch (value) {
            case 0:
                return Type.register;
            case 1:
                return Type.register;
            case 2:
                return Type.resetPassword;
            case 3:
                return Type.updateMobile;
            default:
                return null;
        }
    }
    //应用ID
    private String clientId;
    //标签

+ 4 - 1
common/common-request-mapping/src/main/java/com/yihu/jw/rm/base/BaseRequestMapping.java

@ -457,7 +457,6 @@ public class BaseRequestMapping {
        public static final String queryOrgCodeAndNameListBySaasId  = "/queryOrgCodeAndNameListBySaasId";
    }
    /**
     * 居民信息
     */
@ -466,6 +465,10 @@ public class BaseRequestMapping {
        public static final String getPatientById  = "/getPatientById";
        public static final String getBaseInfo  = "/getBaseInfo";
        public static final String sendCaptcha  = "/sendCaptcha";
        public static final String Regist = "regist";
        public static final String GetKey = "getKey";
        public static final String Login = "login";
    }
   /**

+ 0 - 7
common/common-request-mapping/src/main/java/com/yihu/jw/rm/patient/PatientRequestMapping.java

@ -26,13 +26,6 @@ public class PatientRequestMapping {
    }
    /**
     * patientInfo
     */
    public static class PatientInfo extends Basic {
        public static final String PREFIX  = "/patientInfo";
    }
    /**
     * signPackage
     */

+ 11 - 0
svr/svr-patient/src/main/java/com/yihu/jw/patient/constant/SystemConstant.java

@ -0,0 +1,11 @@
package com.yihu.jw.patient.constant;
public class SystemConstant {
    // 别处登录
    public static final int LOGIN_OTHER = 999;
    // 登录超时
    public static final int LOGIN_TIMEOUT = 998;
    // 未登录
    public static final int NOT_LOGIN = 997;
}

+ 5 - 0
svr/svr-patient/src/main/java/com/yihu/jw/patient/dao/personal_info/PatientDao.java

@ -3,6 +3,7 @@ package com.yihu.jw.patient.dao.personal_info;
import com.yihu.jw.entity.base.patient.BasePatientDO;
import org.springframework.data.domain.Pageable;
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;
@ -32,4 +33,8 @@ public interface PatientDao extends PagingAndSortingRepository<BasePatientDO, St
    @Query("from BasePatientDO p where p.mobile = ?1 and p.del=?2")
    List<BasePatientDO> findByMobileAndDel(String mobile,String del);
    @Modifying
    @Query(" update BasePatientDO a set a.openid=?2 where a.id=?1 ")
    int updateOpenId(String id, String openId);
}

+ 0 - 375
svr/svr-patient/src/main/java/com/yihu/jw/patient/endpoint/patient/PatientInfoEndpint.java

@ -1,375 +0,0 @@
package com.yihu.jw.patient.endpoint.patient;
import com.yihu.jw.entity.base.patient.BasePatientDO;
import com.yihu.jw.patient.dao.personal_info.PatientDao;
import com.yihu.jw.patient.service.personal_Info.PatientService;
import com.yihu.jw.patient.util.CommonUtils;
import com.yihu.jw.patient.util.RSAService;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import com.yihu.jw.rm.patient.PatientRequestMapping;
import com.yihu.jw.util.security.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@RestController
@RequestMapping(PatientRequestMapping.PatientInfo.PREFIX)
@Api(value = "患者注册,登陆等", description = "患者注册,登陆等", tags = {"居民端 - 患者注册,登陆等"})
public class PatientInfoEndpint extends EnvelopRestEndpoint {
    @Autowired
    private PatientDao patientDao;
    @Autowired
    private PatientService patientService;
    @Autowired
    private RSAService rsaService;
    /**
     * 公钥生成并返回接口
     *
     * @return
     */
    @RequestMapping(value = "getKey", method = RequestMethod.GET)
    @ApiOperation("获取公钥")
    public Envelop publicKey() {
        String modulus = rsaService.getModulus();
        String exponent = rsaService.getExponent();
        Map<String, String> map = new HashMap<>();
        map.put("modulus", modulus); //加密指数
        map.put("exponent", exponent);//加密系数
        return success(map);
    }
    @ApiOperation("居民注册接口")
    @RequestMapping(value = "regist", method = RequestMethod.POST)
    @ResponseBody
    public Envelop regist(@ApiParam(value = "手机号", name = "mobile") @RequestParam(required = true)String mobile ,
                         @ApiParam(value = "验证码", name = "captcha") @RequestParam(value = "captcha", required = true) String captcha,
                         @ApiParam(value = "微信openId", name = "openid") @RequestParam(value = "openid", required = false) String openid,
                         @ApiParam(value = "密码", name = "password") @RequestParam(value = "password", required = false) String password) {
        Envelop envelop = new Envelop();
        boolean b = CommonUtils.isMobile(mobile);
        if(!b){
            envelop.setMessage("手机号码格式不正确");
            envelop.setStatus(-1);
            return envelop;
        }
        //验证手机是否被注册
        List<BasePatientDO> list =  patientDao.findByMobileAndDel(mobile,"1");
        if(list!=null && list.size()> 0){
            envelop.setMessage("该手机号已经注册!");
            envelop.setStatus(-1);
            return envelop;
        }
        // 对验证码进行校验 todo cyx
//            int res = smsService.check(mobile, 1, captcha);
//            switch (res) {
//                case -2:
//                    return error(-1, "验证码已过期!");
//                case -1:
//                    return error(-1, "请输入正确的验证码!");
//                case 0:
//                    return error(-1, "验证码无效!");
//            }
        BasePatientDO patient = new BasePatientDO();
        patient.setMobile(mobile);
        if(StringUtils.isNotBlank(password)){
            //增加密码
            String salt = UUID.randomUUID().toString().replace("-", "");
            patient.setSalt(salt);
            password = rsaService.decryptString(password);
            password = StringUtils.reverse(password);
            if(password.length()<6 || password.length()>20){
                envelop.setMessage("密码长度需为6-20位");
                envelop.setStatus(-1);
                return envelop;
            }
            patient.setPassword(MD5.GetMD5Code(password + salt));
        }
        if(!"undefined".equals(openid) && StringUtils.isNotBlank(openid)){
            patient.setOpenid(openid);
            patient.setOpenidTime(new Date());
        }
        patient.setDel("1");
        patient.setPatientStatus("1");
        patientService.save(patient);
        envelop.setMessage("注册成功");
        envelop.setStatus(200);
        return envelop;
    }
    /**
     * 患者微信登录接口
     *
     * @param captcha  短信号
     * @param mobile   电话号码
     * @param password 登录密码
     * @return
     */
        @RequestMapping(value = "login", method = RequestMethod.POST)
        @ResponseBody
        public Envelop login(
                @RequestParam(required = true) String mobile,
                @RequestParam(required = false) String captcha,
                @RequestParam(required = false) String password,
                @RequestParam(required = false) String openId) {
        Envelop envelop = new Envelop();
        boolean b = CommonUtils.isMobile(mobile);
        if(!b){
            envelop.setMessage("手机号码格式不正确");
            envelop.setStatus(-1);
            return envelop;
        }
        //验证手机是否被注册
        List<BasePatientDO> list = patientDao.findByMobileAndDel(mobile,"1");
        if(CollectionUtils.isEmpty(list)){
            envelop.setMessage("该手机未注册");
            envelop.setStatus(-1);
            return envelop;
        }
        if(list.size()> 1){
            envelop.setMessage("该手机号存在多个账号,请联系管理员");
            envelop.setStatus(-1);
            return envelop;
        }
        BasePatientDO p = list.get(0);
        //用于判断是否登陆成功,若登陆成功,且openId不为空,则更新openId
        boolean isLogin = false;
        if(StringUtils.isNotBlank(password)){
            password = rsaService.decryptString(password);
            password = StringUtils.reverse(password);
            //生成MD5
            String loginPassword = MD5.GetMD5Code(password + p.getSalt());
            if (loginPassword.equals(p.getPassword())) {
                //使用密码登录成功
            }else{
                //使用密码登录失败
            }
        } else {
          //验证码登陆
        }
        if(isLogin && StringUtils.isNotBlank(openId) && !"undefined".equals(openId)){
            //更新openId
            if(!openId.equals(p.getOpenid())){
            }
        }
//         String errorMessage;
//        try {
//            //账号登录 mobile可能是电话号也可能是身份证
//            if (StringUtils.isNoneEmpty(mobile) && StringUtils.isNoneEmpty(password)) {
//                BasePatientDO p = patientService.findByMobile(mobile);
//                //生成MD5
//                String loginPassword = MD5.GetMD5Code(password + p.getSalt());
//                //判断d登录密码是否正确
//                if (loginPassword.equals(p.getPassword())) {
//                    // 绑定用户手机号和openid
//                    if (StringUtils.isNotBlank(openId) && !"undefined".equals(openId)) {//undefined不更新数据库
//                        //patient.setOpenid(openId);
//                        //1.判斷居民OPenid是不是空
//                        if(StringUtils.isNotBlank(p.getOpenid())){
//                            //如果OPenid与原来用户不相等,则判断登录的openids是否被大于10人登录
//                            if(!p.getOpenid().equals(openId)){
//                                //判断登录的openids是否被大于10人登录
////                                if(!patientService.checkOpenidCount(openId)){
////                                    errorMessage ="您已超过系统允许登录的最大居民账号数量,当前不再允许使用该微信登录新的居民账号,请使用其他微信号进行登录";
////                                    return error(-2, errorMessage);
////                                }
//                            }
//                            patientService.updatePatient(p, openId);
//                        }else{
//                            // 判断登录的openids是否被大于10人登录
//                            if(!patientService.checkOpenidCount(openId)){
////                                errorMessage ="您已超过系统允许登录的最大居民账号数量,当前不再允许使用该微信登录新的居民账号,请使用其他微信号进行登录";
////                                return error(-2, errorMessage);
//                            }else{
//                                //未达到上限更新用户openid
//                                patientService.updatePatient(p, openId);
//                            }
//                        }
//                    }
//
//                    // 用户校验通过,生成token
//                    Token token = tokenService.newTxToken(p.getCode(), openId);
//                    Map<Object, Object> map = new HashMap<Object, Object>();
//                    map.put("id", p.getId());
//                    map.put("uid", p.getCode());
//                    map.put("name", p.getName());
//                    map.put("token", token.getToken());
//                    map.put("photo", p.getPhoto());
//                    if (StringUtils.isNoneEmpty(openId)) {
//                        //发送微信模板
//                        familyService.sendWXMessage(p);
//                    }
//
//                    loginLog.setLoginType("1");
//                    loginLogService.saveLog(loginLog);
//                    //判断是否打过标签
//                    //if (!(Patient.isWchatTage.yes.getValue() == p.getIsWxtag())) {
//
//                    //清空患者的微信标签
//                    weiXinTagUtil.deleteTagWithOpenid(p.getOpenid());
//                    //给患者打微信标签
//                    weiXinTagUtil.addTagWithOpenid(openId, p.getCode(), p.getName());
//                    //}
//                    return write(200, "登录成功", "data", map);
//                } else {
//                    errorMessage = "密码错误,登录失败";
//                    loginLog.setErrorMessage(errorMessage);
//                    loginLogService.saveLog(loginLog);
//                    return error(-1, errorMessage);
//                }
//            }
//            //短信登录
//            if (StringUtils.isNoneEmpty(mobile) && StringUtils.isNoneEmpty(captcha)) {
//                List<Patient> patients = patientService.findByMobile(mobile);
//                if (patients.size() > 1) {
//                    return error(-1, "此手机存在多个用户,请用身份证和密码登录!");
//                }
//                // 对验证码进行校验
//                int res = smsService.check(mobile, 4, captcha);
//                switch (res) {
//                    case -2: {
//                        errorMessage = "验证码已过期!";
//                        loginLog.setErrorMessage(errorMessage);
//                        loginLogService.saveLog(loginLog);
//                        return error(-1, errorMessage);
//                    }
//                    case -1: {
//                        errorMessage = "请输入正确的验证码!";
//                        loginLog.setErrorMessage(errorMessage);
//                        loginLogService.saveLog(loginLog);
//                        return error(-1, errorMessage);
//                    }
//                    case 0: {
//                        errorMessage = "验证码无效!";
//                        loginLog.setErrorMessage(errorMessage);
//                        loginLogService.saveLog(loginLog);
//                        return error(-1, errorMessage);
//                    }
//                }
//
//                loginLog.setLoginType("1");
//                if (patients == null || patients.size() == 0) {
//                    if (mobile.length() == 11) {
//                        errorMessage = "该手机号暂未注册账号,请确认后重新输入!";
//                    } else {
//                        errorMessage = "该身份证号暂未注册账号,请确认后重新输入!";
//                    }
//                    loginLog.setErrorMessage(errorMessage);
//                    loginLogService.saveLog(loginLog);
//                    return error(-1, errorMessage);
//                } else {
//                    Patient p = null;
//                    if (patients.size() == 1) {
//                        p = patients.get(0);
//                    }
//                    if (p.getStatus() == 0) {
//                        if (mobile.length() == 11) {
//                            errorMessage = "该手机号已被禁止使用!";
//                        } else {
//                            errorMessage = "该身份证号已被禁止使用!";
//                        }
//                        loginLog.setErrorMessage(errorMessage);
//                        loginLogService.saveLog(loginLog);
//                        return error(-1, errorMessage);
//                    } else if (p.getStatus() == 2) {
//                        errorMessage = "该账号正在审核中,请确认审核通过后再登录,“如有疑问,拨打400-6677-400转2人工客服”";
//                        loginLog.setErrorMessage(errorMessage);
//                        loginLogService.saveLog(loginLog);
//                        return error(-1, errorMessage);
//                    } else if (StringUtils.isEmpty(openId)) {
//                        errorMessage = "无效的OpenID!";
//                        loginLog.setErrorMessage(errorMessage);
//                        loginLogService.saveLog(loginLog);
//                        return error(-1, errorMessage);
//                    }
//                    loginLog.setUserCode(p.getCode());
//                    // 绑定用户手机号和openid
////                    if (!StringUtils.equals(p.getOpenid(), openId) && !"undefined".equals(openId)) {//undefined不更新数据库
////                        //patient.setOpenid(openId);
////                        patientService.updatePatient(p, openId);
////                    }
//                    if (StringUtils.isNotBlank(openId) && !"undefined".equals(openId)) {//undefined不更新数据库
//                        //patient.setOpenid(openId);
//                        //1.判斷居民OPenid是不是空
//                        if(StringUtils.isNotBlank(p.getOpenid())){
//                            //如果OPenid与原来用户不相等,则判断登录的openids是否被大于10人登录
//                            if(!p.getOpenid().equals(openId)){
//                                //判断登录的openids是否被大于10人登录
//                                if(!patientService.checkOpenidCount(openId)){
////                                    errorMessage ="您已超过系统允许登录的最大居民账号数量,当前不再允许使用该微信登录新的居民账号,请使用其他微信号进行登录";
////                                    return error(-2, errorMessage);
//                                }
//                            }
//                            patientService.updatePatient(p, openId);
//                        }else{
//                            // 判断登录的openids是否被大于10人登录
//                            if(!patientService.checkOpenidCount(openId)){
////                                errorMessage ="您已超过系统允许登录的最大居民账号数量,当前不再允许使用该微信登录新的居民账号,请使用其他微信号进行登录";
////                                return error(-2, errorMessage);
//                            }else{
//                                //未达到上限更新用户openid
//                                patientService.updatePatient(p, openId);
//                            }
//                        }
//                    }
//
//                    // 用户校验通过,生成token
//                    Token token = tokenService.newTxToken(p.getCode(), openId);
//                    Map<Object, Object> map = new HashMap<Object, Object>();
//                    map.put("id", p.getId());
//                    map.put("uid", p.getCode());
//                    map.put("name", p.getName());
//                    map.put("token", token.getToken());
//                    map.put("photo", p.getPhoto());
//                    if (StringUtils.isNoneEmpty(openId)) {
//                        //发送微信模板
//                        familyService.sendWXMessage(p);
//                    }
//                    loginLog.setType("1");
//                    loginLogService.saveLog(loginLog);
//                    //判断是否打过标签
//                    //if (!(Patient.isWchatTage.yes.getValue() == p.getIsWxtag())) {
//                    //清空患者的微信标签
//                    weiXinTagUtil.deleteTagWithOpenid(p.getOpenid());
//                    //给患者打微信标签
//                    weiXinTagUtil.addTagWithOpenid(openId, p.getCode(), p.getName());
//                    //}
//                    return write(200, "登录成功", "data", map);
//                }
//            }
//            return error(-1, "登录失败");
//        } catch (Exception e) {
//
//            errorMessage = "系统异常,登录失败";
//            loginLog.setErrorMessage(errorMessage);
//            loginLogService.saveLog(loginLog);
//            error(e);
//            return error(-1, "系统异常,登录失败");
//        }
            return null;
    }
}

+ 203 - 8
svr/svr-patient/src/main/java/com/yihu/jw/patient/endpoint/personal_info/PatientEndpoint.java

@ -1,11 +1,14 @@
package com.yihu.jw.patient.endpoint.personal_info;
import com.alibaba.fastjson.JSONObject;
import com.yihu.jw.entity.base.patient.BasePatientDO;
import com.yihu.jw.entity.base.security.Token;
import com.yihu.jw.entity.base.sms.SmsDO;
import com.yihu.jw.entity.base.sms.SmsTemplateDO;
import com.yihu.jw.patient.service.BasePatientService;
import com.yihu.jw.patient.dao.personal_info.PatientDao;
import com.yihu.jw.patient.service.personal_Info.PatientService;
import com.yihu.jw.patient.util.CommonUtils;
import com.yihu.jw.patient.util.ConstantUtils;
import com.yihu.jw.patient.util.RSAService;
import com.yihu.jw.restmodel.base.patient.BasePatientVO;
import com.yihu.jw.restmodel.base.sms.SmsVO;
import com.yihu.jw.restmodel.web.Envelop;
@ -14,17 +17,20 @@ import com.yihu.jw.restmodel.web.ObjEnvelop;
import com.yihu.jw.restmodel.web.PageEnvelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import com.yihu.jw.rm.base.BaseRequestMapping;
import com.yihu.jw.security.service.TokenService;
import com.yihu.jw.sms.service.BaseSmsGatewayService;
import com.yihu.jw.sms.service.BaseSmsService;
import com.yihu.jw.util.security.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.*;
/**
 * 居民信息控制器
@ -41,15 +47,204 @@ import java.util.Map;
@Api(value = "居民信息管理", description = "居民信息管理服务接口", tags = {"wlyy基础服务 - 居民信息管理服务接口"})
public class PatientEndpoint extends EnvelopRestEndpoint {
    @Autowired
    private BasePatientService patientService;
    @Value("sms.clientId")
    @Value("${sms.clientId}")
    private String clientId;
    @Autowired
    private BaseSmsGatewayService baseSmsGatewayService;
    @Autowired
    private PatientDao patientDao;
    @Autowired
    private PatientService patientService;
    @Autowired
    private RSAService rsaService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private BaseSmsService baseSmsService;
    /**
     * 公钥生成并返回接口
     *
     * @return
     */
    @RequestMapping(value = BaseRequestMapping.BasePatient.GetKey, method = RequestMethod.GET)
    @ApiOperation("获取公钥")
    public Envelop publicKey() {
        String modulus = rsaService.getModulus();
        String exponent = rsaService.getExponent();
        Map<String, String> map = new HashMap<>();
        map.put("modulus", modulus); //加密指数
        map.put("exponent", exponent);//加密系数
        return success(map);
    }
    @ApiOperation("居民注册接口")
    @RequestMapping(value = BaseRequestMapping.BasePatient.Regist, method = RequestMethod.POST)
    @ResponseBody
    public Envelop regist(@ApiParam(value = "手机号", name = "mobile") @RequestParam(required = true)String mobile ,
                          @ApiParam(value = "验证码", name = "captcha") @RequestParam(value = "captcha", required = true) String captcha,
                          @ApiParam(value = "微信openId", name = "openid") @RequestParam(value = "openid", required = false) String openid,
                          @ApiParam(value = "密码", name = "password") @RequestParam(value = "password", required = false) String password) {
        Envelop envelop = new Envelop();
        boolean b = CommonUtils.isMobile(mobile);
        if(!b){
            envelop.setMessage("手机号码格式不正确");
            envelop.setStatus(-1);
            return envelop;
        }
        //验证手机是否被注册
        List<BasePatientDO> list = patientDao.findByMobileAndDel(mobile,"1");
        if(list!=null && list.size()> 0){
            envelop.setMessage("该手机号已经注册!");
            envelop.setStatus(-1);
            return envelop;
        }
        // 对验证码进行校验
        int res = baseSmsService.check(mobile, 0, captcha);
        if(-2 == res){
            envelop.setMessage("验证码已过期!");
            envelop.setStatus(-1);
            return envelop;
        }else if(-1 == res){
            envelop.setMessage("请输入正确的验证码!");
            envelop.setStatus(-1);
            return envelop;
        }else if(0 == res){
            envelop.setMessage("验证码无效!");
            envelop.setStatus(-1);
            return envelop;
        } else{
            BasePatientDO patient = new BasePatientDO();
            patient.setMobile(mobile);
            if(StringUtils.isNotBlank(password)){
                //增加密码
                String salt = UUID.randomUUID().toString().replace("-", "");
                patient.setSalt(salt);
                password = rsaService.decryptString(password);
                password = StringUtils.reverse(password);
                if(password.length()<6 || password.length()>20){
                    envelop.setMessage("密码长度需为6-20位");
                    envelop.setStatus(-1);
                    return envelop;
                }
                patient.setPassword(MD5.GetMD5Code(password + salt));
            }
            if(!"undefined".equals(openid) && StringUtils.isNotBlank(openid)){
                patient.setOpenid(openid);
                patient.setOpenidTime(new Date());
            }
            patient.setDel("1");
            patient.setPatientStatus("1");
            patientService.save(patient);
            envelop.setMessage("注册成功");
            envelop.setStatus(200);
            return envelop;
        }
    }
    /**
     * 患者微信登录接口
     *
     * @param captcha  短信号
     * @param mobile   电话号码
     * @param password 登录密码
     * @return
     */
    @RequestMapping(value = BaseRequestMapping.BasePatient.Login, method = RequestMethod.POST)
    @ResponseBody
    public Envelop login(
            @RequestParam(required = true) String mobile,
            @RequestParam(required = false) String captcha,
            @RequestParam(required = false) String password,
            @RequestParam(required = false) String openId) throws Exception {
        Envelop envelop = new Envelop();
        boolean b = CommonUtils.isMobile(mobile);
        if(!b){
            envelop.setMessage("手机号码格式不正确");
            envelop.setStatus(-1);
            return envelop;
        }
        //验证手机是否被注册
        List<BasePatientDO> list = patientDao.findByMobileAndDel(mobile,"1");
        if(CollectionUtils.isEmpty(list)){
            envelop.setMessage("该手机未注册");
            envelop.setStatus(-1);
            return envelop;
        }
        if(list.size()> 1){
            envelop.setMessage("该手机号存在多个账号,请联系管理员");
            envelop.setStatus(-1);
            return envelop;
        }
        BasePatientDO p = list.get(0);
        //用于判断是否登陆成功,若登陆成功,且openId不为空,则更新openId
        boolean isLogin = false;
        if(org.apache.commons.lang3.StringUtils.isNotBlank(password)){
            password = rsaService.decryptString(password);
            password = org.apache.commons.lang3.StringUtils.reverse(password);
            //生成MD5
            String loginPassword = MD5.GetMD5Code(password + p.getSalt());
            if (loginPassword.equals(p.getPassword())) {
                //使用密码登录成功
                isLogin = true;
            }else{
                //使用密码登录失败
                envelop.setMessage("密码错误");
                envelop.setStatus(-1);
                return envelop;
            }
        } else {
            //验证码登陆
            int res = baseSmsService.check(mobile, 4, captcha);
            if(-2 == res){
                envelop.setMessage("验证码已过期!");
                envelop.setStatus(-1);
                return envelop;
            }else if(-1 == res){
                envelop.setMessage("请输入正确的验证码!");
                envelop.setStatus(-1);
                return envelop;
            }else if(0 == res) {
                envelop.setMessage("验证码无效!");
                envelop.setStatus(-1);
                return envelop;
            } else {
                //使用密码登录成功
                isLogin = true;
            }
        }
        if(isLogin){
            if(StringUtils.isNotBlank(openId) && !"undefined".equals(openId)){
                //更新openId
                if(!openId.equals(p.getOpenid())){
                    patientService.updateOpenId(p.getId(), openId);
                }
            }
            // 用户校验通过,生成token
            Token token = tokenService.newTxToken(p.getId(), p.getOpenid());
            Map<Object, Object> map = new HashMap<Object, Object>();
            map.put("id", p.getId());
            map.put("uid", p.getId());
            map.put("name", p.getName());
            map.put("token", token.getToken());
            map.put("photo", p.getPhoto());
            envelop.setMessage("登陆成功");
            envelop.setStatus(200);
        }
        return envelop;
    }
    @PostMapping(value = BaseRequestMapping.BasePatient.CREATE)
    @ApiOperation(value = "创建")
    public Envelop create(

+ 4 - 2
svr/svr-patient/src/main/java/com/yihu/jw/patient/service/personal_Info/PatientService.java

@ -4,7 +4,6 @@ import com.google.common.base.Preconditions;
import com.yihu.jw.entity.base.patient.BasePatientDO;
import com.yihu.jw.exception.business.patient.CapthcaInvalidException;
import com.yihu.jw.exception.business.patient.NotFoundPatientException;
import com.yihu.jw.file_upload.FileUploadService;
import com.yihu.jw.patient.dao.BasePatientDao;
import com.yihu.jw.patient.dao.personal_info.PatientDao;
import com.yihu.jw.patient.service.BasePatientService;
@ -101,7 +100,10 @@ public class PatientService extends BasePatientService<BasePatientDO, BasePatien
        return patientDO;
    }
    public void updateOpenId(String openId){
    @Transactional(rollbackFor = Exception.class)
    public int updateOpenId(String id, String openId){
        return patientDao.updateOpenId(id,openId);
    }

+ 7 - 4
svr/svr-patient/src/main/resources/application.yml

@ -99,9 +99,8 @@ spring:
fastDFS:
  fastdfs_file_url: http://172.19.103.54:80/
# 短信发送地址
jw:
  smsUrl: http://svr-base:10020/sms_gateway/send
sms:
  clientId: EwC0iRSrcP
myFamily:
  qrCodeFailurTime: 2
---
@ -131,6 +130,8 @@ spring:
    port: 6379 # Redis server port.
fastDFS:
  fastdfs_file_url: http://172.19.103.54:80/
sms:
  clientId: EwC0iRSrcP
myFamily:
  qrCodeFailurTime: 2
---
@ -158,4 +159,6 @@ spring:
fastDFS:
  fastdfs_file_url: http://172.19.103.54:80/
myFamily:
  qrCodeFailurTime: 2
  qrCodeFailurTime: 2
sms:
  clientId: EwC0iRSrcP # todo 待确认

+ 1 - 1
svr/svr-patient/src/main/resources/bootstrap.yml

@ -1,6 +1,6 @@
spring:
  application:
    name: svr-patient-lyx
    name: svr-patient
  cloud:
    config:
      failFast: true