11 changed files with 356 additions and 17 deletions
			
			
		| @ -0,0 +1,149 @@ | |||
| package com.epmet.commons.tools.utils; | |||
| 
 | |||
| import lombok.extern.slf4j.Slf4j; | |||
| 
 | |||
| import java.io.UnsupportedEncodingException; | |||
| import java.security.MessageDigest; | |||
| 
 | |||
| @Slf4j | |||
| public class Md5Util { | |||
| 	/** | |||
| 	 * 加密盐 值 | |||
| 	 */ | |||
| 	public static final String SALT = "EPMET_UMD_SALT"; | |||
| 
 | |||
| 	public static String md5(String string) { | |||
| 		if (string == null || string.trim().length() == 0) { | |||
| 			return null; | |||
| 		} | |||
| 		try { | |||
| 			return getMD5(string.getBytes("GBK")); | |||
| 		} catch (UnsupportedEncodingException e) { | |||
| 			log.error( "md5 is error,msg={0}", e); | |||
| 			return null; | |||
| 		} | |||
| 	} | |||
| 
 | |||
| 	private static final char hexDigits[] = { // 用来将字节转换成 16 进制表示的字符
 | |||
| 			'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | |||
| 
 | |||
| 	private static String getMD5(byte[] source) { | |||
| 		String s = null; | |||
| 		try { | |||
| 			MessageDigest md = MessageDigest.getInstance("MD5"); | |||
| 			md.update(source); | |||
| 			byte tmp[] = md.digest(); // MD5 的计算结果是一个 128 位的长整数,
 | |||
| 			// 用字节表示就是 16 个字节
 | |||
| 			char str[] = new char[16 * 2]; // 每个字节用 16 进制表示的话,使用两个字符,
 | |||
| 			// 所以表示成 16 进制需要 32 个字符
 | |||
| 			int k = 0; // 表示转换结果中对应的字符位置
 | |||
| 			for (int i = 0; i < 16; i++) { // 从第一个字节开始,对 MD5 的每一个字节
 | |||
| 				// 转换成 16 进制字符的转换
 | |||
| 				byte byte0 = tmp[i]; // 取第 i 个字节
 | |||
| 				str[k++] = hexDigits[byte0 >>> 4 & 0xf]; // 取字节中高 4 位的数字转换,
 | |||
| 				// >>> 为逻辑右移,将符号位一起右移
 | |||
| 				str[k++] = hexDigits[byte0 & 0xf]; // 取字节中低 4 位的数字转换
 | |||
| 			} | |||
| 			s = new String(str); // 换后的结果转换为字符串
 | |||
| 
 | |||
| 		} catch (Exception e) { | |||
| 			log.error("getMD5 is error,msg={0}", e); | |||
| 		} | |||
| 		return s; | |||
| 	} | |||
| 
 | |||
| 	private static String byteArrayToHexString(byte b[]) { | |||
| 		StringBuffer resultSb = new StringBuffer(); | |||
| 		for (int i = 0; i < b.length; i++) | |||
| 			resultSb.append(byteToHexString(b[i])); | |||
| 
 | |||
| 		return resultSb.toString(); | |||
| 	} | |||
| 
 | |||
| 	private static String byteToHexString(byte b) { | |||
| 		int n = b; | |||
| 		if (n < 0) | |||
| 			n += 256; | |||
| 		int d1 = n / 16; | |||
| 		int d2 = n % 16; | |||
| 		return hexDigits[d1] + "" + hexDigits[d2]; | |||
| 	} | |||
| 
 | |||
| 	public static String MD5Encode(String origin, String charsetname) { | |||
| 		String resultString = null; | |||
| 		try { | |||
| 			resultString = new String(origin); | |||
| 			MessageDigest md = MessageDigest.getInstance("MD5"); | |||
| 			if (charsetname == null || "".equals(charsetname)) | |||
| 				resultString = byteArrayToHexString(md.digest(resultString | |||
| 						.getBytes())); | |||
| 			else | |||
| 				resultString = byteArrayToHexString(md.digest(resultString | |||
| 						.getBytes(charsetname))); | |||
| 		} catch (Exception e) { | |||
| 			log.error("MD5Encode is error,msg={0}", e); | |||
| 		} | |||
| 		return resultString; | |||
| 	} | |||
| 
 | |||
| 
 | |||
| 	public static void main(String[] args) { | |||
| 		for (int i = 0; i < 5; i++) { | |||
| 			String uuid = "03a1dcd8cb1811eabac1c03fd56f7847"; | |||
| 			System.out.println(get12Char(uuid)); | |||
| 		} | |||
| 	} | |||
| 
 | |||
| 	/** | |||
| 	 * 获取短字符 | |||
| 	 * | |||
| 	 * @param str | |||
| 	 * @return 大写 | |||
| 	 */ | |||
| 	public static String get12Char(String str) { | |||
| 		String arr[] = ShortText(str); | |||
| 		String rst = (arr[0] + arr[1]).toUpperCase(); | |||
| 		return rst.substring(0, 4) + rst.substring(4, 8) + rst.substring(8, 12); | |||
| 	} | |||
| 
 | |||
| 	private static String[] ShortText(String string) { | |||
| 		String[] chars = new String[]{ // 要使用生成URL的字符
 | |||
| 				"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", | |||
| 				"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", | |||
| 				"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", | |||
| 				"C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", | |||
| 				"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}; | |||
| 
 | |||
| 		String hex = ""; | |||
| 
 | |||
| 		MessageDigest md = null; | |||
| 		try { | |||
| 			md = MessageDigest.getInstance("MD5"); | |||
| 			hex = byteArrayToHexString(md.digest(SALT.concat(string) | |||
| 					.getBytes("utf-8"))); | |||
| 		} catch (Exception e) { | |||
| 			e.printStackTrace(); | |||
| 		} | |||
| 
 | |||
| 		int hexLen = hex.length(); | |||
| 		int subHexLen = hexLen / 8; | |||
| 		String[] ShortStr = new String[4]; | |||
| 
 | |||
| 		for (int i = 0; i < subHexLen; i++) { | |||
| 			String outChars = ""; | |||
| 			int j = i + 1; | |||
| 			String subHex = hex.substring(i * 8, j * 8); | |||
| 			long idx = Long.valueOf("3FFFFFFF", 16) & Long.valueOf(subHex, 16); | |||
| 
 | |||
| 			for (int k = 0; k < 6; k++) { | |||
| 				int index = (int) (Long.valueOf("0000003D", 16) & idx); | |||
| 				outChars += chars[index]; | |||
| 				idx = idx >> 5; | |||
| 			} | |||
| 			ShortStr[i] = outChars; | |||
| 		} | |||
| 
 | |||
| 		return ShortStr; | |||
| 	} | |||
| 
 | |||
| } | |||
| @ -0,0 +1,25 @@ | |||
| package com.epmet.resi.mine.dto.from; | |||
| 
 | |||
| import lombok.Data; | |||
| 
 | |||
| import java.io.Serializable; | |||
| 
 | |||
| /** | |||
|  * @Description | |||
|  * @ClassName MyResiUserInfoFormDTO | |||
|  * @Auth wangc | |||
|  * @Date 2020-05-22 17:27 | |||
|  */ | |||
| @Data | |||
| public class GenerateShortUserIdFormDTO implements Serializable { | |||
| 
 | |||
|     /** | |||
|      * 过期时间 单位秒 | |||
|      */ | |||
|     private Integer expires; | |||
| 
 | |||
|     /** | |||
|      * 场景类型 | |||
|      */ | |||
|     private String bizType; | |||
| } | |||
| @ -0,0 +1,27 @@ | |||
| package com.epmet.resi.mine.dto.result; | |||
| 
 | |||
| import lombok.Data; | |||
| 
 | |||
| import java.io.Serializable; | |||
| import java.util.Date; | |||
| 
 | |||
| /** | |||
|  * desc:  生成用户短用户标识 result | |||
|  * | |||
|  * @date: 2020/7/22 10:50 | |||
|  * @author: jianjun liu | |||
|  */ | |||
| @Data | |||
| public class ShortUserIdInfoResultDTO implements Serializable { | |||
| 
 | |||
| 	/** | |||
| 	 * 12位用户标识 | |||
| 	 */ | |||
| 	private String shortUserId; | |||
| 	/** | |||
| 	 * 过期时间 | |||
| 	 */ | |||
| 	private Date expiresTime; | |||
| 
 | |||
| 
 | |||
| } | |||
| @ -0,0 +1,41 @@ | |||
| package com.epmet.eums; | |||
| 
 | |||
| /** | |||
|  * 生成用户唯一标识 枚举类 | |||
|  * dev|test|prod | |||
|  * | |||
|  * @author jianjun liu | |||
|  * @date 2020-07-03 11:14 | |||
|  **/ | |||
| public enum GenerateShortUserIdBizTypeEnum { | |||
| 	POINT_EXCHANGE("pointExchange", "积分兑换") | |||
| 	; | |||
| 
 | |||
| 	private String code; | |||
| 	private String desc; | |||
| 
 | |||
| 
 | |||
| 
 | |||
| 	GenerateShortUserIdBizTypeEnum(String code, String name) { | |||
| 		this.code = code; | |||
| 		this.desc = name; | |||
| 	} | |||
| 
 | |||
| 	public static GenerateShortUserIdBizTypeEnum getEnum(String code) { | |||
| 		GenerateShortUserIdBizTypeEnum[] values = GenerateShortUserIdBizTypeEnum.values(); | |||
| 		for (GenerateShortUserIdBizTypeEnum value : values) { | |||
| 			if (code != null && value.getCode().equals(code)) { | |||
| 				return value; | |||
| 			} | |||
| 		} | |||
| 		return null; | |||
| 	} | |||
| 
 | |||
| 	public String getCode() { | |||
| 		return code; | |||
| 	} | |||
| 
 | |||
| 	public String getDesc() { | |||
| 		return desc; | |||
| 	} | |||
| } | |||
| @ -1,43 +1,91 @@ | |||
| package com.epmet.modules.mine.controller; | |||
| 
 | |||
| import com.alibaba.fastjson.JSON; | |||
| import com.epmet.commons.tools.annotation.LoginUser; | |||
| import com.epmet.commons.tools.constant.NumConstant; | |||
| import com.epmet.commons.tools.exception.EpmetErrorCode; | |||
| import com.epmet.commons.tools.exception.RenException; | |||
| import com.epmet.commons.tools.redis.RedisKeys; | |||
| import com.epmet.commons.tools.redis.RedisUtils; | |||
| import com.epmet.commons.tools.security.dto.TokenDto; | |||
| import com.epmet.commons.tools.utils.DateUtils; | |||
| import com.epmet.commons.tools.utils.Md5Util; | |||
| import com.epmet.commons.tools.utils.Result; | |||
| import com.epmet.commons.tools.validator.ValidatorUtils; | |||
| import com.epmet.eums.GenerateShortUserIdBizTypeEnum; | |||
| import com.epmet.modules.feign.EpmetUserFeignClient; | |||
| import com.epmet.resi.mine.dto.from.GenerateShortUserIdFormDTO; | |||
| import com.epmet.resi.mine.dto.from.MyResiUserInfoFormDTO; | |||
| import com.epmet.resi.mine.dto.result.MyResiUserInfoResultDTO; | |||
| import com.epmet.resi.mine.dto.result.ShortUserIdInfoResultDTO; | |||
| import lombok.extern.slf4j.Slf4j; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.web.bind.annotation.PostMapping; | |||
| import org.springframework.web.bind.annotation.RequestBody; | |||
| import org.springframework.web.bind.annotation.RequestMapping; | |||
| import org.springframework.web.bind.annotation.RestController; | |||
| 
 | |||
| import java.util.Date; | |||
| 
 | |||
| /** | |||
|  * @Description | |||
|  * @ClassName MineController | |||
|  * @Auth wangc | |||
|  * @Date 2020-05-22 18:33 | |||
|  */ | |||
| @Slf4j | |||
| @RestController | |||
| @RequestMapping("mine") | |||
| public class MineController { | |||
| 
 | |||
|     @Autowired | |||
|     EpmetUserFeignClient epmetUserFeignClient; | |||
| 	@Autowired | |||
| 	private EpmetUserFeignClient epmetUserFeignClient; | |||
| 	@Autowired | |||
| 	private RedisUtils redisUtils; | |||
| 
 | |||
| 	/** | |||
| 	 * @param myResiUserInfoFormDTO | |||
| 	 * @return MyResiUserInfoResultDTO | |||
| 	 * @Description 居民端获取我的信息 | |||
| 	 * @author wangc | |||
| 	 * @date 2020.05.22 18:37 | |||
| 	 **/ | |||
| 	@PostMapping("profile") | |||
| 	Result<MyResiUserInfoResultDTO> getMyResiInfo(@LoginUser TokenDto tokenDto, @RequestBody MyResiUserInfoFormDTO myResiUserInfoFormDTO) { | |||
| 		myResiUserInfoFormDTO.setUserId(tokenDto.getUserId()); | |||
| 		ValidatorUtils.validateEntity(myResiUserInfoFormDTO); | |||
| 		return epmetUserFeignClient.resiMyselfMsg(myResiUserInfoFormDTO); | |||
| 	} | |||
| 
 | |||
|     /** | |||
|      * @Description 居民端获取我的信息 | |||
|      * @param myResiUserInfoFormDTO | |||
|      * @return  MyResiUserInfoResultDTO | |||
|      * @author wangc | |||
|      * @date 2020.05.22 18:37 | |||
|      **/ | |||
|     @PostMapping("profile") | |||
|     Result<MyResiUserInfoResultDTO> getMyResiInfo(@LoginUser TokenDto tokenDto, @RequestBody MyResiUserInfoFormDTO myResiUserInfoFormDTO){ | |||
|         myResiUserInfoFormDTO.setUserId(tokenDto.getUserId()); | |||
|         ValidatorUtils.validateEntity(myResiUserInfoFormDTO); | |||
|         return epmetUserFeignClient.resiMyselfMsg(myResiUserInfoFormDTO); | |||
|     } | |||
| 	/** | |||
| 	 * desc:生成用户12位唯一标识 默认10分钟过期 | |||
| 	 * | |||
| 	 * @param tokenDto | |||
| 	 * @param formDTO | |||
| 	 * @return | |||
| 	 */ | |||
| 	@PostMapping("generateshortuserid") | |||
| 	Result<ShortUserIdInfoResultDTO> generateShortUserId(TokenDto tokenDto, @RequestBody GenerateShortUserIdFormDTO formDTO) { | |||
| 		String userId = tokenDto.getUserId(); | |||
| 		GenerateShortUserIdBizTypeEnum bizTypeEnum = GenerateShortUserIdBizTypeEnum.getEnum(formDTO.getBizType()); | |||
| 		if (bizTypeEnum == null) { | |||
| 			throw new RenException(EpmetErrorCode.INTERNAL_VALIDATE_ERROR.getMsg()); | |||
| 		} | |||
| 		String shortUserId = Md5Util.get12Char(bizTypeEnum.getCode().concat(userId)); | |||
| 		String redisKey = RedisKeys.getShortUserIdKey(shortUserId); | |||
| 		//过期时间 单位:秒
 | |||
| 		int expires = formDTO.getExpires() == null ? NumConstant.TEN * NumConstant.SIXTY : formDTO.getExpires(); | |||
| 		if (redisUtils.hasKey(shortUserId)) { | |||
| 			redisUtils.expire(redisKey, expires); | |||
| 		} else { | |||
| 			redisUtils.setString(redisKey, userId, expires); | |||
| 		} | |||
| 		Date expiresDate = DateUtils.addDateSeconds(new Date(), expires); | |||
| 		ShortUserIdInfoResultDTO result = new ShortUserIdInfoResultDTO(); | |||
| 		result.setShortUserId(shortUserId); | |||
| 		result.setExpiresTime(expiresDate); | |||
| 		log.info("generateShortUserId success,result:{}", JSON.toJSONString(result)); | |||
| 		return new Result<ShortUserIdInfoResultDTO>().ok(result); | |||
| 	} | |||
| 
 | |||
| } | |||
|  | |||
					Loading…
					
					
				
		Reference in new issue