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

@IonicPage()
@Component({
wangqinghua's avatar
wangqinghua committed
    selector: 'page-publish',
    templateUrl: 'publish.html',
wangqinghua's avatar
wangqinghua committed
})
export class PublishPage {

wangqinghua's avatar
wangqinghua committed
    //1党规党章,2系列讲话,3中央精神,4本市部署,5通知公告,6党建动态,7工作提示, 8党务参考,9廉政格言,10纪检提示,11风险排查,12警示教育,13支部活动,14党建联建,15结对帮扶
    plateList = [
        {name: '党规党章', id: '1'}, {name: '系列讲话', id: '2'}, {name: '中央精神', id: '3'}, {
            name: '本市部署',
            id: '4'
        }, {name: '通知公告', id: '5'},
        {name: '党建动态', id: '6'}, {name: '工作提示', id: '7'}, {name: '党务参考', id: '8'}, {
            name: '廉政格言',
            id: '9'
        }, {name: '纪检提示', id: '10'},
        {name: '风险排查', id: '11'}, {name: '警示教育', id: '12'}, {name: '支部活动', id: '13'}, {
            name: '党建联建',
            id: '14'
        }, {name: '结对帮扶', id: '15'},
    ];
    obj = {
        type: '1',
        title: '',
        content: '',
        publishRange: '',
    };
wangqinghua's avatar
wangqinghua committed
    fileList = [];   //文件数组
wangqinghua's avatar
wangqinghua committed
    group;  //用户组
    userid;  //当前登录人的id
    file;  // 文件

wangqinghua's avatar
wangqinghua committed
    pictrue = AppGlobal.domain + '/wisdomgroup';

    type;   //视频文章or图片文章
wangqinghua's avatar
wangqinghua committed

    constructor(public navCtrl: NavController, public navParams: NavParams, public storage: Storage,
                public camera: Camera, public actionSheetCtrl: ActionSheetController,
wangqinghua's avatar
wangqinghua committed
                public loadingCtrl: LoadingController, public transfer: FileTransfer,
wangqinghua's avatar
wangqinghua committed
                public commonSer: CommonService, public tabSer: TabsService) {
    }

    ionViewDidLoad() {
wangqinghua's avatar
wangqinghua committed
        this.type = this.navParams.get('type');
wangqinghua's avatar
wangqinghua committed
        this.storage.get("userLoginInfo").then((value) => {
            this.userid = value.userid;
        })
        //获取用户组
        this.tabSer.getGroup().subscribe(
            (res) => {
                this.group = res.list;
            }
        )
    }


    tackePic() {
        const actionSheet = this.actionSheetCtrl.create({
            title: '上传图片',
            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');
                    }
                }
            ]
        });
        actionSheet.present();
    }

    //选择图片
    selectPicture(srcType) {
        const options: CameraOptions = {
            quality: 100,
wangqinghua's avatar
wangqinghua committed
            destinationType: this.camera.DestinationType.FILE_URI,
            encodingType: this.camera.EncodingType.PNG,
wangqinghua's avatar
wangqinghua committed
            mediaType: this.camera.MediaType.PICTURE,
            sourceType: srcType,
            saveToPhotoAlbum: false
        };
wangqinghua's avatar
wangqinghua committed
        const option: FileUploadOptions = {
            httpMethod: 'POST',
            headers: {
                'Accept': 'application/json', //不加入 发生错误!!
            },
            fileName:'image.png'
        };
wangqinghua's avatar
wangqinghua committed
        this.camera.getPicture(options).then((imagedata) => {
wangqinghua's avatar
wangqinghua committed
            if(imagedata.indexOf('?') !== -1){    //获取文件名
                imagedata = imagedata.split('?')[0];
            }
            let arr = imagedata.split('/');
            this.upload(arr[arr.length -1], option);
wangqinghua's avatar
wangqinghua committed
        })
wangqinghua's avatar
wangqinghua committed
    }
wangqinghua's avatar
wangqinghua committed

    //选择视频
    selectVideo() {
        const options: CameraOptions = {
            quality: 30,
            destinationType: this.camera.DestinationType.FILE_URI,
            mediaType: this.camera.MediaType.VIDEO,
            sourceType: 0,
        };
wangqinghua's avatar
wangqinghua committed
        const option: FileUploadOptions = {
            httpMethod: 'POST',
            headers: {
                'Accept': 'application/json', //不加入 发生错误!!
            },
            fileName:'video.mp4',
            mimeType:'mp4'
wangqinghua's avatar
wangqinghua committed
        };
        this.camera.getPicture(options).then((videoData) => {
wangqinghua's avatar
wangqinghua committed
            if(videoData.indexOf('?') !== -1){     //获取文件名
                videoData = videoData.split('?')[0];
            }
            let arr = videoData.split('/');
            this.upload(arr[arr.length -1], option);
            this.upload(videoData, option);
wangqinghua's avatar
wangqinghua committed
        })

    }


    //上传文件或视频
    // 接口返回参数的格式
    // {
    //     "code" : 0,
    //     "data" : [ {
    //         "id" : null,
    //         "fileName" : null,
    //         "fileType" : "image/png",
    //         "showName" : "demo.png",
    //         "uploadTime" : 1557734823883,
    //         "filePath" : "/upload/image/F1F18A0762814B25A2DA98344C38058/demo.png"
    //     } ],
    //     "error" : [ ]
    // }
wangqinghua's avatar
wangqinghua committed
    upload(file, options) {
        this.commonSer.log(file);
wangqinghua's avatar
wangqinghua committed
        const uploadLoading = this.loadingCtrl.create({
wangqinghua's avatar
wangqinghua committed
            content: '上传中...',
            dismissOnPageChange:true,
            enableBackdropDismiss:true,
wangqinghua's avatar
wangqinghua committed
        });
        uploadLoading.present();
        const fileTransfer: FileTransferObject = this.transfer.create();
wangqinghua's avatar
wangqinghua committed
        fileTransfer.upload(file, AppGlobal.domain + '/wisdomgroup/modules/common/file/upload1', options).then(
wangqinghua's avatar
wangqinghua committed
            (res) => {
                uploadLoading.dismiss();
                this.commonSer.toast('上传成功');
                const data = JSON.parse(res.response);
wangqinghua's avatar
wangqinghua committed
                const obj = {
                    resourceType: 1,
                    path: data.data[0].filePath,
                    name: data.data[0].showName,
                };
                this.commonSer.log(data);
                this.fileList.push(obj);
            }, err => {
wangqinghua's avatar
wangqinghua committed
                uploadLoading.dismiss();
                this.commonSer.toast('上传错误');
            });
    }

    //发布
    publish() {
        const data = {
            title: this.obj.title,
            plateType: this.obj.type,
            source: '上海市委组织部',
            publishStatus: '2',
            content: this.obj.content,
            publishRange: this.obj.publishRange,
            submitter: this.userid,
            stuffType: 1,
wangqinghua's avatar
wangqinghua committed
            attachments: this.fileList,
wangqinghua's avatar
wangqinghua committed
        };
        this.commonSer.alert('确定提交审核当前内容?', () => {
            this.tabSer.stuffSave(data).subscribe(
                (res) => {
                    this.commonSer.toast('已提交审核')
                    setTimeout(() => {
                        this.navCtrl.pop()
                    }, 800)
                }
            )
        })
wangqinghua's avatar
wangqinghua committed
    }
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
    //选择发布或者预览
    handle(){
        const actionSheet = this.actionSheetCtrl.create({
            title: '',
            buttons: [
                {
                    text: '预览',
                    handler: () => {
                        const obj = {
                            title:this.obj.title,
                            source: '上海市委组织部',
                            publishTime:new Date().getTime(),
                            content:this.obj.content,
                            attachments:this.fileList,
                        }
                        this.navCtrl.push('PreviewPage',{
                            stuff:obj
                        });
                    }
                }, {
                    text: '发布',
                    role: 'fromPhoto',
                    handler: () => {
                        this.publish();
                    }
                }, {
                    text: '取消',
                    role: 'cancel',
                    handler: () => {
                        console.log('Cancel clicked');
                    }
                }
            ]
        });
        actionSheet.present();
    }

wangqinghua's avatar
wangqinghua committed
}