基本信息
源码名称:天气预报 微信小程序源码
源码大小:0.91M
文件格式:.zip
开发语言:js
更新时间:2018-08-17
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍


Page({
    data:{
        AK: 'hWtYHdhw2fIwi9PWsWX09IVmGtLVp2UG',
        city: '',
        temp: '',
        todayDayImg: '',
        todayNightImg: '',
        weather: '',
        todayDate: '',
        todayTime: ''
    },
    // 监听页面加载
    onLoad: function () {
        this.loadInfo();
        this.getTime();
    },
    // 监听页面初次渲染完成
    onReady: function () {

    },
    // 监听页面显示
    onShow: function () {

    },
    // 监听页面隐藏
    onHide: function () {

    },
    // 监听页面卸载
    onUpload: function () {

    },
    // 监听用户下拉动作
    onPullDownRefresh: function () {
        this.getTime();
    },
    // 监听页面上拉触底
    onReachBottom: function () {

    },
    loadInfo: function () {
        var that = this;
        wx.getLocation({
            type: 'gcj02',
            success: function(res) {
                var latitude = res.latitude;
                var longitude = res.longitude;
                var AK = that.data.AK;
               that.loadCity(latitude, longitude, AK, that.loadWeather);
               console.log(that.data.city);
            //    that.loadWeather(that.data.city, AK);
            }
        })
    },
    loadCity: function (latitude, longitude, AK, callback) {
        var that = this;
        var url = 'https://api.map.baidu.com/geocoder/v2/?location='   latitude   ','   longitude   '&output=json&ak='   AK;
        wx.request({
            url: url,
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                console.log(res);
                var city = res.data.result.addressComponent.city;
                that.setData({city :city});
                console.log(that.data.city);
                callback && callback(city, AK);
            }
        })
    },
    loadWeather: function (city, AK) {
        var that  = this;
        var url = 'https://api.map.baidu.com/telematics/v3/weather?location='   city   '&output=json&ak='   AK;
        console.log(url);
        wx.request({
            url: url,
            header: {
                'content-type': 'application/json'
            },
            success: function(res) {
                var desNum = that.random(5);
                console.log(res);
                var future = res.data.results[0].weather_data.filter(function(ele, index) {
                    return index > 0;
                });
                var temReg = /\d ℃/;
                that.setData({
                    temp: res.data.results[0].weather_data[0].date.match(temReg)[0],
                    todayDayImg: res.data.results[0].weather_data[0].dayPictureUrl,
                    todayNightImg: res.data.results[0].weather_data[0].nightPictureUrl,
                    weather: res.data.results[0].weather_data[0].weather   ' | '   res.data.results[0].weather_data[0].wind,
                });
            console.log(that.data.future);

            }
        })
    },
    random: function(max, min) {
        var min = min || 0;
        return Math.floor(Math.random() * (max - min  1)   min);
    },
    getTime: function () {
        var date = new Date();
        var minute = date.getMinutes() >=10 ? date.getMinutes() : ('0'   date.getMinutes());
        var hour = date.getHours() >=10 ? date.getHours() : ('0'   date.getHours());
        var now = date.getDate() >= 10 ? date.getDate() : ('0'   date.getDate());
        var todayDate = date.getFullYear()   '-'   (date.getMonth()   1)   '-'   now;
        var todayTime = hour   ':'   minute;
        this.setData({
            todayDate: todayDate,
            todayTime: todayTime
        })
    }
})