Skip to content

Commit

Permalink
optimize network choosing, fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
lowesyang committed Feb 9, 2019
1 parent 23651a2 commit 7f8fb24
Show file tree
Hide file tree
Showing 20 changed files with 228 additions and 77 deletions.
4 changes: 2 additions & 2 deletions config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@
<variable name="ANDROID_SUPPORT_VERSION" value="26+" />
</plugin>
<plugin name="cordova-plugin-themeablebrowser" spec="0.2.17" />
<engine name="ios" spec="4.5.5" />
<engine name="android" spec="7.1.4" />
<plugin name="cordova-plugin-ionic" spec="5.3.0">
<variable name="APP_ID" value="205551f0" />
<variable name="CHANNEL_NAME" value="Master" />
Expand All @@ -102,4 +100,6 @@
<variable name="UPDATE_API" value="https://api.ionicjs.com" />
<variable name="MIN_BACKGROUND_DURATION" value="30" />
</plugin>
<engine name="ios" spec="4.5.5" />
<engine name="android" spec="7.1.4" />
</widget>
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
"iconv-lite": "^0.4.24",
"is-buffer": "^2.0.3",
"moment": "^2.24.0",
"moment-timezone": "^0.5.23",
"rxjs": "^6.4.0",
"socket.io": "^2.2.0",
"stream": "^0.0.2",
Expand Down Expand Up @@ -130,4 +129,4 @@
"android"
]
}
}
}
4 changes: 2 additions & 2 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {NgModule} from '@angular/core';
import {Routes, RouterModule} from '@angular/router';

const routes: Routes = [
{path: '', loadChildren: './tabs/tabs.module#TabsPageModule'},
{path: 'tabs', loadChildren: './tabs/tabs.module#TabsPageModule'},
{path: 'login', loadChildren: './pages/login/login.module#LoginPageModule'},
{path: 'transfer', loadChildren: './pages/transfer/transfer.module#TransferPageModule'},
{path: 'bill', loadChildren: './pages/bill/bill.module#BillPageModule'},
Expand All @@ -17,7 +17,7 @@ const routes: Routes = [
{path: 'dapp/:url', loadChildren: './pages/dapp-container/dapp-container.module#DappContainerPageModule'},
{path: 'export-key/:privkey', loadChildren: './pages/export-key/export-key.module#ExportKeyPageModule'},
{path: 'dapp-list/:group', loadChildren: './pages/dapp-list/dapp-list.module#DappListPageModule'},
{ path: 'share', loadChildren: './pages/share/share.module#SharePageModule' },
{path: 'share', loadChildren: './pages/share/share.module#SharePageModule'},
];

@NgModule({
Expand Down
63 changes: 46 additions & 17 deletions src/app/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,52 @@ export const tokenCode = 'zjubcatokent';
// kylin testnet chain id
export const CHAINID = '5fff1dae8dc8e2fc4d5b23b2c7665c97f9e9d8edf2b6485a86ba311c25639191';

export const endpoints = [
{
name: 'EOSLaomao',
endpoint: 'https://api-kylin.eoslaomao.com'
},
{
name: 'MEET.ONE',
endpoint: 'https://api-kylin.meet.one',
},
{
name: 'EOS Asia',
endpoint: 'https://api-kylin.eosasia.one'
}

];

export const timezone = 'Asia/Shanghai';
export const endpoints = {
'normal': [
{
name: 'MEET.ONE',
endpoint: 'http://kylin.meet.one:8888',
},
{
name: 'EOSpacex',
endpoint: 'https://api.kylin-testnet.eospacex.com'
},
{
name: 'EOSbixin',
endpoint: 'http://kylin.fn.eosbixin.com'
},
{
name: 'Alohaeos',
endpoint: 'https://api.kylin.alohaeos.com'
},
{
name: 'EOScanada',
endpoint: 'https://kylin.eoscanada.com'
},
{
name: 'EOSbeijing',
endpoint: 'http://api.kylin.eosbeijing.one:8880'
},
{
name: 'StartEOS',
endpoint: 'http://api-kylin.starteos.io'
}
],
// support `get_action`
'actions': [
{
name: 'EOSLaomao',
endpoint: 'https://api-kylin.eoslaomao.com'
},
{
name: 'EOS Asia',
endpoint: 'https://api-kylin.eosasia.one'
}
]
};

// export const timezone = 'Asia/Shanghai';
export const timezone = 960;


/* ===== storage key ===== */
Expand Down
11 changes: 6 additions & 5 deletions src/app/components/create-wallet/create-wallet.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Component, OnInit} from '@angular/core';
import {ToastController, AlertController, LoadingController} from '@ionic/angular';
import {Route, Router} from '@angular/router';
import {ToastController, AlertController, LoadingController, NavController} from '@ionic/angular';
import axios from '../../common/axios';
import {AccountService} from '../../services/account.service';
import {WalletService} from '../../services/wallet.service';
Expand Down Expand Up @@ -34,7 +33,7 @@ export class CreateWalletComponent implements OnInit {
private walletService: WalletService,
private eosService: EosService,
private storage: Storage,
private router: Router,
private navCtrl: NavController,
private clipboard: Clipboard,
private loadingCtrl: LoadingController
) {
Expand Down Expand Up @@ -120,12 +119,14 @@ export class CreateWalletComponent implements OnInit {
name: name,
permissions: permissions
}]);
await this.walletService.saveWallet(this.form.name, pubKey, privateKey, this.form.password);
const curr = await this.accService.current();
if (!curr) {
await this.accService.setCurrent(name);
await this.navCtrl.navigateRoot('/tabs/assets');
} else {
await this.navCtrl.navigateBack('/tabs/assets', {replaceUrl: true});
}
await this.walletService.saveWallet(this.form.name, pubKey, privateKey, this.form.password);
await this.router.navigate(['/tabs/assets'], {replaceUrl: true});
} else {
throw new Error(resp.msg);
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/components/home-pop-menu/home-pop-menu.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Component, OnInit} from '@angular/core';
import {PopoverController} from '@ionic/angular';
import {NavController, PopoverController} from '@ionic/angular';
import {Router} from '@angular/router';

@Component({
Expand All @@ -10,7 +10,7 @@ import {Router} from '@angular/router';
export class HomePopMenuComponent implements OnInit {

constructor(
private router: Router,
private navCtrl: NavController,
private popoverCtrl: PopoverController
) {
}
Expand All @@ -19,7 +19,7 @@ export class HomePopMenuComponent implements OnInit {
}

async nav(url) {
await this.router.navigateByUrl(url);
await this.navCtrl.navigateForward(url);
await this.popoverCtrl.dismiss();
}
}
10 changes: 6 additions & 4 deletions src/app/components/import-wallet/import-wallet.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Component, OnInit} from '@angular/core';
import {ToastController, LoadingController} from '@ionic/angular';
import {ToastController, LoadingController, NavController} from '@ionic/angular';
import {EosService} from '../../services/eos.service';
import {AccountService} from '../../services/account.service';
import {Router} from '@angular/router';
Expand Down Expand Up @@ -29,7 +29,7 @@ export class ImportWalletComponent implements OnInit {
private eosService: EosService,
private accService: AccountService,
private walletService: WalletService,
private router: Router,
private navCtrl: NavController,
) {
this.form = new ImportForm();
}
Expand Down Expand Up @@ -90,12 +90,14 @@ export class ImportWalletComponent implements OnInit {
);
// @ts-ignore
await this.accService.saveAccounts(accounts);
await this.walletService.saveWallet(this.form.name, pubkey, privKey, this.form.password);
const curr = await this.accService.current();
if (!curr) {
await this.accService.setCurrent(names[0]);
await this.navCtrl.navigateRoot('/tabs/assets');
} else {
await this.navCtrl.navigateBack('/tabs/assets', {replaceUrl: true});
}
await this.walletService.saveWallet(this.form.name, pubkey, privKey, this.form.password);
await this.router.navigate(['/tabs/assets'], {replaceUrl: true});
} else {
await this.alert('该密钥下无账户名');
}
Expand Down
15 changes: 10 additions & 5 deletions src/app/pages/dapp-container/dapp-container.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import Error from '../../common/Error';
import {LoadingController, ModalController, ToastController} from '@ionic/angular';
import {TransactModalComponent} from '../../modals/transact-modal/transact-modal.component';
import {Action} from '../../../classes';
import moment from 'moment-timezone';
import {beautifyValue} from '../../common/helper';
import {ActivatedRoute} from '@angular/router';
import {DomSanitizer} from '@angular/platform-browser';
import * as moment from 'moment';


@Component({
Expand Down Expand Up @@ -273,11 +273,16 @@ class ApiService {
if (network.chainId === CHAINID) {
return this.response(Error.notKylinChain());
}
network = endpoints.find(x => x.endpoint === `${network.prototype}://${network.host}`);
if (!network) {
return this.response(Error.noNetwork());
network = endpoints.normal.find(x => x.endpoint === `${network.prototype}://${network.host}`);
if (network) {
return this.response(true);
}
this.response(true);
network = endpoints.actions.find(x => x.endpoint === `${network.prototype}://${network.host}`);
if (network) {
return this.response(true);
}
this.response(Error.noNetwork());

}

suggestNetwork() {
Expand Down
11 changes: 10 additions & 1 deletion src/app/pages/network/network.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@
<ion-content>
<ion-list lines="full">
<ion-radio-group (ionChange)="peerChange($event)" [value]="currPeer.name">
<ion-list-header>
<ion-label>可选节点(支持获取历史action)</ion-label>
</ion-list-header>
<ion-item *ngFor="let peer of actionsPeers">
<ion-label>{{peer.name}}</ion-label>
<ion-radio slot="start" [value]="peer.name" [checked]="peer.name === currPeer.name"></ion-radio>
<ion-note slot="end" class="f14">{{peer.delay}}</ion-note>
</ion-item>
<ion-list-header>
<ion-label>可选节点</ion-label>
</ion-list-header>
<ion-item *ngFor="let peer of peers">
<ion-item *ngFor="let peer of normalPeers">
<ion-label>{{peer.name}}</ion-label>
<ion-radio slot="start" [value]="peer.name" [checked]="peer.name === currPeer.name"></ion-radio>
<ion-note slot="end" class="f14">{{peer.delay}}</ion-note>
</ion-item>
</ion-radio-group>
</ion-list>
Expand Down
40 changes: 35 additions & 5 deletions src/app/pages/network/network.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {NetworkService} from '../../services/network.service';
interface Peer {
name: string;
endpoint: string;
delay?: string;
}

@Component({
Expand All @@ -13,11 +14,12 @@ interface Peer {
styleUrls: ['./network.page.scss'],
})
export class NetworkPage implements OnInit {
peers: Peer[];
normalPeers: Peer[];
actionsPeers: Peer[];

currPeer: Peer = {
endpoint: '',
name: ''
name: '',
};

constructor(
Expand All @@ -26,14 +28,42 @@ export class NetworkPage implements OnInit {
}

async ngOnInit() {
this.peers = endpoints;
this.normalPeers = endpoints.normal;
this.actionsPeers = endpoints.actions;
this.currPeer = await this.networkSvc.getNetwork();

const peers = await Promise.all([
await this.getDelay(this.normalPeers),
await this.getDelay(this.actionsPeers)
]);
this.normalPeers = peers[0];
this.actionsPeers = peers[1];
}

async peerChange(ev) {
const name = ev.detail.value;
this.currPeer = this.peers.find(item => item.name === name);
await this.networkSvc.setNetwork(this.currPeer);
let currPeer = this.normalPeers.find(item => item.name === name);
if (!currPeer) {
currPeer = this.actionsPeers.find(item => item.name === name);
}
try {
await this.networkSvc.setNetwork(currPeer);
this.currPeer = currPeer;
} catch (e) {
console.log(e);
}
}

async getDelay(peers: Peer[]): Promise<Peer[]> {
return await Promise.all(peers.map(async peer => {
let delay = '';
try {
delay = (await this.networkSvc.ping(peer.endpoint)) + ' ms';
} catch (e) {
delay = '超时';
}
return Object.assign(peer, {delay});
}));
}

}
8 changes: 6 additions & 2 deletions src/app/pages/qr-scan/qr-scan.page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Component, OnInit} from '@angular/core';
import {Component, OnDestroy, OnInit} from '@angular/core';
import {QRScanner, QRScannerStatus} from '@ionic-native/qr-scanner/ngx';
import {QRData} from '../../../classes';
import {NavController} from '@ionic/angular';
Expand All @@ -9,7 +9,7 @@ import {NavController} from '@ionic/angular';
templateUrl: './qr-scan.page.html',
styleUrls: ['./qr-scan.page.scss'],
})
export class QrScanPage implements OnInit {
export class QrScanPage implements OnInit, OnDestroy {

noCameraPermission = false;
errorMessage: string;
Expand All @@ -24,6 +24,10 @@ export class QrScanPage implements OnInit {
this.qrScan();
}

ngOnDestroy() {
this.qrScanner.hide();
}

async qrScan() {
try {
const status: QRScannerStatus = await this.qrScanner.prepare();
Expand Down
3 changes: 3 additions & 0 deletions src/app/pages/share/share.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

<ion-content>
<div class="qrcode">
<div class="name">
<ion-label class="f18" color="dark">浙大区块链协会官方钱包</ion-label>
</div>
<qrcode [qrdata]="qrcode" [size]="256" [level]="'M'"></qrcode>
</div>
</ion-content>
11 changes: 11 additions & 0 deletions src/app/pages/share/share.page.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.qrcode {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

.name {
text-align: center;
margin-bottom: 10px;
}
}
Loading

0 comments on commit 7f8fb24

Please sign in to comment.