Vue加入購物車判斷token添加登錄提示功能
Vue-加入購物車-判斷token添加登錄提示
目標:給未登錄的用戶,添加登錄提示
說明:加入購物車,是一個登錄后的用戶 才能進行的操作,所以需要進行鑒權判斷,判斷用戶token是否存在
- 若存在:繼續(xù)加入購物車操作
- 不存在:提示用戶未登錄,引導到登錄頁,登錄完回跳
addCart () { // 判斷 token 是否存在 // 1.如果token不存在,彈確認框 // 2.如果token存在,繼續(xù)請求操作 if (!this.$store.getters.token) { // 彈確認框 this.$dialog.confirm({ title: '溫馨提示', message: '此時需要先登錄才能繼續(xù)操作哦', confirmButtonText: '去登錄', cancelButtonText: '再逛逛' }) .then(() => { // 點擊確認表示用戶要去登錄界面,此時讓路由跳轉 // 如果希望,跳轉到登錄 =》登錄后能回跳回來,需要在跳轉去攜帶參數(shù),(當前的路徑地址) // this.$route.fullPath(會包含查詢參數(shù)) this.$router.replace({ path: '/login', query: { backUrl: this.$route.fullPath } }) }) .catch(() => { // on cancel }) return } console.log('正常請求') }
當用戶點擊去登錄后:頁面會封裝成一個請求參數(shù)到login界面,隨后要在login界面中通過路由的方式去判單是否有回調(diào)url,如果有則登錄成功后回到當前界面。
對于一些后端接口,尤其是用戶權限等,我們需要在請求頭中攜帶token信息,對此在請求攔截器上進行配置
基于vuex管理購物車模塊
說明:購物車 數(shù)據(jù)聯(lián)動關系較多,且通常會封裝一些小組件,所以為了便于維護,一般都會將購物車的數(shù)據(jù)基于vuex分模塊管理
需求分析:
- 基本靜態(tài)結構
- 構建vuex cart模塊,獲取數(shù)據(jù)存儲
- 基于數(shù)據(jù) 動態(tài)渲染 購物車列表
- 封裝getters 實現(xiàn)動態(tài)統(tǒng)計
- 全選反選功能
- 數(shù)字框修改數(shù)量功能
- 編輯切換狀態(tài),刪除功能
- 空購物車處理
對于vuex cart模塊來說:
import { getCartList, updateCart } from '@/api/cart' export default { namespaced: true, state () { return { cartList: [] // 購物車列表 } }, mutations: { // 提供一個修改 cartList 的方法 setCartList (state, payload) { state.cartList = payload }, setCartTotal (state, payload) { state.cartTotal = payload }, // 切換狀態(tài) toggleCheck (state, goodsId) { const goods = state.cartList.find(item => item.goods_id === goodsId) goods.isChecked = !goods.isChecked }, // 全不選或全選 toggleAll (state, isChecked) { state.cartList.forEach(item => { item.isChecked = !isChecked }) }, // 修改列表中的數(shù)量,根據(jù)id修改 changeCount (state, { goodsId, goodsNum }) { const goods = state.cartList.find(item => item.goods_id === goodsId) goods.goods_num = goodsNum } }, actions: { // 獲取購物車列表 async getCartListAction (content) { const { data: { list } } = await getCartList() // 后臺返回的數(shù)據(jù)中,不包含復選框的選中狀態(tài),為了實現(xiàn)將來的的功能 // 需要手動維護數(shù)據(jù),給每一項,添加一個isChecked狀態(tài),(標記當前商品是否選中) list.forEach(item => { item.isChecked = true }) console.log('list', list) // 調(diào)用mutations,實現(xiàn)對state的修改 content.commit('setCartList', list) }, async changeCountAction (content, obj) { const { goodsNum, goodsId, goodsSkuId } = obj // 先本地修改 content.commit('changeCount', { goodsId, goodsNum }) // 在同步到后臺 await updateCart(goodsId, goodsNum, goodsSkuId) } }, getters: { // 求所有的商品累加總數(shù) cartTotal (state) { return state.cartList.reduce((sum, item) => sum + item.goods_num, 0) }, // 選中的商品項 selCartList (state) { return state.cartList.filter(item => item.isChecked) }, // 對于getter來說,可以通過 getters 作為第二個參數(shù)去訪問我們getters中定義的計算屬性 // 選中的總數(shù) selCartCount (state, getters) { return getters.selCartList.reduce((sum, item) => sum + item.goods_num, 0) }, // 選中的總價 selCartPrice (state, getters) { return getters.selCartList.reduce((sum, item) => sum + item.goods_num * item.goods.goods_price_min, 0).toFixed(2) }, // 是否全選 isAllChecked (state, getters) { return state.cartList.every(item => item.isChecked) } } }
對于購物車組件來說:
<script> import CountBox from '@/components/CountBox' import { mapActions, mapGetters, mapState } from 'vuex' export default { name: 'CartPage', components: { CountBox: CountBox }, computed: { ...mapState('cart', ['cartList']), ...mapGetters('cart', ['cartTotal', 'selCartCount', 'selCartPrice', 'selCartList', 'isAllChecked']) }, methods: { ...mapActions('cart', ['getCartListAction']), init () { if (this.$store.getters.token) { // 必須是登錄過的用戶,才能獲取購物車列表 this.getCartListAction() } }, // 切換選中狀態(tài) toggleCheck (goodsId) { this.$store.commit('cart/toggleCheck', goodsId) }, // 全選或反選 toggleAllCheck () { this.$store.commit('cart/toggleAll', this.isAllChecked) }, // 數(shù)字框點擊事件 async changeCount (goodsNum, goodsId, goodsSkuId) { // 調(diào)用 vuex 的action,進行數(shù)量修改 this.$store.dispatch('cart/changeCountAction', { goodsNum, goodsId, goodsSkuId }) } }, data () { return { } }, created () { this.init() } } </script>
到此這篇關于Vue-加入購物車-判斷token添加登錄提示的文章就介紹到這了,更多相關vue登錄token內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue3與pywebview實現(xiàn)獲取本地文件夾的絕對路徑
這篇文章主要為大家詳細介紹了Vue3如何結合pywebview實現(xiàn)獲取本地文件夾的絕對路徑,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2024-11-11vue項目中將element-ui table表格寫成組件的實現(xiàn)代碼
這篇文章主要介紹了vue項目中將element-ui table表格寫成組件的方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-06-06vue中同時監(jiān)聽多個參數(shù)的實現(xiàn)
這篇文章主要介紹了vue中同時監(jiān)聽多個參數(shù)的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue.js獲取數(shù)據(jù)庫數(shù)據(jù)實例代碼
本篇文章主要介紹了vue.js獲取數(shù)據(jù)庫數(shù)據(jù)實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05