This commit is contained in:
Ymnuk 2023-08-16 08:40:43 +03:00
commit aa9f582a08
5 changed files with 50 additions and 16 deletions

View File

@ -2,6 +2,12 @@
Утилита предназначена для генерирования некоторых частей кода на основе файла с описанием для облегчения и ускорения написания приложений.
Дополнительный компонент:
```
go install github.com/swaggo/swag/cmd/swag@latest
```
# Angular
## Генератор

View File

@ -54,10 +54,6 @@ func generateFrontendAngularTmpl() {
FileIn: "tmpl/frontend/angular/src/app/guard/auth.guard.ts.tmpl",
FileOut: filepath.Join(AppConfig.OutdirFrontend, "src", "app", "guard", "auth.guard.ts"),
},
{
FileIn: "tmpl/frontend/angular/src/app/service/auth.service.ts.tmpl",
FileOut: filepath.Join(AppConfig.OutdirFrontend, "src", "app", "service", "auth.service.ts"),
},
{
FileIn: "tmpl/frontend/angular/src/app/service/notify.service.ts.tmpl",
FileOut: filepath.Join(AppConfig.OutdirFrontend, "src", "app", "service", "notify.service.ts"),
@ -168,7 +164,7 @@ func generateFrontendAngularTmpl() {
},
{
FileIn: "tmpl/frontend/angular/options.json",
FileIn: "tmpl/frontend/angular/options.json.tmpl",
FileOut: filepath.Join(AppConfig.OutdirFrontend, "options.json"),
},
{
@ -225,6 +221,10 @@ func generateFrontendAngularTmpl() {
FileIn: "tmpl/frontend/angular/src/app/template/main/main.component.html.tmpl",
FileOut: filepath.Join(AppConfig.OutdirFrontend, "src", "app", "template", "main", "main.component.html"),
},
{
FileIn: "tmpl/frontend/angular/src/app/service/auth.service.ts.tmpl",
FileOut: filepath.Join(AppConfig.OutdirFrontend, "src", "app", "service", "auth.service.ts"),
},
}
for _, staticTmpls := range templs {
@ -234,6 +234,12 @@ func generateFrontendAngularTmpl() {
}
}
// Сервис аутентификации
/*err := templ.PrepareTmplFile("tmpl/frontend/angular/src/app/service/auth.service.ts.tmpl", Project, filepath.Join(AppConfig.OutdirFrontend, "src", "app", "service", "auth.service.ts"))
if err != nil {
log.Fatal(err)
}*/
/*templs = []structs.TmplInOut{
{
FileIn: "tmpl/frontend/angular/src/app/page/page.component.ts.tmpl",

View File

@ -13,6 +13,7 @@ func prepareRest(project *structs.Project) *structs.Project {
if project == nil {
log.Fatal("Пустой проект")
}
project.Backend.RestGroups = append(project.Backend.RestGroups, "users")
if len(project.Backend.Rest) > 0 {
// Обрабатываем REST для подготовки данных
for i := range project.Backend.Rest {
@ -118,6 +119,17 @@ func prepareRest(project *structs.Project) *structs.Project {
project.Backend.Rest[i].Edit[j].Name = findFieldNameInInterfaceById(project, project.Backend.Rest[i].Data.ID, project.Backend.Rest[i].Filter[j].ID)
}
}
// Собираем группы, чтобы они были уникальными
project.Backend.Rest[i].GroupName = templ.FieldNamePrepare(project.Backend.Rest[i].GroupName)
runes := []rune(project.Backend.Rest[i].GroupName)
runes[0] = []rune(strings.ToLower(string(runes[0])))[0]
project.Backend.Rest[i].GroupName = string(runes)
for _, grp := range project.Backend.RestGroups {
if grp == project.Backend.Rest[i].GroupName {
continue
}
}
project.Backend.RestGroups = append(project.Backend.RestGroups, project.Backend.Rest[i].GroupName)
}
}
return project

View File

@ -3,7 +3,7 @@ import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { Router } from '@angular/router';
import jwt_decode from "jwt-decode";
import { ModelUser, StructsResult, UsersService } from '../module/api';
import { ModelUser, StructsResult{{ range $index, $value := .Backend.RestGroups }}, {{ fieldNamePrepare $value }}Service {{ end }}} from '../module/api';
@Injectable({
providedIn: 'root'
@ -18,14 +18,18 @@ export class AuthService {
constructor(
private router: Router,
private userService: UsersService
{{ range $index, $value := .Backend.RestGroups }}
private {{ $value }}Service: {{ fieldNamePrepare $value }}Service,
{{ end }}
) {
if (localStorage.getItem('auth-token') != null) {
this._tkn = localStorage.getItem('auth-token');
if (this.userService.configuration.apiKeys == null) {
this.userService.configuration.apiKeys = {}
{{ range $index, $value := .Backend.RestGroups }}
if (this.{{ $value }}Service.configuration.apiKeys == null) {
this.{{ $value }}Service.configuration.apiKeys = {}
}
this.userService.configuration.apiKeys['Authorization'] = `Bearer ${this._tkn}`;
this.{{ $value }}Service.configuration.apiKeys['Authorization'] = `Bearer ${this._tkn}`;
{{ end }}
let jwt = jwt_decode(this.token as string);
if ((jwt as any).roles) {
@ -35,7 +39,7 @@ export class AuthService {
}
login(login: ModelUser): Observable<StructsResult> {
return this.userService.authLoginPost(login).pipe(
return this.usersService.authLoginPost(login).pipe(
tap(
(result: StructsResult) => {
localStorage.setItem('auth-token', result.data);
@ -55,10 +59,13 @@ export class AuthService {
set token(value: string | null) {
this._tkn = value;
if (this.userService.configuration.apiKeys == null) {
this.userService.configuration.apiKeys = {}
{{ range $index, $value := .Backend.RestGroups }}
if (this.{{ $value }}Service.configuration.apiKeys == null) {
this.{{ $value }}Service.configuration.apiKeys = {}
}
this.userService.configuration.apiKeys['Authorization'] = `Bearer ${value}`;
this.{{ $value }}Service.configuration.apiKeys['Authorization'] = `Bearer ${this._tkn}`;
{{ end }}
}
isAuthenticated(): boolean {
@ -69,9 +76,12 @@ export class AuthService {
logout() {
this.token = null;
localStorage.clear();
if (this.userService.configuration.apiKeys != null) {
delete this.userService.configuration.apiKeys['Authorization']
{{ range $index, $value := .Backend.RestGroups }}
if (this.{{ $value }}Service.configuration.apiKeys != null) {
delete this.{{ $value }}Service.configuration.apiKeys['Authorization']
}
{{ end }}
this.router.navigate(['/login']);
this._roles = null;
}