JSONObject登陸接口

servicejson

/**
* 登陸
*/
@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;
}session

 

controllerapp

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

 

只爲記錄本身所學過的東西spa

相關文章
相關標籤/搜索