seoHelper.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { randomRange, log10 } from '../utils/misc'
  2. import murmurhash from 'murmurhash'
  3. export const genDocumentHeadData = (input: {
  4. ctx: any
  5. catName?: string
  6. }) => {
  7. const { ctx, catName } = input || {}
  8. const canonical: string = process.server ? `https://${ctx.req.headers.host}${ctx.req.url}` : location && location.href
  9. let title: string
  10. let keywords: string
  11. let descrption: string
  12. if (catName) {
  13. title = `${catName} 软件、网站、APP、小程序开发及SaaS、PaaS、IaaS、API服务平台-开发屋`
  14. keywords = `${catName}软件开发,${catName}APP开发,${catName}小程序开发,${catName}SaaS服务商`
  15. descrption = `开发屋为${catName}中小型企业企业提供各行业软件、APP、小程序开发服务,并整合开发项目所需的各种SaaS、PaaS,LaaS以及API服务商供选择,全程解决开发需求!`
  16. } else {
  17. title = '开发屋-提供网站建设、APP软件、小程序开发及SaaS、PasS、IaaS服务'
  18. keywords = '网站开发, 软件APP开发, SaaS, PaaS'
  19. descrption = '开发屋为企业提供行业内领先的技术解决方案,包括行业定制化SaaS、PasS、API数据接口服务以及技术组织,保障企业在降低人力开发成本的同时,得到优质的项目开发实力。'
  20. }
  21. return {
  22. title,
  23. keywords,
  24. descrption,
  25. canonical
  26. }
  27. }
  28. const scoreSubCatItem = ({path, topCatId, subCatId}) => {
  29. const uni = murmurhash.v3(path)
  30. const isRoot = path === '/'
  31. return (item: any, idx: number) => {
  32. let score = 0
  33. if (topCatId && item.parent_id === topCatId) {
  34. score += 100
  35. }
  36. if (subCatId && item.cat_id === subCatId) {
  37. score -= 100
  38. }
  39. if (!isRoot && !score) {
  40. score += log10(Math.abs(uni - murmurhash.v3(item.cat_id)))
  41. }
  42. return Object.assign({}, item, {
  43. _score: score
  44. })
  45. }
  46. }
  47. const genFooterLinkItem = (name: string, hashId: string) => {
  48. return {
  49. name: `${name}技术开发`,
  50. url: `/c/${hashId}`
  51. }
  52. }
  53. export const genDocumentFooterData = (input: {
  54. ctx: any
  55. classes: any[]
  56. topCatId?: string
  57. subCatId?: string
  58. }) => {
  59. const { ctx, classes, topCatId, subCatId } = input || {}
  60. const host = (process.server ? ctx.req.headers.host : location.host) || ''
  61. const path = process.server ? ctx.req.url : location.pathname || '/'
  62. const baseLink = host && host.indexOf('local') > -1 ? `http://${host}` : `https://${host}`
  63. const link = [{
  64. name: '热门领域技术解决方案',
  65. data: classes.map(topCat => genFooterLinkItem(topCat.name, topCat.hash_id))
  66. }, {
  67. name: '细分领域技术解决方案',
  68. data: classes
  69. .flatMap(topCat => topCat.categories || [])
  70. .map(scoreSubCatItem({path, topCatId, subCatId}))
  71. .sort((a, b) => b._score - a._score)
  72. .slice(0, randomRange(20, 30))
  73. .map(cat => genFooterLinkItem(cat.name, cat.cat_id))
  74. }]
  75. return {
  76. baseLink,
  77. link
  78. }
  79. }