实现购物车商品添加基本操作
This commit is contained in:
parent
54a8ad02d2
commit
c6675668f7
13
src/App.vue
13
src/App.vue
@ -3,13 +3,24 @@ import TopHeader from "@/components/TopHeader.vue";
|
|||||||
import IndexHeader from "@/components/IndexHeader.vue";
|
import IndexHeader from "@/components/IndexHeader.vue";
|
||||||
import IndexFooter from "@/components/IndexFooter.vue";
|
import IndexFooter from "@/components/IndexFooter.vue";
|
||||||
import BackToTop from "@/components/BackToTop.vue";
|
import BackToTop from "@/components/BackToTop.vue";
|
||||||
import {computed} from "vue";
|
import {computed, onMounted} from "vue";
|
||||||
import {useRoute} from "vue-router";
|
import {useRoute} from "vue-router";
|
||||||
|
import {useAccount} from "@/stores/user.js";
|
||||||
|
import request from "@/net/index.js";
|
||||||
|
import {getToken} from "@/utils/token.js";
|
||||||
|
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
|
const account = useAccount()
|
||||||
|
|
||||||
const isAuthPage = computed(
|
const isAuthPage = computed(
|
||||||
() => route.name === 'login' || route.name === 'register')
|
() => route.name === 'login' || route.name === 'register')
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
if(getToken()) {
|
||||||
|
request.get('/getInfo').then(({ data }) =>
|
||||||
|
Object.assign(account.info, data.user))
|
||||||
|
}
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
|
@ -1,5 +1,12 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
|
import {computed} from "vue";
|
||||||
|
import {getToken} from "@/utils/token.js";
|
||||||
|
import request from "@/net/index.js";
|
||||||
|
import {useAccount} from "@/stores/user.js";
|
||||||
|
|
||||||
|
const account = useAccount()
|
||||||
|
|
||||||
|
const isLogin = computed(() => !!getToken())
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@ -24,11 +31,20 @@
|
|||||||
<li><a href="#"><span class="icon-instagram"></span></a></li>
|
<li><a href="#"><span class="icon-instagram"></span></a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
<div class="contact-info">
|
<div class="contact-info" v-if="isLogin"
|
||||||
|
style="display: flex;align-items: center;margin-left: 20px">
|
||||||
|
<div style="color: white;text-align: right;font-size: 13px;line-height: 15px">
|
||||||
|
<div>{{ account.info.userName }}</div>
|
||||||
|
<div>{{ account.info.email }}</div>
|
||||||
|
</div>
|
||||||
|
<img :src="`${request.defaults.baseURL}${account.info.avatar}`"
|
||||||
|
class="user-avatar">
|
||||||
|
</div>
|
||||||
|
<div class="contact-info" v-if="!isLogin">
|
||||||
<a class="login-btn" style="margin-left: 10px" href="/login">
|
<a class="login-btn" style="margin-left: 10px" href="/login">
|
||||||
<span class="icon-user-notes"></span> 登录</a>
|
<span class="icon-user-notes"></span> 登录</a>
|
||||||
</div>
|
</div>
|
||||||
<div class="contact-info">
|
<div class="contact-info" v-if="!isLogin">
|
||||||
<a class="login-btn" style="margin-left: 10px" href="/register">
|
<a class="login-btn" style="margin-left: 10px" href="/register">
|
||||||
<span class="icon-user-plus-solid-1"></span> 注册</a>
|
<span class="icon-user-plus-solid-1"></span> 注册</a>
|
||||||
</div>
|
</div>
|
||||||
@ -40,5 +56,10 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style scoped>
|
<style scoped>
|
||||||
|
.user-avatar {
|
||||||
|
height: 35px;
|
||||||
|
width: 35px;
|
||||||
|
margin-left: 10px;
|
||||||
|
border-radius: 50%;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
16
src/stores/user.js
Normal file
16
src/stores/user.js
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import {defineStore} from "pinia";
|
||||||
|
import {ref} from "vue";
|
||||||
|
|
||||||
|
export const useAccount = defineStore('account', () => {
|
||||||
|
|
||||||
|
const info = ref({
|
||||||
|
avatar: '',
|
||||||
|
userName: '',
|
||||||
|
nickName: '',
|
||||||
|
email: '',
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
info
|
||||||
|
}
|
||||||
|
})
|
@ -2,7 +2,7 @@ export function setToken(token) {
|
|||||||
localStorage.setItem("access_token", token);
|
localStorage.setItem("access_token", token);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getToken(token) {
|
export function getToken() {
|
||||||
return localStorage.getItem("access_token");
|
return localStorage.getItem("access_token");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,9 @@ import request from "@/net/index.js";
|
|||||||
import Swal from "sweetalert2";
|
import Swal from "sweetalert2";
|
||||||
import router from "@/router/index.js";
|
import router from "@/router/index.js";
|
||||||
import {setToken} from "@/utils/token.js";
|
import {setToken} from "@/utils/token.js";
|
||||||
|
import {useAccount} from "@/stores/user.js";
|
||||||
|
|
||||||
|
const account = useAccount()
|
||||||
|
|
||||||
const verify = reactive({
|
const verify = reactive({
|
||||||
img: '',
|
img: '',
|
||||||
@ -37,6 +40,8 @@ function login() {
|
|||||||
icon: "success"
|
icon: "success"
|
||||||
}).then(() => {
|
}).then(() => {
|
||||||
setToken(data.token)
|
setToken(data.token)
|
||||||
|
request.get('/getInfo').then(({ data }) =>
|
||||||
|
Object.assign(account.info, data.user))
|
||||||
router.push('/')
|
router.push('/')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,12 @@ const data = ref([])
|
|||||||
|
|
||||||
request.get('/system/course/list')
|
request.get('/system/course/list')
|
||||||
.then((res) => data.value = res.data.rows)
|
.then((res) => data.value = res.data.rows)
|
||||||
|
|
||||||
|
function addCourseToCart(id) {
|
||||||
|
request.post(`/system/item/add/${id}`).then(({ data }) => {
|
||||||
|
console.info(data)
|
||||||
|
})
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@ -47,7 +53,7 @@ request.get('/system/course/list')
|
|||||||
<p>¥{{ (course.price * 0.9).toFixed(0) }} <span>¥{{ course.price }}</span></p>
|
<p>¥{{ (course.price * 0.9).toFixed(0) }} <span>¥{{ course.price }}</span></p>
|
||||||
</div>
|
</div>
|
||||||
<div class="course-buy">
|
<div class="course-buy">
|
||||||
<p><a href="#">
|
<p><a @click="addCourseToCart(course.id)">
|
||||||
<i class="icon-icon_cart_alt"></i>
|
<i class="icon-icon_cart_alt"></i>
|
||||||
<span>添加到购物车</span>
|
<span>添加到购物车</span>
|
||||||
</a>
|
</a>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user