infoTab.page.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { Component, OnInit, ViewChild } from '@angular/core';
  2. import { from } from 'rxjs';
  3. import { HTTP } from '@ionic-native/http/ngx';
  4. import { ToastController } from '@ionic/angular';
  5. import { finalize } from 'rxjs/operators';
  6. import { Keyboard } from '@ionic-native/keyboard/ngx';
  7. import { Storage } from '@ionic/storage'
  8. import { BarcodeScanner } from '@ionic-native/barcode-scanner/ngx';
  9. @Component({
  10. selector: 'app-infoTab',
  11. templateUrl: './infoTab.page.html',
  12. styleUrls: ['./infoTab.page.scss'],
  13. })
  14. export class infoTabPage implements OnInit {
  15. private pCode: string
  16. private currentpCode: string
  17. private supplierName: string
  18. private outSendFactoryName: string
  19. private imgSrc: string
  20. private ssfId: any
  21. private access_token: string
  22. constructor(private nativeHttp: HTTP,
  23. private barcodeScanner: BarcodeScanner,
  24. private storage: Storage,
  25. private keyboard: Keyboard,
  26. private toastCtrl: ToastController) { }
  27. @ViewChild('infoInput') infoInput;
  28. ngOnInit() {
  29. this.storage.get('access_token').then((val) => {
  30. this.access_token = val
  31. });
  32. }
  33. ionViewDidEnter() {
  34. this.inputFocus()
  35. console.log('enter')
  36. }
  37. pcodechange() {
  38. if (this.pCode) {
  39. this.infoInput.value = ''
  40. this.currentpCode = this.pCode
  41. this.getPcodeInfo()
  42. }
  43. }
  44. inputFocus() {
  45. this.infoInput.setFocus();
  46. this.keyboard.hide()
  47. }
  48. doClick() {
  49. this.inputFocus()
  50. console.log('click info')
  51. }
  52. // 扫描货号获取详细信息
  53. async getPcodeInfo() {
  54. this.storage.get('ssfId').then((val) => {
  55. this.ssfId = val
  56. });
  57. let headers = { 'Authorization': `Bearer ${this.access_token}`, 'Content-Type': 'application/json;charset=UTF-8' }
  58. // Returns a promise, need to convert with of() to Observable (if want)!
  59. from(this.nativeHttp.get('http://192.168.20.32/production/sample/factory/pCode',
  60. { pCode: this.currentpCode },
  61. headers)).pipe(
  62. finalize(() => { })
  63. ).subscribe(data => {
  64. this.supplierName = (JSON.parse(data.data).data).supplierName
  65. this.outSendFactoryName = (JSON.parse(data.data).data).outSendFactoryName
  66. if ((JSON.parse(data.data).data).pictures.length !== 0) {
  67. this.imgSrc = (JSON.parse(data.data).data).pictures[0].smallPicture
  68. }
  69. }, async err => {
  70. let toast = await this.toastCtrl.create({
  71. message: '获取数据失败',
  72. duration: 1000,
  73. position: 'top'
  74. });
  75. await toast.present();
  76. });
  77. }
  78. async save() {
  79. this.nativeHttp.setDataSerializer('json');
  80. let data = { ssfCode: this.currentpCode, ssfId: this.ssfId }
  81. let headers = { 'Authorization': `Bearer ${this.access_token}`, 'Content-Type': 'application/json;charset=UTF-8' }
  82. // Returns a promise, need to convert with of() to Observable (if want)!
  83. from(this.nativeHttp.post('http://192.168.20.32/production/sample/factory/details/',
  84. data,
  85. headers)).pipe(
  86. finalize(() => { })
  87. ).subscribe(async data => {
  88. if (JSON.parse(data.data).msg === 'success') {
  89. let toast = await this.toastCtrl.create({
  90. message: '保存成功',
  91. position: 'top',
  92. duration: 1000
  93. });
  94. toast.present()
  95. }
  96. }, async err => {
  97. let toast = await this.toastCtrl.create({
  98. message: '保存失败',
  99. duration: 1000,
  100. position: 'top'
  101. });
  102. await toast.present();
  103. });
  104. }
  105. qrscan() {
  106. this.barcodeScanner.scan().then(barcodeData => {
  107. this.pCode = barcodeData.text
  108. }).catch(err => {
  109. console.log('Error', err);
  110. });
  111. }
  112. }