preview.js 18 KB

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