index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <div class="login-container">
  3. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
  4. <h3 class="title">vue-admin-template</h3>
  5. <el-form-item prop="username">
  6. <span class="svg-container svg-container_login">
  7. <svg-icon icon-class="user" />
  8. </span>
  9. <el-input v-model="loginForm.username" name="username" type="text" auto-complete="on" placeholder="username" />
  10. </el-form-item>
  11. <el-form-item prop="password">
  12. <span class="svg-container">
  13. <svg-icon icon-class="password" />
  14. </span>
  15. <el-input
  16. :type="pwdType"
  17. v-model="loginForm.password"
  18. name="password"
  19. auto-complete="on"
  20. placeholder="password"
  21. @keyup.enter.native="handleLogin" />
  22. <span class="show-pwd" @click="showPwd">
  23. <svg-icon icon-class="eye" />
  24. </span>
  25. </el-form-item>
  26. <el-form-item>
  27. <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
  28. Sign in
  29. </el-button>
  30. </el-form-item>
  31. <div class="tips">
  32. <span style="margin-right:20px;">username: admin</span>
  33. <span> password: admin</span>
  34. </div>
  35. </el-form>
  36. </div>
  37. </template>
  38. <script>
  39. import { isvalidUsername } from '@/utils/validate'
  40. export default {
  41. name: 'Login',
  42. data() {
  43. const validateUsername = (rule, value, callback) => {
  44. if (!isvalidUsername(value)) {
  45. callback(new Error('请输入正确的用户名'))
  46. } else {
  47. callback()
  48. }
  49. }
  50. const validatePass = (rule, value, callback) => {
  51. if (value.length < 5) {
  52. callback(new Error('密码不能小于5位'))
  53. } else {
  54. callback()
  55. }
  56. }
  57. return {
  58. loginForm: {
  59. username: 'admin',
  60. password: 'admin'
  61. },
  62. loginRules: {
  63. username: [{ required: true, trigger: 'blur', validator: validateUsername }],
  64. password: [{ required: true, trigger: 'blur', validator: validatePass }]
  65. },
  66. loading: false,
  67. pwdType: 'password',
  68. redirect: undefined
  69. }
  70. },
  71. watch: {
  72. $route: {
  73. handler: function(route) {
  74. this.redirect = route.query && route.query.redirect
  75. },
  76. immediate: true
  77. }
  78. },
  79. methods: {
  80. showPwd() {
  81. if (this.pwdType === 'password') {
  82. this.pwdType = ''
  83. } else {
  84. this.pwdType = 'password'
  85. }
  86. },
  87. handleLogin() {
  88. this.$refs.loginForm.validate(valid => {
  89. if (valid) {
  90. this.loading = true
  91. this.$store.dispatch('Login', this.loginForm).then(() => {
  92. this.loading = false
  93. this.$router.push({ path: this.redirect || '/' })
  94. }).catch(() => {
  95. this.loading = false
  96. })
  97. } else {
  98. console.log('error submit!!')
  99. return false
  100. }
  101. })
  102. }
  103. }
  104. }
  105. </script>
  106. <style rel="stylesheet/scss" lang="scss">
  107. $bg:#2d3a4b;
  108. $light_gray:#eee;
  109. /* reset element-ui css */
  110. .login-container {
  111. .el-input {
  112. display: inline-block;
  113. height: 47px;
  114. width: 85%;
  115. input {
  116. background: transparent;
  117. border: 0px;
  118. -webkit-appearance: none;
  119. border-radius: 0px;
  120. padding: 12px 5px 12px 15px;
  121. color: $light_gray;
  122. height: 47px;
  123. &:-webkit-autofill {
  124. -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
  125. -webkit-text-fill-color: #fff !important;
  126. }
  127. }
  128. }
  129. .el-form-item {
  130. border: 1px solid rgba(255, 255, 255, 0.1);
  131. background: rgba(0, 0, 0, 0.1);
  132. border-radius: 5px;
  133. color: #454545;
  134. }
  135. }
  136. </style>
  137. <style rel="stylesheet/scss" lang="scss" scoped>
  138. $bg:#2d3a4b;
  139. $dark_gray:#889aa4;
  140. $light_gray:#eee;
  141. .login-container {
  142. position: fixed;
  143. height: 100%;
  144. width: 100%;
  145. background-color: $bg;
  146. .login-form {
  147. position: absolute;
  148. left: 0;
  149. right: 0;
  150. width: 520px;
  151. max-width: 100%;
  152. padding: 35px 35px 15px 35px;
  153. margin: 120px auto;
  154. }
  155. .tips {
  156. font-size: 14px;
  157. color: #fff;
  158. margin-bottom: 10px;
  159. span {
  160. &:first-of-type {
  161. margin-right: 16px;
  162. }
  163. }
  164. }
  165. .svg-container {
  166. padding: 6px 5px 6px 15px;
  167. color: $dark_gray;
  168. vertical-align: middle;
  169. width: 30px;
  170. display: inline-block;
  171. &_login {
  172. font-size: 20px;
  173. }
  174. }
  175. .title {
  176. font-size: 26px;
  177. font-weight: 400;
  178. color: $light_gray;
  179. margin: 0px auto 40px auto;
  180. text-align: center;
  181. font-weight: bold;
  182. }
  183. .show-pwd {
  184. position: absolute;
  185. right: 10px;
  186. top: 7px;
  187. font-size: 16px;
  188. color: $dark_gray;
  189. cursor: pointer;
  190. user-select: none;
  191. }
  192. }
  193. </style>