preview.js 17 KB

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