index.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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://file-cloud.fmode.cn/qpFbRRSZrO/20250102/gtontq125800552.jpg?imageView2/1/w/200/h/200',
  20. 'https://file-cloud.fmode.cn/qpFbRRSZrO/20250102/8e1ps0125800358.jpg?imageView2/1/w/200/h/200',
  21. 'https://file-cloud.fmode.cn/qpFbRRSZrO/20250102/l5321n125800284.jpg?imageView2/1/w/200/h/200',
  22. 'https://file-cloud.fmode.cn/qpFbRRSZrO/20250102/j1p6l7125757884.jpg?imageView2/1/w/200/h/200'
  23. ],
  24. randomImage: '',
  25. saveimage: ''
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad: function (options) {
  31. // 计算
  32. const systemInfo = wx.getSystemInfoSync();
  33. const statusBarHeight = systemInfo.statusBarHeight || 0;
  34. const screenHeight = systemInfo.screenHeight || 0;
  35. const custom = wx.getMenuButtonBoundingClientRect();
  36. const customHeight = custom.height + 10 + 2 || 0;
  37. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  38. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  39. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  40. this.setData({
  41. statusBarHeight,
  42. screenHeight,
  43. customHeight,
  44. bottomNavHeight,
  45. contentpadding,
  46. contentHeight
  47. });
  48. this.order()
  49. this.showRandomImage()
  50. },
  51. /**
  52. * 生命周期函数--监听页面初次渲染完成
  53. */
  54. onReady: function () {
  55. },
  56. /**
  57. * 生命周期函数--监听页面显示
  58. */
  59. onShow: function () {
  60. },
  61. /**
  62. * 生命周期函数--监听页面隐藏
  63. */
  64. onHide: function () {
  65. },
  66. /**
  67. * 生命周期函数--监听页面卸载
  68. */
  69. onUnload: function () {
  70. },
  71. /**
  72. * 页面相关事件处理函数--监听用户下拉动作
  73. */
  74. onPullDownRefresh: function () {
  75. },
  76. /**
  77. * 页面上拉触底事件的处理函数
  78. */
  79. onReachBottom: function () {
  80. },
  81. /**
  82. * 用户点击右上角分享
  83. */
  84. onShareAppMessage: function () {
  85. },
  86. //获取当天运动数据
  87. async order() {
  88. const currentUser = Parse.User.current();
  89. let ActivityDataquery = new Parse.Query('ActivityData');
  90. // ActivityDataquery.equalTo('user', currentUser.id);
  91. ActivityDataquery.equalTo('company', company);
  92. ActivityDataquery.equalTo('type', 'today');
  93. ActivityDataquery.notEqualTo('isDeleted', true);
  94. // 获取今天的日期
  95. const today = new Date();
  96. const todayStart = new Date(today.getFullYear(), today.getMonth(), today.getDate()); // 今天的开始时间
  97. const todayEnd = new Date(todayStart);
  98. todayEnd.setHours(23, 59, 59, 999); // 今天的结束时间
  99. // 在查询条件中添加对 createdAt 的限制
  100. ActivityDataquery.greaterThanOrEqualTo('createdAt', todayStart);
  101. ActivityDataquery.lessThanOrEqualTo('createdAt', todayEnd);
  102. ActivityDataquery.include('user');
  103. // 根据 steps 字段进行降序排序
  104. ActivityDataquery.descending('steps');
  105. let r = await ActivityDataquery.find();
  106. let todayList = r.map(item => {
  107. let itemData = item.toJSON();
  108. // 获取当前时间并格式化
  109. const now = new Date();
  110. 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')}`;
  111. itemData.currentTime = formattedTime; // 将当前时间添加到 item 中
  112. return itemData;
  113. });
  114. let sharList = []
  115. todayList.forEach((item, index) => {
  116. if (item.user.objectId == currentUser.id) {
  117. sharList.push({
  118. ...item, // 包含用户数据
  119. rank: index + 1 // 计算排名(index 从 0 开始,所以加 1)
  120. });
  121. }
  122. })
  123. this.setData({
  124. sharList
  125. });
  126. this.saveImage()
  127. console.log(this.data.sharList);
  128. },
  129. //随机展示图片
  130. showRandomImage: function () {
  131. const randomIndex = Math.floor(Math.random() * this.data.images.length);
  132. this.setData({
  133. randomImage: this.data.images[randomIndex]
  134. });
  135. },
  136. //绘制canvas
  137. rpxToPx(rpx) {
  138. const systemInfo = wx.getSystemInfoSync();
  139. return rpx * (systemInfo.windowWidth / 750);
  140. },
  141. getImageInfo(url) {
  142. console.log(url);
  143. return new Promise((result) => {
  144. wx.getImageInfo({
  145. src: url, //服务器返回的图片地址
  146. success: function (res) {
  147. // res.path是网络图片的本地地址
  148. let Path = res.path;
  149. result(Path)
  150. },
  151. fail(err) {
  152. console.log(err);
  153. result()
  154. }
  155. });
  156. })
  157. },
  158. //rpx
  159. async saveImage() {
  160. // console.log('运行了');
  161. const canvas = wx.createCanvasContext('myCanvas');
  162. const width = this.rpxToPx(670);
  163. const height = this.rpxToPx(1100);
  164. // console.log(width, height);
  165. canvas.width = this.rpxToPx(670);
  166. canvas.height = this.rpxToPx(1100);
  167. // 绘制背景
  168. canvas.setFillStyle('#CAE1FD'); // 背景色
  169. canvas.fillRect(0, 0, width, height);
  170. canvas.setFillStyle('#0178EE');
  171. let mainimage = await this.getImageInfo(this.data.randomImage)
  172. let avatar = Parse.User.current()?.get('avatar')
  173. console.log('avatar', avatar);
  174. let userAvatar = await this.getImageInfo(avatar)
  175. console.log('userAvatar', userAvatar);
  176. //主图片
  177. canvas.drawImage(mainimage, 0, 0, width, this.rpxToPx(500)); // 调整图片高度
  178. //头像
  179. if (userAvatar) {
  180. // 绘制带圆角的头像
  181. const avatarX = this.rpxToPx(40);
  182. const avatarY = this.rpxToPx(540);
  183. const avatarSize = this.rpxToPx(83);
  184. const radius = avatarSize / 2; // 半径为头像大小的一半
  185. // 绘制圆角矩形
  186. canvas.save(); // 保存当前状态
  187. canvas.beginPath(); // 开始路径
  188. canvas.moveTo(avatarX + radius, avatarY); // 左上角
  189. canvas.lineTo(avatarX + avatarSize - radius, avatarY); // 上边
  190. canvas.arc(avatarX + avatarSize - radius, avatarY + radius, radius, 1.5 * Math.PI, 0, false); // 右上角
  191. canvas.lineTo(avatarX + avatarSize, avatarY + avatarSize - radius); // 右边
  192. canvas.arc(avatarX + avatarSize - radius, avatarY + avatarSize - radius, radius, 0, 0.5 * Math.PI, false); // 右下角
  193. canvas.lineTo(avatarX + radius, avatarY + avatarSize); // 下边
  194. canvas.arc(avatarX + radius, avatarY + avatarSize - radius, radius, 0.5 * Math.PI, Math.PI, false); // 左下角
  195. canvas.lineTo(avatarX, avatarY + radius); // 左边
  196. canvas.arc(avatarX + radius, avatarY + radius, radius, Math.PI, 1.5 * Math.PI, false); // 左上角
  197. canvas.closePath(); // 关闭路径
  198. canvas.clip(); // 剪切
  199. // 绘制头像
  200. canvas.drawImage(userAvatar, avatarX, avatarY, avatarSize, avatarSize); // 头像位置和大小
  201. canvas.restore(); // 恢复状态
  202. }
  203. // 绘制昵称和时间
  204. const nickname = this.data.sharList[0].user.nickname;
  205. const currentTime = this.data.sharList[0].currentTime;
  206. const rank = this.data.sharList[0].rank;
  207. const steps = this.data.sharList[0].steps || 0;
  208. const distance = this.data.sharList[0].distance || 0;
  209. canvas.setFontSize(this.rpxToPx(30));
  210. canvas.fillText(nickname, this.rpxToPx(140), this.rpxToPx(580)); // 昵称位置
  211. // console.log('昵称加载成功');
  212. canvas.setFontSize(this.rpxToPx(28));
  213. canvas.fillText(currentTime, this.rpxToPx(140), this.rpxToPx(610)); // 时间位置
  214. // console.log('时间加载成功');
  215. // 绘制其他文本
  216. canvas.setFontSize(this.rpxToPx(55));
  217. canvas.fillText('梦想家', this.rpxToPx(40), this.rpxToPx(720));
  218. // console.log('梦想家加载成功');
  219. canvas.setFontSize(this.rpxToPx(30));
  220. canvas.fillText('用汗水实现想象,用脚步丈量梦想', this.rpxToPx(40), this.rpxToPx(780));
  221. // console.log('汗水加载成功');
  222. // 绘制排名、步数和公里数
  223. canvas.setFontSize(this.rpxToPx(28));
  224. // canvas.fillText('今日排名: ' + rank, 20, 400);
  225. canvas.fillText('今日排名', this.rpxToPx(40), this.rpxToPx(840));
  226. // console.log('排名加载成功');
  227. // canvas.fillText('今日步数: ' + steps, 20, 420);
  228. canvas.fillText('今日步数', this.rpxToPx(240), this.rpxToPx(840));
  229. // console.log('步数加载成功');
  230. // canvas.fillText('公里数: ' + distance + 'km', 20, 430);
  231. canvas.fillText('公里数', this.rpxToPx(440), this.rpxToPx(840));
  232. // console.log('公里数加载成功');
  233. canvas.setFontSize(this.rpxToPx(40));
  234. canvas.fillText(rank, this.rpxToPx(80), this.rpxToPx(900));
  235. canvas.fillText(steps, this.rpxToPx(240), this.rpxToPx(900));
  236. canvas.fillText(distance + 'km', this.rpxToPx(444), this.rpxToPx(900));
  237. // 绘制二维码(如果有)
  238. canvas.setFillStyle('red'); // 示例二维码
  239. canvas.fillRect(width - this.rpxToPx(140), height - this.rpxToPx(160), this.rpxToPx(120), this.rpxToPx(120)); // QR 位置
  240. // console.log('二维码绘制');
  241. canvas.setFillStyle('#0178EE');
  242. canvas.setFontSize(this.rpxToPx(28));
  243. canvas.fillText('长按二维码加入跑团', width - this.rpxToPx(420), height - this.rpxToPx(120));
  244. canvas.fillText('和我一起运动', width - this.rpxToPx(380), height - this.rpxToPx(80));
  245. // 完成绘制
  246. canvas.draw(false, () => {
  247. console.log('123');
  248. wx.canvasToTempFilePath({
  249. canvasId: 'myCanvas',
  250. success: (res) => {
  251. console.log('绘制完成', res);
  252. this.setData({
  253. saveimage: res.tempFilePath
  254. });
  255. console.log(this.data.saveimage);
  256. },
  257. fail: (err) => {
  258. console.error('canvasToTempFilePath 失败', err);
  259. }
  260. });
  261. });
  262. },
  263. //px
  264. // saveImage() {
  265. // // console.log('运行了');
  266. // const canvas = wx.createCanvasContext('myCanvas');
  267. // const width = 335;
  268. // const height = 550;
  269. // // console.log(width, height);
  270. // canvas.width = 335;
  271. // canvas.height = 550;
  272. // // 绘制背景
  273. // canvas.setFillStyle('#CAE1FD'); // 背景色
  274. // canvas.fillRect(0, 0, width, height);
  275. // canvas.setFillStyle('#0178EE');
  276. // // 加载主图片
  277. // wx.getImageInfo({
  278. // src: this.data.randomImage,
  279. // success: (res) => {
  280. // canvas.drawImage(res.path, 0, 0, width, 250); // 调整图片高度
  281. // // console.log('主图片加载成功');
  282. // // 加载用户头像
  283. // wx.getImageInfo({
  284. // src: this.data.sharList[0].user.avatar,
  285. // success: (avatarRes) => {
  286. // // 绘制带圆角的头像
  287. // const avatarX = 20;
  288. // const avatarY = 270;
  289. // const avatarSize = 42.5;
  290. // const radius = avatarSize / 2; // 半径为头像大小的一半
  291. // // 绘制圆角矩形
  292. // canvas.save(); // 保存当前状态
  293. // canvas.beginPath(); // 开始路径
  294. // canvas.moveTo(avatarX + radius, avatarY); // 左上角
  295. // canvas.lineTo(avatarX + avatarSize - radius, avatarY); // 上边
  296. // canvas.arc(avatarX + avatarSize - radius, avatarY + radius, radius, 1.5 * Math.PI, 0, false); // 右上角
  297. // canvas.lineTo(avatarX + avatarSize, avatarY + avatarSize - radius); // 右边
  298. // canvas.arc(avatarX + avatarSize - radius, avatarY + avatarSize - radius, radius, 0, 0.5 * Math.PI, false); // 右下角
  299. // canvas.lineTo(avatarX + radius, avatarY + avatarSize); // 下边
  300. // canvas.arc(avatarX + radius, avatarY + avatarSize - radius, radius, 0.5 * Math.PI, Math.PI, false); // 左下角
  301. // canvas.lineTo(avatarX, avatarY + radius); // 左边
  302. // canvas.arc(avatarX + radius, avatarY + radius, radius, Math.PI, 1.5 * Math.PI, false); // 左上角
  303. // canvas.closePath(); // 关闭路径
  304. // canvas.clip(); // 剪切
  305. // // 绘制头像
  306. // canvas.drawImage(avatarRes.path, avatarX, avatarY, avatarSize, avatarSize); // 头像位置和大小
  307. // canvas.restore(); // 恢复状态
  308. // // console.log('头像加载成功');
  309. // // 绘制昵称和时间
  310. // const nickname = this.data.sharList[0].user.nickname;
  311. // const currentTime = this.data.sharList[0].currentTime;
  312. // const rank = this.data.sharList[0].rank;
  313. // const steps = this.data.sharList[0].steps || 0;
  314. // const distance = this.data.sharList[0].distance || 0;
  315. // canvas.setFontSize(15);
  316. // canvas.fillText(nickname, 70, 290); // 昵称位置
  317. // // console.log('昵称加载成功');
  318. // canvas.setFontSize(14);
  319. // canvas.fillText(currentTime, 70, 305); // 时间位置
  320. // // console.log('时间加载成功');
  321. // // 绘制其他文本
  322. // canvas.setFontSize(27.5);
  323. // canvas.fillText('梦想家', 20, 360);
  324. // // console.log('梦想家加载成功');
  325. // canvas.setFontSize(15);
  326. // canvas.fillText('用汗水实现想象,用脚步丈量梦想', 20, 390);
  327. // // console.log('汗水加载成功');
  328. // // 绘制排名、步数和公里数
  329. // canvas.setFontSize(14);
  330. // // canvas.fillText('今日排名: ' + rank, 20, 400);
  331. // canvas.fillText('今日排名', 20, 420);
  332. // // console.log('排名加载成功');
  333. // // canvas.fillText('今日步数: ' + steps, 20, 420);
  334. // canvas.fillText('今日步数', 120, 420);
  335. // // console.log('步数加载成功');
  336. // // canvas.fillText('公里数: ' + distance + 'km', 20, 430);
  337. // canvas.fillText('公里数', 220, 420);
  338. // // console.log('公里数加载成功');
  339. // canvas.setFontSize(20);
  340. // canvas.fillText(rank, 40, 450);
  341. // canvas.fillText(steps, 122, 450);
  342. // canvas.fillText(distance + 'km', 222, 450);
  343. // // 绘制二维码(如果有)
  344. // canvas.setFillStyle('red'); // 示例二维码
  345. // canvas.fillRect(width - 70, height - 80, 60, 60); // QR 位置
  346. // // console.log('二维码绘制');
  347. // canvas.setFillStyle('#0178EE');
  348. // canvas.setFontSize(14);
  349. // canvas.fillText('长按二维码加入跑团', width - 210, height - 60);
  350. // canvas.fillText('和我一起运动', width - 190, height - 40);
  351. // // 完成绘制
  352. // canvas.draw(false, () => {
  353. // console.log('123');
  354. // wx.canvasToTempFilePath({
  355. // canvasId: 'myCanvas',
  356. // success: (res) => {
  357. // console.log('绘制完成', res);
  358. // this.setData({
  359. // saveimage: res.tempFilePath
  360. // });
  361. // console.log(this.data.saveimage);
  362. // },
  363. // fail: (err) => {
  364. // console.error('canvasToTempFilePath 失败', err);
  365. // }
  366. // });
  367. // });
  368. // },
  369. // fail: () => {
  370. // console.error('用户头像加载失败');
  371. // }
  372. // });
  373. // },
  374. // fail: () => {
  375. // console.error('主图片加载失败');
  376. // }
  377. // });
  378. // },
  379. //点击保存图片
  380. async savepic() {
  381. await this.saveImage()
  382. const imagePath = this.data.saveimage; // 获取保存的图片路径
  383. if (imagePath) {
  384. // 调用保存图片到相册的方法
  385. wx.saveImageToPhotosAlbum({
  386. filePath: imagePath,
  387. success: () => {
  388. wx.showToast({
  389. title: '图片已保存到相册',
  390. icon: 'success'
  391. });
  392. },
  393. fail: (err) => {
  394. console.error('保存失败', err);
  395. wx.showToast({
  396. title: '保存失败',
  397. icon: 'none'
  398. });
  399. }
  400. });
  401. } else {
  402. wx.showToast({
  403. title: '没有可保存的图片',
  404. icon: 'none'
  405. });
  406. }
  407. },
  408. //保存图片到相册
  409. savepic2: function () {
  410. let that = this;
  411. wx.canvasToTempFilePath({
  412. x: 0,
  413. y: 0,
  414. width: 670,
  415. height: 1100,
  416. destWidth: 670,
  417. destHeight: 1100,
  418. canvasId: 'myCanvas',
  419. success: function (result) {
  420. wx.getSetting({
  421. success(res) {
  422. if (!res.authSetting['scope.writePhotosAlbum']) {
  423. wx.authorize({
  424. scope: 'scope.writePhotosAlbum',
  425. success() {
  426. //这里是用户同意授权后的回调
  427. // that.saveImgToLocal();
  428. wx.saveImageToPhotosAlbum({
  429. filePath: result.tempFilePath,
  430. success(res) {
  431. wx.showToast({
  432. title: '图片已保存到相册',
  433. icon: 'success'
  434. });
  435. }
  436. })
  437. },
  438. fail() { //这里是用户拒绝授权后的回调
  439. wx.showToast({
  440. title: '打开相册授权,才能保存图片哦~',
  441. icon: 'none',
  442. duration: 2000
  443. })
  444. that.setData({
  445. openSettingBtnHidden: false
  446. })
  447. }
  448. })
  449. } else {
  450. //调取小程序当中获取图片
  451. console.log(result.tempFilePath);
  452. wx.saveImageToPhotosAlbum({
  453. filePath: result.tempFilePath,
  454. success(res) {
  455. wx.showToast({
  456. title: '图片已保存到相册',
  457. icon: 'success'
  458. });
  459. }
  460. })
  461. }
  462. }
  463. })
  464. },
  465. fail: function (res) {
  466. console.log(res)
  467. }
  468. })
  469. },
  470. shar() {
  471. wx.downloadFile({
  472. url: this.data.saveimage, //图片服务器地址
  473. success: (res) => {
  474. wx.showShareImageMenu({
  475. path: res.tempFilePath, //转为本地地址showShareImageMenu进行分享
  476. success: (res) => {
  477. uni.hideLoading();
  478. this.modalShare = null;
  479. },
  480. fail: (err) => {
  481. uni.hideLoading();
  482. this.modalShare = null;
  483. },
  484. });
  485. },
  486. });
  487. },
  488. sharecircle() {
  489. wx.navigateTo({
  490. url: '../../circle/send-circle/index?image=' + this.data.saveimage // 目标页面的路径
  491. });
  492. }
  493. })