Skip to content

Commit

Permalink
修复退出登录失效问题
Browse files Browse the repository at this point in the history
  • Loading branch information
MonoLogueChi committed Mar 20, 2020
1 parent c0a0fe2 commit aaf7e3b
Show file tree
Hide file tree
Showing 17 changed files with 145 additions and 111 deletions.
10 changes: 3 additions & 7 deletions Danmu/Model/DataTable/DanmuTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,19 @@ public class DanmuTable
/// <summary>
/// Id
/// </summary>
[Key]
[Column("Id")]
[Key, Column("Id")]
public Guid Id { get; set; } = new Guid();

/// <summary>
/// 弹幕所在视频
/// </summary>
[Column("Vid")]
[Required]
[MaxLength(36)]
[Column("Vid"), Required, MaxLength(36)]
public string Vid { get; set; }

/// <summary>
/// 弹幕数据
/// </summary>
[Column("Data", TypeName = "jsonb")]
[Required]
[Column("Data", TypeName = "jsonb"), Required]
public BaseDanmuData Data { get; set; }

/// <summary>
Expand Down
4 changes: 1 addition & 3 deletions Danmu/Model/DataTable/HttpClientCacheTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ namespace Danmu.Model.DataTable
{
public class HttpClientCacheTable
{
[Key]
[Column("Id")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key, Column("Id"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

[Required]
Expand Down
16 changes: 5 additions & 11 deletions Danmu/Model/DataTable/UserTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,19 @@ public class UserTable
/// <summary>
/// 用户ID
/// </summary>
[Key]
[Column("Id")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key, Column("Id"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

/// <summary>
/// 用户名
/// </summary>
[Required]
[MaxLength(16)]
[MinLength(4)]
[Required, MaxLength(16), MinLength(4)]
public string Name { get; set; }

/// <summary>
/// 密码
/// </summary>
[Required]
[MinLength(6)]
[Required, MinLength(6)]
public string PassWord { get; set; }

/// <summary>
Expand All @@ -41,8 +36,7 @@ public class UserTable
/// <summary>
/// 用户角色
/// </summary>
[Required]
[DefaultValue(UserRole.GeneralUser)]
[Required, DefaultValue(UserRole.GeneralUser)]
public UserRole Role { get; set; } = UserRole.Guests;

/// <summary>
Expand All @@ -65,7 +59,7 @@ public class UserTable
/// 修改时间 UTC
/// </summary>
[Column(TypeName = "timestamp(3)")]
public DateTime UpDateTime { get; set; } = DateTime.UtcNow;
public DateTime UpdateTime { get; set; } = DateTime.UtcNow;
}

public enum UserRole
Expand Down
7 changes: 2 additions & 5 deletions Danmu/Model/DataTable/VideoTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@ public class VideoTable
/// <summary>
/// 主键 自增ID
/// </summary>
[Key]
[Column("Id")]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key, Column("Id"), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }

/// <summary>
/// 视频VID
/// </summary>
[Required]
[MaxLength(36)]
[Required, MaxLength(36)]
public string Vid { get; set; }

[Column(TypeName = "jsonb")] public Referer Referer { get; set; }
Expand Down
4 changes: 3 additions & 1 deletion Danmu/Model/DbContext/BaseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Text.Json;
using Danmu.Model.Config;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Debug;

namespace Danmu.Model.DbContext
{
Expand All @@ -18,4 +20,4 @@ public BaseContext(DbContextOptions options) : base(options)
Sql = settings.DanmuSql;
}
}
}
}
6 changes: 6 additions & 0 deletions Danmu/Model/DbContext/DanmuContext.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Threading.Tasks;
using Danmu.Model.DataTable;
using Microsoft.EntityFrameworkCore;

Expand All @@ -19,5 +20,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<HttpClientCacheTable>().HasIndex(h => h.Key).HasMethod("hash");
}

public async Task<int> ClearTable(string tableName)
{
return await Database.ExecuteSqlRawAsync($"TRUNCATE \"{tableName}\" RESTART IDENTITY;");
}
}
}
7 changes: 5 additions & 2 deletions Danmu/Model/DbContext/DbContextBuild.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.IO;
using Danmu.Model.Config;
using Danmu.Utils.Configuration;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Debug;

namespace Danmu.Model.DbContext
{
Expand All @@ -13,6 +13,9 @@ public DbContextBuild(AppConfiguration config, DbContextOptionsBuilder option)
sql.Port = sql.Port == 0 ? 5432 : sql.Port;
option.UseNpgsql(
$"Host={sql.Host};Port={sql.Port};Database={sql.DataBase};Username={sql.UserName};Password={sql.PassWord};");
#if DEBUG
option.UseLoggerFactory(new LoggerFactory(new[] { new DebugLoggerProvider() }));
#endif
}
}
}
1 change: 0 additions & 1 deletion Danmu/Utils/BiliBili/BiliBiliHelp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Danmu.Model.Danmu.BiliBili;
using Danmu.Utils.Configuration;
using Danmu.Utils.Dao;
using static Danmu.Utils.Global.VariableDictionary;

namespace Danmu.Utils.BiliBili
{
Expand Down
10 changes: 10 additions & 0 deletions Danmu/Utils/Dao/DbInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Danmu.Model.DataTable;
using Danmu.Model.DbContext;
using Danmu.Utils.Common;
using Microsoft.EntityFrameworkCore;

namespace Danmu.Utils.Dao
{
Expand All @@ -12,6 +13,15 @@ public static void Initialize(DanmuContext context, AppSettings appSettings)
{
context.Database.EnsureCreated();

//临时执行,修改以前的数据库
context.Database.ExecuteSqlRaw(
"CREATE TABLE IF NOT EXISTS \"HttpClientCache\" (\"Id\" int4 NOT NULL GENERATED BY DEFAULT AS IDENTITY ( INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 START 1 ),\t\"Key\" TEXT COLLATE \"pg_catalog\".\"default\" NOT NULL, \"Value\" bytea, \"TimeStamp\" int8 NOT NULL,\tCONSTRAINT \"PK_HttpClientCache\" PRIMARY KEY ( \"Id\" ) );");
context.Database.ExecuteSqlRaw(
"CREATE INDEX IF NOT EXISTS \"IX_HttpClientCache_Key\" ON \"HttpClientCache\" USING hash ( \"Key\" COLLATE \"pg_catalog\".\"default\" \"pg_catalog\".\"text_ops\" );");


context.Database.ExecuteSqlRaw("DO $$ BEGIN IF EXISTS(SELECT * FROM information_schema.columns WHERE table_name='User' and column_name='UpDateTime') THEN ALTER TABLE \"User\" RENAME COLUMN \"UpDateTime\" TO \"UpdateTime\"; END IF; END $$;");

if (!context.User.Any())
{
var admin = appSettings.Admin;
Expand Down
12 changes: 11 additions & 1 deletion Danmu/Utils/Dao/HttpClientCacheDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,22 @@ public async Task<byte[]> GetOrCreateCache(string key, TimeSpan expireTime, Func

var d = new HttpClientCacheTable
{
Key = key,
Key = key,
Value = await factory()
};
await _con.HttpClientCache.AddAsync(d);
await _con.SaveChangesAsync();
return d.Value;
}

/// <summary>
/// 清空缓存表
/// </summary>
/// <returns></returns>
public async Task<bool> ClearCacheAsync()
{
var r = _con.ClearTable(nameof(_con.HttpClientCache));
return await r > 0;
}
}
}
6 changes: 3 additions & 3 deletions Danmu/Utils/Dao/UserDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public async Task<bool> ChangePasswordAsync(int id, string oldP, string newP)
var r2 = await r1.FirstOrDefaultAsync();
r2.PassWord = Md5.GetMd5(newP, salt1);
r2.Salt = salt1;
r2.UpDateTime = DateTime.UtcNow;
r2.UpdateTime = DateTime.UtcNow;
_con.User.Update(r2);
return await _con.SaveChangesAsync() > 0;
}
Expand Down Expand Up @@ -131,7 +131,7 @@ public async Task<bool> ChangeUserInfoAsync(int id, string name = null, string e
r.Name = name ?? r.Name;
r.Email = email ?? r.Email;
r.PhoneNumber = phoneNumber ?? r.PhoneNumber;
r.UpDateTime = DateTime.UtcNow;
r.UpdateTime = DateTime.UtcNow;
_con.User.Update(r);
return await _con.SaveChangesAsync() > 0;
}
Expand Down Expand Up @@ -167,7 +167,7 @@ public async Task<UserTable> UserInfoAsync(int id)
Email = s.Email,
PhoneNumber = s.PhoneNumber,
CreateTime = s.CreateTime,
UpDateTime = s.UpDateTime,
UpdateTime = s.UpdateTime,
Role = s.Role
}).FirstOrDefaultAsync();
}
Expand Down
4 changes: 2 additions & 2 deletions Danmu/clientapp/src/api/admin/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export function logout() {
window.location.href = apiPrefix + baseUrl + '/logout'
}

export function getUserInfo(id) {
return request.get(baseUrl + '/user/user', { params: { id } }).then(({ data }) => data.data)
export function getUserInfo(uid) {
return request.get(baseUrl + '/user/user', { params: { uid } }).then(({ data }) => data.data)
}

export function changePwd({ uid, oldP, newP }) {
Expand Down
3 changes: 2 additions & 1 deletion Danmu/clientapp/src/store/modules/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import cookie from 'js-cookie'
import { cookieRoleKey } from '@/config'
import { login, logout } from '@/api/admin/account'
import { createMutations, isEmpty } from '@/utils'
import { getUser, setUser } from '@/utils/sessionStorage'
import { getUser, setUser, removeUser } from '@/utils/sessionStorage'

//刷新时从本地存储中获取用户信息
const user = getUser()
Expand Down Expand Up @@ -41,6 +41,7 @@ const actions = {
return new Promise((resolve, reject) => {
if (state.prepareLogout) return Promise.reject()
commit('setPrepareLogout', true)
removeUser()
logout()
/*logout(state.token)
.then(() => {
Expand Down
2 changes: 1 addition & 1 deletion Danmu/clientapp/src/utils/sessionStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export function setUser(user) {
sessionStorage.setItem(sessionUserKey, JSON.stringify(user))
}

function removeUser() {
export function removeUser() {
sessionStorage.removeItem(sessionUserKey)
}
2 changes: 1 addition & 1 deletion Danmu/clientapp/src/views/app/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
})
},
success() {
elSuccess('登陆成功')
elSuccess('登录成功')
let redirect = this.$route.query.redirect || '/'
window.location.href = routerMode === 'history' ? redirect : '/#' + redirect
}
Expand Down
Loading

0 comments on commit aaf7e3b

Please sign in to comment.