preview.js 17 KB

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