shareitem.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. import common from '../../utils/util';
  2. import main from '../../utils/main';
  3. const app = getApp();
  4. var innerAudioContext1;
  5. Page({
  6. data: {
  7. IPhoneX: "",
  8. IsPlay: false,
  9. },
  10. onReady: function () {
  11. var that = this;
  12. innerAudioContext1 = wx.createInnerAudioContext();
  13. innerAudioContext1.onPlay(() => {
  14. console.log('开始播放');
  15. });
  16. innerAudioContext1.onEnded(() => {
  17. console.log('停止播放');
  18. that.playSound();
  19. });
  20. innerAudioContext1.onError((res) => {
  21. console.log("innerAudioContext1.errMsg:" + res.errMsg);
  22. console.log("innerAudioContext1.errCode:" + res.errCode);
  23. main.getBaiduToken();
  24. that.setData({
  25. IsPlay: false,
  26. });
  27. });
  28. },
  29. onLoad: function (options) {
  30. var that = this;
  31. var ishistory = 0;
  32. if (options.IsHistory)
  33. ishistory = 1;
  34. that.setData({
  35. Containnerheight: main.getWindowHeight(),
  36. IsHistory: ishistory,
  37. IsBuildReport: false,
  38. IsBuildReportHistory: false,
  39. });
  40. that.init(options.TaskID, options.Day, ishistory);
  41. },
  42. onUnload: function () {
  43. if (innerAudioContext1) {
  44. innerAudioContext1.stop();
  45. }
  46. },
  47. init: function (taskid, day, isHistory) {
  48. var that = this;
  49. var url = 'GetShareItemInfo?IsHistory=' + isHistory + '&UserID=' + app.globalData.userInfo.UserID + "&TaskID=" + taskid;
  50. main.getData(url, function (data) {
  51. data.Day = day;
  52. that.setData({
  53. Info: data,
  54. });
  55. if (that.data.IsHistory) {
  56. that.setData({
  57. Avatar: app.globalData.userInfo.AvatarUrl,
  58. NickName: app.globalData.userInfo.NickName,
  59. BgColor: main.getShareImageBackColor(),
  60. });
  61. }
  62. });
  63. if (app.globalData.IsIPhoneX) {
  64. that.setData({
  65. IPhoneX: "_IPhoneX",
  66. });
  67. }
  68. //如果没有生成过二维码,则生成
  69. if (!app.globalData.userInfo.HasACode) {
  70. var imageQRCode = "BuildMiaoguoQRCode?IsWeb=1&UserID=" + app.globalData.userInfo.UserID;
  71. main.getData(imageQRCode, function (data) {});
  72. app.globalData.userInfo.HasACode = 1;
  73. }
  74. },
  75. showImageSource: function (e) {
  76. var name = e.currentTarget.dataset.image;
  77. name = name.replace("_b_", "_a_");
  78. wx.previewImage({
  79. current: name,
  80. urls: [name],
  81. });
  82. },
  83. playSound: function () {
  84. var that = this;
  85. that.data.IsPlay = !that.data.IsPlay;
  86. that.setData({
  87. IsPlay: that.data.IsPlay,
  88. });
  89. if (that.data.IsPlay) {
  90. var str = "古往今来肺腑之言,常用名句收入囊中。\n";
  91. str += "小朋友,你好。\n";
  92. str += "今天来给你打气的是【" + that.data.Info.AuthorCn + "】\n";
  93. str += "他要对你说:\n";
  94. str += that.data.Info.Content + "\n";
  95. str += that.data.Info.AuthorCn + "是谁?";
  96. str += "他是" + that.data.Info.Country + "的" + that.data.Info.Title + "。\n";
  97. str += "有兴趣的小朋友,可以了解一下他的故事。我们下次接力再见。";
  98. var url = app.globalData.audioUrlBaidu;
  99. url = url.replace("[token]", app.globalData.BaiduToken);
  100. url = url.replace("[word]", str);
  101. url = url.replace("spd=3", "spd=5");
  102. url = encodeURI(url);
  103. wx.showLoading({
  104. title: '音频下载中',
  105. mask: true,
  106. });
  107. var timeout = setTimeout(function () {
  108. wx.hideLoading();
  109. }, 30000);
  110. //console.log(url);
  111. wx.downloadFile({
  112. url: url,
  113. success(res) {
  114. wx.hideLoading();
  115. clearTimeout(timeout);
  116. innerAudioContext1.src = res.tempFilePath;
  117. innerAudioContext1.play();
  118. },
  119. fail(err) {
  120. wx.hideLoading();
  121. }
  122. });
  123. } else {
  124. if (innerAudioContext1)
  125. innerAudioContext1.stop();
  126. }
  127. },
  128. //生成里程碑分享图片
  129. buildShareImageMilestone: function () {
  130. var that = this;
  131. that.setData({
  132. IsBuildReport: true,
  133. });
  134. var url1 = that.data.Info.MilestoneImage;
  135. var url3 = app.globalData.userInfo.AvatarUrl;
  136. var _uid = common.Encrypt(app.globalData.userInfo.UserID);
  137. if (_uid.indexOf("/") >= 0)
  138. _uid = common.ReplaceAllString(_uid, "/", "___");
  139. var url4 = app.globalData.uploadImageUrl + "MiaoguoUser" + _uid + ".png";
  140. main.DownloadFile(url1, function (tempFilePath1) {
  141. main.DownloadFile(url3, function (tempFilePath3) {
  142. main.DownloadFile(url4, function (tempFilePath4) {
  143. const query = wx.createSelectorQuery();
  144. query.select('#shareCanvas')
  145. .fields({
  146. node: true,
  147. size: true
  148. })
  149. .exec((res) => {
  150. const canvas = res[0].node;
  151. const ctx = canvas.getContext('2d');
  152. const dpr = wx.getSystemInfoSync().pixelRatio;
  153. canvas.width = res[0].width * dpr;
  154. canvas.height = res[0].height * dpr;
  155. ctx.scale(dpr, dpr);
  156. var img = canvas.createImage();
  157. img.src = tempFilePath1;
  158. img.onload = e => {
  159. ctx.drawImage(img, 0, 0, 700, 1050);
  160. };
  161. setTimeout(function(){
  162. ctx.fillStyle = "#ffffff";
  163. ctx.shadowBlur=4;
  164. ctx.shadowOffsetY=2;
  165. ctx.shadowColor="rgba(0,0,0,0.3)";
  166. ctx.fillRect(570, 70, 90, 90);
  167. var img3 = canvas.createImage();
  168. img3.src = tempFilePath3;
  169. img3.onload = e => {
  170. ctx.drawImage(img3, 575, 75, 80, 80);
  171. };
  172. var img4 = canvas.createImage();
  173. img4.src = tempFilePath4;
  174. img4.onload = e => {
  175. ctx.drawImage(img4, 530, 880, 140, 140);
  176. };
  177. ctx.fillStyle = "#f2f2f2";
  178. ctx.font = "normal 500 24px caption";
  179. ctx.fillText(that.data.Info.Content1, 56, 650);
  180. ctx.fillText(that.data.Info.Content2, 56, 683);
  181. ctx.fillText(that.data.Info.Content3, 56, 716);
  182. ctx.fillStyle = "#4d4d4d";
  183. ctx.font = "normal 500 16px caption";
  184. ctx.shadowBlur=0;
  185. ctx.shadowOffsetY=0;
  186. for(var i=0;i< that.data.Info.Content.length;i++)
  187. ctx.fillText(that.data.Info.Content[i], 50, 917+i*30);
  188. main.BuildImage(700, 1050, canvas, function () {
  189. main.getData("ShareMiaoguoTask?UserID=" + app.globalData.userInfo.UserID, function (data) {
  190. that.setData({
  191. IsBuildReport: false,
  192. });
  193. });
  194. });
  195. },500);
  196. });
  197. });
  198. });
  199. });
  200. },
  201. //生成分享图片
  202. buildShareImage: function () {
  203. var that = this;
  204. that.setData({
  205. IsBuildReport: true,
  206. });
  207. var url1 = that.data.Info.Image.replace("_b_", "_d_");
  208. var url2 = that.data.Info.WordImage;
  209. var url3 = app.globalData.userInfo.AvatarUrl;
  210. var _uid = common.Encrypt(app.globalData.userInfo.UserID);
  211. if (_uid.indexOf("/") >= 0)
  212. _uid = common.ReplaceAllString(_uid, "/", "___");
  213. var url4 = app.globalData.uploadImageUrl + "MiaoguoUser" + _uid + ".png";
  214. main.DownloadFile(url1, function (tempFilePath1) {
  215. main.DownloadFile(url2, function (tempFilePath2) {
  216. main.DownloadFile(url3, function (tempFilePath3) {
  217. main.DownloadFile(url4, function (tempFilePath4) {
  218. const query = wx.createSelectorQuery();
  219. query.select('#shareCanvas')
  220. .fields({
  221. node: true,
  222. size: true
  223. })
  224. .exec((res) => {
  225. const canvas = res[0].node;
  226. const ctx = canvas.getContext('2d');
  227. const dpr = wx.getSystemInfoSync().pixelRatio;
  228. canvas.width = res[0].width * dpr;
  229. canvas.height = res[0].height * dpr;
  230. ctx.scale(dpr, dpr);
  231. var img = canvas.createImage();
  232. img.src = tempFilePath1;
  233. img.onload = e => {
  234. ctx.drawImage(img, 0, 0, 700, 520);
  235. };
  236. var img2 = canvas.createImage();
  237. img2.src = tempFilePath2;
  238. img2.onload = e => {
  239. ctx.drawImage(img2, 0, 520, 700, 270);
  240. };
  241. ctx.fillStyle = "#ffffff";
  242. ctx.fillRect(0, 790, 700, 260);
  243. var img3 = canvas.createImage();
  244. img3.src = tempFilePath3;
  245. img3.onload = e => {
  246. ctx.drawImage(img3, 610, 807, 60, 60);
  247. var img5 = canvas.createImage();
  248. img5.src = "../../pages/images/promotion_face_circle.png";
  249. img5.onload = e => {
  250. ctx.drawImage(img5, 610, 807, 60, 60);
  251. };
  252. };
  253. var img4 = canvas.createImage();
  254. img4.src = tempFilePath4;
  255. img4.onload = e => {
  256. ctx.drawImage(img4, 530, 880, 140, 140);
  257. };
  258. ctx.fillStyle = "#4d4d4d";
  259. ctx.fillRect(30, 836, 560, 2);
  260. ctx.font = "normal 600 18px caption";
  261. ctx.fillText("我的「秒过」勤勉接力", 30, 866);
  262. ctx.font = "normal 600 18px caption";
  263. ctx.fillText("坚持练习", 30, 911);
  264. ctx.font = "normal 600 18px caption";
  265. ctx.fillText("天", 30, 1016);
  266. ctx.font = "normal 600 16px caption";
  267. ctx.fillText("勇敢挑战", 266, 934);
  268. ctx.font = "normal 600 16px caption";
  269. ctx.fillText("个知识点", 266, 998);
  270. ctx.font = "normal 600 16px caption";
  271. ctx.fillText("专注复习", 370, 934);
  272. ctx.font = "normal 600 16px caption";
  273. ctx.fillText("分钟", 370, 998);
  274. ctx.font = "normal 500 18px Roboto-Black";
  275. ctx.fillText(that.data.Info.CreateTimeStr1, 30, 823);
  276. ctx.font = "normal 500 96px Roboto-Black";
  277. ctx.fillText(that.data.Info.Day, 30, 994);
  278. ctx.font = "normal 500 36px Roboto-Black";
  279. ctx.fillText(that.data.Info.CardTaskTotal, 266, 974);
  280. ctx.font = "normal 500 36px Roboto-Black";
  281. ctx.fillText(that.data.Info.DurationTotal, 370, 974);
  282. main.BuildImage(700, 1050, canvas, function () {
  283. main.getData("ShareMiaoguoTask?UserID=" + app.globalData.userInfo.UserID, function (data) {
  284. that.setData({
  285. IsBuildReport: false,
  286. });
  287. });
  288. });
  289. });
  290. });
  291. });
  292. });
  293. });
  294. },
  295. //生成分享图片(历史)
  296. buildShareImageHistory: function () {
  297. var that = this;
  298. that.setData({
  299. IsBuildReportHistory: true,
  300. });
  301. var date = that.data.Info.CreateTimeStr1
  302. var todayTime = that.data.Info.DurationMinute;
  303. var dayNumber = that.data.Info.Day;
  304. var avatarUrl = app.globalData.userInfo.AvatarUrl;
  305. avatarUrl = avatarUrl.substring(0, avatarUrl.lastIndexOf("/") + 1);
  306. avatarUrl = avatarUrl + "0";
  307. main.DownloadFile(avatarUrl, function (tempFilePath) {
  308. const query = wx.createSelectorQuery();
  309. query.select('#shareCanvasHistory')
  310. .fields({
  311. node: true,
  312. size: true
  313. })
  314. .exec((res) => {
  315. const canvas = res[0].node;
  316. const ctx = canvas.getContext('2d');
  317. const dpr = wx.getSystemInfoSync().pixelRatio;
  318. canvas.width = res[0].width * dpr;
  319. canvas.height = res[0].height * dpr;
  320. ctx.scale(dpr, dpr);
  321. var img = canvas.createImage();
  322. img.src = tempFilePath;
  323. img.onerror = err => {
  324. console.log(err);
  325. };
  326. img.onload = e => {
  327. ctx.drawImage(img, 0, 0, 360, 360);
  328. var img2 = canvas.createImage();
  329. img2.src = "../images/universalpic_screenshot_jl_bgimage_360x360.png";
  330. img2.onload = e => {
  331. ctx.drawImage(img2, 0, 0, 360, 360);
  332. ctx.fillStyle = that.data.BgColor;
  333. ctx.fillRect(360, 0, 240, 360);
  334. ctx.fillStyle = "#ffffff";
  335. ctx.font = "normal 500 24px caption";
  336. ctx.fillText(app.globalData.userInfo.NickName, 30, 306);
  337. ctx.font = "normal 500 18px caption";
  338. ctx.fillText(date, 30, 335);
  339. ctx.font = "normal 500 20px caption";
  340. ctx.fillText("秒过学习", 390, 40);
  341. ctx.font = "normal 500 44px caption";
  342. ctx.fillText("勤勉接力", 390, 88);
  343. ctx.font = "normal 500 20px caption";
  344. ctx.fillText("见证成长", 390, 140);
  345. ctx.font = "normal 500 68px caption";
  346. if (!todayTime)
  347. todayTime = 0;
  348. ctx.fillText(todayTime, 390, 209);
  349. ctx.font = "normal 500 68px caption";
  350. var metrics = ctx.measureText(todayTime.toString());
  351. //console.log(JSON.stringify(metrics));
  352. var h = 209;
  353. if (app.globalData.IsIOS)
  354. h = 206;
  355. ctx.font = "normal 500 32px caption";
  356. ctx.fillText("分钟", 393 + Math.round(metrics.width), h);
  357. ctx.font = "normal 500 20px caption";
  358. ctx.fillText("迄今已练习", 390, 295);
  359. ctx.font = "normal 500 32px caption";
  360. ctx.fillText(dayNumber + "天", 390, 333);
  361. main.BuildImage(600, 360, canvas, function () {
  362. that.setData({
  363. IsBuildReportHistory: false,
  364. });
  365. });
  366. };
  367. };
  368. });
  369. });
  370. },
  371. showHelpImage: function (e) {
  372. main.ShowHelpImage();
  373. },
  374. onShareAppMessage: function () {
  375. return {
  376. title: app.globalData.ShareTitle,
  377. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  378. imageUrl: app.globalData.ShareImage,
  379. }
  380. },
  381. })