dealSeoList.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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: 12,
  11. total: 0,
  12. loading: false,
  13. selectedCateIdOne: '',
  14. selectedCateIdTwo: '',
  15. noMore: true
  16. }
  17. this.cateNameOne = ''
  18. this.cateNameTwo = ''
  19. this.mobile = this.app.$deviceType.isMobile()
  20. this.root_type = 0
  21. }
  22. async dealData() {
  23. const self = this
  24. let {
  25. name,
  26. query: { page = 1, root_type = 0 },
  27. path,
  28. params,
  29. fullPath
  30. } = this.app.context.route
  31. this.pagination.page = Number(page)
  32. this.root_type = root_type
  33. // 目前仅将二级 id 拼接到 url 上
  34. let match = params.pathMatch || ''
  35. let matchList = match.split('/')
  36. matchList.pop()
  37. let lastMatch = matchList.pop() || ''
  38. // console.log(`match: ${match}, matchList: ${matchList}, lastMatch: ${lastMatch}`)
  39. // 重定向
  40. if (path.indexOf('/frontend/skill/list') > -1) {
  41. this.redirect(301, '/skill/' + lastMatch)
  42. }
  43. let skillCate = await this._getSkillCate()
  44. let skillCateAll = []
  45. skillCate.forEach(item => {
  46. if (item.children && item.children.length) {
  47. item.children.forEach(child => {
  48. skillCateAll.push(child)
  49. })
  50. }
  51. })
  52. if (lastMatch) {
  53. // 遍历分类数组,因为每个 children 都添加了“全部”,此处逻辑无需修改
  54. let selectedCateIdOne = ''
  55. skillCate.forEach(cateOne => {
  56. cateOne.children.forEach(cateTwo => {
  57. if (cateTwo.value === lastMatch) {
  58. selectedCateIdOne = cateOne.value
  59. self.cateNameOne = cateOne.label
  60. if (cateTwo.label === '全部') {
  61. self.cateNameTwo = cateOne.label
  62. } else {
  63. self.cateNameTwo = cateTwo.label
  64. }
  65. }
  66. })
  67. })
  68. if (selectedCateIdOne) {
  69. this.pagination.selectedCateIdOne = selectedCateIdOne
  70. this.pagination.selectedCateIdTwo = lastMatch
  71. }
  72. }
  73. // 处理完分类信息,再获取数据
  74. let skillList = await this._getSkillList()
  75. return {
  76. root_type,
  77. skillCate,
  78. skillCateAll,
  79. skillList, //首次获取的数据
  80. mobile: this.mobile,
  81. pagination: this.pagination,
  82. head: this.dealThisMeta()
  83. }
  84. }
  85. /** 获取技能分类 */
  86. async _getSkillCate () {
  87. let res = await this.$axios.$post('/api/sale/cateListYes', { type: 2, root_type: this.root_type })
  88. let skillCate = []
  89. if (Number(res.status) === 1) {
  90. skillCate = res.data || []
  91. skillCate = skillCate.map(item => {
  92. let children = item.child_list.map(child => {
  93. return {
  94. value: child.f_name,
  95. label: child.name
  96. }
  97. })
  98. return {
  99. value: item.f_name,
  100. label: item.name,
  101. children: children
  102. }
  103. })
  104. // web 端,为所有二级分类添加 “全部”
  105. if (!this.mobile) {
  106. skillCate.forEach(item => {
  107. if (item.children) {
  108. let allItem = { value: item.value, label: '全部' }
  109. item.children.splice(0, 0, allItem)
  110. }
  111. })
  112. }
  113. }
  114. return skillCate
  115. }
  116. /** 获取技能服务列表 */
  117. async _getSkillList () {
  118. // 接口参数释义:https://www.yesdev.cn/apidocs-detail-20.html
  119. const data = {
  120. type: 1,
  121. page: this.pagination.page,
  122. page_size: this.pagination.pagesize,
  123. cate_id: this.pagination.selectedCateIdTwo,
  124. status: 2,
  125. owner_type: 1,
  126. root_type: this.root_type
  127. }
  128. let res = await this.$axios.$post('/api/sale/saleList', data)
  129. let skillList = []
  130. if (Number(res.status) === 1) {
  131. skillList = res.data.list || []
  132. skillList.forEach((item) => {
  133. let imageList = item.image.split(',')
  134. item.coverImage = imageList[0] || ''
  135. imageList.splice(0, 1)
  136. item.imageList = imageList
  137. })
  138. this.pagination.total = Number(res.data.total)
  139. this.pagination.pagesize = Number(res.data.page_size) || 9
  140. if (this.pagination.page * this.pagination.pagesize >= this.pagination.total) {
  141. this.pagination.noMore = true
  142. } else {
  143. this.pagination.noMore = false
  144. }
  145. }
  146. return skillList
  147. }
  148. dealThisMeta() {
  149. let head = {
  150. title: "",
  151. keyword: "",
  152. description: "",
  153. h1: "",
  154. canonical: "",
  155. metaLocation: ""
  156. }
  157. if (this.req) {
  158. const { headers: { host }, url } = this.req
  159. //拼接canonical
  160. if (host.indexOf('local') !== -1) {
  161. head.canonical = 'http://' + host + url
  162. } else {
  163. head.canonical = 'https://' + host + url
  164. }
  165. }
  166. if (this.cateNameTwo) {
  167. // 分类页
  168. head.title = `${this.cateNameTwo}技能服务-程序员客栈技术服务`;
  169. head.keyword = `${this.cateNameTwo}开发,${this.cateNameTwo}编程,自学${this.cateNameTwo},${this.cateNameTwo}教学`;
  170. head.description = "技能服务是程序员客栈远程工作平台为企业和自由职业者提供的标准化数字服务,通过标准定价的模块化技能,帮助企业和自由职业者快速达成合作。";
  171. } else {
  172. // 列表页,无筛选参数
  173. head.title = "程序员客栈技能商城-【程序员客栈技能服务】";
  174. head.keyword = "程序员客栈技能商城,远程工作,Logo设计,网页设计,微信公众号运营,PPT设计,社群运营,文案编辑,视频剪辑,音频录制,翻译";
  175. head.description = "技能服务是程序员客栈远程工作平台为企业和自由职业者提供的标准化数字服务,通过标准定价的模块化技能,帮助企业和自由职业者快速达成合作。";
  176. }
  177. return head
  178. }
  179. }