<template> 
 | 
  <TableLayout> 
 | 
    <template v-slot:table-wrap> 
 | 
      <ul class="toolbar" v-permissions="['business:company:create', 'business:company:sync']"> 
 | 
        <li><el-button type="primary" v-permissions="['business:company:create']" :loading="loading"  @click="synchronous()">全量同步企业微信组织</el-button></li> 
 | 
      </ul> 
 | 
      <el-table  v-loading="isWorking.search" :data="list" stripe 
 | 
                :tree-props="{ children: 'childList', hasChildren: 'hasChildren' }" row-key="id" 
 | 
                @selection-change="handleSelectionChange" default-expand-all> 
 | 
        <el-table-column type="selection" width="55"></el-table-column> 
 | 
        <el-table-column prop="name" label="组织名称" min-width="100px"></el-table-column> 
 | 
        <el-table-column prop="namePath" label="组织路径" min-width="100px"></el-table-column> 
 | 
        <el-table-column prop="qwId" label="企业微信标识" min-width="100px"></el-table-column> 
 | 
        <el-table-column prop="editorName" label="操作人" width="100px"></el-table-column> 
 | 
        <el-table-column prop="editDate" label="最后更新时间" min-width="100px"></el-table-column> 
 | 
      </el-table> 
 | 
    </template> 
 | 
    <!-- 新建/修改 --> 
 | 
    <OperaInternalCompanyWindow ref="OperaInternalCompanyWindow" :list="list" @success="handlePageChange" /> 
 | 
  </TableLayout> 
 | 
</template> 
 | 
  
 | 
<script> 
 | 
import BaseTable from '@/components/base/BaseTable' 
 | 
import TableLayout from '@/layouts/TableLayout' 
 | 
import {companySync} from '@/api/business/company' 
 | 
export default { 
 | 
  name: 'internalCompany', 
 | 
  extends: BaseTable, 
 | 
  components: { TableLayout }, 
 | 
  data () { 
 | 
    return { 
 | 
      // 搜索 
 | 
      loading: false, 
 | 
      sorting: false, 
 | 
      searchForm: { 
 | 
      }, 
 | 
      list: [], 
 | 
      total: 0 
 | 
    } 
 | 
  }, 
 | 
  created () { 
 | 
    this.config({ 
 | 
      module: '企业信息表', 
 | 
      api: '/business/company', 
 | 
      'field.id': 'id', 
 | 
      'field.main': 'id' 
 | 
    }) 
 | 
    this.handlePageChange() 
 | 
  }, 
 | 
  methods: { 
 | 
    // 页码变更处理 
 | 
    handlePageChange (pageIndex) { 
 | 
      this.__checkApi() 
 | 
      this.isWorking.search = true 
 | 
      this.api.treeList({}) 
 | 
        .then(data => { 
 | 
          this.list = data 
 | 
        }) 
 | 
        .catch(e => { 
 | 
          this.$tip.apiFailed(e) 
 | 
        }) 
 | 
        .finally(() => { 
 | 
          this.isWorking.search = false 
 | 
        }) 
 | 
    }, 
 | 
    // 同步信息 
 | 
    async synchronous () { 
 | 
      this.$dialog.actionConfirm(' 您确认同步全部信息吗?', '操作确认提醒') 
 | 
        .then(() => { 
 | 
          this.loading = true 
 | 
          companySync({}) 
 | 
            .then(res => { 
 | 
              this.$tip.apiSuccess(res || '同步成功') 
 | 
              this.handlePageChange(1) 
 | 
            }) 
 | 
            .catch(e => { 
 | 
              this.$tip.apiFailed(e) 
 | 
            }) 
 | 
            .finally(() => { 
 | 
              this.loading = false 
 | 
            }) 
 | 
        }) 
 | 
        .catch(() => { }) 
 | 
    }, 
 | 
    // 排序 
 | 
    sort (direction) { 
 | 
      if (this.sorting) { 
 | 
        return 
 | 
      } 
 | 
      if (this.tableData.selectedRows.length === 0) { 
 | 
        this.$tip.warning('请选择一条数据') 
 | 
        return 
 | 
      } 
 | 
      if (this.tableData.selectedRows.length > 1) { 
 | 
        this.$tip.warning('排序时仅允许选择一条数据') 
 | 
        return 
 | 
      } 
 | 
      const menuId = this.tableData.selectedRows[0].id 
 | 
      // 找到菜单范围 
 | 
      let menuPool 
 | 
      for (const rootMenu of this.list) { 
 | 
        console.log(this.list) 
 | 
        const parent = this.__findParent(menuId, rootMenu) 
 | 
        if (parent != null) { 
 | 
          menuPool = parent.children 
 | 
        } 
 | 
      } 
 | 
      menuPool = menuPool || this.list 
 | 
      const menuIndex = menuPool.findIndex(menu => menu.id === menuId) 
 | 
      // 上移校验 
 | 
      if (direction === 'top' && menuIndex === 0) { 
 | 
        this.$tip.warning('菜单已到顶部') 
 | 
        return 
 | 
      } 
 | 
      // 下移校验 
 | 
      if (direction === 'bottom' && menuIndex === menuPool.length - 1) { 
 | 
        this.$tip.warning('菜单已到底部') 
 | 
        return 
 | 
      } 
 | 
      this.sorting = true 
 | 
      sort({ 
 | 
        id: this.tableData.selectedRows[0].id, 
 | 
        direction 
 | 
      }) 
 | 
        .then(() => { 
 | 
          /* if (direction === 'top') { 
 | 
               menuPool.splice(menuIndex, 0, menuPool.splice(menuIndex - 1, 1)[0]) 
 | 
             } else { 
 | 
               menuPool.splice(menuIndex, 0, menuPool.splice(menuIndex + 1, 1)[0]) 
 | 
             } */ 
 | 
          this.handlePageChange(1) 
 | 
        }) 
 | 
        .catch(e => { 
 | 
          this.$tip.apiFailed(e) 
 | 
        }) 
 | 
        .finally(() => { 
 | 
          this.sorting = false 
 | 
        }) 
 | 
    }, 
 | 
    // 查询父节点 
 | 
    __findParent (id, parent) { 
 | 
      if (parent.childList === 0) { 
 | 
        return 
 | 
      } 
 | 
      for (const menu of parent.childList) { 
 | 
        if (menu.id === id) { 
 | 
          return parent 
 | 
        } 
 | 
        if (menu.childList.length > 0) { 
 | 
          const m = this.__findParent(id, menu) 
 | 
          if (m != null) { 
 | 
            return m 
 | 
          } 
 | 
        } 
 | 
      } 
 | 
      return null 
 | 
    } 
 | 
  } 
 | 
} 
 | 
</script> 
 |