import { Component, OnInit } from '@angular/core'; import {environment} from '../../../../environments/environment'; import {CommonsService} from '../../../services/commons.service'; import {MsgService} from '../../../services/msg.service'; import {FormBuilder, FormGroup, Validators} from '@angular/forms'; import {HttpClient} from '@angular/common/http'; import {NzMessageService, NzUploadFile} from 'ng-zorro-antd'; import {Router} from '@angular/router'; function getBase64(file: File): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => resolve(reader.result); reader.onerror = error => reject(error); }); } @Component({ selector: 'app-system-msg', templateUrl: './system-msg.component.html', styleUrls: ['./system-msg.component.scss'] }) export class SystemMsgComponent implements OnInit { WEB_SERVE_URL = environment.imageUrl; POST_URL = environment.baseUrl; searchForm: FormGroup; // 搜索框 validateForm: FormGroup; // 添加框 requestData = []; // 列表数据 nodes = []; // 类型树 total: number; // 页码 pageNum = 1; // 页码 pageSize = 10; // 条码 loading = true; editFlag = false; isVisible = false; imgFile = []; previewImage: string | undefined = ''; previewVisible = false; id: number; constructor( private fb: FormBuilder, private http: HttpClient, // http请求 private message: NzMessageService, // 信息提示 private router: Router, private common: CommonsService, private msg: MsgService ) { this.WEB_SERVE_URL = environment.baseUrl; } ngOnInit() { this.searchForm = this.fb.group({ type: [null], jumpType: [null], title: [null], }); this.validateForm = this.fb.group({ type: [null, [Validators.required]], title: [null, [Validators.required]], jumpType: [null, [Validators.required]], content: [null, [Validators.required]], }); this.getRequest(true, this.searchForm.value); } // 查询列表 public getRequest(reset: boolean = false, whereObject: object) { if (reset) { this.pageNum = 1; } whereObject['pageNum'] = this.pageNum; whereObject['pageSize'] = this.pageSize; this.msg.getMsgByList(whereObject, data => { console.log(data); this.loading = false; if (data['return_code'] === '000000') { this.requestData = data['return_data'].list; this.total = data['return_data'].total; } else { this.message.error(data['return_msg']); } }); } // 重置 public resetForm(): void { this.searchForm.reset(); } // 新增 getAdd() { this.isVisible = true; this.editFlag = false; } // 编辑 getEdit(data: object) { console.log(data); if (data['image'] != null && data['image'] !== '') { const logo = String(data['image']); const logoArray = []; logoArray.push( { uid: 1, name: logo, status: 'done', url: environment.imageUrl + logo }); this.imgFile = logoArray; } this.id = data['id']; this.validateForm.patchValue(data); this.editFlag = true; this.isVisible = true; } // 提交内容 handleOk() { // tslint:disable-next-line:forin for (const i in this.validateForm.controls) { this.validateForm.controls[i].markAsDirty(); this.validateForm.controls[i].updateValueAndValidity(); if (this.validateForm.controls[i].errors != null) { this.message.error('必填项不能为空'); return; } } if (this.imgFile.length !== 0) { if (this.imgFile[0]['response'] != null) { this.validateForm.value.image = this.imgFile[0]['response']['return_data'][0]; } else { this.validateForm.value.image = this.imgFile[0].name; } } this.msg.insertMsg(this.validateForm.value , data => { if (data['return_code'] === '000000') { this.isVisible = false; this.getRequest(false, this.searchForm.value); this.message.success('新增成功'); } else { this.message.create('error', '修改失败'); } }); } // 图片查看 handlePreview = async (file: NzUploadFile) => { if (!file.url && !file.preview) { // tslint:disable-next-line:no-non-null-assertion file.preview = await getBase64(file.originFileObj!); } this.previewImage = file.url || file.preview; this.previewVisible = true; } public getForbiddenUser(id): void { const message = '是否发布'; this.common.showConfirm(message, data => { if (data) { this.msg.pushMsg(id, dataUser => { if (dataUser['return_code'] === '000000') { this.message.success(dataUser['return_data']); } else { this.message.error(dataUser['return_msg']); } this.getRequest(false , this.searchForm.value); }); } }); } }