import common from '../../utils/util'; import main from '../../utils/main'; import constant1 from '../../utils/constant'; Page({ data: { wordList: [], selectedWords: [], difficulty: 0, difficulties: constant1.arrHardLevel, articleStyles: constant1.arrArticleStyle, articleStyle: '', generatedArticle: '', articleTranslation: '', generating: false, Content:"", }, onLoad: function(options) { let that=this; that.setData({ Containnerheight: main.getWindowHeight(), IsShowTranslate:false, }); let words=options.words; if (!options.words) words="boy,girl,king,come,go,yellow,dragon,fire"; if (words) { try { const wordList = words.split(","); this.setData({ wordList }); } catch (e) { wx.showToast({ title: '数据加载失败', icon: 'none' }); } } }, onDifficultyChange: function(e) { this.setData({ difficulty: Number(e.detail.value) }); }, onArticleStyleChange: function(e) { this.setData({ articleStyle: e.detail.value }); console.log('选择的文章类型:', e.detail.value); }, generateArticle: async function() { let that=this; if (!this.data.articleStyle) { wx.showToast({ title: '请选择文章类型', icon: 'none' }); return; } this.setData({ generating: true, Content:"" }); try { let url = common.Encrypt("GenerateArticle"); //url="https://www.kylx365.com/apiData/"+url; url="http://localhost:3020/api/GenerateArticle"; let words=that.data.wordList; const cloudRes = await new Promise((resolve, reject) => { wx.request({ url: url, method: "POST", data: { Words:JSON.stringify(words), Level:that.data.difficulty, Style:that.data.articleStyle }, success: resolve, fail: reject, }) }); //console.log(cloudRes); let result=cloudRes.data.result.choices[0].message.content; console.log(result); let content=JSON.parse(result); //console.log(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;isample article for testing the PDF generation functionality.It contains multiple paragraphs and some bold text to test formatting.The PDF should be generated with proper layout and styling.", ArticleEnglish: [ "This is a sample article for testing the PDF generation functionality.", "It contains multiple paragraphs and some bold text to test formatting.", "The PDF should be generated with proper layout and styling." ], Question: [ { QuestionEnglish: "What is the purpose of this article?", OptionsEnglish: [ "A. To test PDF generation", "B. To teach English", "C. To demonstrate formatting", "D. All of the above" ] }, { QuestionEnglish: "What special formatting is used in this article?", OptionsEnglish: [ "A. Italics", "B. Bold text", "C. Underline", "D. Strikethrough" ] } ] }, }) }, //生成PDF文件 //访问服务器的GeneratePDF接口,提交this.data.Content数据,获得一个生成好的pdf文件,服务端的代码已经生成好 generatePDF: function(e) { let that = this; this.setData({ generatingPDF: true, }); let url = common.Encrypt("GeneratePDF"); url = "https://www.kylx365.com/apiData/" + url; //url="http://localhost:3020/api/GeneratePDF"; 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' }); }, complete: function() { that.setData({ generatingPDF: false }); } }); }, });