preview.js 18 KB

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