index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <template>
  2. <div class="login-container">
  3. <div class="title-container">
  4. <h3 class="title"><el-image class="logo" :src="logoImage" /></h3>
  5. </div>
  6. <div class="login-form-wrap">
  7. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
  8. <div class="title-container">
  9. <h3 class="form-title">用户登录</h3>
  10. </div>
  11. <el-form-item prop="username">
  12. <span class="svg-container">
  13. <svg-icon icon-class="user" />
  14. </span>
  15. <el-input
  16. ref="username"
  17. v-model="loginForm.username"
  18. placeholder="Username"
  19. name="username"
  20. type="text"
  21. tabindex="1"
  22. auto-complete="on"
  23. />
  24. </el-form-item>
  25. <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
  26. <el-form-item prop="password">
  27. <span class="svg-container">
  28. <svg-icon icon-class="password" />
  29. </span>
  30. <el-input
  31. :key="passwordType"
  32. ref="password"
  33. v-model="loginForm.password"
  34. :type="passwordType"
  35. placeholder="密码"
  36. name="password"
  37. tabindex="2"
  38. auto-complete="on"
  39. @keyup.native="checkCapslock"
  40. @blur="capsTooltip = false"
  41. @keyup.enter.native="handleLogin"
  42. />
  43. <span class="show-pwd" @click="showPwd">
  44. <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
  45. </span>
  46. </el-form-item>
  47. </el-tooltip>
  48. <p class="tip-text">
  49. <span>*</span> 忘记密码可咨询管理人员重新获取
  50. </p>
  51. <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">
  52. 登录
  53. </el-button>
  54. <div class="tips">
  55. <span style="margin-right:20px;">username: admin</span>
  56. <span> password: any</span>
  57. </div>
  58. </el-form>
  59. </div>
  60. </div>
  61. </template>
  62. <script>
  63. import { mapGetters } from 'vuex'
  64. export default {
  65. name: 'Login',
  66. data() {
  67. const validateUsername = (rule, value, callback) => {
  68. if (value.length < 1) {
  69. callback(new Error('用户名不能为空'))
  70. } else {
  71. callback()
  72. }
  73. }
  74. const validatePassword = (rule, value, callback) => {
  75. if (value.length < 6) {
  76. callback(new Error('密码不能少于6位'))
  77. } else {
  78. callback()
  79. }
  80. }
  81. return {
  82. loginForm: {
  83. username: '',
  84. password: ''
  85. },
  86. loginRules: {
  87. username: [{ required: true, trigger: 'blur', validator: validateUsername }],
  88. password: [{ required: true, trigger: 'blur', validator: validatePassword }]
  89. },
  90. loading: false,
  91. passwordType: 'password',
  92. capsTooltip: false,
  93. showDialog: false,
  94. redirect: undefined,
  95. logoImage: require('@/assets/images/logo/logo.png'),
  96. otherQuery: {}
  97. }
  98. },
  99. computed: {
  100. ...mapGetters([
  101. 'permission_routes',
  102. 'permits'
  103. ])
  104. },
  105. watch: {
  106. $route: {
  107. handler: function(route) {
  108. // this.redirect = route.query && route.query.redirect
  109. // console.log(route.query)
  110. if (route.query.username) {
  111. this.$store.dispatch('user/login', {
  112. username: route.query.username,
  113. password: route.query.password
  114. }).then(async(res) => {
  115. this.loading = false
  116. this.$router.push({ path: '/', query: this.otherQuery })
  117. }).catch(() => {
  118. this.loading = false
  119. })
  120. }
  121. },
  122. immediate: true
  123. }
  124. },
  125. mounted() {
  126. if (this.loginForm.username === '') {
  127. this.$refs.username.focus()
  128. } else if (this.loginForm.password === '') {
  129. this.$refs.password.focus()
  130. }
  131. },
  132. destroyed() {
  133. localStorage.clear()
  134. // window.removeEventListener('storage', this.afterQRScan)
  135. },
  136. methods: {
  137. checkCapslock(e) {
  138. const { key } = e
  139. this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
  140. },
  141. showPwd() {
  142. if (this.passwordType === 'password') {
  143. this.passwordType = ''
  144. } else {
  145. this.passwordType = 'password'
  146. }
  147. this.$nextTick(() => {
  148. this.$refs.password.focus()
  149. })
  150. },
  151. handleLogin() {
  152. this.$refs.loginForm.validate(valid => {
  153. if (valid) {
  154. this.loading = true
  155. this.$store.dispatch('user/login', this.loginForm).then(async(res) => {
  156. this.loading = false
  157. this.$router.push({ path: '/', query: this.otherQuery })
  158. }).catch(() => {
  159. this.loading = false
  160. })
  161. } else {
  162. return false
  163. }
  164. })
  165. },
  166. async detectionRedirectPath() {
  167. const permits = await this.$store.dispatch('user/getRoute')
  168. const accessRoutes = await this.$store.dispatch('permission/generateRoutes', permits)
  169. const routes = accessRoutes.map(item => item.path)
  170. const redirect = routes.includes(this.redirect) ? this.redirect : '/'
  171. this.$router.addRoutes(accessRoutes)
  172. this.$router.push({ path: redirect, query: this.otherQuery })
  173. this.loading = false
  174. },
  175. getOtherQuery(query) {
  176. return Object.keys(query).reduce((acc, cur) => {
  177. if (cur !== 'redirect') {
  178. acc[cur] = query[cur]
  179. }
  180. return acc
  181. }, {})
  182. }
  183. }
  184. }
  185. </script>
  186. <style lang="scss">
  187. /* 修复input 背景不协调 和光标变色 */
  188. /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
  189. $bg:#283443;
  190. $light_gray:#fff;
  191. $cursor: #fff;
  192. @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  193. .login-container .el-input input {
  194. color: $cursor;
  195. }
  196. }
  197. /* reset element-ui css */
  198. .login-container {
  199. .el-input {
  200. display: inline-block;
  201. height: 47px;
  202. width: 85%;
  203. background:none;
  204. border:none;
  205. input {
  206. background: transparent;
  207. border: 0px;
  208. -webkit-appearance: none;
  209. border-radius: 0px;
  210. padding: 12px 5px 12px 15px;
  211. color: $light_gray;
  212. height: 47px;
  213. caret-color: $cursor;
  214. &:-webkit-autofill {
  215. box-shadow: 0 0 0px 1000px $bg inset !important;
  216. -webkit-text-fill-color: $cursor !important;
  217. }
  218. }
  219. }
  220. .el-form-item {
  221. border: 1px solid rgba(255, 255, 255, 0.1);
  222. background: rgba(0, 0, 0, 0.1);
  223. border-radius: 5px;
  224. color: #454545;
  225. }
  226. }
  227. </style>
  228. <style lang="scss" scoped>
  229. $bg: #2d3a4b;
  230. $dark_gray: #889aa4;
  231. $light_gray: #eee;
  232. .login-container {
  233. min-height: 100%;
  234. width: 100%;
  235. // background-image: url("../../assets/images/login/login_ornament.png");
  236. background-size: 910px 870px;
  237. background-repeat: no-repeat;
  238. background-color: #071A29;
  239. background-position:344px 106px;
  240. overflow: hidden;
  241. position: relative;
  242. .login-form-wrap {
  243. position: absolute;
  244. background: rgba(255, 255, 255, .08);
  245. right: 10%;
  246. top: 30%;
  247. padding: 10px;
  248. }
  249. .login-form {
  250. width: 367px;
  251. height: 319px;
  252. max-width: 100%;
  253. padding: 30px;
  254. margin: 0 auto;
  255. overflow: hidden;
  256. background: rgba(255, 255, 255, .08);
  257. border-radius: 5px;
  258. .icon-image {
  259. width: 20px;
  260. height: 20px;
  261. position: relative;
  262. top: 6px;
  263. }
  264. .form-title {
  265. font-size: 14px;
  266. font-weight: bold;
  267. color: #FFFFFF;
  268. text-align: center;
  269. margin-top: 0;
  270. margin-bottom: 34px;
  271. }
  272. .tip-text {
  273. font-size: 12px;
  274. font-weight: bold;
  275. color: #7C878E;
  276. margin-bottom: 20px;
  277. span {
  278. color: #E41111;
  279. position: relative;
  280. top: 2px;
  281. margin-right: 5px;
  282. }
  283. }
  284. }
  285. .tips {
  286. font-size: 14px;
  287. color: #fff;
  288. margin-bottom: 10px;
  289. span {
  290. &:first-of-type {
  291. margin-right: 16px;
  292. }
  293. }
  294. }
  295. .svg-container {
  296. padding: 6px 5px 6px 15px;
  297. color: $dark_gray;
  298. vertical-align: middle;
  299. width: 30px;
  300. display: inline-block;
  301. }
  302. .title-container {
  303. position: relative;
  304. .title {
  305. font-size: 28px;
  306. color: $light_gray;
  307. margin: 26px auto 40px 43px;
  308. text-align: left;
  309. font-weight: bold;
  310. }
  311. }
  312. .show-pwd {
  313. position: absolute;
  314. right: 10px;
  315. top: 7px;
  316. font-size: 16px;
  317. color: $dark_gray;
  318. cursor: pointer;
  319. user-select: none;
  320. }
  321. .thirdparty-button {
  322. position: absolute;
  323. right: 0;
  324. bottom: 6px;
  325. }
  326. @media only screen and (max-width: 470px) {
  327. .thirdparty-button {
  328. display: none;
  329. }
  330. }
  331. }
  332. </style>