GitBucket
4.6.0
Toggle navigation
Sign in
Files
Branches
1
Tags
Issues
Pull Requests
Labels
Milestones
Wiki
08335
/
hivui-platform-template
hivui平台项目模板
Browse code
res.end("pcform error!");
master
1 parent
f8ee1af
commit
63d98042d3b8289d2e32481958ea99367d57ea41
12112
authored
on 9 Sep 2021
Showing
1 changed file
vite-plugin/vite-plugin-func.js
Ignore Space
Show notes
View
vite-plugin/vite-plugin-func.js
var querystring = require('querystring'); import Cookies from 'js-cookie' import { builtinModules } from 'module'; var url = require('url'); const fs = require('fs') var https = require('https'); var request = require('request'); var ejs = require('ejs'); function myPlugin(rawOptions) { const options = { isProduction: process.env.NODE_ENV === 'production', ...rawOptions, root: process.cwd(), }; let postData; let config; return { name: 'vite-plugin-func', // 必须的,将会显示在 warning 和 error 中 configResolved(resolvedConfig) { // 存储最终解析的配置 config = resolvedConfig }, configureServer(server) { server.middlewares.use((req, res, next) => { console.log(new Date(), 'req.url:', req.url) if (req.url.indexOf(".flow") != -1) { let url = req.url; postData = ''; let fmodelpath = url.split("?")[0] var post_data = { fmodelpath: fmodelpath } let path = process.env.VITE_APP_BASE_API + process.env.VITE_APP_SERVER + "/flow/open?pn="; let pn = url.split("/")[1]; const TokenKey = 'EAP-Token'; req.on('data', (chunk) => { post += chunk; postData = querystring.parse(post); // console.log(new Date(), 'PostData:', postData); }); let token = Cookies.get(TokenKey) || "ef5d714e-d9ef-4011-ac15-80b477d312d2"; request({ url: path + pn, method: "POST", json: true, headers: { "token": token, "Authorization": "Bearer " + token, "Content-Type": "application/json;charset=UTF-8" }, body: post_data }, function (error, response, body) { if (!error && response.statusCode == 200) { // console.log(body) // 请求成功的处理逻辑 let pcform; if (body && body.outParameter) { pcform = body.outParameter.task.pcform; } if (!pcform) { res.writeHead(500, { "Content-Type": "application/json;charset=UTF-8" }); res.end(JSON.stringify(body)); return; } // var fullPath = "eaptpl/12112/mokuai/shitumulu/biaodan/dtv/1.0.0/desktop/index.html"; let content; try { content = fs.readFileSync(pcform, { encoding: 'utf8' }) } catch (e) { res.writeHead(404, { "Content-Type": "application/json;charset=UTF-8" }); res.end("pcform error!"); return; } const statusCode = 200; if (!body) throw new Error(`No body text found for the ${statusCode} status code`); var html = ejs.render(content, rawOptions) let postDataStr = JSON.stringify(postData || {}); let varName = options.varName || 'viteRequestData'; let funcName = new Date().valueOf(); let queryScript = ` function _viteGetQuery${funcName}() { var url = window.location.search; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]); } } return theRequest; }` let titleStr = html.match(/<head(.*?)>/g)[0] || ""; html = html.replace( /<head(.*?)>/g, `${titleStr}<script>${queryScript};window.${varName} = Object.assign(_viteGetQuery${funcName}(),${postDataStr}) </script>` ) res.writeHead(statusCode, { 'Content-Length': html.length, 'Content-Type': 'html' }); res.write(html) res.end(); } else { console.log(body) } }, (msg) => { debugger; }); return; } else { next(); } }) }, transformIndexHtml(html) { console.log('config.command:', config.command) if (config.command === 'build') { return html; } let postDataStr = JSON.stringify(postData || {}); let varName = options.varName || 'viteRequestData'; let funcName = new Date().valueOf(); let queryScript = ` function _viteGetQuery${funcName}() { var url = window.location.search; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]); } } return theRequest; }` postData = ''; let titleStr = html.match(/<head(.*?)>/g)[0] || ""; return html.replace( /<head(.*?)>/g, `${titleStr}<script>${queryScript};window.${varName} = Object.assign(_viteGetQuery${funcName}(),${postDataStr}) </script>` ) } } } export default myPlugin;
var querystring = require('querystring'); import Cookies from 'js-cookie' var url = require('url'); const fs = require('fs') var https = require('https'); var request = require('request'); var ejs = require('ejs'); function myPlugin(rawOptions) { const options = { isProduction: process.env.NODE_ENV === 'production', ...rawOptions, root: process.cwd(), }; let postData; let config; return { name: 'vite-plugin-func', // 必须的,将会显示在 warning 和 error 中 configResolved(resolvedConfig) { // 存储最终解析的配置 config = resolvedConfig }, configureServer(server) { server.middlewares.use((req, res, next) => { console.log(new Date(), 'req.url:', req.url) if (req.url.indexOf(".flow") != -1) { let url = req.url; postData = ''; let fmodelpath = url.split("?")[0] var post_data = { fmodelpath: fmodelpath } let path = process.env.VITE_APP_BASE_API + process.env.VITE_APP_SERVER + "/flow/open?pn="; let pn = url.split("/")[1]; const TokenKey = 'EAP-Token'; req.on('data', (chunk) => { post += chunk; postData = querystring.parse(post); // console.log(new Date(), 'PostData:', postData); }); let token = Cookies.get(TokenKey) || "ef5d714e-d9ef-4011-ac15-80b477d312d2"; request({ url: path + pn, method: "POST", json: true, headers: { "token": token, "Authorization": "Bearer " + token, "Content-Type": "application/json;charset=UTF-8" }, body: post_data }, function (error, response, body) { if (!error && response.statusCode == 200) { // console.log(body) // 请求成功的处理逻辑 let pcform = body.outParameter.task.pcform; // var fullPath = "eaptpl/12112/mokuai/shitumulu/biaodan/dtv/1.0.0/desktop/index.html"; let content; try { content = fs.readFileSync(pcform, { encoding: 'utf8' }) } catch (e) { } const statusCode = 200; if (!body) throw new Error(`No body text found for the ${statusCode} status code`); var html = ejs.render(content, rawOptions) let postDataStr = JSON.stringify(postData || {}); let varName = options.varName || 'viteRequestData'; let funcName = new Date().valueOf(); let queryScript = ` function _viteGetQuery${funcName}() { var url = window.location.search; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]); } } return theRequest; }` let titleStr = html.match(/<head(.*?)>/g)[0] || ""; html = html.replace( /<head(.*?)>/g, `${titleStr}<script>${queryScript};window.${varName} = Object.assign(_viteGetQuery${funcName}(),${postDataStr}) </script>` ) res.writeHead(statusCode, { 'Content-Length': html.length, 'Content-Type': 'html' }); res.write(html) res.end(); } else { console.log(body) } }); return; } else { next(); } }) }, transformIndexHtml(html) { console.log('config.command:', config.command) if (config.command === 'build') { return html; } let postDataStr = JSON.stringify(postData || {}); let varName = options.varName || 'viteRequestData'; let funcName = new Date().valueOf(); let queryScript = ` function _viteGetQuery${funcName}() { var url = window.location.search; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]); } } return theRequest; }` postData = ''; let titleStr = html.match(/<head(.*?)>/g)[0] || ""; return html.replace( /<head(.*?)>/g, `${titleStr}<script>${queryScript};window.${varName} = Object.assign(_viteGetQuery${funcName}(),${postDataStr}) </script>` ) } } } export default myPlugin;
Show line notes below