Browse Source

Sync

pull/6/head
遇见 4 years ago
parent
commit
9c46eda878
  1. 1
      .gitignore
  2. 2
      YuJian-Blog-Admin/.env.development
  3. 7
      YuJian-Blog-Server/db/config.js
  4. 2
      YuJian-Blog-Server/router/index.js
  5. 4
      YuJian-Blog-Server/router/jwt.js
  6. 2
      YuJian-Blog-Web/.env.development
  7. 5
      YuJian-Blog-Web/package-lock.json
  8. 1
      YuJian-Blog-Web/package.json
  9. 8
      YuJian-Blog-Web/src/api/article.js
  10. 7
      YuJian-Blog-Web/src/api/user.js
  11. 58
      YuJian-Blog-Web/src/store/index.js
  12. 11
      YuJian-Blog-Web/src/utils/auth.js
  13. 45
      YuJian-Blog-Web/src/utils/request.js

1
.gitignore vendored

@ -24,4 +24,5 @@ pnpm-debug.log* @@ -24,4 +24,5 @@ pnpm-debug.log*
#LOOOO
YuJian-Blog开发日志.md
Push时注意.md
Test.http

2
YuJian-Blog-Admin/.env.development

@ -4,4 +4,4 @@ ENV = 'development' @@ -4,4 +4,4 @@ ENV = 'development'
# base api
VUE_APP_BASE_API = 'http://127.0.0.1:5000'
VUE_CLI_BABEL_TRANSPILE_MODULES = true
VUE_CLI_BABEL_TRANSPILE_MODULES = true

7
YuJian-Blog-Server/db/config.js

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
module.exports = {
host: "cdb-2ujv2cb2.cd.tencentcdb.com",
port: "10001",
user: "root",
password: "123456789X",
database: "yujian-blog"
}

2
YuJian-Blog-Server/router/index.js

@ -8,7 +8,7 @@ const ArticleRouter = require("./article"); @@ -8,7 +8,7 @@ const ArticleRouter = require("./article");
const router = express.Router();
// router.use(jwtAuth);
router.use(jwtAuth);
router.get("/", (req, res) => {
res.send("Welcome Server");

4
YuJian-Blog-Server/router/jwt.js

@ -7,7 +7,7 @@ module.exports = jwt({ @@ -7,7 +7,7 @@ module.exports = jwt({
// algorithms: ['HS256'],
credentialsRequired: true,
}).unless({
path: ["/", "/user/login"],
path: ["/", "/user/login", "/article/list"],
});
// 校验Token
// 校验Token

2
YuJian-Blog-Web/.env.development

@ -1 +1 @@ @@ -1 +1 @@
VUE_APP_BASE_API = "http://www.exapmple.com/dev"
VUE_APP_BASE_API = 'http://127.0.0.1:5000'

5
YuJian-Blog-Web/package-lock.json generated

@ -6504,6 +6504,11 @@ @@ -6504,6 +6504,11 @@
}
}
},
"js-cookie": {
"version": "2.2.1",
"resolved": "https://registry.npm.taobao.org/js-cookie/download/js-cookie-2.2.1.tgz",
"integrity": "sha1-aeEG3F1YBolFYpAqpbrsN0Tpsrg="
},
"js-message": {
"version": "1.0.5",
"resolved": "https://registry.npm.taobao.org/js-message/download/js-message-1.0.5.tgz",

1
YuJian-Blog-Web/package.json

@ -11,6 +11,7 @@ @@ -11,6 +11,7 @@
"animejs": "^3.2.0",
"axios": "^0.19.2",
"core-js": "^3.6.5",
"js-cookie": "^2.2.1",
"vue": "^2.6.11",
"vue-router": "^3.2.0",
"vuex": "^3.4.0"

8
YuJian-Blog-Web/src/api/article.js

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
import request from "@/utils/request";
export function getArticle() {
return request({
url: "/article/list",
method: "get",
});
}

7
YuJian-Blog-Web/src/api/user.js

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
export function login(data) {
return request({
url: "/user/login",
method: "post",
data,
});
}

58
YuJian-Blog-Web/src/store/index.js

@ -1,11 +1,61 @@ @@ -1,11 +1,61 @@
import Vue from "vue";
import Vuex from "vuex";
import { login } from "@/api/user";
import { getArticle } from "@/api/user";
Vue.use(Vuex);
const state = {
Token: "",
Article: [],
};
const mutations = {
SET_TOKEN: (state, Token) => {
state.Token = Token;
},
SET_ARTICLE: (state, Article) => {
state.Article = Article;
},
};
const actions = {
login({ commit }) {
const username = "Ghost";
const password = "4b18888d253715d62093683bd206dfda";
return new Promise((resolve, reject) => {
login({ username: username, password: password })
.then((response) => {
const { data } = response;
commit("SET_TOKEN", data.token);
resolve();
})
.catch((error) => {
reject(error);
});
});
},
feactArticle({ commit }) {
return new Promise((resolve, reject) => {
getArticle()
.than((response) => {
if (response.error) {
reject(response.error);
}
commit("SET_ARTICLE", response.data);
resolve(response.data);
})
.catch((error) => {
reject(error);
});
});
},
};
export default new Vuex.Store({
state: {},
mutations: {},
actions: {},
modules: {}
state,
mutations,
actions,
});

11
YuJian-Blog-Web/src/utils/auth.js

@ -0,0 +1,11 @@ @@ -0,0 +1,11 @@
import Cookies from "js-cookie";
const TokenKey = "YuJianBlog";
export function getToken() {
return Cookies.get(TokenKey);
}
export function setToken(token) {
return Cookies.set(TokenKey, token)
}

45
YuJian-Blog-Web/src/utils/request.js

@ -1 +1,44 @@ @@ -1 +1,44 @@
import axios from "axios";
import axios from "axios";
import { getToken } from "@/utils/auth";
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API,
timeout: 5000,
});
service.interceptors.request.use(
(config) => {
if (this.$store.state.Token) {
config.headers["Authorization"] = `Bearer ${getToken()}`;
}
return config;
},
(error) => {
console.log(error);
return Promise.reject(error);
}
);
service.interceptors.response.use(
(response) => {
const res = response.data;
// 如果返回的状态码不为0
if (res.code !== 0) {
console.log("请求异常:未知错误", res.code);
// 如果返回的状态码为-2,则Token失效
if (res.code === -2) {
console.log("请求异常:Token失效");
}
return Promise.reject(new Error(res.msg || "请求异常"));
} else {
return res;
}
},
(error) => {
return Promise.reject(error);
}
);
export default service;

Loading…
Cancel
Save