selectStore.vue 7.18 KB
<template>
  <view class="layout">
    <!-- 标题 -->
    <view class="header">
      <view class="title">选择门店</view>
    </view>

    <!-- 搜索框 -->
    <view class="search-box">
      <input
        class="search-input"
        type="text"
        v-model="searchKeyword"
        placeholder="请输入门店名称/门店编码搜索"
        placeholder-class="search-placeholder"
      />
      <view class="search-icon">
        <uni-icons type="search" size="18" color="#999"></uni-icons>
      </view>
    </view>

    <!-- 门店列表 -->
    <view class="store-list">
      <view
        v-for="(item, index) in filteredStores"
        :key="item.id || index"
        class="store-item"
        :class="{ active: selectedStoreCode === item.storeCode }"
        @click="selectStore(item)"
      >
        <view class="store-icon">
          <view class="sprite-icon icon-store"></view>
        </view>
        <view class="store-info">
          <view class="store-name">{{ item.storeName }}</view>
          <view class="store-code">门店编码:{{ item.spCode || item.storeCode }}</view>
        </view>
        <view class="selected-icon" v-if="selectedStoreCode === item.storeCode">
          <uni-icons type="checkbox-filled" size="24" color="#2C66F7"></uni-icons>
        </view>
      </view>
    </view>
  </view>
</template>

<script type="text/ecmascript-6">
export default {
  data () {
    return {
      stores: [],
      selectedStoreIndex: -1,
      selectedStoreCode: '',
      loginForm: {},
      searchKeyword: ''
    };
  },
  computed: {
    filteredStores () {
      if (!this.searchKeyword) {
        return this.stores;
      }
      const keyword = this.searchKeyword.toLowerCase();
      return this.stores.filter(item => {
        const name = (item.storeName || '').toLowerCase();
        const code = (item.spCode || item.storeCode || '').toLowerCase();
        return name.includes(keyword) || code.includes(keyword);
      });
    }
  },
  onLoad (options) {
    // 从参数获取 stores
    if (options.stores) {
      this.stores = JSON.parse(decodeURIComponent(options.stores));
    }
    // 从 storage 获取登录信息
    const loginForm = uni.getStorageSync('loginForm') || {};
    this.loginForm = loginForm;

    console.log('selectStore onLoad - stores:', this.stores);
    console.log('selectStore onLoad - loginForm:', this.loginForm);

    // 如果只有一个门店,直接选中并确认
    if (this.stores.length === 1) {
      this.selectedStoreCode = this.stores[0].storeCode;
      this.confirmSelect();
    }
  },
  methods: {
    selectStore (item) {
      this.selectedStoreCode = item.storeCode;
      this.confirmSelect();
    },
    confirmSelect () {
      if (!this.selectedStoreCode) {
        uni.showToast({
          title: '请选择门店',
          icon: 'none'
        });
        return;
      }

      // 从 filteredStores 中找到选中的门店
      const selectedStore = this.filteredStores.find(item => item.storeCode === this.selectedStoreCode);
      const storeCode = selectedStore.storeCode;

      uni.showLoading({
        mask: true
      });

      // 调用 confirmLoginStore 确认门店
      uni.$api.loginApi.confirmLoginStore({
        username: this.loginForm.username,
        password: this.loginForm.password,
        spCode: this.loginForm.spCode,
        storeCode
      }).then(async res => {
        if (!res || !res.data) {
          return Promise.reject('确认门店失败');
        }

        const token = res.data.token || res.data.sessionId || res.data.access_token;
        if (!token) return Promise.reject('登录失败:未获取到 token');

        // 存储最终 token
        uni.$storage.set('sessionId', token);

        // 存储用户信息(从 confirmLoginStore 返回的 loginUser 对象)
        const userInfo = res.data.loginUser || {};
        uni.$storage.set('userInfo', userInfo);

        // 存储门店信息到缓存
        uni.$storage.set('storeInfo', {
          storeCode: selectedStore.storeCode,
          storeName: selectedStore.storeName
        });

        // 如果勾选了 30 天免登,调用 saveLoginInfo 保存登录信息
        if (this.loginForm.noSignIn) {
          // 存储 noSignInUuid,用于 401 时恢复登录
          uni.$storage.set('noSignInUuid', this.loginForm.username);
          try {
            await uni.$api.loginApi.saveLoginInfo({});
          } catch (e) {
            uni.$logger.local.error(`saveLoginInfo 失败:${e?.message || e}`);
          }
        }

        // 清理临时登录信息
        uni.removeStorageSync('loginForm');

        uni.$logger.local.info(`${this.loginForm.username}登录成功`);

        // 跳转至首页
        uni.hideLoading();
        uni.reLaunch({
          url: '/pages/equity/index'
        });
      }).catch(error => {
        uni.$logger.local.error(`确认门店失败:${error?.message || error}`);
        uni.hideLoading();
        uni.showToast({
          title: error?.message || error || '确认失败',
          icon: 'none'
        });
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.layout {
  min-height: 100vh;
  background: linear-gradient(180deg, #F5F7FA 0%, #E8EEF7 100%);
  padding: toRpx(40);
  box-sizing: border-box;
}

/* 标题 */
.header {
  padding: toRpx(40) 0 toRpx(30);
  text-align: center;

  .title {
    font-size: toRpx(36);
    font-weight: 600;
    color: #333;
  }
}

/* 搜索框 */
.search-box {
  position: relative;
  background: #fff;
  border-radius: toRpx(56);
  padding: toRpx(24) toRpx(32);
  margin-bottom: toRpx(40);
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);

  .search-input {
    width: 100%;
    font-size: toRpx(28);
    color: #333;
  }

  .search-placeholder {
    color: #999;
  }

  .search-icon {
    position: absolute;
    right: toRpx(32);
    top: 50%;
    transform: translateY(-50%);
  }
}

/* 门店列表 */
.store-list {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: toRpx(24);
  padding-bottom: toRpx(40);

  .store-item {
    background: #fff;
    border-radius: toRpx(24);
    padding: toRpx(32);
    display: flex;
    align-items: center;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
    transition: all 0.2s ease;
    position: relative;

    &.active {
      border: 2rpx solid #2C66F7;
      box-shadow: 0 4rpx 20rpx rgba(44, 102, 247, 0.15);
    }

    .store-icon {
      background: linear-gradient(135deg, #E8EEF7 0%, #D0D9F5 100%);
      border-radius: toRpx(16);
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: toRpx(20);
      flex-shrink: 0;

      .sprite-icon {
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }

    .store-info {
      flex: 1;
      min-width: 0;
      display: flex;
      flex-direction: column;
      justify-content: center;

      .store-name {
        font-size: toRpx(28);
        font-weight: 500;
        color: #333;
        margin-bottom: toRpx(8);
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .store-code {
        font-size: toRpx(22);
        color: #999;
      }
    }

    .selected-icon {
      display: flex;
      align-items: center;
    }
  }
}
</style>