在 model 中创建获取历史记录api

在 model 的 Uploader 接口中添加 get 方法,用于查询 leancloud 历史记录

  • 注意如果是被删除的图片则没有 url, 所以resolve时需要过滤:resolve(images.filter((image: any) => image.attributes.url)))

    1. const Uploader = {
    2. add(file: any, filename: string) {
    3. // ...
    4. },
    5. get(page = 0, limit = 10) {
    6. const query = new Query('Image')
    7. query.include('owner')
    8. query.limit(limit)
    9. query.skip(page * limit)
    10. query.descending('createAt')
    11. query.equalTo('owner', AV.User.current())
    12. return new Promise((resolve, reject) => {
    13. query
    14. .find()
    15. .then((images) => resolve(images.filter((image: any) => image.attributes.url)))
    16. .catch((error) => reject(error))
    17. })
    18. },
    19. }

    创建 historyStore

    ```typescript import { message } from ‘antd’ import { makeObservable, observable, action } from ‘mobx’ import { Uploader } from ‘../models’

class HistoryStore { @observable list = [] @observable isLoading = false @observable hasMore = true @observable page = 0 limit = 10

constructor() { makeObservable(this) }

@action append(newList) { this.list = this.list.concat(newList) }

@action get() { this.isLoading = true Uploader.get(this.page, this.limit) .then((newList) => { this.append(newList) this.page++ if ((newList as any).length < this.limit) { this.hasMore = false } }) .catch((error) => { message.error(‘加载数据失败’) }) .finally(() => { this.isLoading = false }) } }

export default new HistoryStore()

  1. <a name="SO0LJ"></a>
  2. ### 使用 antd 的 List 组件展示历史记录
  3. 安装 `react-infinite-scroll-component` 用于无限滚动
  4. ```bash
  5. yarn add react-infinite-scroll-component
  1. import { Button, Divider, List } from 'antd'
  2. import { observer } from 'mobx-react'
  3. import { useStore } from '../store'
  4. import InfiniteScroll from 'react-infinite-scroll-component'
  5. import { MutableRefObject, useEffect, useRef } from 'react'
  6. import Loading from '../components/Loading'
  7. import { handleCopy } from '../models'
  8. const History: React.FC = observer(() => {
  9. const { HistoryStore } = useStore()
  10. const inputRef = useRef() as MutableRefObject<HTMLInputElement>
  11. const loadMore = () => {
  12. HistoryStore.get()
  13. }
  14. useEffect(() => {
  15. loadMore()
  16. // 组件卸载时清除本地记录
  17. return () => HistoryStore.resetHistory()
  18. }, [])
  19. return (
  20. <>
  21. <InfiniteScroll
  22. dataLength={HistoryStore.list.length}
  23. next={loadMore}
  24. loader={<Loading />}
  25. hasMore={!HistoryStore.isLoading && HistoryStore.hasMore}
  26. endMessage={<Divider plain>没有更多了 🤐</Divider>}
  27. >
  28. <List
  29. dataSource={HistoryStore.list}
  30. renderItem={(item: any) =>
  31. item ? (
  32. <List.Item key={item.id}>
  33. {/* 列表信息省略 */}
  34. </List.Item>
  35. ) : (
  36. ''
  37. )
  38. }
  39. ></List>
  40. </InfiniteScroll>
  41. </>
  42. )
  43. })
  44. export default History

删除图片

只需要知道图片的 id 就可以删除

  1. delete(id) {
  2. const file = AV.File.createWithoutData(id)
  3. file.destroy()
  4. },