edit_questions.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. var code,
  2. info,
  3. table_tp_id,
  4. last_index = 1; //当前的题目数 +1
  5. var question_info;
  6. mui.init();
  7. mui.plusReady(function(){
  8. var self = plus.webview.currentWebview();
  9. code = self.code;
  10. table_tp_id = self.table_tp_id;
  11. info = self.info;
  12. sqlite.open('MyData','1.0','My Database', 1024*100);
  13. createTable("questions2");
  14. //添加标记记录说模板内容已经被修改了
  15. plus.storage.setItem("isEdit", "1");
  16. if(code){
  17. //判断是否数据库中已经存在了数据
  18. sqlite.executeSql("select * from questions2 WHERE template_id = "+table_tp_id,[],
  19. function(tx,rs){
  20. console.log(rs);
  21. if(rs.rows.length == 0){
  22. if(self.question_info){
  23. console.log(self.question_info);
  24. fillQuestionData(self.question_info);
  25. }else{
  26. getQuestions();
  27. }
  28. }else{
  29. refresh();
  30. }
  31. },function(tx,rs){
  32. console.log(rs);
  33. });
  34. }else{
  35. refresh();
  36. }
  37. bindEvents();
  38. });
  39. function getQuestions(){
  40. var url = "/doctor/questionnaire/getTemplateDetail",
  41. params = {code: code};
  42. sendGet(url, params, null, function(res){
  43. if(res.status == 200){
  44. fillQuestionData(res.data.questions);
  45. }else{
  46. mui.toast(res.msg);
  47. }
  48. }, true);
  49. }
  50. function fillQuestionData(data){
  51. question_info = _.map(data,function(o,index){
  52. var options = _.map(o.options, function(op){
  53. op.nextQuestion = "";
  54. //判断是否存在选项的code,如果不存在则使用旧的code,如果不存在则新增
  55. var optCode = op.optCode || getUUID();
  56. op.optCode = optCode;
  57. return op;
  58. });
  59. o.options = options;
  60. o.optionArr = options;
  61. var code = o.qstCode || getUUID();
  62. o.code = code;
  63. o.index1 = index+1;
  64. o.nextQuestion = "";
  65. o.minOptions = o.minNum;
  66. o.maxOptions = o.maxNum;
  67. o.jsonStr = JSON.stringify(o);
  68. return o;
  69. });
  70. var html = template("question_tmp", {list: question_info});
  71. $("#question_list").append(html);
  72. last_index = question_info.length + 1;
  73. insertIntoTable();
  74. $(".header-link").addClass("active");
  75. }
  76. function insertIntoTable(){
  77. //将数据保存到数据表中
  78. sqlite.db.transaction(function(tx){
  79. for(var i=0; i<question_info.length; i++){
  80. var time = (new Date()).getTime();
  81. tmp = question_info[i];
  82. tx.executeSql(
  83. 'INSERT INTO questions2'+
  84. ' (template_id, index1, code, title, comment, type, isRequired, options, updateTime, sort, minOptions, maxOptions, nextQuestion) '+
  85. ' VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)',
  86. [
  87. table_tp_id, i+1, tmp.code, tmp.title, tmp.comment||"", tmp.type,
  88. tmp.isRequired, JSON.stringify(tmp.options), time, i+1, tmp.minOptions|| tmp.minNum ||"", tmp.maxOptions|| tmp.maxNum || "", ""
  89. ],
  90. function(tx, rs){
  91. console.log(rs)
  92. }, function(tx, rs){
  93. console.log(rs)
  94. });
  95. }
  96. }, function(tx, rs){
  97. console.log(rs)
  98. });
  99. }
  100. function bindEvents(){
  101. $("#add_question").on("click", function(){
  102. mui('#sheet1').popover('toggle');
  103. });
  104. $(".header-link").on("click", function(){
  105. var active = $(this).hasClass("active");
  106. if(active){
  107. mui.openWindow({
  108. url:"preview.html",
  109. id:"preview1",
  110. extras: {
  111. table_tp_id: table_tp_id,
  112. code: code,
  113. info: info,
  114. question_info: question_info
  115. }
  116. });
  117. }
  118. });
  119. $("#sheet1").on("tap", "li", function(){
  120. var val = $(this).attr("data-val");
  121. if(val == "questions"){
  122. openWebview("questions_list.html", {tp_id: table_tp_id, q_num: last_index});
  123. }else if(val == "blankfill"){
  124. openWebview("blank_filling_question.html", {tp_id: table_tp_id, q_num: last_index});
  125. }else if(val == "radio" || val == "checkbox"){
  126. openWebview("multiple_choice_question.html", {tp_id: table_tp_id, type: val, q_num: last_index});
  127. }
  128. mui('#sheet1').popover('toggle');
  129. });
  130. $("body").on("click", '.del-icon', function(e){
  131. e.stopPropagation();
  132. var $this = $(this);
  133. dialog({
  134. content: "确定要删除这个问题吗?",
  135. okValue: "确定",
  136. ok: function(){
  137. var $parent = $this.closest(".question"),
  138. index = parseInt($parent.find(".index").text()), //页面中的排序
  139. code = $parent.attr("data-code"), //数据库中的排序
  140. $index_list = $(".index"),
  141. index1 = $parent.attr("data-index"),
  142. len = $index_list.length;
  143. for(i=index; i<len; i++){
  144. var val = $($index_list[i]).text();
  145. $($index_list[i]).text(parseInt(val)-1);
  146. }
  147. $parent.remove();
  148. var sql = "delete from questions2 where template_id = "+table_tp_id+" and code = '"+code+"'";
  149. console.log(sql);
  150. sqlite.executeSql(sql,[],function(tx,rs){
  151. console.log("数据删除成功");
  152. console.log(rs);
  153. last_index = last_index - 1;
  154. if(last_index == 1){
  155. $(".header-link").removeClass("active");
  156. }
  157. question_info.splice(index-1, 1);
  158. //将本题后的题目的index-1,sort-1.因为会影响新增的代码
  159. var usql = "UPDATE questions2 SET index1=(index1-1), sort = (sort-1) WHERE template_id = "+table_tp_id+" AND index1 > "+index1;
  160. console.log(usql);
  161. sqlite.executeSql(usql,[],function(tx, rs){
  162. console.log("update success after delete");
  163. console.log(rs);
  164. },function(tx, rs){
  165. console.log("update failed after delete");
  166. console.log(rs);
  167. });
  168. }, function(tx, rs){
  169. console.log(rs);
  170. });
  171. },
  172. cancelValue: "取消",
  173. cancel: function(){}
  174. }).showModal();
  175. });
  176. $("body").on('click', '.question', function(){
  177. var $this = $(this),
  178. jsonData = $this.attr("data-json"),
  179. index1 = parseInt($this.find(".index").text());
  180. jsonData = JSON.parse(jsonData);
  181. if(jsonData.type == 2){
  182. openWebview("blank_filling_question.html", {
  183. tp_id: table_tp_id,
  184. jsonData: jsonData,
  185. q_index: index1,
  186. q_num: last_index-1
  187. });
  188. }else{
  189. openWebview("multiple_choice_question.html", {
  190. tp_id: table_tp_id,
  191. type: (jsonData.type == 0)? "radio" : "checkbox",
  192. jsonData: jsonData,
  193. q_index: index1,
  194. q_num: last_index-1
  195. });
  196. }
  197. });
  198. $("#pre_btn").on('click', function(){
  199. mui.back();
  200. });
  201. $("#next_btn").on('click', function(){
  202. if(question_info.length == 0){
  203. dialog({
  204. content: "最少需要一个问题,请添加问题",
  205. contentType: "tipsbox",
  206. skin: 'bk-popup',
  207. closeTime: 2000
  208. }).showModal();
  209. }else{
  210. openWebview("choose_respondent.html", {
  211. code: code,
  212. pre_info: info,
  213. question_info: question_info
  214. });
  215. }
  216. });
  217. window.addEventListener("addQuestion", function(e){
  218. var info = e.detail.info;
  219. sqlite.executeSql(
  220. 'INSERT INTO questions2 '+
  221. ' (template_id, index1, code, title, isRequired, comment, type, options, sort, updateTime, minOptions, maxOptions) '+
  222. ' VALUES(?,?,?,?,?,?,?,?,?,?,?,?)',
  223. [
  224. parseInt(info.tp_id), info.index, getUUID(), info.title, info.isRequired, info.comment, info.type, info.options,
  225. info.sort, info.updateTime, info.minOptions, info.maxOptions
  226. ],
  227. function(tx, rs){
  228. console.log(rs);
  229. refresh();
  230. }, function(tx, rs){
  231. console.log(rs)
  232. });
  233. });
  234. window.addEventListener("update", function(e){
  235. var info = e.detail.info,
  236. code = e.detail.code;
  237. console.log(info);
  238. //单双引号在sqlite中有区别,\无特殊含义,双引号代表着单引号的作用
  239. var sql = "UPDATE questions2 "+
  240. " SET title = \'"+info.title+"\', isRequired = "+info.isRequired+", type="+info.type+", comment= '"+info.comment+"', "+
  241. " updateTime="+info.updateTime+", sort = "+info.sort+", options = \'"+info.options+"\', minOptions='"+info.minOptions+"', maxOptions='"+info.maxOptions+"'"+
  242. " WHERE template_id = "+parseInt(info.tp_id)+" AND code = \'"+ code+"\'";
  243. console.log(sql);
  244. //如果index1< sort,则需要将之前的sort的数据往前挪
  245. var sql1 = "UPDATE questions2 SET sort="+(info.sort-1)+" WHERE template_id = "+parseInt(info.tp_id)+" AND sort = "+info.sort;
  246. console.log("sql1 = "+sql1);
  247. if(info.index < parseInt(info.sort)){
  248. sqlite.executeSql(sql1,[],
  249. function(tx, rs){
  250. console.log("update1 success");
  251. console.log(rs);
  252. sqlite.executeSql(sql,[],
  253. function(tx, rs){
  254. console.log("update success");
  255. console.log(rs);
  256. refresh();
  257. }, function(tx, rs){
  258. console.log(rs)
  259. });
  260. }, function(tx, rs){
  261. console.log(rs)
  262. });
  263. }else{
  264. //题目往前移的情况,将这个sort后的序号加1
  265. var sql2 = "UPDATE questions2 SET sort=sort+1 WHERE template_id = "+parseInt(info.tp_id)+" AND sort >= "+info.sort;
  266. console.log("sql2 = "+sql2);
  267. sqlite.executeSql(sql2,[],
  268. function(tx, rs){
  269. console.log("update2 success");
  270. console.log(rs);
  271. sqlite.executeSql(sql,[],
  272. function(tx, rs){
  273. console.log("update success");
  274. console.log(rs);
  275. refresh();
  276. }, function(tx, rs){
  277. console.log(rs)
  278. });
  279. }, function(tx, rs){
  280. console.log(rs)
  281. });
  282. }
  283. });
  284. window.addEventListener("multiInsert", function(e){
  285. var list = e.detail.list;
  286. console.log(list);
  287. var time = (new Date()).getTime();
  288. sqlite.db.transaction(function(tx){
  289. var sql = 'INSERT INTO questions2 '+
  290. ' (template_id, index1, code, title, isRequired, comment, type, options, sort, updateTime, minOptions, maxOptions) '+
  291. ' VALUES(?,?,?,?,?,?,?,?,?,?,?,?)';
  292. for(var i=0; i<list.length; i++){
  293. var item = list[i];
  294. tx.executeSql( sql,
  295. [
  296. table_tp_id, last_index+i, getUUID(), item.title, item.isRequired, item.comment||"", item.questionType,
  297. item.options?JSON.stringify(item.options):"",last_index+i, time, item.minOptions||"", item.maxOptions||""
  298. ] ,
  299. function(tx, rs){
  300. console.log(rs);
  301. refresh();
  302. }, function(tx, rs){
  303. console.log(rs);
  304. });
  305. }
  306. });
  307. });
  308. }
  309. template.helper("setType", function(str){
  310. if(str == 0){
  311. return "单选";
  312. }
  313. if(str == 1){
  314. return "多选";
  315. }
  316. if(str == 2){
  317. return "填空";
  318. }
  319. return "";
  320. });
  321. function refresh(){
  322. //获取数据表的数据,然后重新渲染页面内容
  323. sqlite.executeSql("SELECT * FROM questions2 WHERE "+
  324. "template_id = "+table_tp_id+" ORDER BY sort ASC, updateTime DESC",[],
  325. function(tx, rs){
  326. console.log(rs);
  327. var data = [];
  328. console.log(rs.rows);
  329. for(var i=0; i<rs.rows.length; i++){
  330. var obj = rs.rows.item(i);
  331. console.log(obj);
  332. var options = obj.options?JSON.parse(obj.options):[];
  333. obj.optionArr = options;
  334. obj.jsonStr = JSON.stringify(obj);
  335. data.push( obj );
  336. }
  337. question_info = data;
  338. var html = template("question_tmp", {list: data});
  339. $("#question_list").empty().append(html);
  340. last_index = data.length + 1;
  341. if(data.length > 0){
  342. $(".header-link").addClass("active");
  343. }
  344. },
  345. function(tx, rs){
  346. console.error("获取数据失败");
  347. })
  348. }
  349. function createTable(tableName){
  350. sqlite.executeSql("create table if not exists "+ tableName +
  351. " (id INTEGER PRIMARY KEY AUTOINCREMENT, index1 INTEGER, code text, template_id INTEGER, comment TEXT, title TEXT,"+
  352. " type INTEGER, isRequired INTEGER, options TEXT, updateTime TEXT, sort INTEGER, minOptions TEXT, maxOptions TEXT, nextQuestion TEXT )", [],
  353. function(tx, rs){
  354. console.log(tableName+"创建存储空间成功");
  355. },
  356. function(tx, rs){
  357. console.error(tableName);
  358. console.error(rs)
  359. alert("创建缓存空间失败!");
  360. });
  361. }
  362. function getUUID(){//生成全局唯一标识符
  363. var d = new Date().getTime();
  364. var uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  365. var r = (d + Math.random()*16)%16 | 0;
  366. d = Math.floor(d/16);
  367. return (c=='x' ? r : (r&0x3|0x8)).toString(16);
  368. });
  369. return uuid;
  370. }