import common from '../../utils/util'; import main from '../../utils/main'; const Theme=[{ "Name":"DarkColor", "backgroundColor": "#004433", "color":"#C1E1C1", "frontColor": '#ffffff', },{ "Name":"LightColor", "backgroundColor": "#D0ECD3", "color":"#151815", "frontColor": '#000000', } ]; const app = getApp(); Page({ data: { Words:"", IsShowFirstOpen:true, IsBuilding:false, IsBuildError:false, IsShowLightColor:false, IsShowKeyword:true,//显示关键词 IsShowQuestion:false, IsShowTranslate:false, IsShowSetting:false, CurrentQuestionIndex:0, swiperHeight: "526rpx", lastTapTime: 0, // 记录上一次点击的时间,用于检测双击 CountDown:30,//倒计时秒数 }, onLoad: function (options) { let that = this; let words=app.globalData.SelectedWords.join(","); let wordsStr=app.globalData.SelectedWords.join(" "); const hiddenhelp=wx.getStorageSync('HiddenArticleFirstOpen'); let IsShowLightColor=wx.getStorageSync('IsShowLightColor'); if (!IsShowLightColor) IsShowLightColor=false; that.setData({ Containnerheight: main.getWindowHeight(), Words:words, WordsStr:wordsStr, IsShowFirstOpen:!hiddenhelp, IsShowLightColor:IsShowLightColor, IsBuildError:false, }); if (options.ID) that.getArticleByID(options.ID); else that.init(options); that.setTheme(); }, getArticleByID:function(id){ let that=this; main.getData('GetYJBDCArticleList?UserID=' + app.globalData.userInfo.UserID+'&ID='+id, function (data) { if (data) { data=data[0]; that.setData({ Words:data.Words, ID:id, }); let content=data.JSONString; that.updateData(content); } }); }, init:function(options){ let that=this; let interval=0; that.data.CountDown=30; interval = setInterval(function(){ that.setData({ CountDown:--that.data.CountDown, }); if (that.data.CountDown<=0) clearInterval(interval); },1200); that.setData({ IsBuilding:true }); let words=app.globalData.SelectedWords.join(","); main.postData('GenerateArticle?UserID='+app.globalData.userInfo.UserID, { Words:words, Level:options.Level, ArticleStyle:options.ArticleStyle }, function (data) { if (data){ if (data=="-1"){ that.setData({ IsBuildError:true, }); } else{ let content=data; that.updateData(content); that.setData({ IsBuilding:false, }); } clearInterval(interval); } }); }, updateData:function(content){ let that=this; //console.log(content); if (typeof content === 'string') { content = JSON.parse(content); } if (typeof content === 'string') { content = JSON.parse(content); } for(let i=0;i { let punctuation = match.match(/[.,!?;:]$/); let punc = punctuation ? punctuation[0] : ''; let wordPart = match.replace(/[.,!?;:]$/, ''); return `${wordPart}${punc}`; }); } } } content.ArticleEnglishStr=content.ArticleEnglish.join(" "); for(let i=0;i 0) { console.log('双击事件触发'); // 在这里添加双击事件的处理逻辑 // 例如:切换翻译显示状态 this.showData({currentTarget:{dataset:{name:"IsShowTranslate"}}}); } // 更新上一次点击的时间 this.setData({ lastTapTime: currentTime }); }, //生成PDF文件 //访问服务器的GeneratePDF接口,提交this.data.Content数据,获得一个生成好的pdf文件,服务端的代码已经生成好 generatePDF: function(e) { let that = this; that.data.Content.Words=that.data.Words.split(" ").join(","); let url = common.Encrypt("GeneratePDF"); url =app.globalData.serverUrl + url; wx.request({ url: url, method: "POST", data: { Content: that.data.Content, }, responseType: 'arraybuffer', // 确保响应类型为arraybuffer success: function(res) { // 将arraybuffer转为临时文件 const fsm = wx.getFileSystemManager(); const tempFilePath = `${wx.env.USER_DATA_PATH}/temp_${Date.now()}.pdf`; try { fsm.writeFileSync( tempFilePath, res.data, 'binary' ); // 直接使用临时文件路径,不再尝试永久保存 console.log('文件已生成:', tempFilePath); // 打开PDF文件预览 wx.openDocument({ filePath: tempFilePath, fileType: 'pdf', showMenu: true, // 显示右上角菜单,可以分享 success: function() { console.log('打开文档成功'); wx.showToast({ title: 'PDF生成成功', icon: 'success' }); }, fail: function(error) { console.error('打开文档失败:', error); wx.showToast({ title: '打开文件失败', icon: 'none' }); } }); } catch (error) { console.error('写入文件失败:', error); wx.showToast({ title: '写入文件失败', icon: 'none' }); } }, fail: function(err) { console.error('请求GeneratePDF接口失败:', err); wx.showToast({ title: '网络错误,请稍候重试', icon: 'none' }); } }); }, // 处理导航栏返回按钮点击事件 back: function() { if (this.data.IsBuilding) { return; // 如果正在生成文章,不执行返回操作 } if (!this.data.ID){ wx.navigateBack({ delta: 2, }); } else { wx.navigateBack(); } }, onUnload:function(){ if (!this.data.ID){ wx.navigateBack({ delta: 2, }); } }, onShareAppMessage: function () { return { title: app.globalData.ShareTitle, path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID, imageUrl: app.globalData.ShareImage, } }, })