fileList.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { mapGetters } from 'vuex'
  2. import { toAuthorization } from '~/utils'
  3. import downloadFile from '~/utils/donwload-tools'
  4. import { isWeChat } from '~/utils/validator'
  5. export default {
  6. layout: 'app-ross',
  7. filters: {
  8. crumbFormat(name) {
  9. if (name.length < 12) return name
  10. return name.substring(0, 10) + '…'
  11. },
  12. fileNameFormat(name) {
  13. return name.replace(/(.+)(.txt$)/, (match, $1, $2) => {
  14. return $1
  15. })
  16. },
  17. },
  18. data() {
  19. return {
  20. fileId: '',
  21. list: [],
  22. crumbList: [],
  23. }
  24. },
  25. computed: {
  26. ...mapGetters([
  27. 'routePrefix',
  28. 'supplierInfo',
  29. 'authUserId',
  30. 'accountType',
  31. 'appId',
  32. ]),
  33. },
  34. created() {
  35. this.fileId = this.$route.params.fileId
  36. this.fetchFileList()
  37. this.fetchCrumbsList()
  38. },
  39. methods: {
  40. // 处理面包屑
  41. generateCrumb(node) {
  42. const list = []
  43. if (!node) return list
  44. function recursive(node) {
  45. list.push(node)
  46. if (node.childNode) {
  47. recursive(node.childNode)
  48. }
  49. }
  50. recursive(node)
  51. return list
  52. },
  53. // 获取面包屑
  54. async fetchCrumbsList() {
  55. try {
  56. const res = await this.$http.api.fetchCrumbsList({
  57. authUserId: this.authUserId,
  58. fileId: this.fileId,
  59. })
  60. this.crumbList = this.generateCrumb(res.data)
  61. } catch (error) {
  62. console.log(error)
  63. }
  64. },
  65. // 下一级
  66. onRowClick(row) {
  67. if (row.fileType === 'article') {
  68. if (!this.checkLogin()) return
  69. if (row.articleType === 1) {
  70. const url = `${this.routePrefix}/database/article-detail?id=${row.articleId}`
  71. this.$router.push(url)
  72. } else {
  73. window.open(row.ossUrl, '_blank')
  74. }
  75. } else if (row.packageType === 1) {
  76. if (!this.checkLogin()) return
  77. this.$router.push(`${this.routePrefix}/docs/detail?id=${row.id}`)
  78. } else {
  79. this.$router.push(`${this.routePrefix}/docs/${row.id}`)
  80. }
  81. },
  82. // 获取文件列表
  83. async fetchFileList() {
  84. try {
  85. const res = await this.$http.api.fetchDocsList({
  86. fileId: this.fileId,
  87. authUserId: this.authUserId,
  88. })
  89. this.list = res.data
  90. } catch (error) {
  91. console.log(error)
  92. }
  93. },
  94. // 校验登录
  95. checkLogin() {
  96. const hasLogin = this.$store.getters.accessToken
  97. if (!hasLogin) {
  98. // 在微信浏览器中使用微信授权登录
  99. if (isWeChat() && this.appId && this.accountType === 2) {
  100. const payload = {
  101. authUserId: this.authUserId,
  102. routePrefix: this.routePrefix,
  103. }
  104. return toAuthorization(this.appId, payload)
  105. }
  106. this.$toast({ message: '请先登录', duration: 1000 })
  107. this.$store.commit('app/SHOW_LOGIN')
  108. return false
  109. }
  110. return true
  111. },
  112. // 下载文件
  113. onDownload(row, $event) {
  114. if (!this.checkLogin()) return
  115. let downUrl = ''
  116. if (row.packageType === 0) {
  117. downUrl = `${process.env.BASE_URL}/wx/data/path/package/zip?fileId=${
  118. row.id
  119. }&fileName=${encodeURIComponent(row.fileName)}&authUserId=${
  120. this.authUserId
  121. }`
  122. } else {
  123. downUrl = `${process.env.BASE_URL}/download/file?ossName=${
  124. row.ossName
  125. }&fileName=${encodeURIComponent(row.fileName)}`
  126. }
  127. downloadFile(downUrl, row.fileName, this, $event)
  128. },
  129. },
  130. }