refactor: 用LocalStorageService代理localStorage

This commit is contained in:
禾几海
2021-03-16 20:38:42 +08:00
parent a2e80e6fdd
commit 2ba3d4ca1b
5 changed files with 36 additions and 18 deletions

View File

@@ -39,6 +39,18 @@ export class LocalStorageService {
return this.getToken() != null;
}
setItem(key: string, value: any) {
localStorage.setItem(key, JSON.stringify(value));
}
getItem(key: string) {
return localStorage.getItem(key);
}
removeItem(key: string) {
localStorage.removeItem(key);
}
// setUser(user: User) {
// // TODO: 简单加个密
// localStorage.setItem('t', new Date().valueOf().toString());

View File

@@ -3,6 +3,7 @@ import {Data} from './data';
import {PageList, RequestObj} from '../../../../class/HttpReqAndResp';
import {HttpService} from '../../../../api/http/http.service';
import {CdkDragDrop, moveItemInArray} from '@angular/cdk/drag-drop';
import {LocalStorageService} from '../../../../services/local-storage.service';
@Component({
selector: 'common-table',
@@ -28,13 +29,14 @@ export class CommonTableComponent<T> implements OnInit, OnChanges {
changed: boolean = false;
visibleFieldLength: number = 0;
constructor(private httpService: HttpService) {
constructor(private httpService: HttpService,
private localStorageService: LocalStorageService) {
}
ngOnInit(): void {
if (localStorage.getItem(this.request.path)) {
this.filedData = this.cloneData(localStorage.getItem(this.request.path));
if (this.localStorageService.getItem(this.request.path)) {
this.filedData = this.cloneData(this.localStorageService.getItem(this.request.path));
this.changed = true;
} else {
this.filedData = this.cloneData(this.headData);
@@ -132,7 +134,7 @@ export class CommonTableComponent<T> implements OnInit, OnChanges {
return;
}
this.dataList = JSON.parse(JSON.stringify(this.dataList));
localStorage.setItem(this.request.path, JSON.stringify(this.filedData));
this.localStorageService.setItem(this.request.path, JSON.stringify(this.filedData));
this.changed = true;
}
@@ -142,7 +144,7 @@ export class CommonTableComponent<T> implements OnInit, OnChanges {
}
reset = () => {
localStorage.removeItem(this.request.path);
this.localStorageService.removeItem(this.request.path);
this.filedData = this.cloneData(this.headData);
this.changed = false;
this.calculateVisibleFieldLength();

View File

@@ -5,6 +5,7 @@ import {ActivatedRoute, Router} from '@angular/router';
import {LoginRegistrationService} from '../../service/login-registration.service';
import {Title} from '@angular/platform-browser';
import {GlobalUserService} from '../../../../services/global-user.service';
import {LocalStorageService} from '../../../../services/local-storage.service';
@Component({
selector: 'c-login',
@@ -26,16 +27,17 @@ export class LoginComponent implements OnInit {
private activatedRoute: ActivatedRoute,
private router: Router,
private loginRegistrationService: LoginRegistrationService,
private title: Title) {
private title: Title,
private localStorageService: LocalStorageService) {
this.title.setTitle('小海博客 | 登录 ');
}
ngOnInit() {
this.url = this.activatedRoute.snapshot.queryParamMap.get('url');
this.loginReq.email = localStorage.getItem('e');
this.loginReq.password = localStorage.getItem('p');
localStorage.removeItem('e');
localStorage.removeItem('p');
this.loginReq.email = this.localStorageService.getItem('e');
this.loginReq.password = this.localStorageService.getItem('p');
this.localStorageService.removeItem('e');
this.localStorageService.removeItem('p');
}
doLogin() {

View File

@@ -6,6 +6,7 @@ import {Router} from '@angular/router';
import {RequestObj} from '../../../../class/HttpReqAndResp';
import {LoginReq} from '../../../../class/User';
import {Title} from '@angular/platform-browser';
import {LocalStorageService} from '../../../../services/local-storage.service';
@Component({
selector: 'c-registration',
@@ -29,7 +30,8 @@ export class RegistrationComponent implements OnInit {
constructor(private apiService: ApiService,
private nzMessageService: NzMessageService,
private router: Router,
private title: Title) {
private title: Title,
private localStorageService: LocalStorageService) {
this.title.setTitle('小海博客 | 注册');
}
@@ -74,8 +76,8 @@ export class RegistrationComponent implements OnInit {
this.apiService.verifyImgCode(this.imgCode).subscribe(data => {
// 验证成功 注册
this.apiService.registration(this.email, this.pwd).subscribe(regData => {
localStorage.setItem('e', this.email);
localStorage.setItem('p', this.pwd);
this.localStorageService.setItem('e', this.email);
this.localStorageService.setItem('p', this.pwd);
this.email = '';
this.pwd = '';
this.imgCode = '';

View File

@@ -119,7 +119,7 @@ export class WriteComponent implements OnInit, OnDestroy {
next: data => {
// TODO 成功
this.message.success('发布成功,即将转跳');
localStorage.removeItem('tmpArticle');
this.localStorageService.removeItem('tmpArticle');
setTimeout(() => {
this.router.navigateByUrl('article/' + data.result.id);
@@ -143,7 +143,7 @@ export class WriteComponent implements OnInit, OnDestroy {
this.apiService.updateArticle(this.article).subscribe({
next: data => {
this.message.success('更新成功,即将转跳');
localStorage.removeItem('tmpArticle');
this.localStorageService.removeItem('tmpArticle');
setTimeout(() => {
this.router.navigateByUrl('article/' + data.result.id);
}, 2500);
@@ -253,8 +253,8 @@ export class WriteComponent implements OnInit, OnDestroy {
this.isUpdate = true;
this.getArticle();
}
if (!this.articleId && localStorage.getItem('tmpArticle')) {
this.article = JSON.parse(localStorage.getItem('tmpArticle'));
if (!this.articleId && this.localStorageService.getItem('tmpArticle')) {
this.article = JSON.parse(this.localStorageService.getItem('tmpArticle'));
}
}
};