index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 ref="username" v-model="loginForm.username" placeholder="请输入您的账号" name="username" type="text"
  13. tabindex="1" auto-complete="on" />
  14. </el-form-item>
  15. <el-form-item prop="password">
  16. <span class="svg-container">
  17. <svg-icon icon-class="password" />
  18. </span>
  19. <el-input :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
  20. placeholder="请输入登录密码" name="password" tabindex="2" auto-complete="on"
  21. @keyup.enter.native="handleLogin" />
  22. <span class="show-pwd" @click="showPwd">
  23. <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
  24. </span>
  25. </el-form-item>
  26. <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
  27. @click.native.prevent="handleLogin">Login</el-button>
  28. <!-- <div class="tips">
  29. <span style="margin-right:20px;">username: admin</span>
  30. <span>password: any</span>
  31. </div> -->
  32. </el-form>
  33. </div>
  34. </template>
  35. <script>
  36. import {
  37. validUsername
  38. } from '@/utils/validate'
  39. export default {
  40. name: 'Login',
  41. data() {
  42. const validateUsername = (rule, value, callback) => {
  43. if (!validUsername(value)) {
  44. callback(new Error('请输入正确的账号'))
  45. } else {
  46. callback()
  47. }
  48. }
  49. const validatePassword = (rule, value, callback) => {
  50. if (value.length < 6) {
  51. callback(new Error('请输入正确的密码'))
  52. } else {
  53. callback()
  54. }
  55. }
  56. return {
  57. loginForm: {},
  58. loginRules: {
  59. username: [{
  60. required: true,
  61. trigger: 'blur',
  62. validator: validateUsername
  63. }],
  64. password: [{
  65. required: true,
  66. trigger: 'blur',
  67. validator: validatePassword
  68. }]
  69. },
  70. loading: false,
  71. passwordType: 'password',
  72. redirect: undefined
  73. }
  74. },
  75. watch: {
  76. $route: {
  77. handler: function(route) {
  78. this.redirect = route.query && route.query.redirect
  79. },
  80. immediate: true
  81. }
  82. },
  83. methods: {
  84. showPwd() {
  85. if (this.passwordType === 'password') {
  86. this.passwordType = ''
  87. } else {
  88. this.passwordType = 'password'
  89. }
  90. this.$nextTick(() => {
  91. this.$refs.password.focus()
  92. })
  93. },
  94. handleLogin() {
  95. this.$refs.loginForm.validate(valid => {
  96. if (valid) {
  97. this.loginForm.companyName = '黑龙江中天昊元贸易有限公司'
  98. this.loading = true
  99. this.$store.dispatch('user/login', this.loginForm)
  100. .then(response => {
  101. localStorage.setItem('UserInfo', JSON.stringify(response.data))
  102. this.$router.push({
  103. path: this.redirect || '/'
  104. })
  105. this.loading = false
  106. })
  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>