Files
sphc/pages/index/index_address/index_address.js
2024-05-06 15:34:15 +08:00

279 lines
7.2 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// pages/index/index_address/index_address.js
const qqMap = require('../../../utils/qqmap-wx-jssdk.js')
const qqmapsdk = new qqMap({
key: 'UTFBZ-PQZLW-AX5R6-OWOG6-SRRBH-7FFEL'
});
const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
point: [],
addressName: '当前地址',
pay:false,
},
//复制
fz(e){
wx.setClipboardData({
data: e.currentTarget.dataset.fz,
})
},
//选择地址
pointXz: function (e) {
let index = e.currentTarget.dataset.index;
let dataList = this.data.point;
console.log(dataList[index].storesProvince + dataList[index].storesCity + dataList[index].storesArea + dataList[index].storesDetailed)
const pages = getCurrentPages()
const prevPage = pages[pages.length - 2]  // 上一页
// 调用上一个页面的setData 方法将数据存储
prevPage.setData({
address: dataList[index].storesProvince + dataList[index].storesCity + dataList[index].storesArea + dataList[index].storesDetailed,
addressId: dataList[index].id,
orderPick: 1,
})
wx.navigateBack();
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
if(options.pay){
//支付选择自提点进入
this.setData({
pay:options.pay
})
}
this.map({
type: 1//当前地址 2重新定位
})
},
map(options) {
var that = this;
wx.getSetting({
success(res) {
console.log('get-setting', res.authSetting);
// 只返回用户请求过的授权
let auth = res.authSetting;
if (auth['scope.userLocation']) {
// 已授权,申请定位地址
if (options.type == 1) {
that.toAddress();
} else {
that.again();
}
} else if (auth['scope.userLocation'] === undefined) {
// 用户没有请求过的授权,不需要我们主动弹窗,微信会提供弹窗
if (options.type == 1) {
that.toAddress();
} else {
that.again();
}
} else if (!auth['scope.userLocation']) {
// 没有授权过,需要用户重新授权
// 这个弹窗是为了实现点击不然openSetting会失败
wx.showModal({
title: '是否授权位置信息?',
content: '需要获取您的位置信息,请确认授权',
success: res => {
if (res.confirm) {
wx.openSetting({
success(res) {
console.log('位置信息', res.authSetting);
console.log('open-setting-suc', res.authSetting);
let setting = res.authSetting;
if (!setting['scope.userLocation']) {
wx.showToast({
title: '位置信息授权失败',
icon: 'none',
});
} else {
// 地址授权成功,申请定位地址
if (options.type == 1) {
that.toAddress();
} else {
that.again();
}
}
},
fail(err) {
// 需要点击有时候没有点击是无法触发openSetting
console.log('open-setting-fail', err);
}
});
}
}
});
}
}
});
},
// 调用地址
toAddress: function () {
var that = this
wx.showLoading({
title: '加载中..',
mask: true
})
wx.getLocation({
//定位类型 wgs84, gcj02
// wgs84 返回 gps 坐标gcj02 返回可用于wx.openLocation的坐标
type: 'gcj02',
//获取位置成功
success: function (ress) {
console.log(ress) //获取的的当前位置的详细信息
that.setData({
latitude: ress.latitude,
longitude: ress.longitude
})
//经纬度转化为详细位置
qqmapsdk.reverseGeocoder({
location: {
latitude: ress.latitude,
longitude: ress.longitude
},
success: function (resss) {
wx.hideLoading()
console.log(resss)
var region = [];
that.setData({
addressNo: 1,//地址授权
region: region,
addressName: resss.result.address + resss.result.formatted_addresses.recommend,
})
//获取自提点
that.getStores()
},
fail: function (err) {
wx.hideLoading()
console.log(err)
}
})
},
//获取位置失败
fail: function (err) {
console.log("获取位置信息失败,请返回重试")
wx.showToast({
title: '获取位置信息失败,请返回重试',
icon: 'none'
})
wx.hideLoading()
},
//接口调用结束的回调函数(调用成功、失败都会执行)
complete: function (info) {
console.log("完成")
},
})
},
again: function (e) {
console.log(this.data.latitude)
console.log(this.data.longitude)
let latitude = this.data.latitude;
let longitude = this.data.longitude;
wx.chooseLocation({
latitude: latitude,
longitude: longitude,
success: res => {
// success
console.log(res)
this.setData({
addressName: res.address + res.name,
latitude: res.latitude,
longitude: res.longitude,
})
//获取自提点
this.getStores()
},
fail: function () { },
complete: function () {
// complete
},
})
},
//获取门店列表
getStores() {
wx.showLoading({
title: '加载中',
})
wx.request({
url: app.globalData.ip + '/wisdommining/api/user/getStores',
method: 'GET',
header: {
"content-type": "application/x-www-form-urlencoded",
},
data: {
latitude: this.data.latitude,
longitude: this.data.longitude,
},
success: res => {
console.log(res)
wx.hideLoading()
if (res.data.code == 1) {
let point = res.data.value;
for (let i = 0; i < point.length; i++) {
point[i].location = (point[i].distance / 1000).toFixed(2)
}
this.setData({
point: point
})
} else {
wx.showModal({
title: '获取数据失败!',
content: res.data.message,
showCancel: false,
})
}
}
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
})