pwdLogin.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import { defineComponent, reactive, ref } from 'vue';
  2. import styles from '../index.module.less';
  3. import lockIcon from '../images/lock-icon.png';
  4. import useIcon from '../images/user-icon.png';
  5. import openEye from '../images/openEye.png';
  6. import closeEye from '../images/closeEye.png';
  7. import {
  8. useMessage,
  9. NForm,
  10. NFormItem,
  11. NInput,
  12. NButton,
  13. NCheckbox
  14. } from 'naive-ui';
  15. import { useRoute, useRouter } from 'vue-router';
  16. import { PageEnum } from '/src/enums/pageEnum';
  17. import { storage } from '@/utils/storage';
  18. import { useUserStore } from '/src/store/modules/users';
  19. interface FormState {
  20. username: string;
  21. password: string;
  22. grant_type?: string;
  23. loginType?: string;
  24. client_id?: string;
  25. client_secret?: string;
  26. clientId?: string;
  27. clientSecret?: string;
  28. }
  29. export default defineComponent({
  30. name: 'codeLogin',
  31. emits: ['changType'],
  32. setup(props, { emit }) {
  33. const router = useRouter();
  34. const route = useRoute();
  35. const formRef = ref();
  36. const message = useMessage();
  37. const loading = ref(false);
  38. const autoLogin = ref(true);
  39. const LOGIN_NAME = PageEnum.BASE_LOGIN_NAME;
  40. const showPwd = ref(false);
  41. const userStore = useUserStore();
  42. let formInline = reactive({
  43. username: '',
  44. password: '',
  45. isCaptcha: true
  46. });
  47. const formInlineHistory = storage.get('userInfo-teacher');
  48. if (formInlineHistory) {
  49. formInline = reactive({ ...JSON.parse(formInlineHistory) });
  50. }
  51. const handleSubmit = async () => {
  52. formRef.value.validate(async (errors: any) => {
  53. if (!errors) {
  54. const { username, password } = formInline;
  55. loading.value = true;
  56. const params: FormState = {
  57. username,
  58. password,
  59. loginType:'PASSWORD',
  60. grant_type: 'password',
  61. client_id: 'cooleshow-teacher',
  62. client_secret: 'cooleshow-teacher'
  63. };
  64. try {
  65. await userStore.login(params);
  66. message.destroyAll();
  67. // 判断是否勾选自动登录
  68. if (autoLogin.value) {
  69. storage.set('userInfo-teacher', JSON.stringify(formInline));
  70. } else {
  71. storage.remove('userInfo-teacher');
  72. }
  73. // route.query?.redirect ||
  74. const toPath = decodeURIComponent('/' as string);
  75. console.log(toPath,'toPath')
  76. message.success('登录成功,即将进入系统');
  77. router.replace(toPath)
  78. // if (route.name === LOGIN_NAME) {
  79. // router.replace('/');
  80. // } else router.replace(toPath);
  81. } catch (e: any) {
  82. console.log(e, 'e');
  83. } finally {
  84. loading.value = false;
  85. }
  86. }
  87. });
  88. };
  89. return () => (
  90. <div class={styles['view-account-form-wrap']}>
  91. {/* <div class={styles.formTitle}>
  92. <div class={styles.dot}></div>
  93. 酷乐秀课堂乐器
  94. </div> */}
  95. <NForm
  96. ref={formRef}
  97. label-placement="left"
  98. size="large"
  99. model={formInline}>
  100. <NFormItem
  101. path="username"
  102. rule={[
  103. { required: true, message: '请输入用户名', trigger: 'blur' }
  104. ]}>
  105. <NInput
  106. maxlength={11}
  107. v-model:value={formInline.username}
  108. placeholder="请输入用户名">
  109. {{
  110. prefix: () => (
  111. <img src={useIcon} class={styles.prefixIcon} alt="" />
  112. )
  113. }}
  114. </NInput>
  115. </NFormItem>
  116. <NFormItem
  117. path="password"
  118. rule={[{ required: true, message: '请输入密码', trigger: 'blur' }]}>
  119. <NInput
  120. v-model:value={formInline.password}
  121. type="text"
  122. showPasswordOn="click"
  123. placeholder="请输入密码"
  124. inputProps={{ autocomplete: 'off' }}
  125. class={[showPwd.value ? '' : styles['no-pwd']]}
  126. onKeydown={(e: KeyboardEvent) => {
  127. console.log(e.code)
  128. if (e.code === 'Enter' || e.code === 'NumpadEnter') {
  129. handleSubmit();
  130. }
  131. }}>
  132. {{
  133. prefix: () => (
  134. <img src={lockIcon} class={styles.prefixIcon} alt="" />
  135. ),
  136. suffix: () => (
  137. <img
  138. src={showPwd.value ? openEye : closeEye}
  139. class={styles.pwdIcon}
  140. alt=""
  141. onClick={() => {
  142. showPwd.value = !showPwd.value;
  143. }}
  144. />
  145. )
  146. }}
  147. </NInput>
  148. </NFormItem>
  149. <NFormItem class={styles['default-color']}>
  150. <div class={[styles['flex'], styles['justify-between']]}>
  151. <div class={styles['flex-initial']}>
  152. <NCheckbox v-model:checked={autoLogin.value}>
  153. 记住密码
  154. </NCheckbox>
  155. </div>
  156. </div>
  157. </NFormItem>
  158. <NFormItem>
  159. <NButton
  160. class={styles.submitBtm}
  161. type="primary"
  162. onClick={handleSubmit}
  163. size="large"
  164. disabled={loading.value}
  165. loading={loading.value}
  166. block>
  167. 立即登录
  168. </NButton>
  169. </NFormItem>
  170. <NFormItem>
  171. <NButton
  172. text
  173. class={styles.forgetBtm}
  174. onClick={() => {
  175. emit('changType');
  176. }}
  177. size="large"
  178. block>
  179. 忘记密码
  180. </NButton>
  181. </NFormItem>
  182. </NForm>
  183. </div>
  184. );
  185. }
  186. });