dealSeoDetail.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.learnDetail = {}
  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/learn/detail') > -1) {
  22. this.redirect(301, '/l/' + sale_id)
  23. }
  24. const learnDetail = await this._getLearnDetail(sale_id)
  25. const collectObj = await this._getLearnDetailCollected(sale_id)
  26. return {
  27. isExist: this.isExist,
  28. sale_id,
  29. learnDetail,
  30. collectObj,
  31. mobile: this.app.$deviceType.isMobile(),
  32. head: this.dealThisMeta(),
  33. act
  34. }
  35. }
  36. /** 获取课程详情 */
  37. async _getLearnDetail (sale_id="999") {
  38. let res = await this.$axios.$post('/sapi/learn/info', { sale_id })
  39. let learnDetail = {}
  40. // console.log(res)
  41. if (Number(res.status) === 1) {
  42. learnDetail = res.data
  43. } else if (Number(res.status) === 40001) {
  44. this.isExist = false
  45. }
  46. this.learnDetail=learnDetail;
  47. return learnDetail
  48. }
  49. async _getLearnDetailCollected(sale_id){
  50. let params = {
  51. type: '100',
  52. item_id: sale_id+''
  53. }
  54. let collectObj = {}
  55. let res = await this.$axios.$post('/uapi/collection/check', params)
  56. if(res.status==1){
  57. collectObj = res.data
  58. }
  59. return collectObj
  60. }
  61. dealThisMeta() {
  62. if (!this.isExist) {
  63. // 页面不存在时
  64. return {
  65. title: "页面不存在-程序员客栈",
  66. keyword: "",
  67. description: "",
  68. h1: "",
  69. canonical: "",
  70. metaLocation: ""
  71. }
  72. }
  73. let head = {
  74. title: `客栈学院`,
  75. keyword: `客栈学院`,
  76. description: `客栈学院`,
  77. h1: "",
  78. canonical: "",
  79. metaLocation: ""
  80. }
  81. head.title = `${this.learnDetail.seo.title}`
  82. head.keyword = `${this.learnDetail.seo.keywords}`;
  83. head.description = `${this.learnDetail.seo.description}`;
  84. return head
  85. }
  86. }