123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- var code,
- info,
- table_tp_id,
- last_index = 1; //当前的题目数 +1
- var question_info;
- mui.init();
- mui.plusReady(function(){
- var self = plus.webview.currentWebview();
- code = self.code;
- table_tp_id = self.table_tp_id;
- info = self.info;
- sqlite.open('MyData','1.0','My Database', 1024*100);
- createTable("questions2");
- //添加标记记录说模板内容已经被修改了
- plus.storage.setItem("isEdit", "1");
- if(code){
- //判断是否数据库中已经存在了数据
- sqlite.executeSql("select * from questions2 WHERE template_id = "+table_tp_id,[],
- function(tx,rs){
- console.log(rs);
- if(rs.rows.length == 0){
- if(self.question_info){
- console.log(self.question_info);
- fillQuestionData(self.question_info);
- }else{
- getQuestions();
- }
- }else{
- refresh();
- }
- },function(tx,rs){
- console.log(rs);
- });
-
- }else{
- refresh();
- }
- bindEvents();
- });
- function getQuestions(){
- var url = "/doctor/questionnaire/getTemplateDetail",
- params = {code: code};
- sendGet(url, params, null, function(res){
- if(res.status == 200){
- fillQuestionData(res.data.questions);
- }else{
- mui.toast(res.msg);
- }
- }, true);
- }
- function fillQuestionData(data){
- question_info = _.map(data,function(o,index){
- var options = _.map(o.options, function(op){
- op.nextQuestion = "";
- //判断是否存在选项的code,如果不存在则使用旧的code,如果不存在则新增
- var optCode = op.optCode || getUUID();
- op.optCode = optCode;
- return op;
- });
- o.options = options;
- o.optionArr = options;
- var code = o.qstCode || getUUID();
- o.code = code;
- o.index1 = index+1;
- o.nextQuestion = "";
- o.minOptions = o.minNum;
- o.maxOptions = o.maxNum;
- o.jsonStr = JSON.stringify(o);
- return o;
- });
- var html = template("question_tmp", {list: question_info});
- $("#question_list").append(html);
- last_index = question_info.length + 1;
- insertIntoTable();
- $(".header-link").addClass("active");
- }
- function insertIntoTable(){
- //将数据保存到数据表中
- sqlite.db.transaction(function(tx){
- for(var i=0; i<question_info.length; i++){
- var time = (new Date()).getTime();
- tmp = question_info[i];
- tx.executeSql(
- 'INSERT INTO questions2'+
- ' (template_id, index1, code, title, comment, type, isRequired, options, updateTime, sort, minOptions, maxOptions, nextQuestion) '+
- ' VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)',
- [
- table_tp_id, i+1, tmp.code, tmp.title, tmp.comment||"", tmp.type,
- tmp.isRequired, JSON.stringify(tmp.options), time, i+1, tmp.minOptions|| tmp.minNum ||"", tmp.maxOptions|| tmp.maxNum || "", ""
- ],
- function(tx, rs){
- console.log(rs)
- }, function(tx, rs){
- console.log(rs)
- });
- }
- }, function(tx, rs){
- console.log(rs)
- });
- }
- function bindEvents(){
- $("#add_question").on("click", function(){
- mui('#sheet1').popover('toggle');
- });
-
- $(".header-link").on("click", function(){
- var active = $(this).hasClass("active");
- if(active){
- mui.openWindow({
- url:"preview.html",
- id:"preview1",
- extras: {
- table_tp_id: table_tp_id,
- code: code,
- info: info,
- question_info: question_info
- }
- });
- }
- });
-
- $("#sheet1").on("tap", "li", function(){
- var val = $(this).attr("data-val");
- if(val == "questions"){
- openWebview("questions_list.html", {tp_id: table_tp_id, q_num: last_index});
- }else if(val == "blankfill"){
- openWebview("blank_filling_question.html", {tp_id: table_tp_id, q_num: last_index});
- }else if(val == "radio" || val == "checkbox"){
- openWebview("multiple_choice_question.html", {tp_id: table_tp_id, type: val, q_num: last_index});
- }
- mui('#sheet1').popover('toggle');
- });
-
- $("#question_list").on("click", '.del-icon', function(e){
- e.stopPropagation();
- var $this = $(this);
- dialog({
- content: "确定要删除这个问题吗?",
- okValue: "确定",
- ok: function(){
- var $parent = $this.closest(".question"),
- index = parseInt($parent.find(".index").text()), //页面中的排序
- code = $parent.attr("data-code"), //数据库中的排序
- $index_list = $(".index"),
- index1 = $parent.attr("data-index"),
- len = $index_list.length;
- for(i=index; i<len; i++){
- var val = $($index_list[i]).text();
- $($index_list[i]).text(parseInt(val)-1);
- }
- $parent.remove();
- var sql = "delete from questions2 where template_id = "+table_tp_id+" and code = '"+code+"'";
- console.log(sql);
- sqlite.executeSql(sql,[],function(tx,rs){
- console.log("数据删除成功");
- last_index = last_index - 1;
- if(last_index == 1){
- $(".header-link").removeClass("active");
- }
- question_info.splice(index-1, 1);
- //将本题后的题目的index-1,sort-1.因为会影响新增的代码
- var usql = "UPDATE questions2 SET index1=(index1-1), sort = (sort-1) WHERE template_id = "+table_tp_id+" AND index1 > "+index1;
- sqlite.executeSql(usql,[],function(tx, rs){
- console.log("update success after delete");
- console.log(rs);
- },function(tx, rs){
- console.log("update failed after delete");
- console.log(rs);
- });
- }, function(tx, rs){
- console.log(rs);
- });
- },
- cancelValue: "取消",
- cancel: function(){}
- }).showModal();
- });
-
- $("#question_list").on('click', '.question', function(){
- var $this = $(this),
- jsonData = $this.attr("data-json"),
- index1 = parseInt($this.find(".index").text());
- jsonData = JSON.parse(jsonData);
- if(jsonData.type == 2){
- openWebview("blank_filling_question.html", {
- tp_id: table_tp_id,
- jsonData: jsonData,
- q_index: index1,
- q_num: last_index-1
- });
- }else{
- openWebview("multiple_choice_question.html", {
- tp_id: table_tp_id,
- type: (jsonData.type == 0)? "radio" : "checkbox",
- jsonData: jsonData,
- q_index: index1,
- q_num: last_index-1
- });
- }
- });
-
- $("#pre_btn").on('click', function(){
- mui.back();
- });
-
- $("#next_btn").on('click', function(){
- if(question_info.length == 0){
- dialog({
- content: "最少需要一个问题,请添加问题",
- contentType: "tipsbox",
- skin: 'bk-popup',
- closeTime: 2000
- }).showModal();
- }else{
- openWebview("choose_respondent.html", {
- code: code,
- pre_info: info,
- question_info: question_info
- });
- }
- });
-
- window.addEventListener("addQuestion", function(e){
- var info = e.detail.info;
- sqlite.executeSql(
- 'INSERT INTO questions2 '+
- ' (template_id, index1, code, title, isRequired, comment, type, options, sort, updateTime, minOptions, maxOptions) '+
- ' VALUES(?,?,?,?,?,?,?,?,?,?,?,?)',
- [
- parseInt(info.tp_id), info.index, getUUID(), info.title, info.isRequired, info.comment, info.type, info.options,
- info.sort, info.updateTime, info.minOptions, info.maxOptions
- ],
- function(tx, rs){
- console.log(rs);
- refresh();
- }, function(tx, rs){
- console.log(rs)
- });
- });
- window.addEventListener("update", function(e){
- var info = e.detail.info,
- code = e.detail.code;
- console.log(info);
- //单双引号在sqlite中有区别,\无特殊含义,双引号代表着单引号的作用
- var sql = "UPDATE questions2 "+
- " SET title = \'"+info.title+"\', isRequired = "+info.isRequired+", type="+info.type+", comment= '"+info.comment+"', "+
- " updateTime="+info.updateTime+", sort = "+info.sort+", options = \'"+info.options+"\', minOptions='"+info.minOptions+"', maxOptions='"+info.maxOptions+"'"+
- " WHERE template_id = "+parseInt(info.tp_id)+" AND code = \'"+ code+"\'";
- console.log(sql);
- //如果index1< sort,则需要将之前的sort的数据往前挪
- var sql1 = "UPDATE questions2 SET sort="+(info.sort-1)+" WHERE template_id = "+parseInt(info.tp_id)+" AND sort = "+info.sort;
- console.log("sql1 = "+sql1);
- if(info.index < parseInt(info.sort)){
- sqlite.executeSql(sql1,[],
- function(tx, rs){
- console.log("update1 success");
- console.log(rs);
- sqlite.executeSql(sql,[],
- function(tx, rs){
- console.log("update success");
- console.log(rs);
- refresh();
- }, function(tx, rs){
- console.log(rs)
- });
- }, function(tx, rs){
- console.log(rs)
- });
- }else{
- //题目往前移的情况,将这个sort后的序号加1
- var sql2 = "UPDATE questions2 SET sort=sort+1 WHERE template_id = "+parseInt(info.tp_id)+" AND sort >= "+info.sort;
- console.log("sql2 = "+sql2);
- sqlite.executeSql(sql2,[],
- function(tx, rs){
- console.log("update2 success");
- console.log(rs);
- sqlite.executeSql(sql,[],
- function(tx, rs){
- console.log("update success");
- console.log(rs);
- refresh();
- }, function(tx, rs){
- console.log(rs)
- });
- }, function(tx, rs){
- console.log(rs)
- });
- }
- });
-
- window.addEventListener("multiInsert", function(e){
- var list = e.detail.list;
- console.log(list);
- var time = (new Date()).getTime();
- sqlite.db.transaction(function(tx){
- var sql = 'INSERT INTO questions2 '+
- ' (template_id, index1, code, title, isRequired, comment, type, options, sort, updateTime, minOptions, maxOptions) '+
- ' VALUES(?,?,?,?,?,?,?,?,?,?,?,?)';
- for(var i=0; i<list.length; i++){
- var item = list[i];
- tx.executeSql( sql,
- [
- table_tp_id, last_index+i, getUUID(), item.title, item.isRequired, item.comment||"", item.questionType,
- item.options?JSON.stringify(item.options):"",last_index+i, time, item.minOptions||"", item.maxOptions||""
- ] ,
- function(tx, rs){
- console.log(rs);
- refresh();
- }, function(tx, rs){
- console.log(rs);
- });
- }
- });
- });
- }
- template.helper("setType", function(str){
- if(str == 0){
- return "单选";
- }
- if(str == 1){
- return "多选";
- }
- if(str == 2){
- return "填空";
- }
-
- return "";
- });
- function refresh(){
- //获取数据表的数据,然后重新渲染页面内容
- sqlite.executeSql("SELECT * FROM questions2 WHERE "+
- "template_id = "+table_tp_id+" ORDER BY sort ASC, updateTime DESC",[],
- function(tx, rs){
- console.log(rs);
- var data = [];
- console.log(rs.rows);
- for(var i=0; i<rs.rows.length; i++){
- var obj = rs.rows.item(i);
- console.log(obj);
- var options = obj.options?JSON.parse(obj.options):[];
- obj.optionArr = options;
- obj.jsonStr = JSON.stringify(obj);
- data.push( obj );
- }
- question_info = data;
- var html = template("question_tmp", {list: data});
- $("#question_list").empty().append(html);
- last_index = data.length + 1;
- if(data.length > 0){
- $(".header-link").addClass("active");
- }
- },
- function(tx, rs){
- console.error("获取数据失败");
- })
- }
- function createTable(tableName){
- sqlite.executeSql("create table if not exists "+ tableName +
- " (id INTEGER PRIMARY KEY AUTOINCREMENT, index1 INTEGER, code text, template_id INTEGER, comment TEXT, title TEXT,"+
- " type INTEGER, isRequired INTEGER, options TEXT, updateTime TEXT, sort INTEGER, minOptions TEXT, maxOptions TEXT, nextQuestion TEXT )", [],
- function(tx, rs){
- console.log(tableName+"创建存储空间成功");
- },
- function(tx, rs){
- console.error(tableName);
- console.error(rs)
- alert("创建缓存空间失败!");
- });
- }
- function getUUID(){//生成全局唯一标识符
- var d = new Date().getTime();
- var uuid = 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
- var r = (d + Math.random()*16)%16 | 0;
- d = Math.floor(d/16);
- return (c=='x' ? r : (r&0x3|0x8)).toString(16);
- });
- return uuid;
- }
|