Commit 32bbf77a authored by 前端-许佳敏's avatar 前端-许佳敏

chore: 优化构建前脚本,接入国际化配置

parent 416ca015
......@@ -14,7 +14,7 @@
"api": "god-ytt",
"testApi": "cross-env PRO_ENV=v2 god-ytt",
"scripts:build": "node scripts/run",
"scripts:build-yxc": "node scripts/run http://yxc-web-demo.shushangyun.com/api",
"scripts:build-test": "cross-env PRO_ENV=v2 node scripts/run",
"start:dev": "cross-env NODE_OPTIONS=--max_old_space_size=4096 umi dev",
"start:analyze": "ANALYZE=1 umi dev",
"clean": "rimraf node_modules",
......
const Axios = require('axios').default;
const deepClone = require('clone')
const { prefixServiceConfig, serviceConfig} = require('./server')
const BACK_GATEWAY = process.env.BACK_GATEWAY
const demoFetch = require('../../demo').fetchConfig
const isDemo = true
const remoteUrl = BACK_GATEWAY || process.argv[2] || 'http://10.0.0.25:8100'
console.log(`\n当前访问的后端url是${remoteUrl}!!!!!!`)
const axios = Axios.create({
baseURL: remoteUrl,
responseType: 'json',
headers: {
"source": "1"
}
})
async function prefixHttp() {
try {
const { data } = await axios(prefixServiceConfig.global)
if (data.code === 1000) {
return data.data
} else {
return {}
}
} catch (error) {
console.log(error)
}
}
// 批量组装接口
async function batchAxiosHttps(requestConfig) {
const asyncHttpQueue = deepClone(serviceConfig)
const httpErrorQueue = []
const serverErrorQueue = []
console.log('\n')
for (const item in serviceConfig) {
if (JSON.stringify(item) !== '{}') {
for (const subItem in serviceConfig[item]) {
try {
axios.interceptors.request.use(config => {
config.headers = Object.assign({}, config.headers, requestConfig.headers)
return config
})
const data = await axios(serviceConfig[item][subItem])
// 当接口出错时 不写入json文件
if (data.data.code === 1000) {
asyncHttpQueue[item][subItem] = data.data.data
} else {
serverErrorQueue.push({ ...asyncHttpQueue[item][subItem], ...data.data })
// 默认置为null
asyncHttpQueue[item][subItem] = null
}
} catch (err) {
console.log(err)
httpErrorQueue.push({ ...serviceConfig[item][subItem], ...err.response.data })
}
}
}
}
if (httpErrorQueue.length > 0) {
console.log('\n网络错误\n')
// 可在此做日志收集
console.log(httpErrorQueue)
}
if (serverErrorQueue.length > 0) {
console.log('\n接口服务错误\n')
// 可在此做日志收集
console.log(serverErrorQueue)
}
if (httpErrorQueue.length > 0 || serverErrorQueue.length > 0) {
// 退出构建
console.log('\n脚本构建失败!!!!!!')
// 终止当前进程
process.exit(1)
}
return isDemo ? Object.assign(asyncHttpQueue, await demoFetch()) : asyncHttpQueue
}
module.exports = {
prefixHttp,
batchAxiosHttps,
}
......@@ -13,25 +13,11 @@
*/
const Axios = require('axios').default;
const deepClone = require('clone')
const demoFetch = require('../../demo').fetchConfig
const isDemo = true
const { prefixHttp, batchAxiosHttps } = require('./batchAxiosHttps')
const SITE_ID = Number(process.env.SITE_ID)
const BACK_GATEWAY = process.env.BACK_GATEWAY
const remoteUrl = BACK_GATEWAY || process.argv[2] || 'http://10.0.0.25:8100'
console.log(`\n当前访问的后端url是${remoteUrl}!!!!!!`)
const axios = Axios.create({
baseURL: remoteUrl,
responseType: 'json',
headers: {
"source": "1"
}
})
if (!SITE_ID) {
console.log('\n构建失败!!!!!!')
......@@ -39,109 +25,16 @@ if (!SITE_ID) {
process.exit(1)
}
const serviceConfig = {
// 用于注册页
userRegister: {
useType: {
url: '/member/menu/register/type',
method: 'get'
}
},
web: {
shopInfo: {
url: '/manage/shop/findShopsBySiteId',
params: {
siteId: SITE_ID,
},
method: 'get'
},
orderMode: {
url: '/system/rule/getOrderMode',
method: 'get'
},
orderType: {
url: '/system/rule/getOrderType',
method: 'get'
},
},
global: {
siteInfo: {
url: '/manage/paas/site/details',
params: {
id: SITE_ID
},
method: 'get'
},
imConfig: {
url: '/manage/online/service/find/config',
method: 'get'
},
},
}
// 批量组装接口
async function batchAxiosHttps() {
const asyncHttpQueue = deepClone(serviceConfig)
// 请求队列的回调数据
// const requestQueueResult = {}
// const serverFn = async (obj) => {
// for (const item in obj) {
// // 当前属性是一个请求方法
// if (obj[item].url && obj[item].method) {
// const requestConfig = deepClone(obj[item])
// const { data } = await axios(requestConfig)
// requestQueueResult[requestConfig.url] = data.data
// obj[item] = data.data
// } else {
// serverFn(obj[item])
// }
// }
// }
// serverFn(asyncHttpQueue)
const httpErrorQueue = []
const serverErrorQueue = []
console.log('\n')
for (const item in serviceConfig) {
if (JSON.stringify(item) !== '{}') {
for (const subItem in serviceConfig[item]) {
try {
const data = await axios(serviceConfig[item][subItem])
// 当接口出错时 不写入json文件
if (data.data.code === 1000) {
asyncHttpQueue[item][subItem] = data.data.data
} else {
serverErrorQueue.push({ ...asyncHttpQueue[item][subItem], ...data.data })
// 默认置为null
asyncHttpQueue[item][subItem] = null
}
} catch (err) {
console.log(err)
httpErrorQueue.push({ ...serviceConfig[item][subItem], ...err.response.data })
}
}
}
}
if (httpErrorQueue.length > 0) {
console.log('\n网络错误\n')
// 可在此做日志收集
console.log(httpErrorQueue)
}
if (serverErrorQueue.length > 0) {
console.log('\n接口服务错误\n')
// 可在此做日志收集
console.log(serverErrorQueue)
}
if (httpErrorQueue.length > 0 || serverErrorQueue.length > 0) {
// 退出构建
console.log('\n脚本构建失败!!!!!!')
// 终止当前进程
process.exit(1)
async function pullConfigHttp() {
const data = await prefixHttp()
const { language = 'zh' } = data
console.log(`当前站点语言为${language}`)
const result = await batchAxiosHttps({
headers: {
'Accept-Language': language
}
return isDemo ? Object.assign(asyncHttpQueue, await demoFetch()) : asyncHttpQueue
})
return result
}
exports.fetchConfig = batchAxiosHttps
exports.fetchConfig = pullConfigHttp
const SITE_ID = Number(process.env.SITE_ID)
/**
* 前置接口 用于后续配置接口的信息获取
*/
const prefixServiceConfig = {
global: {
url: '/manage/paas/site/details',
params: {
id: SITE_ID
},
method: 'get'
}
}
const serviceConfig = {
// 用于注册页
userRegister: {
useType: {
url: '/member/menu/register/type',
method: 'get'
}
},
web: {
shopInfo: {
url: '/manage/shop/findShopsBySiteId',
params: {
siteId: SITE_ID,
},
method: 'get'
},
orderMode: {
url: '/system/rule/getOrderMode',
method: 'get'
},
orderType: {
url: '/system/rule/getOrderType',
method: 'get'
},
},
global: {
siteInfo: {
url: '/manage/paas/site/details',
params: {
id: SITE_ID
},
method: 'get'
},
imConfig: {
url: '/manage/online/service/find/config',
method: 'get'
},
},
}
module.exports = {
serviceConfig,
prefixServiceConfig
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment