login.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <div id="userLayout" class="user-layout-wrapper">
  3. <div class="container">
  4. <div class="top">
  5. <div class="desc">
  6. <img class="logo_login" src="@/assets/logo_login.png" alt="" />
  7. </div>
  8. <div class="header">
  9. <a href="/">
  10. <!-- <img src="~@/assets/logo.png" class="logo" alt="logo" /> -->
  11. <span class="title">Gin-Vue-Admin</span>
  12. </a>
  13. </div>
  14. </div>
  15. <div class="main">
  16. <el-form
  17. :model="loginForm"
  18. :rules="rules"
  19. ref="loginForm"
  20. @keyup.enter.native="submitForm"
  21. >
  22. <el-form-item prop="username">
  23. <el-input placeholder="请输入用户名" v-model="loginForm.username">
  24. <i class="el-input__icon el-icon-user" slot="suffix"></i
  25. ></el-input>
  26. </el-form-item>
  27. <el-form-item prop="password">
  28. <el-input
  29. :type="lock === 'lock' ? 'password' : 'text'"
  30. placeholder="请输入密码"
  31. v-model="loginForm.password"
  32. >
  33. <i
  34. :class="'el-input__icon el-icon-' + lock"
  35. @click="changeLock"
  36. slot="suffix"
  37. ></i>
  38. </el-input>
  39. </el-form-item>
  40. <el-form-item style="position: relative">
  41. <el-input
  42. v-model="loginForm.captcha"
  43. name="logVerify"
  44. placeholder="请输入验证码"
  45. style="width: 60%"
  46. />
  47. <div class="vPic">
  48. <img
  49. v-if="picPath"
  50. :src="picPath"
  51. width="100%"
  52. height="100%"
  53. alt="请输入验证码"
  54. @click="loginVefify()"
  55. />
  56. </div>
  57. </el-form-item>
  58. <el-form-item>
  59. <el-button type="primary" @click="submitForm" style="width: 100%"
  60. >登 录</el-button
  61. >
  62. </el-form-item>
  63. </el-form>
  64. </div>
  65. <div class="footer">
  66. <div class="links">
  67. <a href="http://doc.henrongyi.top/"
  68. ><img src="@/assets/docs.png" class="link-icon"
  69. /></a>
  70. <a href="https://www.yuque.com/flipped-aurora/"
  71. ><img src="@/assets/yuque.png" class="link-icon"
  72. /></a>
  73. <a href="https://github.com/flipped-aurora/gin-vue-admin"
  74. ><img src="@/assets/github.png" class="link-icon"
  75. /></a>
  76. <a href="https://space.bilibili.com/322210472"
  77. ><img src="@/assets/video.png" class="link-icon"
  78. /></a>
  79. </div>
  80. <div class="copyright">Copyright &copy; {{ curYear }} 💖flipped-aurora</div>
  81. </div>
  82. </div>
  83. </div>
  84. </template>
  85. <script>
  86. import { mapActions } from "vuex";
  87. import { captcha } from "@/api/user";
  88. export default {
  89. name: "Login",
  90. data() {
  91. const checkUsername = (rule, value, callback) => {
  92. if (value.length < 5 || value.length > 12) {
  93. return callback(new Error("请输入正确的用户名"));
  94. } else {
  95. callback();
  96. }
  97. };
  98. const checkPassword = (rule, value, callback) => {
  99. if (value.length < 6 || value.length > 12) {
  100. return callback(new Error("请输入正确的密码"));
  101. } else {
  102. callback();
  103. }
  104. };
  105. return {
  106. curYear: 0,
  107. lock: "lock",
  108. loginForm: {
  109. username: "admin",
  110. password: "123456",
  111. captcha: "",
  112. captchaId: "",
  113. },
  114. rules: {
  115. username: [{ validator: checkUsername, trigger: "blur" }],
  116. password: [{ validator: checkPassword, trigger: "blur" }],
  117. },
  118. logVerify: "",
  119. picPath: "",
  120. };
  121. },
  122. created() {
  123. this.loginVefify();
  124. this.curYear = new Date().getFullYear();
  125. },
  126. methods: {
  127. ...mapActions("user", ["LoginIn"]),
  128. async login() {
  129. return await this.LoginIn(this.loginForm);
  130. },
  131. async submitForm() {
  132. this.$refs.loginForm.validate(async (v) => {
  133. if (v) {
  134. const flag = await this.login();
  135. if (!flag) {
  136. this.loginVefify();
  137. }
  138. } else {
  139. this.$message({
  140. type: "error",
  141. message: "请正确填写登录信息",
  142. showClose: true,
  143. });
  144. this.loginVefify();
  145. return false;
  146. }
  147. });
  148. },
  149. changeLock() {
  150. this.lock === "lock" ? (this.lock = "unlock") : (this.lock = "lock");
  151. },
  152. loginVefify() {
  153. captcha({}).then((ele) => {
  154. this.picPath = ele.data.picPath;
  155. this.loginForm.captchaId = ele.data.captchaId;
  156. });
  157. },
  158. },
  159. };
  160. </script>
  161. <style scoped lang="scss">
  162. @import "@/style/login.scss";
  163. </style>