preview.js 17 KB

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