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;
}
}