index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. // nova-werun/pages/home/sport/sport-start/index.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. //屏幕高度
  8. statusBarHeight: 0, // 状态栏高度
  9. screenHeight: 0, // 屏幕高度
  10. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  11. bottomNavHeight: 0, // 底部导航栏高度
  12. contentHeight: 0, // 可用内容高度
  13. contentHeight2: 0,
  14. contentpadding: 0, //顶部padding高度
  15. navheight: 0,
  16. //地图
  17. longitude: 0,
  18. latitude: 0,
  19. markers: [],
  20. //是否暂停
  21. isstop: false,
  22. //标题
  23. title: '',
  24. percentage: '',
  25. timer: null,
  26. startTime: 0,
  27. show: false,
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. onLoad: function (options) {
  33. // 计算
  34. const systemInfo = wx.getSystemInfoSync();
  35. const statusBarHeight = systemInfo.statusBarHeight || 0;
  36. const screenHeight = systemInfo.screenHeight || 0;
  37. const custom = wx.getMenuButtonBoundingClientRect();
  38. const customHeight = custom.height + 10 + 2 || 0;
  39. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  40. const navheight = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  41. const contentHeight = (screenHeight - bottomNavHeight) * 750 / systemInfo.windowWidth;
  42. this.setData({
  43. statusBarHeight,
  44. screenHeight,
  45. customHeight,
  46. bottomNavHeight,
  47. contentHeight,
  48. navheight,
  49. });
  50. console.log(navheight);
  51. this.setData({
  52. title: options.id
  53. })
  54. //地图
  55. this.Getlocation()
  56. },
  57. /**
  58. * 生命周期函数--监听页面初次渲染完成
  59. */
  60. onReady: function () {
  61. },
  62. /**
  63. * 生命周期函数--监听页面显示
  64. */
  65. onShow: function () {
  66. },
  67. /**
  68. * 生命周期函数--监听页面隐藏
  69. */
  70. onHide: function () {
  71. },
  72. /**
  73. * 生命周期函数--监听页面卸载
  74. */
  75. onUnload: function () {
  76. },
  77. /**
  78. * 页面相关事件处理函数--监听用户下拉动作
  79. */
  80. onPullDownRefresh: function () {
  81. },
  82. /**
  83. * 页面上拉触底事件的处理函数
  84. */
  85. onReachBottom: function () {
  86. },
  87. /**
  88. * 用户点击右上角分享
  89. */
  90. onShareAppMessage: function () {
  91. },
  92. //获取当前位置信息
  93. Getlocation() {
  94. // 获取当前位置信息
  95. wx.getLocation({
  96. type: 'wgs84',
  97. success: (res) => {
  98. const {
  99. latitude,
  100. longitude
  101. } = res;
  102. console.log('获取到的经纬度:', latitude, longitude); // 添加日志
  103. //调用api解析地址
  104. wx.request({
  105. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  106. data: {},
  107. header: {
  108. 'Content-Type': 'application/json'
  109. },
  110. success: (ops) => { // 使用箭头函数
  111. console.log(ops);
  112. const address = ops.data.result.formatted_address;
  113. this.setData({
  114. address: address,
  115. latitude: latitude, // 保证 latitude 被设置
  116. longitude: longitude, // 保证 longitude 被设置
  117. markers: [{ // 设置 markers
  118. id: 1,
  119. latitude: latitude,
  120. longitude: longitude,
  121. iconPath: 'https://file-cloud.fmode.cn/13WZ0W7u3l/20240724/7ebg0k104325941.png?imageView2/1/w/200/h/200', // 自定义标记图标
  122. width: 40,
  123. height: 40,
  124. callout: {
  125. content: address, // 可以显示解析出的地址
  126. color: '#ffffff',
  127. bgColor: '#7F56B2',
  128. padding: 10,
  129. borderRadius: 5,
  130. display: 'ALWAYS'
  131. }
  132. }]
  133. });
  134. console.log(this.data.address);
  135. },
  136. fail: function (resq) {
  137. wx.showModal({
  138. title: '信息提示',
  139. content: '请求失败',
  140. showCancel: false,
  141. confirmColor: '#f37938'
  142. });
  143. },
  144. complete: function () {}
  145. })
  146. },
  147. fail: (err) => {
  148. console.error(err);
  149. wx.showToast({
  150. title: '获取位置失败',
  151. icon: 'none'
  152. });
  153. }
  154. });
  155. },
  156. stop() {
  157. this.setData({
  158. isstop: !this.data.isstop
  159. })
  160. console.log(this.data.isstop);
  161. },
  162. //结束光环展示
  163. startIncrease() {
  164. // 记录开始时间
  165. this.setData({
  166. startTime: Date.now(),
  167. });
  168. // 清除之前的定时器
  169. if (this.data.timer) {
  170. clearInterval(this.data.timer);
  171. }
  172. // 设置定时器,每隔 40 毫秒更新一次 percentage
  173. this.setData({
  174. timer: setInterval(() => {
  175. const currentTime = Date.now();
  176. const elapsedTime = currentTime - this.data.startTime;
  177. const percentage = Math.min((elapsedTime / 4000) * 100, 100);
  178. this.setData({
  179. percentage: `conic-gradient(from 0deg, #015EEA ${percentage}%, white 0%)`,
  180. });
  181. if(percentage >= 100){
  182. wx.showToast({
  183. title: '运动已结束',
  184. icon: 'success',
  185. duration: 500
  186. });
  187. setTimeout(()=>{
  188. this.onConfirm()
  189. },500)
  190. }
  191. // 如果达到 100%,清除定时器
  192. if (percentage >= 100) {
  193. clearInterval(this.data.timer);
  194. this.setData({
  195. timer: null,
  196. });
  197. }
  198. }, 40),
  199. });
  200. },
  201. stopIncrease() {
  202. // 清除定时器
  203. if (this.data.timer) {
  204. clearInterval(this.data.timer);
  205. this.setData({
  206. timer: null,
  207. });
  208. // 如果未达到 100%,清零 percentage
  209. const elapsedTime = Date.now() - this.data.startTime;
  210. if (elapsedTime < 4000) {
  211. this.setData({
  212. percentage: '',
  213. });
  214. }
  215. }
  216. },
  217. //点击返回按钮
  218. goback() {
  219. this.setData({
  220. show: true,
  221. isstop: false,
  222. })
  223. },
  224. //取消弹窗
  225. onClose() {
  226. this.setData({
  227. show: false
  228. });
  229. },
  230. //确认返回
  231. onConfirm(){
  232. wx.navigateBack({
  233. delta: 1 // 返回的页面层数,1 表示返回上一页
  234. });
  235. }
  236. })