article.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. for(let i=0;i<content.ArticleEnglish.length;i++){
  141. for(let j=0;j<content.FormsOfWords.length;j++){
  142. let word = content.FormsOfWords[j];
  143. let regex = new RegExp(`\\b${word}\\b[.,!?;:]?`, 'gi');
  144. content.ArticleEnglish[i] = content.ArticleEnglish[i].replace(regex, match => {
  145. let punctuation = match.match(/[.,!?;:]$/);
  146. let punc = punctuation ? punctuation[0] : '';
  147. let wordPart = match.replace(/[.,!?;:]$/, '');
  148. return `<span class='`+hl+`'>${wordPart}</span>${punc}`;
  149. });
  150. }
  151. }
  152. content.ArticleEnglishArr=[];
  153. //debugger;
  154. for(let i=0;i<content.ArticleEnglish.length;i++){
  155. let obj=common.splitByMultipleDelimiters(content.ArticleEnglish[i],["<span class='"+hl+"'>","</span>"]);
  156. obj=common.removeDuplicateAndTrimStrings(obj);
  157. //debugger;
  158. let arr=[];
  159. for(let j=0;j<obj.length;j++){
  160. let objChild={};
  161. objChild.Content=obj[j]+" ";
  162. objChild.CSS="";
  163. if (j%2==1){
  164. objChild.CSS=hl;
  165. objChild.Content=" "+objChild.Content;
  166. }
  167. arr.push(objChild);
  168. }
  169. content.ArticleEnglishArr.push(arr);
  170. }
  171. for(let i=0;i<content.Question.length;i++){
  172. for(let j=0;j<content.Question[i].OptionsEnglish.length;j++){
  173. let str=content.Question[i].OptionsChinese[j];
  174. content.Question[i].OptionsChinese[j]=str.substr(2);
  175. }
  176. let char = content.Question[i].Answer;
  177. let asciiCode = char.charCodeAt(0);
  178. content.Question[i].AnswerNumber=asciiCode-65;
  179. content.Question[i].IsShowAnswer=false;
  180. }
  181. that.setData({
  182. Content:content,
  183. });
  184. },
  185. setTheme:function(){
  186. let that=this;
  187. const css=Theme[that.data.IsShowLightColor?1:0];
  188. wx.setNavigationBarColor({
  189. frontColor: css.frontColor,
  190. backgroundColor: css.backgroundColor,
  191. });
  192. wx.setBackgroundColor({
  193. backgroundColor: css.backgroundColor,
  194. backgroundColorTop:css.backgroundColor,
  195. backgroundColorBottom:css.backgroundColor,
  196. });
  197. that.setData({
  198. ThemeCSS:css.Name,
  199. });
  200. wx.setStorageSync('IsShowLightColor', that.data.IsShowLightColor);
  201. },
  202. closeHelp:function(){
  203. this.setData({
  204. IsShowFirstOpen:false,
  205. });
  206. wx.setStorageSync('HiddenArticleFirstOpen', true);
  207. },
  208. selectedAnswer:function(e){
  209. let that=this;
  210. const question=e.currentTarget.dataset.question;
  211. const index=e.currentTarget.dataset.index;
  212. if (that.data.Content.Question[question].UserAnswer==index)
  213. that.data.Content.Question[question].UserAnswer=-1;
  214. else
  215. that.data.Content.Question[question].UserAnswer=index;
  216. that.setData({
  217. Content:that.data.Content,
  218. });
  219. },
  220. showData:function(e){
  221. let that=this;
  222. let name=e.currentTarget.dataset.name;
  223. this.data[name] = !this.data[name];
  224. this.setData(this.data);
  225. if (name=="IsShowKeyword"){
  226. that.setShowKeyword();
  227. }
  228. else if (name=="IsShowLightColor"){
  229. that.setTheme();
  230. }
  231. else if (name=="IsShowAnswer"){
  232. let index=e.currentTarget.dataset.index;
  233. let content=that.data.Content;
  234. content.Question[index].IsShowAnswer=!content.Question[index].IsShowAnswer;
  235. that.setData({
  236. Content:content,
  237. });
  238. }
  239. },
  240. setShowKeyword:function(){
  241. let that=this;
  242. let content=that.data.Content;
  243. let source="highlight",target="nonelight";
  244. if (that.data["IsShowKeyword"]){
  245. source="nonelight";
  246. target="highlight";
  247. }
  248. for(let i=0;i<content.ArticleEnglishArr.length;i++){
  249. let obj=content.ArticleEnglishArr[i];
  250. let arr=[];
  251. for(let j=0;j<obj.length;j++){
  252. if (obj[j].CSS==source)
  253. obj[j].CSS=target;
  254. }
  255. }
  256. for(let i=0;i<content.ArticleEnglish.length;i++){
  257. content.ArticleEnglish[i]=common.ReplaceAllString(content.ArticleEnglish[i],source,target);
  258. }
  259. that.setData({
  260. Content:content,
  261. });
  262. wx.setStorageSync('IsShowKeyword', that.data.IsShowKeyword);
  263. },
  264. nextQuestion:function(e){
  265. if (this.data.CurrentQuestionIndex+1<this.data.Content.Question.length){
  266. this.data.CurrentQuestionIndex=this.data.CurrentQuestionIndex+1;
  267. this.setData({
  268. CurrentQuestionIndex:this.data.CurrentQuestionIndex,
  269. });
  270. }
  271. },
  272. updateQuestionIndex:function(e){
  273. this.setData({
  274. CurrentQuestionIndex:e.detail.current,
  275. });
  276. console.log(e.detail.current);
  277. },
  278. onContainerTap: function() {
  279. const currentTime = new Date().getTime();
  280. const lastTapTime = this.data.lastTapTime;
  281. const timeDiff = currentTime - lastTapTime;
  282. // 如果两次点击的时间间隔小于300毫秒,则认为是双击
  283. if (timeDiff < 300 && timeDiff > 0) {
  284. console.log('双击事件触发');
  285. // 在这里添加双击事件的处理逻辑
  286. // 例如:切换翻译显示状态
  287. this.showData({currentTarget:{dataset:{name:"IsShowTranslate"}}});
  288. }
  289. // 更新上一次点击的时间
  290. this.setData({
  291. lastTapTime: currentTime
  292. });
  293. },
  294. //生成PDF文件
  295. //访问服务器的GeneratePDF接口,提交this.data.Content数据,获得一个生成好的pdf文件,服务端的代码已经生成好
  296. generatePDF: function(e) {
  297. let that = this;
  298. //debugger;
  299. that.data.Content.Words=that.data.Words;
  300. that.data.Content.Level=that.data.Level;
  301. that.data.Content.ArticleStyle=that.data.ArticleStyle;
  302. that.data.Content.CreateTime=that.data.CreateTime;
  303. let url = common.Encrypt("GeneratePDF");
  304. url =app.globalData.serverUrl + url;
  305. wx.request({
  306. url: url,
  307. method: "POST",
  308. data: {
  309. Content: that.data.Content,
  310. },
  311. responseType: 'arraybuffer', // 确保响应类型为arraybuffer
  312. success: function(res) {
  313. // 将arraybuffer转为临时文件
  314. const fsm = wx.getFileSystemManager();
  315. const tempFilePath = `${wx.env.USER_DATA_PATH}/temp_${Date.now()}.pdf`;
  316. try {
  317. fsm.writeFileSync(
  318. tempFilePath,
  319. res.data,
  320. 'binary'
  321. );
  322. // 直接使用临时文件路径,不再尝试永久保存
  323. console.log('文件已生成:', tempFilePath);
  324. // 打开PDF文件预览
  325. wx.openDocument({
  326. filePath: tempFilePath,
  327. fileType: 'pdf',
  328. showMenu: true, // 显示右上角菜单,可以分享
  329. success: function() {
  330. },
  331. fail: function(error) {
  332. console.error('打开文档失败:', error);
  333. wx.showToast({
  334. title: '打开文件失败',
  335. icon: 'none'
  336. });
  337. }
  338. });
  339. } catch (error) {
  340. console.error('写入文件失败:', error);
  341. wx.showToast({
  342. title: '写入文件失败',
  343. icon: 'none'
  344. });
  345. }
  346. },
  347. fail: function(err) {
  348. console.error('请求GeneratePDF接口失败:', err);
  349. wx.showToast({
  350. title: '网络错误,请稍候重试',
  351. icon: 'none'
  352. });
  353. }
  354. });
  355. },
  356. // 处理导航栏返回按钮点击事件
  357. back: function() {
  358. if (this.data.IsBuilding) {
  359. return; // 如果正在生成文章,不执行返回操作
  360. }
  361. if (!this.data.ID){
  362. wx.navigateBack({
  363. delta: 1,
  364. });
  365. }
  366. },
  367. onLongPress: function(e) {
  368. let that = this;
  369. let strType=e.currentTarget.dataset.strtype;
  370. let selectedIndex=e.currentTarget.dataset.index;
  371. let selectedIndex2=e.currentTarget.dataset.index2;
  372. let engSentence,chnSentence;
  373. if (strType=="article"){
  374. //console.log("选中的句子索引:", selectedIndex);
  375. //console.log("选中的句子:", that.data.Content.ArticleEnglish[selectedIndex]);
  376. engSentence=that.data.Content.ArticleEnglish[selectedIndex];
  377. engSentence=common.ReplaceAllString(engSentence,"<span class='highlight'>","");
  378. engSentence=common.ReplaceAllString(engSentence,"<span class='nonelight'>","");
  379. engSentence=common.ReplaceAllString(engSentence,"</span>","");
  380. chnSentence=that.data.Content.ArticleChinese[selectedIndex];
  381. }
  382. else if (strType=="question"){
  383. engSentence=that.data.Content.Question[selectedIndex].QuestionEnglish;
  384. chnSentence=that.data.Content.Question[selectedIndex].QuestionChinese;
  385. }
  386. else if (strType=="option"){
  387. engSentence=that.data.Content.Question[selectedIndex].OptionsEnglish[selectedIndex2];
  388. chnSentence=that.data.Content.Question[selectedIndex].OptionsChinese[selectedIndex2];
  389. }
  390. this.setData({
  391. EnglishSentence:engSentence,
  392. ChineseSentence:chnSentence,
  393. remindAnimation: "remind-slide-up", // 添加上滑动画
  394. IsShowRemind:true,
  395. });
  396. },
  397. showRemind:function(e){
  398. let that = this;
  399. if (that.data.IsShowRemind) {
  400. // 如果当前是显示状态,先播放下滑动画,然后再隐藏
  401. that.setData({
  402. remindAnimation: "remind-slide-down" // 添加下滑动画
  403. });
  404. // 等待动画完成后再隐藏弹窗
  405. setTimeout(function() {
  406. that.setData({
  407. IsShowRemind: false
  408. });
  409. }, 300); // 动画持续时间为0.3秒
  410. } else {
  411. // 如果当前是隐藏状态,直接显示并播放上滑动画
  412. that.setData({
  413. IsShowRemind: true,
  414. remindAnimation: "remind-slide-up"
  415. });
  416. }
  417. },
  418. onShareAppMessage: function () {
  419. return {
  420. title: app.globalData.ShareTitle,
  421. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  422. imageUrl: app.globalData.ShareImage,
  423. }
  424. },
  425. })