Skip to content

Commit

Permalink
fix: 设备id获取方式调整
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyueyi committed Aug 15, 2023
1 parent a923f57 commit 9ba2bb0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,10 @@ <h2 class="title">添加二哥微信备注星球,审核更快</h2>
}
}

const subscribeUrl = "/subscribe";
if(!deviceId) {
deviceId = getCookie("f-device");
}
const subscribeUrl = "/subscribe?deviceId=" + deviceId;
const source = new EventSource(subscribeUrl);
sseSource = source;

Expand Down Expand Up @@ -487,11 +490,21 @@ <h2 class="title">添加二哥微信备注星球,审核更快</h2>
buildConnect(code);
}

fetchCodeCnt = 0;
intHook = self.setInterval("fetchCode()", 1000);
}

let fetchCodeCnt = 0;
function fetchCode() {
if (deviceId) {
if (++fetchCodeCnt > 5) {
// 为了避免不停的向后端发起请求,做一个最大的重试计数限制
try {
window.clearInterval(intHook);
} catch (e) {}
return;
}

$.ajax({
url: "/login/fetch?deviceId=" + deviceId, type: "get", dataType: "text", success: function (data) {
console.log("data>>>>>>>>: ", data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ public class WxLoginController extends BaseViewController {
@MdcDot
@ResponseBody
@GetMapping(path = "subscribe", produces = {org.springframework.http.MediaType.TEXT_EVENT_STREAM_VALUE})
public SseEmitter subscribe(HttpServletResponse response) throws IOException {
public SseEmitter subscribe(String deviceId) throws IOException {
return qrLoginHelper.subscribe();
}

@GetMapping(path = "/login/fetch")
@ResponseBody
public String resendCode(String deviceId) throws IOException {
return qrLoginHelper.resend(deviceId);
return qrLoginHelper.resend();
}

/**
Expand All @@ -56,7 +56,7 @@ public String resendCode(String deviceId) throws IOException {
@MdcDot
@GetMapping(path = "/login/refresh")
@ResponseBody
public ResVo<WxLoginVo> refresh() throws IOException {
public ResVo<WxLoginVo> refresh(String deviceId) throws IOException {
WxLoginVo vo = new WxLoginVo();
String code = qrLoginHelper.refreshCode();
if (StringUtils.isBlank(code)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ public SseEmitter subscribe() throws IOException {
return sseEmitter;
}

public String resend(String deviceId) throws IOException {
public String resend() throws IOException {
// 获取旧的验证码,注意不使用 getUnchecked, 避免重新生成一个验证码
deviceId = StringUtils.isBlank(deviceId) ? ReqInfoContext.getReqInfo().getDeviceId() : deviceId;
String deviceId = ReqInfoContext.getReqInfo().getDeviceId();
String oldCode = deviceCodeCache.getIfPresent(deviceId);
SseEmitter lastSse = oldCode == null ? null : verifyCodeCache.getIfPresent(oldCode);
if (lastSse != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,14 @@ private boolean isStaticURI(HttpServletRequest request) {
* @return
*/
private String getOrInitDeviceId(HttpServletRequest request, HttpServletResponse response) {
if (StringUtils.isNotBlank(request.getParameter("deviceId"))) {
return request.getParameter("deviceId");
String deviceId = request.getParameter("deviceId");
if (StringUtils.isNotBlank(deviceId) && !"null".equalsIgnoreCase(deviceId)) {
return deviceId;
}

Cookie device = SessionUtil.findCookieByName(request, LoginOutService.USER_DEVICE_KEY);
if (device == null) {
String deviceId = UUID.randomUUID().toString();
deviceId = UUID.randomUUID().toString();
if (response != null) {
response.addCookie(SessionUtil.newCookie(LoginOutService.USER_DEVICE_KEY, deviceId));
}
Expand Down

0 comments on commit 9ba2bb0

Please sign in to comment.