Employee.uvue 7.89 KB
<template>
    <view>
        <un-popup :show="visible"
                  mode="bottom"
                  :round="20"
                  :z-index="dynamicZIndex"
                  @close="handlePopupClose">
            <view class="popup-wrapper" :style="{ height: windowHeight * 0.6 + 'px' }">
                <view class="popup-header">
                    <view class="icon-close-box" @click="handleHide">
                        <uni-icons color="#1A1A1A" :size="18" type="closeempty"></uni-icons>
                    </view>
                    <text class="popup-title">请选择{{ title }}</text>
                </view>
                <scroll-view class="select-box"
                             direction="vertical"
                             :show-scrollbar="false"
                             :bounces="false"
                             :scroll-with-animation="false"
                             scroll-y>
                    <view v-if="employeeList.length == 0" style="padding: 40px 20px; text-align: center;">
                        <text style="color: #999; font-size: 14px;">暂无销售人员数据</text>
                    </view>
                    <view class="view-content">
                        <template v-for="(item, index) in employeeList" :key="index">
                            <view class="select-item"
                                  :class="[{ 'is-active': (salesUuid as string) == (item as UTSJSONObject).uuid }]"
                                  @click="handleSelect(item)">
                                <text class="select-item-text" :class="[{ 'is-active-text': (salesUuid as string) == (item as UTSJSONObject).uuid }]">{{ (item as UTSJSONObject).staffName }}</text>
                            </view>
                        </template>
                    </view>
                </scroll-view>
            </view>
        </un-popup>
    </view>
</template>

<script setup lang="uts">
import { ComponentPublicInstance, computed, onUnmounted, ref, watch } from "vue";

import { getEmployeeListApi } from "@/api/common-api.uts";
// import { resolutionState } from "@/services/adapter/index.uts";
import { dispatch, state } from "@/store/index.uts";
import { storage } from "@/utils/storage.uts";
import { useWindowHeight } from "@/services/adapter/hooks/index.uts";

// 使用统一的 windowHeight hook
const windowHeight = useWindowHeight();
type Props = {
	salesPropsUuid?: string
	title?: string
}

type UserInfo = UTSJSONObject

const props = withDefaults(defineProps<Props>(), {
	title: "销售",
	salesPropsUuid: ""
});

const emit = defineEmits(["close", "select"]);

let componentDisposed = false;
onUnmounted((): void => {
	componentDisposed = true;
});

const visible = ref<boolean>(false);

const watchStatus = computed<boolean>(() => {
	const status = state.user?.historyStatus;
	if (status == null) {
		return false;
	} else if (status == 1) {
		return true;
	} else {
		return false;
	}
});

const updateView = ref<number>(Date.now());

const salesUuid = computed<string>(() => {
	const val1 = state.user.customerInfo.salesUuid;
	const val2 = props.salesPropsUuid;
	return val1 !== null && val1 !== "" ? (val1 as string) : val2;
});
// 动态计算 z-index,确保始终在最上层(高于 ToolBox 的 10073)
const dynamicZIndex = computed<number>(() => {
	return 10090;
});
// const windowHeight = computed<number>(() => {
// 	const height = resolutionState.windowHeight;
// 	return height > 0 ? height : uni.getWindowInfo().windowHeight;
// });

const employeeList = ref<Array<any>>([]);

/** 使用 `string` 字面量键;`String` 类型在桥接时可能异常;缓存可能是 JSON 字符串或数组 */
const STORAGE_KEY_EMPLOYEE_LIST = "employeeList";

function readEmployeeListFromStorage(): Array<any> {
	const raw = storage.get(STORAGE_KEY_EMPLOYEE_LIST) as any | null;
	if (raw == null) {
		return [] as any[];
	}
	if (Array.isArray(raw)) {
		return raw as Array<any>;
	}
	if (typeof raw === "string") {
		const s = (raw as string).trim();
		if (s === "") {
			return [] as any[];
		}
		try {
			const parsed = JSON.parse(s) as any;
			return Array.isArray(parsed) ? (parsed as Array<any>) : ([] as any[]);
		} catch (_e) {
			return [] as any[];
		}
	}
	return [] as any[];
}

watch(updateView, () => {
	console.log("updateView triggered, reading from storage");
	const list = readEmployeeListFromStorage();
	console.log("storage employeeList length:", list.length);
	employeeList.value = list;
	console.log("employeeList.value updated, length:", employeeList.value.length);
}, { immediate: true });

function close(): void {
	visible.value = false;
}

watch(watchStatus, () => {
	close();
}, { immediate: true });

function handleClose(): void {
	emit("close");
}

function handlePopupClose(): void {
	visible.value = false;
	emit("close");
}

function handleHide(): void {
	visible.value = false;
}

function open(): void {
	visible.value = true;
	const userInfo = storage.getParse("userInfo") as UserInfo;
	const shopMerchantIdRaw = userInfo["shopMerchantId"] as string | null;
	const shopMerchantId = shopMerchantIdRaw != null ? shopMerchantIdRaw : "";
	if (shopMerchantId === "") {
		return;
	}

	// 获取销售人员列表
	getEmployeeListApi({ merchantId: shopMerchantId }).then((res: any) => {
		if (componentDisposed) {
			return;
		}
		console.log("getEmployeeListApi response:", res);
		const list = res as Array<any>;
		console.log("list length:", list != null ? list.length : 0);
		dispatch("setEmployeeList", list);
		const listToStore = list != null ? list : [];
		console.log("Saving to storage, length:", listToStore.length);
		storage.set(STORAGE_KEY_EMPLOYEE_LIST, listToStore, 0);
		console.log("Triggering updateView");
		updateView.value = Date.now();
		console.log("updateView triggered with:", updateView.value);
	});
}

function handleSelect(item: any): void {
	close();
	emit("select", item);
}

defineExpose({ open, close });
</script>

<style scoped lang="scss">
  .popup-wrapper {
    background: #ffffff;
    border-radius: 20px 20px 0 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .popup-header {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 56px;
    flex-shrink: 0;
    border-bottom: 1px solid #f0f0f0;
  }

  .popup-title {
    font-size: 18px;
    font-weight: 600;
    color: #292d33;
  }

  .icon-close-box {
    position: absolute;
    right: 16px;
    top: 50%;
    transform: translateY(-50%);
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .select-box {
    flex: 1;
    min-height: 0;
    position: relative;
    display: flex;
    flex-direction: column;
  }

  .view-content {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: space-between;
    width: 100%;
    padding: 16px 24px 24px;
    box-sizing: border-box;
    // ✅ UVUE 不支持 min-height: 100%,移除后由 flex 布局自适应
  }

  .select-item {
    width: 48%; /* 预留间距,避免49%引起的意外换行 */
    margin-bottom: 12px;
    height: 44px; /* 加高触控面积,显得更大气 */
    background-color: #f8f9fa; /* 低调的浅灰背景 */
    border-radius: 8px; /* 圆角增加现代感 */
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    border: 1px solid transparent;
    transition: background-color 0.2s ease, border-color 0.2s ease;
  }

  /* 悬浮反馈 */
  .select-item:active {
    background-color: #f1f3f5;
  }

  /* 选中状态 */
  .is-active {
    background-color: rgba(22, 119, 255, 0.05); /* 淡淡的主题色背景 */
    border: 1px solid rgba(22, 119, 255, 0.3); /* 主题色描边 */
  }

  .select-item-text {
    text-align: center;
    color: #495057; /* 稳重的深灰色 */
    font-size: 15px;
    line-height: 20px;
    font-weight: 500;
  }

  .is-active-text {
    color: #1677FF; /* 选中时的文字色与边框呼应 */
    font-weight: 600;
  }
</style>