nuxt.config.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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://127.0.0.1:${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. let productTypes = {}
  98. productArr.forEach(item=>{
  99. productTypes[item.type_key] = item.type_key
  100. routes.push(
  101. {
  102. url: `/product/info/${item.type_key}?id=${item.id}`,
  103. changefreq: 'daily',
  104. priority: 0.8,
  105. }
  106. );
  107. })
  108. for (let type_key in productTypes) {
  109. routes.push(
  110. {
  111. url: `/product/${type_key}`,
  112. changefreq: 'daily',
  113. priority: 0.9,
  114. }
  115. );
  116. }
  117. }
  118. if(solutionData.code === 1){
  119. let solutionArr = solutionData.data;
  120. let solutionTypes = {}
  121. solutionArr.forEach(item=>{
  122. solutionTypes[item.type_key] = item.type_key
  123. routes.push(
  124. {
  125. url: `/solution/info/${item.type_key}?id=${item.id}`,
  126. changefreq: 'daily',
  127. priority: 0.8,
  128. }
  129. );
  130. })
  131. for (let type_key in solutionTypes) {
  132. routes.push(
  133. {
  134. url: `/solution/${type_key}`,
  135. changefreq: 'daily',
  136. priority: 0.9,
  137. }
  138. );
  139. }
  140. }
  141. if(newsData.code === 1){
  142. let newsArr = newsData.data;
  143. let newsTypes = {}
  144. newsArr.forEach(item=>{
  145. newsTypes[item.type_key] = item.type_key
  146. routes.push(
  147. {
  148. url: `/news/info/${item.type_key}?id=${item.id}`,
  149. changefreq: 'daily',
  150. priority: 0.7,
  151. }
  152. );
  153. })
  154. for (let type_key in newsTypes) {
  155. routes.push(
  156. {
  157. url: `/news/${type_key}`,
  158. changefreq: 'daily',
  159. priority: 0.9,
  160. }
  161. );
  162. }
  163. }
  164. }
  165. console.log(routes);
  166. return routes
  167. }
  168. let seoOption = {
  169. hostname: "http://szhfy.com.cn/", // 你的网站地址
  170. cacheTime: 1000 * 60 * 60 * 24, //一天的更新频率,只在generate:false有用
  171. gzip: true, //用gzip压缩优化生成的 sitemap.xml 文件
  172. generate: false,
  173. exclude: [
  174. '/manger',
  175. '/manger/*',
  176. '/manger/**',
  177. ], //排除不需要收录的页面,这里的路径是相对于hostname, 例如: exclude: ['/404',...]
  178. // 每个对象代表一个页面,这是默认的
  179. defaults: {
  180. changefred: "always", // 收录变化的时间,always 一直,daily 每天
  181. lastmod: new Date(), // 生成该页面站点地图时的时间
  182. priority: 1, // 网页的权重 1是100%最高,一般给最重要的页面,不重要的页面0.7-0.9之间
  183. },
  184. }
  185. export default {
  186. // Global page headers (https://go.nuxtjs.dev/config-head)
  187. alias: {
  188. '@': path.resolve(__dirname),
  189. '~': path.resolve(__dirname),
  190. },
  191. head: {
  192. title: '深圳市合方圆科技',
  193. meta: [
  194. { charset: 'utf-8' },
  195. { name: 'viewport', content: 'width=device-width, initial-scale=1' },
  196. { hid: 'keywords', name: 'keywords', content: '合方圆,深圳合方圆,深圳合方圆科技,合方圆科技,4G低功耗'},
  197. { hid: 'description', name: 'description', content: '网站描述' },
  198. ],
  199. link: [
  200. { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' },
  201. ]
  202. },
  203. css: [
  204. '@/assets/public.css',
  205. '@/assets/tailwindCom.css',
  206. ],
  207. // Auto import components (https://go.nuxtjs.dev/config-components)
  208. components: true,
  209. // Modules for dev and build (recommended) (https://go.nuxtjs.dev/config-modules)
  210. buildModules: [
  211. '@nuxtjs/tailwindcss'
  212. ],
  213. // Modules (https://go.nuxtjs.dev/config-modules)
  214. modules: [
  215. // https://go.nuxtjs.dev/axios
  216. '@nuxtjs/axios',
  217. '@nuxtjs/sitemap'
  218. // '@nuxtjs/style-resources',
  219. ],
  220. sitemap: [
  221. {
  222. hostname: "hofuniot.cn",
  223. path: '/sitemap.xml',
  224. exclude:[
  225. "/news/info",
  226. "/solution/info",
  227. "/product/info"
  228. ],
  229. cacheTime: 1000 * 60 * 60 * 24,
  230. gzip: true,
  231. ...seoOption,
  232. routes: seoDataLoad,
  233. }
  234. ],
  235. // 加入axios 插件
  236. plugins: [
  237. "@plugins/svg-icon.js",
  238. { src: "@plugins/ckeditor.js", mode: "client" ,ssr: false},
  239. { src: "@plugins/vue-directive.js", mode: "client" ,ssr: false},
  240. // {src: '~/plugins/vue-pdf.js', ssr: false}
  241. ],
  242. // api中间件
  243. serverMiddleware: [
  244. '~/server/index.js'
  245. ],
  246. // styleResources: {
  247. // scss: [
  248. // '~/assets/_var.scss'
  249. // ]
  250. // },
  251. // Axios module configuration (https://go.nuxtjs.dev/config-axios)
  252. axios: {
  253. proxy: true
  254. },
  255. proxy: {
  256. },
  257. // Build Configuration (https://go.nuxtjs.dev/config-build)
  258. build: {
  259. extractCSS: true,
  260. extend(config, ctx) {
  261. loadSvgConfig(config,ctx);
  262. // 合并js文件
  263. // js文件转为es5
  264. },
  265. },
  266. env : process.env.NODE_ENV === 'production' ? env.pro : env.dev,
  267. server: {
  268. // 根据环境变量判断当前运行环境 nuxt start 使用production环境
  269. port: process.env.NODE_ENV === 'production' ? serverPort : devPort, // default: 3000
  270. host: '0.0.0.0', // default: localhost
  271. },
  272. generate: {
  273. routes:
  274. [
  275. '/product/m2m',
  276. '/product/aiCam',
  277. '/product/sm',
  278. '/product/low',
  279. '/product/cam',
  280. '/solution/sol',
  281. '/solution/acs',
  282. '/solution/epower',
  283. '/news/com',
  284. '/news/pa',
  285. '/news/in',
  286. ]
  287. }
  288. }