| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523 |
- import common from '../../utils/util';
- import main from '../../utils/main';
- import animation from '../../utils/animation';
- import commonBehavior from '../../pages/behaviors/commonBehavior';
- const Theme = [{
- "Name": "DarkColor",
- "backgroundColor": "#004433",
- "color": "#C1E1C1",
- "frontColor": '#ffffff',
- }, {
- "Name": "LightColor",
- "backgroundColor": "#D0ECD3",
- "color": "#151815",
- "frontColor": '#000000',
- }];
- const app = getApp();
- var timeout1 = []; //用于判断是否是双击
- var canSaveSchedule=1;//存进度
- Page({
- behaviors: [commonBehavior],
- data: {
- Words: "",
- IsShowLightColor: true,
- IsShowTranslate: false,
- IsShowWordTranslate: false,
- IsShowWordDetail: false, //显示单词细节
- TranslateHeight: 100, //翻译单词框的高度
- lastTapTime: 0, // 记录上一次点击的时间,用于检测双击
- Percentage:0,
- },
- onLoad: function (options) {
- let that = this;
- let IsShowLightColor = wx.getStorageSync('IsShowLightColor');
- if (!IsShowLightColor) {
- IsShowLightColor = false;
- }
- that.setData({
- Containnerheight: main.getWindowHeight(),
- IsShowLightColor: IsShowLightColor,
- Title:options.Title,
- Chapter:options.Chapter,
- });
- wx.setNavigationBarTitle({
- title: options.Chapter,
- });
- that.init(options);
- that.setTheme();
- setTimeout(function(){
- wx.createSelectorQuery().select(".panel1").boundingClientRect(function (rect) {
- //console.log("rect.height:"+rect.height);
- that.setData({
- ArticleHeight: rect.height,
- });
- }).exec();
- },1000);
-
- this.audioCtx = wx.createAudioContext('myAudio');
- },
- init: function (options) {
- let that = this;
- let chapter = options.Chapter.replace("Chapter ", "");
- main.getData('GetReaderBooksChapterContent?UserID=' + app.globalData.userInfo.UserID + '&Title=' + options.Title + '&Chapter=' + chapter, function (data) {
- if (data) {
- data = that.updateData(data);
- let arr=[];
- for(let i=0;i<data.length;i++){
- let section={};
- section.List=[];
- for(let j=0;j<data[i].length;j++){
- let sentence={};
- sentence.CSS="";
- sentence.List=[];
- if (data[i][j]){
- for(let k=0;k<data[i][j].length;k++){
- let word={};
- word.CSS="";
- word.Word=data[i][j][k];
- sentence.List.push(word);
- }
- }
- section.List.push(sentence);
- }
- arr.push(section);
- }
- that.setData({
- List: arr,
- });
- setTimeout(function(){
- that.enterSchedule();
- },1000);
- }
- });
- },
- enterSchedule:function(){
- let that=this;
- let schedule=wx.getStorageSync('Schedule_'+this.data.Title);
- if (schedule){
- if (!schedule.ScrollTop){
- schedule.ScrollTop=0;
- }
-
- // let num=Math.ceil(100*(schedule.ScrollTop)/that.data.ArticleHeight);
- // if (num>100)
- // num=100;
- // // 更新scrollTop数据
- // that.setData({
- // Percentage: num,
- // ScrollTop:schedule.ScrollTop,
- // });
- wx.pageScrollTo({
- scrollTop: schedule.ScrollTop,
- duration: 300,
- });
- that.onPageScroll({scrollTop:schedule.ScrollTop});
- }
- },
- updateData: function (content) {
- let that = this;
- let arr1 = [],
- arr2 = [];
- for (let j = 0; j < content.length; j++) {
- arr2 = [];
- for (let i = 0; i < content[j].length; i++) {
- // 确保每个句子末尾有空格,避免和下一句紧挨着
- let sentence = content[j][i];
- if (sentence != "\"") {
- // 替换所有单引号为双引号,包括对话中的单引号和句尾的单引号
- sentence = sentence.replace(/(^|[,.;:!?\s])'([^']+)'([,.;:!?]|\s|$)/g, '$1"$2"$3');
- // 处理常见的带点缩写,将其中的点替换为特殊标记,以防止被分割
- // 1. 称谓缩写
- sentence = sentence.replace(/\b(Dr|Mr|Mrs|Ms|Prof)\./g, '$1@DOT@');
- // 2. 时间缩写
- sentence = sentence.replace(/\b([ap])\.m\./gi, '$1@DOT@m@DOT@');
- // 3. 学位缩写
- sentence = sentence.replace(/\b(B|M|Ph)\.([A-Z])\./gi, '$1@DOT@$2@DOT@');
- // 4. 公司名称缩写
- sentence = sentence.replace(/\b(Inc|Ltd|Co|Corp)\./g, '$1@DOT@');
- // 5. 拉丁缩写
- sentence = sentence.replace(/\b(e\.g|i\.e|etc|et al|vs)\./g, '$1@DOT@');
- // 6. 军衔缩写
- sentence = sentence.replace(/\b(Lt|Col|Gen|Sgt|Capt)\./g, '$1@DOT@');
- // 7. 地理缩写
- sentence = sentence.replace(/\b(St|Ave|Rd|Blvd)\./g, '$1@DOT@');
- // 8. 其他常见缩写
- sentence = sentence.replace(/\b(U\.S|U\.K|U\.N)\./g, '$1@DOT@');
- // 先处理破折号,在破折号前后添加空格,使其成为独立元素
- sentence = sentence.replace(/([^\s])([—\-–—])([^\s])/g, '$1 $2 $3');
- // 将句子按空格分割成单词数组
- let words = sentence.trim().split(/\s+/);
- // 将单词数组添加到ArticleEnglishArr
- arr2.push(words);
- } else
- arr2.push(sentence);
- }
- arr1.push(arr2);
- }
- content=arr1;
- // 在设置数据前,确保所有@DOT@都被替换回.
- that.replaceAllDOTMarkers(content);
- return content;
- },
- // 替换Content对象中所有的@DOT@标记为.
- replaceAllDOTMarkers: function (obj) {
- if (!obj) return;
- // 如果是字符串,直接替换
- if (typeof obj === 'string') {
- return obj.replace(/@DOT@/g, '.');
- }
- // 如果是数组,递归处理每个元素
- if (Array.isArray(obj)) {
- for (let i = 0; i < obj.length; i++) {
- if (typeof obj[i] === 'string') {
- obj[i] = obj[i].replace(/@DOT@/g, '.');
- } else if (typeof obj[i] === 'object' && obj[i] !== null) {
- this.replaceAllDOTMarkers(obj[i]);
- }
- }
- return;
- }
- // 如果是对象,递归处理每个属性
- if (typeof obj === 'object') {
- for (let key in obj) {
- if (obj.hasOwnProperty(key)) {
- if (typeof obj[key] === 'string') {
- obj[key] = obj[key].replace(/@DOT@/g, '.');
- } else if (typeof obj[key] === 'object' && obj[key] !== null) {
- this.replaceAllDOTMarkers(obj[key]);
- }
- }
- }
- }
- },
- 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);
- },
- getSentenceTraslate: function (e) {
- let that = this;
- let index = e.currentTarget.dataset.index;
- let index2 = e.currentTarget.dataset.index2;
- that.setSelectedObject(e,true);
- let engSentence="", chnSentence;
- for(let i=0;i<that.data.List[index].List[index2].List.length;i++)
- engSentence+=that.data.List[index].List[index2].List[i].Word+" ";
- let param = {};
- param.Sentence = engSentence;
- main.postData('GetSentenceTranslate?UserID=' + app.globalData.userInfo.UserID, param, function (data) {
- if (data) {
- chnSentence = data;
- that.setData({
- EnglishSentence: engSentence,
- ChineseSentence: chnSentence,
- IsShowWordTranslate: true,
- TranslateHeight: 240,
- });
- }
- });
- },
- selectWord: function (e) {
- let that = this;
- //以下为双击判断
- const currentTime = new Date().getTime();
- const lastTapTime = this.data.lastTapTime;
- const timeDiff = currentTime - lastTapTime;
- const param = e;
- //以下为单击
- const timeout11 = setTimeout(function () {
- that.onClickHandler(param);
- that.setSelectedObject(param);
- }, 300);
- timeout1.push(timeout11);
- //console.log("timeout1:"+timeout1);
- // 如果两次点击的时间间隔小于300毫秒,则认为是双击
- if (timeDiff < 300 && timeDiff > 0) {
- console.log('双击事件触发');
- for (let i = 0; i < timeout1.length; i++)
- clearTimeout(timeout1[i]);
- // 在这里添加双击事件的处理逻辑
- that.getSentenceTraslate(param);
- }
- // 更新上一次点击的时间
- this.setData({
- lastTapTime: currentTime
- });
- },
- onClickHandler: function (e) {
- let that = this;
- let css = e.currentTarget.dataset.css;
- let word = e.currentTarget.dataset.word;
- let originalWord = e.currentTarget.dataset.originalWord || word;
- originalWord = originalWord.replace(/[^\w'-]/g, '');
- console.log("点击的单词:", originalWord);
- that.playAudio(originalWord);
- let b = false;
- if (css) {
- let list = that.data.Content.FormsOfWords;
- for (let i = 0; i < list.length; i++) {
- if (list[i].toLowerCase() == originalWord.toLowerCase() && that.data.Content.FormsOfWordsChinese && that.data.Content.FormsOfWordsChinese[i]) {
- let wordChinese = that.data.Content.FormsOfWordsChinese[i];
- // 直接更新翻译内容,不改变IsShowWordTranslate状态(如果已经是true)
- let updateData = {
- EnglishSentence: originalWord,
- ChineseSentence: wordChinese,
- };
- //debugger;
- // 只有当当前不显示翻译时,才设置IsShowWordTranslate为true
- if (!that.data.IsShowWordTranslate) {
- updateData.IsShowWordTranslate = true;
- }
- updateData.TranslateHeight = 100;
- that.setData(updateData);
- b = true;
- break;
- }
- }
- }
- if (!b) {
- that.getWordChinese(originalWord, function (wordTarget) {
- // 准备更新数据
- let updateData = {};
- updateData.TranslateHeight = 100;
- if (wordTarget) {
- updateData.EnglishSentence = wordTarget.Word;
- updateData.ChineseSentence = wordTarget.Translate;
- } else {
- updateData.EnglishSentence = originalWord;
- updateData.ChineseSentence = "";
- }
- // 只有当当前不显示翻译时,才设置IsShowWordTranslate为true
- if (!that.data.IsShowWordTranslate) {
- updateData.IsShowWordTranslate = true;
- }
- that.setData(updateData);
- });
- }
- that.setData({
- IsCollect: false,
- });
- app.globalData.UserCollect.filter(function (item) {
- if (item.Word == originalWord) {
- that.setData({
- IsCollect: true,
- });
- }
- });
- },
- onLongPressApi: function (e) {
- let that = this;
- that.setSelectedObject(e);
- let originalWord = e.currentTarget.dataset.originalWord || word;
- originalWord = originalWord.replace(/[^\w'-]/g, '');
- that.playAudio(originalWord);
- that.getWordDetail(originalWord, function (result) {
- if (result) {
- if (result.ENG.Book) {
- for (let i = 0; i < result.ENG.Book.length; i++) {
- result.ENG.Book[i] = result.ENG.Book[i].substring(result.ENG.Book[i].indexOf(" "));
- result.ENG.Book[i] = result.ENG.Book[i].substring(1, result.ENG.Book[i].indexOf("单元词汇") - 2);
- result.ENG.Book[i] = result.ENG.Book[i].replace("英语 ", "英语 人教版PEP ")
- }
- }
- if (result.ENG.ExamplesSentences) {
- for (let j = 0; j < 3; j++) {
- for (let i = 0; i < result.ENG.ExamplesSentences.length; i++) {
- result.ENG.ExamplesSentences[i][0] = result.ENG.ExamplesSentences[i][0].replace("[线]", "");
- result.ENG.ExamplesSentences[i][0] = result.ENG.ExamplesSentences[i][0].replace("[/线]", "");
- }
- }
- }
- that.setData({
- IsShowWordDetail: true,
- IsShowWordTranslate: false,
- WordJSON: result.ENG,
- });
- that.setData({
- IsCollect: false,
- });
- app.globalData.UserCollect.filter(function (item) {
- if (item.Word == originalWord) {
- that.setData({
- IsCollect: true,
- });
- }
- });
- }
- //console.log(result);
- });
- },
- setSelectedObject:function(e,isSentence){
- let that=this;
- let index=e.currentTarget.dataset.index;
- let index2=e.currentTarget.dataset.index2;
- let index3=e.currentTarget.dataset.index3;
- let list=that.data.List;
- for(let i=0;i<list.length;i++){
- for(let j=0;j<list[i].List.length;j++){
- list[i].List[j].CSS="";
- if (i==index && j==index2 && isSentence)
- list[i].List[j].CSS="sentence-def-highlight";
- for(let k=0;k<list[i].List[j].List.length;k++){
- if (i==index && j==index2 && k==index3 && !isSentence)
- list[i].List[j].List[k].CSS="highlight";
- else
- list[i].List[j].List[k].CSS="";
- }
- }
- }
- that.setData({
- List:list,
- });
- },
- getWordChinese: function (word, callback) {
- let that = this;
- main.getData('GetWordChinese?UserID=' + app.globalData.userInfo.UserID + '&Word=' + word, function (data) {
- if (data) {
- callback(data);
- } else
- callback();
- });
- },
- getWordDetail: function (word, callback) {
- let that = this;
- main.getData('GetWordDetail?UserID=' + app.globalData.userInfo.UserID + '&Word=' + word, function (data) {
- if (data) {
- callback(data);
- } else
- callback();
- });
- },
- saveUserCollect: function (e) {
- let that = this;
- let word = e.currentTarget.dataset.word;
- main.getData('AddOrDeleteYJBDCUserCollect?UserID=' + app.globalData.userInfo.UserID + '&Word=' + word, function (data) {
- that.setData({
- IsCollect: !that.data.IsCollect,
- });
- app.globalData.UserCollect = data;
- });
- },
- playAudio: function (word) {
- let url = app.globalData.audioUrlBaidu;
- if (word.currentTarget)
- url = word.currentTarget.dataset.url;
- else {
- url = url.replace("[token]", app.globalData.BaiduToken);
- url = url.replace("[word]", word);
- }
- this.audioCtx.setSrc(url);
- this.audioCtx.play();
- },
- hideWordTranslate: function () {
- console.log("IsShowWordTranslate:false");
- // 点击空白处时隐藏单词翻译
- this.setData({
- IsShowWordTranslate: false
- });
- },
- // 替换Content对象中所有的@DOT@标记为.
- replaceAllDOTMarkers: function (obj) {
- if (!obj) return;
- // 如果是字符串,直接替换
- if (typeof obj === 'string') {
- return obj.replace(/@DOT@/g, '.');
- }
- // 如果是数组,递归处理每个元素
- if (Array.isArray(obj)) {
- for (let i = 0; i < obj.length; i++) {
- if (typeof obj[i] === 'string') {
- obj[i] = obj[i].replace(/@DOT@/g, '.');
- } else if (typeof obj[i] === 'object' && obj[i] !== null) {
- this.replaceAllDOTMarkers(obj[i]);
- }
- }
- return;
- }
- // 如果是对象,递归处理每个属性
- if (typeof obj === 'object') {
- for (let key in obj) {
- if (obj.hasOwnProperty(key)) {
- if (typeof obj[key] === 'string') {
- obj[key] = obj[key].replace(/@DOT@/g, '.');
- } else if (typeof obj[key] === 'object' && obj[key] !== null) {
- this.replaceAllDOTMarkers(obj[key]);
- }
- }
- }
- }
- },
- onPageScroll: function(e) {
- // 获取当前的scrollTop
- const scrollTop = e.scrollTop;
-
- let num=Math.ceil(100*(scrollTop)/this.data.ArticleHeight);
- if (num>100)
- num=100;
- // 更新scrollTop数据
- this.setData({
- Percentage: num,
- ScrollTop:scrollTop,
- });
- this.saveSchedule();
- },
- saveSchedule:function(){
- if (canSaveSchedule){
- let obj={};
- obj.Chapter=this.data.Chapter;
- obj.ScrollTop=this.data.ScrollTop;
- if (!obj.ScrollTop)
- obj.ScrollTop=0;
- wx.setStorageSync('Schedule_'+this.data.Title, obj);
- canSaveSchedule=0;
- setTimeout(function(){
- canSaveSchedule=1;
- },10000);
- }
- },
- onUnload:function(){
- this.saveSchedule();
- },
- catchTouchMove: main.catchTouchMove,
- onShareAppMessage: function () {
- return {
- title: app.globalData.ShareTitle,
- path: app.globalData.SharePath + '?goto=article&ID=' + this.data.ID + '&UserID=' + app.globalData.userInfo.UserID,
- }
- },
- })
|