125 lines
2.2 KiB
Go
125 lines
2.2 KiB
Go
package model
|
|
|
|
import "fmt"
|
|
|
|
func GetTemplate1(
|
|
widgetID WidgetID,
|
|
backgroundUrl MediaUrl,
|
|
) WidgetHTML {
|
|
|
|
style := fmt.Sprintf(`body {
|
|
margin: 0;
|
|
padding: 0;
|
|
height: 100vh;
|
|
display: flex;
|
|
justify-content: center;
|
|
align-items: flex-start;
|
|
background-image: url('%s');
|
|
background-repeat: no-repeat;
|
|
background-size: cover;
|
|
}
|
|
#content {
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
margin-top: 100px
|
|
}
|
|
|
|
#content img {
|
|
width: 50vw;
|
|
height: 50vh;
|
|
object-fit: cover;
|
|
}
|
|
|
|
#content audio {
|
|
display: none;
|
|
}
|
|
|
|
#content p {
|
|
font-size: 60px;
|
|
}
|
|
`, backgroundUrl)
|
|
|
|
script := fmt.Sprintf(`let widgetID = '%v'
|
|
let baseUrl = 'http://localhost:8002/api/widget'
|
|
|
|
function delay(ms) {
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
}
|
|
|
|
async function getWidgetInfo() {
|
|
let response = await fetch(baseUrl + '/info/' + widgetID);
|
|
let widget = await response.json();
|
|
return widget
|
|
}
|
|
|
|
function addImage(imageUrl) {
|
|
img = document.createElement('img');
|
|
img.src = imageUrl + '?t=' + new Date().getTime();
|
|
contentDiv.appendChild(img);
|
|
}
|
|
|
|
function addText(text) {
|
|
p = document.createElement('p');
|
|
p.innerHTML = text;
|
|
contentDiv.appendChild(p);
|
|
}
|
|
|
|
function addAudio(audioUrl) {
|
|
audio = document.createElement('audio');
|
|
audio.src = audioUrl;
|
|
audio.autoplay = true;
|
|
audio.controls = false;
|
|
contentDiv.appendChild(audio);
|
|
}
|
|
|
|
async function endDonat(donatID) {
|
|
if (audio) {
|
|
audio.pause();
|
|
audio.remove();
|
|
}
|
|
while (contentDiv.firstChild) {
|
|
contentDiv.removeChild(contentDiv.firstChild);
|
|
}
|
|
let response = await fetch(baseUrl + '/donat/delete/' + String(donatID), {method: 'DELETE'});
|
|
}
|
|
|
|
let audio;
|
|
const contentDiv = document.getElementById('content');
|
|
|
|
async function widgetView() {
|
|
while (true) {
|
|
let widget = await getWidgetInfo()
|
|
console.log(widget);
|
|
if (!widget.display) {
|
|
await delay(5 * 1000);
|
|
continue
|
|
}
|
|
|
|
addImage(widget.imageUrl)
|
|
addText(widget.text + widget.amount)
|
|
addAudio(widget.audioUrl)
|
|
|
|
await delay(widget.duration * 1000);
|
|
await endDonat(widget.donatID)
|
|
}
|
|
}
|
|
widgetView()`, widgetID)
|
|
|
|
template1 := fmt.Sprintf(`<!DOCTYPE html>
|
|
<html>
|
|
<style>
|
|
%s
|
|
</style>
|
|
<body>
|
|
<div id='content'>
|
|
|
|
</div>
|
|
<script>
|
|
%s
|
|
</script>
|
|
</body>
|
|
</html>`, style, script)
|
|
return WidgetHTML(template1)
|
|
}
|