shareitem.js 15 KB

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