Browse Source

feat(store/tabbar): 重构购物车计数逻辑并添加底部导航栏尺寸存储

- 将购物车数量计算改为基于购物车列表长度
- 添加底部导航栏高度和项目宽度的存储
- 移除不再使用的结算商品相关代码
- 更新登录页面逻辑,简化系统信息获取方式
master
wei 6 days ago
parent
commit
6912766be4
  1. 20
      components/custom-tab-bar/my-tab-bar.vue
  2. 45
      pages/login/login.vue
  3. 51
      store/index.js

20
components/custom-tab-bar/my-tab-bar.vue

@ -1,5 +1,7 @@
<script setup>
import { computed, ref } from "vue";
import { onReady } from "@dcloudio/uni-app";
import { computed, getCurrentInstance, ref } from "vue";
import { useRect } from "@/libs/utils";
import useStore from "@/store";
const props = defineProps({
@ -7,6 +9,7 @@ const props = defineProps({
});
const store = useStore();
const instance = getCurrentInstance();
/**
* 底部导航栏下标
@ -23,7 +26,7 @@ const activeColor = "#06CA64";
/**
* 购物车数量
*/
const cartCount = computed(() => store.count);
const cartCount = computed(() => store.cartList.length);
/**
* 底部导航栏标签信息
@ -37,7 +40,7 @@ const tabList = ref([
badgeCount: 0,
},
{
pagePath: "/pages/classification/classification",
pagePath: "/pages/allDish/allDish",
text: "全部菜品",
iconPath: "/static/tabbar/alls.png",
selectedIconPath: "/static/tabbar/alls-selected.png",
@ -75,6 +78,15 @@ function onChangeTab(index) {
url: tabList.value[index].pagePath,
});
}
onReady(() => {
useRect(".tab-bar", instance.proxy).then((rect) => {
store.tabbarBottomHeight = rect?.height || 0;
});
useRect(".tab-bar-item", instance.proxy).then((rect) => {
store.tabbarItemWidth = rect?.width || 0;
});
});
</script>
<template>
@ -104,7 +116,7 @@ function onChangeTab(index) {
</view>
</template>
<style lang="scss" scoped>
<style lang="scss">
@import '../../uni.scss';
.tab-bar {

45
pages/login/login.vue

@ -12,7 +12,7 @@ import { gotoBack, validates } from "@/libs/utils";
import config from "@/libs/utils/config";
import { encryptData } from "@/libs/utils/crypto";
const appData = getApp().globalData;
// const appData = getApp().globalData;
/**
* 判断小程序的API回调参数组件等是否在当前版本可用
*/
@ -105,8 +105,11 @@ function wxLogin() {
async success(res) {
const res2 = await xcxWxLoginApi({ code: res.code });
if (res2.code === "0") {
openid.value = res2.openid;
openid.value = res2.data.openid;
uni.setStorageSync("openid", openid.value);
// FIXME: session_key
// uni.setStorageSync("session", res2.data.session_key);
getUserByXcxAPPOpenId({ openid: openid.value });
}
else {
@ -121,21 +124,24 @@ function wxLogin() {
* 根据用户openid获取session
*/
async function getUserByXcxAPPOpenId(data) {
appData.header = {
"content-type": "",
"header": "",
"version": "2.0",
};
// appData.header = {
// "content-type": "",
// "header": "",
// "version": "2.0",
// };
uni.showLoading({ title: "登录中..." });
const res = await getUserByXcxAPPOpenIdApi(data);
uni.hideLoading();
if (res.code === "0") {
appData.header.session = res.header.session;
appData.header["X-Header-Token"] = res.header.session;
uni.setStorageSync("session", res.header.session);
uni.hideLoading();
// uni.setStorageSync("X-Header-Token", res.header.session);
// appData.header.session = res.header.session;
// appData.header["X-Header-Token"] = res.header.session;
// uni.setStorageSync("session", res.header.session);
getmyarea();
}
else {
uni.hideLoading();
updateUserInfo(res);
}
}
@ -143,9 +149,9 @@ async function getUserByXcxAPPOpenId(data) {
async function getmyarea() {
const res = await getmyareaApi({ warehouseid: "", isEnabled: 1 });
if (res.code === "0") {
const list = res.data.list.sort((pre, cur) => Number(cur) - Number(pre));
// const list = res.data.list.sort((pre, cur) => Number(cur) - Number(pre));
// each isLastDefaultAddr
const each = list.find(each => each.isLastDefaultAddr) || list[0];
const each = res.data.list.find(each => each.isLastDefaultAddr) || res.data.list[0];
uni.setStorageSync("warehousId", each.warehousId);
uni.setStorageSync("addressId", each.addrId);
// selectAddress.value = list[0];
@ -249,11 +255,14 @@ onLoad(() => {
if (uni.getUserProfile) {
canIUseGetUserProfile.value = true;
}
uni.getSystemInfo({
success(res) {
isIos.value = res.platform === "ios";
},
});
isIos.value = uni.getAppBaseInfo().uniPlatform === "ios";
// uni.getSystemInfo({
// success(res) {
// isIos.value = res.platform === "ios";
// },
// });
});
</script>

51
store/index.js

@ -6,17 +6,18 @@ import { ref } from "vue";
* @property {string} count - 购物车数量
* @property {string} searchValue - 搜索内容
* @property {string} homeTypeIndex - 首页点击的分类下标
* @property {string} goodsCheckedItems - 结算商品
* @property {string} cartList - 购物车列表
* @property {string} productTypeId - 商品分类id
* @property {string} showVoice - 是否显示语音弹窗
* @property {string} tabbarBottomHeight - 底部导航高度
* @property {string} tabbarItemWidth - 底部导航每个的宽度
* @property {method} increment - 增加购物车角标数量
* @property {method} getCartListSize - 获取购物车列表数量
* @property {method} changeCartList - 修改购物车列表
* @property {method} changeShowVoice - 修改语音弹窗是否显示
* @property {method} changeCount - 修改购物车数量
* @property {method} changeSearchValue - 修改搜索内容
* @property {method} clearSearchValue - 清空搜索值
* @property {method} changeHomeType - 首页点击分类
* @property {method} setGoodsCheckedItems - 设置去结算商品
*/
export default defineStore("store",
@ -36,18 +37,30 @@ export default defineStore("store",
* 首页点击的分类下标
*/
const homeTypeIndex = ref(-1);
/**
* 结算商品
*/
const goodsCheckedItems = ref([]);
// /**
// * 结算商品
// */
// const goodsCheckedItems = ref([]);
/**
* 购物车列表
*/
const cartList = ref([]);
/**
* 商品分类id
*/
const productTypeId = ref("");
/**
* 是否显示语音弹窗
*/
const showVoice = ref(false);
/**
* 底部导航高度
*/
const tabbarBottomHeight = ref(76);
/**
* 底部导航每个的宽度
*/
const tabbarItemWidth = ref(82);
/**
* 修改购物车数量
@ -77,18 +90,24 @@ export default defineStore("store",
homeTypeIndex.value = info;
}
/**
* 设置去结算商品
*/
function setGoodsCheckedItems(info) {
goodsCheckedItems.value = info;
}
// /**
// * 设置去结算商品
// */
// function setGoodsCheckedItems(info) {
// goodsCheckedItems.value = info;
// }
// function getGoodsCheckedItemsSize() {
// return .value.length;
// }
/**
* 修改购物车列表
*/
function changeCartList(info) {
cartList.value = info;
}
function getCartListSize() {
return cartList.value.length;
}
/**
* 语音弹窗是否显示
*/
@ -100,15 +119,17 @@ export default defineStore("store",
count,
searchValue,
homeTypeIndex,
goodsCheckedItems,
cartList,
productTypeId,
showVoice,
tabbarBottomHeight,
tabbarItemWidth,
increment,
changeSearchValue,
clearSearchValue,
changeHomeType,
setGoodsCheckedItems,
changeCartList,
getCartListSize,
changeShowVoice,
};
});
Loading…
Cancel
Save