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