article.js 16 KB

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