article.js 9.9 KB

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