//获取经纬度
var page;
function getLocation() {
var that = this;
wx.getLocation({
type: 'wgs84',
//请求成功
success(res) {
const latitude = res.latitude
const longitude = res.longitude
getCity(latitude, longitude);
},
//请求失败
fail() {
wx.showToast({
title: '获取失败,请检查网络设置',
icon: 'none',
duration: 2000
});
}
})
}
//获取城市信息
function getCity(latitude, longitude) {
var url = "https://api.map.baidu.com/reverse_geocoding/v3/";
var struct = {
location: latitude + "," + longitude,
ak: "jENyKFL5cVcIHtRanGbvaBllXFbTDexu",
output: "json"
}
wx.request({
url: url,
data: struct,
//成功
success: function (res) {
var city = res.data.result.addressComponent.city;//获取城市信息
city = city.slice(0,city.length-1);
//获取城市电影
getMovies(city);
},
//失败
fail: function (res) {
wx.showToast({
title: '获取失败,请检查网络设置',
icon: 'none',
duration: 2000
});
}
})
}
//获取当地电影信息
function getMovies(city)
{
wx.request({
url: 'https://douban.uieee.com/v2/movie/in_theaters',
data: {
city:city
},
header: { 'content-type': 'application/text' },
//成功
success: function (res) {
wx.hideToast();
page.setData({
movies: res.data.subjects,
title: res.data.title
});
console.log(page.data.movies);
console.log(page.data.title);
},
fail: function (res) {
wx.showToast({
title: '获取失败,请检查网络设置',
icon: 'none',
duration: 2000
});
},
});
}
Page({
/**
* 页面的初始数据
*/
data: {
movies:["Loading.."],
title:"Loading.."
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
page=this;
wx.showModal({
title: '提示',
content: '小德评影XD将获取您的城市信息以展示城市热播',
success(res) {
//成功授权
if (res.confirm) {
//获取城市信息
wx.showToast({
title: '获取成功,请稍后..',
icon: 'none',
duration: 10000
});
getLocation();
}
//授权失败
else if (res.cancel) {
wx.showToast({
title: '您取消了授权,无法获取城市热播',
icon: 'none',
duration: 2000
})
}
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})