index.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { defineComponent, onMounted, ref } from 'vue';
  2. import styles from './index.module.less';
  3. import { Radio, RadioGroup, Image, Button } from 'vant';
  4. import checkBoxActive from './images/icon-n-1.png';
  5. import checkBoxDefault from './images/icon-n-2.png';
  6. import request from '@/helpers/request';
  7. import { storage } from '@/helpers/storage';
  8. import { ACCESS_TOKEN } from '@/store/mutation-types';
  9. import { setLogin } from '@/state';
  10. export default defineComponent({
  11. name: 'login-change-model',
  12. props: {
  13. credential: {
  14. type: Object,
  15. default: () => ({})
  16. }
  17. },
  18. emits: ['close', 'confirm'],
  19. setup(props, { emit }) {
  20. const radioChecked = ref();
  21. const btnLoading = ref(false);
  22. const list = ref([] as any);
  23. onMounted(async () => {
  24. try {
  25. const { data } = await request.post('/edu-app/open/user/getMultiUser', {
  26. data: {
  27. ...props.credential
  28. }
  29. });
  30. const result = data || [];
  31. result.forEach((item: any) => {
  32. list.value.push({
  33. userId: item.studentId,
  34. avatar: item.avatar,
  35. nickname: item.nickname,
  36. schoolName: item.schoolName,
  37. token: item.token,
  38. tokenExpireTime: item.tokenExpireTime
  39. });
  40. });
  41. } catch {
  42. //
  43. }
  44. });
  45. return () => (
  46. <div class={styles.loginChangeModel}>
  47. <i
  48. class={styles.iconClose}
  49. onClick={() => {
  50. emit('close');
  51. }}></i>
  52. <div class={styles.popupTitle}>
  53. <span>选择学生</span>
  54. </div>
  55. <RadioGroup class={styles.selectStudent} v-model={radioChecked.value}>
  56. {list.value.map((item: any) => (
  57. <div
  58. class={[
  59. styles.cell,
  60. radioChecked.value === item.userId && styles.cellSelected
  61. ]}
  62. onClick={() => {
  63. radioChecked.value = item.userId;
  64. // emit('confirm', item);
  65. // emit('close');
  66. }}>
  67. <Radio name={item.userId}>
  68. {{
  69. icon: (props: any) => (
  70. <Image
  71. class={styles.iconImage}
  72. src={props.checked ? checkBoxActive : checkBoxDefault}
  73. />
  74. )
  75. }}
  76. </Radio>
  77. <div class={styles.userInfo}>
  78. <Image src={item.avatar} class={styles.userImg} fit="cover" />
  79. <div class={styles.usernames}>
  80. <div class={styles.name}>
  81. <span class={styles.names}>{item.nickname}</span>
  82. </div>
  83. {item.schoolName && (
  84. <div class={styles.schoolname}>{item.schoolName}</div>
  85. )}
  86. </div>
  87. </div>
  88. </div>
  89. ))}
  90. </RadioGroup>
  91. <div class={styles.addStudentBtn}>
  92. <Button
  93. block
  94. round
  95. loading={btnLoading.value}
  96. disabled={!radioChecked.value || btnLoading.value}
  97. color="linear-gradient( 135deg, #31C7FF 0%, #007AFE 100%)"
  98. onClick={async () => {
  99. btnLoading.value = true;
  100. try {
  101. const item = list.value.find(
  102. (item: any) => item.userId === radioChecked.value
  103. );
  104. if (!item.userId) return;
  105. const forms: any = {
  106. username: props.credential.phone,
  107. client_id: 'cooleshow-student',
  108. client_secret: 'cooleshow-student',
  109. password: item.token,
  110. grant_type: 'password',
  111. loginType: 'TOKEN'
  112. };
  113. const result = await request.post('/edu-app/userlogin', {
  114. requestType: 'form',
  115. data: {
  116. ...forms
  117. }
  118. });
  119. storage.set(
  120. ACCESS_TOKEN,
  121. result.data.token_type + ' ' + result.data.access_token
  122. );
  123. const userCash = await request.get(
  124. '/edu-app/user/getUserInfo',
  125. {
  126. initRequest: true // 初始化接口
  127. }
  128. );
  129. setLogin(userCash.data);
  130. emit('confirm', {
  131. loginTag: true
  132. });
  133. emit('close');
  134. } finally {
  135. //
  136. btnLoading.value = false;
  137. }
  138. }}>
  139. <span>确认</span>
  140. </Button>
  141. </div>
  142. </div>
  143. );
  144. }
  145. });