提交 7ac4108d 编写于 作者: yma16's avatar yma16

fix:倒序

上级 a2432a94
...@@ -11,7 +11,7 @@ module.exports = { ...@@ -11,7 +11,7 @@ module.exports = {
assetsPublicPath: "/", assetsPublicPath: "/",
proxyTable: { proxyTable: {
"/api/": { "/api/": {
target: "http://114.116.52.53/", //后端接口地址 target: "http://yongma16.xyz/", //后端接口地址
ws: true, //接受websocket请求 ws: true, //接受websocket请求
changeOrigin: true, //是否允许跨越 changeOrigin: true, //是否允许跨越
chunkOrigins: true, chunkOrigins: true,
......
...@@ -55,7 +55,7 @@ export default { ...@@ -55,7 +55,7 @@ export default {
}; };
}, },
mounted() { mounted() {
this.getComments(); articleId && this.getComments();
}, },
methods: { methods: {
dateToString(dateValue) { dateToString(dateValue) {
...@@ -95,7 +95,6 @@ export default { ...@@ -95,7 +95,6 @@ export default {
let params = { let params = {
articleId: articleId ? articleId : that.articleId, articleId: articleId ? articleId : that.articleId,
}; };
console.log("获取评论id", params.articleId);
axios axios
.post(that.baseurl + "comment/get/", params) .post(that.baseurl + "comment/get/", params)
.then((res) => { .then((res) => {
...@@ -106,35 +105,34 @@ export default { ...@@ -106,35 +105,34 @@ export default {
item.date = that.dateToString(item.date); item.date = that.dateToString(item.date);
}); });
} catch (e) { } catch (e) {
console.log(e); throw Error(e)
} }
console.log("评论获取成功", res);
}) })
.catch((r) => { .catch((r) => {
console.log("评论获取失败", r); throw Error(r)
}); });
}, },
increment(userId, articleId, content) { increment(userId, articleId, content) {
if (content === "" || !content) {
this.$notify({
title: "评论失败!",
message: "评论内容不能为空",
position: "top-left",
});
return false;
}
let that = this; let that = this;
if (!localStorage.getItem("yma16siteUserInfoName")) { if (!localStorage.getItem("yma16siteUserInfo")) {
that.$router that.$router
.push({ .push({
path: "/login", path: "/login",
}) })
.catch((error) => { .catch((error) => {
console.log(error); throw Error(error);
}); });
} else { } else {
that.userId = localStorage.getItem("yma16siteUserInfoName"); if (content === "" || !content) {
console.log("提交评论", articleId, that.userId, content); that.$notify({
title: "评论失败!",
message: "评论内容不能为空",
position: "top-left",
});
return false;
}
const userInfo = JSON.parse(localStorage.getItem("yma16siteUserInfo"));
that.userId = userInfo.username;
let params = { let params = {
username: that.userId, username: that.userId,
articleId: articleId, articleId: articleId,
...@@ -151,11 +149,18 @@ export default { ...@@ -151,11 +149,18 @@ export default {
item.date = that.dateToString(item.date); item.date = that.dateToString(item.date);
}); });
} catch (e) { } catch (e) {
console.log(e); throw Error(e)
} }
}) })
.catch((r) => { .catch((r) => {
console.log("评论提交失败", r); that.$router
.push({
path: "/login",
})
.catch((error) => {
throw Error(error)
});
throw Error(r);
}); });
} }
}, },
......
...@@ -262,7 +262,7 @@ export default { ...@@ -262,7 +262,7 @@ export default {
pageSize: 4, pageSize: 4,
pageArray: [6, 8, 10], pageArray: [6, 8, 10],
postcomment: { postcomment: {
articleId: 1, articleId: undefined,
articleCommit: null, articleCommit: null,
}, },
articleId: [], //文章的id articleId: [], //文章的id
...@@ -270,8 +270,6 @@ export default { ...@@ -270,8 +270,6 @@ export default {
}, },
watch: { watch: {
topshow: function (newvalue, oldvalue) { topshow: function (newvalue, oldvalue) {
console.log("侦听");
console.log("new", newvalue, "old", oldvalue);
if (newvalue === true) { if (newvalue === true) {
document.getElementById("topiconid").style.display = "none"; document.getElementById("topiconid").style.display = "none";
} else { } else {
...@@ -293,16 +291,13 @@ export default { ...@@ -293,16 +291,13 @@ export default {
handleSizeChange(val) { handleSizeChange(val) {
// 转到第一页md_title // 转到第一页md_title
let that = this; let that = this;
console.log(`每页 ${val} 条`);
that.pageAnalysFun(that.pageAnalys.allTitle, val); //触发修改 that.pageAnalysFun(that.pageAnalys.allTitle, val); //触发修改
}, },
handleCurrentChange(val) { handleCurrentChange(val) {
let that = this; let that = this;
console.log(that.currentPage, "currentPage", that.pageAnalys.allTitle);
//触发md_title的修改 //触发md_title的修改
that.currentPage = val; that.currentPage = val;
that.pageAnalysFun(that.pageAnalys.allTitle); //触发修改 that.pageAnalysFun(that.pageAnalys.allTitle); //触发修改
console.log(`当前页: ${val}`);
}, },
pageAnalysFun(allTitleArray, selectListLength) { pageAnalysFun(allTitleArray, selectListLength) {
let that = this; let that = this;
...@@ -315,7 +310,6 @@ export default { ...@@ -315,7 +310,6 @@ export default {
that.splitLength = selectListLength; //划分长度 that.splitLength = selectListLength; //划分长度
} }
splitLength = that.splitLength; splitLength = that.splitLength;
console.log("一页几条", splitLength);
let splitNum = Math.ceil(that.total / splitLength); //向上取整 let splitNum = Math.ceil(that.total / splitLength); //向上取整
that.pageAnalys.childNum = splitNum; //子数组个数 that.pageAnalys.childNum = splitNum; //子数组个数
that.pageAnalys.allTitle = allTitleArray; //所有标题 that.pageAnalys.allTitle = allTitleArray; //所有标题
...@@ -348,20 +342,15 @@ export default { ...@@ -348,20 +342,15 @@ export default {
try { try {
that.md_title = that.pageAnalys.titleArray[this.currentPage - 1]; that.md_title = that.pageAnalys.titleArray[this.currentPage - 1];
} catch (e) { } catch (e) {
console.log(e); throw Error(e)
} }
console.log("总数", that.pageAnalys.titleArray);
console.log("分页数据", that.pageAnalys.titleArray[this.currentPage - 1]);
console.log("当前页", that.md_title);
}, },
increment: function () { increment: function () {
this.$store.commit("increment"); this.$store.commit("increment");
console.log(this.$store.state.count);
}, },
topvisible: function () { topvisible: function () {
let dom = document.getElementById("hometitle"); let dom = document.getElementById("hometitle");
this.topshow = this.elementIsVisibleInViewport(dom); this.topshow = this.elementIsVisibleInViewport(dom);
// console.log('this.topshow', this.topshow)
setTimeout(this.topvisible); setTimeout(this.topvisible);
}, },
elementIsVisibleInViewport: function (el, partiallyVisible = false) { elementIsVisibleInViewport: function (el, partiallyVisible = false) {
...@@ -375,7 +364,7 @@ export default { ...@@ -375,7 +364,7 @@ export default {
: top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth; : top >= 0 && left >= 0 && bottom <= innerHeight && right <= innerWidth;
}, },
li_errorClass: function () { li_errorClass: function () {
console.log("选择文章");
}, },
cssPage: function (index) { cssPage: function (index) {
this.challenge_loc = index; this.challenge_loc = index;
...@@ -387,7 +376,7 @@ export default { ...@@ -387,7 +376,7 @@ export default {
path: "/css", path: "/css",
}) })
.catch((error) => { .catch((error) => {
console.log(error); throw Error(error)
}); });
break; break;
case "echarts地图配置": case "echarts地图配置":
...@@ -396,7 +385,7 @@ export default { ...@@ -396,7 +385,7 @@ export default {
path: "/home", path: "/home",
}) })
.catch((error) => { .catch((error) => {
console.log(error); throw Error(error)
}); });
break; break;
case "websocket聊天": case "websocket聊天":
...@@ -405,14 +394,13 @@ export default { ...@@ -405,14 +394,13 @@ export default {
path: "/onlinewebsocket", path: "/onlinewebsocket",
}) })
.catch((error) => { .catch((error) => {
console.log(error); throw Error(error)
}); });
break; break;
case "vue3测试平台": case "vue3测试平台":
window.open(this.baseurl + "emoji/index/"); window.open(this.baseurl + "/emoji_api/index/");
break; break;
default: default:
console.log("case name没找到");
break; break;
} }
}, },
...@@ -445,72 +433,37 @@ export default { ...@@ -445,72 +433,37 @@ export default {
// 传入索引 // 传入索引
}) })
.then((res) => { .then((res) => {
// alter('提交中');
// console.log(res.data)
if (res.data.code === 1) { if (res.data.code === 1) {
console.log("success return home!");
// that.$router.push({path:'/home'})
// 无需跳转
// document.cookie= `user=${that.ruleForm.name}`
// 添加cookie
console.log("获取Article");
let respdata = res.data; let respdata = res.data;
let content = respdata.article; let content = respdata.article;
that.md_data = []; // 清空 that.md_data = []; // 清空
// console.log(that.md_data)
content.map((o) => { content.map((o) => {
that.md_data.push(o); // 传入一个文章 that.md_data.push(o); // 传入一个文章
}); });
that.the_title = respdata.the_title; that.the_title = respdata.the_title;
} else if (res.data.code === 0) { } else if (res.data.code === 0) {
// alter("失败!") // alter("失败!")
console.log("post 失败"); that.$message.error('失败')
} }
}) })
.catch((res) => { .catch((error) => {
console.log(res); throw Error(error)
}); });
console.log("that.postcomment.articleId", that.postcomment.articleId);
that.getCommentsFromArticle(that.postcomment.articleId); that.getCommentsFromArticle(that.postcomment.articleId);
}, },
}, },
// mounted() {
// console.log('document滚动',document.documentElement.scrollTop);
// window.addEventListener("scroll", this.handleScroll);
// window.addEventListener("echarts", this.datasetfunc);
// },
// watch(){
// console.log('watch事件!');
// console.log('document滚动',document.documentElement.scrollTop);
// },
// destroyed() {
// console.log('document滚动',document.documentElement.scrollTop);
// // window.removeEventListener("echarts", this.datasetfunc);
// },
created: function () { created: function () {
// 初始化 // 初始化
console.log("初始化");
let that = this; let that = this;
// window.οnscrοll = function () {
// //鼠标滚轮滚动距离超过10像素时,回到顶部按钮才显示
// if (document.documentElement.scrollTop > 10) {
// that.toTopIsShow = true;
// } else {
// that.toTopIsShow = false;
// }
// };
console.log("测试created!");
axios axios
.get(that.baseurl + "article/index/") .get(that.baseurl + "article/index/")
.then((res) => { .then((res) => {
console.log("获取Article", res);
let resdata = res.data; let resdata = res.data;
let content = resdata.article; // 传递过来的文章 进行处理 let content = resdata.article; // 传递过来的文章 进行处理
let title = resdata.title; let title = resdata.title;
that.articleId = resdata.articleObjectId; that.articleId = resdata.articleObjectId;
console.log("获取ArticleId", that.articleId); that.postcomment.articleId = resdata.articleId;
// console.log('content', content, 'title', title) this.$refs.commentRef.getComments(that.postcomment.articleId); //触发子组件的方法
content.map((o) => { content.map((o) => {
that.md_data.push(o); that.md_data.push(o);
}); });
...@@ -518,31 +471,10 @@ export default { ...@@ -518,31 +471,10 @@ export default {
return o; return o;
}); });
that.the_title = resdata.the_title; that.the_title = resdata.the_title;
console.log("分页实现");
this.pageAnalysFun(titleAllArray); //分页 this.pageAnalysFun(titleAllArray); //分页
// that.md_data.push(res.data.article); //markdown值传递
// let weather = resdata.weather
// let high = weather.high
// let low = weather.low
// console.log('weather', weather)
// weather.title.map((o) => {
// that.weather_title.push(o)
// })
// high.map((o) => {
// that.weather_high.push(o)
// })
// low.map((o) => {
// that.weather_low.push(o)
// })
// try {
// this.datasetfunc() // 等待dom和文件加载完之后执行dataserfunc,加载天气
// } catch (e) {
// console.log(e)
// window.onload = this.datasetfunc // 等待dom
// }
}) })
.catch((res) => { .catch((r) => {
console.log(res); throw Error(r);
}); // get log }); // get log
}, },
}; };
......
...@@ -67,7 +67,6 @@ export default { ...@@ -67,7 +67,6 @@ export default {
window.parent.location.href = this.ifreame_content; window.parent.location.href = this.ifreame_content;
}, },
li_errorClass: function () { li_errorClass: function () {
console.log(0);
}, },
get_ifreame: function (index) { get_ifreame: function (index) {
let that = this; // that let that = this; // that
...@@ -77,15 +76,10 @@ export default { ...@@ -77,15 +76,10 @@ export default {
}, },
getCss: function () { getCss: function () {
try { try {
console.log(
"domMain",
document.getElementById("mainappid"),
document.getElementById("mainappid").style
);
document.getElementById("mainappid").style.padding = "0"; document.getElementById("mainappid").style.padding = "0";
document.getElementById("mainappid").style.margin = "0"; document.getElementById("mainappid").style.margin = "0";
} catch (e) { } catch (e) {
console.log("e", e); throw Error(e)
} }
let that = this; let that = this;
...@@ -101,7 +95,6 @@ export default { ...@@ -101,7 +95,6 @@ export default {
// 标题传递 // 标题传递
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
that.css_title = csstitle; that.css_title = csstitle;
console.log(that.css_path, that.css_title);
// 传递第一个路html路径 // 传递第一个路html路径
that.ifreame_content = that.css_path[0]; that.ifreame_content = that.css_path[0];
// 传递长度 // 传递长度
......
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
type="password" type="password"
v-model="ruleForm.pass" v-model="ruleForm.pass"
autocomplete="off" autocomplete="off"
show-password
></el-input> ></el-input>
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
...@@ -31,7 +32,7 @@ ...@@ -31,7 +32,7 @@
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
<el-link target="_blank" @click="$router.push({ path: '/register' })" <el-link target="_blank" @click="$router.push({ path: '/register' })"
>没有账号</el-link >没有账号?</el-link
> >
<el-link type="primary" @click="$router.push({ path: '/register' })" <el-link type="primary" @click="$router.push({ path: '/register' })"
>去注册</el-link >去注册</el-link
...@@ -43,12 +44,11 @@ ...@@ -43,12 +44,11 @@
</template> </template>
<script> <script>
import axios from "axios";
import store from "@/store"; import store from "@/store";
export default { export default {
name: "Login", name: "Login",
data() { data() {
var checkName = (rule, value, callback) => { const checkName = (rule, value, callback) => {
if (!value) { if (!value) {
return callback(new Error("账号不能为空")); return callback(new Error("账号不能为空"));
} }
...@@ -63,7 +63,7 @@ export default { ...@@ -63,7 +63,7 @@ export default {
} }
}, 1000); }, 1000);
}; };
var validatePass = (rule, value, callback) => { const validatePass = (rule, value, callback) => {
if (value === "") { if (value === "") {
callback(new Error("请输入密码")); callback(new Error("请输入密码"));
} else { } else {
...@@ -104,39 +104,37 @@ export default { ...@@ -104,39 +104,37 @@ export default {
this.$refs[formName].validate((valid) => { this.$refs[formName].validate((valid) => {
if (valid) { if (valid) {
// 提交数据 // 提交数据
console.log(that.ruleForm.name, that.ruleForm.pass);
// axios提交数据 post
const loginUrl = that.baseurl + "user/login/"; const loginUrl = that.baseurl + "user/login/";
const userInfo = { const userInfo = {
name: that.ruleForm.name, name: that.ruleForm.name,
password: that.ruleForm.pass, password: that.ruleForm.pass,
}; };
axios that.$axios
.post(loginUrl, userInfo) .post(loginUrl, userInfo)
.then((res) => { .then((res) => {
// alter('提交中'); // alter('提交中');
console.log(res.data);
if (res.data.code == 1) { if (res.data.code == 1) {
localStorage.setItem("yma16siteUserInfoName", userInfo.name); localStorage.setItem(
localStorage.setItem("yma16siteUserInfoPwd", userInfo.password); "yma16siteUserInfo",
JSON.stringify({
username: userInfo.name,
password: userInfo.password,
})
);
store.commit("setUserInfo", userInfo); // store信息 store.commit("setUserInfo", userInfo); // store信息
console.log("return login success!");
document.cookie = `user=${that.ruleForm.name}`; document.cookie = `user=${that.ruleForm.name}`;
that.$router.push({ path: "/" }); that.$router.push({ path: "/" });
// 添加cookie // 添加cookie
} else if (res.data.code == 0) { } else if (res.data.code == 0) {
// alter("失败!") // alter("失败!" res.data.msg)
console.log("失败", res); that.$message.error('用化名或密码错误');
} }
// that.$cookies.set('')
}) })
.catch((res) => { .catch((error) => {
console.log("post失败"); that.$message.error(error);
console.log(res);
}); });
} else { } else {
alert("请输入账号密码"); that.$message.error("请输入账号密码");
console.log("请输入账号密码");
return false; return false;
} }
}); });
......
...@@ -45,13 +45,15 @@ ...@@ -45,13 +45,15 @@
type="password" type="password"
v-model="ruleForm.pass" v-model="ruleForm.pass"
autocomplete="off" autocomplete="off"
show-password
></el-input> ></el-input>
</el-form-item> </el-form-item>
<el-form-item label="确认" prop="checkPass"> <el-form-item label="密码" prop="checkPass">
<el-input <el-input
type="password" type="password"
v-model="ruleForm.checkPass" v-model="ruleForm.checkPass"
autocomplete="off" autocomplete="off"
show-password
></el-input> ></el-input>
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
...@@ -62,7 +64,7 @@ ...@@ -62,7 +64,7 @@
</el-form-item> </el-form-item>
<el-form-item> <el-form-item>
<el-link target="_blank" @click="$router.push({ path: '/login' })" <el-link target="_blank" @click="$router.push({ path: '/login' })"
>已有账号</el-link >已有账号?</el-link
> >
<el-link type="primary" @click="$router.push({ path: '/login' })" <el-link type="primary" @click="$router.push({ path: '/login' })"
>去登录</el-link >去登录</el-link
...@@ -136,7 +138,6 @@ export default { ...@@ -136,7 +138,6 @@ export default {
const that = this; const that = this;
if (valid) { if (valid) {
// 提交数据 // 提交数据
console.log(this.ruleForm.name, this.ruleForm.pass);
// alert("yes submit!"); // alert("yes submit!");
that.register_success = false; // 初始化register的状态 that.register_success = false; // 初始化register的状态
that.register_error = false; that.register_error = false;
...@@ -147,34 +148,19 @@ export default { ...@@ -147,34 +148,19 @@ export default {
password: that.ruleForm.pass, password: that.ruleForm.pass,
}) })
.then(function (res) { .then(function (res) {
console.log("成功post", res);
console.log(res.data.code);
if (res.data.code === 0) { if (res.data.code === 0) {
console.log("重名");
console.log(that.$cookies.get("user_session")); // 取出cookies的user_session
console.log(that.$cookies); // 存在
document.cookie = `user=${that.ruleForm.name}`; document.cookie = `user=${that.ruleForm.name}`;
that.register_error = true; that.register_error = true;
that.$message({ that.$message({
message: "名称重复" + that.ruleForm.name + "!", message: "名称重复" + that.ruleForm.name + "!",
type: "error", type: "error",
}); });
// console.log(this.$cookies)
} else { } else {
that.register_success = true; that.register_success = true;
console.log("时间节点");
that.$message({ that.$message({
message: "注册成功,欢迎您," + that.ruleForm.name + "!", message: "注册成功,欢迎您," + that.ruleForm.name + "!",
type: "success", type: "success",
}); });
// let token={'user':res.data.name};//传递主码用户名即可
// this.$cookie.set('token',token,1);//设置token
// console.log('token设置');
// let token=this.$cookie.get('token');
// console.log(token);
// console.log('设置token成功!');
} }
}) })
.catch(function (res) { .catch(function (res) {
...@@ -183,17 +169,12 @@ export default { ...@@ -183,17 +169,12 @@ export default {
message: "注册失败" + that.ruleForm.name + "!", message: "注册失败" + that.ruleForm.name + "!",
type: "error", type: "error",
}); });
alert("后端的问题!");
console.log("失败post", res);
}); });
} else { } else {
// alert("no submit!");
console.log("前端的问题!");
that.$message({ that.$message({
message: "注册失败" + that.ruleForm.name + "!", message: "注册失败" + that.ruleForm.name + "!",
type: "error", type: "error",
}); });
// console.log(this.$cookies);
return false; return false;
} }
}); });
......
import Vue from 'vue' import Vue from "vue";
import Nprogress from 'nprogress' import Nprogress from "nprogress";
import 'nprogress/nprogress.css' import "nprogress/nprogress.css";
import { getToken } from '@/utils/auth' import { getToken } from "@/utils/auth";
import { isEmpty } from '@/utils' import { isEmpty } from "@/utils";
import VueRouter from 'vue-router' import VueRouter from "vue-router";
// import Router from 'vue-router' // import Router from 'vue-router'
import store from '@/store' import store from "@/store";
// store // store
import Article from '@/components/Article' import Article from "@/components/Article";
import Login from '@/components/Login' import Login from "@/components/Login";
import Register from '@/components/Register' import Register from "@/components/Register";
import Onlinewebsocket from '@/components/Onlinewebsocket' import Onlinewebsocket from "@/components/Onlinewebsocket";
import Home from '@/components/Home' import Home from "@/components/Home";
import Bilicom from '@/components/Bilicom' import Bilicom from "@/components/Bilicom";
import Mavoneditor from '@/components/Mavoneditor' import Mavoneditor from "@/components/Mavoneditor";
import Loading from '@/components/Loading' import Loading from "@/components/Loading";
import GrilShow from '@/components/GrilShow' import GrilShow from "@/components/GrilShow";
import Csslearn from '@/components/Csslearn' import Csslearn from "@/components/Csslearn";
Vue.use(VueRouter) Vue.use(VueRouter);
const defaultRoutes = [ const defaultRoutes = [
{ {
path: '/', path: "/",
name: 'Article', name: "Article",
component: Article component: Article,
}, },
{ {
path: '/login', path: "/login",
name: 'Login', name: "Login",
component: Login component: Login,
}, },
{ {
path: '/register', path: "/register",
name: 'Register', name: "Register",
component: Register component: Register,
}, },
{ {
path: '/home', path: "/home",
name: 'Home', name: "Home",
component: Home component: Home,
}, },
{ {
path: '/onlinewebsocket', path: "/onlinewebsocket",
name: 'Onlinewebsocket', name: "Onlinewebsocket",
component: Onlinewebsocket component: Onlinewebsocket,
}, },
{ {
path: '/bilicom', path: "/bilicom",
name: 'Bilicom', name: "Bilicom",
component: Bilicom component: Bilicom,
}, },
{ {
path: '/mavoneditor', path: "/mavoneditor",
name: 'Mavoneditor', name: "Mavoneditor",
component: Mavoneditor component: Mavoneditor,
}, },
{ {
path: '/loading', path: "/loading",
name: 'loading', name: "loading",
component: Loading component: Loading,
}, },
{ {
path: '/gril', path: "/gril",
name: 'grilshow', name: "grilshow",
component: GrilShow component: GrilShow,
}, },
{ {
path: '/css', path: "/css",
name: 'css', name: "css",
component: Csslearn component: Csslearn,
} },
// { // {
// path: '/register', // path: '/register',
// name: 'Register', // name: 'Register',
...@@ -102,65 +102,55 @@ const defaultRoutes = [ ...@@ -102,65 +102,55 @@ const defaultRoutes = [
// component: () => // component: () =>
// import ('../components/Bilicom.vue') // import ('../components/Bilicom.vue')
// } // }
] ];
let routes = [ let routes = [
...defaultRoutes ...defaultRoutes,
// ...modulesRoutes, // ...modulesRoutes,
// ...errorRoutes, // ...errorRoutes,
// ...dashboardRouters // ...dashboardRouters
] ];
const originalPush = VueRouter.prototype.push const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push (location) { VueRouter.prototype.push = function push(location) {
return originalPush.call(this, location).catch((err) => err) return originalPush.call(this, location).catch((err) => err);
} };
const router = new VueRouter({ const router = new VueRouter({
routes routes,
}) });
const whiteList = ['/login'] const whiteList = ["/login", "/register"];
router.beforeEach(async (to, from, next) => { router.beforeEach(async (to, from, next) => {
// Nprogress.start() next();
// next() let yma16siteUserInfo = localStorage.getItem("yma16siteUserInfo")
// let hasToken = getToken() ? JSON.parse(localStorage.getItem("yma16siteUserInfo"))
next() : "";
console.log('localStorage', localStorage) let name = yma16siteUserInfo.username,
let name = localStorage.getItem('yma16siteUserInfoName') pwd = yma16siteUserInfo.password;
let pwd = localStorage.getItem('yma16siteUserInfoPwd')
let hasToken = { let hasToken = {
name: name, name: name,
password: pwd password: pwd,
} };
console.log('hasToken', hasToken) console.log("localStorage", hasToken);
console.log('store.state 初始化', store.state)
if (hasToken.name && hasToken.password) { if (hasToken.name && hasToken.password) {
console.log('store.state.user 存在', store.state.user)
if (isEmpty(store.state.user.userInfo)) { if (isEmpty(store.state.user.userInfo)) {
// 空的 modules下的user // 空的 modules下的user
console.log('路由的登录认证') console.log("路由的登录认证");
// 用户自主登录 // 用户自主登录
await store.dispatch('user/loginUserInfo', hasToken) await store.dispatch("user/loginUserInfo", hasToken);
// await store.dispatch('user/loginUserInfo', hasToken) next();
next()
} else { } else {
next() next();
} }
} else { } else {
if (whiteList.indexOf(to.path) !== -1) { // next({ path: "/login" }); //去登录
console.log('store.state', store.state) next();
next()
} else {
console.log('store.state 通过', store.state)
next()
// next({ path: '/login' }) // 否则全部重定向到登录页
// Nprogress.done();
}
} }
}) });
router.afterEach((to, from) => { router.afterEach((to, from) => {
Nprogress.done() Nprogress.done();
}) });
export default router export default router;
...@@ -2,15 +2,21 @@ import axios from "axios"; ...@@ -2,15 +2,21 @@ import axios from "axios";
export function loginUser(data) { export function loginUser(data) {
console.log("用户登录service!", data); console.log("用户登录service!", data);
let baseurl = "/api/user/login/"; let baseUrl = "/api/user/login/";
let res = null; let res = null;
axios axios
.post(baseurl, data) .post(baseUrl, data)
.then((o) => { .then((o) => {
res = o; res = o;
console.log("成功认证", res); console.log("成功认证", res);
localStorage.setItem("yma16siteUserInfoName", res.name); // 只返回username
localStorage.setItem("yma16siteUserInfoPwd", res.password); localStorage.setItem(
"yma16siteUserInfo",
JSON.stringify({
username: data.name, //保存一个用户名
password: data.password,
})
);
}) })
.catch((r) => { .catch((r) => {
console.log("登录失败!", r); console.log("登录失败!", r);
......
import { loginUser } from "@/service/user.service"; import { loginUser } from '@/service/user.service'
export default { export default {
// 自带命名空间 // 自带命名空间
namespaced: true, namespaced: true,
state: { state: {
// 用户账号密码 // 用户账号密码
userInfo: null userInfo: null
}, },
getters: { getters: {
userInfo: state => { return state.userInfo } userInfo: state => { return state.userInfo }
}, },
mutations: { mutations: {
// 配置用户登录信息 userInfo // 配置用户登录信息 userInfo
setUserInfo(state, userInfo) { setUserInfo (state, userInfo) {
state.userInfo = userInfo; state.userInfo = userInfo
} }
}, },
actions: { actions: {
// Promise // Promise
loginUserInfo({ commit }, info) { loginUserInfo ({ commit }, info) {
console.log('登录认证 store里面', 'info', info) console.log('登录认证 store里面', 'info', info)
return new Promise(async(resolve, reject) => { return new Promise(async (resolve, reject) => {
try { try {
let data = await loginUser(info); let data = await loginUser(info)
// 等待登录信息 // 等待登录信息
console.log('返回的post', data) console.log('返回的post', data)
// 调用 mutation // 调用 mutation
commit("setUserInfo", data); commit('setUserInfo', data)
resolve(data); resolve(data)
} catch (error) { } catch (error) {
reject(error); reject(error)
}
})
} }
})
} }
} }
\ No newline at end of file }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册