wx-img.component.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {Component, forwardRef, Input, Output} from '@angular/core';
  2. import {wxService} from "../../services/wxService";
  3. import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
  4. import {NavController, NavParams, ToastController} from "ionic-angular";
  5. @Component({
  6. selector: 'app-wx-img',
  7. templateUrl: './wx-img.component.html',
  8. providers: [{
  9. provide: NG_VALUE_ACCESSOR,
  10. useExisting: forwardRef(() => WxImgComponent),
  11. multi: true
  12. }]
  13. })
  14. export class WxImgComponent implements ControlValueAccessor {
  15. registerOnChange(fn: any): void {
  16. this.propagateChange = fn;
  17. }
  18. registerOnTouched(fn: any): void {
  19. }
  20. setDisabledState(isDisabled: boolean): void {
  21. }
  22. writeValue(obj: any): void {
  23. if (obj) {
  24. this.pictureUrl = obj;
  25. this.urlChanged();
  26. }
  27. }
  28. @Output() @Input() public pictureUrl: string;
  29. hasPicture: boolean = false;
  30. private propagateChange: any = {};
  31. constructor(private wxService: wxService,public navCtrl: NavController, public navParams: NavParams,public toastCtrl: ToastController,) {
  32. }
  33. ngOnInit() {
  34. this.urlChanged();
  35. }
  36. urlChanged() {
  37. if (this.pictureUrl) {
  38. this.hasPicture = true;
  39. } else {
  40. this.hasPicture = false;
  41. }
  42. }
  43. openFile(): void {
  44. let $wxService = this.wxService;
  45. let that = this;
  46. wx.chooseImage({
  47. count: 1, // 默认9
  48. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  49. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  50. success: function (chooseRes) {
  51. //console.log(chooseRes);
  52. that.presentToast(chooseRes[0].toString(),"success");
  53. that.presentToast(chooseRes[1],"success");
  54. that.presentToast(chooseRes,"success");
  55. that.presentToast(chooseRes.localIds,"success");
  56. if (chooseRes.localIds.length > 0) {
  57. let localId = chooseRes.localIds[0];// 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
  58. that.presentToast(localId,"success");
  59. if (localId.indexOf("wxlocalresource") != -1) {
  60. localId = localId.replace("wxlocalresource", "wxLocalResource");
  61. }
  62. wx.uploadImage({
  63. localId: localId,
  64. isShowProgressTips: 1,
  65. success: function (uploadRes) {
  66. $wxService.uploadMediaID(uploadRes.serverId)
  67. .then(res => {
  68. if (res.isSuccess) {
  69. let pictureUrl = res.message;
  70. that.hasPicture = true;
  71. that.pictureUrl = pictureUrl;
  72. that.propagateChange(that.pictureUrl);
  73. } else {
  74. that.presentToast(res.message,res.isSuccess?"success":"error");
  75. // that.reload();
  76. }
  77. }); // localData是图片的base64数据,可以用img标签显示
  78. }
  79. });
  80. }
  81. }
  82. });
  83. }
  84. presentToast(message:string,classstyle:string) {
  85. let toast = this.toastCtrl.create({
  86. message: message,
  87. duration: 3000,
  88. position: 'middle',
  89. cssClass: classstyle
  90. });
  91. toast.onDidDismiss(() => {
  92. //console.log('Dismissed toast');
  93. });
  94. toast.present();
  95. }
  96. }