两天撸两个天气小程序
经过最近两年多的发展,小程序的地位也逐渐越来越高了,各个平台前赴后继做了自家的小程序平台,随着市场的需求越来愈多,我们开发各平台的小程序的激情也随(被)之(逼)高(无)涨(奈)。 选择uniapp来开发。uni-app 是一个使用 Vue.js 开发所有前端应用的框架,开发者编写一套代码,可发布到iOS、Android、H5、以及各种小程序(微信/支付宝/百度/头条/QQ/钉钉)等多个平台。即使不跨端,uni-app同时也是更好的小程序开发框架。来自官方。喜欢taro, wepy,mpvue的朋友也莫喷我,大家各有所好,大家开心就好。 【智行天气】小程序效果图1、获取位置信息在定位功能中,本程序用到腾讯地图的api 以及 腾讯天气的api接口, 需要到官网中注册开发者账号,通过注册后得到的appKey来请求我们需要的数据,详细注册步骤请自行度娘 由于需要用到定位功能,uniapp的getLocation方法获取到的是当前位置的坐标,然后对应腾讯地图具体城市 uni.getLocation({
// #ifdef MP-WEIXIN
type: 'wgs84',// #endif
async success (res) {
const {latitude,longitude} = res
const result = await that.ajax({url: 'https://apis.map.qq.com/ws/geocoder/v1',data: {
location: `${latitude},${longitude}`,key: ''
}})
let {province,city,district} = result.result.address_component
that.getData(province,district)
},fail(){
uni.showModal({
content: '检测到您没打开定位权限,是否去设置打开?',confirmText: "确认",cancelText: "取消",success: function (res) {
if (res.confirm) {
// #ifdef MP-WEIXIN
wx.openSetting({
success: (res) => {
that.getIn()
}
})
// #endif
// #ifdef MP-ALIPAY
my.openSetting({
success: (res) => {
that.getIn()
}
})
// #endif
}
}
});
}
})
2、查询天气得到城市名后,再用城市名查询天气的接口,得到未来几天的天气预报。 天气接口使用腾讯天气接口api。 在小程序中使用前,要在小程序设置界面,开发设置中添加request合法域名。 methods: {
async getData(province,district){
const that = this
const data = await that.ajax({url: 'https://wis.qq.com/weather/common',data: {
source: 'xw',weather_type: 'observe|alarm|air|forecast_1h|forecast_24h|index|limit|tips|rise',province: province,city: city,county: district
}})
that.region = [province,district]
if(data.status != 200){
uni.showToast({
title: result.message,icon: 'none'
});
return false;
}
if(!data.data.air.aqi_name){
uni.showToast({
title: '暂无该地区的天气信息',96);">false;
}
that.data = data
}
}
3、小程序界面由于没有什么审美,缺乏想象力,参考腾讯天气的界面来做的。功能十分简单,查看当前地区的天气和切换其他地区的天气,查看最近24小时的天气情况以及最近6天的天气情况,展示今天的农历时间。 4、插件使用想快速完成小程序的搭建,里面的折线图采用的?uchart.js?, 因为可以兼容支付宝小程序和微信小程序,农历查询也是采用的插件?calendar.js 折线图在支付宝小程序中会有模糊的问题,需要做兼容处理 <template>
<!-- #ifdef MP-ALIPAY -->
<canvas canvas-id="canvas" id="canvas" width="750" height="240" style="width:750rpx;height:240rpx;" class="canvas">
</canvas>
<!-- #endif -->
<!-- #ifdef MP-WEIXIN -->
<"canvas" class=<!-- #endif -->
</template>
<script>
var wxCharts = require('../../utils/chart.js');
lineChart = new wxCharts({
$this: this,canvasId: 'canvas',type: 'line',categories: ['',''],colors: ['#ffad35',0);">'#4fc3f7'],background: '#fff',animation: true,series: [{
name: data: that.max,format: function (val,name) {
return val + '°';
}
},{
name: data: that.min,0);">'°';
}
}],xAxis: {
disableGrid: disabled: axisLine: false
},yAxis: {
max: Math.max.apply(Math,that.max) * 1 + 0.1,disableGrid: legend:{
show: false
},136);">// #ifdef MP-ALIPAY
pixelRatio: that.pixelRatio,136);">// 解决支付宝模糊问题
// #endif
width: that.cWidth,height: that.cHeight
});
</script>
微信小程序有城市选择组件,支付宝的没有可以直接使用的城市组件,uniapp官方介绍:支持安装 mpvue 组件,但npm方式不支持小程序自定义组件(如 wxml格式的vant-weapp),找到一款支付宝可以使用的城市插件:?mpvue-citypicker?,254);'>城市选择组件 <template>
<view class="txt-location" @tap="showCityPicker">
<"icon"></view>
<block v-if="region.length">{{region[0]}}{{region[1]}}{{region[2]}}</block>
<block v-else>选择城市</block>
<!-- #ifdef MP-WEIXIN -->
<picker class="city" mode="region" @change="handleChange" :value="region">
<"picker">
当前选择:{{region[0]}},{{region[1]}},{{region[2]}}
</view>
</picker>
<!-- #endif -->
</view>
<mpvue-city-picker ref="mpvueCityPicker" :pickerValueDefault="pickerValueDefault" @onConfirm="onConfirm"></mpvue-city-picker>
</import mpvueCityPicker from 'mpvue-citypicker';
export default {
data() {
return {
region: [],pickerValueDefault: [0,1]
};
},components: {
mpvueCityPicker
},methods: {
showCityPicker() {
// #ifdef MP-ALIPAY
this.$refs.mpvueCityPicker.show()
// #endif
},onConfirm(e) {
if(e.label){
this.region = e.label.split('-')
this.getData(this.region[0],0);">1],0);">2])
}
},handleChange(e) {
this.region = e.detail.value
2])
}
}
};
</script>
总结
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |