preview.js 18 KB

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