158 lines
6.2 KiB
JavaScript
158 lines
6.2 KiB
JavaScript
// template/nearbySchool/nearbySchool.js
|
|
const app = getApp()
|
|
var miucms = require('../../utils/miucms.js');
|
|
|
|
Component({
|
|
|
|
/**
|
|
* 组件的属性列表
|
|
*/
|
|
properties: {
|
|
uniqid: {
|
|
type: String,
|
|
observer(res) {
|
|
this.distanceSchool()
|
|
}
|
|
},
|
|
options: {
|
|
type: Object,
|
|
observer(res) {}
|
|
},
|
|
istype: Number,
|
|
},
|
|
|
|
/**
|
|
* 组件的初始数据
|
|
*/
|
|
data: {
|
|
annexSchoolOmit: [], // 附近院校 前面 7个 或者 6个的
|
|
annexSchoolList: [], // 附近院校数据
|
|
academyPitchIndex: 0, // 附近学校距离选中院校 下标
|
|
targetAcademyPitch: [], // 附近学校距离选中院校 数据
|
|
specialSchoolDistance: null, // 特殊的 用户带有school参数 则需要特殊显示 学校距离
|
|
|
|
},
|
|
|
|
/**
|
|
* 组件的方法列表
|
|
*/
|
|
methods: {
|
|
// 获取 距离学校距离
|
|
distanceSchool() {
|
|
miucms.request(`${app.globalData.baseURL}/tenement/v2/api/details/distance`, {
|
|
uniqid: this.data.uniqid,
|
|
istype: this.data.istype,
|
|
}).then(res => {
|
|
if (res.code != 200) return
|
|
let data = res.data
|
|
let specialSchoolDistance = null
|
|
let academyPitchIndex = 0
|
|
const school = this.data.options['school'] || ''
|
|
let annexSchoolOmit = []
|
|
data.forEach((element, index) => {
|
|
element['distanceText'] = this.metersToKilometers(element.distance)
|
|
element.list.forEach((ele, ii) => {
|
|
ele['durationText'] = this.secondsToHoursMinutes(ele.duration, 'chinese')
|
|
ele['durationText2'] = this.secondsToHoursMinutes(ele.duration)
|
|
ele['distanceText'] = this.metersToKilometers(ele.distance)
|
|
if (Object.prototype.toString.call(ele.publictransport) === '[object Object]') {
|
|
ele.publictransport['durationText'] = this.secondsToHoursMinutes(ele?.publictransport?.duration || 0, 'chinese')
|
|
ele.publictransport['durationText2'] = this.secondsToHoursMinutes(ele?.publictransport?.duration || 0)
|
|
const segments = ele.publictransport['segments']
|
|
if (Array.isArray(segments)) {
|
|
segments.forEach(e => {
|
|
// console.log("e", e);durationText
|
|
e['via_num'] = 1 + Math.floor(e.via_num)
|
|
e['durationText'] = this.secondsToHoursMinutes(e.duration)
|
|
e['distanceText'] = this.metersToKilometers(e.distance, 'chinese')
|
|
})
|
|
}
|
|
} else ele.publictransport = null
|
|
|
|
|
|
if (ii == 0) {
|
|
const obj = element.list[0] || {}
|
|
let toolText = "步行"
|
|
if (Object.prototype.toString.call(obj.publictransport) === '[object Object]') toolText = "公交地铁"
|
|
element['distanceText'] = obj['distanceText']
|
|
element['durationText'] = obj.publictransport?.durationText2 || obj['durationText2']
|
|
element['toolText'] = toolText
|
|
}
|
|
})
|
|
|
|
if (school == element.id) {
|
|
academyPitchIndex = index
|
|
specialSchoolDistance = element
|
|
} else annexSchoolOmit.push(element)
|
|
})
|
|
|
|
if (specialSchoolDistance) annexSchoolOmit = annexSchoolOmit.slice(0, 5)
|
|
else annexSchoolOmit = annexSchoolOmit.slice(0, 4)
|
|
|
|
this.setData({
|
|
annexSchoolList: data,
|
|
targetAcademyPitch: data[academyPitchIndex],
|
|
annexSchoolOmit,
|
|
specialSchoolDistance,
|
|
academyPitchIndex,
|
|
})
|
|
})
|
|
},
|
|
|
|
// 切换附近学校弹窗
|
|
cutNearbySchools() {
|
|
this.setData({
|
|
academyState: true,
|
|
})
|
|
},
|
|
|
|
// 选择附近学校距离的学校下标
|
|
selectAcademyIndex(e) {
|
|
const academyPitchIndex = e.currentTarget.dataset.index || 0
|
|
const annexSchoolList = this.data.annexSchoolList
|
|
this.setData({
|
|
academyPitchIndex,
|
|
targetAcademyPitch: annexSchoolList[academyPitchIndex],
|
|
})
|
|
},
|
|
|
|
// 转换 秒数
|
|
secondsToHoursMinutes(seconds, type) {
|
|
// 计算小时数
|
|
const hours = Math.floor(seconds / 3600);
|
|
// 计算剩余的秒数
|
|
const remainingSecondsAfterHours = seconds % 3600;
|
|
// 计算分钟数
|
|
const minutes = Math.floor(remainingSecondsAfterHours / 60);
|
|
// 计算剩余的秒数
|
|
const remainingSeconds = remainingSecondsAfterHours % 60;
|
|
|
|
let text = ""
|
|
if (type == 'chinese') {
|
|
if (hours != 0) text += hours + '小时'
|
|
if (minutes != 0) text += minutes + '分钟'
|
|
if (remainingSeconds != 0 && minutes == 0) text += remainingSeconds + '秒'
|
|
} else {
|
|
if (hours != 0) text += hours + 'h'
|
|
if (minutes != 0) text += minutes + 'min'
|
|
if (remainingSeconds != 0 && minutes == 0) text += remainingSeconds + 's'
|
|
}
|
|
return text
|
|
},
|
|
// 转换 米数
|
|
metersToKilometers(input, type) {
|
|
let text = ""
|
|
if (input >= 1000) text = (input / 1000).toFixed(1) + (type == 'chinese' ? '公里' : 'km')
|
|
else text = input + (type == 'chinese' ? '米' : 'm');
|
|
return text;
|
|
},
|
|
|
|
// 专门取反的函数
|
|
negation(e) {
|
|
let key = e.currentTarget.dataset.key
|
|
this.setData({
|
|
[key]: !this.data[key]
|
|
})
|
|
},
|
|
}
|
|
}) |