photo-create.tsx 8.2 KB

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