north-glass-erp/northglass-erp/src/lang/en.js
@@ -3,6 +3,7 @@ userErr:'Please enter your user id', pwErr:'Please enter your password', loginSuccessful:'User login successful', loginErr:'The account or password is incorrect', connectErr:'server connection failed', user:'User', password:'Pass', north-glass-erp/northglass-erp/src/lang/zh.js
@@ -3,6 +3,7 @@ userErr:'请输入你的账号', pwErr:'请输入你的密码', loginSuccessful:'登录成功', loginErr:'账号或密码错误', connectErr:'服务器连接失败', user:'用户', password:'密码', north-glass-erp/northglass-erp/src/layout/MainErpView.vue
@@ -18,11 +18,18 @@ const userStore = useUserInfoStore() const user = userStore.user.userName store.createWebSocket(); function quit(){ async function quit(){ userStore.$patch({ user:null }) router.push("/login") await request.post("/userInfo/logout").then((res) => { }).catch((e)=>{ }).finally(()=>{ router.push("/login") }) } north-glass-erp/northglass-erp/src/utils/request.js
@@ -15,7 +15,7 @@ request.interceptors.request.use(config => { config.headers['Content-Type'] = 'application/json;charset=utf-8'; if(userStore.user){ config.headers['token'] = userStore.user.token; config.headers['satoken'] = userStore.user.token; } // 设置请求头 return config north-glass-erp/northglass-erp/src/views/LoginView.vue
@@ -1,14 +1,12 @@ <script lang="ts" setup> import {onMounted, onUnmounted, reactive, ref} from "vue" import {useRouter,useRoute } from 'vue-router' import {useRoute, useRouter} from 'vue-router' import type {FormInstance, FormRules} from 'element-plus' import { ElMessage } from "element-plus"; import { Lock,Avatar } from '@element-plus/icons-vue' import {ElMessage} from "element-plus"; import {Avatar, Lock} from '@element-plus/icons-vue' import request from '@/utils/request' import userInfo from '@/stores/userInfo' import { sendSock, createWebSocket, closeSock,global_callback1 } from "@/utils/webSocket" import { useI18n } from 'vue-i18n' import i18n from "@/lang/index" import {useI18n} from 'vue-i18n' //语言获取 const { t } = useI18n() let language = ref(localStorage.getItem('lang') || 'zh') @@ -54,15 +52,15 @@ if (valid) { loginLoadings.value=true userForm.pass = btoa(userForm.pass) request.post('/user/login', request.post('/userInfo/login', userForm).then((res) => { if(res['code']==200){ if(res['code']==200 && res['data']){ store.$patch({user:res.data}) router.push('/main') ElMessage.success(t('login.loginSuccessful')) } else { ElMessage.error(res['msg']) ElMessage.error(t('login.loginErr')) loginLoadings.value=false return false } north-glass-erp/northglass-erp/src/views/sd/order/UpdateOrderCraft.vue
@@ -556,7 +556,7 @@ <el-row> <el-col :span="4">商标位置:</el-col> <el-col :span="25"> <el-radio v-model="trademarkAttr.location" v-for="item in trademarkLocation" :label="item" /> <el-checkbox v-model="trademarkAttr.location" v-for="item in trademarkLocation" :label="item" /> </el-col> </el-row> north-glass-erp/pom.xml
@@ -130,13 +130,19 @@ <artifactId>hibernate-core</artifactId> <version>4.3.5.Final</version> </dependency> <!--后端导出--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>cn.dev33</groupId> <artifactId>sa-token-spring-boot-starter</artifactId> <version>1.37.0</version> </dependency> </dependencies> north-glass-erp/src/main/java/com/example/erp/common/Constants.java
@@ -6,6 +6,7 @@ public interface Constants { String Code_500="500";//系统错误 String Code_401="401";//权限不足 String Code_402="402";//还未登陆 String Code_400="400";// String Code_200="200";//成功 String Code_600="600";//其他问题 north-glass-erp/src/main/java/com/example/erp/config/SaTokenConfigure.java
New file @@ -0,0 +1,16 @@ package com.example.erp.config; import cn.dev33.satoken.interceptor.SaInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class SaTokenConfigure implements WebMvcConfigurer { // 注册 Sa-Token 拦截器,打开注解式鉴权功能 @Override public void addInterceptors(InterceptorRegistry registry) { // 注册 Sa-Token 拦截器,打开注解式鉴权功能 registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**"); } } north-glass-erp/src/main/java/com/example/erp/config/StpInterfaceImpl.java
New file @@ -0,0 +1,44 @@ package com.example.erp.config; import cn.dev33.satoken.stp.StpInterface; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; /** * 自定义权限认证接口扩展,Sa-Token 将从此实现类获取每个账号拥有的权限码 * * @author kong * @since 2022-10-13 */ @Component // 打开此注解,保证此类被springboot扫描,即可完成sa-token的自定义权限验证扩展 public class StpInterfaceImpl implements StpInterface { /** * 返回一个账号所拥有的权限码集合 */ @Override public List<String> getPermissionList(Object loginId, String loginType) { // 本list仅做模拟,实际项目中要根据具体业务逻辑来查询权限 List<String> list = new ArrayList<>(); list.add("101"); list.add("user.add"); list.add("user.update"); list.add("user.get"); // list.add("user.delete"); list.add("art.*"); return list; } /** * 返回一个账号所拥有的角色标识集合 */ @Override public List<String> getRoleList(Object loginId, String loginType) { // 本list仅做模拟,实际项目中要根据具体业务逻辑来查询角色 List<String> list = new ArrayList<String>(); list.add("admin"); list.add("super-admin"); list.add("tao"); return list; } } north-glass-erp/src/main/java/com/example/erp/controller/userInfo/SysMenuController.java
@@ -1,5 +1,6 @@ package com.example.erp.controller.userInfo; import cn.dev33.satoken.annotation.SaCheckLogin; import com.example.erp.common.CacheUtil; import com.example.erp.common.Result; import com.example.erp.service.userInfo.SysMenuService; @@ -15,10 +16,15 @@ @RequestMapping("/menu") public class SysMenuController { @Autowired private CacheUtil cacheUtil; @Autowired private SysMenuService sysMenuService; private final CacheUtil cacheUtil; private final SysMenuService sysMenuService; public SysMenuController(CacheUtil cacheUtil, SysMenuService sysMenuService) { this.cacheUtil = cacheUtil; this.sysMenuService = sysMenuService; } @SaCheckLogin @GetMapping("/getMenu/{lang}") public Result getMenu(@PathVariable String lang){ MyChannelHandlerPool myChannelHandlerPool = new MyChannelHandlerPool(); north-glass-erp/src/main/java/com/example/erp/controller/userInfo/UserInfoController.java
New file @@ -0,0 +1,49 @@ package com.example.erp.controller.userInfo; import cn.dev33.satoken.annotation.SaCheckLogin; import cn.dev33.satoken.annotation.SaCheckPermission; import cn.dev33.satoken.stp.StpUtil; import com.example.erp.common.Result; import com.example.erp.controller.dto.UserDTO; import com.example.erp.service.userInfo.UserService; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/userInfo") public class UserInfoController { private final UserService userService; public UserInfoController(UserService userService) { this.userService = userService; } @PostMapping("/login") public Result login(@RequestBody UserDTO userDTO) { return Result.seccess(userService.doLogin(userDTO)); } @PostMapping("/logout") public Result logout() { return Result.seccess(userService.logout()); } @PostMapping("/isLogin") public Result isLogin() { return Result.seccess(userService.isLogin()); } @SaCheckLogin @PostMapping("/userAdd") public Result userAdd() { return Result.seccess(StpUtil.getTokenInfo()); } @PostMapping("/userDelete") @SaCheckPermission("user.delete") public Result userDelete() { return Result.seccess(123123); } } north-glass-erp/src/main/java/com/example/erp/entity/userInfo/User.java
@@ -10,7 +10,7 @@ @TableId(type = IdType.AUTO) private Integer id; private String loginName; private String passWord; private String password; private String userName; private String address; private String phone; north-glass-erp/src/main/java/com/example/erp/exception/GlobalExceptionHandle.java
@@ -1,5 +1,9 @@ package com.example.erp.exception; import cn.dev33.satoken.exception.NotLoginException; import cn.dev33.satoken.exception.NotPermissionException; import cn.dev33.satoken.util.SaResult; import com.example.erp.common.Constants; import com.example.erp.common.Result; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -15,4 +19,18 @@ public Result handle(ServiceException se){ return Result.error(se.getCode(),se.getMessage()); } @ExceptionHandler(NotPermissionException.class) @ResponseBody public Result handlerException(NotPermissionException e) { return Result.error(Constants.Code_401,"This user has no permission to access this resource"); } // 拦截:未登录异常 @ExceptionHandler(NotLoginException.class) @ResponseBody public Result handlerException(NotLoginException e) { // 返回给前端 return Result.error(Constants.Code_402,"Please login user"); } } north-glass-erp/src/main/java/com/example/erp/mapper/userInfo/UserMapper.java
@@ -1,6 +1,7 @@ package com.example.erp.mapper.userInfo; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.erp.controller.dto.UserDTO; import com.example.erp.entity.userInfo.User; import org.apache.ibatis.annotations.Mapper; @@ -11,7 +12,7 @@ @Mapper public interface UserMapper { public interface UserMapper extends BaseMapper<User> { List<User> findAll(); User findOne(Integer id); north-glass-erp/src/main/java/com/example/erp/service/userInfo/UserService.java
@@ -1,28 +1,27 @@ package com.example.erp.service.userInfo; import cn.dev33.satoken.stp.StpUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.dynamic.datasource.annotation.DS; import com.example.erp.common.CacheUtil; import com.example.erp.common.Result; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.example.erp.controller.dto.UserDTO; import com.example.erp.entity.userInfo.User; import com.example.erp.mapper.userInfo.UserMapper; import com.example.erp.controller.dto.UserDTO; import com.example.erp.tools.TokenTools; import org.apache.ibatis.jdbc.Null; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; @Service @DS("user_info") public class UserService { @Autowired private UserMapper userMapper; private final UserMapper userMapper; public UserService(UserMapper userMapper) { this.userMapper = userMapper; } @Cacheable(value="users", key="#userDTO.getUserId()") @@ -65,5 +64,29 @@ } return returnUser; } /*----------------新版本登陆*/ public UserDTO doLogin(UserDTO userDTO) { User user = userMapper.selectOne(new QueryWrapper<User>(). eq("login_name", userDTO.getUserId()).eq("password", userDTO.getPass())); if(user!=null){ StpUtil.login(userDTO.getUserId()); userDTO.setPass(null); userDTO.setToken(StpUtil.getTokenValue()); return userDTO; }else{ return null; } } public Boolean isLogin() { // 返回当前用户是否登录 return StpUtil.isLogin(); } public Boolean logout() { StpUtil.logout(); return true; } }