dealSeoList.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. export default class DealSeoData {
  2. constructor({$axios, req, app, redirect, error}) {
  3. this.$axios = $axios
  4. this.req = req
  5. this.app = app
  6. this.redirect = redirect
  7. this.error = error
  8. this.pagination = {
  9. page: 1,
  10. keywords: '',
  11. cate_id_two: [],
  12. // 价格筛选:0 全部,1 付费,2 免费
  13. filter_price: 0,
  14. // 排序:0 默认,1 价格从高到低,2 价格从低到高
  15. sort: 0,
  16. // 非接口参数
  17. pagesize: 20,
  18. total: 0,
  19. loading: false,
  20. noMore: true,
  21. selectedCateIdOne: ''
  22. }
  23. this.cateNameOne = ''
  24. this.cateNameTwo = ''
  25. this.mobile = this.app.$deviceType.isMobile()
  26. this.root_type = 0
  27. }
  28. async dealData() {
  29. const self = this
  30. let {
  31. name,
  32. query: { page = 1, root_type = 0 },
  33. path,
  34. params,
  35. fullPath
  36. } = this.app.context.route
  37. this.pagination.page = Number(page)
  38. this.root_type = root_type
  39. // 目前仅将二级 id 拼接到 url 上
  40. let match = params.pathMatch || ''
  41. let matchList = match.split('/')
  42. matchList.pop()
  43. let lastMatch = matchList.pop() || ''
  44. console.log(`match: ${match}, matchList: ${matchList}, lastMatch: ${lastMatch}`)
  45. // 重定向
  46. if (path.indexOf('/work_down') > -1) {
  47. this.redirect(301, '/works/' + lastMatch)
  48. }
  49. let categoryList = await this._getWorksCategory()
  50. let categoryAll = []
  51. categoryList.forEach(item => {
  52. if (item.child && item.child.length) {
  53. item.child.forEach(child => {
  54. if (child.name && child.name !== '全部') {
  55. categoryAll.push(child)
  56. }
  57. })
  58. }
  59. })
  60. if (lastMatch) {
  61. // 遍历分类数组
  62. let selectedCateIdOne = ''
  63. categoryList.forEach(cateOne => {
  64. cateOne.child.forEach(cateTwo => {
  65. if (cateTwo.f_name === lastMatch) {
  66. selectedCateIdOne = cateOne.f_name
  67. self.cateNameOne = cateOne.name
  68. if (cateTwo.name === '全部') {
  69. self.cateNameTwo = cateOne.name
  70. } else {
  71. self.cateNameTwo = cateTwo.name
  72. }
  73. }
  74. })
  75. })
  76. if (selectedCateIdOne) {
  77. this.pagination.selectedCateIdOne = selectedCateIdOne
  78. this.pagination.cate_id_two.push(lastMatch)
  79. }
  80. }
  81. // 处理完分类信息,再获取数据
  82. let worksList = await this._getWorksList()
  83. return {
  84. root_type,
  85. categoryList,
  86. categoryAll,
  87. worksList, //首次获取的数据
  88. mobile: this.mobile,
  89. pagination: this.pagination,
  90. head: this.dealThisMeta()
  91. }
  92. }
  93. /** 获取作品分类 */
  94. async _getWorksCategory () {
  95. let res = await this.$axios.$post('/api/user_works/cate', { root_type: this.root_type })
  96. let categoryList = []
  97. if (Number(res.status) === 1) {
  98. categoryList = res.data || []
  99. // web 端,为所有二级分类添加 “全部”
  100. if (!this.mobile) {
  101. categoryList.forEach(item => {
  102. if (item.child) {
  103. let allItem = { f_name: item.f_name, name: '全部' }
  104. item.child.splice(0, 0, allItem)
  105. }
  106. })
  107. }
  108. }
  109. return categoryList
  110. }
  111. /** 获取作品列表 */
  112. async _getWorksList () {
  113. const data = {
  114. page: this.pagination.page,
  115. keywords: this.pagination.keywords,
  116. cate_id_two: this.pagination.cate_id_two.join(','),
  117. root_type: this.root_type
  118. }
  119. let res = await this.$axios.$post('/api/user_works/workFileList', data)
  120. let worksList = []
  121. if (Number(res.status) === 1) {
  122. worksList = res.data.list || []
  123. this.pagination.pagesize = Number(res.data.pagesize) || 20
  124. this.pagination.total = Number(res.data.total) || 0
  125. if (this.pagination.page * this.pagination.pagesize >= this.pagination.total) {
  126. this.pagination.noMore = true
  127. } else {
  128. this.pagination.noMore = false
  129. }
  130. }
  131. return worksList
  132. }
  133. dealThisMeta() {
  134. let head = {
  135. title: "",
  136. keyword: "",
  137. description: "",
  138. h1: "",
  139. canonical: "",
  140. metaLocation: ""
  141. }
  142. if (this.req) {
  143. const { headers: { host }, url } = this.req
  144. //拼接canonical
  145. if (host.indexOf('local') !== -1) {
  146. head.canonical = 'http://' + host + url
  147. } else {
  148. head.canonical = 'https://' + host + url
  149. }
  150. }
  151. if (this.cateNameTwo) {
  152. // 分类页
  153. head.title = `${this.cateNameTwo}资源下载-开发技术资源-程序员客栈资源商城`;
  154. head.keyword = `${this.cateNameTwo}资源下载,${this.cateNameTwo}开发技术资源下载,${this.cateNameTwo}项目源码下载`;
  155. head.description = `${this.cateNameTwo}开发技术资源,为IT开发人员提供权威的${this.cateNameTwo}学习、${this.cateNameTwo}编程源码、${this.cateNameTwo}电子书、各阶段资料下载等服务.更多下载资源请访问程序员客栈APP或官网资源商城`;
  156. } else {
  157. // 列表页,无筛选参数
  158. head.title = "IT开发资源下载-【程序员客栈资源商城】";
  159. head.keyword = "IT开发资源下载,开发源码下载";
  160. head.description = "程序员客栈资源商城是一个提供学习资源、源码、在线学习视频、IT电子书、各类免费软件等下载服务的IT资源大本营,致力于为软件开发者提供知识传播、资源共享、共同学习的优质学习资源平台.";
  161. }
  162. return head
  163. }
  164. }