封装wx.request()

master
许宏杰 3 years ago
parent da8d5e9fa9
commit 0c1488f63a

@ -1,4 +1,5 @@
// app.js
import fetch from './utils/request'
App({
onLaunch() {
// 隐藏原生的tabbar
@ -24,6 +25,7 @@ App({
menuHeight: 0, // 胶囊高度(自定义内容可与胶囊高度保证一致)
menuWidth:0
},
fetch,
/**
* @description 设置导航栏信息

@ -9,11 +9,24 @@ Page({
* 页面的初始数据
*/
data: {
cityName:'南京',
cityName:'',
show:false,
columns:[
'南京','无锡','扬州','常州','盐城','苏州','南通','连云港','淮安','宿迁','镇江','徐州','泰州'
{id:1045,name:'南京市'},
{id:1061,name:'无锡市'},
{id:1071,name:'徐州市'},
{id:1084,name:'常州市'},
{id:1093,name:'苏州市'},
{id:1108,name:'南通市'},
{id:1119,name:'连云港'},
{id:1130,name:'淮安市'},
{id:1141,name:'盐城市'},
{id:1152,name:'扬州市'},
{id:1161,name:'镇江市'},
{id:1169,name:'泰州市'},
{id:1177,name:'宿迁市'},
],
code:0,
latitude: 31.942406,
longitude: 118.836155,
temp: 0,
@ -388,13 +401,11 @@ Page({
// 实例化API核心类
qqmapsdk = new QQMapWX({
key: 'CECBZ-55LCW-TSYR7-O4UGV-75DE7-2JBQH',
sig: "BmvXO4wJDbVYKa7yrzuIY2wPuXKaxetj"
});
// this.getWeatherData()
// this.showGroundOverlay()
// this.getMian()
// 请求面数据
this.getMian()
},
//选择市区
//打开选择市区
cityClick(){
this.setData({
show:true,
@ -408,10 +419,14 @@ Page({
},
//确定
onConfirm(e){
let selData = e.detail
this.setData({
show:false,
cityName:e.detail.value
code:selData.index,
cityName:selData.value
})
//获取地区天气
this.getWeatherData()
},
//地图定位
getLcation() {
@ -427,7 +442,7 @@ Page({
})
},
//获取天气
getWeatherData() {
getWeatherData(e) {
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
@ -435,31 +450,24 @@ Page({
month = month >= 10 ? month : '0' + month
date = date >= 10 ? date : '0' + date
let nowTimer = year + '-' + month + '-' + date
wx.request({
url: 'https://www.jichuanglanhai.com:9018/Luying/getweatherdata/getdata',
method: 'GET',
data: {
Cityid: 1045,
date: nowTimer
},
success: (res) => {
this.setData({
temp: res.data.data.temp,
tempCode: `http://www.jichuanglanhai.com/demo/yingdi-file/W${res.data.data.icon}.png`
let params = {
Cityid:this.data.columns[this.data.code].id,
date: nowTimer
}
app.fetch('9018/Luying/getweatherdata/getdata',params,false,'GET').then(res=>{
if(res.code == 200){
this.setData({
temp: res.data.temp,
tempCode: `http://www.jichuanglanhai.com/demo/yingdi-file/W${res.data.icon}.png`
})
}
})
},
//请求面数据
getMian() {
//请求面数据
wx.request({
url: 'https://www.jichuanglanhai.com:9017/jsonData/nanjing.json',
headers: {
'Content-Type': 'application/json'
},
success: (res) => {
let list = res.data.features[0].geometry.coordinates[0][0]
app.fetch('9017/jsonData/nanjing.json',{},false,'GET').then(res=>{
// console.log(res)
let list = res.features[0].geometry.coordinates[0][0]
let mianList = [{
points: [],
fillColor: "#A2D1B4", //前六位数字表示多边形背景颜色后两位表示透明度90是不透明多90%反过来透明度10%
@ -468,117 +476,99 @@ Page({
zIndex: 2
}]
for (let i = 0; i < list.length; i++) {
mianList[0].points.push({
longitude: list[i][0],
latitude: list[i][1],
})
mianList[0].points.push({
longitude: list[i][0],
latitude: list[i][1],
})
}
this.getMianShadow(mianList)
}
})
},
//获取面阴影
getMianShadow(mianList) {
wx.request({
url: 'https://www.jichuanglanhai.com:9017/jsonData/nanjing-shaw.json',
headers: {
'Content-Type': 'application/json'
},
success: (res) => {
let lists = res.data.features[0].geometry.coordinates
for (let j = 0; j < lists.length; j++) {
var shadow = {
points: [],
fillColor: "#437353", //前六位数字表示多边形背景颜色后两位表示透明度90是不透明多90%反过来透明度10%
strokeColor: 'none',
strokeWidth: 0,
zIndex: 2
}
var list = lists[j][0]
for (let i = 0; i < list.length; i++) {
shadow.points.push({
longitude: list[i][0],
latitude: list[i][1],
})
}
mianList.push(shadow)
app.fetch('9017/jsonData/nanjing-shaw.json',{},false,'GET').then(res=>{
let lists = res.features[0].geometry.coordinates
for (let j = 0; j < lists.length; j++) {
var shadow = {
points: [],
fillColor: "#437353", //前六位数字表示多边形背景颜色后两位表示透明度90是不透明多90%反过来透明度10%
strokeColor: 'none',
strokeWidth: 0,
zIndex: 2
}
var list = lists[j][0]
for (let i = 0; i < list.length; i++) {
shadow.points.push({
longitude: list[i][0],
latitude: list[i][1],
})
}
mianList.push(shadow)
}
//河流
this.getRiver(mianList)
//高速公路
this.getWaydata()
},
})
},
//河流
getRiver(mianList) {
wx.request({
url: 'https://www.jichuanglanhai.com:9017/jsonData/nanjing-river.json',
headers: {
'Content-Type': 'application/json'
},
success: (res) => {
let list = res.data.features
list.forEach(item => {
var rr = {
points: [],
fillColor: "#5ED2E0", //前六位数字表示多边形背景颜色后两位表示透明度90是不透明多90%反过来透明度10%
strokeColor: 'none',
strokeWidth: 0,
zIndex: 2,
}
item.geometry.coordinates.forEach(data => {
data.forEach(river => {
river.forEach(it => {
rr.points.push({
longitude: it[0],
latitude: it[1],
})
app.fetch('9017/jsonData/nanjing-river.json',{},false,'GET').then(res=>{
let list = res.features
list.forEach(item => {
var rr = {
points: [],
fillColor: "#5ED2E0", //前六位数字表示多边形背景颜色后两位表示透明度90是不透明多90%反过来透明度10%
strokeColor: 'none',
strokeWidth: 0,
zIndex: 2,
}
item.geometry.coordinates.forEach(data => {
data.forEach(river => {
river.forEach(it => {
rr.points.push({
longitude: it[0],
latitude: it[1],
})
})
})
mianList.push(rr)
})
this.setData({
polygons: mianList,
polygons2: mianList
})
// console.log(this.data.polygons,'数据')
},
mianList.push(rr)
})
this.setData({
polygons: mianList,
polygons2: mianList
})
})
},
//高速公路
getWaydata() {
wx.request({
url: 'https://www.jichuanglanhai.com:9017/jsonData/nanjing-way.json',
headers: {
'Content-Type': 'application/json'
},
success: (res) => {
let list = res.data.features
let lineList = []
list.forEach((item, index) => {
lineList[index] = {
points: [],
color: '#C6E9CD',
width: 3
}
item.geometry.coordinates.forEach(line => {
line.forEach(datas => {
lineList[index].points.push({
longitude: datas[0],
latitude: datas[1]
})
app.fetch('9017/jsonData/nanjing-way.json',{},false,'GET').then(res=>{
let list = res.features
let lineList = []
list.forEach((item, index) => {
lineList[index] = {
points: [],
color: '#C6E9CD',
width: 3
}
item.geometry.coordinates.forEach(line => {
line.forEach(datas => {
lineList[index].points.push({
longitude: datas[0],
latitude: datas[1]
})
})
});
this.setData({
polyline: lineList,
polyline2: lineList
})
},
});
this.setData({
polyline: lineList,
polyline2: lineList
})
})
},
//单击地图的marker
clickMapMarker(e) {
@ -670,7 +660,7 @@ Page({
// this.setData({
// style: 1
// })
console.log('页面显示')
//初次进入获取定位
this.getUserLocation()
},
@ -717,11 +707,11 @@ Page({
},
})
}else if(res.authSetting['scope.userLocation'] == undefined) {
//用户首次进入页面
_this.geo();
console.log('初次进入')
//用户首次进入页面
_this.geo();
// console.log('初次进入')
}else{
console.log('授权成功')
// console.log('授权成功')
//已授权
_this.geo();
}
@ -733,18 +723,28 @@ Page({
geo: function () {
var _this = this;
wx.getLocation({
type: 'wgs84',
type: 'gcj02',
isHighAccuracy: true,//开启高精度定位
success: function (res) {
let {latitude,longitude} = res
// 调用腾讯地图api获取当前位置
qqmapsdk.reverseGeocoder({
sig: "BmvXO4wJDbVYKa7yrzuIY2wPuXKaxetj",
location: {
latitude: latitude,
longitude: longitude
},
success: function (res) {
console.log(res.result)
// console.log(res.result)
//获取城市id
let index =_this.data.columns.findIndex(item=>item.name == res.result.address_component.city)
_this.setData({
cityName:res.result.address_component.city,
code:index,
// latitude: res.result.location.lat,
// longitude: res.result.location.lng,
})
_this.getWeatherData()
},
fail: function (error) {
console.log('腾讯api调取失败',error);

@ -1,12 +1,12 @@
<!--pages/home/index.wxml-->
<view class="map-container">
<!-- <map id="map" latitude="{{latitude}}" longitude="{{longitude}}" markers="{{markers}}" scale="{{scale}}" polygons="{{polygons}}" polyline="{{polyline}}" enable-poi="false" subkey="{{key}}" layer-style="{{style}}" bindmarkertap="clickMapMarker" min-scale="8.5" style="height: 100vh;" show-location="true" bindregionchange="handleRegionChange" >
</map> -->
<map id="map" latitude="{{latitude}}" longitude="{{longitude}}" markers="{{markers}}" scale="{{scale}}" polygons="{{polygons}}" polyline="{{polyline}}" enable-poi="false" subkey="{{key}}" layer-style="{{style}}" bindmarkertap="clickMapMarker" min-scale="8.5" style="height: 100vh;" show-location="true" bindregionchange="handleRegionChange" >
</map>
<view class="map-select">
<view class="left-sel" bindtap="cityClick">
<view class="left-icon-name">
<view class="city-icon"></view>
<view class="city-name">{{cityName}}</view>
<view class="city-name">{{cityName}}</view>
</view>
<view class="left-icones">
<van-icon name="arrow-down" />
@ -41,13 +41,27 @@
<image src="http://www.jichuanglanhai.com/demo/gonghui-image/icon/icon_location@2x.png" mode="aspectFill"></image>
</view>
</view>
<my-tarBar activeIdx="{{activeIdx}}"></my-tarBar>
<!-- select选择模式款 -->
<van-popup
show="{{show}}"
round
position="bottom"
>
<van-picker columns="{{ columns }}" bind:cancel="onCancel" bind:confirm="onConfirm" show-toolbar title="营地列表" toolbar-class="toolbar-box"/>
</van-popup>
<van-picker columns="{{ m1.getArr(columns) }}" bind:cancel="onCancel" bind:confirm="onConfirm" show-toolbar title="营地列表" toolbar-class="toolbar-box"/>
</van-popup>
<!-- wxs过滤十三太保数组 -->
<wxs module="m1">
var getArr = function(arr){
var filterList = []
for (var i = 0; i < arr.length; i++) {
filterList.push(arr[i].name)
}
return filterList
}
module.exports.getArr = getArr;
</wxs>
<!-- 底部导航条 -->
<my-tarBar activeIdx="{{activeIdx}}"></my-tarBar>

@ -1,6 +1,6 @@
{
"setting": {
"urlCheck": true,
"urlCheck": false,
"compileHotReLoad": false,
"ignoreDevUnusedFiles": false
},

@ -0,0 +1,33 @@
const API_URL = 'https://www.jichuanglanhai.com:'
export default function fetch(url, params, showLoading , method) {
if (showLoading) {
wx.showLoading({ title: '加载中' });
}
var cookie = wx.getStorageSync('cookie');
if (!cookie) {
cookie = '';
}
return new Promise((resolve, reject) => {
wx.request({
url: `${API_URL}${url}`,
data: params || {},
method: method || 'post',
header: {
"Accept": "*/*",
"Content-Type": "application/json; charset=UTF-8'",
"Cookie": cookie,
},
success: res => {
resolve(res.data);
},
fail: error => {
reject(error);
},
complete() {
if (showLoading) {
wx.hideLoading();
}
}
})
})
}
Loading…
Cancel
Save