LoginController.java 5.89 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
package com.lyms.hospital.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.lyms.annotation.TokenRequired;
import com.lyms.base.common.entity.organ.Organizations;
import com.lyms.base.common.entity.role.Permissions;
import com.lyms.base.common.entity.role.Roles;
import com.lyms.base.common.entity.user.Users;
import com.lyms.base.common.service.organ.OrganizationsService;
import com.lyms.base.common.service.role.PermissionsService;
import com.lyms.base.common.service.role.RolesService;
import com.lyms.base.common.service.user.UsersService;
import com.lyms.hospital.service.token.TokenService;
import com.lyms.util.InstanceUtils;
import com.lyms.util.MD5Utils;
import com.lyms.web.bean.AjaxResult;
import com.lyms.web.controller.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = "/login")
@Api(value = "/api", description = "登录的相关操作")
public class LoginController extends BaseController {
@Autowired
private UsersService usersService;
@Autowired
private OrganizationsService organizationsService;
@Autowired
private RolesService rolesService;
@Autowired
private TokenService tokenService;
@Autowired
private PermissionsService permissionsService;

@ApiOperation(value = "测试登录", notes = "测试登录说明")
@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User") })
@RequestMapping(value = "/testLogin", method = RequestMethod.GET)
public String testLogin() {
// request.getSession().setAttribute("abc", "123");
return "abc";
}
/**
* <li>@Description:测试@RequestBody
* <li>@param reqJson
* <li>@param users
* <li>@return
* <li>创建人:方承
* <li>创建时间:2017年3月28日
* <li>修改人:
* <li>修改时间:
*/
@RequestMapping(value = "/testPostJson", method = RequestMethod.POST)
@ResponseBody
public String testPostJson(@RequestBody String reqJson){
Users u = getRequestUsersEntity(reqJson);
System.out.println(JSON.toJSONString(u));
return "1";
}
/**
* <li>@Description:设置业务实体
* <li>@param reqJson
* <li>@param users
* <li>创建人:方承
* <li>创建时间:2017年3月28日
* <li>修改人:
* <li>修改时间:
*/
private Users getRequestUsersEntity(String reqJson){
//users.setId(reqJson.getString("id"));
return JSON.parseObject(reqJson, Users.class);
}
@RequestMapping(value = "/tokens")
@ApiOperation(value = "登录接口", notes = "登录接口")
@ApiImplicitParams({ @ApiImplicitParam(name = "account", value = "用户account", required = true, dataType = "String"),
@ApiImplicitParam(name = "password", value = "password", required = true, dataType = "String") })
@ResponseBody
public AjaxResult usersLogin(@RequestParam(value = "account") String account,
@RequestParam(value = "vercode", required = false) String code,
@RequestParam(value = "password", required = false) String password,
AjaxResult ajaxResult,
HttpServletResponse response) {
ajaxResult.setSuccess(false);
if (StringUtils.isEmpty(account) && (StringUtils.isEmpty(code) || StringUtils.isEmpty(password))) {
ajaxResult.setMessage("登录账户或者验证码为空,请输入!");
return ajaxResult;
}
Users users = usersService.getUserByUsername(account);
if(users == null){
ajaxResult.setMessage("用户不存在!");
return ajaxResult;
}
if(!users.getPwd().equals(MD5Utils.md5(password))){
ajaxResult.setMessage("密码不正确!");
return ajaxResult;
}
if(users.getEnable() < 1){
ajaxResult.setMessage("用户被禁用!");
return ajaxResult;
}
//Organizations organizations = organizationsService.selectById( users.getOrgId());
Map<String, Object> result = InstanceUtils.newHashMap();
String token = tokenService.createToken(users);
List<Roles> roles = rolesService.selectBatchIds(usersService.getRoleIdListByUserid(users.getId()));
List<Permissions> permissions = permissionsService.getUserPermission(users.getId());
Organizations org = organizationsService.selectById(users.getOrgId());
result.put("token", token);
result.put("user",users);
result.put("roles",roles);
result.put("organization",org);
result.put("organizations",org);
result.put("permissions", permissions);
result.put("watermark", "water");
ajaxResult.setData(result);
ajaxResult.setSuccess(true);
return ajaxResult;
}
@RequestMapping(value = "/tokensCheck", method = RequestMethod.POST)
@ResponseBody
@TokenRequired
public AjaxResult usersLogin(
HttpServletResponse response) {
System.out.println(1111111);
return null;
}
}