Login.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <div class="container">
  3. <div class="login-form">
  4. <div class="project-title">
  5. <h1>Nginx UI</h1>
  6. </div>
  7. <a-form
  8. id="components-form-demo-normal-login"
  9. :form="form"
  10. @submit="handleSubmit"
  11. >
  12. <a-form-item>
  13. <a-input
  14. v-decorator="[
  15. 'name',
  16. { rules: [{ required: true, message: '请输入用户名' }] },
  17. ]"
  18. placeholder="Username"
  19. >
  20. <a-icon slot="prefix" type="user" style="color: rgba(0,0,0,.25)"/>
  21. </a-input>
  22. </a-form-item>
  23. <a-form-item>
  24. <a-input
  25. v-decorator="[
  26. 'password',
  27. { rules: [{ required: true, message: '请输入密码' }] },
  28. ]"
  29. type="password"
  30. placeholder="Password"
  31. >
  32. <a-icon slot="prefix" type="lock" style="color: rgba(0,0,0,.25)"/>
  33. </a-input>
  34. </a-form-item>
  35. <a-form-item>
  36. <a-button type="primary" :block="true" html-type="submit" :loading="loading">
  37. 登录
  38. </a-button>
  39. </a-form-item>
  40. </a-form>
  41. <div class="footer">
  42. Copyright © 2020 - {{ thisYear }} Nginx UI
  43. </div>
  44. </div>
  45. </div>
  46. </template>
  47. <script>
  48. export default {
  49. name: 'Login',
  50. data() {
  51. return {
  52. form: {},
  53. thisYear: new Date().getFullYear(),
  54. loading: false
  55. }
  56. },
  57. created() {
  58. this.form = this.$form.createForm(this)
  59. },
  60. mounted() {
  61. this.$api.install.get_lock().then(r => {
  62. if (!r.lock) {
  63. this.$router.push('/install')
  64. }
  65. })
  66. if (this.$store.state.user.token) {
  67. this.$router.push('/')
  68. }
  69. },
  70. methods: {
  71. login(values) {
  72. return this.$api.auth.login(values.name, values.password).then(async () => {
  73. await this.$message.success('登录成功', 1)
  74. const next = this.$route.query.next ? this.$route.query.next : '/'
  75. await this.$router.push(next)
  76. }).catch(r => {
  77. console.log(r)
  78. this.$message.error(r.message ?? '服务器错误')
  79. })
  80. },
  81. handleSubmit: async function (e) {
  82. e.preventDefault()
  83. this.loading = true
  84. await this.form.validateFields(async (err, values) => {
  85. if (!err) {
  86. await this.login(values)
  87. }
  88. this.loading = false
  89. })
  90. },
  91. },
  92. }
  93. </script>
  94. <style lang="less">
  95. .container {
  96. display: flex;
  97. align-items: center;
  98. justify-content: center;
  99. height: 100%;
  100. .login-form {
  101. max-width: 400px;
  102. width: 80%;
  103. .project-title {
  104. margin: 50px;
  105. h1 {
  106. font-size: 50px;
  107. font-weight: 100;
  108. text-align: center;
  109. }
  110. }
  111. .login-form-button {
  112. }
  113. .footer {
  114. padding: 30px;
  115. text-align: center;
  116. }
  117. }
  118. }
  119. </style>