import { ConfigGroupDatasQueryDto, MailServerConfigDto } from './../../syc-models'; import { RhSycService } from './../../syc.service'; import { Component, OnInit } from '@angular/core'; import { RhBaseDto, DataResultT, RhSelectableDto, RhTableButtonInfoDto, OpMode, RhSafeAny } from 'rh-base/model'; import { MsgHelper, ObjectHelper, ArrayHelper } from 'rh-base/core'; import * as _ from 'lodash'; type AddressType = { Name: string; DisplayName: string }; @Component({ selector: 'rh-mail-servers-config', templateUrl: './mail-servers-config.component.html', styleUrls: ['./mail-servers-config.component.css'] }) export class RhMailServersConfigComponent implements OnInit { selectedIndex = 0; editDto: RhBaseDto = {}; dataLoading = false; dataresult!: DataResultT; editButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('edit', '编辑', 'default', false, 'edit', false, null, (data) => this.openEditTab(data as RhBaseDto)) ]; titleButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('new', '新建', 'primary', false, 'file', false, null, () => this.openEditTab(), true), new RhTableButtonInfoDto( 'delete', '删除', 'primary', true, 'delete', true, null, (datas) => this.removeConfigDatas(datas as RhSafeAny[]), false ) ]; extraButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto('refresh', '刷新', 'default', false, 'sync', false, null, () => this.getDatas(), true) ]; addrInfoEditButtonDataset: RhTableButtonInfoDto[] = [ new RhTableButtonInfoDto( 'delete', '删除', 'primary', true, 'delete', true, null, (data) => this.removeAddrInfo(data as RhBaseDto), false, false, 'red' ) ]; configSelectedIndex = 0; addrSelectedIndex = 0; /** 邮件地址 */ Name: string | null = null; /** 昵称 */ DisplayName: string | null = null; /** 地址信息数据源 */ addrInfoDataresult: DataResultT[] = []; /** 发件人地址 */ ToAddresses: AddressType[] = []; /** 抄送人地址 */ CcAddresses: AddressType[] = []; /** 密送人地址 */ BccAddresses: AddressType[] = []; /** 配置信息 */ configInfoDto: MailServerConfigDto | null = null; formRefresh = true; tableRefresh = true; beforeSubmit = (data: RhBaseDto) => { this.configInfoDto = MailServerConfigDto.create(); ObjectHelper.MapT(data, this.configInfoDto); MsgHelper.ShowSuccessMessage('服务器设置信息保存成功!'); return false; }; constructor(private operator: RhSycService) { // } ngOnInit(): void { // this.getDatas(); } openEditTab(data?: RhBaseDto) { // if (data) { const temp = JSON.parse(data['CustomData']); this.editDto = _.cloneDeep(temp); this.editDto.OpSign = OpMode.OpEdit; this.ToAddresses = temp.ToAddresses || []; this.CcAddresses = temp.CcAddresses || []; this.BccAddresses = temp.BccAddresses || []; this.configInfoDto = _.cloneDeep(temp); } else { this.editDto = {}; this.editDto.OpSign = OpMode.OpAdd; this.ToAddresses = []; this.CcAddresses = []; this.BccAddresses = []; this.configInfoDto = null; } this.setAddrInfoDataresult(); this.selectedIndex = 1; } closeEditTab() { // this.resetConfigInfoDto(); this.selectedIndex = 0; } // 获取数据 private getDatas() { const queryDto = new ConfigGroupDatasQueryDto(null, null, null, null, -1, 'PP'); queryDto.GroupKey = 'MailServerConfig'; this.dataLoading = true; this.operator.GetGroupConfigDatas(queryDto).subscribe( (dresult) => { this.dataresult = dresult; if (!this.dataresult?.Success) { MsgHelper.ShowWarningMessage(`搜索无数据!${this.dataresult?.Message}`); } this.dataLoading = false; }, () => { this.dataLoading = false; } ); } removeConfigDatas(datas: RhSafeAny[]) { this.titleButtonDataset[1].loading = true; this.operator.RemoveConfigGroupDatas(datas).subscribe( (result) => { if (result.Success) { MsgHelper.ShowSuccessMessage('删除成功!'); this.getDatas(); } else { MsgHelper.ShowWarningMessage(`删除失败!${result.Message}`); } this.titleButtonDataset[1].loading = false; }, () => { this.titleButtonDataset[1].loading = false; } ); } /** 切换地址信息tab页 */ addrInfoTabChange() { this.resetAddrInfo(); } /** 添加地址信息 */ addAddrInfo() { if (this.Name && this.DisplayName) { const temp = this.addrInfoDataresult[this.addrSelectedIndex].Attach; ArrayHelper.AddTo(temp, { Name: this.Name, DisplayName: this.DisplayName }, (ele) => ele.Name === this.Name); this.addrInfoDataresult[this.addrSelectedIndex].Record = this.addrInfoDataresult[this.addrSelectedIndex].TotalCount = temp.length; if (temp.length === 1) { this.tableRefresh = false; setTimeout(() => { this.tableRefresh = true; }); } this.resetAddrInfo(); } else { MsgHelper.ShowWarningMessage('必须填写邮件地址和昵称!'); } } /** 移除地址信息 */ removeAddrInfo(data: RhBaseDto) { const temp = this.addrInfoDataresult[this.addrSelectedIndex].Attach; ArrayHelper.Remove(temp, (ele) => ele.Name === data['Name']); this.addrInfoDataresult[this.addrSelectedIndex].Record = this.addrInfoDataresult[this.addrSelectedIndex].TotalCount = temp.length; if (temp.length === 0) { this.tableRefresh = false; setTimeout(() => { this.tableRefresh = true; }); } } /** 保存配置信息 */ saveConfigInfo() { if (!this.configInfoDto) { MsgHelper.ShowWarningMessage('请先保存服务器设置信息!'); return; } this.configInfoDto.ToAddresses = this.ToAddresses; this.configInfoDto.CcAddresses = this.CcAddresses; this.configInfoDto.BccAddresses = this.BccAddresses; this.operator.StoreMailServerConfigData(this.configInfoDto).subscribe((result) => { if (result.Success) { MsgHelper.ShowSuccessMessage('邮箱服务器配置成功!'); if (this.editDto.OpSign === OpMode.OpEdit) { this.selectedIndex = 0; } else { this.openEditTab(); this.formRefresh = false; setTimeout(() => { this.formRefresh = true; }); } this.configSelectedIndex = this.addrSelectedIndex = 0; this.getDatas(); } else { MsgHelper.ShowWarningMessage(`邮箱服务器配置失败!${result.Message}`); } }); } resetAddrInfo() { this.Name = null; this.DisplayName = null; } resetConfigInfoDto() { this.ToAddresses = []; this.CcAddresses = []; this.BccAddresses = []; this.setAddrInfoDataresult(); this.configSelectedIndex = this.addrSelectedIndex = 0; } setAddrInfoDataresult() { this.addrInfoDataresult = [ { Success: false, Attach: this.ToAddresses, Message: '', Record: this.ToAddresses.length, TotalCount: this.ToAddresses.length, SkipCount: 0 }, { Success: false, Attach: this.CcAddresses, Message: '', Record: this.CcAddresses.length, TotalCount: this.CcAddresses.length, SkipCount: 0 }, { Success: false, Attach: this.BccAddresses, Message: '', Record: this.BccAddresses.length, TotalCount: this.BccAddresses.length, SkipCount: 0 } ]; } }