WebVersionLoader.qml 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import QtQuick 2.0
  2. Item {
  3. id: versionLoader
  4. signal error()
  5. readonly property string url: isDebug
  6. ? "http://localhost:8000/resource.json"
  7. : "https://sdk-os-static.mihoyo.com/hk4e_global/mdk/launcher/api/resource?key=gcStgarh&launcher_id=10"
  8. property var json
  9. property bool isLoading: false
  10. function loadVersion() {
  11. isLoading = true
  12. var xhr = new XMLHttpRequest();
  13. xhr.onreadystatechange = ((response) => {
  14. return () => {
  15. if (response.readyState === 4) {
  16. isLoading = false
  17. try {
  18. onResponseLoaded(JSON.parse(response.responseText))
  19. } catch(e) {
  20. console.error(e)
  21. error()
  22. }
  23. }
  24. }
  25. })(xhr)
  26. xhr.open('GET', url, true);
  27. xhr.send('');
  28. }
  29. function onResponseLoaded(json) {
  30. versionLoader.json = json
  31. // console.log(JSON.stringify(json, null, 2))
  32. }
  33. Component.onCompleted: console.log(url)
  34. }