Commit bbaef679 authored by 耿迪迪's avatar 耿迪迪

账户切换

parent afd0b960
......@@ -61,6 +61,15 @@ public class SysLoginController
return ajax;
}
@PostMapping("/changeLogin")
public AjaxResult changeLogin(@RequestBody LoginBody loginBody){
AjaxResult ajax = AjaxResult.success();
// 生成令牌
String token = loginService.changeLogin(loginBody.getUsername(), loginBody.getPassword());
ajax.put(Constants.TOKEN, token);
return ajax;
}
/**
* 获取用户信息
*
......
package com.zehong.web.controller.userRelation;
import java.util.List;
import com.zehong.common.core.domain.entity.SysUser;
import com.zehong.common.core.exception.BusinessException;
import com.zehong.common.utils.SecurityUtils;
import com.zehong.common.utils.StringUtils;
import com.zehong.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zehong.common.annotation.Log;
import com.zehong.common.core.controller.BaseController;
import com.zehong.common.core.domain.AjaxResult;
import com.zehong.common.enums.BusinessType;
import com.zehong.system.domain.TUserRelation;
import com.zehong.system.service.ITUserRelationService;
import com.zehong.common.utils.poi.ExcelUtil;
import com.zehong.common.core.page.TableDataInfo;
/**
* 用户关联Controller
*
* @author zehong
* @date 2023-07-11
*/
@RestController
@RequestMapping("/user/relation")
public class TUserRelationController extends BaseController
{
@Autowired
private ITUserRelationService tUserRelationService;
@Autowired
private ISysUserService sysUserService;
/**
* 查询用户关联列表
*/
//@PreAuthorize("@ss.hasPermi('system:relation:list')")
@GetMapping("/list")
public TableDataInfo list(TUserRelation tUserRelation)
{
startPage();
List<TUserRelation> list = tUserRelationService.selectTUserRelationList(tUserRelation);
return getDataTable(list);
}
/**
* 导出用户关联列表
*/
//@PreAuthorize("@ss.hasPermi('system:relation:export')")
@Log(title = "用户关联", businessType = BusinessType.EXPORT)
@GetMapping("/export")
public AjaxResult export(TUserRelation tUserRelation)
{
List<TUserRelation> list = tUserRelationService.selectTUserRelationList(tUserRelation);
ExcelUtil<TUserRelation> util = new ExcelUtil<TUserRelation>(TUserRelation.class);
return util.exportExcel(list, "用户关联数据");
}
/**
* 获取用户关联详细信息
*/
// @PreAuthorize("@ss.hasPermi('system:relation:query')")
@GetMapping(value = "/{relationId}")
public AjaxResult getInfo(@PathVariable("relationId") Long relationId)
{
return AjaxResult.success(tUserRelationService.selectTUserRelationById(relationId));
}
/**
* 新增用户关联
*/
//@PreAuthorize("@ss.hasPermi('system:relation:add')")
@Log(title = "用户关联", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@RequestBody TUserRelation tUserRelation)
{
validateRelationUser(tUserRelation);
return toAjax(tUserRelationService.insertTUserRelation(tUserRelation));
}
/**
* 校验关联信息
* @param tUserRelation 关联信息
*/
private void validateRelationUser(TUserRelation tUserRelation){
SysUser sysUser = sysUserService.selectUserByUserName(tUserRelation.getAccount());
if (StringUtils.isNull(sysUser)){
throw new UsernameNotFoundException("登录用户:" + tUserRelation.getAccount() + " 不存在");
}
boolean isPass = SecurityUtils.matchesPassword(tUserRelation.getPassword(),sysUser.getPassword());
if(!isPass){
throw new BusinessException("密码错误");
}
TUserRelation relation = new TUserRelation();
relation.setRelationUserId(sysUser.getUserId());
relation.setUserId(tUserRelation.getUserId());
List<TUserRelation> relations = tUserRelationService.selectTUserRelationList(relation);
if(!CollectionUtils.isEmpty(relations)){
throw new BusinessException("关联账户已存在!");
}
if(tUserRelation.getUserId().equals(sysUser.getUserId())){
throw new BusinessException("不能关联当前账户");
}
tUserRelation.setRelationUserId(sysUser.getUserId());
}
/**
* 修改用户关联
*/
//@PreAuthorize("@ss.hasPermi('system:relation:edit')")
@Log(title = "用户关联", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody TUserRelation tUserRelation)
{
return toAjax(tUserRelationService.updateTUserRelation(tUserRelation));
}
/**
* 删除用户关联
*/
//@PreAuthorize("@ss.hasPermi('system:relation:remove')")
@Log(title = "用户关联", businessType = BusinessType.DELETE)
@DeleteMapping("/{relationIds}")
public AjaxResult remove(@PathVariable Long[] relationIds)
{
return toAjax(tUserRelationService.deleteTUserRelationByIds(relationIds));
}
}
......@@ -97,7 +97,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter
// 过滤请求
.authorizeRequests()
// 对于登录login 验证码captchaImage 允许匿名访问
.antMatchers("/login", "/captchaImage", "/webSocket/**").anonymous()
.antMatchers("/login","/changeLogin", "/captchaImage", "/webSocket/**").anonymous()
.antMatchers(
HttpMethod.GET,
"/*.html",
......
......@@ -6,6 +6,7 @@ import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;
import com.zehong.common.constant.Constants;
import com.zehong.common.core.domain.entity.SysUser;
......@@ -43,6 +44,9 @@ public class SysLoginService
@Autowired
private ISysUserService userService;
@Resource
private UserDetailsService userDetailsService;
/**
* 登录验证
*
......@@ -95,6 +99,24 @@ public class SysLoginService
return tokenService.createToken(loginUser);
}
/**
* 切换登录
* @param username 用户名
* @param password 密码
*/
public String changeLogin(String username,String password){
LoginUser loginUser = (LoginUser) userDetailsService.loadUserByUsername(username);
//校验密码
if(!loginUser.getUser().getPassword().equals(password)){
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.password.not.match")));
throw new UserPasswordNotMatchException();
}
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
recordLoginInfo(loginUser.getUser());
// 生成token
return tokenService.createToken(loginUser);
}
/**
* 记录登录信息
*/
......
package com.zehong.system.domain;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.zehong.common.annotation.Excel;
import com.zehong.common.core.domain.BaseEntity;
/**
* 用户关联对象 t_user_relation
*
* @author zehong
* @date 2023-07-11
*/
public class TUserRelation extends BaseEntity
{
private static final long serialVersionUID = 1L;
/** 主键id */
private Long relationId;
/** 用户id */
@Excel(name = "用户id")
private Long userId;
/** 关联用户id */
@Excel(name = "关联用户id")
private Long relationUserId;
/** 是否删除:0否,1是 */
@Excel(name = "是否删除:0否,1是")
private String isDel;
private String account;
private String password;
public void setRelationId(Long relationId)
{
this.relationId = relationId;
}
public Long getRelationId()
{
return relationId;
}
public void setUserId(Long userId)
{
this.userId = userId;
}
public Long getUserId()
{
return userId;
}
public void setRelationUserId(Long relationUserId)
{
this.relationUserId = relationUserId;
}
public Long getRelationUserId()
{
return relationUserId;
}
public void setIsDel(String isDel)
{
this.isDel = isDel;
}
public String getIsDel()
{
return isDel;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("relationId", getRelationId())
.append("userId", getUserId())
.append("relationUserId", getRelationUserId())
.append("createTime", getCreateTime())
.append("updateTime", getUpdateTime())
.append("isDel", getIsDel())
.append("remark", getRemark())
.toString();
}
}
package com.zehong.system.mapper;
import java.util.List;
import com.zehong.system.domain.TUserRelation;
/**
* 用户关联Mapper接口
*
* @author zehong
* @date 2023-07-11
*/
public interface TUserRelationMapper
{
/**
* 查询用户关联
*
* @param relationId 用户关联ID
* @return 用户关联
*/
public TUserRelation selectTUserRelationById(Long relationId);
/**
* 查询用户关联列表
*
* @param tUserRelation 用户关联
* @return 用户关联集合
*/
public List<TUserRelation> selectTUserRelationList(TUserRelation tUserRelation);
/**
* 新增用户关联
*
* @param tUserRelation 用户关联
* @return 结果
*/
public int insertTUserRelation(TUserRelation tUserRelation);
/**
* 修改用户关联
*
* @param tUserRelation 用户关联
* @return 结果
*/
public int updateTUserRelation(TUserRelation tUserRelation);
/**
* 删除用户关联
*
* @param relationId 用户关联ID
* @return 结果
*/
public int deleteTUserRelationById(Long relationId);
/**
* 批量删除用户关联
*
* @param relationIds 需要删除的数据ID
* @return 结果
*/
public int deleteTUserRelationByIds(Long[] relationIds);
}
package com.zehong.system.service;
import java.util.List;
import com.zehong.system.domain.TUserRelation;
/**
* 用户关联Service接口
*
* @author zehong
* @date 2023-07-11
*/
public interface ITUserRelationService
{
/**
* 查询用户关联
*
* @param relationId 用户关联ID
* @return 用户关联
*/
public TUserRelation selectTUserRelationById(Long relationId);
/**
* 查询用户关联列表
*
* @param tUserRelation 用户关联
* @return 用户关联集合
*/
public List<TUserRelation> selectTUserRelationList(TUserRelation tUserRelation);
/**
* 新增用户关联
*
* @param tUserRelation 用户关联
* @return 结果
*/
public int insertTUserRelation(TUserRelation tUserRelation);
/**
* 修改用户关联
*
* @param tUserRelation 用户关联
* @return 结果
*/
public int updateTUserRelation(TUserRelation tUserRelation);
/**
* 批量删除用户关联
*
* @param relationIds 需要删除的用户关联ID
* @return 结果
*/
public int deleteTUserRelationByIds(Long[] relationIds);
/**
* 删除用户关联信息
*
* @param relationId 用户关联ID
* @return 结果
*/
public int deleteTUserRelationById(Long relationId);
}
package com.zehong.system.service.impl;
import java.util.List;
import com.zehong.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zehong.system.mapper.TUserRelationMapper;
import com.zehong.system.domain.TUserRelation;
import com.zehong.system.service.ITUserRelationService;
/**
* 用户关联Service业务层处理
*
* @author zehong
* @date 2023-07-11
*/
@Service
public class TUserRelationServiceImpl implements ITUserRelationService
{
@Autowired
private TUserRelationMapper tUserRelationMapper;
/**
* 查询用户关联
*
* @param relationId 用户关联ID
* @return 用户关联
*/
@Override
public TUserRelation selectTUserRelationById(Long relationId)
{
return tUserRelationMapper.selectTUserRelationById(relationId);
}
/**
* 查询用户关联列表
*
* @param tUserRelation 用户关联
* @return 用户关联
*/
@Override
public List<TUserRelation> selectTUserRelationList(TUserRelation tUserRelation)
{
return tUserRelationMapper.selectTUserRelationList(tUserRelation);
}
/**
* 新增用户关联
*
* @param tUserRelation 用户关联
* @return 结果
*/
@Override
public int insertTUserRelation(TUserRelation tUserRelation)
{
tUserRelation.setCreateTime(DateUtils.getNowDate());
return tUserRelationMapper.insertTUserRelation(tUserRelation);
}
/**
* 修改用户关联
*
* @param tUserRelation 用户关联
* @return 结果
*/
@Override
public int updateTUserRelation(TUserRelation tUserRelation)
{
tUserRelation.setUpdateTime(DateUtils.getNowDate());
return tUserRelationMapper.updateTUserRelation(tUserRelation);
}
/**
* 批量删除用户关联
*
* @param relationIds 需要删除的用户关联ID
* @return 结果
*/
@Override
public int deleteTUserRelationByIds(Long[] relationIds)
{
return tUserRelationMapper.deleteTUserRelationByIds(relationIds);
}
/**
* 删除用户关联信息
*
* @param relationId 用户关联ID
* @return 结果
*/
@Override
public int deleteTUserRelationById(Long relationId)
{
return tUserRelationMapper.deleteTUserRelationById(relationId);
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zehong.system.mapper.TUserRelationMapper">
<resultMap type="TUserRelation" id="TUserRelationResult">
<result property="relationId" column="relation_id" />
<result property="userId" column="user_id" />
<result property="relationUserId" column="relation_user_id" />
<result property="createTime" column="create_time" />
<result property="updateTime" column="update_time" />
<result property="isDel" column="is_del" />
<result property="remark" column="remark" />
<result property="account" column="account"/>
<result property="password" column="password"/>
</resultMap>
<sql id="selectTUserRelationVo">
SELECT
re.relation_id,
re.user_id,
re.relation_user_id,
re.create_time,
re.update_time,
re.is_del,
re.remark,
us.user_name AS account,
us.password
FROM
t_user_relation re
LEFT JOIN sys_user us ON us.user_id = re.relation_user_id
</sql>
<select id="selectTUserRelationList" parameterType="TUserRelation" resultMap="TUserRelationResult">
<include refid="selectTUserRelationVo"/>
<where>
<if test="userId != null "> and re.user_id = #{userId}</if>
<if test="relationUserId != null "> and re.relation_user_id = #{relationUserId}</if>
<if test="isDel != null and isDel != ''"> and re.is_del = #{isDel}</if>
</where>
</select>
<select id="selectTUserRelationById" parameterType="Long" resultMap="TUserRelationResult">
<include refid="selectTUserRelationVo"/>
where re.relation_id = #{relationId}
</select>
<insert id="insertTUserRelation" parameterType="TUserRelation" useGeneratedKeys="true" keyProperty="relationId">
insert into t_user_relation
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="userId != null">user_id,</if>
<if test="relationUserId != null">relation_user_id,</if>
<if test="createTime != null">create_time,</if>
<if test="updateTime != null">update_time,</if>
<if test="isDel != null">is_del,</if>
<if test="remark != null">remark,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="userId != null">#{userId},</if>
<if test="relationUserId != null">#{relationUserId},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateTime != null">#{updateTime},</if>
<if test="isDel != null">#{isDel},</if>
<if test="remark != null">#{remark},</if>
</trim>
</insert>
<update id="updateTUserRelation" parameterType="TUserRelation">
update t_user_relation
<trim prefix="SET" suffixOverrides=",">
<if test="userId != null">user_id = #{userId},</if>
<if test="relationUserId != null">relation_user_id = #{relationUserId},</if>
<if test="createTime != null">create_time = #{createTime},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="isDel != null">is_del = #{isDel},</if>
<if test="remark != null">remark = #{remark},</if>
</trim>
where relation_id = #{relationId}
</update>
<delete id="deleteTUserRelationById" parameterType="Long">
delete from t_user_relation where relation_id = #{relationId}
</delete>
<delete id="deleteTUserRelationByIds" parameterType="String">
delete from t_user_relation where relation_id in
<foreach item="relationId" collection="array" open="(" separator="," close=")">
#{relationId}
</foreach>
</delete>
</mapper>
\ No newline at end of file
......@@ -15,6 +15,18 @@ export function login(username, password, code, uuid) {
})
}
export function changeLogin(username, password) {
const data = {
username,
password
}
return request({
url: '/changeLogin',
method: 'post',
data: data
})
}
// 获取用户详细信息
export function getInfo() {
return request({
......
import request from '@/utils/request'
// 查询用户关联列表
export function listRelation(query) {
return request({
url: '/user/relation/list',
method: 'get',
params: query
})
}
// 查询用户关联详细
export function getRelation(relationId) {
return request({
url: '/user/relation/' + relationId,
method: 'get'
})
}
// 新增用户关联
export function addRelation(data) {
return request({
url: '/user/relation',
method: 'post',
data: data
})
}
// 修改用户关联
export function updateRelation(data) {
return request({
url: '/user/relation',
method: 'put',
data: data
})
}
// 删除用户关联
export function delRelation(relationId) {
return request({
url: '/user/relation/' + relationId,
method: 'delete'
})
}
// 导出用户关联
export function exportRelation(query) {
return request({
url: '/user/relation/export',
method: 'get',
params: query
})
}
<template>
<div class="changeAccount">
<el-button type="text" @click="changeOpen=true">账户切换</el-button>
<!-- 关联账户管理 -->
<el-dialog title="关联账户"
class="relationAccount"
:visible.sync="changeOpen"
width="800px"
:close-on-click-modal="false"
append-to-body>
<el-button type="primary" style="margin-bottom: 10px" @click="addOpen = true">添加关联账号</el-button>
<el-table v-loading="loading" :data="accountList">
<el-table-column label="账户名称" align="center" prop="account" />
<el-table-column label="操作列" align="center">
<template slot-scope="scope">
<el-button type="text" @click="changeAccout(scope.row)">切换</el-button>
<el-button type="text" @click="deleteRelation(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</el-dialog>
<!-- 添加关联账户 -->
<el-dialog title="添加关联账户"
class="relationAccount"
:visible.sync="addOpen"
width="800px"
:close-on-click-modal="false"
append-to-body>
<el-form :model="accountForm" label-width="80px">
<el-row>
<el-col :span="12">
<el-form-item label="账号">
<el-input v-model="accountForm.account" placeholder="请输入账号"/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="密码">
<el-input v-model="accountForm.password" placeholder="请输入密码" show-password/>
</el-form-item>
</el-col>
</el-row>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="submitForm">确 定</el-button>
<el-button @click="cancel">取 消</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import { listRelation, addRelation, delRelation } from "@/api/userRelation/userRelation.js";
import Cookies from "js-cookie";
export default {
name: "changeAccount",
watch:{
changeOpen(newVal,oldVal){
if(newVal){
this.getList();
}
}
},
data(){
return{
changeOpen: false,
loading: false,
accountList: [],
addOpen: false,
accountForm: {
account: null,
password: null
}
}
},
methods:{
getList(){
listRelation({pageNum:1,pageSize:9999,userId:this.$store.state.user.userId}).then(res =>{
if(res.code == 200){
this.accountList = res.rows;
}
})
},
submitForm(){
this.accountForm.userId = this.$store.state.user.userId;
addRelation(this.accountForm).then(res =>{
if(res.code == 200){
this.addOpen = false;
this.getList();
this.$message.success("关联账户添加成功!");
}
})
},
cancel(){
this.reset();
this.addOpen = false;
},
reset(){
this.accountForm = {
account: null,
password: null
}
},
deleteRelation(row){
this.$confirm(
'是否确认删除账户为"' + row.account + '"的数据项?',
"警告",
{
confirmButtonText: "确定",
cancelButtonText: "取消",
type: "warning",
}
)
.then(function () {
return delRelation(row.relationId);
})
.then(() => {
this.getList();
this.msgSuccess("删除成功");
})
.catch(() => {});
},
changeAccout(row){
this.$store.dispatch('LogOut').then(() => {
//关闭websocket
this.$websocket.close();
Cookies.remove("username");
Cookies.remove("password");
Cookies.remove('rememberMe');
this.$store.dispatch("ChangeLogin", {username:row.account,password:row.password}).then(() => {
this.changeOpen = false;
//this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
window.location.reload();
})
})
},
}
}
</script>
<style lang="scss">
.changeAccount{
.relationAccount{
height: 800px;
overflow-y: auto;
&::-webkit-scrollbar {
/* 设置滚动条宽度 */
width: 4px;
/* 设置滚动条背景色 */
//background: black;
}
//滚动条轨道
&::-webkit-scrollbar-track {
background-color:transparent;
-webkit-border-radius: 2em;
-moz-border-radius: 2em;
border-radius:2em;
}
//滚动条滑块
&::-webkit-scrollbar-thumb {
background-color: rgb(147,147,153,0.5);
-webkit-border-radius: 2em;
-moz-border-radius: 2em;
border-radius:2em;
}
}
.el-button--text{
color: #606266;
}
}
</style>
......@@ -28,6 +28,9 @@
<router-link to="/user/profile">
<el-dropdown-item>个人中心</el-dropdown-item>
</router-link>
<el-dropdown-item>
<ChangeAccount/>
</el-dropdown-item>
<el-dropdown-item @click.native="setting = true">
<span>布局设置</span>
</el-dropdown-item>
......@@ -49,6 +52,7 @@ import Screenfull from '@/components/Screenfull'
import SizeSelect from '@/components/SizeSelect'
import Search from '@/components/HeaderSearch'
import Message from "./Message/Message";
import ChangeAccount from "./ChangeAccount/index";
export default {
components: {
Breadcrumb,
......@@ -57,7 +61,8 @@ export default {
Screenfull,
SizeSelect,
Search,
Message
Message,
ChangeAccount
},
computed: {
...mapGetters([
......
import { login, logout, getInfo } from '@/api/login'
import { login, logout, getInfo, changeLogin } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
const user = {
......@@ -122,7 +122,21 @@ const user = {
removeToken()
resolve()
})
}
},
// 切换登录
ChangeLogin({ commit }, userInfo) {
const username = userInfo.username.trim();
const password = userInfo.password
return new Promise((resolve, reject) => {
changeLogin(username, password).then(res => {
setToken(res.token)
commit('SET_TOKEN', res.token)
resolve()
}).catch(error => {
reject(error)
})
})
},
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment