preview.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. import common from '../../utils/util';
  2. import main from '../../utils/main';
  3. const app = getApp();
  4. var intervalSound = 0, intervalRefresh = 0;
  5. var arrImage = [];
  6. var isCopying = false;
  7. var isPlaying = false;
  8. var isCollecting = false;
  9. var playList = [];
  10. var innerAudioContext;
  11. var timeoutPlayAudio;
  12. var tempPlayUrl = "";
  13. Page({
  14. data: {
  15. IsExistCard: false,
  16. },
  17. onLoad: function (options) {
  18. var that = this;
  19. isCopying = false;
  20. var id = 0;
  21. if (options.id)
  22. id = options.id;
  23. that.setData({
  24. Containnerheight: main.getWindowHeight(),
  25. ShowType: options.type,
  26. MiaoguoCardID: id,
  27. Color: main.getDetailColor(app.globalData.ColorIndex),
  28. });
  29. console.log("MiaoguoCardID:" + id);
  30. innerAudioContext = wx.createInnerAudioContext();
  31. innerAudioContext.onPlay(() => {
  32. console.log('开始播放');
  33. });
  34. innerAudioContext.onEnded(() => {
  35. console.log('结束播放');
  36. isPlaying = false;
  37. });
  38. if (options.type == "show") {
  39. wx.setNavigationBarTitle({
  40. title: "查看题卡"
  41. });
  42. if (wx.setBackgroundColor) {
  43. wx.setBackgroundColor({
  44. backgroundColor: that.data.Color.BackColor,
  45. backgroundColorTop: "#ffffff",
  46. backgroundColorBottom: that.data.Color.BackColor,
  47. })
  48. }
  49. }
  50. else {
  51. if (options.type == "share") {
  52. wx.setNavigationBarTitle({
  53. title: "来自分享"
  54. });
  55. }
  56. else if (options.type == "play") {
  57. wx.setNavigationBarTitle({
  58. title: "幻灯片"
  59. });
  60. if (that.data.ShowType == "play") {
  61. playList = [];
  62. for (var i = 0; i < app.globalData.CardList.length; i++) {
  63. playList.push(app.globalData.CardList[i].MiaoguoCardID);
  64. }
  65. playList = common.randomArray(playList);
  66. that.data.MiaoguoCardID = playList[0];
  67. that.setData({
  68. MiaoguoCardID: that.data.MiaoguoCardID,
  69. CurrentIndex: 0,
  70. PlayListCount: playList.length,
  71. });
  72. }
  73. }
  74. if (wx.setBackgroundColor) {
  75. wx.setBackgroundColor({
  76. backgroundColor: that.data.Color.BackColor,
  77. backgroundColorTop: that.data.Color.BackColor,
  78. backgroundColorBottom: that.data.Color.BackColor,
  79. })
  80. }
  81. if (wx.setNavigationBarColor) {
  82. wx.setNavigationBarColor({
  83. frontColor: "#ffffff",
  84. backgroundColor: that.data.Color.BackColor,
  85. })
  86. }
  87. }
  88. if (app.globalData.BaiduToken==="")
  89. main.getBaiduToken();
  90. },
  91. onShow: function () {
  92. var that = this;
  93. if (that.data.ShowType)
  94. if (that.data.ShowType == "share") {
  95. var url = "GetMiaoguoCardInfo?UserID=" + app.globalData.introducer + "&MiaoguoCardID=" + that.data.MiaoguoCardID;
  96. main.getData(url, function (data) {
  97. if (data) {
  98. app.globalData.CardList = [data];
  99. that.init();
  100. }
  101. else {
  102. var time = 2000;
  103. wx.showToast({
  104. title: '分享人已删除',
  105. mask: true,
  106. duration: time,
  107. complete: function () {
  108. setTimeout(function () {
  109. wx.reLaunch({
  110. url: './default?IsStart=1',
  111. });
  112. }, time);
  113. }
  114. });
  115. }
  116. });
  117. }
  118. else
  119. that.init();
  120. },
  121. onUnload: function () {
  122. if (innerAudioContext) {
  123. innerAudioContext.stop();
  124. isPlaying = false;
  125. }
  126. },
  127. onPullDownRefresh: function () {
  128. var that = this;
  129. if (that.data.ShowType == "play") {
  130. that.setData({
  131. IsShowAnswer: [0, 0],
  132. });
  133. }
  134. wx.stopPullDownRefresh();
  135. },
  136. init: function () {
  137. var that = this;
  138. var list = app.globalData.CardList;
  139. var card = {};
  140. var prevId = 0, nextId = 0;
  141. for (var i = 0; i < list.length; i++) {
  142. if (this.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  143. if (i > 0)
  144. prevId = list[i - 1].MiaoguoCardID;
  145. else
  146. prevId = 0;
  147. if (i < list.length - 1)
  148. nextId = list[i + 1].MiaoguoCardID;
  149. else
  150. nextId = 0;
  151. card = main.changeStringToView(list[i].Content);
  152. card.MiaoguoCardID = list[i].MiaoguoCardID;
  153. card.LimitTime = list[i].LimitTime;
  154. card.FontSize = list[i].FontSize;
  155. card.IsCollect = list[i].IsCollect;
  156. break;
  157. }
  158. }
  159. if (!card.FontSize)
  160. card.FontSize = 46;
  161. arrImage = card.Images;
  162. //console.log("arrImage:" + arrImage);
  163. var limitTimeStr = common.formatDateCHS(card.LimitTime);
  164. var isTodayPractice = true;
  165. var task = app.globalData.TaskToday;
  166. if (task && task.ListNew && (task.ListNew.length > 0 || task.ListHistory.length > 0 || task.ListReview.length > 0)) {
  167. for (var i = 0; i < task.ListNew.length; i++) {
  168. if (task.ListNew[i].MiaoguoCardID == that.data.MiaoguoCardID) {
  169. isTodayPractice = false;
  170. break;
  171. }
  172. }
  173. for (var i = 0; i < task.ListHistory.length; i++) {
  174. if (task.ListHistory[i].MiaoguoCardID == that.data.MiaoguoCardID) {
  175. isTodayPractice = false;
  176. break;
  177. }
  178. }
  179. for (var i = 0; i < task.ListReview.length; i++) {
  180. if (task.ListReview[i].MiaoguoCardID == that.data.MiaoguoCardID) {
  181. isTodayPractice = false;
  182. break;
  183. }
  184. }
  185. }
  186. var TagWidth = 0;
  187. if (card.Tags && card.Tags.length > 0) {
  188. TagWidth = 92 + (card.Tags.length - 1) * 122;
  189. for (var j = 0; j < card.Tags.length; j++) {
  190. if (card.Tags[j].length > 2) {
  191. TagWidth += 46 * (card.Tags[j].length - 2);
  192. }
  193. }
  194. }
  195. var IsShowAnswer = [1, 1];
  196. if (that.data.ShowType == "play") {
  197. IsShowAnswer = [0, 0];
  198. if (this.data.CurrentIndex <= 0) {
  199. prevId = 0;
  200. }
  201. else
  202. prevId = 1;
  203. if (this.data.CurrentIndex >= playList.length - 1) {
  204. nextId = 0;
  205. }
  206. else
  207. nextId = 1;
  208. }
  209. this.setData({
  210. ID: card.MiaoguoCardID,
  211. Field: card.Field,
  212. Tags: card.Tags,
  213. TagWidth: TagWidth,
  214. PrevID: prevId,
  215. NextID: nextId,
  216. FontSize: card.FontSize,
  217. LimitTimeStr: limitTimeStr,
  218. IsTodayPractice: isTodayPractice,
  219. IsCollect: card.IsCollect,
  220. IsShowAnswer: IsShowAnswer,
  221. });
  222. wx.pageScrollTo({
  223. scrollTop: 0,
  224. });
  225. if (innerAudioContext) {
  226. innerAudioContext.stop();
  227. isPlaying = false;
  228. }
  229. },
  230. practiceToday: function () {
  231. var that = this;
  232. wx.showModal({
  233. title: '要今天练吗?',
  234. content: '这张题卡今天必须练习',
  235. success(res) {
  236. if (res.confirm) {
  237. var arr = [];
  238. var obj = {};
  239. obj.Card = {};
  240. obj.Card.MiaoguoCardID = that.data.ID;
  241. obj.BtnNumber = -1;
  242. obj.LearningType = 2;
  243. arr.push(obj);
  244. wx.setStorageSync("ListTaskFinished", arr);
  245. main.UpdateMiaoguoCardTodayAll(false, function () {
  246. var time = common.formatTime(new Date());
  247. that.setData({
  248. LimitTimeStr: common.formatDateCHS(time),
  249. IsTodayPractice: false,
  250. });
  251. var list = app.globalData.CardList;
  252. for (var i = 0; i < list.length; i++) {
  253. if (that.data.ID == list[i].MiaoguoCardID) {
  254. list[i].LimitTime = time;
  255. list[i].LimitTimeStr = "今天";
  256. break;
  257. }
  258. }
  259. app.globalData.CardList = list;
  260. wx.showToast({
  261. title: '改为今天练',
  262. icon: 'none',
  263. });
  264. });
  265. }
  266. else if (res.cancel) {
  267. }
  268. }
  269. })
  270. },
  271. shelveTask: function () {
  272. //搁置题卡,今天不练
  273. var that = this;
  274. wx.showModal({
  275. title: '今天不练吗?',
  276. content: '这张题卡今天不练习',
  277. success(res) {
  278. if (res.confirm) {
  279. var arr = [];
  280. var obj = {};
  281. obj.Card = {};
  282. obj.Card.MiaoguoCardID = that.data.MiaoguoCardID;
  283. obj.IntervalTime = "1d";
  284. obj.BtnNumber = 1;
  285. obj.Duration = 0;
  286. obj.LearningType = 3;
  287. arr.push(obj);
  288. wx.setStorageSync("ListTaskFinished", arr);
  289. main.UpdateMiaoguoCardTodayAll(false, function () {
  290. var time = common.formatTime(common.addDate("d", 1, new Date()));
  291. that.setData({
  292. LimitTimeStr: common.formatDateCHS(time),
  293. IsTodayPractice: true,
  294. });
  295. var list = app.globalData.CardList;
  296. for (var i = 0; i < list.length; i++) {
  297. if (that.data.ID == list[i].MiaoguoCardID) {
  298. list[i].LimitTime = time;
  299. list[i].LimitTimeStr = "明天";
  300. break;
  301. }
  302. }
  303. app.globalData.CardList = list;
  304. wx.showToast({
  305. title: '改为明天练',
  306. icon: 'none',
  307. });
  308. });
  309. }
  310. else if (res.cancel) {
  311. }
  312. }
  313. })
  314. },
  315. copyCard: function () {
  316. if (!isCopying) {
  317. isCopying = true;
  318. setTimeout(function () {
  319. isCopying = false;
  320. }, 15000);
  321. wx.showLoading({
  322. title: '处理中',
  323. });
  324. setTimeout(function () {
  325. wx.hideLoading();
  326. }, 5000);
  327. var that = this;
  328. var url = "CollectMiaoguoCard?";
  329. url += "MiaoguoCardID=" + that.data.MiaoguoCardID;
  330. url += "&UserIDSource=" + app.globalData.introducer;
  331. url += "&UserIDTarget=" + app.globalData.userInfo.UserID;
  332. main.getData(url, function (data) {
  333. wx.hideLoading();
  334. var title = "题卡已存在!";
  335. var image = "universalpic_wrong_white_120x120";
  336. if (data == 1) {
  337. title = "已保存";
  338. image = "universalpic_saved_white_120x120";
  339. }
  340. wx.showToast({
  341. title: title,
  342. mask: true,
  343. image: "../images/" + image + ".png",
  344. duration: 2000,
  345. success: function () {
  346. that.setData({
  347. IsExistCard: true,
  348. });
  349. }
  350. });
  351. });
  352. }
  353. },
  354. playSound: function (e) {
  355. clearTimeout(timeoutPlayAudio);
  356. var that = this;
  357. var str = e.currentTarget.dataset.content;
  358. var url;
  359. if (str == "recorder") {
  360. url = e.currentTarget.dataset.soundmark;
  361. }
  362. else if (str.indexOf("英 [") >= 0 || str.indexOf("美 [") >= 0) {
  363. str = str.replace("英 [", "[");
  364. str = str.replace("美 [", "[");
  365. url = e.currentTarget.dataset.soundmark;
  366. }
  367. else if (e.currentTarget.dataset.soundmark && e.currentTarget.dataset.soundmark != "undefined") {
  368. var soundmark = e.currentTarget.dataset.soundmark;
  369. if (soundmark && soundmark.indexOf("http") < 0) {
  370. url = app.globalData.audioUrlBaidu;
  371. url = url.replace("[token]", app.globalData.BaiduToken);
  372. url = url.replace("[word]", soundmark);
  373. }
  374. else {
  375. url = soundmark;
  376. }
  377. }
  378. else {
  379. url = app.globalData.audioUrlBaidu;
  380. url = url.replace("[token]", app.globalData.BaiduToken);
  381. url = url.replace("[word]", str);
  382. }
  383. if (url.indexOf("http") > 0)
  384. url = url.substr(url.indexOf("http"));
  385. url = url.replace("http://", "https://");
  386. if (!isPlaying || tempPlayUrl=="" || url!=tempPlayUrl) {
  387. isPlaying = true;
  388. timeoutPlayAudio = setTimeout(function () {
  389. isPlaying = false;
  390. }, 60000);
  391. if (innerAudioContext) {
  392. innerAudioContext.stop();
  393. }
  394. wx.showLoading({
  395. title: '音频下载中',
  396. mask: true,
  397. });
  398. setTimeout(function () {
  399. wx.hideLoading();
  400. }, 30000);
  401. wx.downloadFile({
  402. url: encodeURI(url),
  403. success(res) {
  404. wx.hideLoading();
  405. if (res.statusCode === 200) {
  406. innerAudioContext.src = res.tempFilePath;
  407. innerAudioContext.play();
  408. tempPlayUrl=url;
  409. }
  410. },
  411. fail(err) {
  412. wx.hideLoading();
  413. }
  414. });
  415. }
  416. else {
  417. if (innerAudioContext) {
  418. innerAudioContext.stop();
  419. isPlaying = false;
  420. }
  421. }
  422. },
  423. getDetailInfo: function () {
  424. wx.navigateTo({
  425. url: './cardInfo?id=' + this.data.MiaoguoCardID,
  426. });
  427. },
  428. deleteCard: function () {
  429. var that = this;
  430. wx.showModal({
  431. title: '提醒',
  432. content: '这张题卡要删除吗?',
  433. success(res) {
  434. if (res.confirm) {
  435. var url = 'DeleteMiaoguoCard?UserID=' + app.globalData.userInfo.UserID;
  436. url += "&ID=" + that.data.MiaoguoCardID;
  437. main.getData(url, function (data) {
  438. var list = app.globalData.CardList;
  439. for (var i = 0; i < list.length; i++) {
  440. if (that.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  441. list.splice(i, 1);
  442. break;
  443. }
  444. }
  445. app.globalData.CardList = list;
  446. var taskToday = app.globalData.TaskToday;
  447. taskToday.CardNumber--;
  448. app.globalData.TaskToday = taskToday;
  449. wx.navigateBack({
  450. delta: 1,
  451. });
  452. });
  453. }
  454. }
  455. });
  456. },
  457. previewNext: function (e) {
  458. var that = this;
  459. var id = e.currentTarget.dataset.id;
  460. that.setData({
  461. MiaoguoCardID: id,
  462. });
  463. that.init();
  464. },
  465. close: function () {
  466. wx.navigateBack({
  467. delta: 1,
  468. });
  469. },
  470. editField: function (e) {
  471. if (this.data.ShowType == "show") {
  472. var that = this;
  473. wx.navigateTo({
  474. url: './add?type=edit&id=' + that.data.MiaoguoCardID,
  475. });
  476. }
  477. },
  478. gotoCollect: function () {
  479. var that = this;
  480. if (!isCollecting) {
  481. isCollecting = true;
  482. setTimeout(function () {
  483. isCollecting = false;
  484. }, 3000);
  485. if (!that.data.IsCollect) {
  486. that.data.IsCollect = 1;
  487. }
  488. else {
  489. that.data.IsCollect = 0;
  490. }
  491. that.setData({
  492. IsCollect: that.data.IsCollect
  493. });
  494. main.setCollect(that.data.MiaoguoCardID, that.data.IsCollect, function () {
  495. var list = app.globalData.CardList;
  496. for (var i = 0; i < list.length; i++) {
  497. if (that.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  498. list[i].IsCollect = that.data.IsCollect;
  499. break;
  500. }
  501. }
  502. });
  503. }
  504. },
  505. onShowAnswer: function () {
  506. this.setData({
  507. IsShowAnswer: [1, 1]
  508. });
  509. },
  510. onPlayAgain: function () {
  511. playList.push(this.data.MiaoguoCardID);
  512. this.setData({
  513. PlayListCount: playList.length,
  514. NextID: 1,
  515. });
  516. wx.showToast({
  517. title: '稍后回放',
  518. mask: true,
  519. duration: 500,
  520. });
  521. },
  522. playNext: function (e) {
  523. var that = this;
  524. var div = e.currentTarget.dataset.div;
  525. if (div == 1) {
  526. this.data.CurrentIndex++;
  527. if (this.data.CurrentIndex >= playList.length) {
  528. this.data.CurrentIndex = playList.length - 1;
  529. }
  530. }
  531. else {
  532. this.data.CurrentIndex--;
  533. if (this.data.CurrentIndex < 0)
  534. this.data.CurrentIndex = 0;
  535. }
  536. that.setData({
  537. CurrentIndex: this.data.CurrentIndex,
  538. MiaoguoCardID: playList[this.data.CurrentIndex],
  539. PlayListCount: playList.length,
  540. });
  541. that.init();
  542. },
  543. showImage: function (e) {
  544. var name = e.currentTarget.dataset.name;
  545. if (name && name.length > 5) {
  546. wx.previewImage({
  547. current: name,
  548. urls: arrImage
  549. });
  550. }
  551. },
  552. onBindError: function (e) {
  553. var that = this;
  554. var serverUrl = e.currentTarget.dataset.serverurl;
  555. var oldTempUrl = e.currentTarget.dataset.name;
  556. //console.log("serverUrl1:" + serverUrl);
  557. wx.downloadFile({
  558. url: serverUrl, // 仅为示例,并非真实的资源
  559. success(res) {
  560. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  561. if (res.statusCode === 200) {
  562. //console.log("serverUrl2:" + serverUrl);
  563. //console.log(res.tempFilePath);
  564. main.saveTempImage(serverUrl, res.tempFilePath);
  565. for (var i = 0; i < arrImage.length; i++) {
  566. if (arrImage[i] == oldTempUrl) {
  567. arrImage[i] = res.tempFilePath;
  568. }
  569. }
  570. }
  571. }
  572. });
  573. clearTimeout(intervalRefresh);
  574. intervalRefresh = setTimeout(function () {
  575. that.init();
  576. }, 1000);
  577. },
  578. returnDefault: function () {
  579. wx.reLaunch({
  580. url: './default?IsStart=1',
  581. });
  582. },
  583. onShareAppMessage: function () {
  584. var that = this;
  585. this.setData({
  586. ShowType: 'sharepreview'
  587. });
  588. var url = 'ShareMiaoguoCard?UserID=' + app.globalData.userInfo.UserID;
  589. url += "&MiaoguoCardID=" + that.data.MiaoguoCardID;
  590. main.getData(url, function (data) { });
  591. setTimeout(function () {
  592. that.setData({
  593. ShowType: 'show'
  594. });
  595. }, 1000);
  596. return {
  597. title: '我的分享',
  598. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID + '&Share=1&ColorIndex=' + app.globalData.ColorIndex + '&MiaoguoCardID=' + this.data.MiaoguoCardID,
  599. }
  600. },
  601. })