|
@ -1,17 +1,28 @@
|
|
|
package com.yihu.jw.healthyhouse.controller;
|
|
|
|
|
|
import com.yihu.jw.exception.business.ManageException;
|
|
|
import com.yihu.jw.healthyhouse.cache.WlyyRedisVerifyCodeService;
|
|
|
import com.yihu.jw.healthyhouse.model.user.User;
|
|
|
import com.yihu.jw.healthyhouse.service.user.LoginService;
|
|
|
import com.yihu.jw.restmodel.web.Envelop;
|
|
|
import com.yihu.jw.restmodel.web.ObjEnvelop;
|
|
|
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import io.swagger.annotations.ApiParam;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
import org.springframework.http.*;
|
|
|
import org.springframework.security.oauth2.common.exceptions.InvalidRequestException;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
@ -19,34 +30,108 @@ import java.util.Map;
|
|
|
* @author HZY
|
|
|
* @created 2018/9/18 19:55
|
|
|
*/
|
|
|
@RestController
|
|
|
public class LoginController extends EnvelopRestEndpoint {
|
|
|
|
|
|
@Autowired
|
|
|
private LoginService loginService;
|
|
|
@Autowired
|
|
|
private RestTemplate restTemplate;
|
|
|
private String clientId;
|
|
|
@Autowired
|
|
|
private WlyyRedisVerifyCodeService wlyyRedisVerifyCodeService;
|
|
|
|
|
|
/**
|
|
|
* 获取验证码
|
|
|
* @param parameters
|
|
|
* @return
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
@GetMapping(value = "/login/captcha")
|
|
|
public ResponseEntity<HashMap> captcha(@RequestParam Map<String, String> parameters) throws Exception{
|
|
|
String client_id = parameters.get("client_id");
|
|
|
String username = parameters.get("username");
|
|
|
if (StringUtils.isEmpty(client_id)) {
|
|
|
throw new InvalidRequestException("client_id");
|
|
|
}
|
|
|
if (StringUtils.isEmpty(username)){
|
|
|
throw new InvalidRequestException("username");
|
|
|
}
|
|
|
//验证请求间隔超时,防止频繁获取验证码
|
|
|
if (!wlyyRedisVerifyCodeService.isIntervalTimeout(client_id, username)) {
|
|
|
throw new IllegalAccessException("SMS request frequency is too fast");
|
|
|
}
|
|
|
//发送短信获取验证码
|
|
|
HttpHeaders reqHeaders = new HttpHeaders();
|
|
|
reqHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
|
|
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
|
|
|
params.add("clientId", client_id);
|
|
|
params.add("type", "login");
|
|
|
params.add("to", username);
|
|
|
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, reqHeaders);
|
|
|
HashMap<String, Object> result = restTemplate.postForObject("http://svr-base:10020/sms_gateway/send", httpEntity, HashMap.class);
|
|
|
if (200 == (Integer) result.get("status")){
|
|
|
Map<String, Object> sms = (Map)result.get("obj");
|
|
|
String captcha = (String) sms.get("captcha");
|
|
|
Date deadline = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String) sms.get("deadline"));
|
|
|
Long expire = (deadline.getTime() - System.currentTimeMillis()) / 1000;
|
|
|
wlyyRedisVerifyCodeService.store(client_id, username, captcha, expire.intValue());
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.set("Cache-Control", "no-store");
|
|
|
headers.set("Pragma", "no-cache");
|
|
|
return new ResponseEntity<>(result, headers, HttpStatus.OK);
|
|
|
}
|
|
|
throw new IllegalStateException((String) result.get("message"));
|
|
|
}
|
|
|
|
|
|
@GetMapping("/login")
|
|
|
@ApiOperation(value = "登陆")
|
|
|
public Envelop login(
|
|
|
@GetMapping("/mobile/login")
|
|
|
@ApiOperation(value = "手机登录注册")
|
|
|
public ObjEnvelop register(
|
|
|
HttpServletRequest request,
|
|
|
@ApiParam(name = "username", value = "账号", required = true)@RequestParam(required = true, name = "username") String username,
|
|
|
@ApiParam(name = "password", value = "密码", required = true)@RequestParam(required = true, name = "password") String password) throws ManageException {
|
|
|
User data = loginService.login(request,username, password);
|
|
|
@ApiParam(name = "username", value = "短信验证码", required = true)@RequestParam(required = true, name = "captcha") String captcha) throws ManageException, ParseException {
|
|
|
if (wlyyRedisVerifyCodeService.verification(clientId, username, captcha)) {
|
|
|
User user = loginService.phoneLogin(request,username);
|
|
|
ObjEnvelop envelop = new ObjEnvelop();
|
|
|
envelop.setStatus(200);
|
|
|
envelop.setMessage("登录成功");
|
|
|
envelop.setObj(user);
|
|
|
return envelop;
|
|
|
|
|
|
return Envelop.getSuccess("登陆成功");
|
|
|
} else {
|
|
|
return ObjEnvelop.getError("验证码错误");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@PostMapping("/ijk/login")
|
|
|
@ApiOperation(value = "i健康用户登陆")
|
|
|
public ObjEnvelop ijkLogin(
|
|
|
HttpServletRequest request,
|
|
|
@ApiParam(name = "username", value = "账号", required = true)@RequestParam(required = true, name = "username") String username,
|
|
|
@ApiParam(name = "password", value = "密码", required = true)@RequestParam(required = true, name = "password") String password) throws ManageException {
|
|
|
User user = loginService.iJklogin(request,username, password);
|
|
|
if (user !=null) {
|
|
|
ObjEnvelop envelop = new ObjEnvelop();
|
|
|
envelop.setStatus(200);
|
|
|
envelop.setMessage("登录成功");
|
|
|
envelop.setObj(user);
|
|
|
return envelop;
|
|
|
}else {
|
|
|
return ObjEnvelop.getError("登录失败");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@GetMapping("/loginout")
|
|
|
@ApiOperation(value = "退出")
|
|
|
@PostMapping("/loginout")
|
|
|
@ApiOperation(value = "登出")
|
|
|
public Envelop loginout(
|
|
|
HttpServletRequest request,
|
|
|
@ApiParam(name = "userCode", value = "用户code", required = true)@RequestParam(required = true, name = "userCode") String userCode) {
|
|
|
try {
|
|
|
//修改用户状态 离线
|
|
|
return Envelop.getSuccess("登出成功");
|
|
|
return ObjEnvelop.getSuccess("登出成功");
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
return Envelop.getError("登出成功:" + e.getMessage(), -1);
|
|
|
return ObjEnvelop.getError("登出成功:" + e.getMessage(), -1);
|
|
|
}
|
|
|
}
|
|
|
|