package com.fuzamei.web; import java.util.LinkedHashMap; import java.util.Map; import java.util.UUID; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import com.fuzamei.constant.HintMSG; import com.fuzamei.entity.User; import com.fuzamei.entity.UserDetail; import com.fuzamei.service.LoginService; import com.fuzamei.util.JSONUtil; import com.fuzamei.util.RandomUtil; import com.fuzamei.util.ValidationUtil; @RestController public class LoginAction { @Autowired private LoginService loginService; /** * * @Title: loginUserName * @Description: TODO(用户登陆操作) { "username":"", "password":"" } * @param @return 设定文件 * @return Map 返回类型 * @author ylx * @date 2018年1月30日 下午3:57:05 * @throws */ @RequestMapping(path="/login",method=RequestMethod.POST) public Map loginUserName(@RequestBody User u){ try { String username = ValidationUtil.checkBlankAndAssignString(u.getUsername()); String password = ValidationUtil.checkBlankAndAssignString(u.getPassword()); String token = UUID.randomUUID().toString().replaceAll("-", ""); UserDetail admin = loginService.queryAdminExistence();//查询数据库是否有账号存在 if(admin==null){ UserDetail user=new UserDetail(); Long currentTime = System.currentTimeMillis(); user.setUserId(1);//管理员uId为1 user.setUsername(username); user.setPassword(password); user.setRoleName("管理员"); user.setRoleId(5); user.setPersonName("管理员");//管理员人名为【管理员】 user.setRandom(RandomUtil.getRandomString(10)); user.setCreateTime(currentTime); user.setUpdateTime(currentTime); user.setToken(token); user.setState(1); loginService.insertAdmin(user);//插入管理员数据(同时更新token) Map map=new LinkedHashMap(); map.put("username", user.getUsername()); map.put("roleName", user.getRoleName()); map.put("personName", user.getPersonName()); map.put("random", user.getRandom()); map.put("roleId", user.getRoleId()); map.put("userId", user.getUserId()); map.put("tokenId", "Bearer"+token+"&"+user.getUserId()); return JSONUtil.getJsonMap(200, true, HintMSG.LOGIN_SUCCESS, map); } User user = loginService.queryUser(username,password); if(user==null){ throw new RuntimeException("用户名或密码错误"); } // if(user.getState()==0){ // throw new RuntimeException("账号已停用,请联系管理员"); // } if(user.getToken()==null||"".equals(user.getToken())){ loginService.insertToken(user.getUserId(), token); } else { loginService.updateToken(user.getUserId(),token); } Map map=new LinkedHashMap(); map.put("username", user.getUsername()); map.put("roleName", user.getRoleName()); map.put("personName", user.getPersonName()); map.put("random", user.getRandom()); map.put("roleId", user.getRoleId()); map.put("userId", user.getUserId()); map.put("tokenId", "Bearer"+token+"&"+user.getUserId()); return JSONUtil.getJsonMap(200, true, HintMSG.LOGIN_SUCCESS, map); } catch (Exception e) { return JSONUtil.getJsonMap(300, false, HintMSG.LOGIN_FAIL+":"+e.getMessage(), null); } } }