Skip to content

Commit

Permalink
fix: some error message
Browse files Browse the repository at this point in the history
  • Loading branch information
buqiyuan committed Jun 27, 2022
1 parent efb2ab5 commit dbe7ba0
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</nz-tag> -->
<div>
<span *ngIf="isElectron ? extensionService.localModules.has(it.moduleID) : it.installed"
class=" p-1 text-xs text-green-700 border-green-700 rounded-sm bd_all">installed</span>
class="p-1 text-xs text-green-700 border-green-700 rounded-sm bd_all">Installed</span>
</div>
<!-- <i
nz-icon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,28 @@ export class AboutComponent implements OnInit {
constructor(private electron: ElectronService) {}

ngOnInit(): void {
fetch('https://api.github.com/repos/eolinker/eoapi/releases')
.then((response) => response.json())
.then((data) => {
const publishTime = data.find((n) => n.tag_name.slice(1) === pkg.version)?.published_at;
const publishObj = this.list.find((n) => n.id === 'publishTime');
if (publishTime) {
publishObj.value = new Intl.DateTimeFormat('zh-CN', {
year: 'numeric',
month: '2-digit',
weekday: 'long',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false,
})
.format(new Date(publishTime))
.replace(/星期[^]?/, '');
} else {
publishObj.value = `当前版本(v${pkg.version})尚未发布`;
}
});
// fetch('https://api.github.com/repos/eolinker/eoapi/releases')
// .then((response) => response.json())
// .then((data) => {
// const publishTime = data.find((n) => n.tag_name.slice(1) === pkg.version)?.published_at;
// const publishObj = this.list.find((n) => n.id === 'publishTime');
// if (publishTime) {
// publishObj.value = new Intl.DateTimeFormat('zh-CN', {
// year: 'numeric',
// month: '2-digit',
// weekday: 'long',
// day: '2-digit',
// hour: '2-digit',
// minute: '2-digit',
// second: '2-digit',
// hour12: false,
// })
// .format(new Date(publishTime))
// .replace(/星期[^]?/, '');
// } else {
// publishObj.value = `当前版本(v${pkg.version})尚未发布`;
// }
// });
const systemInfo = this.getSystemInfo();
this.list.forEach((item) => {
if (item.id in systemInfo) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
selector: 'eo-data-storage',
Expand All @@ -24,20 +25,20 @@ import { FormBuilder, FormGroup, Validators } from '@angular/forms';
<ng-container *ngIf="validateForm.value.dataStorage === 'remote-server'">
<nz-form-item>
<nz-form-label>Host</nz-form-label>
<nz-form-control nzErrorTip="Please input your Host!">
<nz-form-control nzErrorTip="Please input your Host">
<input nz-input formControlName="remoteServer.url" placeholder="your host" />
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-label>Security Token</nz-form-label>
<nz-form-control nzErrorTip="Please input your Security Token!">
<nz-form-control nzErrorTip="Please input your Security Token">
<input nz-input formControlName="remoteServer.token" placeholder="your security token" />
</nz-form-control>
</nz-form-item>
</ng-container>
<nz-form-item>
<nz-form-control>
<button nz-button nzType="primary">Change Data Storage</button>
<button nz-button nzType="primary" [nzLoading]="loading">Change Data Storage</button>
</nz-form-control>
</nz-form-item>
</form>
Expand All @@ -51,13 +52,71 @@ import { FormBuilder, FormGroup, Validators } from '@angular/forms';
],
})
export class DataStorageComponent implements OnInit, OnChanges {
@Input() model: object;
@Input() model: Record<string, any> = {};
@Output() modelChange: EventEmitter<any> = new EventEmitter();
validateForm!: FormGroup;
loading = false;

submitForm(): void {
constructor(private fb: FormBuilder, private message: NzMessageService) {}

ngOnInit(): void {
this.validateForm = this.fb.group({
dataStorage: this.model.dataStorage ?? 'remote-server',
'remoteServer.url': [this.model['remoteServer.url'], [Validators.required]],
'remoteServer.token': [this.model['remoteServer.token'], [Validators.required]],
});
}

ngOnChanges(changes: SimpleChanges): void {
const { model } = changes;

if (model && this.validateForm?.value) {
this.setFormValue(model.currentValue);
}
}

/**
* 测试远程服务器地址是否可用
*/
async pingRmoteServerUrl() {
const dataStorage = this.validateForm.value.dataStorage;
const remoteUrl = this.validateForm.value['remoteServer.url'];
const token = this.validateForm.value['remoteServer.token'];

if (dataStorage !== 'remote-server') {
return Promise.reject(false);
}

try {
const url = `${remoteUrl}/system/status`.replace(/(?<!:)\/{2,}/g, '/');
const response = await fetch(url, {
headers: {
'x-api-key': token,
},
});
const result = await response.json();
console.log('result', result);
if (result.statusCode !== 200) {
throw result;
}
// await result.json();
this.message.create('success', '远程服务器地址设置成功!');
return Promise.resolve(true);
} catch (error) {
console.error(error);
this.message.create('error', '远程服务器连接失败!');
return Promise.reject(false);
}
}

async submitForm() {
if (this.validateForm.valid) {
console.log('submit', this.validateForm.value);
this.loading = true;
const result = await this.pingRmoteServerUrl().finally(() => (this.loading = false));
if (Object.is(result, true)) {
this.message.success('远程数据源连接成功');
}
this.model = this.validateForm.value;
this.modelChange.emit(this.validateForm.value);
} else {
Expand All @@ -70,20 +129,9 @@ export class DataStorageComponent implements OnInit, OnChanges {
}
}

constructor(private fb: FormBuilder) {}

ngOnInit(): void {
this.validateForm = this.fb.group({
dataStorage: 'remote-server',
'remoteServer.url': [null, [Validators.required]],
'remoteServer.token': [null, [Validators.required]],
setFormValue(model = {}) {
Object.keys(model).forEach((key) => {
this.validateForm.get(key)?.setValue(model[key]);
});
}

ngOnChanges(changes: SimpleChanges): void {
const { model } = changes;
if (model) {
Object.assign(this.validateForm.value, model.currentValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,15 @@ <h2 class="title" *ngIf="module.title">{{ module.title }}</h2>
</div>
</form>
<eo-about *ngIf="selected === 'eoapi-about'"></eo-about>
<!-- <nz-empty *ngIf="!currentConfiguration.length " nzNotFoundImage="simple" [nzNotFoundContent]="contentTpl">
<nz-empty *ngIf="!currentConfiguration.length && selected === 'eoapi-extensions'" nzNotFoundImage="simple"
[nzNotFoundContent]="contentTpl">
<ng-template #contentTpl>
<span>暂无配置项</span>
<span>No plugins are currently installed,
<a class="eo_link" routerLink="['/home/extension/list']">go to
install</a>
</span>
</ng-template>
</nz-empty> -->
</nz-empty>
</section>
<!-- <div *nzModalFooter class="footer"> -->
<!-- <button nz-button nzType="primary" (click)="handleSave()">保存</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,48 +161,6 @@ export class SettingComponent implements OnInit {
// this.messageService.send({ type: 'switchDataSource', data: { showWithSetting: true } });
}

/**
* 测试远程服务器地址是否可用
*/
async pingRmoteServerUrl() {
const { url: remoteUrl, token } = this.getConfiguration('eoapi-common.remoteServer');
// 是否更新了远程服务地址或token
const isUpdateRemoteServerInfo = remoteUrl !== this.remoteServerUrl || token !== this.remoteServerToken;
let messageId;
if (isUpdateRemoteServerInfo) {
messageId = this.message.loading('远程服务器连接中...', { nzDuration: 0 }).messageId;
}

try {
const url = `${remoteUrl}/system/status`.replace(/(?<!:)\/{2,}/g, '/');
const response = await fetch(url, {
headers: {
'x-api-key': token,
},
});
const result = await response.json();
console.log('result', result);
if (result.statusCode !== 200) {
throw result;
}
// await result.json();
if (isUpdateRemoteServerInfo) {
this.message.create('success', '远程服务器地址设置成功');
return Promise.resolve(true);
}
} catch (error) {
console.error(error);
// if (remoteUrl !== this.remoteServerUrl) {
this.message.create('error', '远程服务器地址/token不可用');
// }
// 远程服务地址不可用时,回退到上次的地址
this.settings['eoapi-common.remoteServer.url'] = this.remoteServerUrl;
this.settings['eoapi-common.remoteServer.token'] = this.remoteServerToken;
} finally {
setTimeout(() => this.message.remove(messageId), 500);
}
}

/**
* 设置数据
*
Expand Down Expand Up @@ -381,11 +339,15 @@ export class SettingComponent implements OnInit {

async handleCancel() {
try {
const result = await this.pingRmoteServerUrl();
if (Object.is(result, true)) {
this.message.success('远程数据源连接成功');
this.remoteService.switchToHttp();
this.remoteService.refreshComponent();
const isUpdateRemoteInfo =
this.remoteServerUrl !== this.settings['eoapi-common.remoteServer.url'] ||
this.remoteServerToken !== this.settings['eoapi-common.remoteServer.token'];
if (isUpdateRemoteInfo && this.isRemote) {
this.message.success('你已修改远程服务相关信息,页面即将刷新');
setTimeout(() => {
this.remoteService.switchToHttp();
this.remoteService.refreshComponent();
}, 2000);
}
} catch (error) {
} finally {
Expand Down

0 comments on commit dbe7ba0

Please sign in to comment.