preview.js 12 KB

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