Java之~ 登录,验证码发送,修改密码,头像更新

1.登录需要用户名,密码

设置为字符串类型的,一般为String userName ,String password.

2.获取token,返回token

3.

//判断token是否有效
@RequestMapping(value = "getUserId",method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> getUserId( String token) throws JsonProcessingException{
Integer userId =UserTokenCache.getTokenCache().get(token);
if(userId ==null){
return ResultMap.sendErrorCode(ResultMap.tokenError);
}else{
return Ajax.buildSuccessResult();
}
}
// 1。用户登录
@RequestMapping(value = "userlogin",method = RequestMethod.POST)  // text/html;charset=utf-8
@ResponseBody
public Map<String, Object> login(String name,String password) throws JsonProcessingException{

Map<String, Object> result =new  HashMap<String, Object>();
User user = userService.login(name,password);
if(user!=null){
int token = RandomUtil.getRandomNum(200000, 100000);
System.out.println("token:"+token);
result.put("token", token);//用户其他操作使用的token  放到header里面 发给服务器
result.put("user", user);
UserTokenCache.loginDealCache(String.valueOf(token), Integer.parseInt(user.getId().toString()));

return Ajax.getSuccessReturnMapWithData(result);
}else{
return Ajax.buildErrorResult("用户名或者密码不正确!");
}
}
//修改密码
@RequestMapping(value = "updatePassword",method = RequestMethod.POST)  // text/html;charset=utf-8
@ResponseBody
public Map<String, Object> updatePassword(String token ,String password,String newPassword) throws JsonProcessingException{
Integer userId =UserTokenCache.getTokenCache().get(token);
if(userId ==null){
return ResultMap.sendErrorCode(ResultMap.tokenError);
}
if(password!=null || password!=""){
User user = userService.updatePassword(userId,password);
if(user!=null){
user.setPassword(newPassword);
userService.update(user);
return Ajax.buildSuccessResult();
}
}
return ResultMap.resultErrorCode(ResultMap.passwordError);

}

//验证码缓存工具类(用于缓存二维码)

public class ValidCodeCache {
    public static HashMap<String, String> validCodeCache = new HashMap(2000);

    public static HashMap<String, String> getValidCodeCache() {
        return validCodeCache;
    }

    public static void setValidCodeCache(HashMap<String, String> validCodeCache) {
        ValidCodeCache.validCodeCache = validCodeCache;
    }

    public static void validCode(String phone,String  validCode) {
        getValidCodeCache().put(phone, validCode);
    }
    public static String getValidCode(String phone) {
        return getValidCodeCache().get(phone) == null ? "":(String)getValidCodeCache().get(phone);
    }
}

// 发送验证码
@RequestMapping(value = "validCode", method = RequestMethod.POST) // text/html;charset=utf-8
@ResponseBody
public Map<String, Object> validCode(@RequestBody Fsuser user) throws JsonProcessingException {
if (user.getUsername() != null && user.getUsername() != "") {
// 生成随机6位数
Random random = new Random();
String msgContent = "";
for (int i = 0; i < 6; i++) {
msgContent += random.nextInt(10);
}
// 返回验证码
System.out.println("发送验证>>>" + msgContent);
HttpDemo hd = new HttpDemo();//(此处工具类方法调用https://blog.csdn.net/haopingping_88/article/details/79991888
// 短信发送
String restr = hd.sendSmsByHttpClient(user.getUsername(), "您本次验证码为" + msgContent + "。请妥善保管!");

// {"stat":100,"message":"发送成功"}

{  //Gson gson = new Gson();//gson 解析

 

//MessageBean mb = gson.fromJson(restr, MessageBean.class);
//int code = mb.getStat();
//String massage = mb.getMessage();
//Map<String, Object> result = ResultMap.sendMessageCode(code, massage);
//if (code == 100) {
//  result.put("data", msgContent);

 

//}

                        }这是解析的短信返回的数据。不需要的不用返回

return result;
}

ValidCodeCache.validCode(bean.getPhone(), validCode);//将验证码放进缓存中
return Ajax.buildExceptionResult();
}

 

// 用户头像更新
(1.)@RequestMapping(value = "updateHeadimg", method = RequestMethod.POST) // text/html;charset=utf-8
@ResponseBody
public Map<String, Object> updateHeadimg(HttpServletRequest request, HttpServletResponse response)
throws JsonProcessingException {
Map<String, Object> map = new HashMap<String, Object>();
try {
map = FileUploadUtil.saveCapture(request, response, FileUploadUtil.host, FileUploadUtil.rootPath);
} catch (Exception e) {
e.printStackTrace();
}
return fsuserService.updateHeadimg(map);

}

@Override
(2.) public Map<String, Object> updateHeadimg(Map<String, Object> map) {
Map<String, Object> m = new HashMap<String, Object>();
if(map.containsKey("param")){
m = (Map<String, Object>)map.get("param");
}else
return ResultMap.sendErrorCode(ResultMap.emptyError);
Integer userId;
if(m.containsKey("token")){
userId = UserTokenCache.getTokenCache().get(m.get("token").toString());
if (userId == null)
return ResultMap.sendErrorCode(ResultMap.tokenError);
}else
return ResultMap.sendErrorCode(ResultMap.emptyError);

if(map.containsKey("images")){
List<String> images = (List<String>)map.get("images");
Fsuser u = fsuserDao.findOne(userId);
for(String i :images){
u.setHeadimage(i);
fsuserDao.update(u);
}
return Ajax.buildSuccessResult();
}
return Ajax.buildExceptionResult();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值