preview.js 13 KB

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