App.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <div>
  3. <router-view></router-view>
  4. </div>
  5. </template>
  6. <script>
  7. export default {
  8. replace: false,
  9. data() {
  10. return {
  11. register: {
  12. email: "",
  13. username: "",
  14. password: ""
  15. },
  16. login: {
  17. email: "",
  18. password: ""
  19. },
  20. likes: [],
  21. dislikes: [],
  22. loggedIn: true,
  23. stations: []
  24. }
  25. },
  26. methods: {
  27. logout() {
  28. $.ajax({
  29. method: "POST",
  30. url: `${window.location.protocol + '//' + window.location.hostname + ':8081'}/users/logout`,
  31. dataType: "json",
  32. complete: msg => {
  33. alert("Logged out!");
  34. location.reload();
  35. }
  36. });
  37. }
  38. },
  39. ready: function() {
  40. let local = this;
  41. local.socket = io(window.location.protocol + '//' + window.location.hostname + ':8081');
  42. local.socket.on("ready", status => {
  43. local.loggedIn = status;
  44. });
  45. local.socket.emit("/stations", function(data) {
  46. local.stations = data;
  47. });
  48. },
  49. events: {
  50. 'register': function() {
  51. $.ajax({
  52. method: "POST",
  53. url: `${window.location.protocol + '//' + window.location.hostname + ':8081'}/users/register`,
  54. data: JSON.stringify({
  55. email: this.register.email,
  56. username: this.register.username,
  57. password: this.register.password,
  58. recaptcha: grecaptcha.getResponse()
  59. }),
  60. contentType: "application/json; charset=utf-8",
  61. dataType: "json",
  62. success: function (msg) {
  63. if (msg) console.log(msg);
  64. },
  65. error: function (err) {
  66. if (err) console.log(err);
  67. alert("Not registered!");
  68. }
  69. });
  70. },
  71. 'login': function() {
  72. $.ajax({
  73. method: "POST",
  74. url: `${window.location.protocol + '//' + window.location.hostname + ':8081'}/users/login`,
  75. data: JSON.stringify({
  76. email: this.login.email,
  77. password: this.login.password
  78. }),
  79. contentType: "application/json; charset=utf-8",
  80. dataType: "json",
  81. success: function (msg) {
  82. if (msg) console.log(msg);
  83. location.reload();
  84. //do something
  85. },
  86. error: function (err) {
  87. if (err) console.log(err);
  88. alert("Not logged in!");
  89. }
  90. });
  91. },
  92. 'joinStation': function(id) {
  93. let local = this;
  94. local.socket.emit('/stations/join/:id', id, (result) => {
  95. local.stations.forEach(function(station) {
  96. if (station.id === id) {
  97. station.users = result;
  98. }
  99. });
  100. });
  101. },
  102. 'leaveStation': function(id) {
  103. let local = this;
  104. local.socket.emit('/stations/leave/:id', id, (result) => {
  105. local.stations.forEach(function(station) {
  106. if (station.id === id) {
  107. station.users = result;
  108. }
  109. });
  110. });
  111. }
  112. }
  113. }
  114. </script>