article.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. import common from '../../utils/util';
  2. import main from '../../utils/main';
  3. const Theme=[{
  4. "Name":"DarkColor",
  5. "backgroundColor": "#004433",
  6. "color":"#C1E1C1",
  7. "frontColor": '#ffffff',
  8. },{
  9. "Name":"LightColor",
  10. "backgroundColor": "#D0ECD3",
  11. "color":"#151815",
  12. "frontColor": '#000000',
  13. }
  14. ];
  15. const app = getApp();
  16. Page({
  17. data: {
  18. Words:"",
  19. IsShowFirstOpen:true,
  20. IsBuilding:false,
  21. IsBuildError:false,
  22. IsShowLightColor:false,
  23. IsShowKeyword:true,//显示关键词
  24. IsShowQuestion:false,
  25. IsShowTranslate:false,
  26. IsShowSetting:false,
  27. IsShowRemind:false,
  28. remindAnimation: "", // 翻译弹窗动画类
  29. CurrentQuestionIndex:0,
  30. swiperHeight: "526rpx",
  31. lastTapTime: 0, // 记录上一次点击的时间,用于检测双击
  32. CountDown:30,//倒计时秒数
  33. },
  34. onLoad: function (options) {
  35. let that = this;
  36. let words=app.globalData.SelectedWords.join(",");
  37. let wordsStr=app.globalData.SelectedWords.join(" ");
  38. const hiddenhelp=wx.getStorageSync('HiddenArticleFirstOpen');
  39. let IsShowKeyword=wx.getStorageSync('IsShowKeyword');
  40. if (IsShowKeyword===""){
  41. IsShowKeyword=true;
  42. wx.setStorageSync('IsShowKeyword', true);
  43. }
  44. let IsShowLightColor=wx.getStorageSync('IsShowLightColor');
  45. if (!IsShowLightColor){
  46. IsShowLightColor=false;
  47. }
  48. that.setData({
  49. Containnerheight: main.getWindowHeight(),
  50. Words:words,
  51. WordsStr:wordsStr,
  52. IsShowFirstOpen:!hiddenhelp,
  53. IsShowLightColor:IsShowLightColor,
  54. IsShowKeyword:IsShowKeyword,
  55. IsBuildError:false,
  56. Level:options.Level,
  57. ArticleStyle:options.ArticleStyle,
  58. IsBuilding:false,
  59. });
  60. if (options.ID)
  61. that.getArticleByID(options.ID);
  62. else
  63. that.init(options);
  64. that.setTheme();
  65. },
  66. getArticleByID:function(id){
  67. let that=this;
  68. main.getData('GetYJBDCArticleList?UserID=' + app.globalData.userInfo.UserID+'&ID='+id, function (data) {
  69. if (data) {
  70. data=data[0];
  71. that.setData({
  72. Words:data.Words,
  73. Level:data.Level,
  74. ArticleStyle:data.ArticleStyle,
  75. CreateTime:data.CreateTime,
  76. ID:id,
  77. });
  78. let content=data.JSONString;
  79. that.updateData(content);
  80. }
  81. });
  82. },
  83. init:function(options){
  84. let that=this;
  85. let interval=0;
  86. //小学初中文章生成时间短
  87. that.data.CountDown=30;
  88. //高中大学文章生成时间长
  89. if (that.data.Level>=2){
  90. that.setData({
  91. CountDown:60,
  92. });
  93. }
  94. interval = setInterval(function(){
  95. that.setData({
  96. CountDown:--that.data.CountDown,
  97. });
  98. if (that.data.CountDown<=0)
  99. clearInterval(interval);
  100. },1200);
  101. that.setData({
  102. IsBuilding:true
  103. });
  104. let words=app.globalData.SelectedWords.join(",");
  105. main.postData('GenerateArticle?UserID='+app.globalData.userInfo.UserID, {
  106. Words:words,
  107. Level:that.data.Level,
  108. ArticleStyle:that.data.ArticleStyle
  109. }, function (data) {
  110. if (data){
  111. if (data=="-1"){
  112. that.setData({
  113. IsBuildError:true,
  114. CreateTime:common.formatDateCHS(common.formatTime(new Date()),true),
  115. });
  116. }
  117. else{
  118. let content=data;
  119. that.updateData(content);
  120. that.setData({
  121. IsBuilding:false,
  122. });
  123. }
  124. clearInterval(interval);
  125. }
  126. });
  127. },
  128. updateData:function(content){
  129. let that=this;
  130. //console.log(content);
  131. if (typeof content === 'string') {
  132. content = JSON.parse(content);
  133. }
  134. if (typeof content === 'string') {
  135. content = JSON.parse(content);
  136. }
  137. let hl="nonelight";
  138. if (that.data.IsShowKeyword)
  139. hl="highlight";
  140. // 简化的解决方案:按长度排序并使用临时标记
  141. for(let i=0;i<content.ArticleEnglish.length;i++){
  142. // 1. 先对单词按长度从长到短排序,确保先处理"Come on"再处理"Come"
  143. let sortedWords = [...content.FormsOfWords].sort((a, b) => b.length - a.length);
  144. // 2. 使用临时标记替换匹配的单词,避免HTML解析问题
  145. let text = content.ArticleEnglish[i];
  146. let placeholders = [];
  147. for(let j=0;j<sortedWords.length;j++){
  148. let word = sortedWords[j];
  149. let regex = new RegExp(`\\b${word}\\b[.,!?;:]?`, 'gi');
  150. text = text.replace(regex, match => {
  151. let punctuation = match.match(/[.,!?;:]$/);
  152. let punc = punctuation ? punctuation[0] : '';
  153. let wordPart = match.replace(/[.,!?;:]$/, '');
  154. // 使用唯一的占位符
  155. let placeholder = `__PLACEHOLDER_${placeholders.length}__`;
  156. placeholders.push(`<span class='${hl}'>${wordPart}</span>${punc}`);
  157. return placeholder;
  158. });
  159. }
  160. // 3. 将临时标记替换为实际的HTML标签
  161. for(let j=0; j<placeholders.length; j++){
  162. text = text.replace(`__PLACEHOLDER_${j}__`, placeholders[j]);
  163. }
  164. content.ArticleEnglish[i] = text;
  165. }
  166. content.ArticleEnglishArr=[];
  167. for(let i=0;i<content.ArticleEnglish.length;i++){
  168. let obj=common.splitByMultipleDelimiters(content.ArticleEnglish[i],["<span class='"+hl+"'>","</span>"]);
  169. obj=common.removeDuplicateAndTrimStrings(obj);
  170. //debugger;
  171. let arr=[];
  172. for(let j=0;j<obj.length;j++){
  173. let objChild={};
  174. objChild.Content=obj[j]+" ";
  175. objChild.CSS="";
  176. if (j%2==1){
  177. objChild.CSS=hl;
  178. objChild.Content=" "+objChild.Content;
  179. }
  180. arr.push(objChild);
  181. }
  182. content.ArticleEnglishArr.push(arr);
  183. }
  184. for(let i=0;i<content.Question.length;i++){
  185. for(let j=0;j<content.Question[i].OptionsEnglish.length;j++){
  186. let str=content.Question[i].OptionsChinese[j];
  187. content.Question[i].OptionsChinese[j]=str.substr(2);
  188. }
  189. let char = content.Question[i].Answer;
  190. let asciiCode = char.charCodeAt(0);
  191. content.Question[i].AnswerNumber=asciiCode-65;
  192. content.Question[i].IsShowAnswer=false;
  193. }
  194. that.setData({
  195. Content:content,
  196. });
  197. },
  198. setTheme:function(){
  199. let that=this;
  200. const css=Theme[that.data.IsShowLightColor?1:0];
  201. wx.setNavigationBarColor({
  202. frontColor: css.frontColor,
  203. backgroundColor: css.backgroundColor,
  204. });
  205. wx.setBackgroundColor({
  206. backgroundColor: css.backgroundColor,
  207. backgroundColorTop:css.backgroundColor,
  208. backgroundColorBottom:css.backgroundColor,
  209. });
  210. that.setData({
  211. ThemeCSS:css.Name,
  212. });
  213. wx.setStorageSync('IsShowLightColor', that.data.IsShowLightColor);
  214. },
  215. closeHelp:function(){
  216. this.setData({
  217. IsShowFirstOpen:false,
  218. });
  219. wx.setStorageSync('HiddenArticleFirstOpen', true);
  220. },
  221. selectedAnswer:function(e){
  222. let that=this;
  223. const question=e.currentTarget.dataset.question;
  224. const index=e.currentTarget.dataset.index;
  225. if (that.data.Content.Question[question].UserAnswer==index)
  226. that.data.Content.Question[question].UserAnswer=-1;
  227. else
  228. that.data.Content.Question[question].UserAnswer=index;
  229. that.setData({
  230. Content:that.data.Content,
  231. });
  232. },
  233. showData:function(e){
  234. let that=this;
  235. let name=e.currentTarget.dataset.name;
  236. this.data[name] = !this.data[name];
  237. this.setData(this.data);
  238. if (name=="IsShowKeyword"){
  239. that.setShowKeyword();
  240. }
  241. else if (name=="IsShowLightColor"){
  242. that.setTheme();
  243. }
  244. else if (name=="IsShowAnswer"){
  245. let index=e.currentTarget.dataset.index;
  246. let content=that.data.Content;
  247. content.Question[index].IsShowAnswer=!content.Question[index].IsShowAnswer;
  248. that.setData({
  249. Content:content,
  250. });
  251. }
  252. },
  253. setShowKeyword:function(){
  254. let that=this;
  255. let content=that.data.Content;
  256. let source="highlight",target="nonelight";
  257. if (that.data["IsShowKeyword"]){
  258. source="nonelight";
  259. target="highlight";
  260. }
  261. for(let i=0;i<content.ArticleEnglishArr.length;i++){
  262. let obj=content.ArticleEnglishArr[i];
  263. let arr=[];
  264. for(let j=0;j<obj.length;j++){
  265. if (obj[j].CSS==source)
  266. obj[j].CSS=target;
  267. }
  268. }
  269. for(let i=0;i<content.ArticleEnglish.length;i++){
  270. content.ArticleEnglish[i]=common.ReplaceAllString(content.ArticleEnglish[i],source,target);
  271. }
  272. that.setData({
  273. Content:content,
  274. });
  275. wx.setStorageSync('IsShowKeyword', that.data.IsShowKeyword);
  276. },
  277. nextQuestion:function(e){
  278. if (this.data.CurrentQuestionIndex+1<this.data.Content.Question.length){
  279. this.data.CurrentQuestionIndex=this.data.CurrentQuestionIndex+1;
  280. this.setData({
  281. CurrentQuestionIndex:this.data.CurrentQuestionIndex,
  282. });
  283. }
  284. },
  285. updateQuestionIndex:function(e){
  286. this.setData({
  287. CurrentQuestionIndex:e.detail.current,
  288. });
  289. console.log(e.detail.current);
  290. },
  291. onContainerTap: function() {
  292. const currentTime = new Date().getTime();
  293. const lastTapTime = this.data.lastTapTime;
  294. const timeDiff = currentTime - lastTapTime;
  295. // 如果两次点击的时间间隔小于300毫秒,则认为是双击
  296. if (timeDiff < 300 && timeDiff > 0) {
  297. console.log('双击事件触发');
  298. // 在这里添加双击事件的处理逻辑
  299. // 例如:切换翻译显示状态
  300. this.showData({currentTarget:{dataset:{name:"IsShowTranslate"}}});
  301. }
  302. // 更新上一次点击的时间
  303. this.setData({
  304. lastTapTime: currentTime
  305. });
  306. },
  307. //生成PDF文件
  308. //访问服务器的GeneratePDF接口,提交this.data.Content数据,获得一个生成好的pdf文件,服务端的代码已经生成好
  309. generatePDF: function(e) {
  310. let that = this;
  311. //debugger;
  312. that.data.Content.Words=that.data.Words;
  313. that.data.Content.Level=that.data.Level;
  314. that.data.Content.ArticleStyle=that.data.ArticleStyle;
  315. that.data.Content.CreateTime=that.data.CreateTime;
  316. let url = common.Encrypt("GeneratePDF");
  317. url =app.globalData.serverUrl + url;
  318. wx.request({
  319. url: url,
  320. method: "POST",
  321. data: {
  322. Content: that.data.Content,
  323. },
  324. responseType: 'arraybuffer', // 确保响应类型为arraybuffer
  325. success: function(res) {
  326. // 将arraybuffer转为临时文件
  327. const fsm = wx.getFileSystemManager();
  328. const tempFilePath = `${wx.env.USER_DATA_PATH}/temp_${Date.now()}.pdf`;
  329. try {
  330. fsm.writeFileSync(
  331. tempFilePath,
  332. res.data,
  333. 'binary'
  334. );
  335. // 直接使用临时文件路径,不再尝试永久保存
  336. console.log('文件已生成:', tempFilePath);
  337. // 打开PDF文件预览
  338. wx.openDocument({
  339. filePath: tempFilePath,
  340. fileType: 'pdf',
  341. showMenu: true, // 显示右上角菜单,可以分享
  342. success: function() {
  343. },
  344. fail: function(error) {
  345. console.error('打开文档失败:', error);
  346. wx.showToast({
  347. title: '打开文件失败',
  348. icon: 'none'
  349. });
  350. }
  351. });
  352. } catch (error) {
  353. console.error('写入文件失败:', error);
  354. wx.showToast({
  355. title: '写入文件失败',
  356. icon: 'none'
  357. });
  358. }
  359. },
  360. fail: function(err) {
  361. console.error('请求GeneratePDF接口失败:', err);
  362. wx.showToast({
  363. title: '网络错误,请稍候重试',
  364. icon: 'none'
  365. });
  366. }
  367. });
  368. },
  369. // 处理导航栏返回按钮点击事件
  370. back: function() {
  371. if (this.data.IsBuilding) {
  372. return; // 如果正在生成文章,不执行返回操作
  373. }
  374. if (!this.data.ID){
  375. wx.navigateBack({
  376. delta: 1,
  377. });
  378. }
  379. },
  380. onLongPress: function(e) {
  381. let that = this;
  382. let strType=e.currentTarget.dataset.strtype;
  383. let selectedIndex=e.currentTarget.dataset.index;
  384. let selectedIndex2=e.currentTarget.dataset.index2;
  385. let engSentence,chnSentence;
  386. if (strType=="article"){
  387. //console.log("选中的句子索引:", selectedIndex);
  388. //console.log("选中的句子:", that.data.Content.ArticleEnglish[selectedIndex]);
  389. engSentence=that.data.Content.ArticleEnglish[selectedIndex];
  390. engSentence=common.ReplaceAllString(engSentence,"<span class='highlight'>","");
  391. engSentence=common.ReplaceAllString(engSentence,"<span class='nonelight'>","");
  392. engSentence=common.ReplaceAllString(engSentence,"</span>","");
  393. chnSentence=that.data.Content.ArticleChinese[selectedIndex];
  394. }
  395. else if (strType=="question"){
  396. engSentence=that.data.Content.Question[selectedIndex].QuestionEnglish;
  397. chnSentence=that.data.Content.Question[selectedIndex].QuestionChinese;
  398. }
  399. else if (strType=="option"){
  400. engSentence=that.data.Content.Question[selectedIndex].OptionsEnglish[selectedIndex2];
  401. chnSentence=that.data.Content.Question[selectedIndex].OptionsChinese[selectedIndex2];
  402. }
  403. this.setData({
  404. EnglishSentence:engSentence,
  405. ChineseSentence:chnSentence,
  406. remindAnimation: "remind-slide-up", // 添加上滑动画
  407. IsShowRemind:true,
  408. });
  409. },
  410. showRemind:function(e){
  411. let that = this;
  412. if (that.data.IsShowRemind) {
  413. // 如果当前是显示状态,先播放下滑动画,然后再隐藏
  414. that.setData({
  415. remindAnimation: "remind-slide-down" // 添加下滑动画
  416. });
  417. // 等待动画完成后再隐藏弹窗
  418. setTimeout(function() {
  419. that.setData({
  420. IsShowRemind: false
  421. });
  422. }, 300); // 动画持续时间为0.3秒
  423. } else {
  424. // 如果当前是隐藏状态,直接显示并播放上滑动画
  425. that.setData({
  426. IsShowRemind: true,
  427. remindAnimation: "remind-slide-up"
  428. });
  429. }
  430. },
  431. onShareAppMessage: function () {
  432. return {
  433. title: app.globalData.ShareTitle,
  434. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  435. imageUrl: app.globalData.ShareImage,
  436. }
  437. },
  438. })