index.vue 5.4 KB

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