preview.js 16 KB

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