web-storage is added
This commit is contained in:
parent
d80ff7368a
commit
a2faa116c7
|
@ -17,6 +17,7 @@ import { AppComponent } from 'app/app.component';
|
||||||
import { appRoutes } from 'app/app.routing';
|
import { appRoutes } from 'app/app.routing';
|
||||||
|
|
||||||
import { NatsModule } from 'app/core/nats/nats.module';
|
import { NatsModule } from 'app/core/nats/nats.module';
|
||||||
|
import { WebStorageModule } from 'app/core/web-storage/web-storage.module';
|
||||||
|
|
||||||
import { environment } from 'environments/environment';
|
import { environment } from 'environments/environment';
|
||||||
|
|
||||||
|
@ -50,6 +51,7 @@ const routerConfig: ExtraOptions = {
|
||||||
MatIconModule,
|
MatIconModule,
|
||||||
|
|
||||||
NatsModule.forRoot(environment.nats),
|
NatsModule.forRoot(environment.nats),
|
||||||
|
WebStorageModule.forRoot({}),
|
||||||
|
|
||||||
MemberModule.forRoot(),
|
MemberModule.forRoot(),
|
||||||
],
|
],
|
||||||
|
|
1
src/app/core/web-storage/config/module-config.ts
Normal file
1
src/app/core/web-storage/config/module-config.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export interface ModuleConfig {}
|
5
src/app/core/web-storage/config/token.ts
Normal file
5
src/app/core/web-storage/config/token.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import { InjectionToken } from '@angular/core';
|
||||||
|
|
||||||
|
export const _MODULE_CONFIG = new InjectionToken(
|
||||||
|
'@bet/beteran web-storage config of module'
|
||||||
|
);
|
9
src/app/core/web-storage/services/index.ts
Normal file
9
src/app/core/web-storage/services/index.ts
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
import { Type } from '@angular/core';
|
||||||
|
|
||||||
|
import { WebLocalStorageService } from './web-local-storage.service';
|
||||||
|
import { WebSessionStorageService } from './web-session-storage.service';
|
||||||
|
|
||||||
|
export const SERVICES: Type<any>[] = [
|
||||||
|
WebLocalStorageService,
|
||||||
|
WebSessionStorageService,
|
||||||
|
];
|
|
@ -0,0 +1,25 @@
|
||||||
|
import { Inject, Injectable } from '@angular/core';
|
||||||
|
|
||||||
|
import { ModuleConfig } from '../config/module-config';
|
||||||
|
import { _MODULE_CONFIG } from '../config/token';
|
||||||
|
import { WebStorageService } from './web-storage.service';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class WebLocalStorageService extends WebStorageService {
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
super(localStorage);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
// @ Accessors
|
||||||
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
// @ Public methods
|
||||||
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
import { Inject, Injectable } from '@angular/core';
|
||||||
|
|
||||||
|
import { ModuleConfig } from '../config/module-config';
|
||||||
|
import { _MODULE_CONFIG } from '../config/token';
|
||||||
|
import { WebStorageService } from './web-storage.service';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class WebSessionStorageService extends WebStorageService {
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
constructor() {
|
||||||
|
super(sessionStorage);
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
// @ Accessors
|
||||||
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
// @ Public methods
|
||||||
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
}
|
87
src/app/core/web-storage/services/web-storage.service.ts
Normal file
87
src/app/core/web-storage/services/web-storage.service.ts
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
import { BehaviorSubject, Observable } from 'rxjs';
|
||||||
|
|
||||||
|
import { ModuleConfig } from '../config/module-config';
|
||||||
|
import { _MODULE_CONFIG } from '../config/token';
|
||||||
|
|
||||||
|
export abstract class WebStorageService {
|
||||||
|
private __storage: Storage;
|
||||||
|
private __subjects: Map<string, BehaviorSubject<any>>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor
|
||||||
|
*/
|
||||||
|
constructor(storage: Storage) {
|
||||||
|
this.__storage = storage;
|
||||||
|
this.__subjects = new Map<string, BehaviorSubject<any>>();
|
||||||
|
}
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
// @ Accessors
|
||||||
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
// @ Public methods
|
||||||
|
// -----------------------------------------------------------------------------------------------------
|
||||||
|
/**
|
||||||
|
* watch data of given key
|
||||||
|
* @param key
|
||||||
|
* @param defaultValue
|
||||||
|
*/
|
||||||
|
watch(key: string, defaultValue: any = null): Observable<any> {
|
||||||
|
if (!this.__subjects.has(key)) {
|
||||||
|
this.__subjects.set(key, new BehaviorSubject<any>(defaultValue));
|
||||||
|
}
|
||||||
|
let item = this.__storage.getItem(key);
|
||||||
|
if (item !== null) {
|
||||||
|
item = JSON.parse(item);
|
||||||
|
}
|
||||||
|
(this.__subjects.get(key) as BehaviorSubject<any>).next(item);
|
||||||
|
return (this.__subjects.get(key) as BehaviorSubject<any>).asObservable();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get data of given key
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
|
get(key: string): any {
|
||||||
|
let item = this.__storage.getItem(key);
|
||||||
|
if (item !== null) {
|
||||||
|
item = JSON.parse(item);
|
||||||
|
}
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set value on given key
|
||||||
|
* @param key
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
|
set(key: string, value: any) {
|
||||||
|
this.__storage.setItem(key, JSON.stringify(value));
|
||||||
|
if (!this.__subjects.has(key)) {
|
||||||
|
this.__subjects.set(key, new BehaviorSubject<any>(value));
|
||||||
|
} else {
|
||||||
|
(this.__subjects.get(key) as BehaviorSubject<any>).next(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* remove given key
|
||||||
|
* @param key
|
||||||
|
*/
|
||||||
|
remove(key: string) {
|
||||||
|
if (this.__subjects.has(key)) {
|
||||||
|
(this.__subjects.get(key) as BehaviorSubject<any>).complete();
|
||||||
|
this.__subjects.delete(key);
|
||||||
|
}
|
||||||
|
this.__storage.removeItem(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* clear all available keys
|
||||||
|
*/
|
||||||
|
clear() {
|
||||||
|
this.__subjects.clear();
|
||||||
|
this.__storage.clear();
|
||||||
|
}
|
||||||
|
}
|
20
src/app/core/web-storage/web-storage.module.ts
Normal file
20
src/app/core/web-storage/web-storage.module.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import { ModuleWithProviders, NgModule } from '@angular/core';
|
||||||
|
import { ModuleConfig } from './config/module-config';
|
||||||
|
import { _MODULE_CONFIG } from './config/token';
|
||||||
|
|
||||||
|
import { SERVICES } from './services';
|
||||||
|
|
||||||
|
@NgModule({})
|
||||||
|
export class WebStorageRootModule {}
|
||||||
|
|
||||||
|
@NgModule({})
|
||||||
|
export class WebStorageModule {
|
||||||
|
public static forRoot(
|
||||||
|
config: ModuleConfig
|
||||||
|
): ModuleWithProviders<WebStorageRootModule> {
|
||||||
|
return {
|
||||||
|
ngModule: WebStorageRootModule,
|
||||||
|
providers: [{ provide: _MODULE_CONFIG, useValue: config }, ...SERVICES],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -198,10 +198,7 @@ export class ListComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||||
__onClickSearch(): void {
|
__onClickSearch(): void {
|
||||||
this.__isSearchOpened = !this.__isSearchOpened;
|
this.__isSearchOpened = !this.__isSearchOpened;
|
||||||
|
|
||||||
console.log(
|
console.log('kkkk', this._identityService.captcha());
|
||||||
'kkkk',
|
|
||||||
this._identityService.checkUsernameForDuplication('sdfsdfsdfsf')
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -2,11 +2,18 @@ import { Injectable } from '@angular/core';
|
||||||
import { NatsService } from 'app/core/nats/services/nats.service';
|
import { NatsService } from 'app/core/nats/services/nats.service';
|
||||||
import * as nats from 'nats.ws';
|
import * as nats from 'nats.ws';
|
||||||
|
|
||||||
|
import { Error } from 'app/modules/protobuf/protobuf/rpc/error_pb';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
CheckUsernameForDuplicationRequest,
|
CheckUsernameForDuplicationRequest,
|
||||||
CheckUsernameForDuplicationResponse,
|
CheckUsernameForDuplicationResponse,
|
||||||
|
CaptchaRequest,
|
||||||
|
CaptchaResponse,
|
||||||
} from 'app/modules/protobuf/c2se/common/identity_pb';
|
} from 'app/modules/protobuf/c2se/common/identity_pb';
|
||||||
import { SUBJECT_CHECK_USERNAME_FOR_DUPLICATION } from 'app/modules/protobuf/c2se/backend/identity_pb';
|
import {
|
||||||
|
SUBJECT_CHECK_USERNAME_FOR_DUPLICATION,
|
||||||
|
SUBJECT_CAPTCHA,
|
||||||
|
} from 'app/modules/protobuf/c2se/backend/identity_pb';
|
||||||
|
|
||||||
@Injectable({
|
@Injectable({
|
||||||
providedIn: 'root',
|
providedIn: 'root',
|
||||||
|
@ -39,8 +46,27 @@ export class IdentityService {
|
||||||
.then((result) => {
|
.then((result) => {
|
||||||
console.log('success', result, result.getDuplicated());
|
console.log('success', result, result.getDuplicated());
|
||||||
})
|
})
|
||||||
.catch((e) => {
|
.catch((e: Error) => {
|
||||||
console.log('failed', e);
|
console.log('failed', e.getCode(), e.getMessage(), e.getData());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
captcha(): Promise<boolean> {
|
||||||
|
return new Promise<boolean>((resolve, reject) => {
|
||||||
|
let req = new CaptchaRequest();
|
||||||
|
|
||||||
|
this.__natsService
|
||||||
|
.request<CaptchaResponse.Result>(
|
||||||
|
SUBJECT_CAPTCHA,
|
||||||
|
req.serializeBinary(),
|
||||||
|
CaptchaResponse.deserializeBinary
|
||||||
|
)
|
||||||
|
.then((result) => {
|
||||||
|
console.log('success', result, result.getToken(), result.getImage());
|
||||||
|
})
|
||||||
|
.catch((e: Error) => {
|
||||||
|
console.log('failed', e.getCode(), e.getMessage(), e.getData());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user