article.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. Page({
  19. behaviors: [commonBehavior],
  20. data: {
  21. Words:"",
  22. IsShowFirstOpen:true,
  23. IsBuilding:false,
  24. IsBuildError:false,
  25. IsShowLightColor:false,
  26. IsShowKeyword:true,//显示关键词
  27. IsShowQuestion:false,
  28. IsShowTranslate:false,
  29. IsShowSetting:false,
  30. IsShowRemind:false,
  31. remindAnimation: "", // 翻译弹窗动画类
  32. CurrentQuestionIndex:0,
  33. swiperHeight: "526rpx",
  34. lastTapTime: 0, // 记录上一次点击的时间,用于检测双击
  35. CountDown:30,//倒计时秒数
  36. },
  37. onLoad: function (options) {
  38. let that = this;
  39. let words=app.globalData.SelectedWords.join(",");
  40. let wordsStr=app.globalData.SelectedWords.join(" ");
  41. const IsShowFirstOpen2=wx.getStorageSync('IsShowFirstOpen2');
  42. const IsShowGuideContainer=wx.getStorageSync('IsShowGuideContainer');
  43. if (!IsShowGuideContainer && !IsShowFirstOpen2){
  44. that.setData({
  45. IsShowFirstOpen2:1,
  46. });
  47. }
  48. else if (IsShowGuideContainer){
  49. that.setData({
  50. IsShowGuideContainer:true,
  51. IsShowFirstOpen2:false,
  52. });
  53. main.showGuideContainer(that,"#btnQAndA",-61,-195,"pic_ha07",240,0);
  54. }
  55. let IsShowKeyword=wx.getStorageSync('IsShowKeyword');
  56. if (IsShowKeyword===""){
  57. IsShowKeyword=true;
  58. wx.setStorageSync('IsShowKeyword', true);
  59. }
  60. let IsShowLightColor=wx.getStorageSync('IsShowLightColor');
  61. if (!IsShowLightColor){
  62. IsShowLightColor=false;
  63. }
  64. that.setData({
  65. Containnerheight: main.getWindowHeight(),
  66. Words:words,
  67. WordsStr:wordsStr,
  68. Level:options.Level,
  69. ArticleStyle:options.ArticleStyle,
  70. AIVersion:options.AIVersion,
  71. OpenType:options.Type,//打开的类型,可能是新用户引导
  72. IsShowLightColor:IsShowLightColor,
  73. IsShowKeyword:IsShowKeyword,
  74. IsBuildError:false,
  75. IsBuilding:false,
  76. });
  77. if (options.ID)
  78. that.getArticleByID(options.ID);
  79. else
  80. that.init(options);
  81. that.setTheme();
  82. // //测试
  83. // that.setData({
  84. // IsBuilding:true,
  85. // });
  86. // that.getChoicenessArticle();
  87. main.checkGenerating();
  88. this.audioCtx = wx.createAudioContext('myAudio');
  89. },
  90. onShow:function(){
  91. main.getPageInfo();
  92. },
  93. getArticleByID:function(id){
  94. let that=this;
  95. main.getData('GetYJBDCArticleList?UserID=' + app.globalData.userInfo.UserID+'&ID='+id, function (data) {
  96. if (data) {
  97. data=data[0];
  98. that.setData({
  99. Words:data.Words,
  100. Level:data.Level,
  101. ArticleStyle:data.ArticleStyle,
  102. CreateTime:data.CreateTime,
  103. ID:id,
  104. });
  105. let content=data.JSONString;
  106. that.updateData(content);
  107. }
  108. });
  109. },
  110. init:function(options){
  111. let that=this;
  112. let interval=0;
  113. that.data.CountDown=options.BuildSecond;
  114. that.setData({
  115. CountDown:that.data.CountDown,
  116. });
  117. interval = setInterval(function(){
  118. that.setData({
  119. CountDown:--that.data.CountDown,
  120. });
  121. if (that.data.CountDown<=0)
  122. clearInterval(interval);
  123. },1200);
  124. that.setData({
  125. IsBuilding:true,
  126. IsNew:true,
  127. });
  128. app.globalData.Generating=true;
  129. let words=app.globalData.SelectedWords.join(",");
  130. main.postData('GenerateArticle?UserID='+app.globalData.userInfo.UserID, {
  131. Words:words,
  132. Level:that.data.Level,
  133. ArticleStyle:that.data.ArticleStyle,
  134. AIVersion:that.data.AIVersion
  135. }, function (data) {
  136. app.globalData.Generating=false;
  137. app.globalData.GeneratingStart=false;
  138. if (data){
  139. if (data=="-1"){
  140. that.setData({
  141. IsBuildError:true,
  142. CreateTime:common.formatDateCHS(common.formatTime(new Date()),true),
  143. });
  144. }
  145. else{
  146. that.updateData(data.Content);
  147. that.setData({
  148. IsBuilding:false,
  149. ID:data.ID,
  150. IsNew:data.IsNew,
  151. });
  152. }
  153. clearInterval(interval);
  154. }
  155. });
  156. that.getChoicenessArticle();
  157. },
  158. updateReadCount:function(id){
  159. if (id!="MAX")
  160. main.getData('UpdateYJBDCArticleReadCount?UserID=' + app.globalData.userInfo.UserID+'&ID='+id, function (data) {});
  161. },
  162. //得到精选文章
  163. getChoicenessArticle:function(){
  164. let that=this;
  165. main.getData('GetYJBDCArticleList?IsFine=1', function (data) {
  166. if (data) {
  167. that.setData({
  168. ChoicenessList:data,
  169. });
  170. }
  171. });
  172. },
  173. updateData:function(content){
  174. let that=this;
  175. //console.log(content);
  176. // 添加检查,确保 content 不是 undefined
  177. if (!content) {
  178. console.error('Content is undefined in updateData');
  179. return;
  180. }
  181. if (typeof content === 'string') {
  182. content = JSON.parse(content);
  183. }
  184. if (typeof content === 'string') {
  185. content = JSON.parse(content);
  186. }
  187. let hl = that.data.IsShowKeyword ? "highlight" : "nonelight";
  188. // 初始化ArticleEnglishArr数组
  189. content.ArticleEnglishArr = [];
  190. for(let i=0; i<content.ArticleEnglish.length; i++){
  191. // 确保每个句子末尾有空格,避免和下一句紧挨着
  192. let sentence = content.ArticleEnglish[i] + " ";
  193. // 替换所有单引号为双引号,包括对话中的单引号
  194. sentence = sentence.replace(/(^|[,.]\s+)'([^']+)'/g, '$1"$2"');
  195. // 将句子按空格分割成单词数组
  196. let words = sentence.trim().split(/\s+/);
  197. let wordObjects = [];
  198. // 处理每个单词,检查是否需要高亮
  199. for(let j=0; j<words.length; j++){
  200. let word = words[j];
  201. let originalWord = word.replace(/[.,!?;:"'()—\-–—_]/g, ''); // 去除标点符号和特殊字符(包括各种破折号)
  202. let isHighlighted = false;
  203. // 检查单词是否在需要高亮的列表中
  204. for(let k=0; k<content.FormsOfWords.length; k++){
  205. if(originalWord.toLowerCase() === content.FormsOfWords[k].toLowerCase()){
  206. isHighlighted = true;
  207. break;
  208. }
  209. }
  210. // 添加单词对象到数组
  211. wordObjects.push({
  212. Sentence: word,
  213. CSS: isHighlighted ? hl : "",
  214. OriginalWord: originalWord
  215. });
  216. }
  217. // 将单词数组添加到ArticleEnglishArr
  218. content.ArticleEnglishArr.push(wordObjects);
  219. }
  220. for(let i=0;i<content.Question.length;i++){
  221. console.log("i:"+i);
  222. for(let j=0;j<content.Question[i].OptionsEnglish.length;j++){
  223. content.Question[i].OptionsEnglish[j] = content.Question[i].OptionsEnglish[j].replace(/(^|[,.]\s+)'([^']+)'/g, '$1"$2"');
  224. if (j<4){//选项不能超过4个
  225. let str=content.Question[i].OptionsChinese[j];
  226. content.Question[i].OptionsChinese[j]=str.substr(2);
  227. }
  228. }
  229. let char = content.Question[i].Answer;
  230. let asciiCode = char.charCodeAt(0);
  231. content.Question[i].AnswerNumber=asciiCode-65;
  232. content.Question[i].IsShowAnswer=false;
  233. }
  234. that.setData({
  235. Content:content,
  236. });
  237. setTimeout(function(){
  238. that.updateReadCount(that.data.ID);
  239. },3000);
  240. },
  241. setTheme:function(){
  242. let that=this;
  243. const css=Theme[that.data.IsShowLightColor?1:0];
  244. wx.setNavigationBarColor({
  245. frontColor: css.frontColor,
  246. backgroundColor: css.backgroundColor,
  247. });
  248. wx.setBackgroundColor({
  249. backgroundColor: css.backgroundColor,
  250. backgroundColorTop:css.backgroundColor,
  251. backgroundColorBottom:css.backgroundColor,
  252. });
  253. that.setData({
  254. ThemeCSS:css.Name,
  255. });
  256. wx.setStorageSync('IsShowLightColor', that.data.IsShowLightColor);
  257. },
  258. selectedAnswer:function(e){
  259. let that=this;
  260. const question=e.currentTarget.dataset.question;
  261. const index=e.currentTarget.dataset.index;
  262. if (that.data.Content.Question[question].UserAnswer==index)
  263. that.data.Content.Question[question].UserAnswer=-1;
  264. else
  265. that.data.Content.Question[question].UserAnswer=index;
  266. that.setData({
  267. Content:that.data.Content,
  268. });
  269. },
  270. showData:function(e){
  271. let that=this;
  272. let name=e.currentTarget.dataset.name;
  273. this.data[name] = !this.data[name];
  274. this.setData(this.data);
  275. if (name=="IsShowKeyword"){
  276. that.setShowKeyword();
  277. }
  278. else if (name=="IsShowLightColor"){
  279. that.setTheme();
  280. }
  281. else if (name=="IsShowAnswer"){
  282. let index=e.currentTarget.dataset.index;
  283. let content=that.data.Content;
  284. content.Question[index].IsShowAnswer=!content.Question[index].IsShowAnswer;
  285. that.setData({
  286. Content:content,
  287. });
  288. }
  289. else if (name=="IsShowQuestion"){
  290. main.showGuideContainer(that,"#panelArticle",38,30,"pic_ha08",196,279);
  291. }
  292. },
  293. setShowKeyword:function(){
  294. let that=this;
  295. let content=that.data.Content;
  296. let source="highlight",target="nonelight";
  297. if (that.data["IsShowKeyword"]){
  298. source="nonelight";
  299. target="highlight";
  300. }
  301. for(let i=0;i<content.ArticleEnglishArr.length;i++){
  302. let obj=content.ArticleEnglishArr[i];
  303. let arr=[];
  304. for(let j=0;j<obj.length;j++){
  305. if (obj[j].CSS==source)
  306. obj[j].CSS=target;
  307. }
  308. }
  309. for(let i=0;i<content.ArticleEnglish.length;i++){
  310. content.ArticleEnglish[i]=common.ReplaceAllString(content.ArticleEnglish[i],source,target);
  311. }
  312. that.setData({
  313. Content:content,
  314. });
  315. wx.setStorageSync('IsShowKeyword', that.data.IsShowKeyword);
  316. },
  317. nextQuestion:function(e){
  318. if (this.data.CurrentQuestionIndex+1<this.data.Content.Question.length){
  319. this.data.CurrentQuestionIndex=this.data.CurrentQuestionIndex+1;
  320. this.setData({
  321. CurrentQuestionIndex:this.data.CurrentQuestionIndex,
  322. });
  323. }
  324. },
  325. updateQuestionIndex:function(e){
  326. this.setData({
  327. CurrentQuestionIndex:e.detail.current,
  328. });
  329. console.log(e.detail.current);
  330. },
  331. onContainerTap: function() {
  332. const currentTime = new Date().getTime();
  333. const lastTapTime = this.data.lastTapTime;
  334. const timeDiff = currentTime - lastTapTime;
  335. // 如果两次点击的时间间隔小于300毫秒,则认为是双击
  336. if (timeDiff < 300 && timeDiff > 0) {
  337. console.log('双击事件触发');
  338. // 在这里添加双击事件的处理逻辑
  339. // 例如:切换翻译显示状态
  340. this.showData({currentTarget:{dataset:{name:"IsShowTranslate"}}});
  341. }
  342. // 更新上一次点击的时间
  343. this.setData({
  344. lastTapTime: currentTime
  345. });
  346. },
  347. //生成PDF文件
  348. //访问服务器的GeneratePDF接口,提交this.data.Content数据,获得一个生成好的pdf文件,服务端的代码已经生成好
  349. generatePDF: function(e) {
  350. let that = this;
  351. //debugger;
  352. that.data.Content.Words=that.data.Words;
  353. that.data.Content.Level=that.data.Level;
  354. that.data.Content.ArticleStyle=that.data.ArticleStyle;
  355. that.data.Content.CreateTime=that.data.CreateTime;
  356. let url = common.Encrypt("GeneratePDF");
  357. url =app.globalData.serverUrl + url;
  358. wx.request({
  359. url: url,
  360. method: "POST",
  361. data: {
  362. Content: that.data.Content,
  363. },
  364. responseType: 'arraybuffer', // 确保响应类型为arraybuffer
  365. success: function(res) {
  366. // 将arraybuffer转为临时文件
  367. const fsm = wx.getFileSystemManager();
  368. let time=common.formatTime(new Date(),"-");
  369. time=common.ReplaceAllString(time,"-","");
  370. time=common.ReplaceAllString(time,":","");
  371. time=common.ReplaceAllString(time," ","_");
  372. time=time.substring(2,time.length-2);
  373. const tempFilePath = `${wx.env.USER_DATA_PATH}/语境背单词_${time}.pdf`;
  374. try {
  375. fsm.writeFileSync(
  376. tempFilePath,
  377. res.data,
  378. 'binary'
  379. );
  380. // 直接使用临时文件路径,不再尝试永久保存
  381. console.log('文件已生成:', tempFilePath);
  382. // 打开PDF文件预览
  383. wx.openDocument({
  384. filePath: tempFilePath,
  385. fileType: 'pdf',
  386. showMenu: true, // 显示右上角菜单,可以分享
  387. success: function() {
  388. },
  389. fail: function(error) {
  390. console.error('打开文档失败:', error);
  391. wx.showToast({
  392. title: '打开文件失败',
  393. icon: 'none'
  394. });
  395. }
  396. });
  397. } catch (error) {
  398. console.error('写入文件失败:', error);
  399. wx.showToast({
  400. title: '写入文件失败',
  401. icon: 'none'
  402. });
  403. }
  404. },
  405. fail: function(err) {
  406. console.error('请求GeneratePDF接口失败:', err);
  407. wx.showToast({
  408. title: '网络错误,请稍候重试',
  409. icon: 'none'
  410. });
  411. }
  412. });
  413. },
  414. // 处理导航栏返回按钮点击事件
  415. onUnload: function() {
  416. if (this.data.IsNew || this.data.OpenType=="Guide"){
  417. wx.removeStorageSync('IsShowGuideContainer');
  418. app.globalData.SelectedWords=[];
  419. wx.navigateBack({
  420. delta: 1,
  421. });
  422. }
  423. },
  424. onLongPress: function(e) {
  425. let that = this;
  426. let strType=e.currentTarget.dataset.strtype;
  427. let selectedIndex=e.currentTarget.dataset.index;
  428. let selectedIndex2=e.currentTarget.dataset.index2;
  429. let engSentence,chnSentence;
  430. if (strType=="article"){
  431. //console.log("选中的句子索引:", selectedIndex);
  432. //console.log("选中的句子:", that.data.Content.ArticleEnglish[selectedIndex]);
  433. engSentence=that.data.Content.ArticleEnglish[selectedIndex];
  434. chnSentence=that.data.Content.ArticleChinese[selectedIndex];
  435. }
  436. else if (strType=="question"){
  437. engSentence=that.data.Content.Question[selectedIndex].QuestionEnglish;
  438. chnSentence=that.data.Content.Question[selectedIndex].QuestionChinese;
  439. }
  440. else if (strType=="option"){
  441. engSentence=that.data.Content.Question[selectedIndex].OptionsEnglish[selectedIndex2];
  442. chnSentence=that.data.Content.Question[selectedIndex].OptionsChinese[selectedIndex2];
  443. }
  444. this.setData({
  445. EnglishSentence:engSentence,
  446. ChineseSentence:chnSentence,
  447. });
  448. // 使用动画工具函数显示弹窗,并指定显示动画
  449. animation.toggleRemindWithAnimation(this, {
  450. showAnimation: 'remind-slide-up'
  451. });
  452. },
  453. selectWord:function(e){
  454. let that=this;
  455. let css=e.currentTarget.dataset.css;
  456. let word=e.currentTarget.dataset.word;
  457. let originalWord=e.currentTarget.dataset.originalWord || word;
  458. console.log("点击的单词:", originalWord);
  459. that.playAudio(originalWord);
  460. if (css){
  461. let list=that.data.Content.FormsOfWords;
  462. for(let i=0;i<list.length;i++){
  463. if(list[i].toLowerCase() == originalWord.toLowerCase()){
  464. console.log(that.data.Content.FormsOfWordsChinese[i]);
  465. }
  466. }
  467. }
  468. },
  469. playAudio: function (word) {
  470. var url = app.globalData.audioUrlBaidu;
  471. url = url.replace("[token]", app.globalData.BaiduToken);
  472. url = url.replace("[word]", word);
  473. this.audioCtx.setSrc(url);
  474. this.audioCtx.play();
  475. },
  476. showRemind:function(e){
  477. animation.toggleRemindWithAnimation(this, {
  478. showAnimation: 'remind-slide-down'
  479. });
  480. },
  481. goto: function (e) {
  482. let that=this;
  483. var url=e.currentTarget.dataset.url;
  484. wx.navigateTo({
  485. url: url,
  486. });
  487. },
  488. catchTouchMove: main.catchTouchMove,
  489. onShareAppMessage: function () {
  490. return {
  491. title: app.globalData.ShareTitle,
  492. path: app.globalData.SharePath + '?goto=article&ID='+this.data.ID+'&UserID=' + app.globalData.userInfo.UserID,
  493. }
  494. },
  495. })