preview.js 17 KB

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