import { Component } from '@angular/core' ; import { NavController , ActionSheet , ViewController , LocalStorage , Storage } from 'ionic-angular' ; import { ImagePicker , Camera , Transfer } from 'ionic-native' ; import { Http , Headers , RequestOptions } from '@angular/http' ; import { Helper } from '../helper/helper' ; @ Component ( { templateUrl : 'build/pages/chose_img/chose_img.html' } ) export class ChoseImgPage { local : Storage ; id : any ; helper : Helper ; img_url : string = 'images/user_head.jpg' ; headers : Headers ; options : RequestOptions ; private uploadImgUrl : string = 'http://119.29.140.85/index.php/user/upload_head' ; constructor ( private nav , private viewCtrl , private http ) { this . helper = new Helper ( nav ) ; this . local = new Storage ( LocalStorage ) ; } goBack ( ) { this . nav . pop ( ) ; } choseUserHeadImg ( ) { this . presentActionSheet ( ) ; } presentActionSheet ( ) { let actionSheet = ActionSheet . create ( { buttons : [ { text : '拍照' , role : 'destructive' , handler : ( ) => { var options = { quality : 50 , destinationType : Camera . DestinationType . FILE_URI , sourceType : Camera . PictureSourceType . CAMERA , encodingType : Camera . EncodingType . JPEG , mediaType : Camera . MediaType . PICTURE , allowEdit : true , correctOrientation : true } ; Camera . getPicture ( options ) . then ( ( imageData ) => { this . img_url = imageData ; this . transferUpLoad ( ) ; } , ( err ) => { } ) ; } } , { text : '从手机相册选择' , handler : ( ) => { this . helper . presentToast ( '开始调用相册' ) ; let options = { maximumImagesCount : 1 , width : 800 , height : 800 , quality : 80 } ; ImagePicker . getPictures ( options ) . then ( ( results ) => { for ( var i = 0 ; i < results . length ; i ++ ) { this . img_url = results [ i ] ; } this . transferUpLoad ( ) ; } , ( err ) => { } ) ; } } ] } ) ; this . nav . present ( actionSheet ) ; } transferUpLoad ( ) { const fileTransfer = new Transfer ( ) ; var options ; options = { fileKey : 'image' , headers : { } , params : { 'id' : this . id } } ; fileTransfer . upload ( this . img_url , this . uploadImgUrl , options ) . then ( ( data ) => { let result = JSON . stringify ( data ) ; let rData = eval ( "(" + result + ")" ) ; let back_img_url = 'http://119.29.140.85' + rData . response . data . head_path ; this . helper . presentToast ( '头像上传成功！' ) ; this . local . set ( 'head_pic_url' , rData . response . data . head_path ) ; } , ( err ) => { this . helper . presentToast ( '上传失败！' ) ; } ) ; } ionViewLoaded ( ) { this . headers = new Headers ( { 'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8' } ) ; this . options = new RequestOptions ( { headers : this . headers } ) ; let localData = JSON . stringify ( window . localStorage ) ; let localJson = eval ( "(" + localData + ")" ) ; this . id = localJson . id ; this . img_url = 'http://119.29.140.85' + localJson . head_pic_url ; } ionViewWillEnter ( ) { this . viewCtrl . setBackButtonText ( '返回' ) ; } }