嗨森逛PC管理端
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.
high-web/src/app/admin/cms/cms-content/cms-content.component.ts

266 lines
8.5 KiB

import {Component, OnInit} from '@angular/core';
4 years ago
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
2 years ago
import {HttpClient} from '@angular/common/http';
import {NzMessageService, NzModalService, NzUploadFile} from 'ng-zorro-antd';
4 years ago
import {Router} from '@angular/router';
import {LocalStorageService} from '../../../services/local-storage.service';
import {ADMIN_INFO_OBJECT} from '../../../services/local-storage.namespace';
import {environment} from '../../../../environments/environment';
2 years ago
import {CmsService} from '../../../services/cms.service';
import {CommonsService} from '../../../services/commons.service';
function getBase64(file: File): Promise<string | ArrayBuffer | null> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = error => reject(error);
});
}
4 years ago
@Component({
selector: 'app-cms-content',
templateUrl: './cms-content.component.html',
styleUrls: ['./cms-content.component.less']
4 years ago
})
export class CmsContentComponent implements OnInit {
2 years ago
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,
2 years ago
private common: CommonsService,
private cms: CmsService,
) {
this.WEB_SERVE_URL = environment.baseUrl;
4 years ago
}
ngOnInit() {
2 years ago
this.searchForm = this.fb.group({
title: [null],
categoryId: [null],
platform: [null],
status: [null],
});
2 years ago
this.validateForm = this.fb.group({
categoryId: [null, [Validators.required]],
title: [null, [Validators.required]],
platform: [null],
platformArray: [null],
sortId: [null, [Validators.required]],
description: [null, [Validators.required]],
jumpType: [null],
jumpUrl: [null],
});
2 years ago
const roleType = 1;
this.findCategoryTree(roleType);
2 years ago
this.getRequest(true, this.searchForm.value);
4 years ago
}
2 years ago
// 查询列表
public getRequest(reset: boolean = false, whereObject: object) {
if (reset) {
2 years ago
this.pageNum = 1;
}
2 years ago
whereObject['pageNum'] = this.pageNum;
whereObject['pageSize'] = this.pageSize;
this.cms.getListContent(whereObject, data => {
console.log(data);
2 years ago
this.loading = false;
if (data['return_code'] === '000000') {
this.requestData = data['return_data'].list;
this.total = data['return_data'].total;
} else {
2 years ago
this.message.error(data['return_msg']);
}
});
4 years ago
}
2 years ago
// 重置
public resetForm(): void {
this.searchForm.reset();
4 years ago
}
2 years ago
// 查询分类树
findCategoryTree(roleType) {
2 years ago
this.cms.getOwnCategoryTree(roleType , data => {
if (data['return_code'] === '000000') {
this.nodes = data['return_data'];
if (this.nodes.length !== 0) {
this.generateComment(this.nodes);
} else {
this.nodes = [];
}
} else {
this.message.create('error', data['return_msg']);
}
});
4 years ago
}
// 递归分类树
generateComment(data) {
for (const i in data) {
if (data[i].nodes == null) {
const item = {
isLeaf: true,
title: data[i].text,
key: data[i].id,
children: data[i].nodes,
};
data[i] = item;
} else {
const item = {
isLeaf: false,
title: data[i].text,
key: data[i].id,
children: data[i].nodes,
};
data[i] = item;
this.generateComment(data[i].children);
}
4 years ago
}
this.nodes = data;
4 years ago
}
2 years ago
// 新增
getAdd() {
this.isVisible = true;
2 years ago
this.editFlag = false;
4 years ago
}
2 years ago
// 编辑
getEdit(data: object) {
console.log(data);
if (data['imgData'] != null && data['imgData'] !== '') {
const logo = String(data['imgData']);
const logoArray = [];
logoArray.push(
{
uid: 1,
name: logo,
status: 'done',
url: environment.imageUrl + logo
});
this.imgFile = logoArray;
}
2 years ago
this.id = data['id'];
this.validateForm.patchValue(data);
this.editFlag = true;
this.isVisible = true;
4 years ago
}
2 years ago
// 提交内容
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;
}
}
2 years ago
if (this.imgFile.length !== 0) {
if (this.imgFile[0]['response'] != null) {
this.validateForm.value.imgData = this.imgFile[0]['response']['return_data'][0];
} else {
2 years ago
this.validateForm.value.imgData = this.imgFile[0].name;
}
}
2 years ago
if (this.editFlag) {
this.validateForm.value.id = this.id;
this.cms.updateCmsContent(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', '修改失败');
}
});
} else {
this.cms.insertCmsContent(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', '修改失败');
}
});
}
}
2 years ago
// 图片查看
handlePreview = async (file: NzUploadFile) => {
if (!file.url && !file.preview) {
// tslint:disable-next-line:no-non-null-assertion
file.preview = await getBase64(file.originFileObj!);
}
2 years ago
this.previewImage = file.url || file.preview;
this.previewVisible = true;
}
2 years ago
showDeleteConfirmDelete(id: number): void {
this.common.showConfirm('是否确定删除!' , dataR => {
if (dataR) {
this.cms.deleteById(id, data => {
if (data['return_code'] === '000000') {
this.getRequest(true, this.searchForm.value);
this.message.success(data['return_data']);
} else {
this.message.error(data['return_msg']);
}
});
}
});
}
2 years ago
public getForbiddenUser(id, status: any): void {
const message = (status === 2 ? '是否下架当前内容' : '是否上架当前内容');
const s = status === 2 ? 1 : 2;
this.common.showConfirm(message, data => {
if (data) {
this.cms.updateContentStatus(id, s , 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);
});
}
});
4 years ago
}
2 years ago
4 years ago
}