JSONObject登录接口

service

/**
* 登录
*/
@Override
public Map<String,Object> login(String args,HttpServletRequest request) throws Exception{
//解析json格式,获取用户名和密码
JSONObject jsonObj=JSON.parseObject(args);
HashMap<String, Object> map = new HashMap<>();
String telephone = null;
String password = null;
try {
telephone = jsonObj.getString("Telephone");
password = jsonObj.getString("Password");
} catch (Exception e) {
map.put("Code", "201");
map.put("Message", "参数异常");
}
try {
//查询用户名是否存在
CloudUserExample example = new CloudUserExample();
example.createCriteria().andTelephoneEqualTo(telephone);
List<CloudUser> list = mapper.selectByExample(example);
if(list!=null && list.size()>0){
//用户存在,验证密码
if(list.get(0).getPassword()!=null && !list.get(0).getPassword().equals(password)){
//密码错误
map.put("Code", "204");
map.put("Message", "密码错误");
}else{
//登录成功
map.put("Code", "200");
map.put("Data",list.get(0));
map.put("Message", "返回成功");
//把用户信息保存在session中
HttpSession session = request.getSession();
session.setAttribute(Constant.user_session,list.get(0));
}
}else{
//用户不存在
map.put("Code", "203");
map.put("Message", "用户不存在");
}
} catch (Exception e) {
map.put("Code","202");
map.put("Message", "返回失败");
e.printStackTrace();
throw new MyException("系统出错!",e);
}
return map;
}

controller

@ResponseBody
public Map<String,Object> Login(String args,HttpServletRequest request) throws Exception{
return service.login(args,request);
}

只为记录自己所学过的东西

原文地址:https://www.cnblogs.com/Mingzz/p/8017765.html