Skip to content
issue.ts 3.88 KiB
Newer Older
wangqinghua's avatar
wangqinghua committed
import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {AppService} from "../../../service/http.service";
import {message} from "../../../app/main";
import {TabsService} from "../../tabs/tabs.service";
import {CommonService} from "../../../provide/common.service";
wangqinghua's avatar
wangqinghua committed


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

wangqinghua's avatar
wangqinghua committed
    correntAnswer = '';
wangqinghua's avatar
wangqinghua committed

wangqinghua's avatar
wangqinghua committed
    qType;  //题目类型
    temp;  //问卷信息
    title;  //标题
    index = 1;  //序号

    quesDescList = [];

    constructor(public navCtrl: NavController, public navParams: NavParams, private tabSer: TabsService,
                public appService: AppService, public toastCtrl: ToastController,private commonSer:CommonService) {
    }

    ionViewDidLoad() {
        this.qType = this.navParams.get('type');    //问卷信息
    }

    addOption() {
        const option = {
            comment: '',  //选项文字
            option: this.index
        }; //序号
        this.index++;
        this.quesDescList.push(option);
    }

    removeOption(i) {
        if (this.quesDescList.length > 1) {
            this.quesDescList.splice(i, 1);
        } else {
            this.appService.popToastView('至少一个选项', 'middle', 1500);
        }

    }

    //多选
    mutiSelect(i, option) {
        let op = String.fromCharCode(64 + ~~option);
        if (this.correntAnswer.indexOf(op) != -1) {
            this.correntAnswer = this.correntAnswer.replace(op + ';', '');
        } else {
            this.correntAnswer += op + ';';
        }
    }

    //提交问卷
    submit() {
        if (!this.check()) {
            return false;
        }
        if (this.qType == 1) {
            const option1 = {
                comment: '',
                option: '1'
            }; //序号
            const option2 = {
                comment: '',
                option: '2'
            }; //序号
            this.quesDescList.push(option1);
            this.quesDescList.push(option2);
        }
        let current;
        if (this.qType == '3') {
            current = this.correntAnswer;
        } else {
            current = String.fromCharCode(64 + ~~this.correntAnswer);
        }
        let arr = [];
        this.quesDescList.forEach(e => {
            const obj = {
                comment: e.comment,
                option: String.fromCharCode(64 + ~~e.option)
            };
            arr.push(obj);
        });

        const data = {
                stem: this.title,
                type: this.qType,
                correntAnswer: current,
                options: arr
            };
        this.tabSer.saveMineQuestion(data).subscribe(
            (res) => {
                if(res.errcode == '1000'){
wangqinghua's avatar
wangqinghua committed
                    let index = this.navCtrl.length() - 1;
                    this.navCtrl.remove(1, index);
wangqinghua's avatar
wangqinghua committed
                    this.commonSer.toast('提问成功');
wangqinghua's avatar
wangqinghua committed
                }else{
                    this.commonSer.toast(res.errMsg);
wangqinghua's avatar
wangqinghua committed
                }
            }
        )
    }

    //校验
    check() {
        if (!this.title) {
            this.appService.popToastView('请输入标题', 'middle', 1000);
            return false;
        }
        if (this.qType == 2 || this.qType == 3) {
            if (this.quesDescList.length == 0) {
                this.appService.popToastView('请输入选项', 'middle', 1000);
                return false;
            } else {
                let index = 0;
                this.quesDescList.forEach((res) => {
                    if (res.comment.length == 0) {
                        index++;
                    }
                });
                if (index > 0) {
                    this.appService.popToastView('请输入选项', 'middle', 1000);
                    return false;
                }

            }
        }
        return true;
    }
wangqinghua's avatar
wangqinghua committed

}