Add AMap location and weather header with warm mini program theme
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
const { secure, toast } = require("../../utils/api");
|
||||
const { localDate } = require("../../utils/date");
|
||||
function dateLabel() {
|
||||
const now = new Date();
|
||||
return `${now.getMonth() + 1}月${now.getDate()}日 周${"日一二三四五六"[now.getDay()]}`;
|
||||
}
|
||||
Page({
|
||||
data: {
|
||||
locationName: "当前位置",
|
||||
headerSubtitle: dateLabel() + " · 轻触获取天气",
|
||||
pets: [],
|
||||
selected: [],
|
||||
tasks: [],
|
||||
@@ -11,6 +17,49 @@ Page({
|
||||
},
|
||||
onShow() {
|
||||
this.load();
|
||||
this.updateEnvironmentLabel();
|
||||
if (!this.environmentAt || Date.now() - this.environmentAt > 15 * 60 * 1000) {
|
||||
this.refreshEnvironment();
|
||||
}
|
||||
},
|
||||
updateEnvironmentLabel() {
|
||||
this.setData({ headerSubtitle: dateLabel() + " · " + (this.weatherLabel || "轻触获取天气") });
|
||||
},
|
||||
async refreshEnvironment(event) {
|
||||
if (this.environmentBusy) return;
|
||||
this.environmentBusy = true;
|
||||
const tapped = !!event;
|
||||
let positioned = false;
|
||||
try {
|
||||
const settings = await new Promise((resolve, reject) => wx.getSetting({ success: resolve, fail: reject }));
|
||||
if (settings.authSetting["scope.userLocation"] === false) {
|
||||
if (!tapped) throw new Error("location-denied");
|
||||
const enabled = await new Promise((resolve, reject) => wx.openSetting({ success: resolve, fail: reject }));
|
||||
if (!enabled.authSetting["scope.userLocation"]) throw new Error("location-denied");
|
||||
}
|
||||
this.weatherLabel = "正在获取天气";
|
||||
this.updateEnvironmentLabel();
|
||||
const point = await new Promise((resolve, reject) => wx.getLocation({
|
||||
type: "gcj02",
|
||||
success: resolve,
|
||||
fail: reject,
|
||||
}));
|
||||
positioned = true;
|
||||
const result = await secure("/environment", "GET", { latitude: point.latitude, longitude: point.longitude });
|
||||
this.setData({ locationName: result.location_name });
|
||||
this.weatherLabel = result.weather
|
||||
? `${result.weather.description} ${result.weather.temperature}°C`
|
||||
: "天气暂不可用";
|
||||
this.environmentAt = result.weather ? Date.now() : 0;
|
||||
} catch (error) {
|
||||
const denied = /denied|deny|auth/.test(error.errMsg || error.message || "");
|
||||
this.setData({ locationName: positioned ? "位置暂不可用" : denied ? "开启定位" : "定位暂不可用" });
|
||||
this.weatherLabel = denied ? "轻触开启定位" : "轻触重试";
|
||||
if (tapped && !denied) wx.showToast({ title: positioned ? "位置和天气获取失败,请重试" : "定位失败,请重试", icon: "none" });
|
||||
} finally {
|
||||
this.environmentBusy = false;
|
||||
this.updateEnvironmentLabel();
|
||||
}
|
||||
},
|
||||
async load() {
|
||||
try {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<page-layout id="page-layout" title="早安,{{name}}">
|
||||
<page-layout id="page-layout" title="{{locationName}}" subtitle="{{headerSubtitle}}" location="{{true}}" bind:locationtap="refreshEnvironment">
|
||||
<view class="screen">
|
||||
<view class="hero">
|
||||
<view class="hero-copy">
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
.hero {
|
||||
min-height: 330rpx;
|
||||
background: #194f42;
|
||||
background: linear-gradient(135deg, #fff0dd, #ffdec7);
|
||||
border-radius: 38rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12rpx;
|
||||
padding: 35rpx;
|
||||
color: white;
|
||||
color: #694c3f;
|
||||
}
|
||||
.hero-copy {
|
||||
display: flex;
|
||||
@@ -15,7 +15,7 @@
|
||||
min-width: 0;
|
||||
}
|
||||
.hero-small {
|
||||
color: #abd9b8;
|
||||
color: #7c5b46;
|
||||
font-size: 23rpx;
|
||||
font-weight: 600;
|
||||
}
|
||||
@@ -26,11 +26,11 @@
|
||||
}
|
||||
.hero-meta {
|
||||
font-size: 23rpx;
|
||||
color: #d9eadd;
|
||||
color: #7c5b46;
|
||||
}
|
||||
.hero-caption {
|
||||
font-size: 20rpx;
|
||||
color: #b5e2be;
|
||||
color: #7c5b46;
|
||||
margin-top: 36rpx;
|
||||
}
|
||||
.hero image {
|
||||
@@ -43,7 +43,7 @@
|
||||
}
|
||||
.link {
|
||||
font-size: 24rpx;
|
||||
color: #398668;
|
||||
color: #b95738;
|
||||
font-weight: 600;
|
||||
}
|
||||
.walk-card {
|
||||
@@ -58,7 +58,7 @@
|
||||
width: 145rpx;
|
||||
height: 145rpx;
|
||||
border-radius: 26rpx;
|
||||
background: #dfeedd;
|
||||
background: #fff0e2;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
font-size: 72rpx;
|
||||
@@ -73,7 +73,7 @@
|
||||
}
|
||||
.picker-label {
|
||||
font-size: 23rpx;
|
||||
color: #7d9989;
|
||||
color: #886c5f;
|
||||
margin: 27rpx 0 16rpx;
|
||||
}
|
||||
.start {
|
||||
@@ -95,11 +95,11 @@
|
||||
background: #f7eadc;
|
||||
}
|
||||
.stats > view:nth-child(3) {
|
||||
background: #e8efea;
|
||||
background: #edf2fb;
|
||||
}
|
||||
.stats text {
|
||||
display: block;
|
||||
color: #8da49a;
|
||||
color: #886c5f;
|
||||
font-size: 22rpx;
|
||||
}
|
||||
.stats strong {
|
||||
@@ -109,7 +109,7 @@
|
||||
}
|
||||
.stats small {
|
||||
font-size: 22rpx;
|
||||
color: #94a69d;
|
||||
color: #886c5f;
|
||||
}
|
||||
.tasks {
|
||||
padding: 12rpx 25rpx;
|
||||
@@ -119,7 +119,7 @@
|
||||
align-items: center;
|
||||
gap: 17rpx;
|
||||
padding: 22rpx 0;
|
||||
border-bottom: 1rpx solid #edf1ed;
|
||||
border-bottom: 1rpx solid #ece2d8;
|
||||
}
|
||||
.task:last-child {
|
||||
border: 0;
|
||||
@@ -127,21 +127,21 @@
|
||||
.check {
|
||||
width: 40rpx;
|
||||
height: 40rpx;
|
||||
border: 3rpx solid #cfdfd2;
|
||||
border: 3rpx solid #ece2d8;
|
||||
border-radius: 13rpx;
|
||||
text-align: center;
|
||||
line-height: 37rpx;
|
||||
color: white;
|
||||
}
|
||||
.check.done {
|
||||
background: #3b9164;
|
||||
border-color: #3b9164;
|
||||
background: #b95738;
|
||||
border-color: #b95738;
|
||||
}
|
||||
.task-icon {
|
||||
width: 65rpx;
|
||||
height: 65rpx;
|
||||
border-radius: 16rpx;
|
||||
background: #f0f5ee;
|
||||
background: #fff0e2;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
font-size: 34rpx;
|
||||
|
||||
Reference in New Issue
Block a user