preview.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. if (url.indexOf("'") > 0)
  390. url = common.ReplaceAllString(url,"'","");
  391. url = url.replace("http://", "https://");
  392. url = encodeURI(url);
  393. if (!isPlaying || tempPlayUrl=="" || url!=tempPlayUrl) {
  394. isPlaying = true;
  395. timeoutPlayAudio = setTimeout(function () {
  396. isPlaying = false;
  397. }, 60000);
  398. wx.showLoading({
  399. title: '音频下载中',
  400. mask: true,
  401. });
  402. setTimeout(function () {
  403. wx.hideLoading();
  404. }, 30000);
  405. console.log(url);
  406. wx.downloadFile({
  407. url: url,
  408. success(res) {
  409. wx.hideLoading();
  410. if (res.statusCode === 200) {
  411. tempPlayUrlLocal = res.tempFilePath;
  412. that.audioCtx.setSrc(res.tempFilePath);
  413. that.audioCtx.play();
  414. tempPlayUrl = url;
  415. }
  416. },
  417. fail(err) {
  418. wx.hideLoading();
  419. }
  420. });
  421. }
  422. else {
  423. this.audioCtx.pause();
  424. if (innerAudioContext1)
  425. innerAudioContext1.stop();
  426. isPlaying = false;
  427. }
  428. },
  429. audioBindEnded:function(){
  430. isPlaying = false;
  431. },
  432. audioBindError: function (err) {
  433. //console.log(err);
  434. innerAudioContext1.src = tempPlayUrlLocal;
  435. innerAudioContext1.play();
  436. },
  437. getDetailInfo: function () {
  438. wx.navigateTo({
  439. url: './cardInfo?id=' + this.data.MiaoguoCardID,
  440. });
  441. },
  442. deleteCard: function () {
  443. var that = this;
  444. wx.showModal({
  445. title: '提醒',
  446. content: '这张题卡要删除吗?',
  447. success(res) {
  448. if (res.confirm) {
  449. var url = 'DeleteMiaoguoCard?UserID=' + app.globalData.userInfo.UserID;
  450. url += "&ID=" + that.data.MiaoguoCardID;
  451. main.getData(url, function (data) {
  452. var list = app.globalData.CardList;
  453. for (var i = 0; i < list.length; i++) {
  454. if (that.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  455. list.splice(i, 1);
  456. break;
  457. }
  458. }
  459. app.globalData.CardList = list;
  460. var taskToday = app.globalData.TaskToday;
  461. taskToday.CardNumber--;
  462. app.globalData.TaskToday = taskToday;
  463. wx.navigateBack({
  464. delta: 1,
  465. });
  466. });
  467. }
  468. }
  469. });
  470. },
  471. previewNext: function (e) {
  472. var that = this;
  473. var id = e.currentTarget.dataset.id;
  474. that.setData({
  475. MiaoguoCardID: id,
  476. });
  477. that.init();
  478. },
  479. close: function () {
  480. wx.navigateBack({
  481. delta: 1,
  482. });
  483. },
  484. editField: function (e) {
  485. if (this.data.ShowType == "show") {
  486. var that = this;
  487. wx.navigateTo({
  488. url: './add?type=edit&id=' + that.data.MiaoguoCardID,
  489. });
  490. }
  491. },
  492. gotoCollect: function () {
  493. var that = this;
  494. if (!isCollecting) {
  495. isCollecting = true;
  496. setTimeout(function () {
  497. isCollecting = false;
  498. }, 3000);
  499. if (!that.data.IsCollect) {
  500. that.data.IsCollect = 1;
  501. }
  502. else {
  503. that.data.IsCollect = 0;
  504. }
  505. that.setData({
  506. IsCollect: that.data.IsCollect
  507. });
  508. main.setCollect(that.data.MiaoguoCardID, that.data.IsCollect, function () {
  509. var list = app.globalData.CardList;
  510. for (var i = 0; i < list.length; i++) {
  511. if (that.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  512. list[i].IsCollect = that.data.IsCollect;
  513. break;
  514. }
  515. }
  516. });
  517. }
  518. },
  519. onShowAnswer: function () {
  520. this.setData({
  521. IsShowAnswer: [1, 1]
  522. });
  523. },
  524. onPlayAgain: function () {
  525. playList.push(this.data.MiaoguoCardID);
  526. this.setData({
  527. PlayListCount: playList.length,
  528. NextID: 1,
  529. });
  530. wx.showToast({
  531. title: '稍后回放',
  532. mask: true,
  533. duration: 500,
  534. });
  535. },
  536. playNext: function (e) {
  537. var that = this;
  538. var div = e.currentTarget.dataset.div;
  539. if (div == 1) {
  540. this.data.CurrentIndex++;
  541. if (this.data.CurrentIndex >= playList.length) {
  542. this.data.CurrentIndex = playList.length - 1;
  543. }
  544. }
  545. else {
  546. this.data.CurrentIndex--;
  547. if (this.data.CurrentIndex < 0)
  548. this.data.CurrentIndex = 0;
  549. }
  550. that.setData({
  551. CurrentIndex: this.data.CurrentIndex,
  552. MiaoguoCardID: playList[this.data.CurrentIndex],
  553. PlayListCount: playList.length,
  554. });
  555. that.init();
  556. },
  557. showImage: function (e) {
  558. var name = e.currentTarget.dataset.name;
  559. if (name && name.length > 5) {
  560. wx.previewImage({
  561. current: name,
  562. urls: arrImage
  563. });
  564. }
  565. },
  566. onBindError: function (e) {
  567. var that = this;
  568. var serverUrl = e.currentTarget.dataset.serverurl;
  569. var oldTempUrl = e.currentTarget.dataset.name;
  570. //console.log("serverUrl1:" + serverUrl);
  571. wx.downloadFile({
  572. url: serverUrl, // 仅为示例,并非真实的资源
  573. success(res) {
  574. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  575. if (res.statusCode === 200) {
  576. //console.log("serverUrl2:" + serverUrl);
  577. //console.log(res.tempFilePath);
  578. main.saveTempImage(serverUrl, res.tempFilePath);
  579. for (var i = 0; i < arrImage.length; i++) {
  580. if (arrImage[i] == oldTempUrl) {
  581. arrImage[i] = res.tempFilePath;
  582. }
  583. }
  584. }
  585. }
  586. });
  587. clearTimeout(intervalRefresh);
  588. intervalRefresh = setTimeout(function () {
  589. that.init();
  590. }, 1000);
  591. },
  592. returnDefault: function () {
  593. wx.reLaunch({
  594. url: './default?IsStart=1',
  595. });
  596. },
  597. onShareAppMessage: function () {
  598. var that = this;
  599. this.setData({
  600. ShowType: 'sharepreview'
  601. });
  602. var url = 'ShareMiaoguoCard?UserID=' + app.globalData.userInfo.UserID;
  603. url += "&MiaoguoCardID=" + that.data.MiaoguoCardID;
  604. main.getData(url, function (data) { });
  605. setTimeout(function () {
  606. that.setData({
  607. ShowType: 'show'
  608. });
  609. }, 1000);
  610. return {
  611. title: '我的分享',
  612. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID + '&type=share&ColorIndex=' + app.globalData.ColorIndex + '&MiaoguoCardID=' + this.data.MiaoguoCardID,
  613. }
  614. },
  615. })