photo-detail.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import OEmpty from '@/components/o-empty'
  2. import OHeader from '@/components/o-header'
  3. import OSticky from '@/components/o-sticky'
  4. import request from '@/helpers/request'
  5. import {
  6. Button,
  7. Checkbox,
  8. CheckboxGroup,
  9. closeToast,
  10. Icon,
  11. Image,
  12. List,
  13. Popup,
  14. showDialog,
  15. showImagePreview,
  16. showLoadingToast,
  17. showToast,
  18. Uploader
  19. } from 'vant'
  20. import { defineComponent, onMounted, reactive } from 'vue'
  21. import { useRoute } from 'vue-router'
  22. import styles from './photo-detail.module.less'
  23. import iconDelete from '../images/icon-delete.png'
  24. import umiRequest from 'umi-request'
  25. import { getOssUploadUrl } from '@/state'
  26. import checkboxCheck from '../images/icon-checkbox-active.png'
  27. import checkboxDefault from '@/common/images/icon-checkbox-default.png'
  28. export default defineComponent({
  29. name: 'photo-detail',
  30. setup() {
  31. const route = useRoute()
  32. const state = reactive({
  33. isEdit: false,
  34. isLoading: false,
  35. list: [] as any,
  36. listState: {
  37. dataShow: true, // 判断是否有数据
  38. loading: false,
  39. finished: false
  40. },
  41. params: {
  42. page: 1,
  43. rows: 20
  44. },
  45. showPhoto: false,
  46. bucket: 'gyt',
  47. fileList: [] as any,
  48. checkboxRefs: [] as any,
  49. check: [] as any
  50. })
  51. const getList = async () => {
  52. try {
  53. if (state.isLoading) return
  54. state.isLoading = true
  55. const res = await request.post('/api-school/orchestraPhoto/page', {
  56. data: {
  57. ...state.params,
  58. orchestraPhotoAlbumId: route.query.photoId
  59. }
  60. })
  61. state.listState.loading = false
  62. const result = res.data || {}
  63. // 处理重复请求数据
  64. if (state.list.length > 0 && result.current === 1) {
  65. return
  66. }
  67. const rows = result.rows || []
  68. state.list = state.list.concat(rows)
  69. state.listState.finished = result.current >= result.pages
  70. state.params.page = result.current + 1
  71. state.listState.dataShow = state.list.length > 0
  72. state.isLoading = false
  73. } catch {
  74. state.listState.dataShow = false
  75. state.listState.finished = true
  76. state.isLoading = false
  77. }
  78. }
  79. const beforeRead = (file: any) => {
  80. // console.log(file, 'beforeRead')
  81. const isLt2M = file.size / 1024 / 1024 < 5
  82. if (!isLt2M) {
  83. showToast(`上传文件大小不能超过 5MB`)
  84. return false
  85. }
  86. return true
  87. }
  88. const beforeDelete = (file: any, detail: { index: any }) => {
  89. // this.dataModel.splice(detail.index, 1)
  90. return true
  91. }
  92. const afterRead = async (file: any, detail: any) => {
  93. try {
  94. file.status = 'uploading'
  95. file.message = '上传中...'
  96. await uploadFile(file)
  97. } catch (error) {
  98. //
  99. closeToast()
  100. }
  101. }
  102. const uploadFile = async (files: any) => {
  103. // 上传文件
  104. try {
  105. console.log(files, 'files')
  106. const file = files.file
  107. // 获取签名
  108. const signUrl = '/api-school/open/getUploadSign'
  109. const tempName = file.name || ''
  110. const fileName = '/orchestra/' + (tempName && tempName.replace(/ /gi, '_'))
  111. const key = new Date().getTime() + fileName
  112. const res = await request.post(signUrl, {
  113. data: {
  114. filename: fileName,
  115. bucketName: state.bucket,
  116. postData: {
  117. filename: fileName,
  118. acl: 'public-read',
  119. key: key,
  120. unknowValueField: []
  121. }
  122. }
  123. })
  124. showLoadingToast({
  125. message: '加载中...',
  126. forbidClick: true,
  127. loadingType: 'spinner',
  128. duration: 0
  129. })
  130. const obj = {
  131. policy: res.data.policy,
  132. signature: res.data.signature,
  133. key: key,
  134. KSSAccessKeyId: res.data.kssAccessKeyId,
  135. acl: 'public-read',
  136. name: fileName
  137. }
  138. const formData = new FormData()
  139. for (const key in obj) {
  140. formData.append(key, obj[key])
  141. }
  142. formData.append('file', file, fileName)
  143. await umiRequest(getOssUploadUrl(state.bucket), {
  144. method: 'POST',
  145. data: formData
  146. })
  147. // console.log(getOssUploadUrl(state.bucket) + key)
  148. const uploadUrl = getOssUploadUrl(state.bucket) + key
  149. closeToast()
  150. // state.fileList.push({ url: uploadUrl })
  151. files.src = uploadUrl
  152. files.status = 'done'
  153. } catch (error) {
  154. files.status = 'failed'
  155. console.log(error, 'uploadFile')
  156. }
  157. }
  158. // 上传图片
  159. const onSubmitPhoto = async () => {
  160. try {
  161. if (state.fileList.length <= 0) {
  162. showToast('请上传照片')
  163. return
  164. }
  165. const files = state.fileList.map((file: any) => {
  166. return file.src
  167. })
  168. console.log(files, 'onSubmitPhoto')
  169. await request.post('/api-school/orchestraPhoto/save', {
  170. data: {
  171. orchestraPhotoAlbumId: route.query.photoId,
  172. fileUrl: files.join(',')
  173. }
  174. })
  175. state.showPhoto = false
  176. state.fileList = []
  177. state.params.page = 1
  178. state.list = []
  179. state.listState.dataShow = true // 判断是否有数据
  180. state.listState.loading = false
  181. state.listState.finished = false
  182. getList()
  183. // setTimeout(() => {
  184. // showToast('添加成功')
  185. // state.showPhoto = false
  186. // state.fileList = []
  187. // }, 100)
  188. // setTimeout(() => {
  189. // state.params.page = 1
  190. // state.list = []
  191. // state.listState.dataShow = true // 判断是否有数据
  192. // state.listState.loading = false
  193. // state.listState.finished = false
  194. // getList()
  195. // }, 1100)
  196. } catch {
  197. //
  198. }
  199. }
  200. // 预览图片
  201. const onShowImage = (index: number) => {
  202. const files = state.list.map((file: any) => {
  203. return file.fileUrl
  204. })
  205. showImagePreview({
  206. images: files,
  207. startPosition: index,
  208. closeable: true
  209. })
  210. }
  211. // 选择照片
  212. const onSelect = (type: string) => {
  213. state.checkboxRefs[type].toggle()
  214. }
  215. // 删除
  216. const onRemove = () => {
  217. if (state.check.length <= 0) {
  218. showToast('请选择需要删除的图片')
  219. return
  220. }
  221. showDialog({
  222. title: '确认删除',
  223. message: '删除选择的照片',
  224. confirmButtonText: '确认',
  225. showCancelButton: true
  226. }).then(async () => {
  227. console.log(state.check, 'check')
  228. try {
  229. await request.post('/api-school/orchestraPhoto/remove', {
  230. requestType: 'form',
  231. data: {
  232. ids: state.check.join(',')
  233. }
  234. })
  235. state.params.page = 1
  236. state.list = []
  237. state.listState.dataShow = true // 判断是否有数据
  238. state.listState.loading = false
  239. state.listState.finished = false
  240. getList()
  241. // setTimeout(() => {
  242. // showToast('删除成功')
  243. // }, 100)
  244. // setTimeout(() => {
  245. // state.params.page = 1
  246. // state.list = []
  247. // state.listState.dataShow = true // 判断是否有数据
  248. // state.listState.loading = false
  249. // state.listState.finished = false
  250. // getList()
  251. // }, 1100)
  252. } catch {
  253. //
  254. }
  255. })
  256. }
  257. onMounted(() => {
  258. getList()
  259. })
  260. return () => (
  261. <div class={[styles.phoneDetail, !state.listState.dataShow && 'emptyRootContainer']}>
  262. <OSticky position="top">
  263. <OHeader title={(route.query.name as any) || ''}>
  264. {{
  265. right: () => (
  266. <Icon name={iconDelete} size={22} onClick={() => (state.isEdit = !state.isEdit)} />
  267. )
  268. }}
  269. </OHeader>
  270. <Button
  271. icon="plus"
  272. block
  273. class={styles.addPhone}
  274. onClick={() => (state.showPhoto = true)}
  275. >
  276. 上传照片
  277. </Button>
  278. </OSticky>
  279. {state.listState.dataShow ? (
  280. <List
  281. // v-model:loading={state.listState.loading}
  282. finished={state.listState.finished}
  283. finishedText=" "
  284. onLoad={getList}
  285. immediateCheck={false}
  286. >
  287. <CheckboxGroup class={styles.phoneContainer} v-model={state.check}>
  288. {state.list.map((item: any, index: number) => (
  289. <div
  290. class={[styles.item, state.check.includes(item.id) && styles.itemBorder]}
  291. onClick={() => {
  292. if (!state.isEdit) {
  293. onShowImage(index)
  294. } else {
  295. onSelect(item.id)
  296. }
  297. }}
  298. >
  299. <Checkbox
  300. name={item.id}
  301. checkedColor="#64a9ff"
  302. class={[styles.checkbox, !state.isEdit && styles.checkboxHide]}
  303. ref={(el: any) => (state.checkboxRefs[item.id] = el)}
  304. onClick={(e: any) => {
  305. e.stopPropagation()
  306. }}
  307. v-slots={{
  308. icon: (props: any) => (
  309. <Icon
  310. class={styles.iconChecked}
  311. name={props.checked ? checkboxCheck : checkboxDefault}
  312. />
  313. )
  314. }}
  315. />
  316. <div
  317. class={styles.img}
  318. style={
  319. item.fileUrl
  320. ? { backgroundImage: `url(${item.fileUrl})`, backgroundSize: 'cover' }
  321. : ''
  322. }
  323. ></div>
  324. </div>
  325. ))}
  326. </CheckboxGroup>
  327. </List>
  328. ) : (
  329. <OEmpty btnStatus={false} tips="暂无照片" />
  330. )}
  331. {state.isEdit && (
  332. <OSticky position="bottom">
  333. <div class={'btnGroup'}>
  334. <Button block round type="primary" onClick={onRemove}>
  335. 删除
  336. </Button>
  337. </div>
  338. </OSticky>
  339. )}
  340. <Popup v-model:show={state.showPhoto} round style={{ width: '92%' }}>
  341. <div class={styles.container}>
  342. <div class={styles.dialogTitle}>
  343. <i></i>
  344. 上传照片
  345. </div>
  346. <div class={styles.photos}>
  347. <Uploader
  348. v-model={state.fileList}
  349. afterRead={afterRead}
  350. beforeRead={beforeRead}
  351. beforeDelete={beforeDelete}
  352. accept="image/*"
  353. maxCount={9}
  354. />
  355. </div>
  356. <div class={['van-hairline--top van-dialog__footer']}>
  357. <Button
  358. onClick={() => {
  359. state.showPhoto = false
  360. state.fileList = []
  361. }}
  362. class={['van-button van-button--default van-button--large van-dialog__cancel']}
  363. >
  364. 取消
  365. </Button>
  366. <Button
  367. onClick={onSubmitPhoto}
  368. class={[
  369. 'van-button van-button--default van-button--large van-dialog__confirm van-hairline--left'
  370. ]}
  371. >
  372. 确认
  373. </Button>
  374. </div>
  375. </div>
  376. </Popup>
  377. </div>
  378. )
  379. }
  380. })