photo.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import OEmpty from '@/components/o-empty'
  2. import request from '@/helpers/request'
  3. import {
  4. ActionSheet,
  5. Button,
  6. Dialog,
  7. Field,
  8. Image,
  9. List,
  10. Popover,
  11. Popup,
  12. showConfirmDialog,
  13. showToast,
  14. Sticky
  15. } from 'vant'
  16. import { defineComponent, onMounted, reactive } from 'vue'
  17. import { useRoute, useRouter } from 'vue-router'
  18. import styles from './photo.module.less'
  19. import iconPhoneDefaut from '../images/icon-photo-default.png'
  20. export default defineComponent({
  21. name: 'phone',
  22. props: {
  23. height: {
  24. type: [String, Number],
  25. default: 'auto'
  26. }
  27. },
  28. setup(props) {
  29. const route = useRoute()
  30. const router = useRouter()
  31. const state = reactive({
  32. oPopover: false,
  33. status: false,
  34. isLoading: false,
  35. photoName: null, // 相册名称
  36. list: [] as any,
  37. listState: {
  38. dataShow: true, // 判断是否有数据
  39. loading: false,
  40. finished: false
  41. },
  42. params: {
  43. page: 1,
  44. rows: 20
  45. },
  46. selectItem: {} as any,
  47. selectType: 'add'
  48. })
  49. const onAddPhoto = async () => {
  50. try {
  51. if (!state.photoName) {
  52. showToast('请输入相册名称')
  53. state.status = true
  54. return
  55. }
  56. if (state.selectType === 'add') {
  57. await request.post('/api-school/orchestraPhotoAlbum/save', {
  58. data: {
  59. orchestraId: route.query.id,
  60. name: state.photoName
  61. }
  62. })
  63. setTimeout(() => {
  64. showToast('添加成功')
  65. }, 100)
  66. } else {
  67. await request.post('/api-school/orchestraPhotoAlbum/update', {
  68. data: {
  69. id: state.selectItem.id,
  70. orchestraId: route.query.id,
  71. name: state.photoName
  72. }
  73. })
  74. setTimeout(() => {
  75. showToast('修改成功')
  76. }, 100)
  77. }
  78. state.status = false
  79. setTimeout(() => {
  80. state.photoName = null
  81. onSearch()
  82. }, 1100)
  83. } catch {
  84. //
  85. }
  86. }
  87. const onSearch = () => {
  88. state.params.page = 1
  89. state.list = []
  90. state.listState.dataShow = true // 判断是否有数据
  91. state.listState.loading = false
  92. state.listState.finished = false
  93. getList()
  94. }
  95. // 班级列表
  96. const getList = async () => {
  97. try {
  98. if (state.isLoading) return
  99. state.isLoading = true
  100. const res = await request.post('/api-school/orchestraPhotoAlbum/page', {
  101. data: {
  102. ...state.params,
  103. orchestraId: route.query.id
  104. }
  105. })
  106. state.listState.loading = false
  107. const result = res.data || {}
  108. // 处理重复请求数据
  109. if (state.list.length > 0 && result.current === 1) {
  110. return
  111. }
  112. const rows = result.rows || []
  113. state.list = state.list.concat(rows)
  114. state.listState.finished = result.current >= result.pages
  115. state.params.page = result.current + 1
  116. state.listState.dataShow = state.list.length > 0
  117. state.isLoading = false
  118. } catch {
  119. state.listState.dataShow = false
  120. state.listState.finished = true
  121. state.isLoading = false
  122. }
  123. }
  124. const onDetail = (item: any) => {
  125. sessionStorage.setItem('orchestra-detail-tab', 'photo')
  126. router.push({
  127. path: '/photo-detail',
  128. query: {
  129. photoId: item.id,
  130. name: item.name
  131. }
  132. })
  133. }
  134. const onRename = async () => {
  135. state.photoName = state.selectItem.name
  136. state.status = true
  137. }
  138. const onRemove = async () => {
  139. showConfirmDialog({
  140. message: '您确认删除该相册吗?'
  141. }).then(async () => {
  142. try {
  143. await request.post('/api-school/orchestraPhotoAlbum/remove', {
  144. requestType: 'form',
  145. data: {
  146. id: state.selectItem.id
  147. }
  148. })
  149. setTimeout(() => {
  150. showToast('删除成功')
  151. }, 100)
  152. setTimeout(() => {
  153. onSearch()
  154. }, 1100)
  155. } catch {
  156. //
  157. }
  158. })
  159. }
  160. onMounted(() => {
  161. getList()
  162. })
  163. return () => (
  164. <div class={styles.phone}>
  165. <Sticky position="top" offsetTop={props.height}>
  166. <Button
  167. icon="plus"
  168. block
  169. class={styles.addPhone}
  170. onClick={() => {
  171. state.status = true
  172. state.selectType = 'add'
  173. }}
  174. >
  175. 新建相册
  176. </Button>
  177. </Sticky>
  178. {state.listState.dataShow ? (
  179. <List
  180. v-model:loading={state.listState.loading}
  181. finished={state.listState.finished}
  182. finishedText=" "
  183. onLoad={getList}
  184. immediateCheck={false}
  185. class={styles.informationGroup}
  186. >
  187. <div class={styles.phoneContainer}>
  188. {state.list.map((item: any) => (
  189. <div class={styles.item} onClick={() => onDetail(item)}>
  190. {/* <i class={styles.more}></i> */}
  191. <i
  192. class={styles.more}
  193. onClick={(e: any) => {
  194. e.stopPropagation()
  195. state.oPopover = true
  196. state.selectItem = item
  197. state.selectType = 'update'
  198. }}
  199. ></i>
  200. {item.coverUrl ? (
  201. <Image class={styles.img} src={item.coverUrl} fit="cover" />
  202. ) : (
  203. <div class={[styles.img, styles.default]}>
  204. <Image src={iconPhoneDefaut} class={styles.defaultImg} />
  205. </div>
  206. )}
  207. <p class={[styles.name, 'van-ellipsis']}>{item.name}</p>
  208. <p class={styles.num}>{item.photoCount}张</p>
  209. </div>
  210. ))}
  211. </div>
  212. </List>
  213. ) : (
  214. <OEmpty btnStatus={false} tips="暂无相册" />
  215. )}
  216. <Popup v-model:show={state.status} round style={{ width: '80%' }}>
  217. <div class={styles.container}>
  218. <div class={styles.dialogTitle}>
  219. <i></i>
  220. {state.selectType === 'add' ? '新建相册' : '重命名相册'}
  221. </div>
  222. <Field
  223. class={styles.phoneName}
  224. v-model={state.photoName}
  225. placeholder="请输入相册名称"
  226. maxlength={15}
  227. />
  228. <div class={['van-hairline--top van-dialog__footer']}>
  229. <Button
  230. onClick={() => (state.status = false)}
  231. class={['van-button van-button--default van-button--large van-dialog__cancel']}
  232. >
  233. 取消
  234. </Button>
  235. <Button
  236. onClick={onAddPhoto}
  237. class={[
  238. 'van-button van-button--default van-button--large van-dialog__confirm van-hairline--left'
  239. ]}
  240. >
  241. 确认
  242. </Button>
  243. </div>
  244. </div>
  245. </Popup>
  246. <ActionSheet
  247. cancelText="取消"
  248. v-model:show={state.oPopover}
  249. closeOnClickAction
  250. actions={[
  251. { name: '重命名', callback: () => onRename() },
  252. { name: '删除', color: '#F44541', callback: () => onRemove() }
  253. ]}
  254. />
  255. </div>
  256. )
  257. }
  258. })