You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
7.1 KiB
Java

6 years ago
package com.gszc.controller;
6 years ago
6 years ago
import com.alibaba.fastjson.JSONObject;
import com.gszc.build.Result;
import com.gszc.build.ResultBuilder;
import com.gszc.entity.MiniUser;
6 years ago
import com.gszc.entity.PcUser;
6 years ago
import com.gszc.service.LoginService;
import com.gszc.service.MiniAppService;
import com.gszc.util.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
6 years ago
6 years ago
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
@Api(value = "工商注册 API", tags = {"登录api"})
6 years ago
@RestController
6 years ago
@RequestMapping("/login")
6 years ago
public class LoginController {
6 years ago
@Autowired
LoginService loginService;
@Autowired
private MiniAppService miniAppService;
@PostMapping("/decryptUserInfo")
@ApiOperation(value = "解密用户信息", notes = "解密用户信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "userId", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "encryptedData", value = "加密数据", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "ivStr", value = "iv", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "signature", value = "签名", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "rawData", value = "原始数据", dataType = "string", paramType = "query"),
})
public Result decryptUserInfo(String userId,String encryptedData ,String ivStr,String signature,String rawData){
MiniUser miniUser = miniAppService.decryptUserInfo(userId, encryptedData, ivStr, signature, rawData);
return ResultBuilder.withPayload(miniUser).build();
}
@PostMapping("/isNewUserLogin")
@ApiOperation(value = "是否新用户", notes = "是否新用户")
@ApiImplicitParams({
@ApiImplicitParam(name = "code", value = "code", dataType = "string", paramType = "query"),
})
public Result isNewUserLogin(String code,HttpServletResponse response){
JSONObject result = miniAppService.isNewUser(code);
Cookie cookie = new Cookie("username", result.getString("openId"));
cookie.setMaxAge(60 * 60);
cookie.setPath("/");
response.addCookie(cookie);
String sign = JwtUtils.sign(result.getString("openId"), result.getString("openId"),"miniUser");
result.put("token",sign);
result.remove("openId");
return ResultBuilder.withPayload(result).build();
}
@ApiOperation(value = "获得手机号码", notes = "获得手机号码")
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", value = "userId", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "encryptedData", value = "加密数据", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "ivStr", value = "iv", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "signature", value = "签名", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "rawData", value = "原始数据", dataType = "string", paramType = "query"),
})
@PostMapping("/decryptPhoneNumber")
public Result decryptPhoneNumber(String userId,String encryptedData ,String ivStr,String signature,String rawData){
MiniUser miniUser = miniAppService.decryptPhoneNumber(userId, encryptedData, ivStr, signature, rawData);
return ResultBuilder.withPayload(miniUser).build();
}
6 years ago
/**
*
*
* @return
*/
@PostMapping("/minitestlogin")
@ApiImplicitParams({
@ApiImplicitParam(name = "openid", value = "openid", dataType = "string", paramType = "query"),
})
public Result minitestlogin(String openid, HttpServletResponse response) {
boolean login = true;
if (login) {
Cookie cookie = new Cookie("username", openid);
cookie.setMaxAge(60 * 60);
cookie.setPath("/");
response.addCookie(cookie);
String sign = JwtUtils.sign(openid, openid,"miniUser");
return ResultBuilder.withPayload(sign).build();
} else {
return ResultBuilder.error("登录失败").build();
}
}
6 years ago
/**
* pc
*
* @param username
* @param password
* @return
*/
@PostMapping("/login")
@ApiImplicitParams({
@ApiImplicitParam(name = "username", value = "用户名", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "password", value = "密码", dataType = "string", paramType = "query")
})
public Result login(String username, String password, HttpServletResponse response) {
6 years ago
PcUser pcUser = loginService.login(username, password);
if (null!=pcUser) {
6 years ago
Cookie cookie = new Cookie("username", username);
cookie.setMaxAge(60 * 60);
cookie.setPath("/");
response.addCookie(cookie);
String sign = JwtUtils.sign(username, password,"pcUser");
6 years ago
JSONObject result = new JSONObject();
result.put("token",sign);
pcUser.setPassword(null);
result.put("user",pcUser);
return ResultBuilder.withPayload(result).build();
} else {
return ResultBuilder.error("登录失败").build();
}
}
/**
* pc
*
* @return
*/
@PostMapping("/sendCode")
@ApiImplicitParams({
@ApiImplicitParam(name = "phone", value = "手机号码", dataType = "string", paramType = "query"),
})
public Result sendCode(String phone) {
boolean sendCode = loginService.sendCode(phone);
if (sendCode) {
return ResultBuilder.success().build();
} else {
return ResultBuilder.error("登录失败").build();
}
}
/**
* pc
*
* @return
*/
@PostMapping("/phoneLogin")
@ApiImplicitParams({
@ApiImplicitParam(name = "phone", value = "手机号码", dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "code", value = "验证码", dataType = "string", paramType = "query")
})
public Result phoneLogin(String phone, String code, HttpServletResponse response) {
PcUser pcUser = loginService.phoneLogin(phone, code);
if (null!=pcUser) {
Cookie cookie = new Cookie("username", pcUser.getLoginName());
cookie.setMaxAge(60 * 60);
cookie.setPath("/");
response.addCookie(cookie);
String sign = JwtUtils.sign(pcUser.getLoginName(), pcUser.getPassword(),"pcUser");
6 years ago
return ResultBuilder.withPayload(sign).build();
} else {
return ResultBuilder.error("登录失败").build();
}
}
6 years ago
}