Merge branch 'feature/BETERAN-BACKEND-APP-BROWSER-init' of https://gitlab.loafle.net/bet/beteran-backend-app-browser into feature/BETERAN-BACKEND-APP-BROWSER-init
This commit is contained in:
commit
a512fe9b17
|
@ -17,6 +17,7 @@ import { AppComponent } from 'app/app.component';
|
|||
import { appRoutes } from 'app/app.routing';
|
||||
|
||||
import { NatsModule } from 'app/core/nats/nats.module';
|
||||
import { WebStorageModule } from 'app/core/web-storage/web-storage.module';
|
||||
|
||||
import { environment } from 'environments/environment';
|
||||
|
||||
|
@ -50,6 +51,7 @@ const routerConfig: ExtraOptions = {
|
|||
MatIconModule,
|
||||
|
||||
NatsModule.forRoot(environment.nats),
|
||||
WebStorageModule.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 {
|
||||
this.__isSearchOpened = !this.__isSearchOpened;
|
||||
|
||||
console.log(
|
||||
'kkkk',
|
||||
this._identityService.checkUsernameForDuplication('sdfsdfsdfsf')
|
||||
);
|
||||
console.log('kkkk', this._identityService.captcha());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -2,11 +2,18 @@ import { Injectable } from '@angular/core';
|
|||
import { NatsService } from 'app/core/nats/services/nats.service';
|
||||
import * as nats from 'nats.ws';
|
||||
|
||||
import { Error } from 'app/modules/protobuf/protobuf/rpc/error_pb';
|
||||
|
||||
import {
|
||||
CheckUsernameForDuplicationRequest,
|
||||
CheckUsernameForDuplicationResponse,
|
||||
CaptchaRequest,
|
||||
CaptchaResponse,
|
||||
} 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({
|
||||
providedIn: 'root',
|
||||
|
@ -39,8 +46,27 @@ export class IdentityService {
|
|||
.then((result) => {
|
||||
console.log('success', result, result.getDuplicated());
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log('failed', e);
|
||||
.catch((e: Error) => {
|
||||
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