提交 d48b4134 编写于 作者: rushui.chen's avatar rushui.chen

20190827 工具类的封装

上级 8c93aa14
流水线 #13700 已失败 于阶段
in 0 second
package com.pica.cloud.account.account.server.controller;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;
@Api("修改手机号资源")
@RestController
public class ModifyMobileController {
}
......@@ -7,4 +7,10 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public class RegisterController {
}
package com.pica.cloud.account.account.server.util;
import com.alibaba.fastjson.JSONObject;
import com.pica.cloud.foundation.entity.PicaException;
import com.pica.cloud.foundation.entity.PicaResultCode;
import com.pica.cloud.foundation.redis.ICacheClient;
import com.pica.cloud.foundation.utils.utils.EncryptCreateUtil;
import com.pica.cloud.foundation.utils.utils.ValidateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
/**
* 账户工具类
*/
@Component
public class AccountUtils {
@Autowired
@Qualifier("cacheMigrateClient")
private ICacheClient cacheClient;
private static final String AUTH_CODE_PREFIX = "authCode-";
//手机格式校验
public static void checkMobilePhone(String mobilePhone) {
if (StringUtils.isBlank(mobilePhone) || !ValidateUtils.isMobile(mobilePhone)) {
throw new PicaException(PicaResultCode.PARAM_IS_INVALID.code(), "请输入正确的手机号");
}
}
//获取验证码redis key
public static String getAuthCodeKey(String mobilePhone, String flag) {
return AUTH_CODE_PREFIX + flag + "-" + EncryptCreateUtil.encrypt(mobilePhone);
}
//校验验证码
public void checkAuthCode(String mobile, String type, String sysCode) {
String flag = org.apache.commons.lang.StringUtils.isBlank(type) ? "0" : type;
if (org.apache.commons.lang.StringUtils.isBlank(sysCode)) {
throw new PicaException(PicaResultCode.PARAM_IS_INVALID.code(), "短信验证码错误");
}
String authCodeKey = AccountUtils.getAuthCodeKey(mobile, flag);
String cacheCode = cacheClient.get(authCodeKey); //从redis获取验证码
if (org.apache.commons.lang.StringUtils.isBlank(cacheCode)) {
throw new PicaException(PicaResultCode.RESULE_DATA_NONE.code(), "短信验证码已过期,请重新获取");
}
if (!org.apache.commons.lang.StringUtils.equals(sysCode, cacheCode)) {
throw new PicaException(PicaResultCode.PARAM_IS_INVALID.code(), "短信验证码错误");
}
//清除验证码
cacheClient.del(authCodeKey);
}
//手机号和验证码校验
public void checkMobilePhoneAndAuthCode(String mobile, String type, String sysCode) {
if (StringUtils.isBlank(mobile) || !ValidateUtils.isMobile(mobile)) {
throw new PicaException(PicaResultCode.PARAM_IS_INVALID.code(), "请输入正确的手机号");
}
String flag = org.apache.commons.lang.StringUtils.isBlank(type) ? "0" : type;
if (org.apache.commons.lang.StringUtils.isBlank(sysCode)) {
throw new PicaException(PicaResultCode.PARAM_IS_INVALID.code(), "短信验证码错误");
}
String authCodeKey = AccountUtils.getAuthCodeKey(mobile, flag);
String cacheCode = cacheClient.get(authCodeKey); //从redis获取验证码
if (org.apache.commons.lang.StringUtils.isBlank(cacheCode)) {
throw new PicaException(PicaResultCode.RESULE_DATA_NONE.code(), "短信验证码已过期,请重新获取");
}
if (!org.apache.commons.lang.StringUtils.equals(sysCode, cacheCode)) {
throw new PicaException(PicaResultCode.PARAM_IS_INVALID.code(), "短信验证码错误");
}
cacheClient.del(authCodeKey);
}
/**
* 请求参数解密、反序列化
*
* @param params
* @param zClass
* @param <T>
* @return
*/
public static <T> T getRequestEntity(String params, Class<T> zClass) throws Exception {
String json = EncryptCreateUtil.dencrypt(params);
return JSONObject.parseObject(json, zClass);
}
/**
* 获取终端来源
*
* @param registerSource
* @return
*/
public static String getSourceType(Integer registerSource) {
String sourceType = null;
if (registerSource == 4) {
sourceType = "h5";
} else if (registerSource == 3) {
sourceType = "web";
} else if (registerSource == 5) {
sourceType = "admin";
} else {
sourceType = "app";
}
return sourceType;
}
/**
* 校验手机号是否注册过
*
* @param mobile
*/
// public boolean checkRegisterMobile(String mobile) {
// String encrypt = EncryptCreateUtil.encrypt(mobile);
// AccountContact accountContact = accountContactServer.selectByMobile(encrypt);
// return (accountContact != null && accountContact.getAcctId() != null);
// }
}
package com.pica.cloud.account.account.server.util;
import com.pica.cloud.account.account.server.entity.Account;
import com.pica.cloud.foundation.redis.ICacheClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
/**
* Token工具类
*/
@Component
public class TokenUtils {
@Autowired
@Qualifier("cacheMigrateClient")
private ICacheClient cacheClient;
/**
* 校验token的状态
*
* @param token
* @return
*/
public boolean checkTokenStatus(String token) {
String str = cacheClient.get("token-" + token);
return StringUtils.isBlank(str);
}
/**
* 生成随机token
*
* @param account
* @return
*/
public String generateToken(Account account) {
//判断用户终端类型
String sourceType = AccountUtils.getSourceType(account.getRegisterSource());
String newToken = StringUtils.EMPTY;
//用户唯一key
String tokenDoctorId = "token-doctor-" + account.getId().toString();
//清除旧token对应的用户id
String oldToken = cacheClient.get(tokenDoctorId + "-" + sourceType);
if (StringUtils.isNoneBlank(oldToken)) {
cacheClient.del(oldToken);
}
//生成新的token,并和用户唯一key绑定
newToken = UUID.randomUUID().toString().replace("-", "").toUpperCase();
int expiredSeconds = 30 * 24 * 60 * 60;
cacheClient.set("token-" + newToken, tokenDoctorId, expiredSeconds);
//存储当前登录终端对应的token
cacheClient.set(tokenDoctorId + "-" + sourceType, "token-" + newToken, expiredSeconds);
String userData = cacheClient.hget(tokenDoctorId, "id");
if (StringUtils.isEmpty(userData)) {
Map<String, String> map = new HashMap<>();
map.put("token", newToken);
map.put("id", account.getId() + "");
map.put("mobile", account.getMobilePhone());
map.put("name", account.getMobilePhone().replaceAll("(\\d{3})\\d{4}(\\w{4})", "$1****$2"));
map.put("created_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(account.getCreatTime()));
map.put("sysCode", sourceType);
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> mapData = iterator.next();
String key = mapData.getKey();
String valueInfo = mapData.getValue();
//存储token:(token-doctor-1:用户数据)
cacheClient.hset(tokenDoctorId, key, valueInfo);
}
}
return newToken;
}
}
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册