article.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. IsShowPanelHelp:true,
  20. IsBuilding:false,
  21. IsShowLightColor:false,
  22. IsShowKeyword:true,//显示关键词
  23. IsShowQuestion:true,
  24. IsShowTranslate:false,
  25. IsShowAnswer:false,
  26. IsShowSetting:false,
  27. CurrentQuestionIndex:0,
  28. swiperHeight: "526rpx",
  29. lastTapTime: 0, // 记录上一次点击的时间,用于检测双击
  30. },
  31. onLoad: function (options) {
  32. let that = this;
  33. let words=app.globalData.SelectedWords.join(" ");
  34. const hiddenhelp=wx.getStorageSync('HiddenArticleHelp');
  35. that.setData({
  36. Containnerheight: main.getWindowHeight(),
  37. Words:words,
  38. IsShowPanelHelp:!hiddenhelp,
  39. });
  40. that.init();
  41. that.setTheme();
  42. },
  43. init:function(){
  44. const that=this;
  45. let content={
  46. "ArticleEnglish": [
  47. "Once upon a time, there was a king in a beautiful kingdom.",
  48. "A fierce dragon came and brought yellow fire to the kingdom.",
  49. "The people in the kingdom were very afraid.",
  50. "A brave boy and a kind - hearted girl decided to go and fight against the dragon.",
  51. "They came to the dragon's cave and saw the dragon sleeping.",
  52. "The boy used a big stick to wake up the dragon.",
  53. "The dragon was angry and spat out yellow fire at them.",
  54. "But the boy and the girl were not scared.",
  55. "They worked together and finally defeated the dragon.",
  56. "The king was very happy and thanked the boy and the girl."
  57. ],
  58. "ArticleChinese": [
  59. "从前,在一个美丽的王国里有一位国王。",
  60. "一条凶猛的龙来了,给王国带来了黄色的火焰。",
  61. "王国里的人们非常害怕。",
  62. "一个勇敢的男孩和一个善良的女孩决定去与龙战斗。",
  63. "他们来到了龙的洞穴,看到龙正在睡觉。",
  64. "男孩用一根大棍子把龙叫醒了。",
  65. "龙很生气,朝他们吐出了黄色的火焰。",
  66. "但是男孩和女孩并不害怕。",
  67. "他们齐心协力,最终打败了龙。",
  68. "国王非常高兴,感谢了男孩和女孩。"
  69. ],
  70. "FormsOfWords": [
  71. "boy",
  72. "girl",
  73. "king",
  74. "come",
  75. "came",
  76. "go",
  77. "went",
  78. "yellow",
  79. "dragon",
  80. "dragons",
  81. "fire"
  82. ],
  83. "Question": [
  84. {
  85. "QuestionEnglish": "Who brought yellow fire to the kingdom?",
  86. "QuestionChinese": "谁给王国带来了黄色的火焰?",
  87. "OptionsEnglish": [
  88. "A. The boy",
  89. "B. The girl",
  90. "C. The king",
  91. //"D. The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon The dragon"
  92. "D. The dragon"
  93. ],
  94. "OptionsChinese": [
  95. "A. 男孩",
  96. "B. 女孩",
  97. "C. 国王",
  98. "D. 龙"
  99. ],
  100. "Answer": "D"
  101. },
  102. {
  103. "QuestionEnglish": "What did the boy and the girl decide to do?",
  104. "QuestionChinese": "男孩和女孩决定做什么?",
  105. "OptionsEnglish": [
  106. "A. Go home",
  107. "B. Fight against the dragon",
  108. "C. Play with the dragon",
  109. "D. Sleep in the cave"
  110. ],
  111. "OptionsChinese": [
  112. "A. 回家",
  113. "B. 与龙战斗",
  114. "C. 和龙一起玩",
  115. "D. 在洞穴里睡觉"
  116. ],
  117. "Answer": "B"
  118. },
  119. {
  120. "QuestionEnglish": "How did the boy wake up the dragon?",
  121. "QuestionChinese": "男孩是如何叫醒龙的?",
  122. "OptionsEnglish": [
  123. "A. By shouting",
  124. "B. By using a big stick",
  125. "C. By throwing stones",
  126. "D. By singing"
  127. ],
  128. "OptionsChinese": [
  129. "A. 通过大喊",
  130. "B. 通过使用一根大棍子",
  131. "C. 通过扔石头",
  132. "D. 通过唱歌"
  133. ],
  134. "Answer": "B"
  135. },
  136. {
  137. "QuestionEnglish": "How did the dragon feel when it woke up?",
  138. "QuestionChinese": "龙醒来时感觉如何?",
  139. "OptionsEnglish": [
  140. "A. Happy",
  141. "B. Sad",
  142. "C. Angry",
  143. "D. Tired"
  144. ],
  145. "OptionsChinese": [
  146. "A. 高兴的",
  147. "B. 悲伤的",
  148. "C. 生气的",
  149. "D. 疲惫的"
  150. ],
  151. "Answer": "C"
  152. },
  153. {
  154. "QuestionEnglish": "What did the king do after the boy and the girl defeated the dragon?",
  155. "QuestionChinese": "男孩和女孩打败龙后,国王做了什么?",
  156. "OptionsEnglish": [
  157. "A. He punished them",
  158. "B. He thanked them",
  159. "C. He ignored them",
  160. "D. He was angry with them"
  161. ],
  162. "OptionsChinese": [
  163. "A. 他惩罚了他们",
  164. "B. 他感谢了他们",
  165. "C. 他无视了他们",
  166. "D. 他对他们很生气"
  167. ],
  168. "Answer": "B"
  169. }
  170. ]
  171. };
  172. that.updateData(content);
  173. },
  174. setTheme:function(){
  175. const that=this;
  176. const css=Theme[that.data.IsShowLightColor?1:0];
  177. wx.setNavigationBarColor({
  178. frontColor: css.frontColor,
  179. backgroundColor: css.backgroundColor,
  180. });
  181. wx.setBackgroundColor({
  182. backgroundColor: css.backgroundColor,
  183. backgroundColorTop:css.backgroundColor,
  184. backgroundColorBottom:css.backgroundColor,
  185. });
  186. that.setData({
  187. ThemeCSS:css.Name,
  188. });
  189. },
  190. updateData:function(content){
  191. const that=this;
  192. //console.log(content);
  193. for(let i=0;i<content.ArticleEnglish.length;i++){
  194. for(let j=0;j<content.FormsOfWords.length;j++){
  195. let word = content.FormsOfWords[j];
  196. let regex = new RegExp(`\\b${word}\\b[.,!?;:]?`, 'gi');
  197. if (that.data.IsShowKeyword){
  198. content.ArticleEnglish[i] = content.ArticleEnglish[i].replace(regex, match => {
  199. let punctuation = match.match(/[.,!?;:]$/);
  200. let punc = punctuation ? punctuation[0] : '';
  201. let wordPart = match.replace(/[.,!?;:]$/, '');
  202. return `<span class='highlight'>${wordPart}</span>${punc}`;
  203. });
  204. }
  205. }
  206. }
  207. content.ArticleEnglishStr=content.ArticleEnglish.join(" ");
  208. for(let i=0;i<content.Question.length;i++){
  209. for(let j=0;j<content.Question[i].OptionsEnglish.length;j++){
  210. let str=content.Question[i].OptionsChinese[j];
  211. content.Question[i].OptionsChinese[j]=str.substr(2);
  212. }
  213. let char = content.Question[i].Answer;
  214. let asciiCode = char.charCodeAt(0);
  215. content.Question[i].AnswerNumber=asciiCode-65;
  216. }
  217. that.setData({
  218. Content:content,
  219. }, function() {
  220. // 数据加载完成后,计算swiper高度
  221. that.calculateSwiperHeight();
  222. setTimeout(function(){
  223. that.setData({
  224. IsShowQuestion:false,
  225. })
  226. },1000);
  227. });
  228. },
  229. closeHelp:function(){
  230. this.setData({
  231. IsShowPanelHelp:false,
  232. });
  233. wx.setStorageSync('HiddenArticleHelp', true);
  234. },
  235. selectedAnswer:function(e){
  236. const that=this;
  237. const question=e.currentTarget.dataset.question;
  238. const index=e.currentTarget.dataset.index;
  239. that.data.Content.Question[question].UserAnswer=index;
  240. that.setData({
  241. Content:that.data.Content,
  242. });
  243. },
  244. showData:function(e){
  245. const that=this;
  246. let name=e.currentTarget.dataset.name;
  247. this.data[name] = !this.data[name];
  248. this.setData(this.data);
  249. if (name=="IsShowKeyword"){
  250. that.setShowKeyword();
  251. }
  252. else if (name=="IsShowLightColor"){
  253. that.setTheme();
  254. }
  255. else if (this.data[name] && (name=="IsShowTranslate" || name=="IsShowQuestion")){
  256. wx.showLoading({
  257. title: '请稍等',
  258. mask:true,
  259. })
  260. that.calculateSwiperHeight();
  261. setTimeout(function(){
  262. wx.hideLoading();
  263. },500);
  264. }
  265. },
  266. setShowKeyword:function(){
  267. const that=this;
  268. let content=that.data.Content;
  269. let source="highlight",target="nonelight";
  270. if (that.data["IsShowKeyword"]){
  271. source="nonelight";
  272. target="highlight";
  273. }
  274. content.ArticleEnglishStr=common.ReplaceAllString(content.ArticleEnglishStr,source,target);
  275. for(let i=0;i<content.Question.length;i++){
  276. for(let j=0;j<content.Question[i].OptionsEnglish.length;j++){
  277. content.Question[i].OptionsEnglish[j]=common.ReplaceAllString(content.Question[i].OptionsEnglish[j],source,target);
  278. }
  279. }
  280. that.setData({
  281. Content:content,
  282. });
  283. },
  284. nextQuestion:function(e){
  285. if (this.data.CurrentQuestionIndex+1<this.data.Content.Question.length){
  286. this.data.CurrentQuestionIndex=this.data.CurrentQuestionIndex+1;
  287. this.setData({
  288. IsShowAnswer:false,
  289. CurrentQuestionIndex:this.data.CurrentQuestionIndex,
  290. });
  291. }
  292. },
  293. updateQuestionIndex:function(e){
  294. this.setData({
  295. CurrentQuestionIndex:e.detail.current,
  296. });
  297. },
  298. // 计算所有swiper-item的高度,并设置swiper的高度为最高的那个
  299. calculateSwiperHeight: function() {
  300. const that = this;
  301. // 增加延迟时间,确保DOM完全渲染
  302. setTimeout(function() {
  303. // 分别计算每个问题和选项的高度
  304. const query = wx.createSelectorQuery();
  305. // 先计算问题文本的高度
  306. query.selectAll('.text04').boundingClientRect();
  307. // 再计算所有选项的高度
  308. query.selectAll('.text05').boundingClientRect();
  309. query.exec(function(res) {
  310. if (!res || res.length < 2 || !res[0] || !res[1]) return;
  311. const questionRects = res[0];
  312. const optionsRects = res[1];
  313. // 计算每个swiper-item的总高度
  314. let itemHeights = [];
  315. // 假设问题和选项是一一对应的
  316. const questionCount =questionRects.length;
  317. for (let i = 0; i < questionCount; i++) {
  318. // 问题高度 + 选项高度 + 固定间距(margin等)
  319. let totalHeight = questionRects[i].height + 100; // 100是估计的间距
  320. for(let j=0;j<4;j++){
  321. optionsRects[i*4+j].height=optionsRects[i*4+j].height*2+40;
  322. console.log(optionsRects[i*4+j].height);
  323. totalHeight+=optionsRects[i*4+j].height;
  324. }
  325. //debugger;
  326. itemHeights.push(totalHeight);
  327. }
  328. // 找出最高的高度
  329. let maxHeight = 0;
  330. itemHeights.forEach(function(height) {
  331. if (height > maxHeight) {
  332. maxHeight = height;
  333. }
  334. });
  335. console.log("计算的各项高度:", itemHeights);
  336. console.log("最大高度:", maxHeight);
  337. // 为swiper高度增加一些额外空间(为指示点和边距)
  338. maxHeight += 200; // 增加额外空间,确保内容完全显示
  339. // 设置swiper的高度
  340. that.setData({
  341. swiperHeight: maxHeight + 'rpx',
  342. });
  343. });
  344. }, 500); // 增加延迟时间到500ms
  345. },
  346. onContainerTap: function() {
  347. const currentTime = new Date().getTime();
  348. const lastTapTime = this.data.lastTapTime;
  349. const timeDiff = currentTime - lastTapTime;
  350. // 如果两次点击的时间间隔小于300毫秒,则认为是双击
  351. if (timeDiff < 300 && timeDiff > 0) {
  352. console.log('双击事件触发');
  353. // 在这里添加双击事件的处理逻辑
  354. // 例如:切换翻译显示状态
  355. this.showData({currentTarget:{dataset:{name:"IsShowTranslate"}}});
  356. }
  357. // 更新上一次点击的时间
  358. this.setData({
  359. lastTapTime: currentTime
  360. });
  361. },
  362. onShareAppMessage: function () {
  363. return {
  364. title: app.globalData.ShareTitle,
  365. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  366. imageUrl: app.globalData.ShareImage,
  367. }
  368. },
  369. })