article.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. CurrentQuestionIndex:0,
  28. swiperHeight: "526rpx",
  29. lastTapTime: 0, // 记录上一次点击的时间,用于检测双击
  30. CountDown:30,//倒计时秒数
  31. },
  32. onLoad: function (options) {
  33. let that = this;
  34. let words=app.globalData.SelectedWords.join(",");
  35. let wordsStr=app.globalData.SelectedWords.join(" ");
  36. const hiddenhelp=wx.getStorageSync('HiddenArticleFirstOpen');
  37. let IsShowKeyword=wx.getStorageSync('IsShowKeyword');
  38. if (IsShowKeyword===""){
  39. IsShowKeyword=true;
  40. wx.setStorageSync('IsShowKeyword', true);
  41. }
  42. let IsShowLightColor=wx.getStorageSync('IsShowLightColor');
  43. if (!IsShowLightColor){
  44. IsShowLightColor=false;
  45. }
  46. that.setData({
  47. Containnerheight: main.getWindowHeight(),
  48. Words:words,
  49. WordsStr:wordsStr,
  50. IsShowFirstOpen:!hiddenhelp,
  51. IsShowLightColor:IsShowLightColor,
  52. IsShowKeyword:IsShowKeyword,
  53. IsBuildError:false,
  54. });
  55. if (options.ID)
  56. that.getArticleByID(options.ID);
  57. else
  58. that.init(options);
  59. that.setTheme();
  60. },
  61. getArticleByID:function(id){
  62. let that=this;
  63. main.getData('GetYJBDCArticleList?UserID=' + app.globalData.userInfo.UserID+'&ID='+id, function (data) {
  64. if (data) {
  65. data=data[0];
  66. that.setData({
  67. Words:data.Words,
  68. ID:id,
  69. });
  70. let content=data.JSONString;
  71. that.updateData(content);
  72. }
  73. });
  74. },
  75. init:function(options){
  76. let that=this;
  77. let interval=0;
  78. that.data.CountDown=30;
  79. interval = setInterval(function(){
  80. that.setData({
  81. CountDown:--that.data.CountDown,
  82. });
  83. if (that.data.CountDown<=0)
  84. clearInterval(interval);
  85. },1200);
  86. that.setData({
  87. IsBuilding:true
  88. });
  89. let words=app.globalData.SelectedWords.join(",");
  90. main.postData('GenerateArticle?UserID='+app.globalData.userInfo.UserID, {
  91. Words:words,
  92. Level:options.Level,
  93. ArticleStyle:options.ArticleStyle
  94. }, function (data) {
  95. if (data){
  96. if (data=="-1"){
  97. that.setData({
  98. IsBuildError:true,
  99. });
  100. }
  101. else{
  102. let content=data;
  103. that.updateData(content);
  104. that.setData({
  105. IsBuilding:false,
  106. });
  107. }
  108. clearInterval(interval);
  109. }
  110. });
  111. },
  112. updateData:function(content){
  113. let that=this;
  114. //console.log(content);
  115. if (typeof content === 'string') {
  116. content = JSON.parse(content);
  117. }
  118. if (typeof content === 'string') {
  119. content = JSON.parse(content);
  120. }
  121. let hl="nonelight";
  122. if (that.data.IsShowKeyword)
  123. hl="highlight";
  124. for(let i=0;i<content.ArticleEnglish.length;i++){
  125. for(let j=0;j<content.FormsOfWords.length;j++){
  126. let word = content.FormsOfWords[j];
  127. let regex = new RegExp(`\\b${word}\\b[.,!?;:]?`, 'gi');
  128. content.ArticleEnglish[i] = content.ArticleEnglish[i].replace(regex, match => {
  129. let punctuation = match.match(/[.,!?;:]$/);
  130. let punc = punctuation ? punctuation[0] : '';
  131. let wordPart = match.replace(/[.,!?;:]$/, '');
  132. return `<span class='`+hl+`'>${wordPart}</span>${punc}`;
  133. });
  134. }
  135. }
  136. content.ArticleEnglishArr=[];
  137. for(let i=0;i<content.ArticleEnglish.length;i++){
  138. let obj=common.splitByMultipleDelimiters(content.ArticleEnglish[i],["<span class='"+hl+"'>","</span>"]);
  139. obj=common.removeDuplicateAndTrimStrings(obj);
  140. //debugger;
  141. let arr=[];
  142. for(let j=0;j<obj.length;j++){
  143. let objChild={};
  144. objChild.Content=obj[j]+" ";
  145. objChild.CSS="";
  146. if (j%2==1){
  147. objChild.CSS=hl;
  148. objChild.Content=" "+objChild.Content;
  149. }
  150. arr.push(objChild);
  151. }
  152. content.ArticleEnglishArr.push(arr);
  153. }
  154. for(let i=0;i<content.Question.length;i++){
  155. for(let j=0;j<content.Question[i].OptionsEnglish.length;j++){
  156. let str=content.Question[i].OptionsChinese[j];
  157. content.Question[i].OptionsChinese[j]=str.substr(2);
  158. }
  159. let char = content.Question[i].Answer;
  160. let asciiCode = char.charCodeAt(0);
  161. content.Question[i].AnswerNumber=asciiCode-65;
  162. content.Question[i].IsShowAnswer=false;
  163. }
  164. that.setData({
  165. Content:content,
  166. });
  167. },
  168. setTheme:function(){
  169. let that=this;
  170. const css=Theme[that.data.IsShowLightColor?1:0];
  171. wx.setNavigationBarColor({
  172. frontColor: css.frontColor,
  173. backgroundColor: css.backgroundColor,
  174. });
  175. wx.setBackgroundColor({
  176. backgroundColor: css.backgroundColor,
  177. backgroundColorTop:css.backgroundColor,
  178. backgroundColorBottom:css.backgroundColor,
  179. });
  180. that.setData({
  181. ThemeCSS:css.Name,
  182. });
  183. wx.setStorageSync('IsShowLightColor', that.data.IsShowLightColor);
  184. },
  185. closeHelp:function(){
  186. this.setData({
  187. IsShowFirstOpen:false,
  188. });
  189. wx.setStorageSync('HiddenArticleFirstOpen', true);
  190. },
  191. selectedAnswer:function(e){
  192. let that=this;
  193. const question=e.currentTarget.dataset.question;
  194. const index=e.currentTarget.dataset.index;
  195. that.data.Content.Question[question].UserAnswer=index;
  196. that.setData({
  197. Content:that.data.Content,
  198. });
  199. },
  200. showData:function(e){
  201. let that=this;
  202. let name=e.currentTarget.dataset.name;
  203. this.data[name] = !this.data[name];
  204. this.setData(this.data);
  205. if (name=="IsShowKeyword"){
  206. that.setShowKeyword();
  207. }
  208. else if (name=="IsShowLightColor"){
  209. that.setTheme();
  210. }
  211. else if (name=="IsShowAnswer"){
  212. let index=e.currentTarget.dataset.index;
  213. let content=that.data.Content;
  214. content.Question[index].IsShowAnswer=!content.Question[index].IsShowAnswer;
  215. that.setData({
  216. Content:content,
  217. });
  218. }
  219. },
  220. setShowKeyword:function(){
  221. let that=this;
  222. let content=that.data.Content;
  223. let source="highlight",target="nonelight";
  224. if (that.data["IsShowKeyword"]){
  225. source="nonelight";
  226. target="highlight";
  227. }
  228. for(let i=0;i<content.ArticleEnglishArr.length;i++){
  229. let obj=content.ArticleEnglishArr[i];
  230. let arr=[];
  231. for(let j=0;j<obj.length;j++){
  232. if (obj[j].CSS==source)
  233. obj[j].CSS=target;
  234. }
  235. }
  236. for(let i=0;i<content.ArticleEnglish.length;i++){
  237. content.ArticleEnglish[i]=common.ReplaceAllString(content.ArticleEnglish[i],source,target);
  238. }
  239. that.setData({
  240. Content:content,
  241. });
  242. wx.setStorageSync('IsShowKeyword', that.data.IsShowKeyword);
  243. },
  244. nextQuestion:function(e){
  245. if (this.data.CurrentQuestionIndex+1<this.data.Content.Question.length){
  246. this.data.CurrentQuestionIndex=this.data.CurrentQuestionIndex+1;
  247. this.setData({
  248. CurrentQuestionIndex:this.data.CurrentQuestionIndex,
  249. });
  250. }
  251. },
  252. updateQuestionIndex:function(e){
  253. this.setData({
  254. CurrentQuestionIndex:e.detail.current,
  255. });
  256. console.log(e.detail.current);
  257. },
  258. onContainerTap: function() {
  259. const currentTime = new Date().getTime();
  260. const lastTapTime = this.data.lastTapTime;
  261. const timeDiff = currentTime - lastTapTime;
  262. // 如果两次点击的时间间隔小于300毫秒,则认为是双击
  263. if (timeDiff < 300 && timeDiff > 0) {
  264. console.log('双击事件触发');
  265. // 在这里添加双击事件的处理逻辑
  266. // 例如:切换翻译显示状态
  267. this.showData({currentTarget:{dataset:{name:"IsShowTranslate"}}});
  268. }
  269. // 更新上一次点击的时间
  270. this.setData({
  271. lastTapTime: currentTime
  272. });
  273. },
  274. //生成PDF文件
  275. //访问服务器的GeneratePDF接口,提交this.data.Content数据,获得一个生成好的pdf文件,服务端的代码已经生成好
  276. generatePDF: function(e) {
  277. let that = this;
  278. //debugger;
  279. that.data.Content.Words=that.data.Words;
  280. let url = common.Encrypt("GeneratePDF");
  281. url =app.globalData.serverUrl + url;
  282. wx.request({
  283. url: url,
  284. method: "POST",
  285. data: {
  286. Content: that.data.Content,
  287. },
  288. responseType: 'arraybuffer', // 确保响应类型为arraybuffer
  289. success: function(res) {
  290. // 将arraybuffer转为临时文件
  291. const fsm = wx.getFileSystemManager();
  292. const tempFilePath = `${wx.env.USER_DATA_PATH}/temp_${Date.now()}.pdf`;
  293. try {
  294. fsm.writeFileSync(
  295. tempFilePath,
  296. res.data,
  297. 'binary'
  298. );
  299. // 直接使用临时文件路径,不再尝试永久保存
  300. console.log('文件已生成:', tempFilePath);
  301. // 打开PDF文件预览
  302. wx.openDocument({
  303. filePath: tempFilePath,
  304. fileType: 'pdf',
  305. showMenu: true, // 显示右上角菜单,可以分享
  306. success: function() {
  307. },
  308. fail: function(error) {
  309. console.error('打开文档失败:', error);
  310. wx.showToast({
  311. title: '打开文件失败',
  312. icon: 'none'
  313. });
  314. }
  315. });
  316. } catch (error) {
  317. console.error('写入文件失败:', error);
  318. wx.showToast({
  319. title: '写入文件失败',
  320. icon: 'none'
  321. });
  322. }
  323. },
  324. fail: function(err) {
  325. console.error('请求GeneratePDF接口失败:', err);
  326. wx.showToast({
  327. title: '网络错误,请稍候重试',
  328. icon: 'none'
  329. });
  330. }
  331. });
  332. },
  333. // 处理导航栏返回按钮点击事件
  334. back: function() {
  335. if (this.data.IsBuilding) {
  336. return; // 如果正在生成文章,不执行返回操作
  337. }
  338. if (!this.data.ID){
  339. wx.navigateBack({
  340. delta: 2,
  341. });
  342. } else {
  343. wx.navigateBack();
  344. }
  345. },
  346. onLongPress: function(e) {
  347. let that = this;
  348. let strType=e.currentTarget.dataset.strtype;
  349. let selectedIndex=e.currentTarget.dataset.index;
  350. let selectedIndex2=e.currentTarget.dataset.index2;
  351. let content;
  352. if (strType=="article"){
  353. //console.log("选中的句子索引:", selectedIndex);
  354. //console.log("选中的句子:", that.data.Content.ArticleEnglish[selectedIndex]);
  355. content=that.data.Content.ArticleEnglish[selectedIndex];
  356. content=common.ReplaceAllString(content,"<span class='highlight'>","");
  357. content=common.ReplaceAllString(content,"<span class='nonelight'>","");
  358. content=common.ReplaceAllString(content,"</span>","");
  359. content+="\r\n"+that.data.Content.ArticleChinese[selectedIndex];
  360. }
  361. else if (strType=="question"){
  362. content=that.data.Content.Question[selectedIndex].QuestionEnglish;
  363. content+="\r\n"+that.data.Content.Question[selectedIndex].QuestionChinese;
  364. }
  365. else if (strType=="option"){
  366. content=that.data.Content.Question[selectedIndex].OptionsEnglish[selectedIndex2];
  367. content+="\r\n"+that.data.Content.Question[selectedIndex].OptionsChinese[selectedIndex2];
  368. }
  369. wx.showModal({
  370. title: '翻译',
  371. content: content,
  372. showCancel:false,
  373. confirmText:"知道了",
  374. complete: (res) => {
  375. if (res.confirm) {
  376. }
  377. }
  378. })
  379. },
  380. onUnload:function(){
  381. if (!this.data.ID){
  382. wx.navigateBack({
  383. delta: 2,
  384. });
  385. }
  386. },
  387. onShareAppMessage: function () {
  388. return {
  389. title: app.globalData.ShareTitle,
  390. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  391. imageUrl: app.globalData.ShareImage,
  392. }
  393. },
  394. })