index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // nova-werun/pages/home/share/index.js
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. //屏幕高度
  10. statusBarHeight: 0, // 状态栏高度
  11. screenHeight: 0, // 屏幕高度
  12. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  13. bottomNavHeight: 0, // 底部导航栏高度
  14. contentHeight: 0, // 可用内容高度
  15. contentHeight2: 0,
  16. contentpadding: 0, //顶部padding高度
  17. sharList: [],
  18. images: [
  19. 'https://tse2-mm.cn.bing.net/th/id/OIP-C.kACuwmhGcHKV4G4Og9AFqgHaLG?rs=1&pid=ImgDetMain',
  20. 'https://tse1-mm.cn.bing.net/th/id/OIP-C.LYrbU3wnasLo4nvoAqs_HQHaLH?rs=1&pid=ImgDetMain',
  21. 'https://tse3-mm.cn.bing.net/th/id/OIP-C.KKzj-pCKvzmWJ9MU5pE5KwHaKq?rs=1&pid=ImgDetMain',
  22. 'https://ts1.cn.mm.bing.net/th/id/R-C.d8333a26a5494dfe1dc88e9fdd04fb31?rik=08fpnYZSbboHfQ&riu=http%3a%2f%2fpic.ibaotu.com%2fspiders%2f33817-59a6262d3ff70.jpg&ehk=fKs2FTTgk2k0kqet%2fVO%2bAS3D0Q6zlNyUPmT8UE%2bvpdY%3d&risl=&pid=ImgRaw&r=0',
  23. 'https://pic.nximg.cn/file/20230420/12106414_122949747127_2.jpg'
  24. ],
  25. randomImage: '',
  26. saveimage: ''
  27. },
  28. /**
  29. * 生命周期函数--监听页面加载
  30. */
  31. onLoad: function (options) {
  32. // 计算
  33. const systemInfo = wx.getSystemInfoSync();
  34. const statusBarHeight = systemInfo.statusBarHeight || 0;
  35. const screenHeight = systemInfo.screenHeight || 0;
  36. const custom = wx.getMenuButtonBoundingClientRect();
  37. const customHeight = custom.height + 10 + 2 || 0;
  38. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  39. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  40. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  41. this.setData({
  42. statusBarHeight,
  43. screenHeight,
  44. customHeight,
  45. bottomNavHeight,
  46. contentpadding,
  47. contentHeight
  48. });
  49. this.order()
  50. this.showRandomImage()
  51. },
  52. /**
  53. * 生命周期函数--监听页面初次渲染完成
  54. */
  55. onReady: function () {
  56. },
  57. /**
  58. * 生命周期函数--监听页面显示
  59. */
  60. onShow: function () {
  61. },
  62. /**
  63. * 生命周期函数--监听页面隐藏
  64. */
  65. onHide: function () {
  66. },
  67. /**
  68. * 生命周期函数--监听页面卸载
  69. */
  70. onUnload: function () {
  71. },
  72. /**
  73. * 页面相关事件处理函数--监听用户下拉动作
  74. */
  75. onPullDownRefresh: function () {
  76. },
  77. /**
  78. * 页面上拉触底事件的处理函数
  79. */
  80. onReachBottom: function () {
  81. },
  82. /**
  83. * 用户点击右上角分享
  84. */
  85. onShareAppMessage: function () {
  86. },
  87. //获取当天运动数据
  88. async order() {
  89. const currentUser = Parse.User.current();
  90. let ActivityDataquery = new Parse.Query('ActivityData');
  91. // ActivityDataquery.equalTo('user', currentUser.id);
  92. ActivityDataquery.equalTo('company', company);
  93. ActivityDataquery.equalTo('type', 'today');
  94. ActivityDataquery.notEqualTo('isDeleted', true);
  95. // 获取今天的日期
  96. const today = new Date();
  97. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  98. const todayEnd = new Date(todayStart);
  99. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  100. // 在查询条件中添加对 createdAt 的限制
  101. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  102. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  103. ActivityDataquery.include('user');
  104. // 根据 steps 字段进行降序排序
  105. ActivityDataquery.descending('steps');
  106. let r = await ActivityDataquery.find();
  107. let todayList = r.map(item => {
  108. let itemData = item.toJSON();
  109. // 获取当前时间并格式化
  110. const now = new Date();
  111. const formattedTime = `${now.getFullYear()}/${String(now.getMonth() + 1).padStart(2, '0')}/${String(now.getDate()).padStart(2, '0')} ${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`;
  112. itemData.currentTime = formattedTime; // 将当前时间添加到 item 中
  113. return itemData;
  114. });
  115. let sharList = []
  116. todayList.forEach((item, index) => {
  117. if (item.user.objectId == currentUser.id) {
  118. sharList.push({
  119. ...item, // 包含用户数据
  120. rank: index + 1 // 计算排名(index 从 0 开始,所以加 1)
  121. });
  122. }
  123. })
  124. this.setData({
  125. sharList
  126. });
  127. this.saveImage()
  128. console.log(this.data.sharList);
  129. },
  130. //随机展示图片
  131. showRandomImage: function () {
  132. const randomIndex = Math.floor(Math.random() * this.data.images.length);
  133. this.setData({
  134. randomImage: this.data.images[randomIndex]
  135. });
  136. },
  137. //绘制canvas
  138. rpxToPx(rpx) {
  139. const systemInfo = wx.getSystemInfoSync();
  140. return rpx * (systemInfo.windowWidth / 750);
  141. },
  142. //rpx
  143. saveImage() {
  144. // console.log('运行了');
  145. const canvas = wx.createCanvasContext('myCanvas');
  146. const width = this.rpxToPx(670);
  147. const height = this.rpxToPx(1100);
  148. // console.log(width, height);
  149. canvas.width = this.rpxToPx(670);
  150. canvas.height = this.rpxToPx(1100);
  151. // 绘制背景
  152. canvas.setFillStyle('#CAE1FD'); // 背景色
  153. canvas.fillRect(0, 0, width, height);
  154. canvas.setFillStyle('#0178EE');
  155. // 加载主图片
  156. wx.getImageInfo({
  157. src: this.data.randomImage,
  158. success: (res) => {
  159. canvas.drawImage(res.path, 0, 0, width, this.rpxToPx(500)); // 调整图片高度
  160. // console.log('主图片加载成功');
  161. // 加载用户头像
  162. wx.getImageInfo({
  163. src: this.data.sharList[0].user.avatar,
  164. success: (avatarRes) => {
  165. // 绘制带圆角的头像
  166. const avatarX = this.rpxToPx(40);
  167. const avatarY = this.rpxToPx(540);
  168. const avatarSize = this.rpxToPx(83);
  169. const radius = avatarSize / 2; // 半径为头像大小的一半
  170. // 绘制圆角矩形
  171. canvas.save(); // 保存当前状态
  172. canvas.beginPath(); // 开始路径
  173. canvas.moveTo(avatarX + radius, avatarY); // 左上角
  174. canvas.lineTo(avatarX + avatarSize - radius, avatarY); // 上边
  175. canvas.arc(avatarX + avatarSize - radius, avatarY + radius, radius, 1.5 * Math.PI, 0, false); // 右上角
  176. canvas.lineTo(avatarX + avatarSize, avatarY + avatarSize - radius); // 右边
  177. canvas.arc(avatarX + avatarSize - radius, avatarY + avatarSize - radius, radius, 0, 0.5 * Math.PI, false); // 右下角
  178. canvas.lineTo(avatarX + radius, avatarY + avatarSize); // 下边
  179. canvas.arc(avatarX + radius, avatarY + avatarSize - radius, radius, 0.5 * Math.PI, Math.PI, false); // 左下角
  180. canvas.lineTo(avatarX, avatarY + radius); // 左边
  181. canvas.arc(avatarX + radius, avatarY + radius, radius, Math.PI, 1.5 * Math.PI, false); // 左上角
  182. canvas.closePath(); // 关闭路径
  183. canvas.clip(); // 剪切
  184. // 绘制头像
  185. canvas.drawImage(avatarRes.path, avatarX, avatarY, avatarSize, avatarSize); // 头像位置和大小
  186. canvas.restore(); // 恢复状态
  187. // console.log('头像加载成功');
  188. // 绘制昵称和时间
  189. const nickname = this.data.sharList[0].user.nickname;
  190. const currentTime = this.data.sharList[0].currentTime;
  191. const rank = this.data.sharList[0].rank;
  192. const steps = this.data.sharList[0].steps || 0;
  193. const distance = this.data.sharList[0].distance || 0;
  194. canvas.setFontSize(this.rpxToPx(30));
  195. canvas.fillText(nickname, this.rpxToPx(140), this.rpxToPx(580)); // 昵称位置
  196. // console.log('昵称加载成功');
  197. canvas.setFontSize(this.rpxToPx(28));
  198. canvas.fillText(currentTime, this.rpxToPx(140), this.rpxToPx(610)); // 时间位置
  199. // console.log('时间加载成功');
  200. // 绘制其他文本
  201. canvas.setFontSize(this.rpxToPx(55));
  202. canvas.fillText('梦想家', this.rpxToPx(40), this.rpxToPx(720));
  203. // console.log('梦想家加载成功');
  204. canvas.setFontSize(this.rpxToPx(30));
  205. canvas.fillText('用汗水实现想象,用脚步丈量梦想', this.rpxToPx(40), this.rpxToPx(780));
  206. // console.log('汗水加载成功');
  207. // 绘制排名、步数和公里数
  208. canvas.setFontSize(this.rpxToPx(28));
  209. // canvas.fillText('今日排名: ' + rank, 20, 400);
  210. canvas.fillText('今日排名', this.rpxToPx(40), this.rpxToPx(840));
  211. // console.log('排名加载成功');
  212. // canvas.fillText('今日步数: ' + steps, 20, 420);
  213. canvas.fillText('今日步数', this.rpxToPx(240), this.rpxToPx(840));
  214. // console.log('步数加载成功');
  215. // canvas.fillText('公里数: ' + distance + 'km', 20, 430);
  216. canvas.fillText('公里数', this.rpxToPx(440), this.rpxToPx(840));
  217. // console.log('公里数加载成功');
  218. canvas.setFontSize(this.rpxToPx(40));
  219. canvas.fillText(rank, this.rpxToPx(80), this.rpxToPx(900));
  220. canvas.fillText(steps, this.rpxToPx(240), this.rpxToPx(900));
  221. canvas.fillText(distance + 'km', this.rpxToPx(444), this.rpxToPx(900));
  222. // 绘制二维码(如果有)
  223. canvas.setFillStyle('red'); // 示例二维码
  224. canvas.fillRect(width - this.rpxToPx(140), height - this.rpxToPx(160), this.rpxToPx(120), this.rpxToPx(120)); // QR 位置
  225. // console.log('二维码绘制');
  226. canvas.setFillStyle('#0178EE');
  227. canvas.setFontSize(this.rpxToPx(28));
  228. canvas.fillText('长按二维码加入跑团', width - this.rpxToPx(420), height - this.rpxToPx(120));
  229. canvas.fillText('和我一起运动', width - this.rpxToPx(380), height - this.rpxToPx(80));
  230. // 完成绘制
  231. canvas.draw(false, () => {
  232. console.log('123');
  233. wx.canvasToTempFilePath({
  234. canvasId: 'myCanvas',
  235. success: (res) => {
  236. console.log('绘制完成', res);
  237. this.setData({
  238. saveimage: res.tempFilePath
  239. });
  240. console.log(this.data.saveimage);
  241. },
  242. fail: (err) => {
  243. console.error('canvasToTempFilePath 失败', err);
  244. }
  245. });
  246. });
  247. },
  248. fail: () => {
  249. console.error('用户头像加载失败');
  250. }
  251. });
  252. },
  253. fail: () => {
  254. console.error('主图片加载失败');
  255. }
  256. });
  257. },
  258. //px
  259. // saveImage() {
  260. // // console.log('运行了');
  261. // const canvas = wx.createCanvasContext('myCanvas');
  262. // const width = 335;
  263. // const height = 550;
  264. // // console.log(width, height);
  265. // canvas.width = 335;
  266. // canvas.height = 550;
  267. // // 绘制背景
  268. // canvas.setFillStyle('#CAE1FD'); // 背景色
  269. // canvas.fillRect(0, 0, width, height);
  270. // canvas.setFillStyle('#0178EE');
  271. // // 加载主图片
  272. // wx.getImageInfo({
  273. // src: this.data.randomImage,
  274. // success: (res) => {
  275. // canvas.drawImage(res.path, 0, 0, width, 250); // 调整图片高度
  276. // // console.log('主图片加载成功');
  277. // // 加载用户头像
  278. // wx.getImageInfo({
  279. // src: this.data.sharList[0].user.avatar,
  280. // success: (avatarRes) => {
  281. // // 绘制带圆角的头像
  282. // const avatarX = 20;
  283. // const avatarY = 270;
  284. // const avatarSize = 42.5;
  285. // const radius = avatarSize / 2; // 半径为头像大小的一半
  286. // // 绘制圆角矩形
  287. // canvas.save(); // 保存当前状态
  288. // canvas.beginPath(); // 开始路径
  289. // canvas.moveTo(avatarX + radius, avatarY); // 左上角
  290. // canvas.lineTo(avatarX + avatarSize - radius, avatarY); // 上边
  291. // canvas.arc(avatarX + avatarSize - radius, avatarY + radius, radius, 1.5 * Math.PI, 0, false); // 右上角
  292. // canvas.lineTo(avatarX + avatarSize, avatarY + avatarSize - radius); // 右边
  293. // canvas.arc(avatarX + avatarSize - radius, avatarY + avatarSize - radius, radius, 0, 0.5 * Math.PI, false); // 右下角
  294. // canvas.lineTo(avatarX + radius, avatarY + avatarSize); // 下边
  295. // canvas.arc(avatarX + radius, avatarY + avatarSize - radius, radius, 0.5 * Math.PI, Math.PI, false); // 左下角
  296. // canvas.lineTo(avatarX, avatarY + radius); // 左边
  297. // canvas.arc(avatarX + radius, avatarY + radius, radius, Math.PI, 1.5 * Math.PI, false); // 左上角
  298. // canvas.closePath(); // 关闭路径
  299. // canvas.clip(); // 剪切
  300. // // 绘制头像
  301. // canvas.drawImage(avatarRes.path, avatarX, avatarY, avatarSize, avatarSize); // 头像位置和大小
  302. // canvas.restore(); // 恢复状态
  303. // // console.log('头像加载成功');
  304. // // 绘制昵称和时间
  305. // const nickname = this.data.sharList[0].user.nickname;
  306. // const currentTime = this.data.sharList[0].currentTime;
  307. // const rank = this.data.sharList[0].rank;
  308. // const steps = this.data.sharList[0].steps || 0;
  309. // const distance = this.data.sharList[0].distance || 0;
  310. // canvas.setFontSize(15);
  311. // canvas.fillText(nickname, 70, 290); // 昵称位置
  312. // // console.log('昵称加载成功');
  313. // canvas.setFontSize(14);
  314. // canvas.fillText(currentTime, 70, 305); // 时间位置
  315. // // console.log('时间加载成功');
  316. // // 绘制其他文本
  317. // canvas.setFontSize(27.5);
  318. // canvas.fillText('梦想家', 20, 360);
  319. // // console.log('梦想家加载成功');
  320. // canvas.setFontSize(15);
  321. // canvas.fillText('用汗水实现想象,用脚步丈量梦想', 20, 390);
  322. // // console.log('汗水加载成功');
  323. // // 绘制排名、步数和公里数
  324. // canvas.setFontSize(14);
  325. // // canvas.fillText('今日排名: ' + rank, 20, 400);
  326. // canvas.fillText('今日排名', 20, 420);
  327. // // console.log('排名加载成功');
  328. // // canvas.fillText('今日步数: ' + steps, 20, 420);
  329. // canvas.fillText('今日步数', 120, 420);
  330. // // console.log('步数加载成功');
  331. // // canvas.fillText('公里数: ' + distance + 'km', 20, 430);
  332. // canvas.fillText('公里数', 220, 420);
  333. // // console.log('公里数加载成功');
  334. // canvas.setFontSize(20);
  335. // canvas.fillText(rank, 40, 450);
  336. // canvas.fillText(steps, 122, 450);
  337. // canvas.fillText(distance + 'km', 222, 450);
  338. // // 绘制二维码(如果有)
  339. // canvas.setFillStyle('red'); // 示例二维码
  340. // canvas.fillRect(width - 70, height - 80, 60, 60); // QR 位置
  341. // // console.log('二维码绘制');
  342. // canvas.setFillStyle('#0178EE');
  343. // canvas.setFontSize(14);
  344. // canvas.fillText('长按二维码加入跑团', width - 210, height - 60);
  345. // canvas.fillText('和我一起运动', width - 190, height - 40);
  346. // // 完成绘制
  347. // canvas.draw(false, () => {
  348. // console.log('123');
  349. // wx.canvasToTempFilePath({
  350. // canvasId: 'myCanvas',
  351. // success: (res) => {
  352. // console.log('绘制完成', res);
  353. // this.setData({
  354. // saveimage: res.tempFilePath
  355. // });
  356. // console.log(this.data.saveimage);
  357. // },
  358. // fail: (err) => {
  359. // console.error('canvasToTempFilePath 失败', err);
  360. // }
  361. // });
  362. // });
  363. // },
  364. // fail: () => {
  365. // console.error('用户头像加载失败');
  366. // }
  367. // });
  368. // },
  369. // fail: () => {
  370. // console.error('主图片加载失败');
  371. // }
  372. // });
  373. // },
  374. //点击保存图片
  375. async savepic() {
  376. await this.saveImage()
  377. const imagePath = this.data.saveimage; // 获取保存的图片路径
  378. if (imagePath) {
  379. // 调用保存图片到相册的方法
  380. wx.saveImageToPhotosAlbum({
  381. filePath: imagePath,
  382. success: () => {
  383. wx.showToast({
  384. title: '保存成功',
  385. icon: 'success'
  386. });
  387. },
  388. fail: (err) => {
  389. console.error('保存失败', err);
  390. wx.showToast({
  391. title: '保存失败',
  392. icon: 'none'
  393. });
  394. }
  395. });
  396. } else {
  397. wx.showToast({
  398. title: '没有可保存的图片',
  399. icon: 'none'
  400. });
  401. }
  402. }
  403. })