Login.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <div class="login" id="login">
  3. <div class="limiter">
  4. <div class="container-login100">
  5. <div class="wrap-login100">
  6. <span class="login100-form-title p-b-26">WVP视频平台</span>
  7. <span class="login100-form-title p-b-48">
  8. <i class="fa fa-video-camera"></i>
  9. </span>
  10. <div class="wrap-input100 validate-input" data-validate = "Valid email is: a@b.c">
  11. <input :class="'input100 ' + (username==''?'':'has-val')" type="text" v-model="username" name="username">
  12. <span class="focus-input100" data-placeholder="用户名"></span>
  13. </div>
  14. <div class="wrap-input100 validate-input" data-validate="Enter password">
  15. <span class="btn-show-pass">
  16. <i :class="'fa ' + (!showPassword?'fa-eye':'fa-eye-slash')" @click="showPassword = !showPassword"></i>
  17. </span>
  18. <input :class="'input100 ' + (password==''?'':'has-val')" :type="(!showPassword?'password':'text')" v-model="password" name="password">
  19. <span class="focus-input100" data-placeholder="密码"></span>
  20. </div>
  21. <div class="container-login100-form-btn">
  22. <div class="wrap-login100-form-btn" :class="{'login-loading': isLoging}" v-loading="isLoging" element-loading-background="rgb(0 0 0 / 0%);" element-loading-custom-class="login-loading-class">
  23. <div class="login100-form-bgbtn"></div>
  24. <button class="login100-form-btn" @click="login">登录</button>
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. </template>
  32. <script>
  33. import crypto from 'crypto'
  34. import userService from "./service/UserService";
  35. export default {
  36. name: 'Login',
  37. data(){
  38. return {
  39. isLoging: false,
  40. showPassword: false,
  41. loginLoading: false,
  42. username: '',
  43. password: ''
  44. }
  45. },
  46. created(){
  47. var that = this;
  48. document.onkeydown = function(e) {
  49. var key = window.event.keyCode;
  50. if (key == 13) {
  51. that.login();
  52. }
  53. }
  54. },
  55. methods:{
  56. //登录逻辑
  57. login(){
  58. if(this.username!='' && this.password!=''){
  59. this.toLogin();
  60. }
  61. },
  62. //登录请求
  63. toLogin(){
  64. //需要想后端发送的登录参数
  65. let loginParam = {
  66. username: this.username,
  67. password: crypto.createHash('md5').update(this.password, "utf8").digest('hex')
  68. }
  69. var that = this;
  70. //设置在登录状态
  71. this.isLoging = true;
  72. let timeoutTask = setTimeout(()=>{
  73. that.$message.error("登录超时");
  74. that.isLoging = false;
  75. }, 1000)
  76. this.$axios({
  77. method: 'get',
  78. url:"/api/user/login",
  79. params: loginParam
  80. }).then(function (res) {
  81. window.clearTimeout(timeoutTask)
  82. console.log(res);
  83. console.log("登录成功");
  84. if (res.data.code === 0 ) {
  85. userService.setUser(res.data.data)
  86. //登录成功后
  87. that.cancelEnterkeyDefaultAction();
  88. that.$router.push('/');
  89. }else{
  90. that.isLoging = false;
  91. that.$message({
  92. showClose: true,
  93. message: '登录失败,用户名或密码错误',
  94. type: 'error'
  95. });
  96. }
  97. }).catch(function (error) {
  98. console.log(error)
  99. window.clearTimeout(timeoutTask)
  100. that.$message.error(error.response.data.msg);
  101. that.isLoging = false;
  102. });
  103. },
  104. cancelEnterkeyDefaultAction: function() {
  105. document.onkeydown = function(e) {
  106. var key = window.event.keyCode;
  107. if (key == 13) {
  108. return false;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. </script>