dealSeoList.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. pagesize: 9,
  11. total: 0,
  12. loading: false,
  13. selectedCateIdOne: '',
  14. selectedCateIdTwo: '',
  15. noMore: true
  16. }
  17. this.cateNameOne = ''
  18. this.cateNameTwo = ''
  19. }
  20. async dealData() {
  21. const self = this
  22. let {
  23. name,
  24. query: { page = 1 },
  25. path,
  26. params,
  27. fullPath
  28. } = this.app.context.route
  29. this.pagination.page = Number(page)
  30. // 目前仅将二级 id 拼接到 url 上
  31. let match = params.pathMatch || ''
  32. let matchList = match.split('/').map(item => item.toLocaleLowerCase())
  33. let lastMatch = matchList.pop()
  34. // console.log(`match: ${match}, matchList: ${matchList}, lastMatch: ${lastMatch}`)
  35. // 重定向
  36. if (path.indexOf('/frontend/skill/list') > -1) {
  37. this.redirect(301, '/learn/' + lastMatch)
  38. }
  39. let skillCate = await this._getSkillCate()
  40. let skillCateAll = []
  41. skillCate.forEach(item => {
  42. if (item.children && item.children.length) {
  43. item.children.forEach(child => {
  44. skillCateAll.push(child)
  45. })
  46. }
  47. })
  48. if (Number(lastMatch) > 0) {
  49. // 遍历分类数组
  50. let selectedCateIdOne = ''
  51. skillCate.forEach(cateOne => {
  52. cateOne.children.forEach(cateTwo => {
  53. if (Number(cateTwo.value) === Number(lastMatch)) {
  54. selectedCateIdOne = cateOne.value
  55. self.cateNameOne = cateOne.label
  56. self.cateNameTwo = cateTwo.label
  57. }
  58. })
  59. })
  60. if (Number(selectedCateIdOne) > 0) {
  61. this.pagination.selectedCateIdOne = selectedCateIdOne
  62. this.pagination.selectedCateIdTwo = lastMatch
  63. }
  64. }
  65. // 处理完分类信息,再获取数据
  66. let skillList = await this._getSkillList()
  67. return {
  68. skillCate,
  69. skillCateAll,
  70. skillList, //首次获取的数据
  71. mobile: this.app.$deviceType.isMobile(),
  72. pagination: this.pagination,
  73. head: this.dealThisMeta()
  74. }
  75. }
  76. /** 获取技能分类 */
  77. async _getSkillCate () {
  78. let res = await this.$axios.$post('/api/sale/cateListYes', { type: 2 })
  79. let skillCate = []
  80. if (Number(res.status) === 1) {
  81. skillCate = res.data || []
  82. skillCate = skillCate.map(item => {
  83. let children = item.child_list.map(child => {
  84. return {
  85. value: child.category_id,
  86. label: child.name
  87. }
  88. })
  89. return {
  90. value: item.category_id,
  91. label: item.name,
  92. children: children
  93. }
  94. })
  95. }
  96. return skillCate
  97. }
  98. /** 获取技能服务列表 */
  99. async _getSkillList () {
  100. // 接口参数释义:https://www.yesdev.cn/apidocs-detail-20.html
  101. const data = {
  102. type: 1,
  103. page: this.pagination.page,
  104. page_size: this.pagination.pagesize,
  105. cate_id: this.pagination.selectedCateIdTwo,
  106. status: 2,
  107. owner_type: 1
  108. }
  109. let res = await this.$axios.$post('/api/sale/saleList', data)
  110. let skillList = []
  111. if (Number(res.status) === 1) {
  112. skillList = res.data.list || []
  113. skillList.forEach((item) => {
  114. let imageList = item.image.split(',')
  115. item.coverImage = imageList[0] || ''
  116. imageList.splice(0, 1)
  117. item.imageList = imageList
  118. })
  119. this.pagination.total = Number(res.data.total)
  120. this.pagination.pagesize = Number(res.data.page_size) || 9
  121. if (this.pagination.page * this.pagination.pagesize >= this.pagination.total) {
  122. this.pagination.noMore = true
  123. } else {
  124. this.pagination.noMore = false
  125. }
  126. }
  127. return skillList
  128. }
  129. dealThisMeta() {
  130. let head = {
  131. title: "",
  132. keyword: "",
  133. description: "",
  134. h1: "",
  135. canonical: "",
  136. metaLocation: ""
  137. }
  138. if (this.req) {
  139. const { headers: { host }, url } = this.req
  140. //拼接canonical
  141. if (host.indexOf('local') !== -1) {
  142. head.canonical = 'http://' + host + url
  143. } else {
  144. head.canonical = 'https://' + host + url
  145. }
  146. }
  147. if (this.cateNameTwo) {
  148. // 分类页
  149. head.title = `${this.cateNameTwo}培训-${this.cateNameTwo}学习-程序员客栈技术培训`;
  150. head.keyword = `${this.cateNameTwo}培训、${this.cateNameTwo}学习、自学${this.cateNameTwo}、${this.cateNameTwo}教学、${this.cateNameTwo}编程`;
  151. head.description = `程序员客栈技术培训提供${this.cateNameTwo}技术培训,${this.cateNameTwo}自学教程,${this.cateNameTwo}教学资源,${this.cateNameTwo}编程学习,${this.cateNameTwo}项目实战等高端${this.cateNameTwo}技术大牛一对一教学培训资源`;
  152. } else {
  153. // 列表页,无筛选参数
  154. head.title = "IT技术培训学习-【程序员客栈技术培训】";
  155. head.keyword = "IT技术学习、IT培训、编程开发学习、自学编程";
  156. head.description = "程序员客栈技术培训是一个提供了编程的基础技术教程, 介绍了HTML、CSS、Javascript、Python,Java,Ruby,C,PHP , MySQL等各种编程语言的培训课程。 同时也提供了大量的高端程序员,通过一对一教学,帮助您更好的学习编程。";
  157. }
  158. return head
  159. }
  160. }