Commit 4e05130b by golton_gao

update:优化代码

parent a240c441
...@@ -27,7 +27,8 @@ var webpackConfig = merge(baseWebpackConfig, { ...@@ -27,7 +27,8 @@ var webpackConfig = merge(baseWebpackConfig, {
plugins: [ plugins: [
// http://vuejs.github.io/vue-loader/en/workflow/production.html // http://vuejs.github.io/vue-loader/en/workflow/production.html
new webpack.DefinePlugin({ new webpack.DefinePlugin({
'process.env': env 'process.env': env,
'process.env.PACK_ENV': JSON.stringify(process.env.PACK_ENV)
}), }),
new webpack.optimize.UglifyJsPlugin({ new webpack.optimize.UglifyJsPlugin({
compress: { compress: {
......
...@@ -27,7 +27,8 @@ var webpackConfig = merge(baseWebpackConfig, { ...@@ -27,7 +27,8 @@ var webpackConfig = merge(baseWebpackConfig, {
}, },
plugins: [ plugins: [
new webpack.DefinePlugin({ new webpack.DefinePlugin({
'process.env': env 'process.env': env,
'process.env.PACK_ENV': JSON.stringify(process.env.PACK_ENV)
}), }),
new webpack.optimize.UglifyJsPlugin({ new webpack.optimize.UglifyJsPlugin({
compress: { compress: {
......
...@@ -7,7 +7,7 @@ module.exports = { ...@@ -7,7 +7,7 @@ module.exports = {
index: path.resolve(__dirname, "../www/index.html"), index: path.resolve(__dirname, "../www/index.html"),
assetsRoot: path.resolve(__dirname, "../www"), assetsRoot: path.resolve(__dirname, "../www"),
assetsSubDirectory: "static", assetsSubDirectory: "static",
assetsPublicPath: "", assetsPublicPath: "/",
productionSourceMap: true, productionSourceMap: true,
// Gzip off by default as many popular static hosts such as // Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you. // Surge or Netlify already gzip all static assets for you.
...@@ -26,7 +26,7 @@ module.exports = { ...@@ -26,7 +26,7 @@ module.exports = {
port: 8080, port: 8080,
autoOpenBrowser: true, autoOpenBrowser: true,
assetsSubDirectory: "static", assetsSubDirectory: "static",
assetsPublicPath: "/", assetsPublicPath: "http://192.168.1.11/",
proxyTable: { proxyTable: {
"/api": { "/api": {
target: "http://csmtest.cndrealty.com/csm/api/", //开发环境 target: "http://csmtest.cndrealty.com/csm/api/", //开发环境
......
...@@ -8,8 +8,8 @@ ...@@ -8,8 +8,8 @@
"dev": "node build/dev-server.js", "dev": "node build/dev-server.js",
"build": "node build/build.js", "build": "node build/build.js",
"lint": "eslint --ext .js,.vue src", "lint": "eslint --ext .js,.vue src",
"build:prod": "node build/vt-build.js", "build:prod": "cross-env PACK_ENV=prod && build/vt-build.js",
"build:zip": "node build/vt-build.js" "build:test": "cross-env PACK_ENV=test && node build/vt-build.js"
}, },
"dependencies": { "dependencies": {
"amfe-flexible": "^2.2.1", "amfe-flexible": "^2.2.1",
......
import axios from 'axios'; import axios from 'axios';
// import { getStore } from '@/utils';
// import env from '@/api/env';
// import base64 from "./base64";
const baseURL = const baseURL =
process.env.NODE_ENV === 'development' process.env.PACK_ENV === 'prod'
? '' ? 'https://csm.cndservice.com/csm/api'
: 'https://csmtest.cndservice.com/csm/api'; : 'https://csmtest.cndservice.com/csm/api';
// const baseURL =
// process.env.NODE_ENV === 'development'
// ? '/api'
// : 'https://csm.cndservice.com/csm/api';
const axiosConfig = { const axiosConfig = {
baseURL, baseURL,
timeout: 60000, timeout: 60000,
...@@ -21,12 +14,14 @@ service.defaults.headers.post['Content-Type'] = ...@@ -21,12 +14,14 @@ service.defaults.headers.post['Content-Type'] =
'application/json;charset=utf-8'; 'application/json;charset=utf-8';
// let loadingInstance = null; // let loadingInstance = null;
const authorization =
process.env.PACK_ENV === 'prod'
? 'Basic c21zc2VydmljZXVzZXI6ZmRjVXNlcjY1NCNAIQ=='
: 'Basic c21zc2VydmljZXVzZXI6amZmY0AxMjM=';
// 请求拦截器 // 请求拦截器
service.interceptors.request.use( service.interceptors.request.use(
config => { config => {
config.headers.Authorization = 'Basic c21zc2VydmljZXVzZXI6amZmY0AxMjM='; // 测试环境 config.headers.Authorization = authorization
// config.headers.Authorization =
// 'Basic c21zc2VydmljZXVzZXI6ZmRjVXNlcjY1NCNAIQ=='; // 正式环境
return config; return config;
}, },
error => { error => {
......
...@@ -78,17 +78,6 @@ import Tree from './components/Tree/Index.vue'; ...@@ -78,17 +78,6 @@ import Tree from './components/Tree/Index.vue';
Vue.component('Tree', Tree) Vue.component('Tree', Tree)
let VMT; let VMT;
if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'provt') { if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'provt') {
/*
document.addEventListener('deviceready', async () => {
console.log(navigator.camera);
await getUserUID();
VMT = new Vue({
el: '#app',
router,
store,
render: h => h(App)
});
});*/
window.qing.ready(async () => { window.qing.ready(async () => {
await getUserUID(); await getUserUID();
VMT = new Vue({ VMT = new Vue({
......
...@@ -12,6 +12,10 @@ function isObject (value) { ...@@ -12,6 +12,10 @@ function isObject (value) {
return value !== null && typeof value === 'object' return value !== null && typeof value === 'object'
} }
function isString (fd) {
return Object.prototype.toString.call(fd) === '[object String]'
}
function isFormData (fd) { function isFormData (fd) {
return Object.prototype.toString.call(fd) === '[object FormData]' return Object.prototype.toString.call(fd) === '[object FormData]'
} }
...@@ -35,7 +39,7 @@ function params2object (url) { ...@@ -35,7 +39,7 @@ function params2object (url) {
.forEach((n) => { .forEach((n) => {
n = n.split('=') n = n.split('=')
if (n.length === 2) { if (n.length === 2) {
params[n[0]] = n[1] params[n[0]] = decodeURIComponent(n[1])
} }
}) })
} }
...@@ -43,6 +47,7 @@ function params2object (url) { ...@@ -43,6 +47,7 @@ function params2object (url) {
} }
function request (opt) { function request (opt) {
let serializer = opt.serializer || 'form'
opt = opt || {} opt = opt || {}
if (!opt.url) { if (!opt.url) {
return Promise.reject('interface url is required!') return Promise.reject('interface url is required!')
...@@ -78,35 +83,52 @@ function request (opt) { ...@@ -78,35 +83,52 @@ function request (opt) {
// post非json即是form。 // post非json即是form。
if (opt.method.toLowerCase() === 'post') { if (opt.method.toLowerCase() === 'post') {
var data = opt.data var data = opt.data
if (isFormData(data)) { const headers = opt.headers
opt.headers = Object.assign(opt.headers || {}, { const contentType = headers['content-type'] || headers['Content-Type'] || headers['CONTENT-TYPE'] || ''
'Content-Type': 'application/x-www-form-urlencoded' if (contentType.indexOf('application/json') !== -1) {
}) serializer = 'json'
}
if ((serializer === 'form') && isFormData(data)) {
opt.data = formData2obj(data) opt.data = formData2obj(data)
} else if (isObject(data)) { } else if (isString(data)) {
opt.headers = Object.assign(opt.headers || {}, { if (serializer === 'json') {
'Content-Type': 'application/json;charset=UTF-8' try {
}) opt.data = JSON.parse(data)
} catch (error) {
opt.data = {}
}
} else {
opt.data = params2object(data)
}
} }
} }
// 处理header // 处理header
opt.headers = opt.headers || {} opt.headers = opt.headers || {}
const headers = {}
// headers字段值必须是字符串型 // headers字段值必须是字符串型
Object.keys(opt.headers).forEach(function (key) { Object.keys(opt.headers).forEach(function (key) {
var val = opt.headers[key] var val = opt.headers[key]
if (val) {
var type = typeof val var type = typeof val
if (type === 'undefined') { if (type !== 'string') {
delete opt.headers[key] val = val + ''
} else if (type !== 'string') { }
opt.headers[key] = val + '' // 因content-type可能存在大小写不规范,这里先过滤掉,后面根据serializer统一添加
var _key = key.toLowerCase()
if (_key !== 'content-type') {
headers[_key] = val
}
} }
}) })
// 根据serializer统一添加content-type
headers['content-type'] = serializer === 'json' ? 'application/json' : 'application/x-www-form-urlencoded'
opt.headers = headers
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
window.qing.call('request', { window.qing.call('request', {
url: opt.url, url: opt.url,
method: opt.method, method: opt.method.toUpperCase(),
headers: opt.headers, headers: opt.headers,
header: opt.headers, header: opt.headers,
data: opt.data || {}, data: opt.data || {},
......
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 sign in to comment