search-customer.vue 4.45 KB
<template>
  <Page name="search-customer">
    <template #header>
      <search-bar v-model="searchForm.name" placeholder="请输入搜索关键词" auto @confirm="onSearch" />
    </template>
    <template #content>
      <List ref="list" v-model="list" :api="searchAPI" :addOption='!appId && addOption'>
        <template v-for="(item, index) in list">
          <zui-cell :key="index" @click="onSelect(item, index)">
            <template #title>
              <text>{{ item.name || item.customerName }}</text>
              <view class="ml" v-if="mode === 'collect' && item.parentCustomerName" style="display: inline-flex">
                <zui-tag type="ghost" color="#8c8c8c">{{ item.parentCustomerName }}</zui-tag>
              </view>
            </template>
            <template #right>
              <view class="collect" v-if="mode === 'collect'" hover-class="active" hover-start-time="50" hover-stay-time="70">
                <zui-icon :name="item.collectFlag ? 'favorite' : 'favoriteoutline'"></zui-icon>
                <text>{{ `${item.collectFlag ? '已' : ''}收藏` }}</text>
              </view>
              <zui-tag v-else-if="['collect', 'select'].indexOf(mode) > -1 && item.parentCustomerName" type="ghost" color="#8c8c8c">
                <text>{{ item.parentCustomerName }}</text>
              </zui-tag>
            </template>
          </zui-cell>
        </template>
      </List>
    </template>
  </Page>
</template>

<script>
export default {
  data() {
    return {
      searchForm: {
        name: null,
        topCustomerFlag: false
      },
      list: [],
      mode: 'search',
      appId: '',
    }
  },
  onLoad(option) {
    if (option.mode) this.mode = option.mode;
    if (option.appId) this.appId = option.appId || '';
    if (option.mode === 'collect') {
      uni.setNavigationBarTitle({ title: '收藏客户' });
    } else if (option.mode === 'select') {
      uni.setNavigationBarTitle({ title: '选择客户' });
    }
    if (option.topCustomerFlag !== '') {
      this.searchForm.topCustomerFlag = option.topCustomerFlag;
    }
  },
  methods: {
    searchAPI(params) {
      if (this.appId) {
        return uni.$u.api.filter.getBelongCustomer({...params, ...this.searchForm, appId: this.appId});
      }
      return uni.$u.api.filter.getCommonSelect({ ...params, ...this.searchForm });
    },
    addOption(list){
      let obj = {
        code: '',
        name: '不限',
      };
      let hash = {};
      let options = [obj, ...list].reduce((result, item) => {
        let hashKey = `${item.code}` || '_empty';
        if (!hash[hashKey]) {
          hash[hashKey] = true;
          if (item.name) {
            result.push(item);
          }
        }
        return result; // 返回结果数组
      }, []);
      this.$emit('input', options);
    },
    onSelect(item, index) {
      const $params = this.$params;
      if ($params.mode === 'select') {
        uni.$emit('select-customer', item.name == '不限' && !item.code ? { code: '', name: '' } : item);
        uni.navigateBack();
      } else if ($params.mode === 'collect') {
        if (item.collectFlag === true) {
          uni.showModal({
            title: '提示',
            content: '确定要取消收藏此客户吗?',
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            success: (res) => {
              if (res.confirm) {
                this.$request.post({
                  url: '/customerCollect/cancelCustomerCollect',
                  data: { customerCode: item.code }
                }).then(() => {
                  uni.showToast({ title: '已取消收藏', icon: 'none' });
                  this.$set(this.list, index, { ...this.list[index], collectFlag: false });
                });
              }
            }
          });
        } else {
          this.$request.post({
            url: '/customerCollect/addCustomerCollect',
            data: [item.code]
          }).then(() => {
            uni.showToast({ title: '收藏成功', icon: 'none' });
            this.$set(this.list, index, { ...this.list[index], collectFlag: true });
          });
        }
      }
    }
  }
}
</script>

<style lang="scss">
.page-search-customer {
  .collect {
    display: flex;
    align-items: center;
    color: $color-text-minor;
    .zui-icon {
      font-size: $font-lg;
      color: $color-red;
      padding: 0 $padding-sm;
    }
    &.active {
      .zui-icon {
        color: lighten($color-red, 15);
      }
    }
    font-size: $font-md;
  }
}
</style>