import { Component, OnInit, ViewChild } from '@angular/core'; import { from } from 'rxjs'; import { HTTP } from '@ionic-native/http/ngx'; import { ToastController } from '@ionic/angular'; import { finalize } from 'rxjs/operators'; import { Keyboard } from '@ionic-native/keyboard/ngx'; import { Storage } from '@ionic/storage' import { BarcodeScanner } from '@ionic-native/barcode-scanner/ngx'; @Component({ selector: 'app-infoTab', templateUrl: './infoTab.page.html', styleUrls: ['./infoTab.page.scss'], }) export class infoTabPage implements OnInit { private pCode: string private currentpCode: string private supplierName: string private outSendFactoryName: string private imgSrc: string private ssfId: any private access_token: string constructor(private nativeHttp: HTTP, private barcodeScanner: BarcodeScanner, private storage: Storage, private keyboard: Keyboard, private toastCtrl: ToastController) { } @ViewChild('infoInput') infoInput; ngOnInit() { this.storage.get('access_token').then((val) => { this.access_token = val }); } ionViewDidEnter() { this.inputFocus() console.log('enter') } pcodechange() { if (this.pCode) { this.infoInput.value = '' this.currentpCode = this.pCode this.getPcodeInfo() } } inputFocus() { this.infoInput.setFocus(); this.keyboard.hide() } doClick() { this.inputFocus() console.log('click info') } // 扫描货号获取详细信息 async getPcodeInfo() { this.storage.get('ssfId').then((val) => { this.ssfId = val }); let headers = { 'Authorization': `Bearer ${this.access_token}`, 'Content-Type': 'application/json;charset=UTF-8' } // Returns a promise, need to convert with of() to Observable (if want)! from(this.nativeHttp.get('http://192.168.20.32/production/sample/factory/pCode', { pCode: this.currentpCode }, headers)).pipe( finalize(() => { }) ).subscribe(data => { this.supplierName = (JSON.parse(data.data).data).supplierName this.outSendFactoryName = (JSON.parse(data.data).data).outSendFactoryName if ((JSON.parse(data.data).data).pictures.length !== 0) { this.imgSrc = (JSON.parse(data.data).data).pictures[0].smallPicture } }, async err => { let toast = await this.toastCtrl.create({ message: '获取数据失败', duration: 1000, position: 'top' }); await toast.present(); }); } async save() { this.nativeHttp.setDataSerializer('json'); let data = { ssfCode: this.currentpCode, ssfId: this.ssfId } let headers = { 'Authorization': `Bearer ${this.access_token}`, 'Content-Type': 'application/json;charset=UTF-8' } // Returns a promise, need to convert with of() to Observable (if want)! from(this.nativeHttp.post('http://192.168.20.32/production/sample/factory/details/', data, headers)).pipe( finalize(() => { }) ).subscribe(async data => { if (JSON.parse(data.data).msg === 'success') { let toast = await this.toastCtrl.create({ message: '保存成功', position: 'top', duration: 1000 }); toast.present() } }, async err => { let toast = await this.toastCtrl.create({ message: '保存失败', duration: 1000, position: 'top' }); await toast.present(); }); } qrscan() { this.barcodeScanner.scan().then(barcodeData => { this.pCode = barcodeData.text }).catch(err => { console.log('Error', err); }); } }