| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- import * as path from "path";
- import axios from "axios";
- import {handleAll} from "./until/handle";
- import {apiMap} from "./map/apiMap";
- const devPort = 3000;
- const serverPort = 4201;
- const env = {
- dev: {
- MODE: 'development',
- ENV_API: `http://127.0.0.1:${devPort}` //测试服务器地址
- },
- pro: {
- MODE: 'production',
- ENV_API: `http://szhfy.com.cn:${serverPort}` // 正式服务器地址
- }
- }
- function loadSvgConfig(config, ctx) {
- // 排除 nuxt 原配置的影响,Nuxt 默认有vue-loader,会处理svg,img等
- // 找到匹配.svg的规则,然后将存放svg文件的目录排除
- const svgRule = config.module.rules.find(rule => rule.test.test(".svg"));
- svgRule.exclude = [path.resolve(__dirname, "assets/icons/svg")];
- //添加loader规则
- config.module.rules.push({
- test: /\.svg$/, // 匹配.svg
- include: [path.resolve(__dirname, "assets/icons/svg")], // 将存放svg的目录加入到loader处理目录
- use: [
- { loader: "svg-sprite-loader", options: { symbolId: "icon-[name]" } }
- ]
- });
- }
- /**
- * 获取seo数据
- * @returns {Promise<*[]>}
- */
- async function seoDataLoad(){
- // 静态路由
- let routes = [];
- // 根据生产环境判断baseUrl
- // 请求数据
- let err, productRes, newsRes, solutionRes;
- let baseUrl = process.env.NODE_ENV === 'production' ? env.pro.ENV_API : env.dev.ENV_API;
- let productDataUrl = baseUrl + apiMap.searchProduct.path;
- let solutionDataUrl = baseUrl + apiMap.searchSolution.path;
- let newsDataUrl = baseUrl + apiMap.searchNews.path;
- productDataUrl += `?type=all&p=1&l=1000`;
- solutionDataUrl += `?type=all&p=1&l=1000`;
- newsDataUrl += `?type=all&p=1&l=1000`;
- console.log(`productDataUrl:${productDataUrl}`);
- console.log(`solutionDataUrl:${solutionDataUrl}`);
- console.log(`newsDataUrl:${newsDataUrl}`);
- [err, productRes ,newsRes, solutionRes] =
- await handleAll(
- axios.get(productDataUrl),
- axios.get(solutionDataUrl),
- axios.get(newsDataUrl),
- );
- if(err){
- console.log(err.message);
- console.log(err);
- return routes;
- }else{
- let productData = productRes.data,
- solutionData = solutionRes.data,
- newsData = newsRes.data;
- if(productData.code === 1){
- let productArr = productData.data;
- productArr.forEach(item=>{
- routes.push(
- {
- url: `/product/info?id=${item.id}`,
- changefreq: 'daily',
- priority: 0.9,
- },
- {
- url: `/product/info/${item.type_key}?id=${item.id}`,
- changefreq: 'daily',
- priority: 0.9,
- }
- );
- })
- }
- if(solutionData.code === 1){
- let solutionArr = solutionData.data;
- solutionArr.forEach(item=>{
- routes.push(
- {
- url: `/solution/info?id=${item.id}`,
- changefreq: 'daily',
- priority: 0.8,
- },
- {
- url: `/solution/info/${item.type_key}?id=${item.id}`,
- changefreq: 'daily',
- priority: 0.8,
- }
- );
- })
- }
- if(newsData.code === 1){
- let newsArr = newsData.data;
- newsArr.forEach(item=>{
- routes.push(
- {
- url: `/news/info?id=${item.id}`,
- changefreq: 'daily',
- priority: 0.7,
- },
- {
- url: `/news/info/${item.type_key}?id=${item.id}`,
- changefreq: 'daily',
- priority: 0.7,
- }
- );
- })
- }
- }
- console.log(routes);
- return routes
- }
- let seoOption = {
- hostname: "http://szhfy.com.cn/", // 你的网站地址
- cacheTime: 1000 * 60 * 60 * 24, //一天的更新频率,只在generate:false有用
- gzip: true, //用gzip压缩优化生成的 sitemap.xml 文件
- generate: false,
- exclude: [
- '/manger',
- '/manger/*',
- '/manger/**',
- ], //排除不需要收录的页面,这里的路径是相对于hostname, 例如: exclude: ['/404',...]
- // 每个对象代表一个页面,这是默认的
- defaults: {
- changefred: "always", // 收录变化的时间,always 一直,daily 每天
- lastmod: new Date(), // 生成该页面站点地图时的时间
- priority: 1, // 网页的权重 1是100%最高,一般给最重要的页面,不重要的页面0.7-0.9之间
- },
- }
- export default {
- // Global page headers (https://go.nuxtjs.dev/config-head)
- head: {
- title: '深圳市合方圆科技',
- meta: [
- { charset: 'utf-8' },
- { name: 'viewport', content: 'width=device-width, initial-scale=1' },
- { hid: 'keywords', name: 'keywords', content: '合方圆,深圳合方圆,深圳合方圆科技,合方圆科技,4G低功耗'},
- { hid: 'description', name: 'description', content: '网站描述' },
- ],
- link: [
- { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
- ]
- },
- css: [
- '@/assets/public.css',
- '@/assets/tailwindCom.css',
- ],
- // Auto import components (https://go.nuxtjs.dev/config-components)
- components: true,
- // Modules for dev and build (recommended) (https://go.nuxtjs.dev/config-modules)
- buildModules: [
- '@nuxtjs/tailwindcss'
- ],
- // Modules (https://go.nuxtjs.dev/config-modules)
- modules: [
- // https://go.nuxtjs.dev/axios
- '@nuxtjs/axios',
- '@nuxtjs/sitemap'
- // '@nuxtjs/style-resources',
- ],
- sitemap: [
- {
- path: "/sitemap.xml", //生成的文件路径
- ...seoOption,
- routes: seoDataLoad,
- },
- {
- path: "/sitemap.html", //生成的文件路径
- ...seoOption,
- routes: seoDataLoad,
- }
- ],
- // 加入axios 插件
- plugins: [
- "@plugins/svg-icon.js",
- { src: "@plugins/ckeditor.js", mode: "client" ,ssr: false}
- // {src: '~/plugins/vue-pdf.js', ssr: false}
- ],
- // api中间件
- serverMiddleware: [
- '~/server/index.js'
- ],
- // styleResources: {
- // scss: [
- // '~/assets/_var.scss'
- // ]
- // },
- // Axios module configuration (https://go.nuxtjs.dev/config-axios)
- axios: {
- proxy: true
- },
- proxy: {
- },
- // Build Configuration (https://go.nuxtjs.dev/config-build)
- build: {
- extractCSS: true,
- extend(config, ctx) {
- loadSvgConfig(config,ctx);
- // 合并js文件
- // config.optimization.splitChunks = {
- // chunks: 'all',
- // minChunks: 1,
- // cacheGroups: {
- // vendor: {
- // test: /[\\/]node_modules[\\/]/,
- // name(module) {
- // // get the name. E.g. node_modules/packageName/not/this/part.js
- // // or node_modules/packageName
- // const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
- // // npm package names are URL-safe, but some servers don't like @ symbols
- // return `npm.${packageName.replace('@', '')}`;
- // }
- // }
- // }
- // }
- },
- },
- env : process.env.NODE_ENV === 'production' ? env.pro : env.dev,
- server: {
- // 根据环境变量判断当前运行环境 nuxt start 使用production环境
- port: process.env.NODE_ENV === 'production' ? serverPort : devPort, // default: 3000
- host: '0.0.0.0', // default: localhost
- },
- generate: {
- routes:
- [
- '/product/m2m',
- '/product/aiCam',
- '/product/sm',
- '/product/low',
- '/product/cam',
- '/solution/sol',
- '/solution/acs',
- '/solution/epower',
- '/news/com',
- '/news/pa',
- '/news/in',
- ]
- }
- }
|