zouyu
2026-05-07 b0d4df5f39525ae7fe252e8ee65d85fd71dca721
src/views/system/user/index.vue
@@ -47,7 +47,12 @@
            </div>
          </div>
          <el-col>
            <el-table row-id="userId" ref="dragTable" v-loading="loading" :data="userList" :header-cell-style="{ background: '#f8f8f9', color: '#515a6e' }" border>
            <el-table ref="dragTable" v-loading="loading" row-key="userId" :data="userList" :header-cell-style="{ background: '#f8f8f9', color: '#515a6e' }" border>
              <el-table-column label="拖拽" align="center" width="60">
                <template slot-scope="scope">
                  <i class="el-icon-rank drag-handle" :data-user-id="scope.row.userId"></i>
                </template>
              </el-table-column>
              <el-table-column label="序号" align="center" type="index" />
              <el-table-column label="姓名" align="center" key="nickName" prop="nickName" :show-overflow-tooltip="true" />
              <el-table-column label="账号" align="center" key="userName" prop="userName" :show-overflow-tooltip="true" />
@@ -471,6 +476,7 @@
        nickName: '',
      },
      sortTable: null,
      sortSaving: false,
    };
  },
  watch: {
@@ -486,61 +492,103 @@
      this.initPassword = response.msg;
    });
  },
  mounted(){
    // 挂载后初始化拖拽
    this.initDrag()
  beforeDestroy() {
    this.destroyDrag()
  },
  methods: {
    //表格行拖拽排序
    // 表格行拖拽排序
    destroyDrag() {
      if (this.sortTable) {
        this.sortTable.destroy()
        this.sortTable = null
      }
    },
    syncDragDisabledState() {
      if (this.sortTable) {
        this.sortTable.option('disabled', this.loading || this.sortSaving)
      }
    },
    initDrag() {
      this.destroyDrag()
      if (!this.$refs.dragTable || !this.userList || this.userList.length === 0) {
        return
      }
      // 获取 el-table 的 tbody 元素(拖拽的目标容器)
      const tbody = this.$refs.dragTable.$el.querySelector(
        '.el-table__body-wrapper tbody'
      )
      if (!tbody) {
        return
      }
      // 初始化 Sortable
      this.sortable = Sortable.create(tbody, {
      this.sortTable = Sortable.create(tbody, {
        animation: 150, // 拖拽动画过渡时长
        ghostClass: 'sortable-ghost', // 拖拽占位符样式
        chosenClass: 'sortable-chosen', // 选中行样式
        dragClass: 'sortable-drag', // 拖拽元素样式
        handle: '.drag-handle',
        disabled: this.loading || this.sortSaving,
        // 拖拽结束触发(核心逻辑)
        onEnd: ({ oldIndex, newIndex }) => {
          // oldIndex:原索引,newIndex:新索引
          const defNum = (this.queryParams.pageNum-1)*this.queryParams.pageSize
          // const row = this.userList[oldIndex]
          // let sort = newIndex+defNum;//排序下标
          const row = this.userList.splice(oldIndex, 1)[0]
          this.userList.splice(newIndex, 0, row)
          const data = this.userList.map(item=>{return {userId:item.userId,userName:item.userName}})
          console.log(data)
          console.log(this.userList)
          // 调用接口更新排序
          // let data = {
          //   userId:row.userId,
          //   sort: sort
          // }
          // updateUserSort(data).then(res=>{
          //   if(res.code===200){
          //     this.$message.success("更新成功")
          //     this.$nextTick(()=>{
          //       this.getList()
          //     })
          //   }
          // })
        onEnd: async({ oldIndex, newIndex }) => {
          if (
            this.loading ||
            this.sortSaving ||
            oldIndex === newIndex ||
            oldIndex === undefined ||
            newIndex === undefined
          ) {
            return
          }
        },
          const previousList = [...this.userList]
          const row = this.userList.splice(oldIndex, 1)[0]
          if (!row) {
            this.userList = previousList
            return
          }
          this.userList.splice(newIndex, 0, row)
          const pageOffset =
            (this.queryParams.pageNum - 1) * this.queryParams.pageSize
          const data = this.userList.map((item, index) => ({
            id: item.userId,
            sort: pageOffset + index + 1
          }))
          this.sortSaving = true
          this.syncDragDisabledState()
          try {
            await updateUserSort(data)
            this.$message.success('更新排序成功')
          } catch (error) {
            this.userList = previousList
            this.$message.error('更新排序失败')
          } finally {
            this.sortSaving = false
            this.$nextTick(() => {
              this.syncDragDisabledState()
            })
          }
        }
      })
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      this.loading = true
      this.syncDragDisabledState()
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
        this.userList = response.rows
        this.total = response.total
        this.loading = false
        this.$nextTick(() => {
          this.initDrag()
        })
      }).catch(() => {
        this.loading = false
        this.destroyDrag()
      })
    },
    // 打开添加架构弹框
    addSchema() {
@@ -958,6 +1006,20 @@
</script>
<style scoped lang="scss">
:deep(.drag-handle) {
  cursor: grab;
  color: #909399;
  font-size: 16px;
  display: inline-block;
  user-select: none;
}
:deep(.drag-handle:hover) {
  color: #409EFF;
  cursor: grab;
}
:deep(.drag-handle:active) {
  cursor: grabbing;
}
:deep(.sortable-ghost) {
  opacity: 0.8;
  background: #f0f9eb;