preview.js 16 KB

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