index.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <Teleport to="body">
  3. <div
  4. class="teleport-login-modal-warp"
  5. @click.self.stop="handleClose"
  6. >
  7. <div class="content">
  8. <i
  9. class="close"
  10. @click="handleClose"
  11. ></i>
  12. <n-card>
  13. <n-tabs
  14. :value="currentTab"
  15. :default-value="currentTab"
  16. size="large"
  17. :on-update:value="tabChange"
  18. >
  19. <n-tab-pane
  20. name="pwdlogin"
  21. tab="账密登录"
  22. >
  23. <n-form
  24. ref="loginFormRef"
  25. label-placement="left"
  26. size="large"
  27. :model="loginForm"
  28. :rules="loginRules"
  29. >
  30. <n-form-item path="id">
  31. <n-input
  32. v-model:value="loginForm.id"
  33. type="text"
  34. placeholder="请输入账号"
  35. >
  36. <template #prefix>
  37. <n-icon
  38. size="20"
  39. class="lang"
  40. >
  41. <PersonOutline></PersonOutline>
  42. </n-icon>
  43. </template>
  44. </n-input>
  45. </n-form-item>
  46. <n-form-item path="password">
  47. <n-input
  48. v-model:value="loginForm.password"
  49. type="password"
  50. show-password-on="mousedown"
  51. placeholder="请输入密码"
  52. @focus="onFocus"
  53. @blur="onBlur"
  54. @keyup.enter="handleLoginSubmit"
  55. >
  56. <template #prefix>
  57. <n-icon
  58. size="20"
  59. class="lang"
  60. >
  61. <LockClosedOutline></LockClosedOutline>
  62. </n-icon>
  63. </template>
  64. </n-input>
  65. </n-form-item>
  66. </n-form>
  67. <n-button
  68. type="primary"
  69. block
  70. secondary
  71. strong
  72. @click="handleLoginSubmit"
  73. >
  74. 登录
  75. </n-button>
  76. </n-tab-pane>
  77. </n-tabs>
  78. </n-card>
  79. <div class="other-login">
  80. <span>第三方登录:</span>
  81. <div
  82. class="logo-wrap"
  83. @click="handleQQLogin()"
  84. >
  85. <img
  86. class="logo"
  87. src="@/assets/img/qq_logo.webp"
  88. />
  89. </div>
  90. <div
  91. class="logo-wrap"
  92. @click="handleTip"
  93. >
  94. <img
  95. class="logo"
  96. src="@/assets/img/github_logo.webp"
  97. />
  98. </div>
  99. </div>
  100. </div>
  101. </div>
  102. </Teleport>
  103. </template>
  104. <script lang="ts" setup>
  105. import { LockClosedOutline, PersonOutline } from '@vicons/ionicons5';
  106. import { onMounted, onUnmounted, ref } from 'vue';
  107. import { handleTip } from '@/hooks/use-common';
  108. import { useQQLogin } from '@/hooks/use-login';
  109. import { useAppStore } from '@/store/app';
  110. import { useUserStore } from '@/store/user';
  111. const loginRules = {
  112. id: { required: true, message: '请输入账号', trigger: 'blur' },
  113. password: { required: true, message: '请输入密码', trigger: 'blur' },
  114. };
  115. const userStore = useUserStore();
  116. const appStore = useAppStore();
  117. const loginForm = ref({
  118. id: '',
  119. password: '',
  120. });
  121. const loginFormRef = ref(null);
  122. const currentTab = ref('pwdlogin'); // pwdlogin
  123. const loopTimer = ref();
  124. const emits = defineEmits(['close']);
  125. onMounted(() => {});
  126. onUnmounted(() => {
  127. clearInterval(loopTimer.value);
  128. });
  129. function handleQQLogin() {
  130. useQQLogin({ exp: 24 });
  131. }
  132. function handleClose() {
  133. appStore.showLoginModal = false;
  134. emits('close');
  135. }
  136. const handleLogin = async () => {
  137. let token = null;
  138. token = await userStore.pwdLogin({
  139. id: +loginForm.value.id,
  140. password: loginForm.value.password,
  141. });
  142. if (token) {
  143. window.$message.success('登录成功!');
  144. userStore.getUserInfo();
  145. appStore.showLoginModal = false;
  146. }
  147. };
  148. const handleLoginSubmit = (e) => {
  149. e.preventDefault();
  150. // @ts-ignore
  151. loginFormRef.value.validate((errors) => {
  152. if (!errors) {
  153. handleLogin();
  154. }
  155. });
  156. };
  157. const tabChange = (v) => {
  158. currentTab.value = v;
  159. clearInterval(loopTimer.value);
  160. };
  161. const focus = ref(false);
  162. const onFocus = () => {
  163. focus.value = true;
  164. };
  165. const onBlur = () => {
  166. focus.value = false;
  167. };
  168. </script>
  169. <style lang="scss" scoped>
  170. .teleport-login-modal-warp {
  171. z-index: 100 !important;
  172. @extend %maskBg;
  173. .content {
  174. position: absolute;
  175. top: 50%;
  176. left: 50%;
  177. min-width: 350px;
  178. border-radius: 5px;
  179. background-color: #fff;
  180. transform: translate(-50%, -50%);
  181. .qrcode {
  182. width: 166px;
  183. height: 166px;
  184. margin: 0 auto;
  185. background-color: gray;
  186. background-size: contain;
  187. background-repeat: no-repeat;
  188. background-position: center center;
  189. }
  190. .close {
  191. position: absolute;
  192. top: 20px;
  193. right: 20px;
  194. z-index: 1;
  195. width: 18px;
  196. height: 18px;
  197. cursor: pointer;
  198. @include cross(#ccc, 3px);
  199. }
  200. .top {
  201. position: absolute;
  202. top: 0;
  203. left: 50%;
  204. z-index: 100;
  205. width: 120px;
  206. transform: translate(-50%, -90%);
  207. &.close {
  208. z-index: 0;
  209. transform: translate(-50%, -99%);
  210. }
  211. }
  212. .title {
  213. margin: 10px 0;
  214. text-align: center;
  215. }
  216. .other-login {
  217. display: flex;
  218. align-items: center;
  219. justify-content: space-around;
  220. margin: 5px 0;
  221. .logo-wrap {
  222. display: flex;
  223. align-items: center;
  224. justify-content: center;
  225. width: 36px;
  226. height: 36px;
  227. border-radius: 50%;
  228. background-color: #f4f8fb;
  229. cursor: pointer;
  230. .logo {
  231. width: 26px;
  232. height: 26px;
  233. }
  234. }
  235. }
  236. }
  237. }
  238. </style>