diff --git a/constants/index.js b/constants/index.js index 1df4ae8..b8fd031 100644 --- a/constants/index.js +++ b/constants/index.js @@ -1,9 +1,17 @@ +/* + * @Author: ch + * @Date: 2022-05-17 18:17:00 + * @LastEditors: ch + * @LastEditTime: 2022-06-09 18:50:01 + * @Description: file content + */ /** * 全局常量,请避免使用魔法数字 */ // 用户凭证名 const TOKEN_KEY = "msbPcToken"; +const UUID_KEY = "msbPcUuid"; // 订单状态 const ORDER_STATUS = { @@ -31,4 +39,4 @@ const SECKILL_STATUS = { GOING: 2, // 进行中 }; -export { TOKEN_KEY, ORDER_STATUS, SEX_TYPE, CATEGROY_LEVEL, SECKILL_STATUS }; +export { TOKEN_KEY, UUID_KEY, ORDER_STATUS, SEX_TYPE, CATEGROY_LEVEL, SECKILL_STATUS }; diff --git a/plugins/axios.js b/plugins/axios.js index 054eaca..6d7abe5 100644 --- a/plugins/axios.js +++ b/plugins/axios.js @@ -1,24 +1,25 @@ + + /* * @Author: ch * @Date: 2022-05-03 23:04:45 * @LastEditors: ch - * @LastEditTime: 2022-05-25 17:48:31 + * @LastEditTime: 2022-06-09 18:54:57 * @Description: file content */ +import { creatUuid } from "@/plugins/utils"; let axios = null -export default function({app, $axios, req}) { - // if(req){ - // let str = 'Safari' - // if(req.headers['user-agent'].includes('Chrome')){ - // str = 'Chrome' - // } - // console.log(str, req.headers['cookie'].split('token')[1]); - // } - -// export default function ({app, $axios}) { - // $axios.onRequest(()=>{ +export default function ({ app, $axios, store, req }) { + let uuid = store.state.uuid; + if (!uuid) { + uuid = creatUuid(16, 2); + store.commit('setUUID', uuid); + } - // }) + $axiosTk.onRequest( config =>{ + config.headers.uid = uuid; + return config; + }); $axios.onResponse(response => { const result = response.data; diff --git a/plugins/axiosTk.js b/plugins/axiosTk.js index 0c89bbb..1c46a54 100644 --- a/plugins/axiosTk.js +++ b/plugins/axiosTk.js @@ -2,12 +2,19 @@ * @Author: ch * @Date: 2022-05-04 17:11:07 * @LastEditors: ch - * @LastEditTime: 2022-05-25 17:48:35 + * @LastEditTime: 2022-06-09 18:55:23 * @Description: file content */ +import { creatUuid } from "@/plugins/utils"; let axiosTk = null; export default function ({$axios, store, route}, inject) { const $axiosTk = $axios.create(); + let uuid = store.state.uuid; + if (!uuid) { + uuid = creatUuid(16, 2); + store.commit('setUUID', uuid); + } + config.headers.uid = uuid; $axiosTk.defaults.timeout = 3000; $axiosTk.onRequest( config =>{ diff --git a/store/index.js b/store/index.js index a655789..6fca0b7 100644 --- a/store/index.js +++ b/store/index.js @@ -2,10 +2,10 @@ * @Author: ch * @Date: 2022-05-04 20:35:20 * @LastEditors: ch - * @LastEditTime: 2022-05-07 22:33:28 + * @LastEditTime: 2022-06-09 18:50:16 * @Description: file content */ -import { TOKEN_KEY } from "@/constants"; +import { TOKEN_KEY,UUID_KEY } from "@/constants"; import { ApiGetCurrentUser, ApiPostLogout } from "@/plugins/api/account"; import { ApiGetCartList } from "@/plugins/api/cart"; import { ApiGetCategoryOneList } from "@/plugins/api/goods"; @@ -13,6 +13,7 @@ const ONE_DAY = 86400000; // 一天的毫秒数 24 * 60 * 60 * 1000; const state = () => ({ token: "", + uuid : '', userInfo: {}, loginVisible: false, // 是否展示登录弹窗 seckillTabVisible: false, // 公共头是否展示秒杀tab @@ -25,6 +26,14 @@ const mutations = { setUserInfo(state, info) { state.userInfo = info; }, + + setUUID(state, uuid) { + state.uuid = uuid; + this.$cookies.set(UUID_KEY, uuid, { + path: "/", + maxAge: ONE_DAY, + }); + }, setToken(state, token) { state.token = token; this.$cookies.set(TOKEN_KEY, token, {