第一次JSON返回的登陆模式

时间:2021-08-27 16:11:40

/**
* 跳转到登录页
*
* @param model
* @return
*/
@RequestMapping(value = {"/login2"},method = { RequestMethod.POST, RequestMethod.GET })
@ResponseBody
public JSONObject login1(Model model, HttpServletRequest request,
HttpServletResponse response,
@RequestParam(value = "account", required = false) String account,
@RequestParam(value = "password", required = false) String password){
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Content-Type", "application/json;charset=utf-8");
StringBuilder str=new StringBuilder();//定义变长字符串
Random random=new Random();
//随机生成数字,并添加到字符串
for(int i=0;i<8;i++){
str.append(random.nextInt(10));
}
//将字符串转换为数字并输出
int num=Integer.parseInt(str.toString());
String account1 = "yy";
String password1 ="1";
JSONObject json = new JSONObject();
json.put("msg", "登陆成功");
json.put("success", true);

JSONObject obj=new JSONObject();
obj.put("userID", 1);
obj.put("userName", "名字");
obj.put("account", account);
obj.put("userType", "1/2");
obj.put("token", num);
json.put("obj", obj);

JSONObject json1 = new JSONObject();
json1.put("msg", "登陆失败/账号不存在");
json1.put("success", false);

if(account.equals(account1) && password.equals(password1)){
System.out.println(json);
return json;
}else{
return json1;
}
}