index.js 23 KB

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