index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div class="page-login">
  3. <div class="page-login--wrapper">
  4. <div class="page-login--content">
  5. <div class="page-login--logo-title">
  6. <h5>管理系统登录</h5>
  7. </div>
  8. <el-form :model="loginForm" :rules="formRules" ref="loginForm" label-position="top">
  9. <el-form-item prop="user_name" :rules="formRules.required">
  10. <el-input placeholder="用户名" v-model="loginForm.user_name" size="default">
  11. <i slot="prepend" class="el-icon-user-solid"></i>
  12. </el-input>
  13. </el-form-item>
  14. <el-form-item prop="user_password" :rules="formRules.password">
  15. <el-input show-password placeholder="密码" v-model="loginForm.user_password" size="default"
  16. @keydown.enter.native="handleSubmit">
  17. <i slot="prepend" class="el-icon-lock"></i>
  18. </el-input>
  19. </el-form-item>
  20. <el-form-item>
  21. <el-button
  22. @click="handleSubmit"
  23. type="primary"
  24. class="button-login"
  25. size="default">登录
  26. </el-button>
  27. </el-form-item>
  28. </el-form>
  29. </div>
  30. </div>
  31. </div>
  32. </template>
  33. <script>
  34. import { setToken, setBarId, setBarName } from '@/utils/auth'
  35. export default {
  36. name: 'Login',
  37. data () {
  38. return {
  39. loginForm: {
  40. user_project_id: '1',
  41. user_name: '',
  42. user_password: ''
  43. },
  44. loading: false,
  45. passwordType: 'password',
  46. redirect: undefined
  47. }
  48. },
  49. watch: {
  50. $route: {
  51. handler: function (route) {
  52. this.redirect = route.query && route.query.redirect
  53. },
  54. immediate: true
  55. }
  56. },
  57. methods: {
  58. handleSubmit () {
  59. this.$refs.loginForm.validate(async valid => {
  60. if (valid) {
  61. this.loading = true
  62. const data = await this.$fetch('/api/auth/user/login', this.loginForm)
  63. if (data.code === 200) {
  64. this.$store.commit('user/SET_TOKEN', data.data.token)
  65. setToken(data.data.token)
  66. setBarId(data.data.bar_id)
  67. setBarName(data.data.bar_name)
  68. this.$router.push({ path: this.redirect || '/' })
  69. }
  70. this.loading = false
  71. } else {
  72. console.log('error submit!!')
  73. return false
  74. }
  75. })
  76. }
  77. }
  78. }
  79. </script>
  80. <style lang="scss" scoped>
  81. .page-login {
  82. user-select: none;
  83. cursor: pointer;
  84. $backgroundColor: #f0f2f5;
  85. background-color: $backgroundColor;
  86. height: 100%;
  87. position: relative;
  88. .page-login--wrapper {
  89. display: flex;
  90. justify-content: center;
  91. align-items: center;
  92. flex-direction: row;
  93. position: absolute;
  94. top: 0;
  95. right: 0;
  96. left: 0;
  97. bottom: 0;
  98. .page-login--content {
  99. width: 400px;
  100. padding: 20px;
  101. border-radius: 8px;
  102. background: #fff;
  103. .button-login {
  104. display: block;
  105. width: 100%;
  106. }
  107. .page-login--logo-title {
  108. display: flex;
  109. justify-content: center;
  110. align-items: center;
  111. flex-direction: row;
  112. font-size: 36px;
  113. h5 {
  114. margin: 20px 0;
  115. }
  116. img {
  117. width: 40px;
  118. margin-right: 10px;
  119. }
  120. }
  121. }
  122. }
  123. }
  124. </style>