item.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { defineComponent, ref } from 'vue'
  2. import { Button, Icon, Image, Popup, Tag, Toast } from 'vant'
  3. import classNames from 'classnames'
  4. import MusicIcon from './icons/music-icon.png'
  5. import InitUserIcon from './icons/init-user-icon.png'
  6. import FavoriteIcon from '../album/favorite.svg'
  7. import FavoritedIcon from '../album/favorited.svg'
  8. import iconShare from '../album/icon_share.svg'
  9. import styles from './item.module.less'
  10. import request from '@/helpers/request'
  11. import { state } from '@/state'
  12. import ColShare from '@/components/col-share'
  13. const chargeTypes = {
  14. CHARGE: '点播',
  15. FREE: '免费',
  16. VIP: 'VIP'
  17. }
  18. export default defineComponent({
  19. name: 'MusicItem',
  20. props: {
  21. data: {
  22. type: Object,
  23. default: {}
  24. },
  25. onClick: {
  26. type: Function
  27. }
  28. },
  29. emits: ['favorite'],
  30. setup({ onClick, data }, { emit }) {
  31. // const data = props.data
  32. // console.log(props, emit)
  33. const favorite = ref(data.favorite)
  34. const favoriteLoading = ref(false)
  35. const toggleFavorite = async (evt: MouseEvent) => {
  36. evt.stopPropagation()
  37. favoriteLoading.value = true
  38. try {
  39. await request.post('/music/sheet/favorite/' + data.id, {
  40. prefix:
  41. state.platformType === 'TEACHER' ? '/api-teacher' : '/api-student'
  42. })
  43. favorite.value = !favorite.value
  44. } catch (error) {}
  45. favoriteLoading.value = false
  46. emit('favorite')
  47. }
  48. const shareStatus = ref(false)
  49. const shareUrl = ref('')
  50. // console.log(data)
  51. const onShare = (evt: MouseEvent) => {
  52. evt.stopPropagation()
  53. // if (!shareStatus.value) {
  54. // Toast('暂无分享地址')
  55. // return
  56. // }
  57. // https://dev.colexiu.com/accompany/colexiu-share.html?id=2500&recomUserId=1
  58. shareUrl.value =
  59. location.origin +
  60. `/accompany/colexiu-share.html?id=${data.id}&recomUserId=${state.user.data?.userId}`
  61. shareStatus.value = true
  62. }
  63. return () => (
  64. <div>
  65. <div
  66. class={styles.item}
  67. onClick={() => {
  68. onClick?.(data)
  69. }}
  70. >
  71. <header class={[styles.header, 'van-hairline--bottom']}>
  72. <div class={styles.mate}>
  73. <Image src={MusicIcon} round class={styles.icon} />
  74. <div class={styles.info}>
  75. <h4 class="van-multi-ellipsis--l2">{data.musicSheetName}</h4>
  76. <p>{data.composer}</p>
  77. </div>
  78. </div>
  79. <div class={styles.buttons}>
  80. <Button
  81. class={classNames(
  82. styles.btn,
  83. styles[data.chargeType.toLocaleLowerCase()]
  84. )}
  85. >
  86. {chargeTypes[data.chargeType]}
  87. <Icon name="arrow" />
  88. </Button>
  89. </div>
  90. </header>
  91. <footer class={styles.footer}>
  92. <div class={styles.user}>
  93. <Image
  94. round
  95. src={data.addUserAvatar || InitUserIcon}
  96. class={styles.userIcon}
  97. />
  98. <p>{data.addName}</p>
  99. <div class={styles.tags}>
  100. {(data.subjectNames || '').split(',').map(item => (
  101. <Tag>{item}</Tag>
  102. ))}
  103. </div>
  104. </div>
  105. <div class={styles.icons}>
  106. {state.platformType === 'STUDENT' ? (
  107. <Button
  108. style={{ border: 'none' }}
  109. onClick={toggleFavorite}
  110. loading={favoriteLoading.value}
  111. >
  112. <Icon
  113. class={styles.favorite}
  114. name={favorite.value ? FavoritedIcon : FavoriteIcon}
  115. />
  116. </Button>
  117. ) : (
  118. <Button
  119. style={{ border: 'none' }}
  120. class={styles.shareBtn}
  121. onClick={onShare}
  122. >
  123. <Icon class={styles.favorite} name={iconShare} />
  124. </Button>
  125. )}
  126. </div>
  127. </footer>
  128. </div>
  129. <Popup
  130. v-model:show={shareStatus.value}
  131. style={{ background: 'transparent' }}
  132. teleport="body"
  133. >
  134. <ColShare
  135. teacherId={data.userId}
  136. shareUrl={shareUrl.value}
  137. shareType="music"
  138. >
  139. <div class={styles.shareMate}>
  140. <Image src={MusicIcon} round class={styles.icon} />
  141. <div class={styles.info}>
  142. <h4 class="van-multi-ellipsis--l2">{data.musicSheetName}</h4>
  143. <p>作曲人:{data.composer}</p>
  144. </div>
  145. </div>
  146. </ColShare>
  147. </Popup>
  148. </div>
  149. )
  150. }
  151. })