123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- var d = dialog({contentType:'load', skin:'bk-popup'});
- var survey_id,
- q_info,
- indexArr = [],
- answerInfo = [],
- allAnswer = {},
- pre_info;
- $(function(){
- survey_id = localStorage.getItem("survey_id");
- getQuestionInfo(1);
- bindEvents();
- });
- function getQuestionInfo(sort){
- d.show();
- var url = "/patient/questionnaire/getQuestions",
- params = {
- id: survey_id,
- sort: sort
- };
- sendPost(url, params, 'JSON', 'GET', errorHandler, successHandler);
- }
- function errorHandler(res){
- d.close();
- if(res.msg){
- dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg,bottom:true}).show();
- }else{
- dialog({contentType:'tipsbox', skin:'bk-popup' , content:'数据加载失败!',bottom:true}).show();
- }
- }
- function successHandler(res){
- if(res.status == 200){
- d.close();
- var data = res.data;
- q_info = res.data;
- //计算进度比例 = 已完成题/(未完成题+已完成题)
- var noAns = parseInt(data.totalNum) - parseInt(data.sort)+1,
- ans = answerInfo.length,
- percent = parseInt((ans/(ans + noAns)).toFixed(2)*100);
- indexArr.push(data.sort);
- if(data.sort == 1){
- //如果只有一题的话,直接显示提交按钮
- if(data.totalNum == 1){
- $(".group1").hide();
- $(".group2").hide();
- $(".group3").hide();
- $(".group4").show();
- }else{
- $(".group1").show();
- $(".group2").hide();
- $(".group3").hide();
- $(".group4").hide();
- }
- }
- else if(data.sort > 1 && data.sort!=data.totalNum){
- $(".group1").hide();
- $(".group2").show();
- $(".group3").hide();
- $(".group4").hide();
- }else{
- $(".group1").hide();
- $(".group2").hide();
- $(".group3").show();
- $(".group4").hide();
- }
-
- $(".percent").text(percent+"%");
- $(".progress").css("width", percent+"%");
- if(data.type !=2){
- for(i=0; i<data.options.length; i++){
- var op = data.options[i];
- if(pre_info){
- if(pre_info.options){
- for(j=0; j<pre_info.options.length; j++){
- var p_op = pre_info.options[j];
- if(op.code == p_op.optionCode){
- data.options[i].checked = "checked";
- data.options[i].comment = p_op.comment;
- }
- }
- }
- }else{
- if(allAnswer[q_info.sort]){
- var info = allAnswer[q_info.sort];
- if(info.options){
- for(j=0; j<info.options.length; j++){
- var p_op = info.options[j];
- if(op.code == p_op.optionCode){
- data.options[i].checked = "checked";
- data.options[i].comment = p_op.comment;
- }
- }
- }
- }
- }
-
- data.options[i].jsonStr = JSON.stringify(data.options[i]);
- }
- }else{
- if(pre_info){
- data.answer = pre_info.content;
- }else{
- if(allAnswer[q_info.sort]){
- data.answer = allAnswer[q_info.sort].content;
- }
- }
- }
- var html = template("info_tmp", {data: data, pre_info: pre_info});
- $("#info").empty().append(html);
- }else{
- errorHandler(res);
- }
- }
- template.helper("setType", function(str){
- if(str == "0"){
- return "radio";
- }
- if(str == "1"){
- return "checkbox";
- }
- return "text";
- });
- function validate(){
- //查看选中选项
- var $checked = $(".icon-checkbox:checked"),
- len = $checked.length,
- text = $(".form-textarea").val();
- var obj = {},
- nextCode = "";
-
- if(q_info.nextQuestion >= 0){
- nextCode = q_info.nextQuestion;
- }
- //必填情况判断
- if(q_info.isRequired){
- if(q_info.type !=2){
- if(len == 0){
- dialog({
- contentType:'tipsbox',
- skin:'bk-popup' ,
- content:'此题为必答题,请按要求作答'
- }).show();
- return false;
- }
- }else{
- if($.trim(text).length == 0){
- dialog({
- contentType:'tipsbox',
- skin:'bk-popup' ,
- content:'此题为必答题,请按要求作答'
- }).show();
- return false;
- }
- }
- }
- if(len > 0){
- if(q_info.type == "1"){
- //多选题
- if(q_info.minNum && len < q_info.minNum){
- dialog({
- contentType:'tipsbox',
- skin:'bk-popup' ,
- content:'此题要求最少选择'+q_info.minNum+"项,请按要求作答"
- }).show();
- return false;
- }else if(q_info.maxNum && len > q_info.maxNum){
- dialog({
- contentType:'tipsbox',
- skin:'bk-popup' ,
- content:'此题要求最多选择'+q_info.maxNum+"项,请按要求作答"
- }).show();
- return false;
- }
- }
- obj.qstCode = q_info.qstCode;
- obj.sort = q_info.sort;
- obj.type = q_info.type;
- obj.options = [];
- for(i=0; i<len; i++){
- var $item = $($checked[i]),
- $li = $item.parent(),
- jsonData = $li.attr("data-json");
- jsonData = JSON.parse(jsonData);
- var op = {};
- op.optionCode = jsonData.code;
- op.sort = jsonData.sort;
- if(jsonData.havaComment == 1){
- var $text = $li.find("input[type=text]");
- if(jsonData.isRequired && ($.trim($text.val()).length == 0)){
- dialog({
- contentType:'tipsbox',
- skin:'bk-popup' ,
- content:'请按要求作答'
- }).show();
- return false;
- }else{
- op.comment = $.trim($text.val());
- }
- }
- obj.options.push(op);
- if(jsonData.nextQuestion >= 0){
- nextCode = jsonData.nextQuestion
- }
- }
- }else{
- //文本框内容
- nextCode = q_info.nextCode || (q_info.sort+1);
- obj.qstCode = q_info.qstCode;
- obj.sort = q_info.sort;
- obj.type = q_info.type;
- obj.content = $.trim(text);
- }
- //将答题信息保存到storage中
- answerInfo.push(obj);
- allAnswer[q_info.sort] = obj;
- pre_info = undefined;
- //发送请求获取下一题的内容
- if(isNaN(parseInt(nextCode))){
- nextCode = q_info.sort+1
- }
- return nextCode;
- //hard code 下一题的值
- // return (q_info.sort +1);
- }
- function bindEvents(){
- $(".next_btn").on("click", function(){
- var nextCode = validate();
- if(nextCode){
- getQuestionInfo(nextCode);
- }
- if(parseInt(nextCode) == 0){
- submitAnswer();
- }
- });
-
- $(".pre_btn").on("click", function(){
- //获取上一题的内容
- pre_info = answerInfo[answerInfo.length-1];
- answerInfo.pop();
- getQuestionInfo(pre_info.sort);
- });
-
- $(".submit_btn").on("click", function(){
- var result = validate();
- if(result){
- submitAnswer();
- }
- });
- }
- function submitAnswer(){
- dialog({
- content: "是否确认提交问卷?",
- okValue: "立即提交",
- ok: function(){
- d.show();
- var url = "/patient/questionnaire/saveAnswer",
- params = {
- surveyCode: survey_id,
- questions: answerInfo
- };
- sendPost(url, {jsonData: JSON.stringify(params)}, 'JSON', 'POST', function(res){
- d.close();
- if(res.msg){
- dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg,bottom:true}).show();
- }else{
- dialog({contentType:'tipsbox', skin:'bk-popup' , content:'数据提交失败!',bottom:true}).show();
- }
- }, function(res){
- if(res.status == 200){
- dialog({contentType:'tipsbox', skin:'bk-popup' , content: "已提交成功,感谢您的参与",bottom:true}).show();
- wx.closeWindow();
- }else{
- dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg,bottom:true}).show();
- }
- });
- },
- cancelValue: "我再看看",
- cancel: function(){
- answerInfo.pop();
- }
- }).showModal();
- }
|