| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- export default class DealSeoData {
- constructor({$axios, req, app, redirect, error}) {
- this.$axios = $axios
- this.req = req
- this.app = app
- this.redirect = redirect
- this.error = error
- this.pagination = {
- page: 1,
- keywords: '',
- cate_id_two: [],
- // 价格筛选:0 全部,1 付费,2 免费
- filter_price: 0,
- // 排序:0 默认,1 价格从高到低,2 价格从低到高
- sort: 0,
- // 非接口参数
- pagesize: 20,
- total: 0,
- loading: false,
- noMore: true,
- selectedCateIdOne: ''
- }
- this.cateNameOne = ''
- this.cateNameTwo = ''
- this.mobile = this.app.$deviceType.isMobile()
- this.root_type = 0
- }
- async dealData() {
- const self = this
- let {
- name,
- query: { page = 1, root_type = 0 },
- path,
- params,
- fullPath
- } = this.app.context.route
- this.pagination.page = Number(page)
- this.root_type = root_type
- // 目前仅将二级 id 拼接到 url 上
- let match = params.pathMatch || ''
- let matchList = match.split('/')
- matchList.pop()
- let lastMatch = matchList.pop() || ''
- console.log(`match: ${match}, matchList: ${matchList}, lastMatch: ${lastMatch}`)
- // 重定向
- if (path.indexOf('/work_down') > -1) {
- this.redirect(301, '/works/' + lastMatch)
- }
- let categoryList = await this._getWorksCategory()
- let categoryAll = []
- categoryList.forEach(item => {
- if (item.child && item.child.length) {
- item.child.forEach(child => {
- if (child.name && child.name !== '全部') {
- categoryAll.push(child)
- }
- })
- }
- })
- if (lastMatch) {
- // 遍历分类数组
- let selectedCateIdOne = ''
- categoryList.forEach(cateOne => {
- cateOne.child.forEach(cateTwo => {
- if (cateTwo.f_name === lastMatch) {
- selectedCateIdOne = cateOne.f_name
- self.cateNameOne = cateOne.name
- if (cateTwo.name === '全部') {
- self.cateNameTwo = cateOne.name
- } else {
- self.cateNameTwo = cateTwo.name
- }
- }
- })
- })
- if (selectedCateIdOne) {
- this.pagination.selectedCateIdOne = selectedCateIdOne
- this.pagination.cate_id_two.push(lastMatch)
- }
- }
- // 处理完分类信息,再获取数据
- let worksList = await this._getWorksList()
- return {
- root_type,
- categoryList,
- categoryAll,
- worksList, //首次获取的数据
- mobile: this.mobile,
- pagination: this.pagination,
- head: this.dealThisMeta()
- }
- }
- /** 获取作品分类 */
- async _getWorksCategory () {
- let res = await this.$axios.$post('/api/user_works/cate', { root_type: this.root_type })
- let categoryList = []
- if (Number(res.status) === 1) {
- categoryList = res.data || []
- // web 端,为所有二级分类添加 “全部”
- if (!this.mobile) {
- categoryList.forEach(item => {
- if (item.child) {
- let allItem = { f_name: item.f_name, name: '全部' }
- item.child.splice(0, 0, allItem)
- }
- })
- }
- }
- return categoryList
- }
- /** 获取作品列表 */
- async _getWorksList () {
- const data = {
- page: this.pagination.page,
- keywords: this.pagination.keywords,
- cate_id_two: this.pagination.cate_id_two.join(','),
- root_type: this.root_type
- }
- let res = await this.$axios.$post('/api/user_works/workFileList', data)
- let worksList = []
- if (Number(res.status) === 1) {
- worksList = res.data.list || []
- this.pagination.pagesize = Number(res.data.pagesize) || 20
- this.pagination.total = Number(res.data.total) || 0
- if (this.pagination.page * this.pagination.pagesize >= this.pagination.total) {
- this.pagination.noMore = true
- } else {
- this.pagination.noMore = false
- }
- }
- return worksList
- }
- dealThisMeta() {
- let head = {
- title: "",
- keyword: "",
- description: "",
- h1: "",
- canonical: "",
- metaLocation: ""
- }
- if (this.req) {
- const { headers: { host }, url } = this.req
- //拼接canonical
- if (host.indexOf('local') !== -1) {
- head.canonical = 'http://' + host + url
- } else {
- head.canonical = 'https://' + host + url
- }
- }
- if (this.cateNameTwo) {
- // 分类页
- head.title = `${this.cateNameTwo}资源下载-开发技术资源-程序员客栈资源商城`;
- head.keyword = `${this.cateNameTwo}资源下载,${this.cateNameTwo}开发技术资源下载,${this.cateNameTwo}项目源码下载`;
- head.description = `${this.cateNameTwo}开发技术资源,为IT开发人员提供权威的${this.cateNameTwo}学习、${this.cateNameTwo}编程源码、${this.cateNameTwo}电子书、各阶段资料下载等服务.更多下载资源请访问程序员客栈APP或官网资源商城`;
- } else {
- // 列表页,无筛选参数
- head.title = "IT开发资源下载-【程序员客栈资源商城】";
- head.keyword = "IT开发资源下载,开发源码下载";
- head.description = "程序员客栈资源商城是一个提供学习资源、源码、在线学习视频、IT电子书、各类免费软件等下载服务的IT资源大本营,致力于为软件开发者提供知识传播、资源共享、共同学习的优质学习资源平台.";
- }
- return head
- }
- }
|