index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <div class="login-container">
  3. <el-form
  4. ref="loginForm"
  5. :model="loginForm"
  6. :rules="loginRules"
  7. class="login-form"
  8. autocomplete="on"
  9. label-position="left"
  10. >
  11. <div class="title-container">
  12. <h3 class="title">{{ $t('login') }}</h3>
  13. </div>
  14. <el-form-item prop="user_name">
  15. <el-input
  16. ref="user_name"
  17. v-model="loginForm.user_name"
  18. type="text"
  19. name="user_name"
  20. autocomplete="on"
  21. :placeholder="$t('username')"
  22. />
  23. </el-form-item>
  24. <el-form-item prop="password">
  25. <el-input
  26. ref="password"
  27. v-model="loginForm.password"
  28. type="password"
  29. name="password"
  30. :placeholder="$t('password')"
  31. autocomplete="on"
  32. @keyup.enter.native="handleLogin"
  33. />
  34. </el-form-item>
  35. <el-row>
  36. <el-col :span="12">
  37. <el-button
  38. class="login-button"
  39. type="primary"
  40. :loading="loading"
  41. @click.native.prevent="handleLogin('TEACHER')"
  42. >
  43. {{ $t('Learn_TLogin') }}
  44. </el-button>
  45. </el-col>
  46. <el-col :span="12">
  47. <el-button
  48. class="login-button"
  49. type="primary"
  50. :loading="loading"
  51. @click.native.prevent="handleLogin('STUDENT')"
  52. >
  53. {{ $t('Learn_SLogin') }}
  54. </el-button>
  55. </el-col>
  56. </el-row>
  57. </el-form>
  58. </div>
  59. </template>
  60. <script>
  61. import { updateWordPack } from '@/utils/i18n';
  62. export default {
  63. data() {
  64. const validateUsername = (rule, value, callback) => {
  65. if (!value) {
  66. callback(new Error(this.$i18n.t('Learn_Login_user_warning')));
  67. } else {
  68. callback();
  69. }
  70. };
  71. const validatePassword = (rule, value, callback) => {
  72. if (value.length < 6) {
  73. callback(new Error(this.$i18n.t('Learn_Login_password_warning')));
  74. } else {
  75. callback();
  76. }
  77. };
  78. return {
  79. loginForm: {
  80. user_name: '',
  81. password: '',
  82. user_type: ''
  83. },
  84. loginRules: {
  85. user_name: [{ require: true, trigger: 'blur', validator: validateUsername }],
  86. password: [{ require: true, trigger: 'blur', validator: validatePassword }]
  87. },
  88. loading: false,
  89. redirect: null
  90. };
  91. },
  92. watch: {
  93. $router: {
  94. handler: function (router) {
  95. this.redirect = router.query && router.query.redirect;
  96. }
  97. }
  98. },
  99. created() {
  100. updateWordPack({
  101. word_key_list: [
  102. 'login',
  103. 'password',
  104. 'username',
  105. 'Learn_TLogin',
  106. 'Learn_SLogin',
  107. 'Learn_Login_user_warning',
  108. 'Learn_Login_password_warning'
  109. ]
  110. });
  111. },
  112. methods: {
  113. handleLogin(user_type) {
  114. this.$refs.loginForm.validate(valid => {
  115. if (valid) {
  116. this.loginForm.user_type = user_type;
  117. this.loading = true;
  118. this.$store
  119. .dispatch('user/login', { loginForm: this.loginForm })
  120. .then(() => {
  121. this.$router.push({ path: this.redirect || '/' });
  122. this.loading = false;
  123. this.$message.success('登录成功!');
  124. })
  125. .catch(() => {
  126. this.loading = false;
  127. });
  128. } else {
  129. return false;
  130. }
  131. });
  132. }
  133. }
  134. };
  135. </script>
  136. <style lang="scss">
  137. .login-container {
  138. background-color: #f5f5f5;
  139. min-height: 100%;
  140. width: 100%;
  141. overflow: hidden;
  142. .title-container {
  143. position: relative;
  144. .title {
  145. font-size: 26px;
  146. color: #999;
  147. margin: 0 auto 40px auto;
  148. text-align: center;
  149. font-weight: bold;
  150. }
  151. }
  152. .login-form {
  153. position: relative;
  154. width: 520px;
  155. max-width: 100%;
  156. padding: 160px 35px 0;
  157. margin: 0 auto;
  158. overflow: hidden;
  159. }
  160. .login-button {
  161. display: block;
  162. margin: 0 auto;
  163. }
  164. }
  165. </style>