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.

141 lines
6.3 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;
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
// @RequestMapping("/")
// public void a(HttpServletRequest request, HttpServletResponse response) throws Exception {
// //消息来源可靠性验证
// String signature = request.getParameter("signature");// 微信加密签名
// String timestamp = request.getParameter("timestamp");// 时间戳
// String nonce = request.getParameter("nonce"); // 随机数
// String echostr = request.getParameter("echostr");//成为开发者验证
// //确认此次GET请求来自微信服务器原样返回echostr参数内容则接入生效成为开发者成功否则接入失败
// System.out.println();
// response.getWriter().println(request.getParameter("echostr"));
// }
//
//
// public void post(HttpServletRequest request, HttpServletResponse response) throws Exception {
// // TODO 消息的接收、处理、响应
// }
//
//
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) {
boolean login = loginService.login(username, password);
if (login) {
Cookie cookie = new Cookie("username", username);
cookie.setMaxAge(60 * 60);
cookie.setPath("/");
response.addCookie(cookie);
String sign = JwtUtils.sign(username, password,"pcUser");
return ResultBuilder.withPayload(sign).build();
} else {
return ResultBuilder.error("登录失败").build();
}
}
6 years ago
//
// @PostMapping("/getOpenid")
// public Result getOpenid(String code) {
//
// JSONObject token = loginService.getToken(code);
// String sign = JwtUtils.sign(token.getString("openid"), token.getString("openid"));
// return ResultBuilder.withPayload(sign).build();
// }
//
// @PostMapping("/getUser")
// @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "header")
// public Result getUser(HttpServletRequest request){
// String token = request.getHeader("token");
// String username = JwtUtils.getUsername(token);
// WxUser user = loginService.getUser(username);
// return ResultBuilder.withPayload(user).build();
// }
}