App.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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: false,
  23. stations: {
  24. official: [],
  25. community: []
  26. }
  27. }
  28. },
  29. methods: {
  30. logout: function () {
  31. this.socket.emit('users.logout');
  32. document.cookie = 'SID=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
  33. location.reload();
  34. }
  35. },
  36. ready: function () {
  37. lofig.get('socket.url', res => {
  38. let socket = this.socket = io(window.location.protocol + '//' + res);
  39. socket.on("ready", status => this.loggedIn = status);
  40. socket.emit("stations.index", data => {
  41. if (data.status === "success") data.stations.forEach(station => {
  42. if (station.type == 'official') this.stations.official.push(station);
  43. else this.stations.community.push(station);
  44. });
  45. });
  46. });
  47. },
  48. events: {
  49. 'register': function () {
  50. let { register: { email, username, password } } = this;
  51. this.socket.emit('users.register', email, username, password, grecaptcha.getResponse(), (result) => {
  52. console.log(result);
  53. location.reload();
  54. });
  55. },
  56. 'login': function () {
  57. let { login: { email, password } } = this;
  58. this.socket.emit('users.login', email, password, (result) => {
  59. console.log(result);
  60. if (result.status === 'success') {
  61. let date = new Date();
  62. date.setTime(new Date().getTime() + (2*365*24*60*60*1000));
  63. document.cookie = "SID=" + result.sessionId + "; expires="+ date.toGMTString() +"; path=/";
  64. location.reload();
  65. } else {
  66. //TODO Error toast
  67. }
  68. });
  69. },
  70. 'joinStation': function (id) {
  71. let mergedStations = this.stations.community.concat(this.stations.official);
  72. this.socket.emit('stations.join', id, result => {
  73. mergedStations.find(station => station.id === id).users = result.userCount;
  74. });
  75. },
  76. 'leaveStation': function () {
  77. this.socket.emit('stations.leave', result => {
  78. //this.stations.find(station => station.id === id).users = result.userCount;
  79. });
  80. }
  81. }
  82. }
  83. </script>