shareitem.js 16 KB

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