article.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. },
  89. onShow:function(){
  90. main.getPageInfo();
  91. },
  92. getArticleByID:function(id){
  93. let that=this;
  94. main.getData('GetYJBDCArticleList?UserID=' + app.globalData.userInfo.UserID+'&ID='+id, function (data) {
  95. if (data) {
  96. data=data[0];
  97. that.setData({
  98. Words:data.Words,
  99. Level:data.Level,
  100. ArticleStyle:data.ArticleStyle,
  101. CreateTime:data.CreateTime,
  102. ID:id,
  103. });
  104. let content=data.JSONString;
  105. that.updateData(content);
  106. }
  107. });
  108. },
  109. init:function(options){
  110. let that=this;
  111. let interval=0;
  112. that.data.CountDown=options.BuildSecond;
  113. that.setData({
  114. CountDown:that.data.CountDown,
  115. });
  116. interval = setInterval(function(){
  117. that.setData({
  118. CountDown:--that.data.CountDown,
  119. });
  120. if (that.data.CountDown<=0)
  121. clearInterval(interval);
  122. },1200);
  123. that.setData({
  124. IsBuilding:true
  125. });
  126. app.globalData.Generating=true;
  127. let words=app.globalData.SelectedWords.join(",");
  128. main.postData('GenerateArticle?UserID='+app.globalData.userInfo.UserID, {
  129. Words:words,
  130. Level:that.data.Level,
  131. ArticleStyle:that.data.ArticleStyle,
  132. AIVersion:that.data.AIVersion
  133. }, function (data) {
  134. app.globalData.Generating=false;
  135. app.globalData.GeneratingStart=false;
  136. if (data){
  137. if (data=="-1"){
  138. that.setData({
  139. IsBuildError:true,
  140. CreateTime:common.formatDateCHS(common.formatTime(new Date()),true),
  141. });
  142. }
  143. else{
  144. that.updateData(data.Content);
  145. that.setData({
  146. IsBuilding:false,
  147. ID:data.ID,
  148. });
  149. }
  150. clearInterval(interval);
  151. }
  152. });
  153. that.getChoicenessArticle();
  154. },
  155. updateReadCount:function(id){
  156. if (id!="MAX")
  157. main.getData('UpdateYJBDCArticleReadCount?UserID=' + app.globalData.userInfo.UserID+'&ID='+id, function (data) {});
  158. },
  159. //得到精选文章
  160. getChoicenessArticle:function(){
  161. let that=this;
  162. main.getData('GetYJBDCArticleList?IsChoiceness=1', function (data) {
  163. if (data) {
  164. that.setData({
  165. ChoicenessList:data,
  166. });
  167. }
  168. });
  169. },
  170. updateData:function(content){
  171. let that=this;
  172. //console.log(content);
  173. // 添加检查,确保 content 不是 undefined
  174. if (!content) {
  175. console.error('Content is undefined in updateData');
  176. return;
  177. }
  178. if (typeof content === 'string') {
  179. content = JSON.parse(content);
  180. }
  181. if (typeof content === 'string') {
  182. content = JSON.parse(content);
  183. }
  184. let hl = that.data.IsShowKeyword ? "highlight" : "nonelight";
  185. // 初始化ArticleEnglishArr数组
  186. content.ArticleEnglishArr = [];
  187. for(let i=0; i<content.ArticleEnglish.length; i++){
  188. // 确保每个句子末尾有空格,避免和下一句紧挨着
  189. const sentence = content.ArticleEnglish[i] + " ";
  190. // 按长度从长到短排序单词,确保先匹配较长的词组
  191. const sortedWords = [...content.FormsOfWords].sort((a, b) => b.length - a.length);
  192. // 创建一个句子的分段数组
  193. let segments = [{
  194. Sentence: sentence,
  195. CSS: ""
  196. }];
  197. // 对每个单词进行处理
  198. for(let j=0; j<sortedWords.length; j++){
  199. const word = sortedWords[j];
  200. // 创建一个新的分段数组,用于存储处理后的结果
  201. let newSegments = [];
  202. // 处理每个现有分段
  203. for(let k=0; k<segments.length; k++){
  204. const segment = segments[k];
  205. // 如果当前分段已经被标记为高亮,则不再处理
  206. if(segment.CSS !== ""){
  207. newSegments.push(segment);
  208. continue;
  209. }
  210. const text = segment.Sentence;
  211. // 使用正则表达式查找单词边界
  212. const regex = new RegExp(`\\b${word}\\b`, 'gi');
  213. let lastIndex = 0;
  214. let match;
  215. // 查找所有匹配项
  216. while((match = regex.exec(text)) !== null){
  217. // 添加匹配前的文本
  218. if(match.index > lastIndex){
  219. newSegments.push({
  220. Sentence: text.substring(lastIndex, match.index),
  221. CSS: ""
  222. });
  223. }
  224. // 添加匹配的单词(高亮)
  225. newSegments.push({
  226. Sentence: match[0],
  227. CSS: hl
  228. });
  229. lastIndex = match.index + match[0].length;
  230. }
  231. // 添加最后一个匹配后的文本
  232. if(lastIndex < text.length){
  233. newSegments.push({
  234. Sentence: text.substring(lastIndex),
  235. CSS: ""
  236. });
  237. }
  238. }
  239. // 更新分段数组
  240. segments = newSegments;
  241. }
  242. // 将分段数组添加到ArticleEnglishArr
  243. content.ArticleEnglishArr.push(segments);
  244. }
  245. for(let i=0;i<content.Question.length;i++){
  246. for(let j=0;j<content.Question[i].OptionsEnglish.length;j++){
  247. if (j<4){
  248. let str=content.Question[i].OptionsChinese[j];
  249. content.Question[i].OptionsChinese[j]=str.substr(2);
  250. }
  251. }
  252. let char = content.Question[i].Answer;
  253. let asciiCode = char.charCodeAt(0);
  254. content.Question[i].AnswerNumber=asciiCode-65;
  255. content.Question[i].IsShowAnswer=false;
  256. }
  257. that.setData({
  258. Content:content,
  259. });
  260. setTimeout(function(){
  261. that.updateReadCount(that.data.ID);
  262. },3000);
  263. },
  264. setTheme:function(){
  265. let that=this;
  266. const css=Theme[that.data.IsShowLightColor?1:0];
  267. wx.setNavigationBarColor({
  268. frontColor: css.frontColor,
  269. backgroundColor: css.backgroundColor,
  270. });
  271. wx.setBackgroundColor({
  272. backgroundColor: css.backgroundColor,
  273. backgroundColorTop:css.backgroundColor,
  274. backgroundColorBottom:css.backgroundColor,
  275. });
  276. that.setData({
  277. ThemeCSS:css.Name,
  278. });
  279. wx.setStorageSync('IsShowLightColor', that.data.IsShowLightColor);
  280. },
  281. selectedAnswer:function(e){
  282. let that=this;
  283. const question=e.currentTarget.dataset.question;
  284. const index=e.currentTarget.dataset.index;
  285. if (that.data.Content.Question[question].UserAnswer==index)
  286. that.data.Content.Question[question].UserAnswer=-1;
  287. else
  288. that.data.Content.Question[question].UserAnswer=index;
  289. that.setData({
  290. Content:that.data.Content,
  291. });
  292. },
  293. showData:function(e){
  294. let that=this;
  295. let name=e.currentTarget.dataset.name;
  296. this.data[name] = !this.data[name];
  297. this.setData(this.data);
  298. if (name=="IsShowKeyword"){
  299. that.setShowKeyword();
  300. }
  301. else if (name=="IsShowLightColor"){
  302. that.setTheme();
  303. }
  304. else if (name=="IsShowAnswer"){
  305. let index=e.currentTarget.dataset.index;
  306. let content=that.data.Content;
  307. content.Question[index].IsShowAnswer=!content.Question[index].IsShowAnswer;
  308. that.setData({
  309. Content:content,
  310. });
  311. }
  312. else if (name=="IsShowQuestion"){
  313. main.showGuideContainer(that,"#panelArticle",38,30,"pic_ha08",196,279);
  314. }
  315. },
  316. setShowKeyword:function(){
  317. let that=this;
  318. let content=that.data.Content;
  319. let source="highlight",target="nonelight";
  320. if (that.data["IsShowKeyword"]){
  321. source="nonelight";
  322. target="highlight";
  323. }
  324. for(let i=0;i<content.ArticleEnglishArr.length;i++){
  325. let obj=content.ArticleEnglishArr[i];
  326. let arr=[];
  327. for(let j=0;j<obj.length;j++){
  328. if (obj[j].CSS==source)
  329. obj[j].CSS=target;
  330. }
  331. }
  332. for(let i=0;i<content.ArticleEnglish.length;i++){
  333. content.ArticleEnglish[i]=common.ReplaceAllString(content.ArticleEnglish[i],source,target);
  334. }
  335. that.setData({
  336. Content:content,
  337. });
  338. wx.setStorageSync('IsShowKeyword', that.data.IsShowKeyword);
  339. },
  340. nextQuestion:function(e){
  341. if (this.data.CurrentQuestionIndex+1<this.data.Content.Question.length){
  342. this.data.CurrentQuestionIndex=this.data.CurrentQuestionIndex+1;
  343. this.setData({
  344. CurrentQuestionIndex:this.data.CurrentQuestionIndex,
  345. });
  346. }
  347. },
  348. updateQuestionIndex:function(e){
  349. this.setData({
  350. CurrentQuestionIndex:e.detail.current,
  351. });
  352. console.log(e.detail.current);
  353. },
  354. onContainerTap: function() {
  355. const currentTime = new Date().getTime();
  356. const lastTapTime = this.data.lastTapTime;
  357. const timeDiff = currentTime - lastTapTime;
  358. // 如果两次点击的时间间隔小于300毫秒,则认为是双击
  359. if (timeDiff < 300 && timeDiff > 0) {
  360. console.log('双击事件触发');
  361. // 在这里添加双击事件的处理逻辑
  362. // 例如:切换翻译显示状态
  363. this.showData({currentTarget:{dataset:{name:"IsShowTranslate"}}});
  364. }
  365. // 更新上一次点击的时间
  366. this.setData({
  367. lastTapTime: currentTime
  368. });
  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.ID || this.data.OpenType=="Guide"){
  440. app.globalData.SelectedWords=[];
  441. wx.navigateBack({
  442. delta: 1,
  443. });
  444. }
  445. },
  446. onLongPress: function(e) {
  447. let that = this;
  448. let strType=e.currentTarget.dataset.strtype;
  449. let selectedIndex=e.currentTarget.dataset.index;
  450. let selectedIndex2=e.currentTarget.dataset.index2;
  451. let engSentence,chnSentence;
  452. if (strType=="article"){
  453. //console.log("选中的句子索引:", selectedIndex);
  454. //console.log("选中的句子:", that.data.Content.ArticleEnglish[selectedIndex]);
  455. engSentence=that.data.Content.ArticleEnglish[selectedIndex];
  456. chnSentence=that.data.Content.ArticleChinese[selectedIndex];
  457. }
  458. else if (strType=="question"){
  459. engSentence=that.data.Content.Question[selectedIndex].QuestionEnglish;
  460. chnSentence=that.data.Content.Question[selectedIndex].QuestionChinese;
  461. }
  462. else if (strType=="option"){
  463. engSentence=that.data.Content.Question[selectedIndex].OptionsEnglish[selectedIndex2];
  464. chnSentence=that.data.Content.Question[selectedIndex].OptionsChinese[selectedIndex2];
  465. }
  466. this.setData({
  467. EnglishSentence:engSentence,
  468. ChineseSentence:chnSentence,
  469. });
  470. // 使用动画工具函数显示弹窗,并指定显示动画
  471. animation.toggleRemindWithAnimation(this, {
  472. showAnimation: 'remind-slide-up'
  473. });
  474. },
  475. showRemind:function(e){
  476. animation.toggleRemindWithAnimation(this, {
  477. showAnimation: 'remind-slide-down'
  478. });
  479. },
  480. goto: function (e) {
  481. let that=this;
  482. var url=e.currentTarget.dataset.url;
  483. wx.navigateTo({
  484. url: url,
  485. });
  486. },
  487. catchTouchMove: main.catchTouchMove,
  488. onShareAppMessage: function () {
  489. return {
  490. title: app.globalData.ShareTitle,
  491. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  492. imageUrl: app.globalData.ShareImage,
  493. }
  494. },
  495. })