preview.js 16 KB

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