edit_questions.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. $("#question_list").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. last_index = last_index - 1;
  153. if(last_index == 1){
  154. $(".header-link").removeClass("active");
  155. }
  156. question_info.splice(index-1, 1);
  157. //将本题后的题目的index-1,sort-1.因为会影响新增的代码
  158. var usql = "UPDATE questions2 SET index1=(index1-1), sort = (sort-1) WHERE template_id = "+table_tp_id+" AND index1 > "+index1;
  159. sqlite.executeSql(usql,[],function(tx, rs){
  160. console.log("update success after delete");
  161. console.log(rs);
  162. },function(tx, rs){
  163. console.log("update failed after delete");
  164. console.log(rs);
  165. });
  166. }, function(tx, rs){
  167. console.log(rs);
  168. });
  169. },
  170. cancelValue: "取消",
  171. cancel: function(){}
  172. }).showModal();
  173. });
  174. $("#question_list").on('click', '.question', function(){
  175. var $this = $(this),
  176. jsonData = $this.attr("data-json"),
  177. index1 = parseInt($this.find(".index").text());
  178. jsonData = JSON.parse(jsonData);
  179. if(jsonData.type == 2){
  180. openWebview("blank_filling_question.html", {
  181. tp_id: table_tp_id,
  182. jsonData: jsonData,
  183. q_index: index1,
  184. q_num: last_index-1
  185. });
  186. }else{
  187. openWebview("multiple_choice_question.html", {
  188. tp_id: table_tp_id,
  189. type: (jsonData.type == 0)? "radio" : "checkbox",
  190. jsonData: jsonData,
  191. q_index: index1,
  192. q_num: last_index-1
  193. });
  194. }
  195. });
  196. $("#pre_btn").on('click', function(){
  197. mui.back();
  198. });
  199. $("#next_btn").on('click', function(){
  200. if(question_info.length == 0){
  201. dialog({
  202. content: "最少需要一个问题,请添加问题",
  203. contentType: "tipsbox",
  204. skin: 'bk-popup',
  205. closeTime: 2000
  206. }).showModal();
  207. }else{
  208. openWebview("choose_respondent.html", {
  209. code: code,
  210. pre_info: info,
  211. question_info: question_info
  212. });
  213. }
  214. });
  215. window.addEventListener("addQuestion", function(e){
  216. var info = e.detail.info;
  217. sqlite.executeSql(
  218. 'INSERT INTO questions2 '+
  219. ' (template_id, index1, code, title, isRequired, comment, type, options, sort, updateTime, minOptions, maxOptions) '+
  220. ' VALUES(?,?,?,?,?,?,?,?,?,?,?,?)',
  221. [
  222. parseInt(info.tp_id), info.index, getUUID(), info.title, info.isRequired, info.comment, info.type, info.options,
  223. info.sort, info.updateTime, info.minOptions, info.maxOptions
  224. ],
  225. function(tx, rs){
  226. console.log(rs);
  227. refresh();
  228. }, function(tx, rs){
  229. console.log(rs)
  230. });
  231. });
  232. window.addEventListener("update", function(e){
  233. var info = e.detail.info,
  234. code = e.detail.code;
  235. console.log(info);
  236. //单双引号在sqlite中有区别,\无特殊含义,双引号代表着单引号的作用
  237. var sql = "UPDATE questions2 "+
  238. " SET title = \'"+info.title+"\', isRequired = "+info.isRequired+", type="+info.type+", comment= '"+info.comment+"', "+
  239. " updateTime="+info.updateTime+", sort = "+info.sort+", options = \'"+info.options+"\', minOptions='"+info.minOptions+"', maxOptions='"+info.maxOptions+"'"+
  240. " WHERE template_id = "+parseInt(info.tp_id)+" AND code = \'"+ code+"\'";
  241. console.log(sql);
  242. //如果index1< sort,则需要将之前的sort的数据往前挪
  243. var sql1 = "UPDATE questions2 SET sort="+(info.sort-1)+" WHERE template_id = "+parseInt(info.tp_id)+" AND sort = "+info.sort;
  244. console.log("sql1 = "+sql1);
  245. if(info.index < parseInt(info.sort)){
  246. sqlite.executeSql(sql1,[],
  247. function(tx, rs){
  248. console.log("update1 success");
  249. console.log(rs);
  250. sqlite.executeSql(sql,[],
  251. function(tx, rs){
  252. console.log("update success");
  253. console.log(rs);
  254. refresh();
  255. }, function(tx, rs){
  256. console.log(rs)
  257. });
  258. }, function(tx, rs){
  259. console.log(rs)
  260. });
  261. }else{
  262. //题目往前移的情况,将这个sort后的序号加1
  263. var sql2 = "UPDATE questions2 SET sort=sort+1 WHERE template_id = "+parseInt(info.tp_id)+" AND sort >= "+info.sort;
  264. console.log("sql2 = "+sql2);
  265. sqlite.executeSql(sql2,[],
  266. function(tx, rs){
  267. console.log("update2 success");
  268. console.log(rs);
  269. sqlite.executeSql(sql,[],
  270. function(tx, rs){
  271. console.log("update success");
  272. console.log(rs);
  273. refresh();
  274. }, function(tx, rs){
  275. console.log(rs)
  276. });
  277. }, function(tx, rs){
  278. console.log(rs)
  279. });
  280. }
  281. });
  282. window.addEventListener("multiInsert", function(e){
  283. var list = e.detail.list;
  284. console.log(list);
  285. var time = (new Date()).getTime();
  286. sqlite.db.transaction(function(tx){
  287. var sql = 'INSERT INTO questions2 '+
  288. ' (template_id, index1, code, title, isRequired, comment, type, options, sort, updateTime, minOptions, maxOptions) '+
  289. ' VALUES(?,?,?,?,?,?,?,?,?,?,?,?)';
  290. for(var i=0; i<list.length; i++){
  291. var item = list[i];
  292. tx.executeSql( sql,
  293. [
  294. table_tp_id, last_index+i, getUUID(), item.title, item.isRequired, item.comment||"", item.questionType,
  295. item.options?JSON.stringify(item.options):"",last_index+i, time, item.minOptions||"", item.maxOptions||""
  296. ] ,
  297. function(tx, rs){
  298. console.log(rs);
  299. refresh();
  300. }, function(tx, rs){
  301. console.log(rs);
  302. });
  303. }
  304. });
  305. });
  306. }
  307. template.helper("setType", function(str){
  308. if(str == 0){
  309. return "单选";
  310. }
  311. if(str == 1){
  312. return "多选";
  313. }
  314. if(str == 2){
  315. return "填空";
  316. }
  317. return "";
  318. });
  319. function refresh(){
  320. //获取数据表的数据,然后重新渲染页面内容
  321. sqlite.executeSql("SELECT * FROM questions2 WHERE "+
  322. "template_id = "+table_tp_id+" ORDER BY sort ASC, updateTime DESC",[],
  323. function(tx, rs){
  324. console.log(rs);
  325. var data = [];
  326. console.log(rs.rows);
  327. for(var i=0; i<rs.rows.length; i++){
  328. var obj = rs.rows.item(i);
  329. console.log(obj);
  330. var options = obj.options?JSON.parse(obj.options):[];
  331. obj.optionArr = options;
  332. obj.jsonStr = JSON.stringify(obj);
  333. data.push( obj );
  334. }
  335. question_info = data;
  336. var html = template("question_tmp", {list: data});
  337. $("#question_list").empty().append(html);
  338. last_index = data.length + 1;
  339. if(data.length > 0){
  340. $(".header-link").addClass("active");
  341. }
  342. },
  343. function(tx, rs){
  344. console.error("获取数据失败");
  345. })
  346. }
  347. function createTable(tableName){
  348. sqlite.executeSql("create table if not exists "+ tableName +
  349. " (id INTEGER PRIMARY KEY AUTOINCREMENT, index1 INTEGER, code text, template_id INTEGER, comment TEXT, title TEXT,"+
  350. " type INTEGER, isRequired INTEGER, options TEXT, updateTime TEXT, sort INTEGER, minOptions TEXT, maxOptions TEXT, nextQuestion TEXT )", [],
  351. function(tx, rs){
  352. console.log(tableName+"创建存储空间成功");
  353. },
  354. function(tx, rs){
  355. console.error(tableName);
  356. console.error(rs)
  357. alert("创建缓存空间失败!");
  358. });
  359. }
  360. function getUUID(){//生成全局唯一标识符
  361. var d = new Date().getTime();
  362. var uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  363. var r = (d + Math.random()*16)%16 | 0;
  364. d = Math.floor(d/16);
  365. return (c=='x' ? r : (r&0x3|0x8)).toString(16);
  366. });
  367. return uuid;
  368. }