preview.js 12 KB

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