index.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. //rpx
  142. saveImage() {
  143. // console.log('运行了');
  144. const canvas = wx.createCanvasContext('myCanvas');
  145. const width = this.rpxToPx(670);
  146. const height = this.rpxToPx(1100);
  147. // console.log(width, height);
  148. canvas.width = this.rpxToPx(670);
  149. canvas.height = this.rpxToPx(1100);
  150. // 绘制背景
  151. canvas.setFillStyle('#CAE1FD'); // 背景色
  152. canvas.fillRect(0, 0, width, height);
  153. canvas.setFillStyle('#0178EE');
  154. // 加载主图片
  155. wx.getImageInfo({
  156. src: this.data.randomImage,
  157. success: (res) => {
  158. canvas.drawImage(res.path, 0, 0, width, this.rpxToPx(500)); // 调整图片高度
  159. // console.log('主图片加载成功');
  160. // 加载用户头像
  161. wx.getImageInfo({
  162. src: this.data.sharList[0].user.avatar,
  163. success: (avatarRes) => {
  164. // 绘制带圆角的头像
  165. const avatarX = this.rpxToPx(40);
  166. const avatarY = this.rpxToPx(540);
  167. const avatarSize = this.rpxToPx(83);
  168. const radius = avatarSize / 2; // 半径为头像大小的一半
  169. // 绘制圆角矩形
  170. canvas.save(); // 保存当前状态
  171. canvas.beginPath(); // 开始路径
  172. canvas.moveTo(avatarX + radius, avatarY); // 左上角
  173. canvas.lineTo(avatarX + avatarSize - radius, avatarY); // 上边
  174. canvas.arc(avatarX + avatarSize - radius, avatarY + radius, radius, 1.5 * Math.PI, 0, false); // 右上角
  175. canvas.lineTo(avatarX + avatarSize, avatarY + avatarSize - radius); // 右边
  176. canvas.arc(avatarX + avatarSize - radius, avatarY + avatarSize - radius, radius, 0, 0.5 * Math.PI, false); // 右下角
  177. canvas.lineTo(avatarX + radius, avatarY + avatarSize); // 下边
  178. canvas.arc(avatarX + radius, avatarY + avatarSize - radius, radius, 0.5 * Math.PI, Math.PI, false); // 左下角
  179. canvas.lineTo(avatarX, avatarY + radius); // 左边
  180. canvas.arc(avatarX + radius, avatarY + radius, radius, Math.PI, 1.5 * Math.PI, false); // 左上角
  181. canvas.closePath(); // 关闭路径
  182. canvas.clip(); // 剪切
  183. // 绘制头像
  184. canvas.drawImage(avatarRes.path, avatarX, avatarY, avatarSize, avatarSize); // 头像位置和大小
  185. canvas.restore(); // 恢复状态
  186. // console.log('头像加载成功');
  187. // 绘制昵称和时间
  188. const nickname = this.data.sharList[0].user.nickname;
  189. const currentTime = this.data.sharList[0].currentTime;
  190. const rank = this.data.sharList[0].rank;
  191. const steps = this.data.sharList[0].steps || 0;
  192. const distance = this.data.sharList[0].distance || 0;
  193. canvas.setFontSize(this.rpxToPx(30));
  194. canvas.fillText(nickname, this.rpxToPx(140), this.rpxToPx(580)); // 昵称位置
  195. // console.log('昵称加载成功');
  196. canvas.setFontSize(this.rpxToPx(28));
  197. canvas.fillText(currentTime, this.rpxToPx(140), this.rpxToPx(610)); // 时间位置
  198. // console.log('时间加载成功');
  199. // 绘制其他文本
  200. canvas.setFontSize(this.rpxToPx(55));
  201. canvas.fillText('梦想家', this.rpxToPx(40), this.rpxToPx(720));
  202. // console.log('梦想家加载成功');
  203. canvas.setFontSize(this.rpxToPx(30));
  204. canvas.fillText('用汗水实现想象,用脚步丈量梦想', this.rpxToPx(40), this.rpxToPx(780));
  205. // console.log('汗水加载成功');
  206. // 绘制排名、步数和公里数
  207. canvas.setFontSize(this.rpxToPx(28));
  208. // canvas.fillText('今日排名: ' + rank, 20, 400);
  209. canvas.fillText('今日排名', this.rpxToPx(40), this.rpxToPx(840));
  210. // console.log('排名加载成功');
  211. // canvas.fillText('今日步数: ' + steps, 20, 420);
  212. canvas.fillText('今日步数', this.rpxToPx(240), this.rpxToPx(840));
  213. // console.log('步数加载成功');
  214. // canvas.fillText('公里数: ' + distance + 'km', 20, 430);
  215. canvas.fillText('公里数', this.rpxToPx(440), this.rpxToPx(840));
  216. // console.log('公里数加载成功');
  217. canvas.setFontSize(this.rpxToPx(40));
  218. canvas.fillText(rank, this.rpxToPx(80), this.rpxToPx(900));
  219. canvas.fillText(steps, this.rpxToPx(240), this.rpxToPx(900));
  220. canvas.fillText(distance + 'km', this.rpxToPx(444), this.rpxToPx(900));
  221. // 绘制二维码(如果有)
  222. canvas.setFillStyle('red'); // 示例二维码
  223. canvas.fillRect(width - this.rpxToPx(140), height - this.rpxToPx(160), this.rpxToPx(120), this.rpxToPx(120)); // QR 位置
  224. // console.log('二维码绘制');
  225. canvas.setFillStyle('#0178EE');
  226. canvas.setFontSize(this.rpxToPx(28));
  227. canvas.fillText('长按二维码加入跑团', width - this.rpxToPx(420), height - this.rpxToPx(120));
  228. canvas.fillText('和我一起运动', width - this.rpxToPx(380), height - this.rpxToPx(80));
  229. // 完成绘制
  230. canvas.draw(false, () => {
  231. console.log('123');
  232. wx.canvasToTempFilePath({
  233. canvasId: 'myCanvas',
  234. success: (res) => {
  235. console.log('绘制完成', res);
  236. this.setData({
  237. saveimage: res.tempFilePath
  238. });
  239. console.log(this.data.saveimage);
  240. },
  241. fail: (err) => {
  242. console.error('canvasToTempFilePath 失败', err);
  243. }
  244. });
  245. });
  246. },
  247. fail: (err) => {
  248. console.error('用户头像加载失败', this.data.sharList[0].user.avatar, err);
  249. }
  250. });
  251. },
  252. fail: () => {
  253. console.error('主图片加载失败');
  254. }
  255. });
  256. },
  257. //px
  258. // saveImage() {
  259. // // console.log('运行了');
  260. // const canvas = wx.createCanvasContext('myCanvas');
  261. // const width = 335;
  262. // const height = 550;
  263. // // console.log(width, height);
  264. // canvas.width = 335;
  265. // canvas.height = 550;
  266. // // 绘制背景
  267. // canvas.setFillStyle('#CAE1FD'); // 背景色
  268. // canvas.fillRect(0, 0, width, height);
  269. // canvas.setFillStyle('#0178EE');
  270. // // 加载主图片
  271. // wx.getImageInfo({
  272. // src: this.data.randomImage,
  273. // success: (res) => {
  274. // canvas.drawImage(res.path, 0, 0, width, 250); // 调整图片高度
  275. // // console.log('主图片加载成功');
  276. // // 加载用户头像
  277. // wx.getImageInfo({
  278. // src: this.data.sharList[0].user.avatar,
  279. // success: (avatarRes) => {
  280. // // 绘制带圆角的头像
  281. // const avatarX = 20;
  282. // const avatarY = 270;
  283. // const avatarSize = 42.5;
  284. // const radius = avatarSize / 2; // 半径为头像大小的一半
  285. // // 绘制圆角矩形
  286. // canvas.save(); // 保存当前状态
  287. // canvas.beginPath(); // 开始路径
  288. // canvas.moveTo(avatarX + radius, avatarY); // 左上角
  289. // canvas.lineTo(avatarX + avatarSize - radius, avatarY); // 上边
  290. // canvas.arc(avatarX + avatarSize - radius, avatarY + radius, radius, 1.5 * Math.PI, 0, false); // 右上角
  291. // canvas.lineTo(avatarX + avatarSize, avatarY + avatarSize - radius); // 右边
  292. // canvas.arc(avatarX + avatarSize - radius, avatarY + avatarSize - radius, radius, 0, 0.5 * Math.PI, false); // 右下角
  293. // canvas.lineTo(avatarX + radius, avatarY + avatarSize); // 下边
  294. // canvas.arc(avatarX + radius, avatarY + avatarSize - radius, radius, 0.5 * Math.PI, Math.PI, false); // 左下角
  295. // canvas.lineTo(avatarX, avatarY + radius); // 左边
  296. // canvas.arc(avatarX + radius, avatarY + radius, radius, Math.PI, 1.5 * Math.PI, false); // 左上角
  297. // canvas.closePath(); // 关闭路径
  298. // canvas.clip(); // 剪切
  299. // // 绘制头像
  300. // canvas.drawImage(avatarRes.path, avatarX, avatarY, avatarSize, avatarSize); // 头像位置和大小
  301. // canvas.restore(); // 恢复状态
  302. // // console.log('头像加载成功');
  303. // // 绘制昵称和时间
  304. // const nickname = this.data.sharList[0].user.nickname;
  305. // const currentTime = this.data.sharList[0].currentTime;
  306. // const rank = this.data.sharList[0].rank;
  307. // const steps = this.data.sharList[0].steps || 0;
  308. // const distance = this.data.sharList[0].distance || 0;
  309. // canvas.setFontSize(15);
  310. // canvas.fillText(nickname, 70, 290); // 昵称位置
  311. // // console.log('昵称加载成功');
  312. // canvas.setFontSize(14);
  313. // canvas.fillText(currentTime, 70, 305); // 时间位置
  314. // // console.log('时间加载成功');
  315. // // 绘制其他文本
  316. // canvas.setFontSize(27.5);
  317. // canvas.fillText('梦想家', 20, 360);
  318. // // console.log('梦想家加载成功');
  319. // canvas.setFontSize(15);
  320. // canvas.fillText('用汗水实现想象,用脚步丈量梦想', 20, 390);
  321. // // console.log('汗水加载成功');
  322. // // 绘制排名、步数和公里数
  323. // canvas.setFontSize(14);
  324. // // canvas.fillText('今日排名: ' + rank, 20, 400);
  325. // canvas.fillText('今日排名', 20, 420);
  326. // // console.log('排名加载成功');
  327. // // canvas.fillText('今日步数: ' + steps, 20, 420);
  328. // canvas.fillText('今日步数', 120, 420);
  329. // // console.log('步数加载成功');
  330. // // canvas.fillText('公里数: ' + distance + 'km', 20, 430);
  331. // canvas.fillText('公里数', 220, 420);
  332. // // console.log('公里数加载成功');
  333. // canvas.setFontSize(20);
  334. // canvas.fillText(rank, 40, 450);
  335. // canvas.fillText(steps, 122, 450);
  336. // canvas.fillText(distance + 'km', 222, 450);
  337. // // 绘制二维码(如果有)
  338. // canvas.setFillStyle('red'); // 示例二维码
  339. // canvas.fillRect(width - 70, height - 80, 60, 60); // QR 位置
  340. // // console.log('二维码绘制');
  341. // canvas.setFillStyle('#0178EE');
  342. // canvas.setFontSize(14);
  343. // canvas.fillText('长按二维码加入跑团', width - 210, height - 60);
  344. // canvas.fillText('和我一起运动', width - 190, height - 40);
  345. // // 完成绘制
  346. // canvas.draw(false, () => {
  347. // console.log('123');
  348. // wx.canvasToTempFilePath({
  349. // canvasId: 'myCanvas',
  350. // success: (res) => {
  351. // console.log('绘制完成', res);
  352. // this.setData({
  353. // saveimage: res.tempFilePath
  354. // });
  355. // console.log(this.data.saveimage);
  356. // },
  357. // fail: (err) => {
  358. // console.error('canvasToTempFilePath 失败', err);
  359. // }
  360. // });
  361. // });
  362. // },
  363. // fail: () => {
  364. // console.error('用户头像加载失败');
  365. // }
  366. // });
  367. // },
  368. // fail: () => {
  369. // console.error('主图片加载失败');
  370. // }
  371. // });
  372. // },
  373. //点击保存图片
  374. async savepic() {
  375. await this.saveImage()
  376. const imagePath = this.data.saveimage; // 获取保存的图片路径
  377. if (imagePath) {
  378. // 调用保存图片到相册的方法
  379. wx.saveImageToPhotosAlbum({
  380. filePath: imagePath,
  381. success: () => {
  382. wx.showToast({
  383. title: '保存成功',
  384. icon: 'success'
  385. });
  386. },
  387. fail: (err) => {
  388. console.error('保存失败', err);
  389. wx.showToast({
  390. title: '保存失败',
  391. icon: 'none'
  392. });
  393. }
  394. });
  395. } else {
  396. wx.showToast({
  397. title: '没有可保存的图片',
  398. icon: 'none'
  399. });
  400. }
  401. },
  402. savepic2() {
  403. const that = this
  404. let fileName = new Date().valueOf();
  405. let filePath = wx.env.USER_DATA_PATH + '/' + fileName + '.jpg' //这边就是为了安卓做的兼容,因为安卓机有可能会将图片地址的后缀名读取为:unknow
  406. wx.downloadFile({
  407. url: that.data.saveimage, //需要保存的图片地址
  408. filePath: filePath,
  409. success: function (res) {
  410. // 保存图片到系统相册
  411. wx.saveImageToPhotosAlbum({
  412. filePath: filePath,
  413. success(data) {
  414. let fileMgr = wx.getFileSystemManager()
  415. fileMgr.unlink({
  416. filePath: filePath,
  417. success() {
  418. wx.showToast({
  419. title: '已保存至您的相册',
  420. icon: 'none',
  421. duration: 1500
  422. });
  423. }
  424. })
  425. },
  426. fail(err) {
  427. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
  428. wx.showModal({
  429. title: '提示',
  430. content: '需要您授权保存相册',
  431. showCancel: false,
  432. success: modalSuccess => {
  433. wx.openSetting({
  434. success(settingdata) {
  435. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  436. console.log('获取权限成功,给出再次点击图片保存到相册的提示。')
  437. } else {
  438. console.log('获取权限失败,给出不给权限就无法正常使用的提示')
  439. }
  440. }
  441. })
  442. }
  443. })
  444. }
  445. }
  446. })
  447. },
  448. fail: function (res) {}
  449. })
  450. },
  451. shar() {
  452. wx.downloadFile({
  453. url: this.data.saveimage, //图片服务器地址
  454. success: (res) => {
  455. wx.showShareImageMenu({
  456. path: res.tempFilePath, //转为本地地址showShareImageMenu进行分享
  457. success: (res) => {
  458. uni.hideLoading();
  459. this.modalShare = null;
  460. },
  461. fail: (err) => {
  462. uni.hideLoading();
  463. this.modalShare = null;
  464. },
  465. });
  466. },
  467. });
  468. },
  469. sharecircle(){
  470. wx.navigateTo({
  471. url: '../../circle/send-circle/index?image='+this.data.saveimage // 目标页面的路径
  472. });
  473. }
  474. })