// packageE/questionnaire/questions/questions.js var app = getApp(); var int = app.globalData.statusBarHeight * 2 + 20; Page({ /** * 页面的初始数据 */ data: { statusBarHeight: int, datainfo: [], moreArr: [], hint: false, allNum: 0, isLoading: false, page: 1, plugin_name:'' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if (options.id) { this.setData({ id: options.id, }); } if (options.goods_id) { this.setData({ goods_id: options.goods_id, }); } if (options.tag) { this.setData({ tag: options.tag, }); } this.getData(); }, back() { wx.navigateBack({ delta: 1, }); }, submit() { // 单选和多选的选择 let datainfo = this.data.datainfo; let info = this.data.info.data; let allNum = this.data.allNum; for (let i = 0; i < info.length; i++) { if(!info[i].answer || !info[i].answer.fraction){ continue; } if (info[i].type == 2) { for (let key in info[i].answer.fraction) { allNum += Number(info[i].answer.fraction[key]); } } if (info[i].type == 1) { let Arr = []; // let len = info[i].answer.fraction.length; for (let key in info[i].answer.fraction) { Arr[key] = Number(info[i].answer.fraction[key]); } let r = Arr.filter(function (element, index, self) { if (element) { return element; } }); allNum += Math.max(...r); } } this.setData({ allNum: allNum, }); for (let i = 0; i < info.length; i++) { if (info[i].id && info[i].required) { if (!datainfo[i]) { this.setData({ hint: true, }); setTimeout(() => { this.setData({ hint: false, }); }, 1500); return; } } } this.pushData(); }, multiple(e) { console.log(e); let datainfo = this.data.datainfo; let ind = e.currentTarget.dataset.ind; let indx = e.currentTarget.dataset.indx; let info = this.data.info.data[ind]; let ids = info.id; let qid = info.questionnaire_id; let multipleArr = []; let arr = []; let farr = []; let installArr = []; let is_answer = info.answer.answer[indx]; let is_fraction = info.answer.fraction[indx]; farr.push(info.answer.fraction[indx]); arr.push(info.answer.answer[indx]); if (datainfo.length == 0) { multipleArr.push({ answer: {}, }, { fraction: {}, }, { id: ids, }, { questionnaire_id: qid, }); multipleArr[0].answer[indx] = info.answer.answer[indx]; multipleArr[1].fraction[indx] = info.answer.fraction[indx]; installArr.push(multipleArr); datainfo[ind] = multipleArr; } else { let ischeack = this.cheackarr(datainfo, ind, ids); if (ischeack) { for (let i = 0; i < datainfo[ind].length; i++) { if (datainfo[ind][i].answer) { if (datainfo[ind][i].answer[indx]) { delete datainfo[ind][i].answer[indx]; } else { datainfo[ind][i].answer[indx] = is_answer; } } if (datainfo[ind][i].fraction) { if (datainfo[ind][i].fraction[indx]) { delete datainfo[ind][i].fraction[indx]; } else { datainfo[ind][i].fraction[indx] = is_fraction; } } } } else { multipleArr.push({ answer: {}, }, { fraction: {}, }, { id: ids, }, { questionnaire_id: qid, }); multipleArr[0].answer[indx] = info.answer.answer[indx]; multipleArr[1].fraction[indx] = info.answer.fraction[indx]; installArr.push(multipleArr); datainfo[ind] = multipleArr; } } this.setData({ datainfo: datainfo, }); // 还差多选后的datainfo格式 }, cheackarr(datainfo, ind, ids) { let is_cheack; if (datainfo[ind]) { for (let i = 0; i < datainfo[ind].length; i++) { // console.log(datainfo[ind][i]) if (datainfo[ind][i].id == ids) { is_cheack = true; // console.log('选的是同一个问题') return is_cheack; } } } else { is_cheack = false; return is_cheack; } }, radio(e) { let ind = e.currentTarget.dataset.ind; let radioArr = []; let arr = this.data.info.data; let ids = e.currentTarget.dataset.id; let questionnaire_ids = arr[ind].questionnaire_id; let datainfo = this.data.datainfo; let indx = e.currentTarget.dataset.indx; let info = this.data.info.data[ind]; radioArr.push({ answer: {}, }, { fraction: {}, }, { id: ids, }, { questionnaire_id: questionnaire_ids, }); radioArr[0].answer[indx] = info.answer.answer[indx]; radioArr[1].fraction[indx] = info.answer.fraction[indx]; datainfo[ind] = radioArr; console.log(datainfo); this.setData({ datainfo: datainfo, }); // console.log(this.data.datainfo) }, // 去掉数组的temp trimSpace(array) { for (var i = 0; i < array.length; i++) { if ( array[i] == " " || array[i] == null || typeof array[i] == "undefined" ) { array.splice(i, 1); i = i - 1; } } return array; }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () {}, /** * 生命周期函数--监听页面显示 */ onShow: function () {}, /** * 生命周期函数--监听页面隐藏 */ onHide: function () {}, /** * 生命周期函数--监听页面卸载 */ onUnload: function () {}, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () {}, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { if (this.data.isLoading) { let pages = this.data.page + 1; this.setData({ page: pages, }); this.getMore(); } else { return; } }, /** * 用户点击右上角分享 */ onShareAppMessage: function () {}, pushData() { let that = this; let urlStr = app.getNetAddresss("plugin.questionnaire.api.index.indexInfo"); app._getNetWork({ url: urlStr, data: { data: this.data.datainfo, answerScore: "answerScore", questionnaire_id: that.data.id ? that.data.id : "", }, success: function (resdata) { var res = resdata.data; console.log(res); if (res.result == 1) { let sterarr = res.data.answerScore; let stringsa = JSON.stringify(sterarr); console.log(stringsa); wx.navigateTo({ url: "/packageE/questionnaire/surveyResult/surveyResult?tag=" + that.data.tag + "&score=" + sterarr.score + "&questionnaire_id=" + sterarr.questionnaire_id + "&rewardPoint=" + sterarr.rewardPoint + "&allNum=" + that.data.allNum + "&goods_id=" + that.data.goods_id + "&plugin_point_name="+sterarr.plugin_point_name, }); wx.setStorageSync("surveyResult", stringsa); // if(app._isTextEmpty(that.data.tag)){ // wx.navigateTo({ // url: '/packageE/questionnaire/surveyResult/surveyResult' // }) // }else{ // // } } else { wx.showToast({ icon: "none", title: res.msg, duration: 1500, }); } }, fail: function (res) { console.log(res); }, }); }, getData() { let that = this; let urlStr = app.getNetAddresss("plugin.questionnaire.api.index.indexInfo"); app._getNetWork({ url: urlStr, data: { answer: "answer", questionnaire_id: that.data.id ? that.data.id : "", goods_id: that.data.goods_id ? that.data.goods_id : "", page: that.data.page, }, success: function (resdata) { var res = resdata.data; console.log(res); if (res.result == 1) { that.setData({ info: res.data.answer.choice, id: res.data.answer.questionnaire_id, title: res.data.answer.title, current_page: res.data.answer.choice.current_page, last_page: res.data.answer.choice.last_page, plugin_name:res.data.plugin_name }); if (that.data.current_page >= that.data.last_page) { that.setData({ isLoading: false, }); } else { that.setData({ isLoading: true, }); } } else { wx.showToast({ icon: "none", title: res.msg, duration: 1500, success: function (e) { setTimeout(() => { wx.navigateBack({ delta: 1, }); }, 1600); }, }); } }, fail: function (res) { console.log(res); }, }); }, getMore() { let that = this; let urlStr = app.getNetAddresss("plugin.questionnaire.api.index.indexInfo"); app._getNetWork({ url: urlStr, data: { answer: "answer", questionnaire_id: that.data.id ? that.data.id : "", goods_id: that.data.goods_id ? that.data.goods_id : "", page: that.data.page, }, success: function (resdata) { var res = resdata.data; console.log(res); if (res.result == 1) { that.setData({ "info.data": that.data.info.data.concat( res.data.answer.choice.data ), id: res.data.answer.questionnaire_id, title: res.data.answer.title, current_page: res.data.answer.choice.current_page, last_page: res.data.answer.choice.last_page, }); if (that.data.current_page >= that.data.last_page) { that.setData({ isLoading: false, }); } else { that.setData({ isLoading: true, }); } } else { wx.showToast({ icon: "none", title: res.msg, duration: 1500, }); } }, fail: function (res) { console.log(res); }, }); }, });