Commit 80c27c51 by liangjianmin

js

parent db4449d6
import axios from 'axios'; import axios from 'axios';
import qs from 'qs'; import qs from 'qs';
import Util from "../tool";
/** /**
...@@ -25,14 +26,20 @@ switch (envs) { ...@@ -25,14 +26,20 @@ switch (envs) {
NODE_ENVS = '/'; NODE_ENVS = '/';
break; break;
} }
export const http = (method, url, param) => { export const http = (method, url, param) => {
param = param && typeof param === 'object' ? param : {}; param = param && typeof param === 'object' ? param : {};
let token = Util.getCookie('token') || '';
const config = { const config = {
url: `${NODE_ENVS}${url}`, url: `${NODE_ENVS}${url}`,
method: method, method: method,
transformRequest: [function (param) { transformRequest: [function (param) {
return qs.stringify(param); return qs.stringify(param);
}] }],
headers: {
'Authorization': 'Bearer ' + token
}
}; };
// post请求时需要设定Content-Type // post请求时需要设定Content-Type
......
...@@ -6,6 +6,7 @@ import directive from './directive' ...@@ -6,6 +6,7 @@ import directive from './directive'
import {http} from './ajax/index.js'; import {http} from './ajax/index.js';
import axios from 'axios' import axios from 'axios'
import {Message} from 'element-ui'; import {Message} from 'element-ui';
import Util from "./tool";
//本地环境开启提示信息 //本地环境开启提示信息
Vue.config.productionTip = false; Vue.config.productionTip = false;
...@@ -14,6 +15,7 @@ Vue.config.productionTip = false; ...@@ -14,6 +15,7 @@ Vue.config.productionTip = false;
import '@/assets/css/public/common.min.css' import '@/assets/css/public/common.min.css'
import '@/assets/css/font/iconfont.css' import '@/assets/css/font/iconfont.css'
//加载过滤器 //加载过滤器
Object.keys(filters).forEach(key => Vue.filter(key, filters[key])); Object.keys(filters).forEach(key => Vue.filter(key, filters[key]));
...@@ -57,7 +59,6 @@ router.afterEach((to, from, next) => { ...@@ -57,7 +59,6 @@ router.afterEach((to, from, next) => {
} }
}); });
axios.interceptors.response.use(res => { axios.interceptors.response.use(res => {
return res; return res;
}, error => { }, error => {
...@@ -65,10 +66,25 @@ axios.interceptors.response.use(res => { ...@@ -65,10 +66,25 @@ axios.interceptors.response.use(res => {
return Promise.reject(new Error(error)) return Promise.reject(new Error(error))
}) })
//挂载到VUE原型上封装后的http请求 //挂载到VUE原型上封装后的http请求
Vue.prototype.$http = http; Vue.prototype.$http = http;
//路由页面回跳处理 mate里面参数 back:true 开启登录态回跳
router.beforeEach((to, from, next) => {
http('get', "/api/user/getuserinfo", {
}).then(data => {
let res = data.data;
if (res.err_code === 101) {
window.location.href = '/#/login'
}
}).catch(err => {
console.log(err.message);
})
next();
});
new Vue({ new Vue({
router, router,
render: (h) => h(App), render: (h) => h(App),
......
export default { export default {
/**
sayHellow(){ * 设置cookie
alert("hello") * @param name
return true; * @param value
* @param iDay
*/
setCookie: function (name, value, time, domain) {
domain = domain ? ";domain=" + domain : "";
var Days = time;
var exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + encodeURIComponent(value) + ";expires=" + exp.toGMTString() + ";path=/" + domain;
return true;
},
/**
* 获取cookie
* @param name
* @returns {*}
*/
getCookie: function (name) {
var strCookie = document.cookie;
var arrCookie = strCookie.split("; ");
for (var i = 0; i < arrCookie.length; i++) {
var arr = arrCookie[i].split("=");
if (name == arr[0]) {
return arr[1];
}
} }
return null;
},
/**
* 删除cookie
* @param name
*/
delCookie: function (name) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = this.getCookie(name);
if (cval != null)
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
} }
...@@ -180,12 +180,11 @@ ...@@ -180,12 +180,11 @@
<li class="row rowCenter verCenter">本月</li> <li class="row rowCenter verCenter">本月</li>
<li class="row rowCenter verCenter">全年</li> <li class="row rowCenter verCenter">全年</li>
</ul> </ul>
<el-form-item label="更新日期"> <el-form>
<el-date-picker v-model="date" type="daterange" range-separator="至" start-placeholder="开始日期" <el-form-item label="更新日期">
end-placeholder="结束日期"> <el-date-picker v-model="date" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
</el-date-picker> </el-form-item>
</el-form>
</el-form-item>
</div> </div>
</div> </div>
<div class="wrap row"> <div class="wrap row">
...@@ -207,12 +206,13 @@ ...@@ -207,12 +206,13 @@
import * as echarts from 'echarts'; import * as echarts from 'echarts';
import { import {
Button, Form,
FormItem,
Message, Message,
DatePicker DatePicker
} from 'element-ui'; } from 'element-ui';
Vue.use(Button).use(DatePicker); Vue.use(Form).use(FormItem).use(DatePicker);
export default { export default {
name: "index", name: "index",
...@@ -223,7 +223,6 @@ ...@@ -223,7 +223,6 @@
}, },
watch: {}, watch: {},
created() { created() {
}, },
mounted() { mounted() {
var myChart = echarts.init(document.getElementById('echarts_box')); var myChart = echarts.init(document.getElementById('echarts_box'));
......
...@@ -51,6 +51,7 @@ ...@@ -51,6 +51,7 @@
<script> <script>
import Vue from 'vue'; import Vue from 'vue';
import {Loading, Message} from 'element-ui'; import {Loading, Message} from 'element-ui';
import Util from "../../tool";
export default { export default {
name: "index", name: "index",
...@@ -90,7 +91,6 @@ ...@@ -90,7 +91,6 @@
}, },
created() { created() {
this.updateCp(); this.updateCp();
Message('2121');
}, },
computed: {}, computed: {},
methods: { methods: {
...@@ -129,7 +129,7 @@ ...@@ -129,7 +129,7 @@
background: 'rgba(0, 0, 0, 0)' background: 'rgba(0, 0, 0, 0)'
}); });
this.$http('post', "/auth/login1", { this.$http('post', "/auth/login", {
mobile: this.form.mobile, mobile: this.form.mobile,
password: this.form.password, password: this.form.password,
captcha: this.form.captcha, captcha: this.form.captcha,
...@@ -138,9 +138,11 @@ ...@@ -138,9 +138,11 @@
loadingInstance.close(); loadingInstance.close();
let res = data.data; let res = data.data;
if (res.err_code === 0) { if (res.err_code === 0) {
Util.setCookie("token", res.data.api_token, 1);
window.location.href = '/';
} else { } else {
Message(res.err_msg); Message(res.err_msg);
this.updateCp();
} }
}).catch(err => { }).catch(err => {
loadingInstance.close(); loadingInstance.close();
......
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