Skip to content

Commit

Permalink
[U] 代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
zxysilent committed Apr 15, 2021
1 parent 346de43 commit 9acb769
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 91 deletions.
1 change: 0 additions & 1 deletion vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
},
"dependencies": {
"axios": "^0.21.1",
"js-base64": "^2.5.2",
"js-md5": "^0.7.3",
"mavon-editor": "^2.9.1",
"view-design": "^4.1.2",
Expand Down
28 changes: 14 additions & 14 deletions vue/src/api/fetch.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios from "axios";
import Util from "@/utils.js";
import { getToken ,clearData} from "@/utils/token";
import ViewUI from "view-design";
import Router from "@/router"
import Router from "@/router";
const fetch = axios.create({
baseURL: process.env.VUE_APP_SRV,
timeout: 30000
Expand All @@ -13,10 +13,10 @@ fetch.interceptors.request.use(
//在请求发出之前进行一些操作
console.log("send");
// 仅以/adm开始的接口才传递 token
// 可自行修改为一直携带或者仅登录不携带-由程序内自主控制
// if (config.url.indexOf("/api/auth/login") == -1) {
// 可自行修改为一直携带或者仅登录不携带-由程序内自主控制
// if (config.url.indexOf("/api/auth/login") == -1) {
if (config.url.indexOf("/adm") == 0) {
config.headers.Authorization = Util.getItem("bearer");
config.headers.Authorization = getToken();
}
return config;
},
Expand All @@ -27,21 +27,21 @@ fetch.interceptors.request.use(
);
//添加一个响应拦截器
fetch.interceptors.response.use(
function(res) {
function(resp) {
ViewUI.LoadingBar.finish();
//在这里对返回的数据进行处理
console.log("recv");
if (res.data.code == 330) {
if (resp.data.code == 330) {
// ViewUI.Notice.error({
// duration: 3,
// title: "系统提醒",
// desc: "对不起你没有权限访问"
// });
// return new Promise(() => {});
// location.href = "/#/401"; //没有权限
Router.push({ name: "err401" });
Router.push({ name: "err401" });
}
if (res.data.code == 340) {
if (resp.data.code == 340) {
// ViewUI.Notice.error({
// duration: 2,
// title: "系统提醒",
Expand All @@ -53,21 +53,21 @@ fetch.interceptors.response.use(
// }
// });
// return new Promise(() => {});
Util.clearData();
clearData();
// location.href = "/#/jwt"; //需要重新登陆
Router.push({ name: "errjwt" });
Router.push({ name: "errjwt" });
}
if (res.data.code == 350) {
if (resp.data.code == 350) {
// ViewUI.Notice.error({
// duration: 3,
// title: "系统提醒",
// desc: "服务端发生错误,请重试"
// });
// return new Promise(() => {});
// location.href = "/#/50x"; //服务器异常
Router.push({ name: "err50x" });
Router.push({ name: "err50x" });
}
return res.data;
return resp.data;
},
function(err) {
ViewUI.Notice.error({
Expand Down
30 changes: 22 additions & 8 deletions vue/src/router/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Vue from "vue";
import Utils from "@/utils.js";
import { getToken } from "@/utils/token";
import store from "@/store";
import ViewUI from "view-design";
import VueRouter from "vue-router";
Expand Down Expand Up @@ -56,11 +56,25 @@ export const initRouter = [
}
]
},
{ path: "/jwt", name: "errjwt",meta: { title: "jwt-重新登录" }, component: () => import("@/views/errors/jwt.vue") },
{ path: "/401", name: "err401",meta: { title: "401-没有权限" }, component: () => import("@/views/errors/401.vue") },
{ path: "/50x", name: "err50x",meta: { title: "50x-服务异常" }, component: () => import("@/views/errors/50x.vue") }
{
path: "/jwt",
name: "errjwt",
meta: { title: "jwt-重新登录" },
component: () => import("@/views/errors/jwt.vue")
},
{
path: "/401",
name: "err401",
meta: { title: "401-没有权限" },
component: () => import("@/views/errors/401.vue")
},
{ path: "/50x", name: "err50x", meta: { title: "50x-服务异常" }, component: () => import("@/views/errors/50x.vue") }
];

const toTitle = title => {
title = title || "zadmin";
window.document.title = title;
};
// 路由配置
const RouterConfig = {
mode: "hash", //history
Expand All @@ -72,12 +86,12 @@ const router = new VueRouter(RouterConfig);
const noAuth = ["login", "errjwt", "err50x", "err401"];
router.beforeEach(async (to, from, next) => {
ViewUI.LoadingBar.start();
Utils.title(to.meta.title);
const token = Utils.getToken();
toTitle(to.meta.title);
const token = getToken();
// 去不需要登录的地方
if (noAuth.indexOf(to.name) > -1) {
if (token) {
Utils.title("主页");
toTitle("主页");
next({ name: "home" });
} else {
next();
Expand All @@ -95,7 +109,7 @@ router.beforeEach(async (to, from, next) => {
}
} else {
//没有登陆 不是去不需要权限的地方
Utils.title("登陆");
toTitle("登陆");
next({ name: "login" });
}
}
Expand Down
29 changes: 0 additions & 29 deletions vue/src/utils.js

This file was deleted.

43 changes: 21 additions & 22 deletions vue/src/utils/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function(fmt) {
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
S: this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
return fmt;
};

export function formatTimeToStr(times, pattern) {
var d = new Date(times).Format("yyyy-MM-dd hh:mm:ss");
if (pattern) {
d = new Date(times).Format(pattern);
}
return d.toLocaleString();
}
var d = new Date(times).Format("yyyy-MM-dd hh:mm:ss");
if (pattern) {
d = new Date(times).Format(pattern);
}
return d.toLocaleString();
}
25 changes: 25 additions & 0 deletions vue/src/utils/token.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 开发模式使用 localStorage
// 正式上线使用 sessionStorage
const storage = process.env.NODE_ENV === "development" ? localStorage : sessionStorage;
// token key
const tokenKey = "bearer";
// 保存数据
export const setItem = (k, v) => {
storage.setItem(k, v);
};
// 读取数据
export const getItem = k => {
return storage.getItem(k);
};
// 清空数据
export const clearData = () => {
storage.clear();
};
// 设置token
export const setToken = token => {
storage.setItem(tokenKey, token);
};
// 读取token
export const getToken = () => {
storage.getItem(tokenKey);
};
32 changes: 18 additions & 14 deletions vue/src/views/article/article.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
<Col span="8"><Input type="text" v-bind:disabled="isEdit" v-model="dataForm.path" placeholder="请输入访问路径"></Input>
</Col>
<Col span="10" style="min-width:250px"> .html&nbsp;<a title="预览" @click="clkPreview">
<Icon type="ios-eye" size="22" /></a></Button>
<Icon type="ios-eye" size="22" />
</a></Button>
<Button type="info" @click="cmtDraft" :loading="draftLoading">
<Icon type="ios-trash" size="20" />存草稿</Button>&nbsp;
<Icon type="ios-trash" size="20" />存草稿
</Button>&nbsp;
<Button type="warning" @click="cmtPublish" :loading="publishLoading">
<Icon type="ios-send" size="20" />发 布</Button>
<Icon type="ios-send" size="20" />发 布
</Button>
</Col>
</Row>
</FormItem>
Expand All @@ -47,7 +50,8 @@
</FormItem>
<FormItem label="权限" prop="is_public">
<i-switch v-model="dataForm.is_public"><span slot="open">公开</span>
<span slot="close">私密</span></i-switch>
<span slot="close">私密</span>
</i-switch>
</FormItem>
<FormItem label="评论" prop="allow_comment">
<Checkbox v-model="dataForm.allow_comment">允许评论</Checkbox>
Expand All @@ -74,7 +78,7 @@ import "mavon-editor/dist/css/index.css";
import toolbars from "./toolbars";
import { apiCateAll } from "@/api/cate";
import { apiTagAll } from "@/api/tag";
import util from "@/utils.js";
import { getToken } from "@/utils/token";
import { apiPostGet, admPostOpts, apiPostTagGet } from "@/api/post";
// 通用 文章/页面 + 添加/修改
// 减少js体积
Expand Down Expand Up @@ -118,15 +122,15 @@ export default {
this.getOne();
}
if (this.isPost && this.isEdit) {
apiPostTagGet(this.dataId).then(resp => {
apiPostTagGet(this.dataId).then((resp) => {
if (resp.code == 200) {
this.tags = resp.data;
}
});
}
},
getCate() {
apiCateAll().then(resp => {
apiCateAll().then((resp) => {
if (resp.code == 200) {
this.cateAll = resp.data;
if (this.isAdd) {
Expand All @@ -139,7 +143,7 @@ export default {
});
},
getTag() {
apiTagAll().then(resp => {
apiTagAll().then((resp) => {
if (resp.code == 200) {
this.tagAll = resp.data;
} else {
Expand All @@ -149,7 +153,7 @@ export default {
});
},
getOne() {
apiPostGet(this.dataId).then(resp => {
apiPostGet(this.dataId).then((resp) => {
if (resp.code == 200) {
this.dataForm = resp.data;
} else {
Expand Down Expand Up @@ -210,13 +214,13 @@ export default {
this.$refs.md.$img2Url(pos, process.env.VUE_APP_SRV + json.data);
};
formData.append("token", util.getToken());
formData.append("token", getToken());
formData.append("file", $file);
xhr.send(formData);
},
// 存草稿
cmtDraft() {
this.$refs.dataForm.validate(valid => {
this.$refs.dataForm.validate((valid) => {
if (valid) {
if (this.dataForm.content == "") {
this.$Message.warning("请填写内容");
Expand All @@ -230,7 +234,7 @@ export default {
edit: this.isEdit,
type: this.isPost ? 0 : 1,
tags: this.tags
}).then(resp => {
}).then((resp) => {
this.draftLoading = false;
if (resp.code == 200) {
this.$Message.success({
Expand All @@ -251,7 +255,7 @@ export default {
},
// 发布
cmtPublish() {
this.$refs.dataForm.validate(valid => {
this.$refs.dataForm.validate((valid) => {
if (valid) {
if (this.dataForm.content == "") {
this.$Message.warning("请填写内容");
Expand All @@ -264,7 +268,7 @@ export default {
edit: this.isEdit,
type: this.isPost ? 0 : 1,
tags: this.tags
}).then(resp => {
}).then((resp) => {
this.publishLoading = false;
if (resp.code == 200) {
this.$Message.success({
Expand Down
1 change: 0 additions & 1 deletion vue/src/views/layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@
</Layout>
</template>
<script>
import Utils from "@/utils.js";
import { mapGetters, mapMutations } from "vuex";
import { admAuthGet } from "@/api/auth";
export default {
Expand Down
4 changes: 2 additions & 2 deletions vue/src/views/login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<script>
import md5 from "js-md5";
import { apiAuthLogin, apiAuthVcode } from "@/api/auth";
import Utils from "@/utils";
import { setToken } from "@/utils/token";
export default {
data() {
return {
Expand All @@ -98,7 +98,7 @@ export default {
this.$Message.success({
content: "登陆成功",
onClose: () => {
Utils.setToken(resp.data);
setToken(resp.data);
this.$router.push({ name: "home" });
}
});
Expand Down

0 comments on commit 9acb769

Please sign in to comment.