| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- 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:"",
- IsShowPanelHelp:true,
- IsBuilding:false,
- IsShowLightColor:false,
- IsShowKeyword:true,//显示关键词
- IsShowQuestion:false,
- IsShowTranslate:false,
- IsShowAnswer:false,
- IsShowSetting:false,
- CurrentQuestionIndex:0,
- swiperHeight: "526rpx",
- lastTapTime: 0, // 记录上一次点击的时间,用于检测双击
- },
- onLoad: function (options) {
- let that = this;
- let words=app.globalData.SelectedWords.join(" ");
- const hiddenhelp=wx.getStorageSync('HiddenArticleHelp');
- that.setData({
- Containnerheight: main.getWindowHeight(),
- Words:words,
- IsShowPanelHelp:!hiddenhelp,
- });
- if (options.ID)
- that.getArticleByID(options.ID);
- else
- that.init(options);
- that.setTheme();
- },
- getArticleByID:function(id){
- const 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){
- const that=this;
- wx.showLoading({
- title: '生成中',
- });
- 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){
- wx.hideLoading();
- let content=data;
- that.updateData(content);
- that.setData({
- IsBuilding:false,
- });
- }
- });
- if (1==0){
- // let content={
- // "ArticleEnglish": [
- // "Once upon a time, there was a king in a beautiful kingdom.",
- // "A fierce dragon came and brought yellow fire to the kingdom.",
- // "The people in the kingdom were very afraid.",
- // "A brave boy and a kind - hearted girl decided to go and fight against the dragon.",
- // "They came to the dragon's cave and saw the dragon sleeping.",
- // "The boy used a big stick to wake up the dragon.",
- // "The dragon was angry and spat out yellow fire at them.",
- // "But the boy and the girl were not scared.",
- // "They worked together and finally defeated the dragon.",
- // "The king was very happy and thanked the boy and the girl."
- // ],
- // "ArticleChinese": [
- // "从前,在一个美丽的王国里有一位国王。",
- // "一条凶猛的龙来了,给王国带来了黄色的火焰。",
- // "王国里的人们非常害怕。",
- // "一个勇敢的男孩和一个善良的女孩决定去与龙战斗。",
- // "他们来到了龙的洞穴,看到龙正在睡觉。",
- // "男孩用一根大棍子把龙叫醒了。",
- // "龙很生气,朝他们吐出了黄色的火焰。",
- // "但是男孩和女孩并不害怕。",
- // "他们齐心协力,最终打败了龙。",
- // "国王非常高兴,感谢了男孩和女孩。"
- // ],
- // "FormsOfWords": [
- // "boy",
- // "girl",
- // "king",
- // "come",
- // "came",
- // "go",
- // "went",
- // "yellow",
- // "dragon",
- // "dragons",
- // "fire"
- // ],
- // "Question": [
- // {
- // "QuestionEnglish": "Who brought yellow fire to the kingdom?",
- // "QuestionChinese": "谁给王国带来了黄色的火焰?",
- // "OptionsEnglish": [
- // "A. The boy",
- // "B. The girl",
- // "C. The king",
- // //"D. The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon"
- // "D. The dragon"
- // ],
- // "OptionsChinese": [
- // "A. 男孩",
- // "B. 女孩",
- // "C. 国王",
- // "D. 龙"
- // ],
- // "Answer": "D"
- // },
- // {
- // "QuestionEnglish": "What did the boy and the girl decide to do?",
- // "QuestionChinese": "男孩和女孩决定做什么?",
- // "OptionsEnglish": [
- // "A. Go home",
- // "B. Fight against the dragon",
- // "C. Play with the dragon",
- // "D. Sleep in the cave"
- // ],
- // "OptionsChinese": [
- // "A. 回家",
- // "B. 与龙战斗",
- // "C. 和龙一起玩",
- // "D. 在洞穴里睡觉"
- // ],
- // "Answer": "B"
- // },
- // {
- // "QuestionEnglish": "How did the boy wake up the dragon?",
- // "QuestionChinese": "男孩是如何叫醒龙的?",
- // "OptionsEnglish": [
- // "A. By shouting",
- // "B. By using a big stick",
- // "C. By throwing stones",
- // "D. By singing"
- // ],
- // "OptionsChinese": [
- // "A. 通过大喊",
- // "B. 通过使用一根大棍子",
- // "C. 通过扔石头",
- // "D. 通过唱歌"
- // ],
- // "Answer": "B"
- // },
- // {
- // "QuestionEnglish": "How did the dragon feel when it woke up?",
- // "QuestionChinese": "龙醒来时感觉如何?",
- // "OptionsEnglish": [
- // "A. Happy",
- // "B. Sad",
- // "C. Angry",
- // "D. Tired"
- // ],
- // "OptionsChinese": [
- // "A. 高兴的",
- // "B. 悲伤的",
- // "C. 生气的",
- // "D. 疲惫的"
- // ],
- // "Answer": "C"
- // },
- // {
- // "QuestionEnglish": "What did the king do after the boy and the girl defeated the dragon?",
- // "QuestionChinese": "男孩和女孩打败龙后,国王做了什么?",
- // "OptionsEnglish": [
- // "A. He punished them",
- // "B. He thanked them",
- // "C. He ignored them",
- // "D. He was angry with them"
- // ],
- // "OptionsChinese": [
- // "A. 他惩罚了他们",
- // "B. 他感谢了他们",
- // "C. 他无视了他们",
- // "D. 他对他们很生气"
- // ],
- // "Answer": "B"
- // }
- // ]
- // };
- that.updateData(content);
- }
- },
-
- updateData:function(content){
- const 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<content.ArticleEnglish.length;i++){
- for(let j=0;j<content.FormsOfWords.length;j++){
- let word = content.FormsOfWords[j];
- let regex = new RegExp(`\\b${word}\\b[.,!?;:]?`, 'gi');
- if (that.data.IsShowKeyword){
- content.ArticleEnglish[i] = content.ArticleEnglish[i].replace(regex, match => {
- let punctuation = match.match(/[.,!?;:]$/);
- let punc = punctuation ? punctuation[0] : '';
- let wordPart = match.replace(/[.,!?;:]$/, '');
- return `<span class='highlight'>${wordPart}</span>${punc}`;
- });
- }
- }
- }
- content.ArticleEnglishStr=content.ArticleEnglish.join(" ");
- for(let i=0;i<content.Question.length;i++){
- for(let j=0;j<content.Question[i].OptionsEnglish.length;j++){
- let str=content.Question[i].OptionsChinese[j];
- content.Question[i].OptionsChinese[j]=str.substr(2);
- }
- let char = content.Question[i].Answer;
- let asciiCode = char.charCodeAt(0);
- content.Question[i].AnswerNumber=asciiCode-65;
- }
- that.setData({
- Content:content,
- });
- },
- setTheme:function(){
- const that=this;
- const css=Theme[that.data.IsShowLightColor?1:0];
- wx.setNavigationBarColor({
- frontColor: css.frontColor,
- backgroundColor: css.backgroundColor,
- });
- wx.setBackgroundColor({
- backgroundColor: css.backgroundColor,
- backgroundColorTop:css.backgroundColor,
- backgroundColorBottom:css.backgroundColor,
- });
- that.setData({
- ThemeCSS:css.Name,
- });
- },
- closeHelp:function(){
- this.setData({
- IsShowPanelHelp:false,
- });
- wx.setStorageSync('HiddenArticleHelp', true);
- },
- selectedAnswer:function(e){
- const that=this;
- const question=e.currentTarget.dataset.question;
- const index=e.currentTarget.dataset.index;
- that.data.Content.Question[question].UserAnswer=index;
- that.setData({
- Content:that.data.Content,
- });
- },
- showData:function(e){
- const that=this;
- let name=e.currentTarget.dataset.name;
- this.data[name] = !this.data[name];
- this.setData(this.data);
- if (name=="IsShowKeyword"){
- that.setShowKeyword();
- }
- else if (name=="IsShowLightColor"){
- that.setTheme();
- }
- },
- setShowKeyword:function(){
- const that=this;
- let content=that.data.Content;
- let source="highlight",target="nonelight";
- if (that.data["IsShowKeyword"]){
- source="nonelight";
- target="highlight";
- }
- content.ArticleEnglishStr=common.ReplaceAllString(content.ArticleEnglishStr,source,target);
- for(let i=0;i<content.Question.length;i++){
- for(let j=0;j<content.Question[i].OptionsEnglish.length;j++){
- content.Question[i].OptionsEnglish[j]=common.ReplaceAllString(content.Question[i].OptionsEnglish[j],source,target);
- }
- }
- that.setData({
- Content:content,
- });
- },
- nextQuestion:function(e){
- if (this.data.CurrentQuestionIndex+1<this.data.Content.Question.length){
- this.data.CurrentQuestionIndex=this.data.CurrentQuestionIndex+1;
- this.setData({
- IsShowAnswer:false,
- CurrentQuestionIndex:this.data.CurrentQuestionIndex,
- });
- }
- },
- updateQuestionIndex:function(e){
- this.setData({
- CurrentQuestionIndex:e.detail.current,
- });
- },
- onContainerTap: function() {
- const currentTime = new Date().getTime();
- const lastTapTime = this.data.lastTapTime;
- const timeDiff = currentTime - lastTapTime;
-
- // 如果两次点击的时间间隔小于300毫秒,则认为是双击
- if (timeDiff < 300 && timeDiff > 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;
- 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 });
- }
- });
- },
- 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,
- }
- },
- })
|