index.vue 3.9 KB

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