index.vue 5.3 KB

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