preview.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. import common from '../../utils/util';
  2. import main from '../../utils/main';
  3. const app = getApp();
  4. var iTimeout = 0, intervalSound = 0, intervalRefresh = 0;
  5. var arrImage = [];
  6. var isCopying = false;
  7. var isPlaying = false;
  8. Page({
  9. data: {
  10. IsExistCard: false,
  11. },
  12. onLoad: function (options) {
  13. wx.hideShareMenu();
  14. var that = this;
  15. isCopying = false;
  16. var id = 0;
  17. if (options.id)
  18. id = options.id;
  19. that.setData({
  20. Containnerheight: main.getWindowHeight(),
  21. ShowType: options.type,
  22. MiaoguoCardID: id,
  23. });
  24. console.log("MiaoguoCardID:" + id);
  25. this.audioCtx = [];
  26. if (options.type == "share") {
  27. wx.setNavigationBarTitle({
  28. title: "来自分享"
  29. });
  30. }
  31. else if (options.type == "show") {
  32. wx.setNavigationBarTitle({
  33. title: "查看题卡"
  34. });
  35. }
  36. },
  37. onShow: function () {
  38. var that = this;
  39. if (that.data.ShowType)
  40. if (that.data.ShowType == "share") {
  41. var url = "GetMiaoguoCardInfo?UserID=" + app.globalData.introducer + "&MiaoguoCardID=" + that.data.MiaoguoCardID;
  42. main.getData(url, function (data) {
  43. if (data) {
  44. app.globalData.CardList = [data];
  45. that.init();
  46. }
  47. });
  48. }
  49. else
  50. that.init();
  51. },
  52. onUnload: function () {
  53. clearTimeout(iTimeout);
  54. clearInterval(intervalSound);
  55. },
  56. init: function () {
  57. var that = this;
  58. var list = app.globalData.CardList;
  59. var card = {};
  60. var prevId = 0;
  61. var nextId = 0;
  62. for (var i = 0; i < list.length; i++) {
  63. if (this.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  64. if (i > 0)
  65. prevId = list[i - 1].MiaoguoCardID;
  66. else
  67. prevId = 0;
  68. if (i < list.length - 1)
  69. nextId = list[i + 1].MiaoguoCardID;
  70. else
  71. nextId = 0;
  72. card = main.changeStringToView(list[i].Content);
  73. card.MiaoguoCardID = list[i].MiaoguoCardID;
  74. card.LimitTime = list[i].LimitTime;
  75. card.FontSize = list[i].FontSize;
  76. break;
  77. }
  78. }
  79. if (!card.FontSize)
  80. card.FontSize = 46;
  81. arrImage = card.Images;
  82. //console.log("arrImage:" + arrImage);
  83. var limitTimeStr = common.formatDateCHS(card.LimitTime);
  84. var isTodayPractice = true;
  85. var task = app.globalData.TaskToday;
  86. if (task && task.ListNew && (task.ListNew.length > 0 || task.ListHistory.length > 0 || task.ListReview.length > 0)) {
  87. for (var i = 0; i < task.ListNew.length; i++) {
  88. if (task.ListNew[i].MiaoguoCardID == that.data.MiaoguoCardID) {
  89. isTodayPractice = false;
  90. break;
  91. }
  92. }
  93. for (var i = 0; i < task.ListHistory.length; i++) {
  94. if (task.ListHistory[i].MiaoguoCardID == that.data.MiaoguoCardID) {
  95. isTodayPractice = false;
  96. break;
  97. }
  98. }
  99. for (var i = 0; i < task.ListReview.length; i++) {
  100. if (task.ListReview[i].MiaoguoCardID == that.data.MiaoguoCardID) {
  101. isTodayPractice = false;
  102. break;
  103. }
  104. }
  105. }
  106. this.setData({
  107. ID: card.MiaoguoCardID,
  108. Field: card.Field,
  109. Tags: card.Tags,
  110. PrevID: prevId,
  111. NextID: nextId,
  112. FontSize: card.FontSize,
  113. LimitTimeStr: limitTimeStr,
  114. IsTodayPractice: isTodayPractice,
  115. });
  116. },
  117. practiceToday: function () {
  118. var that = this;
  119. wx.showModal({
  120. title: '要今天练吗?',
  121. content: '这张题卡今天必须练习',
  122. success(res) {
  123. if (res.confirm) {
  124. var obj = {
  125. ID: that.data.ID,
  126. BtnNumber: -1,
  127. LearningType: 2,
  128. }
  129. that.saveCard(obj, function () {
  130. var time = common.formatTime(new Date());
  131. that.setData({
  132. LimitTimeStr: common.formatDateCHS(time),
  133. IsTodayPractice: false,
  134. });
  135. var list = app.globalData.CardList;
  136. for (var i = 0; i < list.length; i++) {
  137. if (that.data.ID == list[i].MiaoguoCardID) {
  138. list[i].LimitTime = time;
  139. list[i].LimitTimeStr = "今天";
  140. break;
  141. }
  142. }
  143. app.globalData.CardList = list;
  144. wx.showToast({
  145. title: '改为今天练',
  146. icon: 'none',
  147. });
  148. });
  149. }
  150. else if (res.cancel) {
  151. }
  152. }
  153. })
  154. },
  155. shelveTask: function () {
  156. //搁置题卡,今天不练
  157. var that = this;
  158. wx.showModal({
  159. title: '今天不练吗?',
  160. content: '这张题卡今天不练习',
  161. success(res) {
  162. if (res.confirm) {
  163. var url = "UpdateMiaoguoCardToday?";
  164. url += "ID=" + that.data.MiaoguoCardID;
  165. url += "&UserID=" + app.globalData.userInfo.UserID;
  166. url += "&IntervalTime=1d";
  167. url += "&BtnNumber=1";
  168. url += "&LearningType=3";
  169. url += "&Duration=0";
  170. url += "&IsUndo=0";
  171. main.getData(url, function (data) {
  172. var time = common.formatTime(common.addDate("d", 1, new Date()));
  173. that.setData({
  174. LimitTimeStr: common.formatDateCHS(time),
  175. IsTodayPractice: true,
  176. });
  177. var list = app.globalData.CardList;
  178. for (var i = 0; i < list.length; i++) {
  179. if (that.data.ID == list[i].MiaoguoCardID) {
  180. list[i].LimitTime = time;
  181. list[i].LimitTimeStr = "明天";
  182. break;
  183. }
  184. }
  185. app.globalData.CardList = list;
  186. wx.showToast({
  187. title: '改为明天练',
  188. icon: 'none',
  189. });
  190. });
  191. }
  192. else if (res.cancel) {
  193. }
  194. }
  195. })
  196. },
  197. saveCard: function (obj, callback) {
  198. var url = "UpdateMiaoguoCardToday?";
  199. url += "ID=" + obj.ID;
  200. url += "&UserID=" + app.globalData.userInfo.UserID;
  201. url += "&BtnNumber=" + obj.BtnNumber;
  202. url += "&LearningType=" + obj.LearningType;
  203. main.getData(url, function (data) {
  204. callback();
  205. });
  206. },
  207. copyCard: function () {
  208. if (!isCopying) {
  209. isCopying = true;
  210. setTimeout(function () {
  211. isCopying = false;
  212. }, 15000);
  213. wx.showLoading({
  214. title: '处理中',
  215. });
  216. var that = this;
  217. var url = "CollectMiaoguoCard?";
  218. url += "MiaoguoCardID=" + that.data.MiaoguoCardID;
  219. url += "&UserIDSource=" + app.globalData.introducer;
  220. url += "&UserIDTarget=" + app.globalData.userInfo.UserID;
  221. main.getData(url, function (data) {
  222. wx.hideLoading();
  223. var title = "题卡已存在!";
  224. var image = "universalpic_wrong_white_120x120";
  225. if (data == 1) {
  226. title = "已保存";
  227. image = "universalpic_saved_white_120x120";
  228. }
  229. wx.showToast({
  230. title: title,
  231. mask: true,
  232. image: "../images/" + image + ".png",
  233. duration: 2000,
  234. success: function () {
  235. that.setData({
  236. IsExistCard: true,
  237. });
  238. }
  239. });
  240. });
  241. }
  242. },
  243. playSound: function (e) {
  244. if (!isPlaying) {
  245. isPlaying = true;
  246. setTimeout(function () {
  247. isPlaying = false;
  248. }, 3000);
  249. var that = this;
  250. var str = e.currentTarget.dataset.content;
  251. var url;
  252. if (str.indexOf("英 [") >= 0 || str.indexOf("美 [") >= 0) {
  253. str = str.replace("英 [", "[");
  254. str = str.replace("美 [", "[");
  255. url = e.currentTarget.dataset.soundmark;
  256. }
  257. else if (e.currentTarget.dataset.soundmark && e.currentTarget.dataset.soundmark != "undefined") {
  258. var soundmark = e.currentTarget.dataset.soundmark;
  259. if (soundmark && soundmark.indexOf("http") < 0) {
  260. url = app.globalData.audioUrlBaidu;
  261. url = url.replace("[token]", app.globalData.BaiduToken);
  262. url = url.replace("[word]", soundmark);
  263. }
  264. else {
  265. url = soundmark;
  266. }
  267. }
  268. else {
  269. url = app.globalData.audioUrlBaidu;
  270. url = url.replace("[token]", app.globalData.BaiduToken);
  271. url = url.replace("[word]", str);
  272. }
  273. var arr = url.split(",");
  274. for (var i = 0; i < arr.length; i++) {
  275. this.audioCtx.push(wx.createAudioContext('myAudio' + i));
  276. }
  277. var index = 0;
  278. intervalSound = setInterval(function () {
  279. that.audioCtx[index].setSrc(arr[index]);
  280. that.audioCtx[index].play();
  281. index++;
  282. if (index >= arr.length) {
  283. clearInterval(intervalSound);
  284. }
  285. }, 1000);
  286. var fieldid = e.currentTarget.dataset.fieldid;
  287. var content = that.data.Field[fieldid];
  288. var selectIndex = 0;
  289. for (var i = 0; i < content.length; i++) {
  290. if (content[i].Type == 'sound') {
  291. if (content[i].Content == e.currentTarget.dataset.content) {
  292. content[i].SoundImageName = "gif";
  293. selectIndex = i;
  294. break;
  295. }
  296. }
  297. }
  298. that.setData({
  299. Field: that.data.Field,
  300. });
  301. iTimeout = setTimeout(function () {
  302. content[selectIndex].SoundImageName = "png";
  303. that.data.Field[fieldid] = content;
  304. that.setData({
  305. Field: that.data.Field,
  306. });
  307. }, 2000);
  308. }
  309. },
  310. onMore: function () {
  311. var that = this;
  312. wx.showActionSheet({
  313. itemList: ["详细信息", "编辑题卡", "删除题卡"],
  314. success(res) {
  315. //console.log(res.tapIndex);
  316. if (res.tapIndex == 0) {
  317. wx.navigateTo({
  318. url: './cardInfo?id=' + that.data.MiaoguoCardID,
  319. });
  320. }
  321. else if (res.tapIndex == 1) {
  322. wx.navigateTo({
  323. url: './add?type=edit2&type2=1&id=' + that.data.MiaoguoCardID,
  324. });
  325. }
  326. else if (res.tapIndex == 2) {
  327. that.deleteCard();
  328. }
  329. },
  330. })
  331. },
  332. deleteCard: function () {
  333. var that = this;
  334. wx.showModal({
  335. title: '提醒',
  336. content: '这张题卡要删除吗?',
  337. success(res) {
  338. if (res.confirm) {
  339. var url = 'DeleteMiaoguoCard?UserID=' + app.globalData.userInfo.UserID;
  340. url += "&ID=" + that.data.MiaoguoCardID;
  341. main.getData(url, function (data) {
  342. var list = app.globalData.CardList;
  343. for (var i = 0; i < list.length; i++) {
  344. if (that.data.MiaoguoCardID == list[i].MiaoguoCardID) {
  345. list.splice(i, 1);
  346. break;
  347. }
  348. }
  349. app.globalData.CardList = list;
  350. var taskToday = app.globalData.TaskToday;
  351. taskToday.CardNumber--;
  352. app.globalData.TaskToday = taskToday;
  353. wx.navigateBack({
  354. delta: 1,
  355. });
  356. });
  357. }
  358. }
  359. });
  360. },
  361. onPreview: function (e) {
  362. clearTimeout(iTimeout);
  363. var id = e.currentTarget.dataset.id;
  364. wx.redirectTo({
  365. url: './preview?type=show&id=' + id,
  366. });
  367. },
  368. close: function () {
  369. wx.navigateBack({
  370. delta: 1,
  371. });
  372. },
  373. editField: function (e) {
  374. if (this.data.ShowType == "show") {
  375. var that = this;
  376. var fieldid = e.currentTarget.dataset.id;
  377. wx.navigateTo({
  378. url: './add?type=edit3&id=' + that.data.MiaoguoCardID + '&fieldid=' + fieldid,
  379. });
  380. }
  381. },
  382. showImage: function (e) {
  383. var name = e.currentTarget.dataset.name;
  384. wx.previewImage({
  385. current: name,
  386. urls: arrImage,
  387. });
  388. },
  389. onBindError: function (e) {
  390. var that = this;
  391. var serverUrl = e.currentTarget.dataset.serverurl;
  392. var oldTempUrl = e.currentTarget.dataset.name;
  393. //console.log("serverUrl1:" + serverUrl);
  394. wx.downloadFile({
  395. url: serverUrl, // 仅为示例,并非真实的资源
  396. success(res) {
  397. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  398. if (res.statusCode === 200) {
  399. //console.log("serverUrl2:" + serverUrl);
  400. //console.log(res.tempFilePath);
  401. main.saveTempImage(serverUrl, res.tempFilePath);
  402. for (var i = 0; i < arrImage.length; i++) {
  403. if (arrImage[i] == oldTempUrl) {
  404. arrImage[i] = res.tempFilePath;
  405. }
  406. }
  407. }
  408. }
  409. });
  410. clearTimeout(intervalRefresh);
  411. intervalRefresh = setTimeout(function () {
  412. that.init();
  413. }, 1000);
  414. },
  415. returnDefault: function () {
  416. wx.reLaunch({
  417. url: './default?IsStart=1',
  418. });
  419. },
  420. onShareAppMessage: function () {
  421. var that = this;
  422. this.setData({
  423. ShowType: 'sharepreview'
  424. });
  425. setTimeout(function () {
  426. that.setData({
  427. ShowType: 'show'
  428. });
  429. }, 1000);
  430. return {
  431. title: '我的分享',
  432. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID + '&Share=1&MiaoguoCardID=' + this.data.MiaoguoCardID,
  433. imageUrl: app.globalData.ShareImage,
  434. }
  435. },
  436. })