preview.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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. if (res.statusCode === 200) {
  361. tempPlayUrlLocal = res.tempFilePath;
  362. that.audioCtx.setSrc(res.tempFilePath);
  363. that.audioCtx.play();
  364. tempPlayUrl = url;
  365. }
  366. //console.log("res.statusCode:" + res.statusCode);
  367. },
  368. fail(err) {
  369. wx.hideLoading();
  370. console.log("downling err:"+err);
  371. clearTimeout(timeout);
  372. }
  373. });
  374. }
  375. else {
  376. this.audioCtx.pause();
  377. if (innerAudioContext1)
  378. innerAudioContext1.stop();
  379. isPlaying = false;
  380. }
  381. },
  382. goto: function (e) {
  383. var url=e.currentTarget.dataset.url;
  384. wx.navigateTo({
  385. url: url,
  386. });
  387. },
  388. audioBindEnded:function(){
  389. isPlaying = false;
  390. },
  391. audioBindError: function (err) {
  392. //console.log(err);
  393. innerAudioContext1.src = tempPlayUrlLocal;
  394. innerAudioContext1.play();
  395. },
  396. getDetailInfo: function () {
  397. this.setData({
  398. IsShowMenu:false,
  399. });
  400. wx.navigateTo({
  401. url: './cardInfo?id=' + this.data.MiaoguoCardID,
  402. });
  403. },
  404. deleteCard: function () {
  405. var that = this;
  406. wx.showModal({
  407. title: '提醒',
  408. content: '确定删除吗?一定时间内可以在「回收」里找回。',
  409. success(res) {
  410. if (res.confirm) {
  411. var url = 'DeleteMiaoguoCard?UserID=' + app.globalData.userInfo.UserID;
  412. url += "&ID=" + that.data.MiaoguoCardID;
  413. main.getData(url, function (data) {
  414. var list = app.globalData.CardList;
  415. for (var i = 0; i < list.length; i++) {
  416. if (that.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  417. list.splice(i, 1);
  418. break;
  419. }
  420. }
  421. app.globalData.CardList = list;
  422. var taskToday = app.globalData.TaskToday;
  423. taskToday.CardNumber--;
  424. app.globalData.TaskToday = taskToday;
  425. wx.navigateBack({
  426. delta: 1,
  427. });
  428. });
  429. }
  430. }
  431. });
  432. },
  433. previewNext: function (e) {
  434. var that = this;
  435. var id = e.currentTarget.dataset.id;
  436. that.setData({
  437. MiaoguoCardID: id,
  438. });
  439. that.init();
  440. },
  441. close: function () {
  442. wx.navigateBack({
  443. delta: 1,
  444. });
  445. },
  446. editField: function (e) {
  447. if (this.data.ShowType == "show") {
  448. var that = this;
  449. var strType="edit";
  450. if (e.currentTarget.dataset.type)
  451. strType="edit2";
  452. var url='./add?type='+strType+'&id=' + that.data.MiaoguoCardID+"&opentype="+e.currentTarget.dataset.type;
  453. wx.navigateTo({
  454. url: url,
  455. });
  456. }
  457. },
  458. gotoCollect: function () {
  459. var that = this;
  460. if (!isCollecting) {
  461. isCollecting = true;
  462. setTimeout(function () {
  463. isCollecting = false;
  464. }, 3000);
  465. if (!that.data.IsCollect) {
  466. that.data.IsCollect = 1;
  467. }
  468. else {
  469. that.data.IsCollect = 0;
  470. }
  471. that.setData({
  472. IsCollect: that.data.IsCollect
  473. });
  474. main.setCollect(that.data.MiaoguoCardID, that.data.IsCollect, function () {
  475. var list = app.globalData.CardList;
  476. for (var i = 0; i < list.length; i++) {
  477. if (that.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  478. list[i].IsCollect = that.data.IsCollect;
  479. break;
  480. }
  481. }
  482. });
  483. }
  484. },
  485. setClickShowAnswer: function () {
  486. this.setData({
  487. IsClickShowAnswer:true,
  488. IsShowAnswer: [0, 0],
  489. IsShowMenu:false,
  490. });
  491. },
  492. setAlwaysShowAnswer: function () {
  493. this.setData({
  494. IsClickShowAnswer:false,
  495. IsShowAnswer: [1, 1],
  496. IsShowMenu:false,
  497. });
  498. },
  499. onHideAnswer: function () {
  500. this.setData({
  501. IsShowAnswer: [0, 0],
  502. });
  503. },
  504. onShowAnswer: function () {
  505. this.setData({
  506. IsShowAnswer: [1, 1],
  507. });
  508. },
  509. onPlayAgain: function () {
  510. playList.push(this.data.MiaoguoCardID);
  511. this.setData({
  512. PlayListCount: playList.length,
  513. NextID: 1,
  514. });
  515. wx.showToast({
  516. title: '稍后回放',
  517. mask: true,
  518. duration: 500,
  519. });
  520. },
  521. showImage: function (e) {
  522. var name = e.currentTarget.dataset.name;
  523. if (name && name.length > 5) {
  524. if (name.indexOf("http://tmp")>=0){
  525. wx.previewImage({
  526. current: name,
  527. urls: [name],
  528. });
  529. }
  530. else{
  531. wx.previewImage({
  532. current: name,
  533. urls: arrImage
  534. });
  535. }
  536. }
  537. },
  538. onBindError: function (e) {
  539. var that = this;
  540. var serverUrl = e.currentTarget.dataset.serverurl;
  541. var oldTempUrl = e.currentTarget.dataset.name;
  542. //console.log("serverUrl1:" + serverUrl);
  543. wx.downloadFile({
  544. url: serverUrl, // 仅为示例,并非真实的资源
  545. success(res) {
  546. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  547. if (res.statusCode === 200) {
  548. //console.log("serverUrl2:" + serverUrl);
  549. //console.log(res.tempFilePath);
  550. main.saveTempImage(serverUrl, res.tempFilePath);
  551. for (var i = 0; i < arrImage.length; i++) {
  552. if (arrImage[i] == oldTempUrl) {
  553. arrImage[i] = res.tempFilePath;
  554. }
  555. }
  556. }
  557. }
  558. });
  559. clearTimeout(intervalRefresh);
  560. intervalRefresh = setTimeout(function () {
  561. that.init();
  562. }, 1000);
  563. },
  564. showBishunOrKaiti:function(e){
  565. var that=this;
  566. main.downloadBishunKaitiImage(
  567. e.currentTarget.dataset.url,
  568. e.currentTarget.dataset.serverurl,
  569. e.currentTarget.dataset.type,
  570. that.data.Field,function(fields){
  571. that.data.Field=fields;
  572. that.setData({
  573. Field: that.data.Field,
  574. });
  575. });
  576. },
  577. returnDefault: function () {
  578. wx.reLaunch({
  579. url: './default?IsStart=1',
  580. });
  581. },
  582. btnMore:function(){
  583. this.setData({
  584. IsShowMenu: true,
  585. });
  586. },
  587. closeMenu:function(){
  588. this.setData({
  589. IsShowMenu: false,
  590. });
  591. },
  592. closeIsCardInfoCloseMenu:function(){
  593. this.setData({
  594. IsCardInfoCloseMenu: 1,
  595. });
  596. wx.setStorageSync('IsCardInfoCloseMenu', 1);
  597. },
  598. openIsCardInfoCloseMenu:function(){
  599. this.setData({
  600. IsCardInfoCloseMenu: 0,
  601. });
  602. wx.setStorageSync('IsCardInfoCloseMenu', 0);
  603. },
  604. onShareAppMessage: function () {
  605. var that = this;
  606. if (that.data.ShowType!="share"){
  607. that.setData({
  608. ShowType: 'sharepreview',
  609. IsCardInfoCloseMenu: 1,
  610. });
  611. var url = 'ShareMiaoguoCard?UserID=' + app.globalData.userInfo.UserID;
  612. url += "&MiaoguoCardID=" + that.data.MiaoguoCardID;
  613. main.getData(url, function (data) { });
  614. setTimeout(function () {
  615. that.setData({
  616. ShowType: 'show',
  617. });
  618. common.getStorageValue(that, "IsCardInfoCloseMenu", 1, function () {});
  619. }, 1000);
  620. }
  621. return {
  622. title: '我的分享',
  623. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID + '&type=share&ColorIndex=' + app.globalData.ColorIndex + '&MiaoguoCardID=' + this.data.MiaoguoCardID,
  624. }
  625. },
  626. })