preview.js 17 KB

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