DynamicInfo.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. export default class DynamicInfoData {
  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.consultDetail= {}
  9. this.from = ''
  10. this.isExist = true
  11. }
  12. async dealData() {
  13. let {
  14. name,
  15. path,
  16. params,
  17. fullPath,
  18. query
  19. } = this.app.context.route
  20. const dynamicId = params.id || ''
  21. // 重定向
  22. // if (path.indexOf('/frontend/consult/user') > -1) {
  23. // this.redirect(301, '/c/' + uid)
  24. // }
  25. const dynamicDetail = await this._fetchData(dynamicId)
  26. return {
  27. isExist: this.isExist,
  28. dynamicDetail,
  29. mobile: this.app.$deviceType.isMobile(),
  30. // head: this.dealThisMeta(),
  31. }
  32. }
  33. async _fetchData(dynamicId){
  34. let res = await this.$axios.$post('/uapi/dynamic/get_dynamic_detail', {
  35. dynamic_id:dynamicId
  36. })
  37. let dynamicDetail = {}
  38. if (Number(res.status) === 1) {
  39. let dynamicList = res.data.list
  40. console.log(dynamicList)
  41. dynamicDetail = {...dynamicList[0]}
  42. }else{
  43. this.isExist = false
  44. }
  45. return dynamicDetail
  46. }
  47. dealThisMeta() {
  48. let title = ''
  49. let descriptionTitle = ''
  50. let description = ''
  51. let canonical = ''
  52. if (!this.isExist) {
  53. // 页面不存在时
  54. return {
  55. title: "页面不存在-程序员客栈",
  56. keyword: "",
  57. description: "",
  58. h1: "",
  59. canonical: "",
  60. metaLocation: ""
  61. }
  62. }
  63. const nickname = this.consultDetail.user.nickname
  64. this.consultDetail.sale_list.forEach((item, index) => {
  65. if (index === 0) {
  66. title = item.title.trim()
  67. description = item.content.trim()
  68. }
  69. // if (index !== this.consultDetail.sale_list.length - 1) {
  70. // description += `${item.title}、`
  71. // } else {
  72. // description += item.title
  73. // }
  74. })
  75. if (description.length > 15) {
  76. description = description.substring(0, 15)
  77. }
  78. if (title.length > 15) {
  79. descriptionTitle = title.substring(0, 15)
  80. } else {
  81. descriptionTitle = title
  82. }
  83. if (this.req) {
  84. const { headers: { host }, url } = this.req
  85. //拼接canonical
  86. if (host.indexOf('local') !== -1) {
  87. canonical = 'http://' + host + url
  88. } else {
  89. canonical = 'https://' + host + url
  90. }
  91. }
  92. let head = {
  93. title: `${title}-程序员客栈咨询服务`,
  94. keyword: `${this.consultDetail.user.company},${this.consultDetail.user.direction_name},${title}`,
  95. description: `${nickname}可以为您提供:${descriptionTitle},程序员客栈邀请到国内外互联网名企资深工作者,为您提供1对1技术咨询服务。`,
  96. h1: "",
  97. canonical: canonical,
  98. metaLocation: ""
  99. }
  100. return head
  101. }
  102. }