ToolBox.vue 10.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
<template>
  <view class="tool-box" :style="toolStyle">
    <view :class="['btn', { hideIcon: !show }]" @click="open">
      <view class="icon sprite-icon icon-tool-file"></view>
        文件夹
    </view>
    <view :class="['list', { showList: !show }]">
      <view class="title" @click="close">{{ folderName }}</view>
      <view class="list-body" :class="{ 'multi-column': flatList.length > 8 }">
        <template v-for="(item, index) in flatList">
          <view :key="index" :class="['item', { active: item.id == id, 'folder-item': item.isFolder }]" @click.stop="handleItemClick(item)" :style="{'--i': index}">
            <template v-if="item.isFolder">
              <view class="folder">{{ item.folderName }}</view>
            </template>
            <template v-else>
              <view class="icon">
                <view class="sprite-icon icon-tool-pay"></view>
              </view>
              <view class="material-name">{{ item.materialName }}</view>
            </template>
          </view>
        </template>
      </view>
      <template v-if="showPayBtn">
        <view class="item" @click="toPay" :style="{'--i': flatList.length}">
          <view class="icon">
            <view class="sprite-icon icon-tool-pay"></view>
          </view>
          付款方式
        </view>
      </template>
      <template v-if="type != 'preview'">
        <view class="item" @click="finish" :style="{'--i': flatList.length + (showPayBtn ? 1 : 0)}">
          <view class="icon">
            <view class="sprite-icon icon-tool-finish"></view>
          </view>
          完成接待
        </view>
      </template>
    </view>
  </view>
</template>

<script type="text/ecmascript-6">
export default {
  name: 'ToolBox',
  props: {
    type: String,
    data: Array,
    id: [String, Number],
    showPayBtn: Boolean,
    idKey: {
      type: String,
      default: 'id'
    },
    toolStyle:String,
    folderName: {
      type: String,
      default: '文件夹'
    },
  },
  data () {
    return {
      show: true,
      touchStartX: 0,
      touchStartY: 0,
      flatList: [],
      expandedFolderCode: ''
    }
  },
  computed: {
    watchStatus () {
      return this.$store.state.user.historyStatus
    },
  },
  watch: {
    watchStatus: {
      immediate: true,
      handler (newval) {
        this.show = true
      }
    },
    data: {
      immediate: true,
      handler (newval) {
        this.syncExpandedFolder(newval)
        this.flatList = this.flattenTreeData(newval)
      }
    },
    id: {
      immediate: true,
      handler () {
        this.syncExpandedFolder(this.data)
        this.flatList = this.flattenTreeData(this.data)
      }
    },
  },
  methods: {
    syncExpandedFolder(treeData) {
      if (!Array.isArray(treeData) || !treeData.length) {
        this.expandedFolderCode = ''
        return
      }
      const matchedFolder = treeData.find(folder =>
        Array.isArray(folder.children) && folder.children.some(child => child && child[this.idKey] == this.id)
      )
      if (matchedFolder?.folderCode) {
        this.expandedFolderCode = matchedFolder.folderCode
        return
      }
      if (!treeData.some(folder => folder.folderCode === this.expandedFolderCode)) {
        this.expandedFolderCode = treeData[0]?.folderCode || ''
      }
    },
    flattenTreeData(treeData) {
      const result = []
      if (!Array.isArray(treeData)) return result

      treeData.forEach(folder => {
        // 添加文件夹标题项
        result.push({
          isFolder: true,
          folderCode: folder.folderCode,
          folderName: folder.folderName
        })
        // 添加子项素材
        if (this.expandedFolderCode === folder.folderCode && Array.isArray(folder.children)) {
          folder.children.forEach(child => {
            result.push({
              ...child,
              isFolder: false
            })
          })
        }
      })
      return result
    },
    handleItemClick(item) {
      if (item.isFolder) {
        // 手风琴效果:仅展开当前分类;若再次点击已展开分类则收起
        this.expandedFolderCode = this.expandedFolderCode === item.folderCode ? '' : item.folderCode
        this.flatList = this.flattenTreeData(this.data)
        if (!this.expandedFolderCode) return
        // 展开后默认定位到该分类第一个素材
        const folder = this.data.find(f => f.folderCode === this.expandedFolderCode)
        if (folder && Array.isArray(folder.children) && folder.children.length > 0) {
          this.$emit('choose', folder.children[0])
        }
      } else {
        // 点击素材,直接切换
        this.$emit('choose', item)
      }
    },
    // handleTouchStart(e) {
    //   this.touchStartX = e.touches[0].pageX
    //   this.touchStartY = e.touches[0].pageY
    // },
    // handleTouchMove(e) {
    //   // 阻止默认行为,防止滚动冲突
    //   if (Math.abs(e.touches[0].pageX - this.touchStartX) > Math.abs(e.touches[0].pageY - this.touchStartY)) {
    //     e.preventDefault()
    //   }
    // },
    // handleTouchEnd(e) {
    //   console.log(e, 'touchend ')
    //   const diffX = e.changedTouches[0].pageX - this.touchStartX
    //   const diffY = e.changedTouches[0].pageY - this.touchStartY

    //   // 判断是否为水平滑动
    //   if (Math.abs(diffX) < 50 || Math.abs(diffY) > 30) return

    //   // 左滑或右滑切换文件夹
    //   const direction = diffX > 0 ? 'prev' : 'next'
    //   this.$emit('swipe', direction)
    // },
    hide () {
      this.show = true
    },
    open () {
      this.show = false
      this.$emit('open')
    },
    close () {
      this.show = true
      this.$emit('close')
    },
    jump (item) {
      this.show = true
      this.$emit('choose', item)
    },
    finish () {
      this.$emit('finish')
    },
    // 选择付款方式
    toPay () {
      this.$emit('toPay')
    },

  }
}
</script>

<style scoped lang="scss">
.tool-box {
  position: fixed;
  right: toRpx(40);
  top: calc(5% + #{toRpx(140)});
  z-index: 998;
  width: toRpx(120);

  /* 基础变量定义 */
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1);

  .btn {
    width: 100%;
    height: toRpx(120);
    background: #ffffff;
    /* 玻璃拟态背景,增加现代感 */
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(10px);
    border: toRpx(2) solid #ffffff;
    box-shadow: 0 toRpx(8) toRpx(24) 0 rgba(0, 0, 0, 0.12);
    border-radius: toRpx(24); /* 更圆润的角 */
    font-size: toRpx(24);
    color: #292d33;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    transition: all 0.4s var(--ease-out);
    opacity: 1;
    transform: scale(1);
    overflow: hidden;
    position: relative;
    z-index: 2;

    .icon {
      width: toRpx(60);
      height: toRpx(60);
      margin-bottom: toRpx(8);
      transition: transform 0.3s ease;
    }

    &:active {
        transform: scale(0.96);
    }
  }

  .hideIcon {
    opacity: 0;
    transform: scale(0.8);
    pointer-events: none;
    transition: all 0.3s ease;
  }

  .list {
    width: 100%;
    /* 手风琴效果核心:通过 max-height 进行高度动画 */
    max-height: 0;
    border: toRpx(2) solid #ffffff;
    box-shadow: 0 toRpx(12) toRpx(32) 0 rgba(0, 0, 0, 0.15);
    border-radius: toRpx(24);
    display: flex;
    flex-direction: column;
    align-items: center;
    overflow: hidden;
    
    /* 定位与初始状态 */
    position: absolute;
    top: 0;
    left: 0;
    background: rgba(255, 255, 255, 0.98);
    backdrop-filter: blur(20px);
    
    opacity: 0;
    visibility: hidden;
    
    z-index: 1;
    /* 干净利落的快速展开 */
    transition: 
      opacity 0.2s ease,
      visibility 0.2s ease,
      max-height 0.3s cubic-bezier(0.19, 1, 0.22, 1); /* Expo Out: 极快冲刺,迅速到位 */

    .title {
      width: 100%;
      height: toRpx(72); 
      background: rgba(248, 249, 251, 0.8);
      border-bottom: toRpx(1) solid rgba(0,0,0,0.03);
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: toRpx(26);
      font-weight: 600;
      color: #333;
      flex-shrink: 0;
      
      &:active {
        background: rgba(0,0,0,0.02);
      }
    }

    .list-body {
      width: 100%;
      /* 移除 max-height 限制,由父容器 max-height 控制 */
      /* max-height: 60vh; */
      overflow-y: auto;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding-bottom: toRpx(16);

      /* 超过 8 个时换列展示 */
      &.multi-column {
        // flex-wrap: wrap;
        justify-content: flex-start;
        width: toRpx(240);

        .item {
          width: toRpx(120);
        }
      }

      /* 滚动条隐藏但可滚动 */
      &::-webkit-scrollbar {
        width: 0;
        height: 0;
        background-color: transparent;
      }
    }

    .item {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      font-size: toRpx(22);
      color: #555;
      width: 100%;
      padding-bottom: toRpx(12);
      position: relative;

      /* 列表项快速复位 */
      opacity: 0;
      transform: translateY(5rpx);
      transition: all 0.2s ease;

      .icon {
        width: toRpx(64);
        height: toRpx(64);
        margin-bottom: toRpx(8);
        transition: transform 0.2s;
      }

      &:active {
        .icon {
            transform: scale(0.9);
        }
      }

      /* 选中态指示器 */
      &.active {
        color: #2c66f7;
        font-weight: 500;
      }

      /* 文件夹项样式 */
      &.folder-item {
        justify-content: flex-start;
        padding: toRpx(8) 0;

        .folder {
          height: toRpx(50);
          line-height: toRpx(50);
          background-color: #eaedf3;
          border: toRpx(1) solid #e2e2e2;
          border-radius: toRpx(8);
          text-align: center;
          width: 100%;
          font-size: toRpx(24);
          font-weight: 600;
          color: #666;
        }
      }

      .material-name {
        font-size: toRpx(22);
        text-align: center;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }

  .showList {
    opacity: 1;
    visibility: visible;
    max-height: toRpx(1000); 
    z-index: 3;

    .item {
      opacity: 1;
      transform: translateY(0);
      /* 极微小的延迟,保持整体性,减少拖沓感 */
      transition-delay: calc(0.05s + var(--i) * 0.02s);
    }
  }
}
</style>