article.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. if (data){
  136. if (data=="-1"){
  137. that.setData({
  138. IsBuildError:true,
  139. CreateTime:common.formatDateCHS(common.formatTime(new Date()),true),
  140. });
  141. }
  142. else{
  143. that.updateData(data.Content);
  144. that.setData({
  145. IsBuilding:false,
  146. ID:data.ID,
  147. });
  148. }
  149. clearInterval(interval);
  150. }
  151. });
  152. that.getChoicenessArticle();
  153. },
  154. updateReadCount:function(id){
  155. if (id!="MAX")
  156. main.getData('UpdateYJBDCArticleReadCount?UserID=' + app.globalData.userInfo.UserID+'&ID='+id, function (data) {});
  157. },
  158. //得到精选文章
  159. getChoicenessArticle:function(){
  160. let that=this;
  161. main.getData('GetYJBDCArticleList?IsChoiceness=1', function (data) {
  162. if (data) {
  163. that.setData({
  164. ChoicenessList:data,
  165. });
  166. }
  167. });
  168. },
  169. updateData:function(content){
  170. let that=this;
  171. //console.log(content);
  172. // 添加检查,确保 content 不是 undefined
  173. if (!content) {
  174. console.error('Content is undefined in updateData');
  175. return;
  176. }
  177. if (typeof content === 'string') {
  178. content = JSON.parse(content);
  179. }
  180. if (typeof content === 'string') {
  181. content = JSON.parse(content);
  182. }
  183. let hl = that.data.IsShowKeyword ? "highlight" : "nonelight";
  184. // 初始化ArticleEnglishArr数组
  185. content.ArticleEnglishArr = [];
  186. for(let i=0; i<content.ArticleEnglish.length; i++){
  187. // 确保每个句子末尾有空格,避免和下一句紧挨着
  188. const sentence = content.ArticleEnglish[i] + " ";
  189. // 按长度从长到短排序单词,确保先匹配较长的词组
  190. const sortedWords = [...content.FormsOfWords].sort((a, b) => b.length - a.length);
  191. // 创建一个句子的分段数组
  192. let segments = [{
  193. Sentence: sentence,
  194. CSS: ""
  195. }];
  196. // 对每个单词进行处理
  197. for(let j=0; j<sortedWords.length; j++){
  198. const word = sortedWords[j];
  199. // 创建一个新的分段数组,用于存储处理后的结果
  200. let newSegments = [];
  201. // 处理每个现有分段
  202. for(let k=0; k<segments.length; k++){
  203. const segment = segments[k];
  204. // 如果当前分段已经被标记为高亮,则不再处理
  205. if(segment.CSS !== ""){
  206. newSegments.push(segment);
  207. continue;
  208. }
  209. const text = segment.Sentence;
  210. // 使用正则表达式查找单词边界
  211. const regex = new RegExp(`\\b${word}\\b`, 'gi');
  212. let lastIndex = 0;
  213. let match;
  214. // 查找所有匹配项
  215. while((match = regex.exec(text)) !== null){
  216. // 添加匹配前的文本
  217. if(match.index > lastIndex){
  218. newSegments.push({
  219. Sentence: text.substring(lastIndex, match.index),
  220. CSS: ""
  221. });
  222. }
  223. // 添加匹配的单词(高亮)
  224. newSegments.push({
  225. Sentence: match[0],
  226. CSS: hl
  227. });
  228. lastIndex = match.index + match[0].length;
  229. }
  230. // 添加最后一个匹配后的文本
  231. if(lastIndex < text.length){
  232. newSegments.push({
  233. Sentence: text.substring(lastIndex),
  234. CSS: ""
  235. });
  236. }
  237. }
  238. // 更新分段数组
  239. segments = newSegments;
  240. }
  241. // 将分段数组添加到ArticleEnglishArr
  242. content.ArticleEnglishArr.push(segments);
  243. }
  244. for(let i=0;i<content.Question.length;i++){
  245. for(let j=0;j<content.Question[i].OptionsEnglish.length;j++){
  246. let str=content.Question[i].OptionsChinese[j];
  247. content.Question[i].OptionsChinese[j]=str.substr(2);
  248. }
  249. let char = content.Question[i].Answer;
  250. let asciiCode = char.charCodeAt(0);
  251. content.Question[i].AnswerNumber=asciiCode-65;
  252. content.Question[i].IsShowAnswer=false;
  253. }
  254. that.setData({
  255. Content:content,
  256. });
  257. setTimeout(function(){
  258. that.updateReadCount(that.data.ID);
  259. },3000);
  260. },
  261. setTheme:function(){
  262. let that=this;
  263. const css=Theme[that.data.IsShowLightColor?1:0];
  264. wx.setNavigationBarColor({
  265. frontColor: css.frontColor,
  266. backgroundColor: css.backgroundColor,
  267. });
  268. wx.setBackgroundColor({
  269. backgroundColor: css.backgroundColor,
  270. backgroundColorTop:css.backgroundColor,
  271. backgroundColorBottom:css.backgroundColor,
  272. });
  273. that.setData({
  274. ThemeCSS:css.Name,
  275. });
  276. wx.setStorageSync('IsShowLightColor', that.data.IsShowLightColor);
  277. },
  278. selectedAnswer:function(e){
  279. let that=this;
  280. const question=e.currentTarget.dataset.question;
  281. const index=e.currentTarget.dataset.index;
  282. if (that.data.Content.Question[question].UserAnswer==index)
  283. that.data.Content.Question[question].UserAnswer=-1;
  284. else
  285. that.data.Content.Question[question].UserAnswer=index;
  286. that.setData({
  287. Content:that.data.Content,
  288. });
  289. },
  290. showData:function(e){
  291. let that=this;
  292. let name=e.currentTarget.dataset.name;
  293. this.data[name] = !this.data[name];
  294. this.setData(this.data);
  295. if (name=="IsShowKeyword"){
  296. that.setShowKeyword();
  297. }
  298. else if (name=="IsShowLightColor"){
  299. that.setTheme();
  300. }
  301. else if (name=="IsShowAnswer"){
  302. let index=e.currentTarget.dataset.index;
  303. let content=that.data.Content;
  304. content.Question[index].IsShowAnswer=!content.Question[index].IsShowAnswer;
  305. that.setData({
  306. Content:content,
  307. });
  308. }
  309. else if (name=="IsShowQuestion"){
  310. main.showGuideContainer(that,"#panelArticle",38,30,"pic_ha08",196,279);
  311. }
  312. },
  313. setShowKeyword:function(){
  314. let that=this;
  315. let content=that.data.Content;
  316. let source="highlight",target="nonelight";
  317. if (that.data["IsShowKeyword"]){
  318. source="nonelight";
  319. target="highlight";
  320. }
  321. for(let i=0;i<content.ArticleEnglishArr.length;i++){
  322. let obj=content.ArticleEnglishArr[i];
  323. let arr=[];
  324. for(let j=0;j<obj.length;j++){
  325. if (obj[j].CSS==source)
  326. obj[j].CSS=target;
  327. }
  328. }
  329. for(let i=0;i<content.ArticleEnglish.length;i++){
  330. content.ArticleEnglish[i]=common.ReplaceAllString(content.ArticleEnglish[i],source,target);
  331. }
  332. that.setData({
  333. Content:content,
  334. });
  335. wx.setStorageSync('IsShowKeyword', that.data.IsShowKeyword);
  336. },
  337. nextQuestion:function(e){
  338. if (this.data.CurrentQuestionIndex+1<this.data.Content.Question.length){
  339. this.data.CurrentQuestionIndex=this.data.CurrentQuestionIndex+1;
  340. this.setData({
  341. CurrentQuestionIndex:this.data.CurrentQuestionIndex,
  342. });
  343. }
  344. },
  345. updateQuestionIndex:function(e){
  346. this.setData({
  347. CurrentQuestionIndex:e.detail.current,
  348. });
  349. console.log(e.detail.current);
  350. },
  351. onContainerTap: function() {
  352. const currentTime = new Date().getTime();
  353. const lastTapTime = this.data.lastTapTime;
  354. const timeDiff = currentTime - lastTapTime;
  355. // 如果两次点击的时间间隔小于300毫秒,则认为是双击
  356. if (timeDiff < 300 && timeDiff > 0) {
  357. console.log('双击事件触发');
  358. // 在这里添加双击事件的处理逻辑
  359. // 例如:切换翻译显示状态
  360. this.showData({currentTarget:{dataset:{name:"IsShowTranslate"}}});
  361. }
  362. // 更新上一次点击的时间
  363. this.setData({
  364. lastTapTime: currentTime
  365. });
  366. },
  367. //生成PDF文件
  368. //访问服务器的GeneratePDF接口,提交this.data.Content数据,获得一个生成好的pdf文件,服务端的代码已经生成好
  369. generatePDF: function(e) {
  370. let that = this;
  371. //debugger;
  372. that.data.Content.Words=that.data.Words;
  373. that.data.Content.Level=that.data.Level;
  374. that.data.Content.ArticleStyle=that.data.ArticleStyle;
  375. that.data.Content.CreateTime=that.data.CreateTime;
  376. let url = common.Encrypt("GeneratePDF");
  377. url =app.globalData.serverUrl + url;
  378. wx.request({
  379. url: url,
  380. method: "POST",
  381. data: {
  382. Content: that.data.Content,
  383. },
  384. responseType: 'arraybuffer', // 确保响应类型为arraybuffer
  385. success: function(res) {
  386. // 将arraybuffer转为临时文件
  387. const fsm = wx.getFileSystemManager();
  388. let time=common.formatTime(new Date(),"-");
  389. time=common.ReplaceAllString(time,"-","");
  390. time=common.ReplaceAllString(time,":","");
  391. time=common.ReplaceAllString(time," ","_");
  392. time=time.substring(2,time.length-2);
  393. const tempFilePath = `${wx.env.USER_DATA_PATH}/语境背单词_${time}.pdf`;
  394. try {
  395. fsm.writeFileSync(
  396. tempFilePath,
  397. res.data,
  398. 'binary'
  399. );
  400. // 直接使用临时文件路径,不再尝试永久保存
  401. console.log('文件已生成:', tempFilePath);
  402. // 打开PDF文件预览
  403. wx.openDocument({
  404. filePath: tempFilePath,
  405. fileType: 'pdf',
  406. showMenu: true, // 显示右上角菜单,可以分享
  407. success: function() {
  408. },
  409. fail: function(error) {
  410. console.error('打开文档失败:', error);
  411. wx.showToast({
  412. title: '打开文件失败',
  413. icon: 'none'
  414. });
  415. }
  416. });
  417. } catch (error) {
  418. console.error('写入文件失败:', error);
  419. wx.showToast({
  420. title: '写入文件失败',
  421. icon: 'none'
  422. });
  423. }
  424. },
  425. fail: function(err) {
  426. console.error('请求GeneratePDF接口失败:', err);
  427. wx.showToast({
  428. title: '网络错误,请稍候重试',
  429. icon: 'none'
  430. });
  431. }
  432. });
  433. },
  434. // 处理导航栏返回按钮点击事件
  435. onUnload: function() {
  436. if (!this.data.ID || this.data.OpenType=="Guide"){
  437. app.globalData.SelectedWords=[];
  438. wx.navigateBack({
  439. delta: 1,
  440. });
  441. }
  442. },
  443. onLongPress: function(e) {
  444. let that = this;
  445. let strType=e.currentTarget.dataset.strtype;
  446. let selectedIndex=e.currentTarget.dataset.index;
  447. let selectedIndex2=e.currentTarget.dataset.index2;
  448. let engSentence,chnSentence;
  449. if (strType=="article"){
  450. //console.log("选中的句子索引:", selectedIndex);
  451. //console.log("选中的句子:", that.data.Content.ArticleEnglish[selectedIndex]);
  452. engSentence=that.data.Content.ArticleEnglish[selectedIndex];
  453. chnSentence=that.data.Content.ArticleChinese[selectedIndex];
  454. }
  455. else if (strType=="question"){
  456. engSentence=that.data.Content.Question[selectedIndex].QuestionEnglish;
  457. chnSentence=that.data.Content.Question[selectedIndex].QuestionChinese;
  458. }
  459. else if (strType=="option"){
  460. engSentence=that.data.Content.Question[selectedIndex].OptionsEnglish[selectedIndex2];
  461. chnSentence=that.data.Content.Question[selectedIndex].OptionsChinese[selectedIndex2];
  462. }
  463. this.setData({
  464. EnglishSentence:engSentence,
  465. ChineseSentence:chnSentence,
  466. });
  467. // 使用动画工具函数显示弹窗,并指定显示动画
  468. animation.toggleRemindWithAnimation(this, {
  469. showAnimation: 'remind-slide-up'
  470. });
  471. },
  472. showRemind:function(e){
  473. animation.toggleRemindWithAnimation(this, {
  474. showAnimation: 'remind-slide-down'
  475. });
  476. },
  477. goto: function (e) {
  478. let that=this;
  479. var url=e.currentTarget.dataset.url;
  480. wx.navigateTo({
  481. url: url,
  482. });
  483. },
  484. catchTouchMove: main.catchTouchMove,
  485. onShareAppMessage: function () {
  486. return {
  487. title: app.globalData.ShareTitle,
  488. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  489. imageUrl: app.globalData.ShareImage,
  490. }
  491. },
  492. })