CustomerAutocomplete.uvue 4.72 KB
<template>
  <view :class="['customer-box',{'is-show':showCustomerBox}]">
    <view class="customer-title">
      客户档案的记录
    </view>
    <scroll-view
    scroll-y
    class="scroll-box"
    :style="scrollBoxStyle"
    >
      <template v-if="loading">
        <view class="loading flex">
          <uni-icons
          type="refreshempty"
          :size="46"
          color="#3773F8"
          class="icon"
          >
          </uni-icons>
        </view>
      </template>
      <template v-else>
        <template v-if="searchList.length">

          <template v-for="(i,index) in searchList"  :key="index">
            <view
            class="item flex"
            hover-class="active"
            @click="chooseItem(i)"
            >
              {{ `${showType == 'tel'?i.ownerPhone:i.ownerName}`}}
            </view>
          </template>
        </template>
        <template v-else>
          <view class="nothing flex">
            <view class="icon">
            </view>
            未匹配到客户信息
          </view>
        </template>
      </template>
    </scroll-view>
  </view>
</template>

<script setup lang="uts">
  import { storage } from '@/utils/storage.uts'
  import { state } from '@/store/index.uts'
  import { getCustomerInfoListApi } from '@/api/customer-api.uts'
type CustomerInfo = UTSJSONObject

type UserInfo = UTSJSONObject

type SearchParams = {
  merchantId: string
  bussinessScene: number
  ownerPhone?: string
  ownerName?: string
}

  const props = defineProps({
      searchValue: String,
      showType: String,
      show: Boolean
    })

  const emit = defineEmits(['choose'])

  const loading = ref<boolean>(true)
  const searchList = ref<Array<CustomerInfo>>([])

  const showCustomerBox = computed<boolean>(() => {
      const searchValue = props.searchValue ?? ''
      return props.show == true && (state.user?.haveCustomerAccess ?? false) && (searchValue !== '' && searchValue !== null)
    })

  // ✅ UVUE 不支持 calc(),动态计算 scroll-box 高度
  const scrollBoxStyle = computed<UTSJSONObject>(() => {
    // 原样式: height: calc(100% - 64rpx)
    // 64rpx ≈ 32px (rpx 是响应式像素,通常 2rpx = 1px)
    // 使用百分比近似:95%
    return {
      "height": "95%"
    } as UTSJSONObject;
  });
  async function getCustomerInfoList(val: string, type: string): Promise<void> {
    try {
      loading.value = true
      const userInfo: UserInfo | null = storage.get("userInfo") as UTSJSONObject | null
      const userInfoObj = userInfo ?? { shopMerchantId: '', sceneType: 0 }
      const shopMerchantId = userInfoObj.shopMerchantId
      const params: SearchParams = {
        merchantId: shopMerchantId as String,
        bussinessScene: userInfoObj.sceneType == 0 ? 1 : 2
      }

      if (type == 'tel') {
        params.ownerPhone= val
      } else {
        params.ownerName = val
      }

      const result = await getCustomerInfoListApi(params as UTSJSONObject)
      searchList.value = result as UTSArray<UTSJSONObject>
      loading.value = false
    } catch (error) {
      loading.value = false
    }
  }
  watch(showCustomerBox.value , (newVal: boolean) => {
      if (newVal == true) {
        getCustomerInfoList(props.searchValue ?? '', props.showType ?? '')
      }
    }, { immediate: true })



  function chooseItem(item: CustomerInfo): void {
    emit('choose', item)
  }
</script>

<style scoped lang='scss'>
  .customer-box {
    position: absolute;
    left: 0;
    top: 88rpx;
    width: 100%;
    height: 352rpx;
    background: #ffffff;
    border-radius: 16rpx;
    z-index: -1;
    opacity: 0;

    .customer-title {
      font-size: 24rpx;
      color: #999999;
      line-height: 40rpx;
      padding: 16rpx 24rpx 8rpx;
    }

    .scroll-box {
      // ✅ UVUE 不支持 calc(),高度由 JS 动态计算
      height: 95%;

      .loading {
        width: 100%;
        height: 100%;
        justify-content: center;

        .icon {
          // ✅ UVUE 不支持 animation,已移除旋转动画
        }
      }

      .nothing {
        width: 100%;
        height: 100%;
        font-size: 24rpx;
        color: #c7ccd7;
        flex-direction: column;

        .icon {
          margin-top: 52rpx;
          width: 96rpx;
          height: 96rpx;
          /* background-image: url('@/assets/images/common/nothing.webp'); */
          background-size: 100%;
        }
      }

      .item {
        width: 100%;
        height: 48rpx;
        font-size: 28rpx;
        color: #333333;
        line-height: 40rpx;
        padding-left: 24rpx;
        margin-bottom: 8rpx;
      }

      .active {
        background: #ecf1ff;
      }

      // ✅ UVUE 不支持 @keyframes,已移除 rotate-loading 动画
    }
  }

  .is-show {
    z-index: 1;
    opacity: 1;
  }
</style>