preview.js 16 KB

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