dealSeoDetail.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.skillDetail = {}
  9. this.isExist = true
  10. }
  11. async dealData() {
  12. let {
  13. name,
  14. query: { act },
  15. path,
  16. params,
  17. fullPath
  18. } = this.app.context.route
  19. const sale_id = params.id || ''
  20. // 重定向
  21. if (path.indexOf('/frontend/skill/detail') > -1) {
  22. this.redirect(301, '/s/' + sale_id)
  23. }
  24. const skillDetail = await this._getSkillDetail(sale_id)
  25. if (skillDetail && skillDetail.image && skillDetail.image.length) {
  26. let imageList = skillDetail.image.split(',')
  27. skillDetail.coverImage = imageList[0] || ''
  28. imageList.splice(0, 1)
  29. skillDetail.imageList = imageList
  30. }
  31. this.skillDetail = skillDetail
  32. return {
  33. isExist: this.isExist,
  34. sale_id,
  35. skillDetail,
  36. mobile: this.app.$deviceType.isMobile(),
  37. head: this.dealThisMeta(),
  38. act
  39. }
  40. }
  41. /** 获取技能详情 */
  42. async _getSkillDetail (sale_id) {
  43. let res = await this.$axios.$post('/api/sale/saleInfo', { sale_id })
  44. let skillDetail = {}
  45. if (Number(res.status) === 1) {
  46. skillDetail = res.data
  47. } else if (Number(res.status) === 40001) {
  48. this.isExist = false
  49. }
  50. return skillDetail
  51. }
  52. dealThisMeta() {
  53. if (!this.isExist) {
  54. // 页面不存在时
  55. return {
  56. title: "页面不存在-程序员客栈",
  57. keyword: "",
  58. description: "",
  59. h1: "",
  60. canonical: "",
  61. metaLocation: ""
  62. }
  63. }
  64. let content = this.skillDetail.content.trim()
  65. let title = this.skillDetail.title.trim()
  66. const nickname = this.skillDetail.user.nickname
  67. if (content.length > 15) {
  68. content = content.substring(0, 15)
  69. }
  70. if (title.length > 15) {
  71. title = title.substring(0, 15)
  72. }
  73. let canonical = ''
  74. if (this.req) {
  75. const { headers: { host }, url } = this.req
  76. //拼接canonical
  77. if (host.indexOf('local') !== -1) {
  78. canonical = 'http://' + host + url
  79. } else {
  80. canonical = 'https://' + host + url
  81. }
  82. }
  83. let head = {
  84. title: `${this.skillDetail.title}-程序员客栈技能服务`,
  85. keyword: `${this.skillDetail.user.company},${this.skillDetail.direction_name},${this.skillDetail.title}`,
  86. description: `${nickname}可以为您提供:${title},技能服务是程序员客栈远程工作平台为企业和自由职业者提供的标准化数字服务,通过标准定价的模块化技能,帮助企业和自由职业者快速达成合作。`,
  87. h1: "",
  88. canonical: canonical,
  89. metaLocation: ""
  90. }
  91. return head
  92. }
  93. }