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; 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) { // setUser(user: User) {
// // TODO: 简单加个密 // // TODO: 简单加个密
// localStorage.setItem('t', new Date().valueOf().toString()); // localStorage.setItem('t', new Date().valueOf().toString());

View File

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

View File

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

View File

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

View File

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