article.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. import common from '../../utils/util';
  2. import main from '../../utils/main';
  3. import animation from '../../utils/animation';
  4. import commonBehavior from '../behaviors/commonBehavior';
  5. const Theme=[{
  6. "Name":"DarkColor",
  7. "backgroundColor": "#004433",
  8. "color":"#C1E1C1",
  9. "frontColor": '#ffffff',
  10. },{
  11. "Name":"LightColor",
  12. "backgroundColor": "#D0ECD3",
  13. "color":"#151815",
  14. "frontColor": '#000000',
  15. }
  16. ];
  17. const app = getApp();
  18. var timeout1=[];//用于判断是否是双击
  19. Page({
  20. behaviors: [commonBehavior],
  21. data: {
  22. Words:"",
  23. IsShowFirstOpen:true,
  24. IsBuilding:false,
  25. IsBuildError:false,
  26. IsShowLightColor:false,
  27. IsShowKeyword:true,//显示关键词
  28. IsShowQuestion:false,
  29. IsShowTranslate:false,
  30. IsShowSetting:false,
  31. IsShowWordTranslate:false,
  32. IsBuildQueue:false,
  33. IsShowWordDetail:false,//显示单词细节
  34. TranslateHeight:100,//翻译单词框的高度
  35. CurrentQuestionIndex:0,
  36. swiperHeight: "526rpx",
  37. lastTapTime: 0, // 记录上一次点击的时间,用于检测双击
  38. CountDown:30,//倒计时秒数
  39. },
  40. onLoad: function (options) {
  41. let that = this;
  42. let words=app.globalData.SelectedWords.join(",");
  43. let wordsStr=app.globalData.SelectedWords.join(" ");
  44. const IsShowFirstOpen2=wx.getStorageSync('IsShowFirstOpen2');
  45. const IsShowGuideContainer=wx.getStorageSync('IsShowGuideContainer');
  46. if (!IsShowGuideContainer && !IsShowFirstOpen2){
  47. that.setData({
  48. IsShowFirstOpen2:1,
  49. });
  50. }
  51. else if (IsShowGuideContainer){
  52. that.setData({
  53. IsShowGuideContainer:true,
  54. IsShowFirstOpen2:false,
  55. });
  56. main.showGuideContainer(that,"#btnQAndA",-61,-195,"pic_ha07",240,0);
  57. }
  58. let IsShowKeyword=wx.getStorageSync('IsShowKeyword');
  59. if (IsShowKeyword===""){
  60. IsShowKeyword=true;
  61. wx.setStorageSync('IsShowKeyword', true);
  62. }
  63. let IsShowLightColor=wx.getStorageSync('IsShowLightColor');
  64. if (!IsShowLightColor){
  65. IsShowLightColor=false;
  66. }
  67. that.setData({
  68. Containnerheight: main.getWindowHeight(),
  69. Words:words,
  70. WordsStr:wordsStr,
  71. Level:options.Level,
  72. ArticleStyle:options.ArticleStyle,
  73. AIVersion:options.AIVersion,
  74. OpenType:options.Type,//打开的类型,可能是新用户引导
  75. IsShowLightColor:IsShowLightColor,
  76. IsShowKeyword:IsShowKeyword,
  77. IsBuildError:false,
  78. IsBuilding:false,
  79. IsCollect:false,
  80. });
  81. if (options.ID)
  82. that.getArticleByID(options.ID);
  83. else
  84. that.init(options);
  85. that.setTheme();
  86. // //测试
  87. // that.setData({
  88. // IsBuilding:true,
  89. // });
  90. // that.getChoicenessArticle();
  91. main.checkGenerating();
  92. this.audioCtx = wx.createAudioContext('myAudio');
  93. },
  94. onShow:function(){
  95. main.getPageInfo();
  96. },
  97. getArticleByID:function(id){
  98. let that=this;
  99. main.getData('GetYJBDCArticleList?UserID=' + app.globalData.userInfo.UserID+'&ID='+id, function (data) {
  100. if (data) {
  101. data=data[0];
  102. that.setData({
  103. Words:data.Words,
  104. Level:data.Level,
  105. ArticleStyle:data.ArticleStyle,
  106. CreateTime:data.CreateTime,
  107. ID:id,
  108. });
  109. let content=data.JSONString;
  110. that.updateData(content);
  111. that.updateReadCount(that.data.ID);
  112. }
  113. });
  114. },
  115. init:function(options){
  116. let that=this;
  117. let interval=0;
  118. that.data.CountDown=options.BuildSecond;
  119. that.setData({
  120. CountDown:that.data.CountDown,
  121. });
  122. interval = setInterval(function(){
  123. that.setData({
  124. CountDown:--that.data.CountDown,
  125. });
  126. if (that.data.CountDown<=0)
  127. clearInterval(interval);
  128. },1200);
  129. that.setData({
  130. IsBuilding:true,
  131. IsNew:true,
  132. });
  133. app.globalData.Generating=true;
  134. let words=app.globalData.SelectedWords.join(",");
  135. main.postData('QueuedGenerateArticle?UserID='+app.globalData.userInfo.UserID, {
  136. Words:words,
  137. Level:that.data.Level,
  138. ArticleStyle:that.data.ArticleStyle,
  139. AIVersion:that.data.AIVersion
  140. }, function (data) {
  141. app.globalData.Generating=false;
  142. app.globalData.GeneratingStart=false;
  143. if (data){
  144. //console.log("请求生成数据:"+JSON.stringify(data));
  145. if (data=="-1"){
  146. that.setData({
  147. IsBuilding:false,
  148. IsBuildError:true,
  149. IsBuildQueue:false,
  150. });
  151. }
  152. else if (data=="-2"){
  153. that.setData({
  154. IsBuilding:false,
  155. IsBuildError:false,
  156. IsBuildQueue:true,
  157. });
  158. }
  159. else{
  160. console.log("生成1");
  161. if (that.data.IsNew){
  162. console.log("生成2");
  163. that.updateData(data.Content);
  164. that.updateReadCount(that.data.ID);
  165. }
  166. that.setData({
  167. IsBuilding:false,
  168. ID:data.ID,
  169. CreateTime:common.formatDateCHS(common.formatTime(new Date()),true),
  170. });
  171. }
  172. clearInterval(interval);
  173. }
  174. });
  175. that.getChoicenessArticle();
  176. },
  177. updateReadCount:function(id){
  178. if (id!="MAX")
  179. main.getData('UpdateYJBDCArticleReadCount?UserID=' + app.globalData.userInfo.UserID+'&ID='+id, function (data) {});
  180. },
  181. //得到精选文章
  182. getChoicenessArticle:function(){
  183. let that=this;
  184. main.getData('GetYJBDCArticleList?IsFine=1', function (data) {
  185. if (data) {
  186. that.setData({
  187. ChoicenessList:data,
  188. });
  189. }
  190. });
  191. },
  192. updateData:function(content){
  193. let that=this;
  194. //console.log(content);
  195. // 添加检查,确保 content 不是 undefined
  196. if (!content) {
  197. console.error('Content is undefined in updateData');
  198. return;
  199. }
  200. if (typeof content === 'string') {
  201. content = JSON.parse(content);
  202. }
  203. if (typeof content === 'string') {
  204. content = JSON.parse(content);
  205. }
  206. let hl = that.data.IsShowKeyword ? "highlight" : "nonelight";
  207. // 初始化ArticleEnglishArr数组
  208. content.ArticleEnglishArr = [];
  209. for(let i=0; i<content.ArticleEnglish.length; i++){
  210. // 确保每个句子末尾有空格,避免和下一句紧挨着
  211. let sentence = content.ArticleEnglish[i] + " ";
  212. // 替换所有单引号为双引号,包括对话中的单引号和句尾的单引号
  213. sentence = sentence.replace(/(^|[,.;:!?\s])'([^']+)'([,.;:!?]|\s|$)/g, '$1"$2"$3');
  214. // 处理常见的带点缩写,将其中的点替换为特殊标记,以防止被分割
  215. // 1. 称谓缩写
  216. sentence = sentence.replace(/\b(Dr|Mr|Mrs|Ms|Prof)\./g, '$1@DOT@');
  217. // 2. 时间缩写
  218. sentence = sentence.replace(/\b([ap])\.m\./gi, '$1@DOT@m@DOT@');
  219. // 3. 学位缩写
  220. sentence = sentence.replace(/\b(B|M|Ph)\.([A-Z])\./gi, '$1@DOT@$2@DOT@');
  221. // 4. 公司名称缩写
  222. sentence = sentence.replace(/\b(Inc|Ltd|Co|Corp)\./g, '$1@DOT@');
  223. // 5. 拉丁缩写
  224. sentence = sentence.replace(/\b(e\.g|i\.e|etc|et al|vs)\./g, '$1@DOT@');
  225. // 6. 军衔缩写
  226. sentence = sentence.replace(/\b(Lt|Col|Gen|Sgt|Capt)\./g, '$1@DOT@');
  227. // 7. 地理缩写
  228. sentence = sentence.replace(/\b(St|Ave|Rd|Blvd)\./g, '$1@DOT@');
  229. // 8. 其他常见缩写
  230. sentence = sentence.replace(/\b(U\.S|U\.K|U\.N)\./g, '$1@DOT@');
  231. // 先处理破折号,在破折号前后添加空格,使其成为独立元素
  232. sentence = sentence.replace(/([^\s])([—\-–—])([^\s])/g, '$1 $2 $3');
  233. // 将句子按空格分割成单词数组
  234. let words = sentence.trim().split(/\s+/);
  235. let wordObjects = [];
  236. // 处理每个单词,检查是否需要高亮
  237. for(let j=0; j<words.length; j++){
  238. let word = words[j];
  239. // 检查是否是单独的破折号
  240. let isDash = /^[—\-–—]$/.test(word);
  241. let originalWord = word.replace(/[.,!?;:"'()—\-–—_]/g, ''); // 去除标点符号和特殊字符(包括各种破折号)
  242. let isHighlighted = false;
  243. // 检查单词是否在需要高亮的列表中
  244. for(let k=0; k<content.FormsOfWords.length; k++){
  245. if(originalWord.toLowerCase() === content.FormsOfWords[k].toLowerCase()){
  246. isHighlighted = true;
  247. break;
  248. }
  249. }
  250. // 添加单词对象到数组
  251. wordObjects.push({
  252. Sentence: word,
  253. CSS: isHighlighted ? hl : "",
  254. OriginalWord: originalWord
  255. });
  256. }
  257. // 将单词数组添加到ArticleEnglishArr
  258. content.ArticleEnglishArr.push(wordObjects);
  259. }
  260. for(let i=0;i<content.Question.length;i++){
  261. //console.log("i:"+i);
  262. for(let j=0;j<content.Question[i].OptionsEnglish.length;j++){
  263. content.Question[i].OptionsEnglish[j] = content.Question[i].OptionsEnglish[j].replace(/(^|[,.]\s+)'([^']+)'/g, '$1"$2"');
  264. if (j<4){//选项不能超过4个
  265. let str=content.Question[i].OptionsChinese[j];
  266. content.Question[i].OptionsChinese[j]=str.substr(2);
  267. }
  268. }
  269. let char = content.Question[i].Answer;
  270. let asciiCode = char.charCodeAt(0);
  271. content.Question[i].AnswerNumber=asciiCode-65;
  272. content.Question[i].IsShowAnswer=false;
  273. }
  274. // 在设置数据前,确保所有@DOT@都被替换回.
  275. that.replaceAllDOTMarkers(content);
  276. that.setData({
  277. Content:content,
  278. });
  279. },
  280. setTheme:function(){
  281. let that=this;
  282. const css=Theme[that.data.IsShowLightColor?1:0];
  283. wx.setNavigationBarColor({
  284. frontColor: css.frontColor,
  285. backgroundColor: css.backgroundColor,
  286. });
  287. wx.setBackgroundColor({
  288. backgroundColor: css.backgroundColor,
  289. backgroundColorTop:css.backgroundColor,
  290. backgroundColorBottom:css.backgroundColor,
  291. });
  292. that.setData({
  293. ThemeCSS:css.Name,
  294. });
  295. wx.setStorageSync('IsShowLightColor', that.data.IsShowLightColor);
  296. },
  297. selectedAnswer:function(e){
  298. let that=this;
  299. const question=e.currentTarget.dataset.question;
  300. const index=e.currentTarget.dataset.index;
  301. if (that.data.Content.Question[question].UserAnswer==index)
  302. that.data.Content.Question[question].UserAnswer=-1;
  303. else
  304. that.data.Content.Question[question].UserAnswer=index;
  305. that.setData({
  306. Content:that.data.Content,
  307. });
  308. },
  309. showData:function(e){
  310. let that=this;
  311. let name=e.currentTarget.dataset.name;
  312. this.data[name] = !this.data[name];
  313. this.setData(this.data);
  314. if (name=="IsShowKeyword"){
  315. that.setShowKeyword();
  316. }
  317. else if (name=="IsShowLightColor"){
  318. that.setTheme();
  319. }
  320. else if (name=="IsShowAnswer"){
  321. let index=e.currentTarget.dataset.index;
  322. let content=that.data.Content;
  323. content.Question[index].IsShowAnswer=!content.Question[index].IsShowAnswer;
  324. that.setData({
  325. Content:content,
  326. });
  327. }
  328. else if (name=="IsShowQuestion"){
  329. main.showGuideContainer(that,"#panelArticle",38,30,"pic_ha08",196,279);
  330. }
  331. },
  332. setShowKeyword:function(){
  333. let that=this;
  334. let content=that.data.Content;
  335. let source="highlight",target="nonelight";
  336. if (that.data["IsShowKeyword"]){
  337. source="nonelight";
  338. target="highlight";
  339. }
  340. for(let i=0;i<content.ArticleEnglishArr.length;i++){
  341. let obj=content.ArticleEnglishArr[i];
  342. let arr=[];
  343. for(let j=0;j<obj.length;j++){
  344. if (obj[j].CSS==source)
  345. obj[j].CSS=target;
  346. }
  347. }
  348. for(let i=0;i<content.ArticleEnglish.length;i++){
  349. content.ArticleEnglish[i]=common.ReplaceAllString(content.ArticleEnglish[i],source,target);
  350. }
  351. that.setData({
  352. Content:content,
  353. });
  354. wx.setStorageSync('IsShowKeyword', that.data.IsShowKeyword);
  355. },
  356. nextQuestion:function(e){
  357. if (this.data.CurrentQuestionIndex+1<this.data.Content.Question.length){
  358. this.data.CurrentQuestionIndex=this.data.CurrentQuestionIndex+1;
  359. this.setData({
  360. CurrentQuestionIndex:this.data.CurrentQuestionIndex,
  361. });
  362. }
  363. },
  364. updateQuestionIndex:function(e){
  365. this.setData({
  366. CurrentQuestionIndex:e.detail.current,
  367. });
  368. console.log(e.detail.current);
  369. },
  370. //生成PDF文件
  371. //访问服务器的GeneratePDF接口,提交this.data.Content数据,获得一个生成好的pdf文件,服务端的代码已经生成好
  372. generatePDF: function(e) {
  373. let that = this;
  374. //debugger;
  375. that.data.Content.Words=that.data.Words;
  376. that.data.Content.Level=that.data.Level;
  377. that.data.Content.ArticleStyle=that.data.ArticleStyle;
  378. that.data.Content.CreateTime=that.data.CreateTime;
  379. let url = common.Encrypt("GeneratePDF");
  380. url =app.globalData.serverUrl + url;
  381. wx.request({
  382. url: url,
  383. method: "POST",
  384. data: {
  385. Content: that.data.Content,
  386. },
  387. responseType: 'arraybuffer', // 确保响应类型为arraybuffer
  388. success: function(res) {
  389. // 将arraybuffer转为临时文件
  390. const fsm = wx.getFileSystemManager();
  391. let time=common.formatTime(new Date(),"-");
  392. time=common.ReplaceAllString(time,"-","");
  393. time=common.ReplaceAllString(time,":","");
  394. time=common.ReplaceAllString(time," ","_");
  395. time=time.substring(2,time.length-2);
  396. const tempFilePath = `${wx.env.USER_DATA_PATH}/语境背单词_${time}.pdf`;
  397. try {
  398. fsm.writeFileSync(
  399. tempFilePath,
  400. res.data,
  401. 'binary'
  402. );
  403. // 直接使用临时文件路径,不再尝试永久保存
  404. console.log('文件已生成:', tempFilePath);
  405. // 打开PDF文件预览
  406. wx.openDocument({
  407. filePath: tempFilePath,
  408. fileType: 'pdf',
  409. showMenu: true, // 显示右上角菜单,可以分享
  410. success: function() {
  411. },
  412. fail: function(error) {
  413. console.error('打开文档失败:', error);
  414. wx.showToast({
  415. title: '打开文件失败',
  416. icon: 'none'
  417. });
  418. }
  419. });
  420. } catch (error) {
  421. console.error('写入文件失败:', error);
  422. wx.showToast({
  423. title: '写入文件失败',
  424. icon: 'none'
  425. });
  426. }
  427. },
  428. fail: function(err) {
  429. console.error('请求GeneratePDF接口失败:', err);
  430. wx.showToast({
  431. title: '网络错误,请稍候重试',
  432. icon: 'none'
  433. });
  434. }
  435. });
  436. },
  437. // 处理导航栏返回按钮点击事件
  438. onUnload: function() {
  439. if ((this.data.IsNew || this.data.OpenType=="Guide") && !this.data.IsBuildError){
  440. wx.removeStorageSync('IsShowGuideContainer');
  441. app.globalData.SelectedWords=[];
  442. wx.navigateBack({
  443. delta: 1,
  444. });
  445. //判断用户是否离开
  446. this.setData({
  447. IsNew:false,
  448. });
  449. }
  450. this.setData({
  451. IsBuildQueue:false,
  452. });
  453. },
  454. onLongPress: function(e) {
  455. let that = this;
  456. let strType=e.currentTarget.dataset.strtype;
  457. let selectedIndex=e.currentTarget.dataset.index;
  458. let selectedIndex2=e.currentTarget.dataset.index2;
  459. let engSentence,chnSentence;
  460. if (strType=="article"){
  461. //console.log("选中的句子索引:", selectedIndex);
  462. //console.log("选中的句子:", that.data.Content.ArticleEnglish[selectedIndex]);
  463. engSentence=that.data.Content.ArticleEnglish[selectedIndex];
  464. chnSentence=that.data.Content.ArticleChinese[selectedIndex];
  465. }
  466. else if (strType=="question"){
  467. engSentence=that.data.Content.Question[selectedIndex].QuestionEnglish;
  468. chnSentence=that.data.Content.Question[selectedIndex].QuestionChinese;
  469. }
  470. else if (strType=="option"){
  471. engSentence=that.data.Content.Question[selectedIndex].OptionsEnglish[selectedIndex2];
  472. chnSentence=that.data.Content.Question[selectedIndex].OptionsChinese[selectedIndex2];
  473. }
  474. this.setData({
  475. EnglishSentence:engSentence,
  476. ChineseSentence:chnSentence,
  477. IsShowWordTranslate:true,
  478. TranslateHeight:240,
  479. });
  480. },
  481. onLongPressApi: function(e) {
  482. let that = this;
  483. let strType=e.currentTarget.dataset.strtype;
  484. let selectedIndex=e.currentTarget.dataset.index;
  485. let engSentence,chnSentence;
  486. if (strType=="article"){
  487. engSentence=that.data.Content.ArticleEnglish[selectedIndex];
  488. let param={};
  489. param.Sentence=engSentence;
  490. main.postData('GetSentenceTranslate?UserID=' + app.globalData.userInfo.UserID,param, function (data) {
  491. if (data) {
  492. chnSentence=data;
  493. that.setData({
  494. EnglishSentence:engSentence,
  495. ChineseSentence:chnSentence,
  496. IsShowWordTranslate:true,
  497. TranslateHeight:240,
  498. });
  499. }
  500. });
  501. }
  502. },
  503. selectWord:function(e){
  504. let that=this;
  505. //以下为双击判断
  506. const currentTime = new Date().getTime();
  507. const lastTapTime = this.data.lastTapTime;
  508. const timeDiff = currentTime - lastTapTime;
  509. const param=e;
  510. //以下为单击
  511. const timeout11=setTimeout(function(){
  512. that.onClickHandler(param);
  513. },300);
  514. timeout1.push(timeout11);
  515. //console.log("timeout1:"+timeout1);
  516. // 如果两次点击的时间间隔小于300毫秒,则认为是双击
  517. if (timeDiff < 300 && timeDiff > 0) {
  518. console.log('双击事件触发');
  519. for(let i=0;i<timeout1.length;i++)
  520. clearTimeout(timeout1[i]);
  521. // 在这里添加双击事件的处理逻辑
  522. that.onDoubleClickHandler(param);
  523. }
  524. // 更新上一次点击的时间
  525. this.setData({
  526. lastTapTime: currentTime
  527. });
  528. },
  529. onClickHandler:function(e){
  530. let that=this;
  531. let css=e.currentTarget.dataset.css;
  532. let word=e.currentTarget.dataset.word;
  533. let originalWord=e.currentTarget.dataset.originalWord || word;
  534. console.log("点击的单词:", originalWord);
  535. that.playAudio(originalWord);
  536. let b=false;
  537. if (css){
  538. let list=that.data.Content.FormsOfWords;
  539. for(let i=0;i<list.length;i++){
  540. if(list[i].toLowerCase() == originalWord.toLowerCase() && that.data.Content.FormsOfWordsChinese && that.data.Content.FormsOfWordsChinese[i]){
  541. let wordChinese=that.data.Content.FormsOfWordsChinese[i];
  542. // 直接更新翻译内容,不改变IsShowWordTranslate状态(如果已经是true)
  543. let updateData = {
  544. EnglishSentence: originalWord,
  545. ChineseSentence: wordChinese,
  546. };
  547. //debugger;
  548. // 只有当当前不显示翻译时,才设置IsShowWordTranslate为true
  549. if (!that.data.IsShowWordTranslate) {
  550. updateData.IsShowWordTranslate = true;
  551. }
  552. updateData.TranslateHeight=100;
  553. that.setData(updateData);
  554. b=true;
  555. break;
  556. }
  557. }
  558. }
  559. if (!b){
  560. that.getWordChinese(originalWord,function(wordTarget){
  561. // 准备更新数据
  562. let updateData = {};
  563. updateData.TranslateHeight=100;
  564. if (wordTarget){
  565. updateData.EnglishSentence = wordTarget.Word;
  566. updateData.ChineseSentence = wordTarget.Translate;
  567. }
  568. else{
  569. updateData.EnglishSentence = originalWord;
  570. updateData.ChineseSentence = "";
  571. }
  572. // 只有当当前不显示翻译时,才设置IsShowWordTranslate为true
  573. if (!that.data.IsShowWordTranslate) {
  574. updateData.IsShowWordTranslate = true;
  575. }
  576. that.setData(updateData);
  577. });
  578. }
  579. that.setData({
  580. IsCollect:false,
  581. });
  582. app.globalData.UserCollect.filter(function (item) {
  583. if (item.Word==originalWord){
  584. that.setData({
  585. IsCollect:true,
  586. });
  587. }
  588. });
  589. },
  590. onDoubleClickHandler:function(e){
  591. let that=this;
  592. let originalWord=e.currentTarget.dataset.originalWord || word;
  593. that.playAudio(originalWord);
  594. that.getWordDetail(originalWord,function(result){
  595. if (result){
  596. if (result.ENG.Book){
  597. for(let i=0;i<result.ENG.Book.length;i++){
  598. result.ENG.Book[i]=result.ENG.Book[i].substring(result.ENG.Book[i].indexOf(" "));
  599. result.ENG.Book[i]=result.ENG.Book[i].substring(1,result.ENG.Book[i].indexOf("单元词汇")-2);
  600. result.ENG.Book[i]=result.ENG.Book[i].replace("英语 ","英语 人教版PEP ")
  601. }
  602. }
  603. if (result.ENG.ExamplesSentences){
  604. for(let j=0;j<3;j++){
  605. for(let i=0;i<result.ENG.ExamplesSentences.length;i++){
  606. result.ENG.ExamplesSentences[i][0]=result.ENG.ExamplesSentences[i][0].replace("[线]","");
  607. result.ENG.ExamplesSentences[i][0]=result.ENG.ExamplesSentences[i][0].replace("[/线]","");
  608. }
  609. }
  610. }
  611. that.setData({
  612. IsShowWordDetail:true,
  613. IsShowWordTranslate:false,
  614. WordJSON:result.ENG,
  615. });
  616. that.setData({
  617. IsCollect:false,
  618. });
  619. app.globalData.UserCollect.filter(function (item) {
  620. if (item.Word==originalWord){
  621. that.setData({
  622. IsCollect:true,
  623. });
  624. }
  625. });
  626. }
  627. //console.log(result);
  628. });
  629. },
  630. getWordChinese:function(word,callback){
  631. let that=this;
  632. main.getData('GetWordChinese?UserID=' + app.globalData.userInfo.UserID+'&Word='+word+'&Level='+that.data.Level, function (data) {
  633. if (data) {
  634. callback(data);
  635. }
  636. else
  637. callback();
  638. });
  639. },
  640. getWordDetail:function(word,callback){
  641. let that=this;
  642. main.getData('GetWordDetail?UserID=' + app.globalData.userInfo.UserID+'&Word='+word, function (data) {
  643. if (data) {
  644. callback(data);
  645. }
  646. else
  647. callback();
  648. });
  649. },
  650. saveUserCollect:function(e){
  651. let that=this;
  652. let word=e.currentTarget.dataset.word;
  653. main.getData('AddOrDeleteYJBDCUserCollect?UserID='+app.globalData.userInfo.UserID+'&Word='+word, function (data) {
  654. that.setData({
  655. IsCollect:!that.data.IsCollect,
  656. });
  657. app.globalData.UserCollect=data;
  658. });
  659. },
  660. playAudio: function (word) {
  661. let url = app.globalData.audioUrlBaidu;
  662. if (word.currentTarget)
  663. url=word.currentTarget.dataset.url;
  664. else{
  665. url = url.replace("[token]", app.globalData.BaiduToken);
  666. url = url.replace("[word]", word);
  667. }
  668. this.audioCtx.setSrc(url);
  669. this.audioCtx.play();
  670. },
  671. hideWordTranslate: function() {
  672. console.log("IsShowWordTranslate:false");
  673. // 点击空白处时隐藏单词翻译
  674. this.setData({
  675. IsShowWordTranslate: false
  676. });
  677. },
  678. goto: function (e) {
  679. let that=this;
  680. var url=e.currentTarget.dataset.url;
  681. wx.navigateTo({
  682. url: url,
  683. });
  684. },
  685. // 替换Content对象中所有的@DOT@标记为.
  686. replaceAllDOTMarkers: function(obj) {
  687. if (!obj) return;
  688. // 如果是字符串,直接替换
  689. if (typeof obj === 'string') {
  690. return obj.replace(/@DOT@/g, '.');
  691. }
  692. // 如果是数组,递归处理每个元素
  693. if (Array.isArray(obj)) {
  694. for (let i = 0; i < obj.length; i++) {
  695. if (typeof obj[i] === 'string') {
  696. obj[i] = obj[i].replace(/@DOT@/g, '.');
  697. } else if (typeof obj[i] === 'object' && obj[i] !== null) {
  698. this.replaceAllDOTMarkers(obj[i]);
  699. }
  700. }
  701. return;
  702. }
  703. // 如果是对象,递归处理每个属性
  704. if (typeof obj === 'object') {
  705. for (let key in obj) {
  706. if (obj.hasOwnProperty(key)) {
  707. if (typeof obj[key] === 'string') {
  708. obj[key] = obj[key].replace(/@DOT@/g, '.');
  709. } else if (typeof obj[key] === 'object' && obj[key] !== null) {
  710. this.replaceAllDOTMarkers(obj[key]);
  711. }
  712. }
  713. }
  714. }
  715. },
  716. catchTouchMove: main.catchTouchMove,
  717. onShareAppMessage: function () {
  718. return {
  719. title: app.globalData.ShareTitle,
  720. path: app.globalData.SharePath + '?goto=article&ID='+this.data.ID+'&UserID=' + app.globalData.userInfo.UserID,
  721. }
  722. },
  723. })