重构项目
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package cn.lailab.toolbox.controller;
|
||||
|
||||
import cn.lailab.toolbox.beans.domain.User;
|
||||
import cn.lailab.toolbox.service.impl.AuthServiceImpl;
|
||||
import cn.lailab.toolbox.service.impl.ResultServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
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.RestController;
|
||||
|
||||
|
||||
@RestController
|
||||
public class AuthController {
|
||||
|
||||
@Autowired
|
||||
AuthServiceImpl authService;
|
||||
|
||||
@Autowired
|
||||
ResultServiceImpl resultService;
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(AuthController.class);
|
||||
|
||||
@RequestMapping("/api/auth/getAuthCode")
|
||||
public String getAuthCode(@RequestBody User user) {
|
||||
String rst;
|
||||
logger.info("调用方法getAuthCode()开始 入参:{}", user);
|
||||
String code = authService.getAuthCode(user);
|
||||
if (code == null|| code.isEmpty()) {
|
||||
rst = resultService.error("获取授权码失败,请确认登录信息无误!","");
|
||||
}else{
|
||||
rst = resultService.success("获取授权码成功!",code);
|
||||
}
|
||||
logger.info("调用方法getAuthCode()结束 返回:{}", code);
|
||||
return rst;
|
||||
}
|
||||
|
||||
@RequestMapping("/api/auth/getAuthMd5Code")
|
||||
public String getAuthMd5Code(@RequestBody User user) {
|
||||
String rst;
|
||||
logger.info("调用方法getAuthMd5Code()开始 入参:{}", user);
|
||||
String code = authService.getAuthMd5Code(user);
|
||||
if (code == null|| code.isEmpty()) {
|
||||
rst = resultService.error("获取授权码失败,请确认登录信息无误!","");
|
||||
}else{
|
||||
rst = resultService.success("获取授权码成功!",code);
|
||||
}
|
||||
logger.info("调用方法getAuthMd5Code()结束 返回:{}", code);
|
||||
return rst;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package cn.lailab.toolbox.controller;
|
||||
|
||||
import cn.lailab.toolbox.beans.domain.Parameter;
|
||||
import cn.lailab.toolbox.service.ParameterService;
|
||||
import cn.lailab.toolbox.service.ResultService;
|
||||
import cn.lailab.toolbox.service.impl.AuthServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class ParameterController {
|
||||
|
||||
@Autowired
|
||||
private ParameterService parameterService;
|
||||
|
||||
@Autowired
|
||||
private AuthServiceImpl authService;
|
||||
|
||||
@Autowired
|
||||
private ResultService resultService;
|
||||
|
||||
@Value("${lailab.default-authCode}")
|
||||
String defaultAuthCode;
|
||||
private final Logger logger = LoggerFactory.getLogger(UserController.class);
|
||||
|
||||
|
||||
@RequestMapping("/api/parameter/getAllParameter")
|
||||
public String getAllParameter(@RequestBody Parameter parameter) {
|
||||
logger.info("调用方法getAllParameter()开始 入参:{}", parameter);
|
||||
String rst;
|
||||
if(this.checkAuth(parameter.getAuthCode())){
|
||||
List<Parameter> parameters = parameterService.getAllParameter(parameter);
|
||||
if(!parameters.isEmpty()) {
|
||||
rst = resultService.success("获取所有参数信息成功!", parameters);
|
||||
} else {
|
||||
rst = resultService.success("获取所有参数信息失败!", "");
|
||||
}
|
||||
}else{
|
||||
rst = resultService.other(-2,"授权信息有误或已过期,请更换后重新尝试!","");
|
||||
}
|
||||
logger.info("调用方法getAllParameter()结束 返回:{}", rst);
|
||||
return rst;
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/api/parameter/getParameterByName")
|
||||
@ResponseBody
|
||||
public String getParameterByName(@RequestBody Parameter parameter){
|
||||
logger.info("调用方法getParameterByName()开始 入参:{}", parameter);
|
||||
String parameterName = parameter.getParameterName();
|
||||
String rst;
|
||||
if(parameterName == null|| parameterName.isEmpty()){
|
||||
rst = resultService.error("参数parameterName不存在,请检查入参!");
|
||||
}else {
|
||||
if(this.checkAuth(parameter.getAuthCode())){
|
||||
Parameter rstParameter = parameterService.getParameterByName(parameter);
|
||||
if (rstParameter == null) {
|
||||
rst = resultService.error("未获取参数信息!", "");
|
||||
} else {
|
||||
rst = resultService.success("获取参数信息成功!", rstParameter);
|
||||
}
|
||||
}else{
|
||||
rst = resultService.other(-2,"授权信息有误或已过期,请更换后重新尝试!","");
|
||||
}
|
||||
}
|
||||
logger.info("调用方法getParameterByName()结束 返回:{}", rst);
|
||||
return rst;
|
||||
}
|
||||
|
||||
public boolean checkAuth(String auth_code){
|
||||
logger.info("调用方法checkAuth()开始 入参:{}",auth_code);
|
||||
String auth_new = authService.getAuthMd5CodeInside();
|
||||
logger.info("最新授权码 {}",auth_new);
|
||||
authService.addUseCount(auth_new);
|
||||
logger.info("授权码 {} 使用次数加1",auth_new);
|
||||
boolean is_match = auth_new.equals(auth_code)||defaultAuthCode.equals(auth_code);
|
||||
logger.info("调用方法checkAuth()结束 返回:{}",is_match);
|
||||
return is_match;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package cn.lailab.toolbox.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Controller
|
||||
public class ToolController {
|
||||
|
||||
@RequestMapping("/JiSuanQi")
|
||||
public String JiSuanQi(){
|
||||
return "tools/JiSuanQi.html";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
package cn.lailab.toolbox.controller;
|
||||
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.lailab.toolbox.beans.domain.User;
|
||||
import cn.lailab.toolbox.service.impl.AuthServiceImpl;
|
||||
import cn.lailab.toolbox.service.impl.ResultServiceImpl;
|
||||
import cn.lailab.toolbox.service.impl.UserServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
UserServiceImpl userService ;
|
||||
|
||||
@Autowired
|
||||
ResultServiceImpl resultService;
|
||||
|
||||
@Autowired
|
||||
AuthServiceImpl authService;
|
||||
@Value("${lailab.default-authCode}")
|
||||
String defaultAuthCode;
|
||||
private final Logger logger = LoggerFactory.getLogger(UserController.class);
|
||||
|
||||
@RequestMapping("/api/user/getAllUser")
|
||||
@ResponseBody
|
||||
public String getAllUser(@RequestBody User user) {
|
||||
String rst;
|
||||
logger.info("调用方法getAllUser()开始 入参:{}", user);
|
||||
if(this.checkAuth(user.getAuthCode())){
|
||||
List<User> users = userService.getAllUser(user);
|
||||
if(!users.isEmpty()) {
|
||||
rst = resultService.success("获取所有用户信息成功!", users);
|
||||
}else{
|
||||
rst = resultService.error("获取所有用户信息失败!", "");
|
||||
}
|
||||
}else{
|
||||
rst = resultService.other(-2,"授权信息有误或已过期,请更换后重新尝试!","");
|
||||
}
|
||||
logger.info("调用方法getAllUser()结束 返回:{}", rst);
|
||||
return rst;
|
||||
}
|
||||
|
||||
@RequestMapping("/api/user/getUserByOpenId")
|
||||
@ResponseBody
|
||||
public String getUserByOpenId(@RequestBody User user){
|
||||
logger.info("调用方法getUserByOpenId()开始 入参:{}", user);
|
||||
String openId = user.getOpenId();
|
||||
String rst;
|
||||
if(openId == null|| openId.isEmpty()){
|
||||
rst = resultService.error("参数code不存在,请检查入参!");
|
||||
}else {
|
||||
if(this.checkAuth(user.getAuthCode())){
|
||||
User rstUser = userService.getUserByOpenId(user);
|
||||
if (rstUser == null) {
|
||||
rst = resultService.error("未获取用户信息!", "");
|
||||
} else {
|
||||
rst = resultService.success("获取用户信息成功!", rstUser);
|
||||
}
|
||||
}else{
|
||||
rst = resultService.other(-2,"授权信息有误或已过期,请更换后重新尝试!","");
|
||||
}
|
||||
}
|
||||
logger.info("调用方法getUserByOpenId()结束 返回:{}", rst);
|
||||
return rst;
|
||||
}
|
||||
|
||||
@RequestMapping("/api/user/addUser")
|
||||
public String addUser(@RequestBody User user){
|
||||
logger.info("调用方法addUser()开始 入参:{}",user);
|
||||
logger.info("1.检测用户是否存在(addUser)");
|
||||
int exist = userService.userExist(user);
|
||||
String rst;
|
||||
if(exist>0) {
|
||||
logger.info("2.用户已存在,不能新建用户(addUser)");
|
||||
rst = resultService.other(-2,"用户已存在!","");
|
||||
}else{
|
||||
logger.info("2.用户不存在,新建用户(addUser)");
|
||||
int code = userService.addUser(user);
|
||||
if (code > 0) {
|
||||
rst = resultService.success("添加用户信息成功!");
|
||||
} else {
|
||||
rst = resultService.error("添加用户信息失败!");
|
||||
}
|
||||
logger.info("调用方法addUser()结束 返回:{}", rst);
|
||||
}
|
||||
return rst;
|
||||
}
|
||||
|
||||
@RequestMapping("/api/user/login")
|
||||
public String login(@RequestBody User user){
|
||||
logger.info("调用方法login()开始 入参:{}",user);
|
||||
String rst;
|
||||
if(this.checkAuth(user.getAuthCode())) {
|
||||
logger.info("1.检测用户是否存在(login)");
|
||||
int exist = userService.userExist(user);
|
||||
if (exist > 0) {
|
||||
// 1.获取用户输入密码
|
||||
String str_pwd = user.getPassword();
|
||||
// 2.将密码MD5加密
|
||||
str_pwd = SecureUtil.md5(str_pwd);
|
||||
// 3.转换为大写字符
|
||||
str_pwd = str_pwd.toUpperCase();
|
||||
user.setPassword(str_pwd);
|
||||
logger.info("2.用户存在,校验密码(login)");
|
||||
User rstUser = userService.login(user);
|
||||
if (rstUser == null) {
|
||||
rst = resultService.error("登录失败,请检查密码是否正确!");
|
||||
} else {
|
||||
rst = resultService.success("登录成功!", rstUser);
|
||||
}
|
||||
} else {
|
||||
logger.info("2.用户不存在,需要注册(login)");
|
||||
rst = resultService.other(-2, "用户不存在,请进行注册!", "");
|
||||
}
|
||||
}else{
|
||||
rst = resultService.other(-2,"授权信息有误或已过期,请更换后重新尝试!","");
|
||||
}
|
||||
logger.info("调用方法login()结束 返回:{}", rst);
|
||||
return rst;
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping("/api/user/loginByOpenId")
|
||||
public String loginByOpenId(@RequestBody User user){
|
||||
logger.info("loginByOpenId()开始 入参:{}",user);
|
||||
if (user.getOpenId() == null || user.getOpenId().trim().isEmpty()) {
|
||||
logger.info("参数OpenId不存在,请检查入参!");
|
||||
return resultService.error("参数OpenId不存在,请检查入参!");
|
||||
}
|
||||
String rst;
|
||||
if(this.checkAuth(user.getAuthCode())) {
|
||||
User rstUser = userService.loginByOpenId(user);
|
||||
if (rstUser == null) {
|
||||
rst = resultService.error("登录失败!");
|
||||
} else {
|
||||
rst = resultService.success("登录成功!", rstUser);
|
||||
}
|
||||
}else{
|
||||
rst = resultService.other(-2,"授权信息有误或已过期,请更换后重新尝试!","");
|
||||
}
|
||||
logger.info("调用方法loginByOpenId()结束 返回:{}", rst);
|
||||
return rst;
|
||||
}
|
||||
|
||||
@RequestMapping("/api/user/updateUser")
|
||||
public String updateUser(@RequestBody User user){
|
||||
logger.info("调用方法updateUser()开始 入参:{}",user);
|
||||
String rst;
|
||||
if(this.checkAuth(user.getAuthCode())) {
|
||||
int code = userService.updateUser(user);
|
||||
if (code > 0) {
|
||||
rst = resultService.success("修改用户信息成功!");
|
||||
} else {
|
||||
rst = resultService.error("修改用户信息失败!");
|
||||
}
|
||||
}else{
|
||||
rst = resultService.other(-2,"授权信息有误或已过期,请更换后重新尝试!","");
|
||||
}
|
||||
logger.info("调用方法updateUser()结束 返回:{}", rst);
|
||||
return rst;
|
||||
}
|
||||
|
||||
@RequestMapping("/api/user/deleteUser")
|
||||
public String deleteUser(@RequestBody User user){
|
||||
logger.info("调用方法deleteUser()开始 入参:{}",user);
|
||||
String rst;
|
||||
if(this.checkAuth(user.getAuthCode())) {
|
||||
logger.info("1.检测用户是否存在(deleteUser)");
|
||||
int exist = userService.userExist(user);
|
||||
if (exist > 0) {
|
||||
logger.info("2.用户存在(deleteUser)");
|
||||
int code = userService.deleteUser(user);
|
||||
if (code > 0) {
|
||||
rst = resultService.success("删除用户成功!");
|
||||
} else {
|
||||
rst = resultService.error("删除用户失败!");
|
||||
}
|
||||
} else {
|
||||
logger.info("2.用户不存在(deleteUser)");
|
||||
rst = resultService.error("用户不存在!");
|
||||
}
|
||||
}else{
|
||||
rst = resultService.other(-2,"授权信息有误或已过期,请更换后重新尝试!","");
|
||||
}
|
||||
logger.info("调用方法deleteUser()结束 返回:{}",rst);
|
||||
return rst;
|
||||
}
|
||||
|
||||
@RequestMapping("/api/user/deleteUserByOpenId")
|
||||
public String deleteUserByOpenId(@RequestBody User user){
|
||||
logger.info("调用方法deleteUserByOpenId()开始 入参:{}",user);
|
||||
String rst;
|
||||
if(this.checkAuth(user.getAuthCode())) {
|
||||
int code = userService.deleteUserByOpenId(user);
|
||||
if (code > 0) {
|
||||
rst = resultService.success("删除用户成功!");
|
||||
} else {
|
||||
rst = resultService.error("删除用户失败!");
|
||||
}
|
||||
}else{
|
||||
rst = resultService.other(-2,"授权信息有误或已过期,请更换后重新尝试!","");
|
||||
}
|
||||
logger.info("调用方法deleteUserByOpenId()结束 返回:{}",rst);
|
||||
return rst;
|
||||
}
|
||||
|
||||
public boolean checkAuth(String auth_code){
|
||||
logger.info("调用方法checkAuth()开始 入参:{}",auth_code);
|
||||
String auth_new = authService.getAuthMd5CodeInside();
|
||||
logger.info("最新授权码 {}",auth_new);
|
||||
authService.addUseCount(auth_new);
|
||||
logger.info("授权码 {} 使用次数加1",auth_new);
|
||||
boolean is_match = auth_new.equals(auth_code)||defaultAuthCode.equals(auth_code);
|
||||
logger.info("调用方法checkAuth()结束 返回:{}",is_match);
|
||||
return is_match;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package cn.lailab.toolbox.controller;
|
||||
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.lailab.toolbox.service.impl.ResultServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class WXController {
|
||||
// 获取小程序AppID
|
||||
@Value("${mini-program.appid}")
|
||||
private String appid;
|
||||
// 获取小程序Secrty
|
||||
@Value("${mini-program.secret}")
|
||||
private String secret;
|
||||
// 返回消息类
|
||||
@Autowired
|
||||
ResultServiceImpl resultService;
|
||||
// 定义日志打印类
|
||||
private final Logger logger = LoggerFactory.getLogger(WXController.class);
|
||||
// 获取微信OpenId
|
||||
@RequestMapping("/api/wx/getOpenId")
|
||||
@ResponseBody
|
||||
public String getOpenId(@RequestParam(required=false,name = "code") String code) {
|
||||
logger.info("调用方法getOpenId()开始 {}",code);
|
||||
// 拼接所需字符串
|
||||
String url = "https://api.weixin.qq.com/sns/jscode2session" +
|
||||
"?appid=" +appid +
|
||||
"&secret=" + secret + //自己的appSecret
|
||||
"&js_code=" + code +
|
||||
"&grant_type=authorization_code" +
|
||||
"&connect_redirect=1";
|
||||
|
||||
if (code == null || code.trim().isEmpty()) {
|
||||
logger.error("参数code不存在,请检查入参!");
|
||||
return resultService.error("参数code不存在,请检查入参!");
|
||||
}
|
||||
String result = HttpUtil.get(url, CharsetUtil.CHARSET_UTF_8);
|
||||
String rst = resultService.success("调用完成",result);
|
||||
logger.info("调用方法getOpenId()结束 {}",rst);
|
||||
return rst;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user