Skip to content

Commit

Permalink
✨增加未登录时跳转与登录后自动回到原界面
Browse files Browse the repository at this point in the history
  • Loading branch information
mewhz committed Nov 20, 2022
1 parent e853d9c commit a7b9c26
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 21 deletions.
7 changes: 7 additions & 0 deletions pastecode-system/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
<version>5.8.6</version>
</dependency>

<!-- Sa-Token 权限认证,在线文档:https://sa-token.cc -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<version>1.32.0</version>
</dependency>



</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static <T> BaseResponse<T> codeError(String responseMessage) {
* 未知错误
* @return 错误代码与未知错误
*/
public static <T> BaseResponse<T> error() {
return new BaseResponse<>(50000, "未知错误");
public static <T> BaseResponse<T> error(String responseMessage) {
return new BaseResponse<>(50000, responseMessage);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mewhz.paste.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import com.mewhz.paste.model.entity.Code;
import com.mewhz.paste.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -38,6 +39,7 @@ public Map<String, Object> save(@RequestBody Code code){
return result;
}

@SaCheckLogin
@GetMapping("/id/{id}")
public Code findById(@PathVariable("id") Integer id){
return codeService.getById(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.List;

@RestController
@RequestMapping("codeInfo")
@RequestMapping("/codeInfo")
public class CodeInfoController {

@Resource
Expand All @@ -24,7 +24,7 @@ public BaseResponse<List<CodeInfoResponse>> selectAll() {
return ResultUtils.success(codeInfoService.selectAll());
}

@GetMapping("/{codeId}")
@GetMapping("codeId/{codeId}")
public BaseResponse<CodeInfoResponse> selectByCodeId(@PathVariable Integer codeId) {
return ResultUtils.success(codeInfoService.selectByCodeId(codeId));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.mewhz.paste.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.mewhz.paste.common.BaseResponse;
import com.mewhz.paste.common.ResultUtils;
import com.mewhz.paste.model.dto.UserLoginRequest;
import com.mewhz.paste.model.entity.User;
import com.mewhz.paste.model.vo.UserLoginResponse;
import com.mewhz.paste.service.UserService;
import org.springframework.web.bind.annotation.*;

Expand All @@ -27,12 +30,13 @@ public BaseResponse<List<User>> findAll() {
}

@PostMapping("/login")
public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest) {
public BaseResponse<UserLoginResponse> userLogin(@RequestBody UserLoginRequest userLoginRequest) {

String userAccount = userLoginRequest.getUserAccount();
String userPassword = userLoginRequest.getUserPassword();

User user = userService.userLogin(userAccount, userPassword);
UserLoginResponse userLoginResponse = new UserLoginResponse();

System.out.println("user = " + user);
if (user == null) {
Expand All @@ -42,10 +46,24 @@ else if (!user.getUserPassword().equals(userPassword)) {
return ResultUtils.loginError("密码错误!!");
}
else if (user.getUserAccount().equals(userAccount) && user.getUserPassword().equals(userPassword)) {
return ResultUtils.success(user);
System.out.println(user);
System.out.println("=====");
BeanUtil.copyProperties(user, userLoginResponse);
System.out.println(userLoginResponse);
StpUtil.login(userLoginResponse.getId());
System.out.println("StpUtil.isLogin() = " + StpUtil.isLogin());
userLoginResponse.setTokenValue(StpUtil.getTokenInfo().getTokenValue());
userLoginResponse.setTokenName(StpUtil.getTokenInfo().getTokenName());

return ResultUtils.success(userLoginResponse);
}
else {
return ResultUtils.error();
return ResultUtils.error("未知错误, 请联系管理员!!");
}
}

@GetMapping("/info")
public boolean info() {
return StpUtil.isLogin();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mewhz.paste.exception;

import cn.dev33.satoken.exception.NotLoginException;
import com.mewhz.paste.common.BaseResponse;
import com.mewhz.paste.common.ResultUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
* @author mewhz
*/
@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(NotLoginException.class)
public BaseResponse<?> handlerNotLoginException(NotLoginException e) {
return ResultUtils.error(e.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.mewhz.paste.model.vo;

import lombok.Data;

/**
* @author mewhz
*/
@Data
public class UserLoginResponse {
private Integer id;
private String userName;
private String userAccount;

private String tokenName;
private String tokenValue;
}
31 changes: 26 additions & 5 deletions pastecode-ui/src/views/Code.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,40 @@ export default {
},
methods: {
load() {
this.$axios.get('http://127.0.0.1:9090/code/id/' + this.id).then((res) => {
this.codeText = res.data.codeText;
this.codeClass = `language-${res.data.codeType} show-language`;
let tokenNames = localStorage.getItem("tokenName");
let tokenValue = localStorage.getItem("tokenValue");
console.log("tokenValue:", localStorage.getItem("tokenName"))
let headers = {};
headers[tokenNames] = tokenValue;
console.table(headers);
this.$axios.get('http://127.0.0.1:9090/code/id/' + this.id, {
headers: headers,
}).then((res) => {
res = res.data;
if (res.responseCode === 50000) {
console.log("path:", this.$route.path);
localStorage.setItem("path", this.$route.path);
this.$router.push("/login");
return;
}
this.codeText = res.codeText;
this.codeClass = `language-${res.codeType} show-language`;
if (this.codeText === undefined || this.codeText === ""){
this.$router.push("/");
}
if (res.data.codeTitle === undefined || res.data.codeTitle === "") {
if (res.codeTitle === undefined || res.codeTitle === "") {
this.codeTitle = "";
}
else {
this.codeTitle += res.data.codeTitle;
this.codeTitle += res.codeTitle;
}
this.$nextTick(() => {
Expand Down
8 changes: 8 additions & 0 deletions pastecode-ui/src/views/Home.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<template>
<el-row>
<span style="float:right;">
<el-link :underline="false" @click="jumpLogin">用户登录</el-link>
</span>
<el-col :offset="5" :xs="8" :sm="6" :md="8" :lg="9" :xl="11">
<div class="page-header">
<h1>
Expand Down Expand Up @@ -151,6 +154,11 @@ export default {
this.$router.push('/code/title/' + this.searchText);
},
// 跳转到登录界面
jumpLogin() {
this.$router.push('/login');
}
},
}
</script>
Expand Down
33 changes: 24 additions & 9 deletions pastecode-ui/src/views/Login.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div id="login">
<el-col :span="8" :offset="8">
<h1>账户登录</h1>
<h1>用户登录</h1>
<el-form :model="user" :rules="rules" ref="ruleForm">
<el-form-item prop="account">
<el-input type="text" v-model="user.account" autocomplete="off" placeholder="账号"/>
Expand Down Expand Up @@ -59,7 +59,16 @@ export default {
console.table(response.data);
if (0 === response.data.responseCode) {
let data = response.data;
if (0 === data.responseCode) {
let tokenValue = data.responseData.tokenValue;
let tokenName = data.responseData.tokenName;
localStorage.setItem("tokenValue", tokenValue);
localStorage.setItem("tokenName", tokenName);
// 弹出的信息提示
this.$message({
Expand All @@ -68,13 +77,13 @@ export default {
duration: 500
});
// setTimeout(() => {
// this.userJump(this.user.username);
// }, 500);
setTimeout(() => {
this.userJump();
}, 500);
} else {
this.$message({
message: response.data.responseMessage,
message: data.responseMessage,
type: 'warning',
duration: 1000
});
Expand All @@ -83,11 +92,17 @@ export default {
},
// 跳转
async userJump(uid) {
async userJump() {
// localStorage.setItem("userStatus", this.user.status);
// localStorage.setItem("uid", this.user.username);
await this.$router.push('/home/' + uid);
console.log("this.path:", localStorage.getItem("path"));
let path = localStorage.getItem("path");
if (path === null) {
await this.$router.push('/');
}
else {
await this.$router.push(path);
}
}
}
}
Expand Down

0 comments on commit a7b9c26

Please sign in to comment.