Skip to content
repair-apply.ts 5.32 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component} from '@angular/core';
import {ActionSheetController, IonicPage, LoadingController, NavController, NavParams} from 'ionic-angular';
import {AppGlobal} from "../../../../service/http.service";
import {CommonService} from "../../../../provide/common.service";
import {Camera, CameraOptions} from "@ionic-native/camera";
import {FileTransfer, FileTransferObject, FileUploadOptions} from "@ionic-native/file-transfer";
import {ServeService} from "../../serve.service";

@Component({
    selector: 'page-repair-apply',
    templateUrl: 'repair-apply.html',
})
export class RepairApplyPage {

    rId;
    pictrue = AppGlobal.domain + '/wisdomgroup';
    obj = <any>{
        equipmentName: '',
        remark: '',
        urlList: []
    };
    status;
    actionSheet_image;

    constructor(public navCtrl: NavController, public navParams: NavParams,
                public camera: Camera, private serveSer: ServeService,
                public loadingCtrl: LoadingController, public transfer: FileTransfer,
                private actionSheetCtrl: ActionSheetController, private commonSer: CommonService) {
    }

    ionViewDidLoad() {
        this.rId = this.navParams.get('id');
        if (this.rId) this.getDetail();
    }

    getDetail() {
        this.serveSer.editRepair(this.rId).subscribe(
            (res) => {
                for (let key in this.obj) {
                    if (res.data[key]) this.obj[key] = res.data[key];
                }
                this.status = res.data.status;
            }
        )
    }

    tackePic() {
        this.actionSheet_image = this.actionSheetCtrl.create({
            cssClass: 'cameraAction',
            buttons: [
                {
                    text: '拍照',
                    role: 'fromCamera',
                    handler: () => {
                        console.log('fromCamera');
                        this.selectPicture(1);
                    }
                }, {
                    text: '从相册中选',
                    role: 'fromPhoto',
                    handler: () => {
                        console.log('fromPhoto');
                        this.selectPicture(0);
                    }
                }, {
                    text: '取消',
                    role: 'cancel',
                    handler: () => {
                        console.log('Cancel clicked');
                    }
                }
            ]
        });
        this.actionSheet_image.present();
    }

    //选择图片
    selectPicture(srcType) {
        if (this.obj.urlList.length > 8) {
            this.commonSer.toast('一批文章最多上传9个图片');
            return false;
        }
        const options: CameraOptions = {
            quality: 70,
            destinationType: this.camera.DestinationType.FILE_URI,
            encodingType: this.camera.EncodingType.PNG,
            mediaType: this.camera.MediaType.PICTURE,
            sourceType: srcType,
            saveToPhotoAlbum: false
        };
        const option: FileUploadOptions = {
            httpMethod: 'POST',
            headers: {
                'Accept': 'application/json',
            },
            fileName: 'image.png'
        };
        this.camera.getPicture(options).then((imagedata) => {
            let filePath = imagedata;
            if (filePath.indexOf('?') !== -1) {     //获取文件名
                filePath = filePath.split('?')[0];
            }
            let arr = filePath.split('/');
            console.log(imagedata);
            option.fileName = arr[arr.length - 1];
            this.upload(imagedata, option);
        })
    }

    upload(file, options) {
        this.commonSer.log(file);
        const uploadLoading = this.loadingCtrl.create({
            content: '上传中...',
            dismissOnPageChange: true,
            enableBackdropDismiss: true,
        });
        uploadLoading.present();
        const fileTransfer: FileTransferObject = this.transfer.create();
        fileTransfer.upload(file, AppGlobal.domain + '/wisdomgroup/modules/common/file/upload1', options).then(
            (res) => {
                uploadLoading.dismiss();
                this.commonSer.toast('上传成功');
                const data = JSON.parse(res.response);
                this.commonSer.log(data.data[0].filePath);
                this.obj.urlList.push(data.data[0].filePath);
            }, err => {
                uploadLoading.dismiss();
                this.commonSer.toast('上传错误');
            });
        fileTransfer.onProgress((listener) => {
            let per = <any>(listener.loaded / listener.total) * 100;
            per = Math.round(per * Math.pow(10, 2)) / Math.pow(10, 2)
            uploadLoading.setContent('上传中...' + per + '%');
        })
    }

    //删除文件
    deleteFile(i) {
        this.obj.urlList.splice(i, 1);
    }

    //提交
    submit() {
        this.commonSer.alert('确定提交申请?', () => {
            if (this.rId) this.obj.id = this.rId;
            this.serveSer.saveRepair(this.obj).subscribe(
                (res) => {
                    if (res.errcode == 1000) {
                        this.commonSer.toast('申请报修成功');
                        this.navCtrl.pop();
                    } else {
                        this.commonSer.toast(res.errmsg);
                    }
                }
            )
        });
    }

}