preview.js 17 KB

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