index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. <div class="title-container">
  5. <h3 class="title">Login Form</h3>
  6. </div>
  7. <el-form-item prop="username">
  8. <span class="svg-container">
  9. <svg-icon icon-class="user" />
  10. </span>
  11. <el-input
  12. ref="username"
  13. v-model="loginForm.username"
  14. placeholder="Username"
  15. name="username"
  16. type="text"
  17. tabindex="1"
  18. auto-complete="on"
  19. />
  20. </el-form-item>
  21. <el-form-item prop="password">
  22. <span class="svg-container">
  23. <svg-icon icon-class="password" />
  24. </span>
  25. <el-input
  26. :key="passwordType"
  27. ref="password"
  28. v-model="loginForm.password"
  29. :type="passwordType"
  30. placeholder="Password"
  31. name="password"
  32. tabindex="2"
  33. auto-complete="on"
  34. @keyup.enter.native="handleLogin"
  35. />
  36. <span class="show-pwd" @click="showPwd">
  37. <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
  38. </span>
  39. </el-form-item>
  40. <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button>
  41. <div class="tips">
  42. <span style="margin-right:20px;">username: admin</span>
  43. <span> password: any</span>
  44. </div>
  45. </el-form>
  46. </div>
  47. </template>
  48. <script>
  49. import { validUsername } from '@/utils/validate'
  50. export default {
  51. name: 'Login',
  52. data() {
  53. const validateUsername = (rule, value, callback) => {
  54. if (!validUsername(value)) {
  55. callback(new Error('Please enter the correct user name'))
  56. } else {
  57. callback()
  58. }
  59. }
  60. const validatePassword = (rule, value, callback) => {
  61. if (value.length < 6) {
  62. callback(new Error('The password can not be less than 6 digits'))
  63. } else {
  64. callback()
  65. }
  66. }
  67. return {
  68. loginForm: {
  69. username: 'admin',
  70. password: '111111'
  71. },
  72. loginRules: {
  73. username: [{ required: true, trigger: 'blur', validator: validateUsername }],
  74. password: [{ required: true, trigger: 'blur', validator: validatePassword }]
  75. },
  76. loading: false,
  77. passwordType: 'password',
  78. redirect: undefined
  79. }
  80. },
  81. watch: {
  82. $route: {
  83. handler: function(route) {
  84. this.redirect = route.query && route.query.redirect
  85. },
  86. immediate: true
  87. }
  88. },
  89. methods: {
  90. showPwd() {
  91. if (this.passwordType === 'password') {
  92. this.passwordType = ''
  93. } else {
  94. this.passwordType = 'password'
  95. }
  96. this.$nextTick(() => {
  97. this.$refs.password.focus()
  98. })
  99. },
  100. handleLogin() {
  101. this.$refs.loginForm.validate(valid => {
  102. if (valid) {
  103. this.loading = true
  104. this.$store.dispatch('user/login', this.loginForm).then(() => {
  105. this.$router.push({ path: this.redirect || '/' })
  106. this.loading = false
  107. }).catch(() => {
  108. this.loading = false
  109. })
  110. } else {
  111. console.log('error submit!!')
  112. return false
  113. }
  114. })
  115. }
  116. }
  117. }
  118. </script>
  119. <style lang="scss">
  120. /* 修复input 背景不协调 和光标变色 */
  121. /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
  122. $bg:#283443;
  123. $light_gray:#fff;
  124. $cursor: #fff;
  125. @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  126. .login-container .el-input input {
  127. color: $cursor;
  128. }
  129. }
  130. /* reset element-ui css */
  131. .login-container {
  132. .el-input {
  133. display: inline-block;
  134. height: 47px;
  135. width: 85%;
  136. input {
  137. background: transparent;
  138. border: 0px;
  139. -webkit-appearance: none;
  140. border-radius: 0px;
  141. padding: 12px 5px 12px 15px;
  142. color: $light_gray;
  143. height: 47px;
  144. caret-color: $cursor;
  145. &:-webkit-autofill {
  146. box-shadow: 0 0 0px 1000px $bg inset !important;
  147. -webkit-text-fill-color: $cursor !important;
  148. }
  149. }
  150. }
  151. .el-form-item {
  152. border: 1px solid rgba(255, 255, 255, 0.1);
  153. background: rgba(0, 0, 0, 0.1);
  154. border-radius: 5px;
  155. color: #454545;
  156. }
  157. }
  158. </style>
  159. <style lang="scss" scoped>
  160. $bg:#2d3a4b;
  161. $dark_gray:#889aa4;
  162. $light_gray:#eee;
  163. .login-container {
  164. min-height: 100%;
  165. width: 100%;
  166. background-color: $bg;
  167. overflow: hidden;
  168. .login-form {
  169. position: relative;
  170. width: 520px;
  171. max-width: 100%;
  172. padding: 160px 35px 0;
  173. margin: 0 auto;
  174. overflow: hidden;
  175. }
  176. .tips {
  177. font-size: 14px;
  178. color: #fff;
  179. margin-bottom: 10px;
  180. span {
  181. &:first-of-type {
  182. margin-right: 16px;
  183. }
  184. }
  185. }
  186. .svg-container {
  187. padding: 6px 5px 6px 15px;
  188. color: $dark_gray;
  189. vertical-align: middle;
  190. width: 30px;
  191. display: inline-block;
  192. }
  193. .title-container {
  194. position: relative;
  195. .title {
  196. font-size: 26px;
  197. color: $light_gray;
  198. margin: 0px auto 40px auto;
  199. text-align: center;
  200. font-weight: bold;
  201. }
  202. }
  203. .show-pwd {
  204. position: absolute;
  205. right: 10px;
  206. top: 7px;
  207. font-size: 16px;
  208. color: $dark_gray;
  209. cursor: pointer;
  210. user-select: none;
  211. }
  212. }
  213. </style>