nili
8 months ago
15 changed files with 345 additions and 46 deletions
@ -0,0 +1,9 @@ |
|||
package awesome.group.game.service.bo.citrus; |
|||
|
|||
public class LoginReq { |
|||
public String mobile; |
|||
public String pwd; |
|||
public String appCode; |
|||
public String code;//验证码
|
|||
|
|||
} |
@ -0,0 +1,32 @@ |
|||
package awesome.group.game.service.bo.citrus; |
|||
|
|||
import awesome.group.game.dao.bean.MatrixUser; |
|||
|
|||
public class UserBo { |
|||
public Integer id; |
|||
public String mobile; |
|||
public String name; |
|||
public String nickname; |
|||
public String avatar; |
|||
public Integer money;//分,已提现金额
|
|||
public String aliPayAccount; |
|||
public Long income;//ecpm和,分
|
|||
public String inviteCode; |
|||
public String inviteUrl; |
|||
|
|||
public UserBo() { |
|||
} |
|||
|
|||
public UserBo(MatrixUser u) { |
|||
this.id = u.getId(); |
|||
this.mobile = u.getMobile(); |
|||
this.name = u.getName(); |
|||
this.aliPayAccount = u.getAliPayAccount(); |
|||
this.income = u.getIncome(); |
|||
this.inviteCode = u.getInviteCode(); |
|||
this.inviteUrl = String.format("https://pomelo.bzgames.cn/register/%s", u.getInviteCode()); |
|||
this.nickname = u.getNickname(); |
|||
this.avatar = u.getAvatar(); |
|||
this.money = u.getMoney(); |
|||
} |
|||
} |
@ -0,0 +1,63 @@ |
|||
package awesome.group.game.service.citrus; |
|||
|
|||
import awesome.group.game.dao.bean.MatrixApp; |
|||
import awesome.group.game.dao.bean.MatrixUser; |
|||
import awesome.group.game.dao.mapper.MatrixAppMapper; |
|||
import awesome.group.game.dao.mapper.MatrixUserMapper; |
|||
import awesome.group.game.service.SmsService; |
|||
import awesome.group.game.service.bo.citrus.LoginReq; |
|||
import awesome.group.game.service.bo.citrus.UserBo; |
|||
import awesome.group.game.service.common.exception.PaganiException; |
|||
import awesome.group.game.service.common.exception.PaganiExceptionCode; |
|||
import awesome.group.game.service.util.EncryptUtil; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.util.Assert; |
|||
import org.springframework.util.StringUtils; |
|||
|
|||
@Service |
|||
public class UserService { |
|||
@Autowired |
|||
private MatrixUserMapper userMapper; |
|||
|
|||
@Autowired |
|||
private MatrixAppMapper appMapper; |
|||
|
|||
@Autowired |
|||
private SmsService smsService; |
|||
|
|||
public MatrixUser login(LoginReq req) { |
|||
Assert.isTrue(req != null, "非法请求"); |
|||
Assert.isTrue(StringUtils.hasText(req.mobile), "非法请求"); |
|||
Assert.isTrue(StringUtils.hasText(req.pwd), "非法请求"); |
|||
Assert.isTrue(StringUtils.hasText(req.appCode), "appCode不能为空"); |
|||
MatrixApp app = appMapper.queryByCode(req.appCode); |
|||
Assert.isTrue(StringUtils.hasText(req.appCode), "appCode非法"); |
|||
MatrixUser user = userMapper.selectByAppIdAndMobile(app.getId(), req.mobile); |
|||
Assert.isTrue(user != null, "用户不存在"); |
|||
String str = EncryptUtil.sha1(req.pwd); |
|||
Assert.isTrue(user.getPwd().equals(str), "密码错误"); |
|||
return user; |
|||
} |
|||
|
|||
public MatrixUser loginByCode(LoginReq req) { |
|||
Assert.isTrue(req != null, "非法请求"); |
|||
Assert.isTrue(StringUtils.hasText(req.mobile), "非法请求"); |
|||
Assert.isTrue(StringUtils.hasText(req.code), "非法请求"); |
|||
Assert.isTrue(StringUtils.hasText(req.appCode), "appCode不能为空"); |
|||
MatrixApp app = appMapper.queryByCode(req.appCode); |
|||
Assert.isTrue(StringUtils.hasText(req.appCode), "appCode非法"); |
|||
if (!smsService.verifyAndUseCaptcha(req.mobile, "login", req.code)) { |
|||
throw new PaganiException(PaganiExceptionCode.GENERAL_ERROR, "验证码错误"); |
|||
} |
|||
|
|||
MatrixUser user = userMapper.selectByAppIdAndMobile(app.getId(), req.mobile); |
|||
Assert.isTrue(user != null, "用户不存在"); |
|||
return user; |
|||
} |
|||
|
|||
public UserBo getUser(Integer uid) { |
|||
MatrixUser u = userMapper.selectById(uid); |
|||
return new UserBo(u); |
|||
} |
|||
} |
@ -0,0 +1,89 @@ |
|||
package awesome.group.game.web.filter; |
|||
|
|||
import awesome.group.game.service.common.log.L; |
|||
import awesome.group.game.service.common.response.R; |
|||
import awesome.group.game.service.util.JwtUtils; |
|||
import awesome.group.game.web.RequestContext; |
|||
import com.auth0.jwt.exceptions.TokenExpiredException; |
|||
import com.google.gson.Gson; |
|||
import jakarta.servlet.*; |
|||
import jakarta.servlet.annotation.WebFilter; |
|||
import jakarta.servlet.http.Cookie; |
|||
import jakarta.servlet.http.HttpServletRequest; |
|||
import jakarta.servlet.http.HttpServletResponse; |
|||
import org.springframework.util.StringUtils; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* @author nidaren |
|||
*/ |
|||
@WebFilter(filterName = "CitrusLoginFilter", urlPatterns = {"/api/citrus/*"}) |
|||
public class CitrusLoginFilter implements Filter { |
|||
|
|||
|
|||
@Override |
|||
public void init(FilterConfig filterConfig) throws ServletException { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
|||
try { |
|||
HttpServletRequest httpRequest = (HttpServletRequest) request; |
|||
String token = ""; |
|||
if(httpRequest.getCookies() != null) { |
|||
for (Cookie c : httpRequest.getCookies()) { |
|||
if (c.getName().equals("CitrusToken")) { |
|||
token = c.getValue(); |
|||
} |
|||
} |
|||
} |
|||
List<String> openApi = List.of( |
|||
"/api/citrus/register", |
|||
"/api/citrus/login" |
|||
); |
|||
boolean pass = false; |
|||
Integer userId = null; |
|||
try { |
|||
if (StringUtils.hasText(token)) { |
|||
userId = JwtUtils.parseToken(token); |
|||
pass = true; |
|||
} |
|||
} catch (TokenExpiredException e) { |
|||
//忽略
|
|||
} catch (Exception e) { |
|||
L.trace("parseTokenError", "token:" + token, e); |
|||
} |
|||
String path = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length()).replaceAll("[/]+$", ""); |
|||
for (String s : openApi) { |
|||
if (path.startsWith(s)) { |
|||
pass = true; |
|||
} |
|||
} |
|||
if (pass) { |
|||
RequestContext.initCitrus(httpRequest, (HttpServletResponse) response, userId); |
|||
chain.doFilter(request, response); |
|||
} else { |
|||
authFail(response); |
|||
} |
|||
} finally { |
|||
RequestContext.clear(); |
|||
} |
|||
} |
|||
|
|||
private void authFail(ServletResponse response) throws IOException { |
|||
R<String> res = new R<>(-88888, "not login ", null); |
|||
Gson gson = new Gson(); |
|||
response.setContentType("application/json;charset=UTF-8"); |
|||
response.setCharacterEncoding("UTF-8"); |
|||
response.getWriter().write(gson.toJson(res)); |
|||
response.getWriter().flush(); |
|||
} |
|||
|
|||
@Override |
|||
public void destroy() { |
|||
|
|||
} |
|||
} |
@ -0,0 +1,78 @@ |
|||
package awesome.group.game.web.rest.citrus; |
|||
|
|||
import awesome.group.game.dao.bean.MatrixUser; |
|||
import awesome.group.game.service.bo.citrus.LoginReq; |
|||
import awesome.group.game.service.citrus.RegisterService; |
|||
import awesome.group.game.service.SmsService; |
|||
import awesome.group.game.service.bo.MatrixAppBo; |
|||
import awesome.group.game.service.bo.RegisterBo; |
|||
import awesome.group.game.service.citrus.UserService; |
|||
import awesome.group.game.service.common.response.R; |
|||
import awesome.group.game.service.util.JwtUtils; |
|||
import awesome.group.game.web.RequestContext; |
|||
import awesome.group.game.web.aop.RestApi; |
|||
import jakarta.servlet.http.Cookie; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.util.StringUtils; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
@RestController |
|||
@RequestMapping("/api/citrus") |
|||
public class OpenController { |
|||
|
|||
@Autowired |
|||
private RegisterService registerService; |
|||
|
|||
@Autowired |
|||
private UserService userService; |
|||
|
|||
@Autowired |
|||
private SmsService smsService; |
|||
|
|||
@PostMapping("/register/sendCode") |
|||
@RestApi |
|||
public R<Void> sendCode(@RequestParam String mobile, @RequestParam String scene) { |
|||
smsService.sendCaptcha(mobile, scene); |
|||
return new R<>(null); |
|||
} |
|||
|
|||
@GetMapping("/register/getApp") |
|||
@RestApi |
|||
public R<MatrixAppBo> getApp(@RequestParam(required = false) String inviteCode, @RequestParam(required = false) String appCode) { |
|||
if (StringUtils.hasText(appCode)) { |
|||
return new R<>(registerService.getAppByAppCode(appCode)); |
|||
} |
|||
if (StringUtils.hasText(inviteCode)) { |
|||
return new R<>(registerService.getApp(inviteCode)); |
|||
} |
|||
return new R<>(null); |
|||
} |
|||
|
|||
@PostMapping("/register/submitRegister") |
|||
@RestApi |
|||
public R<Void> submitRegister(@RequestBody RegisterBo bo) { |
|||
registerService.register(bo); |
|||
return new R<>(null); |
|||
} |
|||
|
|||
@PostMapping("/loginByPwd") |
|||
@RestApi |
|||
public R<String> loginByPwd(@RequestBody LoginReq req) { |
|||
MatrixUser u = userService.login(req); |
|||
String token = JwtUtils.generatorToken(u.getId() + "", 15 * 86400); |
|||
Cookie cookie = new Cookie("CitrusToken", token); |
|||
RequestContext.getResponse().addCookie(cookie); |
|||
return new R<>(R.CODE_SUCCESS, "ok", token); |
|||
} |
|||
|
|||
@PostMapping("/loginByCode") |
|||
@RestApi |
|||
public R<String> loginByCode(@RequestBody LoginReq req) { |
|||
MatrixUser u = userService.loginByCode(req); |
|||
String token = JwtUtils.generatorToken(u.getId() + "", 15 * 86400); |
|||
Cookie cookie = new Cookie("CitrusToken", token); |
|||
RequestContext.getResponse().addCookie(cookie); |
|||
return new R<>(R.CODE_SUCCESS, "ok", token); |
|||
} |
|||
|
|||
} |
@ -1,42 +0,0 @@ |
|||
package awesome.group.game.web.rest.citrus; |
|||
|
|||
import awesome.group.game.service.RegisterService; |
|||
import awesome.group.game.service.SmsService; |
|||
import awesome.group.game.service.bo.MatrixAppBo; |
|||
import awesome.group.game.service.bo.RegisterBo; |
|||
import awesome.group.game.service.common.response.R; |
|||
import awesome.group.game.web.aop.RestApi; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
@RestController |
|||
@RequestMapping("/api/citrus/register") |
|||
public class RegisterController { |
|||
|
|||
@Autowired |
|||
private RegisterService registerService; |
|||
|
|||
@Autowired |
|||
private SmsService smsService; |
|||
|
|||
@PostMapping("/sendCode") |
|||
@RestApi |
|||
public R<Void> sendCode(@RequestParam String mobile) { |
|||
smsService.sendCaptcha(mobile, "register"); |
|||
return new R<>(null); |
|||
} |
|||
|
|||
@GetMapping("/getApp") |
|||
@RestApi |
|||
public R<MatrixAppBo> getApp(@RequestParam String inviteCode) { |
|||
return new R<>(registerService.getApp(inviteCode)); |
|||
} |
|||
|
|||
@PostMapping("/submitRegister") |
|||
@RestApi |
|||
public R<Void> submitRegister(@RequestBody RegisterBo bo) { |
|||
registerService.register(bo); |
|||
return new R<>(null); |
|||
} |
|||
|
|||
} |
@ -0,0 +1,25 @@ |
|||
package awesome.group.game.web.rest.citrus; |
|||
|
|||
import awesome.group.game.service.bo.citrus.UserBo; |
|||
import awesome.group.game.service.citrus.UserService; |
|||
import awesome.group.game.service.common.response.R; |
|||
import awesome.group.game.web.RequestContext; |
|||
import awesome.group.game.web.aop.RestApi; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.GetMapping; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
@RestController |
|||
@RequestMapping("/api/citrus/user") |
|||
public class UserController { |
|||
|
|||
@Autowired |
|||
private UserService userService; |
|||
|
|||
@GetMapping("/current") |
|||
@RestApi |
|||
public R<UserBo> currentUser() { |
|||
return new R<>(userService.getUser(RequestContext.getCitrusUid())); |
|||
} |
|||
} |
Loading…
Reference in new issue