preview.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. IsCardInfoCloseMenu:1,
  21. IsClickShowAnswer:false,
  22. Flag:0,
  23. },
  24. onReady: function () {
  25. var that=this;
  26. innerAudioContext1 = wx.createInnerAudioContext();
  27. innerAudioContext1.onPlay(() => {
  28. console.log('开始播放')
  29. });
  30. innerAudioContext1.onError((res) => {
  31. console.log("innerAudioContext1.errMsg:" + res.errMsg);
  32. console.log("innerAudioContext1.errCode:" + res.errCode);
  33. that.audioCtx.setSrc(tempPlayUrl);
  34. that.audioCtx.play();
  35. });
  36. },
  37. onLoad: function (options) {
  38. var that = this;
  39. isCopying = false;
  40. var id = 0;
  41. if (options.id)
  42. id = options.id;
  43. that.setData({
  44. Containnerheight: main.getWindowHeight(),
  45. ShowType: options.type,
  46. MiaoguoCardID: id,
  47. Color: main.getDetailColor(app.globalData.ColorIndex),
  48. });
  49. console.log("MiaoguoCardID:" + id);
  50. this.audioCtx = wx.createAudioContext('myAudio');
  51. if (options.type == "show") {
  52. wx.setNavigationBarTitle({
  53. title: "浏览题卡"
  54. });
  55. if (wx.setBackgroundColor) {
  56. wx.setBackgroundColor({
  57. backgroundColor: that.data.Color.BackColor,
  58. backgroundColorTop: "#ffffff",
  59. backgroundColorBottom: that.data.Color.BackColor,
  60. })
  61. }
  62. }
  63. else {
  64. if (options.type == "share") {
  65. wx.setNavigationBarTitle({
  66. title: "来自分享"
  67. });
  68. }
  69. else if (options.type == "play") {
  70. wx.setNavigationBarTitle({
  71. title: "幻灯片"
  72. });
  73. if (that.data.ShowType == "play") {
  74. playList = [];
  75. for (var i = 0; i < app.globalData.CardList.length; i++) {
  76. playList.push(app.globalData.CardList[i].MiaoguoCardID);
  77. }
  78. playList = common.randomArray(playList);
  79. that.data.MiaoguoCardID = playList[0];
  80. that.setData({
  81. MiaoguoCardID: that.data.MiaoguoCardID,
  82. CurrentIndex: 0,
  83. PlayListCount: playList.length,
  84. });
  85. }
  86. }
  87. if (wx.setBackgroundColor) {
  88. wx.setBackgroundColor({
  89. backgroundColor: that.data.Color.BackColor,
  90. backgroundColorTop: that.data.Color.BackColor,
  91. backgroundColorBottom: that.data.Color.BackColor,
  92. })
  93. }
  94. if (wx.setNavigationBarColor) {
  95. wx.setNavigationBarColor({
  96. frontColor: "#ffffff",
  97. backgroundColor: that.data.Color.BackColor,
  98. })
  99. }
  100. }
  101. if (app.globalData.BaiduToken==="")
  102. main.getBaiduToken();
  103. if (app.globalData.IsOppo){
  104. that.setData({
  105. TextSpace:"nbsp",
  106. });
  107. }
  108. if (options.PrintID){
  109. that.setData({
  110. PrintID:options.PrintID,
  111. });
  112. }
  113. else{
  114. common.getStorageValue(that, "IsCardInfoCloseMenu", 1, function () {
  115. if (options.type == "preview"){
  116. that.setData({
  117. IsCardInfoCloseMenu:1,
  118. });
  119. }
  120. });
  121. }
  122. },
  123. onShow: function () {
  124. var that = this;
  125. if (that.data.ShowType)
  126. if (that.data.ShowType == "share") {
  127. var url = "GetMiaoguoCardInfo?UserID=" + app.globalData.introducer + "&MiaoguoCardID=" + that.data.MiaoguoCardID;
  128. main.getData(url, function (data) {
  129. if (data) {
  130. app.globalData.CardList = [data];
  131. that.init();
  132. }
  133. else {
  134. var time = 2000;
  135. wx.showToast({
  136. title: '分享人已删除',
  137. mask: true,
  138. duration: time,
  139. complete: function () {
  140. setTimeout(function () {
  141. wx.reLaunch({
  142. url: './default?IsStart=1',
  143. });
  144. }, time);
  145. }
  146. });
  147. }
  148. });
  149. }
  150. else
  151. that.init();
  152. },
  153. onUnload: function () {
  154. isPlaying = false;
  155. if (innerAudioContext1) {
  156. innerAudioContext1.stop();
  157. }
  158. },
  159. onPullDownRefresh: function () {
  160. var that = this;
  161. if (that.data.IsClickShowAnswer) {
  162. that.onHideAnswer();
  163. }
  164. wx.stopPullDownRefresh();
  165. },
  166. init: function () {
  167. var that = this;
  168. var list = app.globalData.CardList;
  169. if (app.globalData.CardList2){
  170. for(var i=0;i<app.globalData.CardList2.length;i++)
  171. list.push(app.globalData.CardList2[i]);
  172. }
  173. var card = {};
  174. var prevId = 0, nextId = 0;
  175. for (var i = 0; i < list.length; i++) {
  176. if (this.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  177. if (i > 0)
  178. prevId = list[i - 1].MiaoguoCardID;
  179. else
  180. prevId = 0;
  181. if (i < list.length - 1)
  182. nextId = list[i + 1].MiaoguoCardID;
  183. else
  184. nextId = 0;
  185. card = main.changeStringToView(list[i].Content);
  186. card.MiaoguoCardID = list[i].MiaoguoCardID;
  187. card.LimitTime = list[i].LimitTime;
  188. card.FontSize = list[i].FontSize;
  189. card.IsCollect = list[i].IsCollect;
  190. card.CardType = list[i].CardType;
  191. card.FolderName=list[i].FolderName;
  192. card.Flag=list[i].Flag;
  193. break;
  194. }
  195. }
  196. if (!card.FontSize)
  197. card.FontSize = 48;
  198. arrImage = card.Images;
  199. //console.log("arrImage:" + arrImage);
  200. var limitTimeStr = common.formatDateCHS(card.LimitTime);
  201. var isTodayPractice = true;
  202. var task = app.globalData.TaskToday;
  203. if (task && task.ListNew && (task.ListNew.length > 0 || task.ListHistory.length > 0 || task.ListReview.length > 0)) {
  204. for (var i = 0; i < task.ListNew.length; i++) {
  205. if (task.ListNew[i].MiaoguoCardID == that.data.MiaoguoCardID) {
  206. isTodayPractice = false;
  207. break;
  208. }
  209. }
  210. for (var i = 0; i < task.ListHistory.length; i++) {
  211. if (task.ListHistory[i].MiaoguoCardID == that.data.MiaoguoCardID) {
  212. isTodayPractice = false;
  213. break;
  214. }
  215. }
  216. for (var i = 0; i < task.ListReview.length; i++) {
  217. if (task.ListReview[i].MiaoguoCardID == that.data.MiaoguoCardID) {
  218. isTodayPractice = false;
  219. break;
  220. }
  221. }
  222. }
  223. var TagWidth = 0;
  224. if (card.Tags && card.Tags.length > 0) {
  225. TagWidth = 92 + (card.Tags.length - 1) * 122;
  226. for (var j = 0; j < card.Tags.length; j++) {
  227. if (card.Tags[j].length > 2) {
  228. TagWidth += 46 * (card.Tags[j].length - 2);
  229. }
  230. }
  231. }
  232. if (that.data.IsClickShowAnswer) {
  233. that.onHideAnswer();
  234. }
  235. else{
  236. that.onShowAnswer();
  237. }
  238. this.setData({
  239. ID: card.MiaoguoCardID,
  240. CardType:card.CardType,
  241. Field: card.Field,
  242. Tags: card.Tags,
  243. FontSize:card.FontSize,
  244. TagWidth: TagWidth,
  245. PrevID: prevId,
  246. NextID: nextId,
  247. FontSize: card.FontSize,
  248. LimitTime: card.LimitTime,
  249. LimitTimeStr: limitTimeStr,
  250. IsTodayPractice: isTodayPractice,
  251. IsCollect: card.IsCollect,
  252. FolderName:card.FolderName,
  253. Flag:card.Flag,
  254. });
  255. wx.pageScrollTo({
  256. scrollTop: 0,
  257. });
  258. isPlaying = false;
  259. if (app.globalData.TempStr=="deleteCard"){
  260. that.deleteCard();
  261. app.globalData.TempStr="";
  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. },
  437. close: function () {
  438. wx.navigateBack({
  439. delta: 1,
  440. });
  441. },
  442. editField: function (e) {
  443. if (this.data.ShowType == "show") {
  444. var that = this;
  445. var strType="edit";
  446. if (e.currentTarget.dataset.type)
  447. strType="edit1";
  448. var url='./add?type='+strType+'&id=' + that.data.MiaoguoCardID+"&opentype="+e.currentTarget.dataset.type;
  449. wx.navigateTo({
  450. url: url,
  451. });
  452. }
  453. },
  454. gotoCollect: function () {
  455. var that = this;
  456. if (!isCollecting) {
  457. isCollecting = true;
  458. setTimeout(function () {
  459. isCollecting = false;
  460. }, 3000);
  461. if (!that.data.IsCollect) {
  462. that.data.IsCollect = 1;
  463. }
  464. else {
  465. that.data.IsCollect = 0;
  466. }
  467. that.setData({
  468. IsCollect: that.data.IsCollect
  469. });
  470. main.setCollect(that.data.MiaoguoCardID, that.data.IsCollect, function () {
  471. var list = app.globalData.CardList;
  472. for (var i = 0; i < list.length; i++) {
  473. if (that.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  474. list[i].IsCollect = that.data.IsCollect;
  475. break;
  476. }
  477. }
  478. });
  479. }
  480. },
  481. setClickShowAnswer: function () {
  482. this.setData({
  483. IsClickShowAnswer:true,
  484. IsShowAnswer: [0, 0],
  485. IsShowMenu:false,
  486. });
  487. },
  488. setAlwaysShowAnswer: function () {
  489. this.setData({
  490. IsClickShowAnswer:false,
  491. IsShowAnswer: [1, 1],
  492. IsShowMenu:false,
  493. });
  494. },
  495. onHideAnswer: function () {
  496. this.setData({
  497. IsShowAnswer: [0, 0],
  498. });
  499. },
  500. onShowAnswer: function () {
  501. this.setData({
  502. IsShowAnswer: [1, 1],
  503. });
  504. },
  505. onPlayAgain: function () {
  506. playList.push(this.data.MiaoguoCardID);
  507. this.setData({
  508. PlayListCount: playList.length,
  509. NextID: 1,
  510. });
  511. wx.showToast({
  512. title: '稍后回放',
  513. mask: true,
  514. duration: 500,
  515. });
  516. },
  517. playNext: function (e) {
  518. var that = this;
  519. var div = e.currentTarget.dataset.div;
  520. if (div == 1) {
  521. this.data.CurrentIndex++;
  522. if (this.data.CurrentIndex >= playList.length) {
  523. this.data.CurrentIndex = playList.length - 1;
  524. }
  525. }
  526. else {
  527. this.data.CurrentIndex--;
  528. if (this.data.CurrentIndex < 0)
  529. this.data.CurrentIndex = 0;
  530. }
  531. that.setData({
  532. CurrentIndex: this.data.CurrentIndex,
  533. MiaoguoCardID: playList[this.data.CurrentIndex],
  534. PlayListCount: playList.length,
  535. });
  536. that.init();
  537. },
  538. showImage: function (e) {
  539. var name = e.currentTarget.dataset.name;
  540. if (name && name.length > 5) {
  541. wx.previewImage({
  542. current: name,
  543. urls: arrImage
  544. });
  545. }
  546. },
  547. onBindError: function (e) {
  548. var that = this;
  549. var serverUrl = e.currentTarget.dataset.serverurl;
  550. var oldTempUrl = e.currentTarget.dataset.name;
  551. //console.log("serverUrl1:" + serverUrl);
  552. wx.downloadFile({
  553. url: serverUrl, // 仅为示例,并非真实的资源
  554. success(res) {
  555. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  556. if (res.statusCode === 200) {
  557. //console.log("serverUrl2:" + serverUrl);
  558. //console.log(res.tempFilePath);
  559. main.saveTempImage(serverUrl, res.tempFilePath);
  560. for (var i = 0; i < arrImage.length; i++) {
  561. if (arrImage[i] == oldTempUrl) {
  562. arrImage[i] = res.tempFilePath;
  563. }
  564. }
  565. }
  566. }
  567. });
  568. clearTimeout(intervalRefresh);
  569. intervalRefresh = setTimeout(function () {
  570. that.init();
  571. }, 1000);
  572. },
  573. returnDefault: function () {
  574. wx.reLaunch({
  575. url: './default?IsStart=1',
  576. });
  577. },
  578. btnMore:function(){
  579. this.setData({
  580. IsShowMenu: true,
  581. });
  582. },
  583. closeMenu:function(){
  584. this.setData({
  585. IsShowMenu: false,
  586. });
  587. },
  588. closeIsCardInfoCloseMenu:function(){
  589. this.setData({
  590. IsCardInfoCloseMenu: 1,
  591. });
  592. wx.setStorageSync('IsCardInfoCloseMenu', 1);
  593. },
  594. openIsCardInfoCloseMenu:function(){
  595. this.setData({
  596. IsCardInfoCloseMenu: 0,
  597. });
  598. wx.setStorageSync('IsCardInfoCloseMenu', 0);
  599. },
  600. onShareAppMessage: function () {
  601. var that = this;
  602. this.setData({
  603. ShowType: 'sharepreview',
  604. IsCardInfoCloseMenu: 1,
  605. });
  606. var url = 'ShareMiaoguoCard?UserID=' + app.globalData.userInfo.UserID;
  607. url += "&MiaoguoCardID=" + that.data.MiaoguoCardID;
  608. main.getData(url, function (data) { });
  609. setTimeout(function () {
  610. that.setData({
  611. ShowType: 'show',
  612. });
  613. common.getStorageValue(that, "IsCardInfoCloseMenu", 1, function () {});
  614. }, 1000);
  615. return {
  616. title: '我的分享',
  617. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID + '&type=share&ColorIndex=' + app.globalData.ColorIndex + '&MiaoguoCardID=' + this.data.MiaoguoCardID,
  618. }
  619. },
  620. })