PasswordLogin.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <template>
  2. <div class="content">
  3. <h1>用户登录</h1>
  4. <div class="input">
  5. <el-input v-model="form.username" size="large" placeholder="输入用户名或邮箱" :prefix-icon="User" />
  6. <el-input v-model="form.password" size="large" placeholder="输入密码" type="password" :prefix-icon="Lock" />
  7. <div class="captcha">
  8. <el-input v-model="form.text" placeholder="输入验证码" size="large" class="captcha-input"
  9. :prefix-icon="Finished" />
  10. <img alt="验证码" :src="ImageCaptcha" @click="getCaptcha">
  11. </div>
  12. </div>
  13. <button @click="Login()">登录</button>
  14. <div class="littleButton">
  15. <span @click="update('register')">注册账号</span> |
  16. <span @click="update('forget')">忘记密码</span>
  17. </div>
  18. </div>
  19. </template>
  20. <script setup>
  21. import { ServerAPI } from '../../../app/lib/ServerAPI';
  22. import { User, Lock, Finished } from '@element-plus/icons-vue';
  23. import { useRoute, useRouter } from 'vue-router';
  24. import { App } from '../../../app/app';
  25. let route = useRoute();
  26. let router = useRouter();
  27. let ImageCaptcha = ref('');
  28. let form = reactive({
  29. username: '',
  30. password: '',
  31. text: '',
  32. id: ''
  33. });
  34. const emit = defineEmits(['update']);
  35. let update = (mode) => {
  36. emit('update', mode);
  37. }
  38. let getCaptcha = () => {
  39. ServerAPI.ImageCaptcha((r) => {
  40. if (!r || r.code != 0)
  41. ElMessage.error(`获取图片验证码失败!${r.msg}`);
  42. ImageCaptcha.value = r.data.img;
  43. form.id = r.data.id;
  44. })
  45. }
  46. onMounted(() => {
  47. getCaptcha()
  48. })
  49. let Login = () => {
  50. if (form.username === '' || form.password === '' || form.text === '')
  51. return ElMessage.error('请确保每项不能为空');
  52. const loading = ElLoading.service({
  53. lock: true,
  54. text: '正在登录中,请稍候'
  55. })
  56. let password = btoa(form.password);
  57. try {
  58. ServerAPI.Login(form.username, password, form.text, form.id, (res) => {
  59. if (!res || res.code !== 0) {
  60. loading.close();
  61. ElMessage.error(`登录失败!${res.msg || ''}`);
  62. getCaptcha();
  63. form.text = '';
  64. return;
  65. } else {
  66. loading.close();
  67. ElMessage.success('登录成功');
  68. App.refershUser(res.data);
  69. if (route.query.backPage !== undefined) {
  70. ElMessage.success("即将跳转...");
  71. setTimeout(() => {
  72. router.replace({
  73. name: route.query.backPage
  74. });
  75. }, 1000);
  76. return;
  77. }
  78. if (window.history.state && window.history.state.key) {
  79. router.back();
  80. } else {
  81. router.replace({
  82. name: "Main",
  83. })
  84. }
  85. }
  86. })
  87. } catch (error) {
  88. ElMessage.error("登录失败!请稍后再试" + error);
  89. router.replace({
  90. name: "Login",
  91. })
  92. }
  93. }
  94. </script>
  95. <style scoped>
  96. .content {
  97. display: flex;
  98. align-items: center;
  99. justify-content: center;
  100. flex-direction: column;
  101. background-color: rgba(255, 255, 255, 0.7);
  102. position: absolute;
  103. top: 50%;
  104. left: 50%;
  105. transform: translate(-50%, -50%);
  106. height: 400px;
  107. width: 650px;
  108. box-sizing: border-box;
  109. border-radius: 10px;
  110. }
  111. .content .input {
  112. display: flex;
  113. flex-direction: column;
  114. gap: 15px;
  115. }
  116. .content h1 {
  117. color: #337ecc;
  118. font-size: 2em;
  119. margin: 0;
  120. margin-bottom: 20px;
  121. }
  122. .el-input {
  123. border-radius: 15px;
  124. height: 50px;
  125. }
  126. .content .captcha {
  127. display: flex;
  128. height: 50px;
  129. }
  130. .content button {
  131. width: 180px;
  132. height: 40px;
  133. margin-top: 20px;
  134. font-size: 1em;
  135. border: none;
  136. border-radius: 10px;
  137. background-color: #337ecc;
  138. color: #fff;
  139. }
  140. .content .littleButton {
  141. color: #444;
  142. margin-top: 20px;
  143. font-size: 0.9em;
  144. }
  145. @media only screen and (max-width: 768px) {
  146. .content {
  147. width: 80%;
  148. height: 45%;
  149. padding: 25px;
  150. }
  151. .el-input {
  152. height: 40px;
  153. }
  154. .content .captcha {
  155. height: 40px;
  156. }
  157. .content h1 {
  158. font-size: 1.5em;
  159. }
  160. .content button {
  161. height: 35px;
  162. width: 150px;
  163. font-size: 0.9em;
  164. }
  165. .content .littleButton {
  166. font-size: 0.8em;
  167. }
  168. }
  169. </style>