You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
puhui-go-web/src/app/pages/system/cms/cms.component.ts

255 lines
7.4 KiB

import { Component } from '@angular/core';
import {NzButtonComponent} from "ng-zorro-antd/button";
import {NzColDirective, NzRowDirective} from "ng-zorro-antd/grid";
import {NzFormControlComponent, NzFormDirective, NzFormItemComponent, NzFormLabelComponent} from "ng-zorro-antd/form";
import {NzInputDirective} from "ng-zorro-antd/input";
import {FormGroup, FormsModule, NonNullableFormBuilder, ReactiveFormsModule, Validators} from "@angular/forms";
import {DatePipe, NgForOf, NgIf, NgStyle} from "@angular/common";
import {NzDividerComponent} from "ng-zorro-antd/divider";
import {NzIconDirective} from "ng-zorro-antd/icon";
import {NzImageDirective, NzImageModule} from "ng-zorro-antd/image";
import {NzModalComponent, NzModalModule} from "ng-zorro-antd/modal";
import {NzPopconfirmDirective} from "ng-zorro-antd/popconfirm";
import {
NzTableComponent, NzTableModule
, NzTdAddOnComponent,
} from "ng-zorro-antd/table";
import {NzUploadComponent, NzUploadFile} from "ng-zorro-antd/upload";
import {environment} from "../../../../environments/environment";
import {NzMessageService} from "ng-zorro-antd/message";
import {CommonService} from "../../../services/common/common.service";
import {GoodsTypeData} from "../../../model/goods.interface";
import { fallbackImg } from '../../../data/goods/goods.namespace';
import {NzOptionComponent, NzSelectComponent} from "ng-zorro-antd/select";
import {CmsService} from "../../../services/cms/cms.service";
import {DictionaryPipe} from "../../../pipes/common/dictionary.pipe";
import {NzTreeSelectComponent} from "ng-zorro-antd/tree-select";
import {GoodsStatusPipe} from "../../../pipes/goods/goods-status.pipe";
import {NzDropDownADirective, NzDropDownDirective, NzDropdownMenuComponent} from "ng-zorro-antd/dropdown";
import {NzMenuDirective, NzMenuItemComponent} from "ng-zorro-antd/menu";
import {
NzInputNumberComponent,
NzInputNumberGroupComponent,
NzInputNumberGroupWhitSuffixOrPrefixDirective
} from "ng-zorro-antd/input-number";
import {NzSpaceItemDirective} from "ng-zorro-antd/space";
import {NzDatePickerComponent} from "ng-zorro-antd/date-picker";
import {NzTypographyComponent} from "ng-zorro-antd/typography";
import {PlatformCodePipe} from "../../../pipes/common/platform-code.pipe";
const getBase64 = (file: File): Promise<string | ArrayBuffer | null> =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
@Component({
selector: 'app-cms',
standalone: true,
imports: [
FormsModule,
NzButtonComponent,
NzColDirective,
NzFormControlComponent,
NzFormDirective,
NzFormItemComponent,
NzFormLabelComponent,
NzInputDirective,
NzRowDirective,
ReactiveFormsModule,
NgForOf,
NzOptionComponent,
NzSelectComponent,
NzModalModule,
NgIf,
NzUploadComponent,
NzIconDirective,
NzImageModule,
NgStyle,
NzTableComponent,
NzTdAddOnComponent,
NzTableModule,
DictionaryPipe,
DatePipe,
NzImageDirective,
NzTreeSelectComponent,
NzDividerComponent,
NzPopconfirmDirective,
GoodsStatusPipe,
NzDropDownADirective,
NzDropDownDirective,
NzDropdownMenuComponent,
NzMenuDirective,
NzMenuItemComponent,
NzInputNumberComponent,
NzInputNumberGroupWhitSuffixOrPrefixDirective,
NzSpaceItemDirective,
NzInputNumberGroupComponent,
NzDatePickerComponent,
NzTypographyComponent,
PlatformCodePipe
],
templateUrl: './cms.component.html',
styleUrl: './cms.component.less'
})
export class CmsComponent {
// 表单页数
tablePageNum = 1;
// 表单数据
tableData: any = {
total: 0,
loading: false,
list: [],
};
// 搜索表单
searchForm: FormGroup;
// 图片
img: NzUploadFile[] = []
baseUrl = environment.baseUrl;
imageUrl = environment.imageUrl;
// 展示图片
previewImage: string | undefined = '';
// 上传是否展示
previewVisible = false;
// 编辑产品类型表单
editForm: FormGroup;
// 编辑弹出框
isVisibleEdit = false;
protected readonly fallbackImg = fallbackImg;
constructor(private fb: NonNullableFormBuilder,
private msg: NzMessageService,
private commonService: CommonService,
private cmsService: CmsService) {
// 初始化搜索框
this.searchForm = this.fb.group({
code: [''],
name: [''],
platformCode: [''],
});
// 初始化
this.editForm = this.fb.group({
name: ['' , [Validators.required]],
code: ['' , [Validators.required]],
platformCode: ['' , [Validators.required]],
id: [null],
img: [''],
jumpType: [null],
jumpUrl: [null],
appid: [null],
});
this.getRequest();
}
// 查询列表
public getRequest(reset: boolean = false) {
this.tableData.loading = true;
if (reset) {
this.tablePageNum = 1;
}
this.searchForm.value.pageNum = this.tablePageNum;
this.searchForm.value.pageSize = 10;
this.searchForm.value.time = new Date().getTime();
this.cmsService.getCms(this.searchForm.value , (data: any) => {
if (data['return_code'] == '000000') {
this.tableData = data['return_data'];
} else {
this.msg.error(data['return_msg']);
}
this.tableData.loading = false;
});
}
// 唤醒编辑表单
showEdit(item?: any) {
if (item != null) {
item.jumpType = String(item.jumpType);
this.editForm.patchValue(item);
if (!this.commonService.whetherStringIsNull(item.img)) {
this.img = this.commonService.stitchImg(item.img);
} else {
this.img = [];
}
} else {
this.editForm.reset();
this.img = [];
}
this.isVisibleEdit = true;
}
// 编辑表单提交
handleEdit(): void {
if (this.editForm.valid) {
if (this.img.length !== 0) {
this.editForm.value.img = this.commonService.imgList(this.img);
} else {
this.editForm.value.img = null;
}
this.cmsService.editCms(this.editForm.value , (data: any) => {
if (data['return_code'] === '000000') {
this.msg.success("成功!");
this.isVisibleEdit = false;
this.getRequest();
} else {
this.msg.error(data['return_msg']);
}
});
} else {
Object.values(this.editForm.controls).forEach(control => {
if (control.invalid) {
control.markAsDirty();
control.updateValueAndValidity({ onlySelf: true });
}
});
}
}
// 上传操作
handlePreview = async (file: NzUploadFile): Promise<void> => {
if (!file.url && !file['preview']) {
file['preview'] = await getBase64(file.originFileObj!);
}
this.previewImage = file.url || file['preview'];
this.previewVisible = true;
};
// 删除
delete(id: number): void {
this.cmsService.delete(id , (data: any) => {
if (data['return_code'] === '000000') {
this.msg.success("成功!");
this.getRequest();
} else {
this.msg.error(data['return_msg']);
}
});
}
// 上下线
cmsUpDown(id: number): void {
let params = {
id: id,
time: new Date().getTime()
}
this.cmsService.cmsUpDown(params, (data: any) => {
if (data['return_code'] === '000000') {
this.msg.success("成功!");
this.getRequest();
} else {
this.msg.error(data['return_msg']);
}
});
}
}