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