| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- import common from '../../utils/util';
- import main from '../../utils/main';
- import animation from '../../utils/animation';
- 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,
- IsShowRemind:false,
- remindAnimation: "", // 翻译弹窗动画类
- 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 IsShowKeyword=wx.getStorageSync('IsShowKeyword');
- if (IsShowKeyword===""){
- IsShowKeyword=true;
- wx.setStorageSync('IsShowKeyword', true);
- }
- let IsShowLightColor=wx.getStorageSync('IsShowLightColor');
- if (!IsShowLightColor){
- IsShowLightColor=false;
- }
-
- that.setData({
- Containnerheight: main.getWindowHeight(),
- Words:words,
- WordsStr:wordsStr,
- Level:options.Level,
- ArticleStyle:options.ArticleStyle,
- AIVersion:options.AIVersion,
- IsShowFirstOpen:!hiddenhelp,
- IsShowLightColor:IsShowLightColor,
- IsShowKeyword:IsShowKeyword,
- IsBuildError:false,
- IsBuilding:false,
- });
- if (options.ID)
- that.getArticleByID(options.ID);
- else
- that.init(options);
- that.setTheme();
- // //测试
- // that.setData({
- // IsBuilding:true,
- // });
- // that.getChoicenessArticle();
- main.checkGenerating();
- },
- 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,
- Level:data.Level,
- ArticleStyle:data.ArticleStyle,
- CreateTime:data.CreateTime,
- ID:id,
- });
- let content=data.JSONString;
- that.updateData(content);
- }
- });
- },
- init:function(options){
- let that=this;
- let interval=0;
- that.data.CountDown=30;
- //1.5文章生成时间长
- if (that.data.AIVersion=="1.5"){
- that.setData({
- CountDown:60,
- });
- }
- interval = setInterval(function(){
- that.setData({
- CountDown:--that.data.CountDown,
- });
- if (that.data.CountDown<=0)
- clearInterval(interval);
- },1200);
- that.setData({
- IsBuilding:true
- });
- app.globalData.Generating=true;
- let words=app.globalData.SelectedWords.join(",");
- main.postData('GenerateArticle?UserID='+app.globalData.userInfo.UserID, {
- Words:words,
- Level:that.data.Level,
- ArticleStyle:that.data.ArticleStyle,
- AIVersion:that.data.AIVersion
- }, function (data) {
- app.globalData.Generating=false;
- if (data){
- if (data=="-1"){
- that.setData({
- IsBuildError:true,
- CreateTime:common.formatDateCHS(common.formatTime(new Date()),true),
- });
- }
- else{
- let content=data;
- that.updateData(content);
- that.setData({
- IsBuilding:false,
- });
- }
-
- clearInterval(interval);
- }
- });
- that.getChoicenessArticle();
- },
- //得到精选文章
- getChoicenessArticle:function(){
- let that=this;
- main.getData('GetYJBDCArticleList?IsChoiceness=1', function (data) {
- if (data) {
- that.setData({
- ChoicenessList:data,
- });
- }
- });
- },
-
- 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);
- }
- let hl = that.data.IsShowKeyword ? "highlight" : "nonelight";
-
- // 初始化ArticleEnglishArr数组
- content.ArticleEnglishArr = [];
-
- for(let i=0; i<content.ArticleEnglish.length; i++){
- // 确保每个句子末尾有空格,避免和下一句紧挨着
- const sentence = content.ArticleEnglish[i] + " ";
- // 按长度从长到短排序单词,确保先匹配较长的词组
- const sortedWords = [...content.FormsOfWords].sort((a, b) => b.length - a.length);
-
- // 创建一个句子的分段数组
- let segments = [{
- Sentence: sentence,
- CSS: ""
- }];
-
- // 对每个单词进行处理
- for(let j=0; j<sortedWords.length; j++){
- const word = sortedWords[j];
- // 创建一个新的分段数组,用于存储处理后的结果
- let newSegments = [];
-
- // 处理每个现有分段
- for(let k=0; k<segments.length; k++){
- const segment = segments[k];
-
- // 如果当前分段已经被标记为高亮,则不再处理
- if(segment.CSS !== ""){
- newSegments.push(segment);
- continue;
- }
-
- const text = segment.Sentence;
- // 使用正则表达式查找单词边界
- const regex = new RegExp(`\\b${word}\\b`, 'gi');
- let lastIndex = 0;
- let match;
-
- // 查找所有匹配项
- while((match = regex.exec(text)) !== null){
- // 添加匹配前的文本
- if(match.index > lastIndex){
- newSegments.push({
- Sentence: text.substring(lastIndex, match.index),
- CSS: ""
- });
- }
-
- // 添加匹配的单词(高亮)
- newSegments.push({
- Sentence: match[0],
- CSS: hl
- });
-
- lastIndex = match.index + match[0].length;
- }
-
- // 添加最后一个匹配后的文本
- if(lastIndex < text.length){
- newSegments.push({
- Sentence: text.substring(lastIndex),
- CSS: ""
- });
- }
- }
-
- // 更新分段数组
- segments = newSegments;
- }
-
- // 将分段数组添加到ArticleEnglishArr
- content.ArticleEnglishArr.push(segments);
- }
- 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;
- content.Question[i].IsShowAnswer=false;
- }
- that.setData({
- Content:content,
- });
- },
- setTheme:function(){
- let 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,
- });
- wx.setStorageSync('IsShowLightColor', that.data.IsShowLightColor);
- },
- closeHelp:function(){
- this.setData({
- IsShowFirstOpen:false,
- });
- wx.setStorageSync('HiddenArticleFirstOpen', true);
- },
- selectedAnswer:function(e){
- let that=this;
- const question=e.currentTarget.dataset.question;
- const index=e.currentTarget.dataset.index;
- if (that.data.Content.Question[question].UserAnswer==index)
- that.data.Content.Question[question].UserAnswer=-1;
- else
- that.data.Content.Question[question].UserAnswer=index;
- that.setData({
- Content:that.data.Content,
- });
- },
- showData:function(e){
- let 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();
- }
- else if (name=="IsShowAnswer"){
- let index=e.currentTarget.dataset.index;
- let content=that.data.Content;
- content.Question[index].IsShowAnswer=!content.Question[index].IsShowAnswer;
- that.setData({
- Content:content,
- });
- }
- },
- setShowKeyword:function(){
- let that=this;
- let content=that.data.Content;
- let source="highlight",target="nonelight";
- if (that.data["IsShowKeyword"]){
- source="nonelight";
- target="highlight";
- }
- for(let i=0;i<content.ArticleEnglishArr.length;i++){
- let obj=content.ArticleEnglishArr[i];
- let arr=[];
- for(let j=0;j<obj.length;j++){
- if (obj[j].CSS==source)
- obj[j].CSS=target;
- }
- }
- for(let i=0;i<content.ArticleEnglish.length;i++){
- content.ArticleEnglish[i]=common.ReplaceAllString(content.ArticleEnglish[i],source,target);
- }
- that.setData({
- Content:content,
- });
- wx.setStorageSync('IsShowKeyword', that.data.IsShowKeyword);
- },
- nextQuestion:function(e){
- if (this.data.CurrentQuestionIndex+1<this.data.Content.Question.length){
- this.data.CurrentQuestionIndex=this.data.CurrentQuestionIndex+1;
- this.setData({
- CurrentQuestionIndex:this.data.CurrentQuestionIndex,
- });
- }
- },
- updateQuestionIndex:function(e){
- this.setData({
- CurrentQuestionIndex:e.detail.current,
- });
- console.log(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;
- //debugger;
- that.data.Content.Words=that.data.Words;
- that.data.Content.Level=that.data.Level;
- that.data.Content.ArticleStyle=that.data.ArticleStyle;
- that.data.Content.CreateTime=that.data.CreateTime;
- 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();
- let time=common.formatTime(new Date(),"-");
- time=common.ReplaceAllString(time,"-","");
- time=common.ReplaceAllString(time,":","");
- time=common.ReplaceAllString(time," ","_");
- time=time.substring(2,time.length-2);
- const tempFilePath = `${wx.env.USER_DATA_PATH}/语境背单词_${time}.pdf`;
-
- try {
- fsm.writeFileSync(
- tempFilePath,
- res.data,
- 'binary'
- );
- // 直接使用临时文件路径,不再尝试永久保存
- console.log('文件已生成:', tempFilePath);
-
- // 打开PDF文件预览
- wx.openDocument({
- filePath: tempFilePath,
- fileType: 'pdf',
- showMenu: true, // 显示右上角菜单,可以分享
- success: function() {
- },
- 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'
- });
- }
- });
- },
- // 处理导航栏返回按钮点击事件
- onUnload: function() {
- if (!this.data.ID){
- app.globalData.SelectedWords=[];
- wx.navigateBack({
- delta: 1,
- });
- }
- },
- onLongPress: function(e) {
- let that = this;
- let strType=e.currentTarget.dataset.strtype;
- let selectedIndex=e.currentTarget.dataset.index;
- let selectedIndex2=e.currentTarget.dataset.index2;
- let engSentence,chnSentence;
- if (strType=="article"){
- //console.log("选中的句子索引:", selectedIndex);
- //console.log("选中的句子:", that.data.Content.ArticleEnglish[selectedIndex]);
- engSentence=that.data.Content.ArticleEnglish[selectedIndex];
- chnSentence=that.data.Content.ArticleChinese[selectedIndex];
- }
- else if (strType=="question"){
- engSentence=that.data.Content.Question[selectedIndex].QuestionEnglish;
- chnSentence=that.data.Content.Question[selectedIndex].QuestionChinese;
- }
- else if (strType=="option"){
- engSentence=that.data.Content.Question[selectedIndex].OptionsEnglish[selectedIndex2];
- chnSentence=that.data.Content.Question[selectedIndex].OptionsChinese[selectedIndex2];
- }
- this.setData({
- EnglishSentence:engSentence,
- ChineseSentence:chnSentence,
- });
- // 使用动画工具函数显示弹窗,并指定显示动画
- animation.toggleRemindWithAnimation(this, {
- showAnimation: 'remind-slide-up'
- });
- },
- showRemind:function(e){
- animation.toggleRemindWithAnimation(this, {
- showAnimation: 'remind-slide-down'
- });
- },
- goto: function (e) {
- let that=this;
- var url=e.currentTarget.dataset.url;
- wx.navigateTo({
- url: url,
- });
- },
- onShareAppMessage: function () {
- return {
- title: app.globalData.ShareTitle,
- path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
- imageUrl: app.globalData.ShareImage,
- }
- },
- })
|