index.vue 5.27 KB
<script setup lang="ts">
import { reactive, ref, onMounted, getCurrentInstance } from "vue";
import { propertyList, propertyStatus } from "@/api/property";
import { userProxy } from '@/api/user/user';
import { parseTime } from "@/utils/tool";
import Pagination from "@/components/Pagination/index.vue";
const {
  appContext: {
    config: { globalProperties },
  },
} = getCurrentInstance();
interface Form {
  page: number;
  pageSize: number;
  name?: string;
}
interface Params {
  total: number;
  query: Form;
}
interface Data {
  loading: boolean;
  list: any[] | null;
  params: Params;
}
let data = reactive({
  list: [],
  loading: false,
  params: {
    total: 0,
    query: {
      page: 1,
      pageSize: 10,
      name: "",
    },
  },
});
/**
 * 列表
 */
const getList = async () => {
  data.loading = true;
  try {
    const res = await propertyList(data.params.query);
    data.list = res?.data?.data?.list;
    data.params.total = res?.data?.data?.total;
  } catch (e) {
    console.log(e);
  } finally {
    data.loading = false;
  }
};
/**
 * 分页
 */
const handlePageChange = (page, size) => {
  data.params.query.page = page;
  data.params.query.pageSize = size;
  getList();
};
/**
 * 查询
 */
const handleSearch = () => {
  getList();
};
/**
 * 切换状态
 * @param {object} 单条数据对象 
 */
const handleStatus = async (obj) => {
  try {
    const res = await propertyStatus({
      id: obj.id,
      status: obj.status ? 0 : 1,
    });
    if (res?.data) {
      globalProperties.$message.success("操作成功");
      getList();
    } else {
      globalProperties.$message.error("操作失败");
    }
  } catch (e) {
    console.log(e);
  }
};
/**
 * 代理登录
 * @param {object} 单条数据对象
 */
const handleProxy = (obj) => {
  userProxy({oId: obj.id}).then(res => {
    if(res?.data) {
      globalProperties.$message.success("代理成功");
      setTimeout(() => {
        location.href = '/'
      }, 500)
    } else {
      globalProperties.$message.error("代理失败");
    }
  })
}
/**
 * 状态值
 * @enum {number} 
 */
enum statusEnum {
  "禁用" = 0,
  "启用",
}
onMounted(() => {
  getList();
});
</script>

<template>
  <div class="page-container">
    <h1>物业列表</h1>
    <el-row style="padding-bottom: 20px">
      <el-form :inline="true" label-width="70px">
        <el-form-item label="物业名称">
          <el-input
            v-model.trim="data.params.query.name"
            placeholder="请填写物业名称"
            maxlength="15"
            @change="handleSearch"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button type="primary">
            <router-link to="/property/edit">创建物业</router-link>
          </el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <el-table
      :data="data.list"
      style="width: 100%"
      border
      v-loading="data.loading"
      align="center"
      element-loading-text="拼命加载中"
    >
      <el-table-column prop="id" label="ID" align="center"> </el-table-column>
      <el-table-column prop="location" label="地区" align="center">
      </el-table-column>
      <el-table-column prop="address" label="详细地址" align="center">
      </el-table-column>
      <el-table-column prop="name" label="公司名称" align="center">
      </el-table-column>
      <el-table-column prop="userName" label="联系人" align="center">
      </el-table-column>
      <el-table-column prop="userPhone" label="联系电话" align="center">
      </el-table-column>
      <el-table-column prop="businessLicense" label="营业执照" align="center">
        <template #default="scope">
          <el-image
            :src="
              scope.row.businessLicense
                ? scope.row.businessLicense
                : globalProperties.$defaultImage
            "
            fit="contain"
            lazy
            style="height: 100px"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="入驻时间" align="center">
        <template #default="scope">
          {{ parseTime(new Date(scope.row.createTime).getTime()) }}
        </template>
      </el-table-column>
      <el-table-column prop="contractNumber" label="合同编号" align="center">
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template #default="scope">
          {{ statusEnum[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="text"
            ><router-link :to="{ path: '/property/edit', query: scope.row }"
              >编辑</router-link
            ></el-button
          >
          <el-button type="text" @click="handleProxy(scope.row)">代理登录</el-button>
          <el-button type="text" @click="handleStatus(scope.row)">{{
            scope.row.status ? "禁用" : "启用"
          }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination
      :total="data.params.total"
      :currentPage="data.params.query.page"
      :pageSize="data.params.query.pageSize"
      @pageChange="handlePageChange"
    ></Pagination>
  </div>
</template>