preview.js 18 KB

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