codeLogin.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. import { defineComponent, reactive, ref } from 'vue';
  2. import styles from '../index.module.less';
  3. import lockIcon from '../images/pwdIcon.png';
  4. import useIcon from '../images/phoneIcon.png';
  5. import {
  6. useMessage,
  7. NForm,
  8. NFormItem,
  9. NInput,
  10. NButton,
  11. NInputGroup,
  12. NModal,
  13. NSpace
  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. import { sendSms } from '../api';
  20. import SendSms from './sendSms';
  21. import { formLight } from 'naive-ui/es/form/styles';
  22. interface FormState {
  23. username: string;
  24. password: string;
  25. grant_type: string;
  26. loginType: string;
  27. client_id: string;
  28. client_secret: string;
  29. }
  30. export default defineComponent({
  31. name: 'codeLogin',
  32. props: {
  33. phone: {
  34. type: String,
  35. default: ''
  36. }
  37. },
  38. emits: ['update:phone'],
  39. setup(props, { emit }) {
  40. const router = useRouter();
  41. const route = useRoute();
  42. const formRef = ref();
  43. const message = useMessage();
  44. const loading = ref(false);
  45. const autoLogin = ref(true);
  46. const showSmsClass = ref(false);
  47. const LOGIN_NAME = PageEnum.BASE_LOGIN_NAME;
  48. const userStore = useUserStore();
  49. const formInline = reactive({
  50. username: '',
  51. password: '',
  52. isCaptcha: true
  53. });
  54. const isDisabledCode = ref(false);
  55. const starTimer = ref(60);
  56. const codeName = '发送短信';
  57. const formInlineHistory = storage.get('userInfo-teacher');
  58. if (formInlineHistory) {
  59. formInline.username = JSON.parse(formInlineHistory).username;
  60. }
  61. if (formInline.username) {
  62. if (formInline.username !== props.phone && props.phone) {
  63. formInline.username = props.phone;
  64. } else {
  65. emit('update:phone', formInline.username);
  66. }
  67. } else {
  68. if (props.phone) {
  69. formInline.username = props.phone;
  70. }
  71. }
  72. const handleSubmit = async () => {
  73. formRef.value.validate(async (errors: any) => {
  74. if (!errors) {
  75. const { username, password } = formInline;
  76. message.loading('登录中...');
  77. loading.value = true;
  78. storage.set('userInfo-teacher', JSON.stringify({ username }));
  79. const params: FormState = {
  80. username,
  81. password,
  82. loginType: 'SMS',
  83. grant_type: 'password',
  84. client_id: 'cooleshow-teacher',
  85. client_secret: 'cooleshow-teacher'
  86. };
  87. try {
  88. await userStore.login(params);
  89. // return;
  90. message.destroyAll();
  91. // if (some.code == ResultEnum.SUCCESS) {
  92. // 判断是否勾选自动登录
  93. if (autoLogin.value) {
  94. storage.set('userInfo', JSON.stringify(formInline));
  95. } else {
  96. storage.remove('userInfo');
  97. }
  98. // route.query?.redirect ||
  99. const toPath = decodeURIComponent('/' as string);
  100. message.success('登录成功,即将进入系统');
  101. if (route.name === LOGIN_NAME) {
  102. router.replace('/');
  103. } else router.replace(toPath);
  104. // } else {
  105. // // message.info(some.msg || "登录失败");
  106. // }
  107. } catch (e: any) {
  108. message.destroyAll();
  109. loading.value = false;
  110. message.error(e.msg);
  111. console.log(e);
  112. } finally {
  113. loading.value = false;
  114. }
  115. }
  116. });
  117. };
  118. const sendMessage = async () => {
  119. // if (!formInline.username) {
  120. // message.error('请输入手机号');
  121. // return;
  122. // }
  123. showSmsClass.value = true;
  124. // try {
  125. // const res = await sendSms({
  126. // clientId: 'cooleshow-teacher',
  127. // mobile: formInline.username,
  128. // type: 'LOGIN'
  129. // });
  130. // checkTimeOut();
  131. // } catch (e) {
  132. // console.log(e);
  133. // }
  134. };
  135. const checkTimeOut = () => {
  136. if (isDisabledCode.value) {
  137. return;
  138. }
  139. isDisabledCode.value = true;
  140. const tiemr = setInterval(() => {
  141. starTimer.value--;
  142. if (starTimer.value <= 0) {
  143. isDisabledCode.value = false;
  144. clearInterval(tiemr);
  145. }
  146. }, 1000);
  147. };
  148. return () => (
  149. <div class={styles['view-account-form-wrap']}>
  150. {/* <div class={styles.formTitle}>
  151. <div class={styles.dot}></div>
  152. 酷乐秀课堂乐器
  153. </div> */}
  154. <NForm
  155. ref={formRef}
  156. label-placement="left"
  157. size="large"
  158. model={formInline}>
  159. <NFormItem
  160. path="username"
  161. rule={[
  162. { required: true, message: '请输入手机号', trigger: 'blur' }
  163. ]}>
  164. <NInput
  165. maxlength={11}
  166. v-model:value={formInline.username}
  167. placeholder="请输入手机号"
  168. onInput={(val: any) => {
  169. emit('update:phone', val);
  170. }}>
  171. {{
  172. prefix: () => (
  173. <img src={useIcon} class={styles.prefixIcon} alt="" />
  174. )
  175. }}
  176. </NInput>
  177. </NFormItem>
  178. <NFormItem
  179. path="password"
  180. rule={[
  181. { required: true, message: '请输入验证码', trigger: 'blur' }
  182. ]}>
  183. <NInputGroup>
  184. <NInput
  185. v-model:value={formInline.password}
  186. type="text"
  187. showPasswordOn="click"
  188. placeholder="请输入验证码"
  189. inputProps={{ autocomplete: 'off' }}
  190. class={styles.sendInput}
  191. maxlength={6}
  192. onKeydown={(e: KeyboardEvent) => {
  193. if (e.code === 'Enter' || e.code === 'NumpadEnter') {
  194. handleSubmit();
  195. }
  196. }}>
  197. {{
  198. prefix: () => (
  199. <img src={lockIcon} class={styles.prefixIcon} alt="" />
  200. ),
  201. suffix: () => (
  202. <NButton
  203. class={styles.sendMsg}
  204. disabled={isDisabledCode.value}
  205. onClick={() => sendMessage()}>
  206. {isDisabledCode.value ? starTimer.value + 'S' : codeName}
  207. </NButton>
  208. )
  209. }}
  210. </NInput>
  211. </NInputGroup>
  212. </NFormItem>
  213. <NFormItem class={styles['default-color']}>
  214. <div class={[styles['flex'], styles['justify-between']]}>
  215. <div class={styles['flex-initial']}>
  216. {/* <NCheckbox v-model:checked={autoLogin.value}>
  217. 记住密码
  218. </NCheckbox> */}
  219. </div>
  220. </div>
  221. </NFormItem>
  222. <NFormItem>
  223. <NButton
  224. class={styles.submitBtm}
  225. type="primary"
  226. onClick={handleSubmit}
  227. size="large"
  228. disabled={loading.value}
  229. block>
  230. 立即登录
  231. </NButton>
  232. </NFormItem>
  233. </NForm>
  234. <NModal v-model:show={showSmsClass.value}>
  235. <SendSms
  236. phone={formInline.username}
  237. onClose={() => (showSmsClass.value = false)}
  238. onSendCode={() => {
  239. checkTimeOut();
  240. }}
  241. />
  242. </NModal>
  243. </div>
  244. );
  245. }
  246. });