preview.js 12 KB

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