Files
kangyuan-mobile/components/mode_switch/mode_switch.vue
T
2026-07-15 14:15:14 +08:00

72 lines
1.8 KiB
Vue

<template>
<view v-if="showSwitch" class="mode-switch" @tap.stop="open">
<view class="mode-switch__text">{{ label }}</view>
<uni-icons type="bottom" size="14" color="#333" />
</view>
</template>
<script setup>
import { computed, onMounted } from 'vue';
import sheep from '@/sheep';
const appStore = sheep.$store('app');
const userStore = sheep.$store('user');
const label = computed(() => (appStore.bizMode === 'merchant' ? '商户' : '用户'));
const hasStoreAccess = (value) => {
if (Array.isArray(value)) {
return value.some((item) => String(item ?? '').trim() !== '');
}
return String(value ?? '')
.split(',')
.some((item) => item.trim() !== '');
};
const showSwitch = computed(() => {
const info = userStore?.userInfo || {};
return hasStoreAccess(info.storeIds) || hasStoreAccess(info.storeId);
});
onMounted(() => {
if (!userStore?.isLogin) return;
if (showSwitch.value) return;
userStore.getInfo();
});
const setMode = (mode) => {
const next = mode === 'merchant' ? 'merchant' : 'user';
if (appStore.bizMode === next) return;
appStore.setBizMode(next);
const url = next === 'merchant' ? '/pages/merchant/pickupList' : '/pages/index/index';
uni.reLaunch({ url });
};
const open = () => {
uni.showActionSheet({
itemList: ['用户', '商户'],
success: ({ tapIndex }) => {
setMode(tapIndex === 1 ? 'merchant' : 'user');
},
fail: () => {},
});
};
</script>
<style scoped lang="scss">
.mode-switch {
display: flex;
align-items: center;
justify-content: flex-end;
padding: 10rpx 18rpx;
border-radius: 100rpx;
background: rgba(255, 255, 255, 0.9);
box-sizing: border-box;
}
.mode-switch__text {
font-size: 26rpx;
color: #333;
margin-right: 8rpx;
line-height: 1;
}
</style>