LoginForm.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <div class="panel">
  3. <div class="panel-inner">
  4. <div class="panel-title">账号登录</div>
  5. <div class="field">
  6. <img class="i" :src="require('@/assets/i_user.png')" />
  7. <span class="field-label">账号</span>
  8. <input class="inp" v-model.trim="username" placeholder="请输入账号" />
  9. </div>
  10. <div class="field">
  11. <img class="i" :src="require('@/assets/i_lock.png')" />
  12. <span class="field-label">密码</span>
  13. <input class="inp" type="password" v-model.trim="password" placeholder="请输入密码" />
  14. </div>
  15. <div class="row" v-if="showCaptcha">
  16. <div class="field cap-field">
  17. <img class="i" :src="require('@/assets/i_captcha.png')" />
  18. <span class="field-label">验证码</span>
  19. <input class="inp" v-model.trim="captchaInput" placeholder="请输入验证码" />
  20. </div>
  21. <CaptchaCanvas class="captcha-img" v-model="captchaCode" />
  22. </div>
  23. <div class="hint" v-if="hint">{{ hint }}</div>
  24. <button class="btn" @click="onLogin">立即登录</button>
  25. </div>
  26. </div>
  27. </template>
  28. <script>
  29. import CaptchaCanvas from "@/components/CaptchaCanvas.vue";
  30. import { apiLogin } from "@/api";
  31. export default {
  32. name: "LoginForm",
  33. components: { CaptchaCanvas },
  34. data() {
  35. return {
  36. username: "admin",
  37. password: "123456",
  38. captchaCode: "",
  39. captchaInput: "",
  40. hint: "",
  41. loginFailedCount: Number(sessionStorage.getItem("loginFailedCount")) || 0,
  42. };
  43. },
  44. computed: {
  45. showCaptcha() {
  46. return this.loginFailedCount >= 3;
  47. },
  48. },
  49. methods: {
  50. async onLogin() { console.log('do login')
  51. this.hint = "";
  52. if (this.showCaptcha) {
  53. if (!this.captchaInput) {
  54. this.hint = "请输入验证码";
  55. return;
  56. }
  57. if (this.captchaInput.toLowerCase() !== this.captchaCode.toLowerCase()) {
  58. this.hint = "验证码错误";
  59. this.captchaInput = "";
  60. return;
  61. }
  62. }
  63. let data;
  64. try {
  65. data = await apiLogin({
  66. username: this.username,
  67. password: this.password,
  68. captcha: this.captchaInput,
  69. });
  70. this.loginFailedCount = 0;
  71. sessionStorage.removeItem("loginFailedCount");
  72. this.$router.push("/transition");
  73. } catch (e) {
  74. this.hint = e.message || "登录失败";
  75. this.loginFailedCount++;
  76. sessionStorage.setItem("loginFailedCount", this.loginFailedCount);
  77. this.captchaInput = "";
  78. return;
  79. }
  80. localStorage.setItem("token", data.token);
  81. this.$emit("login-success");
  82. },
  83. },
  84. };
  85. </script>
  86. <style scoped>
  87. /* ================== 表单主面板 ================== */
  88. .panel {
  89. /* 严格按照设计稿尺寸 */
  90. width: 637px;
  91. height: 514px;
  92. /* 调整内边距,给背景外框留出空间并让内容居中 */
  93. padding: 60px 75px;
  94. box-sizing: border-box;
  95. background: url("~@/assets/panel_frame.png") center/100% 100% no-repeat;
  96. display: flex;
  97. flex-direction: column;
  98. justify-content: center;
  99. transform: scale(var(--s, 1));
  100. /* 将缩放基准点设为右侧垂直居中。这样缩小的时候,表单会贴着右侧边距变小,不会往屏幕中间飘 */
  101. transform-origin: right center;
  102. }
  103. .panel-inner {
  104. width: 100%;
  105. display: flex;
  106. flex-direction: column;
  107. }
  108. /* ================== 标题 ================== */
  109. .panel-title {
  110. font-size: 28px;
  111. /* 根据大面板放大标题 */
  112. color: #ffffff;
  113. margin-bottom: 35px;
  114. font-weight: 600;
  115. text-align: left;
  116. letter-spacing: 2px;
  117. }
  118. /* ================== 输入框容器 ================== */
  119. .field {
  120. width: 100%;
  121. height: 58px;
  122. /* 加高输入框以匹配大面板 */
  123. display: flex;
  124. align-items: center;
  125. padding: 0 20px;
  126. background: rgba(26, 117, 255, 0.12);
  127. border-radius: 6px;
  128. border: 1px solid rgba(71, 120, 255, 0.35);
  129. margin-bottom: 26px;
  130. /* 加大行距 */
  131. position: relative;
  132. transition: all 0.25s ease;
  133. box-sizing: border-box;
  134. }
  135. .field:focus-within {
  136. border-color: rgba(90, 220, 255, 0.95);
  137. background: rgba(26, 117, 255, 0.22);
  138. box-shadow:
  139. 0 0 14px rgba(43, 220, 255, 0.25),
  140. inset 0 0 10px rgba(120, 220, 255, 0.15);
  141. }
  142. /* ================== 输入框内部元素 ================== */
  143. .field .i {
  144. width: 22px;
  145. height: 22px;
  146. object-fit: contain;
  147. opacity: 0.9;
  148. }
  149. .field-label {
  150. color: rgba(220, 245, 255, 0.9);
  151. font-size: 16px;
  152. margin-left: 12px;
  153. margin-right: 20px;
  154. white-space: nowrap;
  155. }
  156. .inp {
  157. flex: 1;
  158. height: 100%;
  159. border: none;
  160. outline: none;
  161. background: transparent;
  162. color: #ffffff;
  163. font-size: 16px;
  164. }
  165. .inp::placeholder {
  166. color: rgba(190, 225, 255, 0.35);
  167. font-size: 15px;
  168. }
  169. /* ================== 验证码同行布局 ================== */
  170. .row {
  171. width: 100%;
  172. display: flex;
  173. gap: 16px;
  174. margin-bottom: 26px;
  175. }
  176. .cap-field {
  177. flex: 1;
  178. margin-bottom: 0;
  179. }
  180. .captcha-img {
  181. width: 140px;
  182. /* 放大的验证码图片宽度 */
  183. height: 58px;
  184. /* 与输入框等高 */
  185. border-radius: 6px;
  186. overflow: hidden;
  187. border: 1px solid rgba(71, 120, 255, 0.35);
  188. }
  189. /* ================== 登录按钮 ================== */
  190. .btn {
  191. width: 100%;
  192. height: 58px;
  193. margin-top: 10px;
  194. color: #ffffff;
  195. font-size: 26px;
  196. letter-spacing: 4px;
  197. cursor: pointer;
  198. font-family: var(--title-font-family);
  199. background: linear-gradient(180deg, rgba(119, 161, 255, 0) 0%, #77A1FF 100%);
  200. border-radius: 8px;
  201. border: 1px solid rgba(161, 190, 255, 0.7);
  202. box-shadow: 0 4px 15px rgba(26, 117, 255, 0.3);
  203. transition: all 0.2s ease;
  204. }
  205. .btn:hover {
  206. filter: brightness(1.15);
  207. box-shadow: 0 4px 20px rgba(26, 117, 255, 0.5);
  208. }
  209. /* ================== 提示文字 ================== */
  210. .hint {
  211. margin-top: -10px;
  212. margin-bottom: 15px;
  213. color: #ff4d4f;
  214. font-size: 14px;
  215. }
  216. </style>