photo.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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: '/orchestra-photo-create',
  128. query:{
  129. orchestraId: route.query.id,
  130. name: item.name,
  131. parentId: item.id
  132. }
  133. })
  134. // router.push({
  135. // path: '/photo-detail',
  136. // query: {
  137. // photoId: item.id,
  138. // name: item.name
  139. // }
  140. // })
  141. }
  142. const onRename = async () => {
  143. state.photoName = state.selectItem.name
  144. state.status = true
  145. }
  146. const onRemove = async () => {
  147. showConfirmDialog({
  148. message: '您确认删除该相册吗?'
  149. }).then(async () => {
  150. try {
  151. await request.post('/api-school/orchestraPhotoAlbum/remove', {
  152. requestType: 'form',
  153. data: {
  154. id: state.selectItem.id
  155. }
  156. })
  157. setTimeout(() => {
  158. showToast('删除成功')
  159. }, 100)
  160. setTimeout(() => {
  161. onSearch()
  162. }, 1100)
  163. } catch {
  164. //
  165. }
  166. })
  167. }
  168. onMounted(() => {
  169. getList()
  170. })
  171. return () => (
  172. <div class={styles.phone}>
  173. <Sticky position="top" offsetTop={props.height}>
  174. <Button
  175. icon="plus"
  176. block
  177. class={styles.addPhone}
  178. onClick={() => {
  179. state.status = true
  180. state.selectType = 'add'
  181. }}
  182. >
  183. 新建相册
  184. </Button>
  185. </Sticky>
  186. {state.listState.dataShow ? (
  187. <List
  188. v-model:loading={state.listState.loading}
  189. finished={state.listState.finished}
  190. finishedText=" "
  191. onLoad={getList}
  192. immediateCheck={false}
  193. class={styles.informationGroup}
  194. >
  195. <div class={styles.phoneContainer}>
  196. {state.list.map((item: any) => (
  197. <div class={styles.item} onClick={() => onDetail(item)}>
  198. {/* <i class={styles.more}></i> */}
  199. <i
  200. class={styles.more}
  201. onClick={(e: any) => {
  202. e.stopPropagation()
  203. state.oPopover = true
  204. state.selectItem = item
  205. state.selectType = 'update'
  206. }}
  207. ></i>
  208. {item.coverUrl ? (
  209. <Image class={styles.img} src={item.coverUrl} fit="cover" />
  210. ) : (
  211. <div class={[styles.img, styles.default]}>
  212. <Image src={iconPhoneDefaut} class={styles.defaultImg} />
  213. </div>
  214. )}
  215. <p class={[styles.name, 'van-ellipsis']}>{item.name}</p>
  216. <p class={styles.num}>{item.photoCount}张</p>
  217. </div>
  218. ))}
  219. </div>
  220. </List>
  221. ) : (
  222. <OEmpty btnStatus={false} tips="暂无相册" />
  223. )}
  224. <Popup v-model:show={state.status} round style={{ width: '80%' }}>
  225. <div class={styles.container}>
  226. <div class={styles.dialogTitle}>
  227. <i></i>
  228. {state.selectType === 'add' ? '新建相册' : '重命名相册'}
  229. </div>
  230. <Field
  231. class={styles.phoneName}
  232. v-model={state.photoName}
  233. placeholder="请输入相册名称"
  234. maxlength={15}
  235. />
  236. <div class={['van-hairline--top van-dialog__footer']}>
  237. <Button
  238. onClick={() => (state.status = false)}
  239. class={['van-button van-button--default van-button--large van-dialog__cancel']}
  240. >
  241. 取消
  242. </Button>
  243. <Button
  244. onClick={onAddPhoto}
  245. class={[
  246. 'van-button van-button--default van-button--large van-dialog__confirm van-hairline--left'
  247. ]}
  248. >
  249. 确认
  250. </Button>
  251. </div>
  252. </div>
  253. </Popup>
  254. <ActionSheet
  255. cancelText="取消"
  256. v-model:show={state.oPopover}
  257. closeOnClickAction
  258. actions={[
  259. { name: '重命名', callback: () => onRename() },
  260. { name: '删除', color: '#F44541', callback: () => onRemove() }
  261. ]}
  262. />
  263. </div>
  264. )
  265. }
  266. })