-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
172 lines (156 loc) · 5.54 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var userLocState = false;
var nearLab;
var nearLabId;
var filterValue = 0;
async function HTTPFetch() {
//Filter = All
if (filterValue == 0) {
axios.get('https://medlab.oxygentw.net/api/fetchAll/').then(resp => {
console.log("Promise ok");
LabData = resp.data;
console.log(LabData);
getLocation(LabData);
}, function (error) {
console.log(error);
alert("取得檢驗所資料失敗,嘗試重新整理頁面或聯絡開發人員");
});
}
else {
//rapidTest
let params;
if (filterValue == 1) {
params = {
rapidTest: true
}
}
else {
params = {
"rapidTest": true,
"rapidTestStock": { "$gt": 0 }
}
}
axios.post('https://medlab.oxygentw.net/api/fetch', params).then(resp => {
console.log("Promise ok");
LabData = resp.data;
console.log(LabData);
getLocation(LabData);
}, function (error) {
console.log(error);
alert("取得檢驗所資料失敗,嘗試重新整理頁面或聯絡開發人員");
});
}
}
async function fetchNearLab(_lng, _lat) {
let payload = { lng: _lng, lat: _lat, limit: 3 };
//RapidTest
if (filterValue == 1) {
payload["query"] = {
rapidTest: true
}
}
//RapidTest + Stock
else if (filterValue == 2) {
payload["query"] = {
rapidTest: true,
rapidTestStock: {
"$gt": 0
}
}
}
let res;
try {
res = await axios.post('https://medlab.oxygentw.net/api/fetchNear', payload);
} catch (error) {
alert("取得鄰近檢驗所時發生錯誤");
console.log(error);
}
nearLab = res.data;
console.log(res.data);
//判斷裝置
if ($(window).width() < 992) {
nearLabId = "#nearlab_card_mobile";
} else {
nearLabId = "#nearlab_card";
}
console.log(nearLab);
var i = 0;
if (nearLab != undefined) {
$("#nearlab_card").empty();
if (nearLab[i]["rapidTest"] == undefined) {
nearLab[i]["rapidTest"] = false;
}
while (nearLab.length > (i)) {
var comment = '<div class="ts-box"> \
<div class="ts-app-layout is-vertical"> \
<div class="cell"> \
<div class="ts-content"> \
<div class="ts-text is-big">' + nearLab[i]["name"] + '</div> \
</div> \
</div> \
<div class="cell is-fluid is-tertiary"> \
<div class="ts-content"> \
<p>地址:' + nearLab[i]["address"] + ' \
<br> 電話:' + nearLab[i]["phone"] + ' \
<br>是否提供快篩服務:' + nearLab[i]["rapidTest"] + ' \
<br>快篩剩餘:' + nearLab[i]["rapidTestStock"] + '</p> \
<button class="ts-button is-start-labeled-icon is-outlined is-small" onclick=googleMapNav("' + nearLab[i]["address"] + '")> \
<span class="ts-icon is-car-icon"></span> \
導航至此 \
</button></div> \
</div> \
</div> \
</div> \
</div> \
<div class="ts-space"></div> \
';
//console.log(comment);
$(nearLabId).append(comment);
i++;
}
} else {
alert("error");
}
return res.data;
}
function getLocation(LabData) {
//HTTPFetch();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
userLoc = { lat: position.coords.latitude, lng: position.coords.longitude, };
console.log(position.coords.latitude + position.coords.longitude);
userLocState = true;
//取得最近檢驗所
fetchNearLab(userLoc["lng"], userLoc["lat"]);
initMap(userLoc, LabData);
},
function (error) {
if (error.code == error.PERMISSION_DENIED) {
alert("別拒絕我嘛~ 我能幫你找到你附近的檢驗所呦~");
}
var userLoc = { lat: 24.1369088, lng: 120.6550528 };
initMap(userLoc, LabData);
}, { enableHighAccuracy: true });
} else {
userLoc = { lat: 24.1369088, lng: 120.6550528 };
initMap(userLoc, LabData);
}
console.log("INIT");
window.initMap = initMap;
}
function filterChange(_radio) {
filterValue = _radio.value;
//Redraw map
if (map != undefined) {
map.remove();
if ($(window).width() < 992) {
$("#nearlab_card_mobile").empty();
} else {
$("#nearlab_card").empty();
}
HTTPFetch();
}
}
function googleMapNav(address) {
var navUrl = `https://www.google.com/maps/dir/${userLoc["lat"]},${userLoc["lng"]}/${address}`;
window.open(navUrl, "Navigator")
}