Employee.vue 4.05 KB
<template>
  <MessageBox ref="employeeBoxVisible" position="bottom" :isFooter="false" :title="`请选择${title}`" :maxHeight="1000" @close="$emit('close')">
    <view class="select-box">
      <view class="select-item" :class="{ checked: selectedId == item[valueKey] }" @click="handleSelect(item)" v-for="(item, index) in displayList" :key="index">
        {{ item[labelKey] }}
      </view>
    </view>
  </MessageBox>
</template>

<script type="text/ecmascript-6">
export default {
  name: 'Employee',
  props: {
    title: {
      type: String,
      default: '销售'
    },
    salesPropsUuid: {
      type: String,
      default: ''
    },
    list: {
      type: Array,
      default: null
    },
    labelKey: {
      type: String,
      default: 'staffName'
    },
    valueKey: {
      type: String,
      default: 'uuid'
    },
    // default: 销售顾问;seller: 转化人员/驻店零售
    employeeType: {
      type: String,
      default: 'default'
    }
  },
  data () {
    return {
      sellerEmployeeList: [],
      defaultEmployeeList: []
    }
  },
  watch: {
    watchStatus: {
      immediate: true,
      handler (newval) {
        this.close()
      }
    },
  },
  computed: {
    watchStatus () {
      return this.$store.state.user.historyStatus
    },
    selectedId () {
      if (Array.isArray(this.list) || this.employeeType === 'seller' || this.employeeType === 'default') {
        return this.salesPropsUuid
      }
      return this.$store.state.user.customerInfo.salesUuid || this.salesPropsUuid
    },
    displayList () {
      if (Array.isArray(this.list)) {
        return this.list
      }
      if (this.employeeType === 'seller') {
        return this.sellerEmployeeList
      }
      if (this.employeeType === 'default') {
        return this.defaultEmployeeList
      }
      return this.$store.state.user.employeeList
    }
  },
  mounted () {
  },
  methods: {
    async open () {
      if (Array.isArray(this.list)) {
        this.$refs.employeeBoxVisible.open()
        return
      }
      uni.showLoading({
        title: '加载中',
        mask: true
      })
      await this.ensureEmployeeList()
      uni.hideLoading()
      this.$refs.employeeBoxVisible.open()
    },
    async ensureEmployeeList () {
      const userInfo = uni.$storage.get('userInfo') || {}
      if (this.employeeType === 'seller') {
        if (this.sellerEmployeeList.length) return
        const { merchantId } = userInfo
        const res = await uni.$api.popupApi.getEmployeeList({
          merchantId,
          sceneList: ''
        })
        this.sellerEmployeeList = res || []
        return
      }
      if (this.employeeType === 'default') {
        if (this.defaultEmployeeList.length) return
        const { shopMerchantId, sceneType } = userInfo
        const apiTargetName = sceneType == 1 ? 'getEmployeeList' : 'getOrderScreenEmployeeList'
        const res = await uni.$api.popupApi[apiTargetName]({
          merchantId: shopMerchantId,
          sceneList: [sceneType]
        })
        this.defaultEmployeeList = res || []
        return
      }
      const employeeList = this.$store.state.user.employeeList
      if (employeeList && employeeList.length) return
      const { shopMerchantId } = userInfo
      const res = await uni.$api.commonApi.getEmployeeList({
        merchantId: shopMerchantId
      })
      this.$store.dispatch('setEmployeeList', res)
      uni.$storage.set('employeeList', res || [])
    },
    handleSelect (item) {
      this.close();
      this.$emit('select', item)
    },
    close () {
      this.$refs?.employeeBoxVisible?.close()
    }
  }
}
</script>

<style scoped lang="scss">
.select-box {
  margin: toRpx(40) 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: toRpx(32);
  overflow-y: scroll;
  max-height: 50vh;
  padding-bottom: toRpx(60);
  .select-item {
    text-align: center;
    height: toRpx(68);
    line-height: toRpx(44);
    opacity: 1;
    border: toRpx(2) solid #dce0e6;
    color: #333;
    font-size: toRpx(32);
    border-radius: toRpx(8);
    &.checked {
      border: toRpx(2) solid #292d33;
    }
  }
}
</style>