parent
93c75399a4
commit
f5530d4259
@ -0,0 +1,39 @@ |
||||
<!--设置高度,添加bootstrap样式类d-flex align-items-center 使表单垂直居中,justify-content-center水平居中--> |
||||
<div [style]="height" class="d-flex align-items-center justify-content-center"> |
||||
<form [formGroup]="noticeForm" class="col-6"> |
||||
<!-- 公告标题--> |
||||
<div class="input-group mb-3 mx-auto col-7"> |
||||
<div class="input-group-prepend"> |
||||
<span class="input-group-text">{{ 'forum.notice_title' | translate }}</span> |
||||
</div> |
||||
<input type="text" class="form-control text-center" [class.is-invalid]="getValue('title').invalid" |
||||
[placeholder]="'tip.input' | translate:{value:'forum.notice_title'|translate}" |
||||
formControlName="title"/> |
||||
<div class="invalid-feedback font-weight-bold text-center"> |
||||
{{'tip.notnull' | translate:{value: 'forum.notice_title'|translate} }} |
||||
</div> |
||||
</div> |
||||
|
||||
<!-- 公告内容--> |
||||
<div class="input-group mb-3 mx-auto col-7"> |
||||
<div class="input-group-prepend"> |
||||
<span class="input-group-text">{{ 'forum.notice_title' | translate }}</span> |
||||
</div> |
||||
<textarea class="form-control" [class.is-invalid]="getValue('content').invalid" |
||||
[placeholder]="'tip.input' | translate:{value:'forum.notice_content'|translate}" |
||||
formControlName="content"></textarea> |
||||
<div class="invalid-feedback font-weight-bold text-center"> |
||||
{{'tip.notnull' | translate:{value: 'forum.notice_content'|translate} }} |
||||
</div> |
||||
</div> |
||||
|
||||
<div class="col-7 mx-auto text-center mb-3"> |
||||
<!-- 提交按钮--> |
||||
<button type="button" (click)="addNotice()" [disabled]="form().invalid" |
||||
class="btn btn-primary btn-lg">{{ 'button.notice' | translate }}</button> |
||||
<!-- 返回论坛主界面--> |
||||
<button type="button" [routerLink]="['/forum']" |
||||
class="btn btn-info btn-lg ml-3">{{ 'button.back' | translate }}</button> |
||||
</div> |
||||
</form> |
||||
</div> |
@ -0,0 +1,25 @@ |
||||
import {async, ComponentFixture, TestBed} from '@angular/core/testing'; |
||||
|
||||
import {NoticeComponent} from './notice.component'; |
||||
|
||||
describe('NoticeComponent', () => { |
||||
let component: NoticeComponent; |
||||
let fixture: ComponentFixture<NoticeComponent>; |
||||
|
||||
beforeEach(async(() => { |
||||
TestBed.configureTestingModule({ |
||||
declarations: [NoticeComponent] |
||||
}) |
||||
.compileComponents(); |
||||
})); |
||||
|
||||
beforeEach(() => { |
||||
fixture = TestBed.createComponent(NoticeComponent); |
||||
component = fixture.componentInstance; |
||||
fixture.detectChanges(); |
||||
}); |
||||
|
||||
it('should create', () => { |
||||
expect(component).toBeTruthy(); |
||||
}); |
||||
}); |
@ -0,0 +1,67 @@ |
||||
import {Component, OnInit} from '@angular/core'; |
||||
import {Commons} from '../../commons'; |
||||
import {FormBuilder, FormGroup, Validators} from '@angular/forms'; |
||||
import {NoticeService} from './notice.service'; |
||||
import {Result} from '../../interface/Result'; |
||||
import {MessageInterface, MessageUtil} from '../../message/message.service'; |
||||
import {CookieService} from 'ngx-cookie-service'; |
||||
import {environment} from '../../../environments/environment'; |
||||
|
||||
@Component({ |
||||
selector: 'app-notice', |
||||
templateUrl: './notice.component.html', |
||||
styleUrls: ['./notice.component.scss'] |
||||
}) |
||||
export class NoticeComponent extends Commons implements OnInit, MessageInterface { |
||||
|
||||
// 公告表单
|
||||
noticeForm = this.fb.group({ |
||||
// 公告标题
|
||||
title: this.fb.control('', [Validators.required]), |
||||
// 公告内容
|
||||
content: this.fb.control('', [Validators.required]) |
||||
}); |
||||
|
||||
managerName = ''; |
||||
|
||||
constructor( |
||||
private fb: FormBuilder, |
||||
private noticeService: NoticeService, |
||||
private messageUtil: MessageUtil, |
||||
private cookieService: CookieService |
||||
) { |
||||
super(); |
||||
} |
||||
|
||||
// 添加公告
|
||||
addNotice() { |
||||
this.noticeService.addNotice(Object.assign( |
||||
this.noticeForm.value, {author: this.cookieService.get(environment.managerKey)} |
||||
)).subscribe(res => { |
||||
if (res.result === Result.OK) { |
||||
location.href = '/forum'; |
||||
} else { |
||||
this.messageUtil.danger(res.message); |
||||
} |
||||
}); |
||||
} |
||||
|
||||
ngOnInit(): void { |
||||
this.validForm = {}; |
||||
for (const key of Object.keys(this.noticeForm.value)) { |
||||
this.validForm[key] = { |
||||
flag: false |
||||
}; |
||||
} |
||||
} |
||||
|
||||
form(): FormGroup { |
||||
return this.noticeForm; |
||||
} |
||||
|
||||
prefix(key: string): string { |
||||
return 'server.forum.notice.'; |
||||
} |
||||
|
||||
|
||||
} |
@ -0,0 +1,16 @@ |
||||
import {TestBed} from '@angular/core/testing'; |
||||
|
||||
import {NoticeService} from './notice.service'; |
||||
|
||||
describe('NoticeService', () => { |
||||
let service: NoticeService; |
||||
|
||||
beforeEach(() => { |
||||
TestBed.configureTestingModule({}); |
||||
service = TestBed.inject(NoticeService); |
||||
}); |
||||
|
||||
it('should be created', () => { |
||||
expect(service).toBeTruthy(); |
||||
}); |
||||
}); |
@ -0,0 +1,53 @@ |
||||
import {Injectable} from '@angular/core'; |
||||
import {JSONRequest} from '../../interface/JSONRequest'; |
||||
import {HttpClient} from '@angular/common/http'; |
||||
import {Observable} from 'rxjs'; |
||||
import {ForumNoticeResponse} from '../../interface/Response'; |
||||
import {HttpInterface} from '../../interface/HttpInterface'; |
||||
import {catchError} from 'rxjs/operators'; |
||||
|
||||
@Injectable({ |
||||
providedIn: 'root' |
||||
}) |
||||
/** |
||||
* 公告服务 |
||||
*/ |
||||
export class NoticeService extends JSONRequest { |
||||
|
||||
constructor( |
||||
private http: HttpClient |
||||
) { |
||||
super(); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 获取公告信息 |
||||
*/ |
||||
getAllNotices(): Observable<ForumNoticeResponse> { |
||||
return this.http.get<ForumNoticeResponse>(HttpInterface.getAllNotices, this.httpOptions) |
||||
.pipe( |
||||
catchError(this.handleError<any>('获取公告信息')) |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* 添加公告 |
||||
*/ |
||||
addNotice(body): Observable<ForumNoticeResponse> { |
||||
return this.http.post<ForumNoticeResponse>(HttpInterface.addNotice, body, this.httpOptions) |
||||
.pipe( |
||||
catchError(this.handleError<any>('添加公告')) |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* 删除公告 |
||||
*/ |
||||
deleteNotice(body): Observable<ForumNoticeResponse> { |
||||
return this.http.post<ForumNoticeResponse>(HttpInterface.deleteNotice, body, this.httpOptions) |
||||
.pipe( |
||||
catchError(this.handleError<any>('添加公告')) |
||||
); |
||||
} |
||||
} |
@ -1,10 +1,12 @@ |
||||
export interface Notice { |
||||
// 公告id
|
||||
id: number; |
||||
// 公告人
|
||||
managerName: string; |
||||
author: string; |
||||
// 公告内容
|
||||
content: string; |
||||
// 公告标题
|
||||
title: string; |
||||
// 公告时间
|
||||
createTime: number; |
||||
time: number; |
||||
} |
||||
|
Loading…
Reference in new issue