联调前端用户管理及角色管理

This commit is contained in:
橙子
2022-04-26 18:29:18 +08:00
parent 512e640c13
commit 7e427605ab
30 changed files with 239 additions and 81 deletions

Binary file not shown.

View File

@@ -155,6 +155,13 @@
用户管理
</summary>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.UserController.Add(Yi.Framework.Model.Models.UserEntity)">
<summary>
添加用户,去重,密码加密
</summary>
<param name="entity"></param>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.UserController.GiveUserSetRole(Yi.Framework.DTOModel.GiveUserSetRoleDto)">
<summary>
给多用户设置多角色
@@ -162,5 +169,11 @@
<param name="giveUserSetRoleDto"></param>
<returns></returns>
</member>
<member name="M:Yi.Framework.ApiMicroservice.Controllers.UserController.GetRoleListByUserId(System.Int64)">
<summary>
通过用户id得到角色列表
</summary>
<returns></returns>
</member>
</members>
</doc>

View File

@@ -34,7 +34,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
/// <returns></returns>
[Permission($"{nameof(T)}:get:one")]
[HttpGet]
public async Task<Result> GetById(long id)
public virtual async Task<Result> GetById(long id)
{
return Result.Success().SetData(await _repository.GetByIdAsync(id));
}
@@ -45,7 +45,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
/// <returns></returns>
[Permission($"{nameof(T)}:get:list")]
[HttpPost]
public async Task<Result> GetList(QueryCondition queryCondition)
public virtual async Task<Result> GetList(QueryCondition queryCondition)
{
return Result.Success().SetData(await _repository.GetListAsync(queryCondition));
}
@@ -57,7 +57,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
/// <returns></returns>
[Permission($"{nameof(T)}:get:page")]
[HttpPost]
public async Task<Result> PageList(QueryPageCondition queryCondition)
public virtual async Task<Result> PageList(QueryPageCondition queryCondition)
{
return Result.Success().SetData(await _repository.CommonPageAsync(queryCondition));
}
@@ -69,7 +69,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
/// <returns></returns>
[Permission($"{nameof(T)}:add")]
[HttpPost]
public async Task<Result> Add(T entity)
public virtual async Task<Result> Add(T entity)
{
return Result.Success().SetData(await _repository.InsertReturnSnowflakeIdAsync(entity));
}
@@ -81,7 +81,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
/// <returns></returns>
[Permission($"{nameof(T)}:update")]
[HttpPut]
public async Task<Result> Update(T entity)
public virtual async Task<Result> Update(T entity)
{
return Result.Success().SetStatus(await _repository.UpdateIgnoreNullAsync(entity));
}
@@ -93,7 +93,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
/// <returns></returns>
[Permission($"{nameof(T)}:delete:list")]
[HttpDelete]
public async Task<Result> DeleteList(List<long> ids)
public virtual async Task<Result> DeleteList(List<long> ids)
{
return Result.Success().SetStatus(await _repository.DeleteByLogicAsync(ids));
}

View File

@@ -39,5 +39,7 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{
return Result.Success().SetStatus(await _iRoleService.GiveRoleSetMenu(giveRoleSetMenuDto.RoleIds, giveRoleSetMenuDto.MenuIds));
}
}
}

View File

@@ -29,6 +29,23 @@ namespace Yi.Framework.ApiMicroservice.Controllers
_iUserService = iUserService;
}
/// <summary>
/// 添加用户,去重,密码加密
/// </summary>
/// <param name="entity"></param>
/// <returns></returns>
[Permission($"{nameof(UserEntity)}:add")]
[HttpPost]
public override async Task<Result> Add(UserEntity entity)
{
if (!await _iUserService.Exist(entity.UserName))
{
entity.BuildPassword();
return Result.Success().SetData(await _iUserService._repository.InsertReturnSnowflakeIdAsync(entity));
}
return Result.SuccessError("用户已存在");
}
/// <summary>
/// 给多用户设置多角色
/// </summary>
@@ -37,7 +54,18 @@ namespace Yi.Framework.ApiMicroservice.Controllers
[HttpPut]
public async Task<Result> GiveUserSetRole(GiveUserSetRoleDto giveUserSetRoleDto)
{
return Result.Success().SetStatus(await _iUserService.GiveUserSetRole(giveUserSetRoleDto.UserIds,giveUserSetRoleDto.RoleIds));
return Result.Success().SetStatus(await _iUserService.GiveUserSetRole(giveUserSetRoleDto.UserIds, giveUserSetRoleDto.RoleIds));
}
/// <summary>
/// 通过用户id得到角色列表
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<Result> GetRoleListByUserId(long userId)
{
return Result.Success().SetData(await _iUserService.GetRoleListByUserId(userId));
}
}
}

View File

@@ -44,5 +44,20 @@ namespace Yi.Framework.Interface
/// <param name="roleIds"></param>
/// <returns></returns>
Task<bool> GiveUserSetRole(List<long> userIds, List<long> roleIds);
/// <summary>
/// 判断用户名是否存在,如果存在可返回该用户
/// </summary>
/// <param name="userName"></param>
/// <param name="userAction"></param>
/// <returns></returns>
Task<bool> Exist(string userName, Action<UserEntity> userAction = null);
/// <summary>
/// 通过用户id得到角色列表
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
Task<List<RoleEntity>> GetRoleListByUserId(long userId);
}
}

View File

@@ -10,6 +10,6 @@ namespace Yi.Framework.Model.Models
public partial class MenuEntity
{
[SqlSugar.SugarColumn(IsIgnore = true)]
public List<MenuEntity> Childs { get; set; }
public List<MenuEntity> Children { get; set; }
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using SqlSugar;
namespace Yi.Framework.Model.Models
{
@@ -15,10 +16,8 @@ namespace Yi.Framework.Model.Models
this.IsDeleted = false;
this.CreateTime = DateTime.Now;
}
/// <summary>
/// 1
///</summary>
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
[JsonConverter(typeof(ValueToStringConverter))]
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
public long Id { get; set; }
/// <summary>
///

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using SqlSugar;
namespace Yi.Framework.Model.Models
{
@@ -15,10 +16,8 @@ namespace Yi.Framework.Model.Models
this.IsDeleted = false;
this.CreateTime = DateTime.Now;
}
/// <summary>
/// 1
///</summary>
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
[JsonConverter(typeof(ValueToStringConverter))]
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
public long Id { get; set; }
/// <summary>
///

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using SqlSugar;
namespace Yi.Framework.Model.Models
{
@@ -15,10 +16,8 @@ namespace Yi.Framework.Model.Models
this.IsDeleted = false;
this.CreateTime = DateTime.Now;
}
/// <summary>
/// 1
///</summary>
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
[JsonConverter(typeof(ValueToStringConverter))]
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
public long Id { get; set; }
/// <summary>
///

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using SqlSugar;
namespace Yi.Framework.Model.Models
{
@@ -15,10 +16,8 @@ namespace Yi.Framework.Model.Models
this.IsDeleted = false;
this.CreateTime = DateTime.Now;
}
/// <summary>
/// 1
///</summary>
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
[JsonConverter(typeof(ValueToStringConverter))]
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
public long Id { get; set; }
/// <summary>
///

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using SqlSugar;
namespace Yi.Framework.Model.Models
{
@@ -15,10 +16,10 @@ namespace Yi.Framework.Model.Models
this.IsDeleted = false;
this.CreateTime = DateTime.Now;
}
/// <summary>
///
///</summary>
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
[Newtonsoft.Json.JsonConverter(typeof(ValueToStringConverter))]
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
public long Id { get; set; }
/// <summary>
///
@@ -75,5 +76,35 @@ namespace Yi.Framework.Model.Models
///</summary>
[SugarColumn(ColumnName="Salt" )]
public string Salt { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName="Icon" )]
public long? Icon { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName="Nick" )]
public string Nick { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName="Email" )]
public string Email { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName="Ip" )]
public string Ip { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName="Address" )]
public string Address { get; set; }
/// <summary>
///
///</summary>
[SugarColumn(ColumnName="Phone" )]
public string Phone { get; set; }
}
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using SqlSugar;
namespace Yi.Framework.Model.Models
{
@@ -15,10 +16,8 @@ namespace Yi.Framework.Model.Models
this.IsDeleted = false;
this.CreateTime = DateTime.Now;
}
/// <summary>
/// 1
///</summary>
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
[JsonConverter(typeof(ValueToStringConverter))]
[SugarColumn(ColumnName="Id" ,IsPrimaryKey = true )]
public long Id { get; set; }
/// <summary>
///

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using SqlSugar;
namespace Yi.Framework.Model.Models
{
public partial class RoleEntity
{
//[Navigate(typeof(UserRoleEntity), nameof(UserRoleEntity.RoleId), nameof(UserRoleEntity.UserId))]
//public List<UserEntity> Users { get; set; }
}
}

View File

@@ -10,6 +10,21 @@ namespace Yi.Framework.Model.Models
/// 看好啦ORM精髓导航属性
///</summary>
[Navigate(typeof(UserRoleEntity), nameof(UserRoleEntity.UserId), nameof(UserRoleEntity.RoleId))]
public List<RoleEntity> Roles { get; set; }
public List<RoleEntity> Roles { get; set; }
/// <summary>
/// 构建密码MD5盐值加密
/// </summary>
public void BuildPassword(string password = null)
{
//如果不传值那就把自己的password当作传进来的password
if (password == null)
{
password = this.Password;
}
this.Salt = Common.Helper.MD5Helper.GenerateSalt();
this.Password = Common.Helper.MD5Helper.SHA2Encode(password, this.Salt);
}
}
}

View File

@@ -13,7 +13,7 @@ namespace Yi.Framework.Service
{
//ParentId 0,代表为根目录,只能存在一个
//复杂查询直接使用db代理
return await _repository._Db.Queryable<MenuEntity>().ToTreeAsync(it=>it.Childs,it=>it.ParentId,0);
return await _repository._Db.Queryable<MenuEntity>().ToTreeAsync(it=>it.Children,it=>it.ParentId,0);
}
}
}

View File

@@ -41,5 +41,6 @@ namespace Yi.Framework.Service
}
}
}

View File

@@ -15,7 +15,7 @@ namespace Yi.Framework.Service
{
return await _repository._Db.Queryable<UserEntity>().ToListAsync();
}
public async Task<bool> Exist(Guid id, Action<UserEntity> userAction = null)
public async Task<bool> Exist(long id, Action<UserEntity> userAction = null)
{
var user = await _repository.GetByIdAsync(id);
userAction.Invoke(user);
@@ -58,8 +58,7 @@ namespace Yi.Framework.Service
if (!await Exist(userEntity.UserName))
{
user.UserName = userEntity.UserName;
user.Salt = Common.Helper.MD5Helper.GenerateSalt();
user.Password = Common.Helper.MD5Helper.SHA2Encode(userEntity.Password, user.Salt);
user.BuildPassword();
userAction.Invoke(await _repository.InsertReturnEntityAsync(user));
return true;
}
@@ -79,23 +78,29 @@ namespace Yi.Framework.Service
return await _repositoryUserRole.UseTranAsync(async () =>
{
//遍历用户
foreach (var userId in userIds)
{
//删除用户之前所有的用户角色关系(物理删除,没有恢复的必要)
await _repositoryUserRole.DeleteAsync(u => u.UserId == userId);
//遍历用户
foreach (var userId in userIds)
{
//删除用户之前所有的用户角色关系(物理删除,没有恢复的必要)
await _repositoryUserRole.DeleteAsync(u => u.UserId == userId);
//添加新的关系
List<UserRoleEntity> userRoleEntities = new();
//添加新的关系
List<UserRoleEntity> userRoleEntities = new();
foreach (var roleId in roleIds)
{
userRoleEntities.Add(new UserRoleEntity() { UserId = userId, RoleId = roleId });
}
//一次性批量添加
await _repositoryUserRole.InsertRangeAsync(userRoleEntities);
//一次性批量添加
await _repositoryUserRole.InsertRangeAsync(userRoleEntities);
}
});
}
public async Task<List<RoleEntity>> GetRoleListByUserId(long userId)
{
return (await _repository._Db.Queryable<UserEntity>().Includes(u => u.Roles).InSingleAsync(userId)).Roles;
}
}
}

View File

@@ -1,4 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Serialization;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -15,6 +17,8 @@ namespace Yi.Framework.WebCore.BuilderExtend
{
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm";
//options.SerializerSettings.Converters.Add(new ValueToStringConverter());
});
}

View File

@@ -1080,6 +1080,11 @@
"integrity": "sha1-ZfCvOC9Xi83HQr2cKB6cstd2gyg=",
"dev": true
},
"bignumber.js": {
"version": "9.0.2",
"resolved": "https://registry.npmmirror.com/bignumber.js/-/bignumber.js-9.0.2.tgz",
"integrity": "sha512-GAcQvbpsM0pUb0zw1EI0KhQEZ+lRwR5fYaAp3vPOYuP7aDvGy6cVN6XHLauvF8SOga2y0dcLcjt3iQDTSEliyw=="
},
"binary-extensions": {
"version": "2.2.0",
"resolved": "https://registry.npm.taobao.org/binary-extensions/download/binary-extensions-2.2.0.tgz?cache=0&sync_timestamp=1610299293319&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fbinary-extensions%2Fdownload%2Fbinary-extensions-2.2.0.tgz",
@@ -4931,6 +4936,14 @@
"integrity": "sha1-peZUwuWi3rXyAdls77yoDA7y9RM=",
"dev": true
},
"json-bigint": {
"version": "1.0.0",
"resolved": "https://registry.npmmirror.com/json-bigint/-/json-bigint-1.0.0.tgz",
"integrity": "sha512-SiPv/8VpZuWbvLSMtTDU8hEfrZWg/mH/nV/b4o0CYbSxu1UIQPLdwKOCIyLQX+VIPO5vrLX3i8qtqFyhdPSUSQ==",
"requires": {
"bignumber.js": "^9.0.0"
}
},
"json-parse-better-errors": {
"version": "1.0.2",
"resolved": "https://registry.npm.taobao.org/json-parse-better-errors/download/json-parse-better-errors-1.0.2.tgz",

View File

@@ -9,6 +9,7 @@
"dependencies": {
"@mdi/font": "^6.6.96",
"axios": "^0.22.0",
"json-bigint": "^1.0.0",
"typeface-roboto": "^1.1.13",
"vue": "^2.6.11",
"vue-chartist": "^2.3.1",

View File

@@ -19,11 +19,6 @@ export default {
method: 'get'
})
},
GetRolesByUserId(userId) {
return myaxios({
url: `/Role/GetRolesByUserId?userId=${userId}`,
method: 'get'
})
},
}
}

View File

@@ -22,10 +22,10 @@ export default {
method: 'get'
})
},
GetAxiosByRouter(router) {
GetRoleListByUserId(userId) {
return myaxios({
url: `/User/GetAxiosByRouter?router=${router}`,
url: `/User/GetRoleListByUserId?userId=${userId}`,
method: 'get'
})
}
},
}

View File

@@ -3,7 +3,20 @@ export default {
getItem(url) {
return myaxios({
url: url,
method: 'get'
method: 'post',
data: {
parameters: [
{
key: "isDeleted",
value: "0",
type: 0
}
],
orderBys: [
"id"
]
}
})
},
addItem(url, data) {

View File

@@ -1,6 +1,7 @@
import axios from 'axios'
import store from '../store/index'
import vm from '../main'
import JsonBig from 'json-bigint'
// import VuetifyDialogPlugin from 'vuetify-dialog/nuxt/index';
const myaxios = axios.create({
// baseURL:'/'//
@@ -9,6 +10,13 @@ const myaxios = axios.create({
headers: {
'Authorization': 'Bearer ' + ""
},
//雪花id精度问题
transformResponse: [ data => {
const json = JsonBig({
storeAsString: true
})
return json.parse(data)
}],
})
// 请求拦截器
myaxios.interceptors.request.use(function(config) {

View File

@@ -12,17 +12,18 @@
</material-card>
</template>
<script>
import userApi from "../api/userApi"
export default {
created() {
this.init();
},
methods: {
init() {
userApi.GetAxiosByRouter(this.$route.path).then(resp=>{
this.axiosUrls=resp.data;
})
this.axiosUrls = {
get: "/role/GetList",
update: "/role/Update",
del: "/role/DeleteList",
add: "/role/Add",
};
}
},
data: () => ({
@@ -31,9 +32,9 @@ export default {
},
headers: [
{ text: "编号", align: "start", value: "id" },
{ text: "角色名", value: "role_name", sortable: false },
{ text: "云盘地址", value: "file_path", sortable: false },
{ text: "简介", value: "introduce", sortable: false },
{ text: "角色名", value: "roleName", sortable: false },
// { text: "云盘地址", value: "file_path", sortable: false },
// { text: "简介", value: "introduce", sortable: false },
{ text: "操作", value: "actions", sortable: false },
],
defaultItem: {

View File

@@ -27,7 +27,6 @@
</template>
<script>
import userApi from "../api/userApi";
import roleApi from "../api/roleApi";
export default {
created() {
this.init();
@@ -36,12 +35,14 @@ export default {
methods: {
async showItem(item) {
var strInfo = "";
roleApi.GetRolesByUserId(item.id).then(async (resp) => {
const roleData = resp.data;
userApi.GetRoleListByUserId(item.id).then(async (resp) => {
var roleData = resp.data;
strInfo += "拥有的角色:<br>";
roleData.forEach((u) => {
strInfo += u.role_name + "<br>";
});
if (roleData != null) {
roleData.forEach((u) => {
strInfo += u.roleName + "<br>";
});
}
strInfo += "<hr>";
Object.keys(item).forEach(async function (key) {
@@ -58,12 +59,16 @@ export default {
});
},
init() {
userApi.GetAxiosByRouter(this.$route.path).then((resp) => {
this.axiosUrls = resp.data;
});
roleApi.getRole().then((resp) => {
this.roleItems = resp.data;
});
this.axiosUrls = {
get: "/user/GetList",
update: "/user/Update",
del: "/user/DeleteList",
add: "/user/Add",
};
// roleApi.getRole().then((resp) => {
// this.roleItems = resp.data;
// });
},
setRole() {
var userIds = [];
@@ -95,8 +100,7 @@ export default {
roleItems: [],
axiosUrls: {},
headers: [
{ text: "编号", align: "start", value: "id" },
{ text: "用户名", value: "username", sortable: false },
{ text: "用户名", value: "userName", sortable: false },
{ text: "密码", value: "password", sortable: false },
{ text: "图标", value: "icon", sortable: false },
{ text: "昵称", value: "nick", sortable: true },
@@ -110,11 +114,11 @@ export default {
defaultItem: {
username: "test",
password: "123",
icon: "mdi-lock",
icon: "",
nick: "橙子",
age: 18,
address: "中国",
phone: "",
phone: "123456789",
},
}),
};