question.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. var d = dialog({contentType:'load', skin:'bk-popup'});
  2. var survey_id,
  3. q_info,
  4. indexArr = [],
  5. answerInfo = [],
  6. allAnswer = {},
  7. pre_info;
  8. $(function(){
  9. survey_id = localStorage.getItem("survey_id");
  10. getQuestionInfo(1);
  11. bindEvents();
  12. });
  13. function getQuestionInfo(sort){
  14. d.show();
  15. var url = "/patient/questionnaire/getQuestions",
  16. params = {
  17. id: survey_id,
  18. sort: sort
  19. };
  20. sendPost(url, params, 'JSON', 'GET', errorHandler, successHandler);
  21. }
  22. function errorHandler(res){
  23. d.close();
  24. if(res.msg){
  25. dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg,bottom:true}).show();
  26. }else{
  27. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'数据加载失败!',bottom:true}).show();
  28. }
  29. }
  30. function successHandler(res){
  31. if(res.status == 200){
  32. d.close();
  33. var data = res.data;
  34. q_info = res.data;
  35. //计算进度比例 = 已完成题/(未完成题+已完成题)
  36. var noAns = parseInt(data.totalNum) - parseInt(data.sort)+1,
  37. ans = answerInfo.length,
  38. percent = parseInt((ans/(ans + noAns)).toFixed(2)*100);
  39. indexArr.push(data.sort);
  40. if(data.sort == 1){
  41. //如果只有一题的话,直接显示提交按钮
  42. if(data.totalNum == 1){
  43. $(".group1").hide();
  44. $(".group2").hide();
  45. $(".group3").hide();
  46. $(".group4").show();
  47. }else{
  48. $(".group1").show();
  49. $(".group2").hide();
  50. $(".group3").hide();
  51. $(".group4").hide();
  52. }
  53. }
  54. else if(data.sort > 1 && data.sort!=data.totalNum){
  55. $(".group1").hide();
  56. $(".group2").show();
  57. $(".group3").hide();
  58. $(".group4").hide();
  59. }else{
  60. $(".group1").hide();
  61. $(".group2").hide();
  62. $(".group3").show();
  63. $(".group4").hide();
  64. }
  65. $(".percent").text(percent+"%");
  66. $(".progress").css("width", percent+"%");
  67. if(data.type !=2){
  68. for(i=0; i<data.options.length; i++){
  69. var op = data.options[i];
  70. if(pre_info){
  71. if(pre_info.options){
  72. for(j=0; j<pre_info.options.length; j++){
  73. var p_op = pre_info.options[j];
  74. if(op.code == p_op.optionCode){
  75. data.options[i].checked = "checked";
  76. data.options[i].comment = p_op.comment;
  77. }
  78. }
  79. }
  80. }else{
  81. if(allAnswer[q_info.sort]){
  82. var info = allAnswer[q_info.sort];
  83. if(info.options){
  84. for(j=0; j<info.options.length; j++){
  85. var p_op = info.options[j];
  86. if(op.code == p_op.optionCode){
  87. data.options[i].checked = "checked";
  88. data.options[i].comment = p_op.comment;
  89. }
  90. }
  91. }
  92. }
  93. }
  94. data.options[i].jsonStr = JSON.stringify(data.options[i]);
  95. }
  96. }else{
  97. if(pre_info){
  98. data.answer = pre_info.content;
  99. }else{
  100. if(allAnswer[q_info.sort]){
  101. data.answer = allAnswer[q_info.sort].content;
  102. }
  103. }
  104. }
  105. var html = template("info_tmp", {data: data, pre_info: pre_info});
  106. $("#info").empty().append(html);
  107. }else{
  108. errorHandler(res);
  109. }
  110. }
  111. template.helper("setType", function(str){
  112. if(str == "0"){
  113. return "radio";
  114. }
  115. if(str == "1"){
  116. return "checkbox";
  117. }
  118. return "text";
  119. });
  120. function validate(){
  121. //查看选中选项
  122. var $checked = $(".icon-checkbox:checked"),
  123. len = $checked.length,
  124. text = $(".form-textarea").val();
  125. var obj = {},
  126. nextCode = "";
  127. if(q_info.nextQuestion >= 0){
  128. nextCode = q_info.nextQuestion;
  129. }
  130. //必填情况判断
  131. if(q_info.isRequired){
  132. if(q_info.type !=2){
  133. if(len == 0){
  134. dialog({
  135. contentType:'tipsbox',
  136. skin:'bk-popup' ,
  137. content:'此题为必答题,请按要求作答'
  138. }).show();
  139. return false;
  140. }
  141. }else{
  142. if($.trim(text).length == 0){
  143. dialog({
  144. contentType:'tipsbox',
  145. skin:'bk-popup' ,
  146. content:'此题为必答题,请按要求作答'
  147. }).show();
  148. return false;
  149. }
  150. }
  151. }
  152. if(len > 0){
  153. if(q_info.type == "1"){
  154. //多选题
  155. if(q_info.minNum && len < q_info.minNum){
  156. dialog({
  157. contentType:'tipsbox',
  158. skin:'bk-popup' ,
  159. content:'此题要求最少选择'+q_info.minNum+"项,请按要求作答"
  160. }).show();
  161. return false;
  162. }else if(q_info.maxNum && len > q_info.maxNum){
  163. dialog({
  164. contentType:'tipsbox',
  165. skin:'bk-popup' ,
  166. content:'此题要求最多选择'+q_info.maxNum+"项,请按要求作答"
  167. }).show();
  168. return false;
  169. }
  170. }
  171. obj.qstCode = q_info.qstCode;
  172. obj.sort = q_info.sort;
  173. obj.type = q_info.type;
  174. obj.options = [];
  175. for(i=0; i<len; i++){
  176. var $item = $($checked[i]),
  177. $li = $item.parent(),
  178. jsonData = $li.attr("data-json");
  179. jsonData = JSON.parse(jsonData);
  180. var op = {};
  181. op.optionCode = jsonData.code;
  182. op.sort = jsonData.sort;
  183. if(jsonData.havaComment == 1){
  184. var $text = $li.find("input[type=text]");
  185. if(jsonData.isRequired && ($.trim($text.val()).length == 0)){
  186. dialog({
  187. contentType:'tipsbox',
  188. skin:'bk-popup' ,
  189. content:'请按要求作答'
  190. }).show();
  191. return false;
  192. }else{
  193. op.comment = $.trim($text.val());
  194. }
  195. }
  196. obj.options.push(op);
  197. if(jsonData.nextQuestion >= 0){
  198. nextCode = jsonData.nextQuestion
  199. }
  200. }
  201. }else{
  202. //文本框内容
  203. nextCode = q_info.nextCode || (q_info.sort+1);
  204. obj.qstCode = q_info.qstCode;
  205. obj.sort = q_info.sort;
  206. obj.type = q_info.type;
  207. obj.content = $.trim(text);
  208. }
  209. //将答题信息保存到storage中
  210. answerInfo.push(obj);
  211. allAnswer[q_info.sort] = obj;
  212. pre_info = undefined;
  213. //发送请求获取下一题的内容
  214. if(isNaN(parseInt(nextCode))){
  215. nextCode = q_info.sort+1
  216. }
  217. return nextCode;
  218. //hard code 下一题的值
  219. // return (q_info.sort +1);
  220. }
  221. function bindEvents(){
  222. $(".next_btn").on("click", function(){
  223. var nextCode = validate();
  224. if(nextCode){
  225. getQuestionInfo(nextCode);
  226. }
  227. if(parseInt(nextCode) == 0){
  228. submitAnswer();
  229. }
  230. });
  231. $(".pre_btn").on("click", function(){
  232. //获取上一题的内容
  233. pre_info = answerInfo[answerInfo.length-1];
  234. answerInfo.pop();
  235. getQuestionInfo(pre_info.sort);
  236. });
  237. $(".submit_btn").on("click", function(){
  238. var result = validate();
  239. if(result){
  240. submitAnswer();
  241. }
  242. });
  243. }
  244. function submitAnswer(){
  245. dialog({
  246. content: "是否确认提交问卷?",
  247. okValue: "立即提交",
  248. ok: function(){
  249. d.show();
  250. var url = "/patient/questionnaire/saveAnswer",
  251. params = {
  252. surveyCode: survey_id,
  253. questions: answerInfo
  254. };
  255. sendPost(url, {jsonData: JSON.stringify(params)}, 'JSON', 'POST', function(res){
  256. d.close();
  257. if(res.msg){
  258. dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg,bottom:true}).show();
  259. }else{
  260. dialog({contentType:'tipsbox', skin:'bk-popup' , content:'数据提交失败!',bottom:true}).show();
  261. }
  262. }, function(res){
  263. if(res.status == 200){
  264. dialog({contentType:'tipsbox', skin:'bk-popup' , content: "已提交成功,感谢您的参与",bottom:true}).show();
  265. wx.closeWindow();
  266. }else{
  267. dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg,bottom:true}).show();
  268. }
  269. });
  270. },
  271. cancelValue: "我再看看",
  272. cancel: function(){
  273. answerInfo.pop();
  274. }
  275. }).showModal();
  276. }