nuxt.config.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import * as path from "path";
  2. import axios from "axios";
  3. import {handleAll, handle} from "./until/handle";
  4. import {apiMap} from "./map/apiMap";
  5. import d_news from "./server/database/d_news";
  6. import d_product from "./server/database/d_product";
  7. const devPort = 3000;
  8. const serverPort = 4201;
  9. const env = {
  10. dev: {
  11. MODE: 'development',
  12. ENV_API: `http://127.0.0.1:${devPort}` //测试服务器地址
  13. },
  14. pro: {
  15. MODE: 'production',
  16. ENV_API: `http://szhfy.com.cn:${serverPort}` // 正式服务器地址
  17. }
  18. }
  19. async function loadProductTypes(baseUrl){
  20. let productTypes = [];
  21. let [err, res] = await handle(d_product.loadTypes())
  22. if(err){
  23. console.log(err);
  24. }
  25. // typekey
  26. res.forEach(item=>{
  27. productTypes.push(`${baseUrl}/${item.typeKey}`)
  28. });
  29. return productTypes;
  30. }
  31. async function loadNewsTypes(solutionUrl, newsUrl){
  32. let pages = [];
  33. let [err, res] = await handle(d_news.loadTypes())
  34. if(err){
  35. console.log(err);
  36. }
  37. // typekey
  38. res.forEach(item=>{
  39. if(item.parent_type == 1){
  40. pages.push(`${solutionUrl}/${item.typeKey}`)
  41. }else{
  42. pages.push(`${newsUrl}/${item.typeKey}`)
  43. }
  44. });
  45. return pages;
  46. }
  47. function loadSvgConfig(config, ctx) {
  48. // 排除 nuxt 原配置的影响,Nuxt 默认有vue-loader,会处理svg,img等
  49. // 找到匹配.svg的规则,然后将存放svg文件的目录排除
  50. const svgRule = config.module.rules.find(rule => rule.test.test(".svg"));
  51. svgRule.exclude = [path.resolve(__dirname, "assets/icons/svg")];
  52. //添加loader规则
  53. config.module.rules.push({
  54. test: /\.svg$/, // 匹配.svg
  55. include: [path.resolve(__dirname, "assets/icons/svg")], // 将存放svg的目录加入到loader处理目录
  56. use: [
  57. { loader: "svg-sprite-loader", options: { symbolId: "icon-[name]" } }
  58. ]
  59. });
  60. }
  61. /**
  62. * 获取seo数据
  63. * @returns {Promise<*[]>}
  64. */
  65. async function seoDataLoad(){
  66. // 静态路由
  67. let routes = [];
  68. // 根据生产环境判断baseUrl
  69. // 请求数据
  70. let err, productRes, newsRes, solutionRes;
  71. let baseUrl = process.env.NODE_ENV === 'production' ? env.pro.ENV_API : env.dev.ENV_API;
  72. let productDataUrl = baseUrl + apiMap.searchProduct.path;
  73. let solutionDataUrl = baseUrl + apiMap.searchSolution.path;
  74. let newsDataUrl = baseUrl + apiMap.searchNews.path;
  75. productDataUrl += `?type=all&p=1&l=1000`;
  76. solutionDataUrl += `?type=all&p=1&l=1000`;
  77. newsDataUrl += `?type=all&p=1&l=1000`;
  78. console.log(`productDataUrl:${productDataUrl}`);
  79. console.log(`solutionDataUrl:${solutionDataUrl}`);
  80. console.log(`newsDataUrl:${newsDataUrl}`);
  81. [err, productRes ,newsRes, solutionRes] =
  82. await handleAll(
  83. axios.get(productDataUrl),
  84. axios.get(solutionDataUrl),
  85. axios.get(newsDataUrl),
  86. );
  87. if(err){
  88. console.log(err.message);
  89. console.log(err);
  90. return routes;
  91. }else{
  92. let productData = productRes.data,
  93. solutionData = solutionRes.data,
  94. newsData = newsRes.data;
  95. if(productData.code === 1){
  96. let productArr = productData.data;
  97. productArr.forEach(item=>{
  98. routes.push(
  99. {
  100. url: `/product/info?id=${item.id}`,
  101. changefreq: 'daily',
  102. priority: 0.9,
  103. },
  104. {
  105. url: `/product/info/${item.type_key}?id=${item.id}`,
  106. changefreq: 'daily',
  107. priority: 0.9,
  108. }
  109. );
  110. })
  111. }
  112. if(solutionData.code === 1){
  113. let solutionArr = solutionData.data;
  114. solutionArr.forEach(item=>{
  115. routes.push(
  116. {
  117. url: `/solution/info?id=${item.id}`,
  118. changefreq: 'daily',
  119. priority: 0.8,
  120. },
  121. {
  122. url: `/solution/info/${item.type_key}?id=${item.id}`,
  123. changefreq: 'daily',
  124. priority: 0.8,
  125. }
  126. );
  127. })
  128. }
  129. if(newsData.code === 1){
  130. let newsArr = newsData.data;
  131. newsArr.forEach(item=>{
  132. routes.push(
  133. {
  134. url: `/news/info?id=${item.id}`,
  135. changefreq: 'daily',
  136. priority: 0.7,
  137. },
  138. {
  139. url: `/news/info/${item.type_key}?id=${item.id}`,
  140. changefreq: 'daily',
  141. priority: 0.7,
  142. }
  143. );
  144. })
  145. }
  146. }
  147. console.log(routes);
  148. return routes
  149. }
  150. let seoOption = {
  151. hostname: "http://szhfy.com.cn/", // 你的网站地址
  152. cacheTime: 1000 * 60 * 60 * 24, //一天的更新频率,只在generate:false有用
  153. gzip: true, //用gzip压缩优化生成的 sitemap.xml 文件
  154. generate: false,
  155. exclude: [
  156. '/manger',
  157. '/manger/*',
  158. '/manger/**',
  159. ], //排除不需要收录的页面,这里的路径是相对于hostname, 例如: exclude: ['/404',...]
  160. // 每个对象代表一个页面,这是默认的
  161. defaults: {
  162. changefred: "always", // 收录变化的时间,always 一直,daily 每天
  163. lastmod: new Date(), // 生成该页面站点地图时的时间
  164. priority: 1, // 网页的权重 1是100%最高,一般给最重要的页面,不重要的页面0.7-0.9之间
  165. },
  166. }
  167. export default {
  168. // Global page headers (https://go.nuxtjs.dev/config-head)
  169. alias: {
  170. '@': path.resolve(__dirname),
  171. '~': path.resolve(__dirname),
  172. },
  173. head: {
  174. title: '深圳市合方圆科技',
  175. meta: [
  176. { charset: 'utf-8' },
  177. { name: 'viewport', content: 'width=device-width, initial-scale=1' },
  178. { hid: 'keywords', name: 'keywords', content: '合方圆,深圳合方圆,深圳合方圆科技,合方圆科技,4G低功耗'},
  179. { hid: 'description', name: 'description', content: '网站描述' },
  180. ],
  181. link: [
  182. { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
  183. ]
  184. },
  185. css: [
  186. '@/assets/public.css',
  187. '@/assets/tailwindCom.css',
  188. ],
  189. // Auto import components (https://go.nuxtjs.dev/config-components)
  190. components: true,
  191. // Modules for dev and build (recommended) (https://go.nuxtjs.dev/config-modules)
  192. buildModules: [
  193. '@nuxtjs/tailwindcss'
  194. ],
  195. // Modules (https://go.nuxtjs.dev/config-modules)
  196. modules: [
  197. // https://go.nuxtjs.dev/axios
  198. '@nuxtjs/axios',
  199. '@nuxtjs/sitemap'
  200. // '@nuxtjs/style-resources',
  201. ],
  202. sitemap: [
  203. {
  204. path: "/sitemap.xml", //生成的文件路径
  205. ...seoOption,
  206. routes: seoDataLoad,
  207. },
  208. {
  209. path: "/sitemap.html", //生成的文件路径
  210. ...seoOption,
  211. routes: seoDataLoad,
  212. }
  213. ],
  214. // 加入axios 插件
  215. plugins: [
  216. "@plugins/svg-icon.js",
  217. { src: "@plugins/ckeditor.js", mode: "client" ,ssr: false},
  218. { src: "@plugins/vue-directive.js", mode: "client" ,ssr: false},
  219. // {src: '~/plugins/vue-pdf.js', ssr: false}
  220. ],
  221. // api中间件
  222. serverMiddleware: [
  223. '~/server/index.js'
  224. ],
  225. // styleResources: {
  226. // scss: [
  227. // '~/assets/_var.scss'
  228. // ]
  229. // },
  230. // Axios module configuration (https://go.nuxtjs.dev/config-axios)
  231. axios: {
  232. proxy: true
  233. },
  234. proxy: {
  235. },
  236. // Build Configuration (https://go.nuxtjs.dev/config-build)
  237. build: {
  238. extractCSS: true,
  239. extend(config, ctx) {
  240. loadSvgConfig(config,ctx);
  241. // 合并js文件
  242. // js文件转为es5
  243. },
  244. },
  245. env : process.env.NODE_ENV === 'production' ? env.pro : env.dev,
  246. server: {
  247. // 根据环境变量判断当前运行环境 nuxt start 使用production环境
  248. port: process.env.NODE_ENV === 'production' ? serverPort : devPort, // default: 3000
  249. host: '0.0.0.0', // default: localhost
  250. },
  251. generate: {
  252. routes:
  253. [
  254. '/product/m2m',
  255. '/product/aiCam',
  256. '/product/sm',
  257. '/product/low',
  258. '/product/cam',
  259. '/solution/sol',
  260. '/solution/acs',
  261. '/solution/epower',
  262. '/news/com',
  263. '/news/pa',
  264. '/news/in',
  265. ]
  266. }
  267. }