preview.js 18 KB

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