Newer
Older
HuangJiPC / src / pages / views / publicService / fansInfoUser.vue
@zhangdeliang zhangdeliang on 21 Jun 12 KB update
<template>
  <!-- 粉丝用户管理 -->
  <div class="fansInfoUser">
    <div class="searchBoxs">
      <n-space>
        <n-input v-model:value="searchVal1" placeholder="请输入用户昵称" style="width: 200px" clearable></n-input>
        <n-button type="primary" @click="handleClick('search')">
          <template #icon>
            <n-icon><Search /></n-icon>
          </template>
          搜索
        </n-button>
      </n-space>
    </div>
    <!-- 表格 -->
    <div class="tableBox">
      <n-data-table
        :bordered="false"
        :max-height="700"
        striped
        :columns="columns"
        :data="tableData"
        :loading="tableLoading"
        :remote="true"
        :pagination="pagination"
      >
      </n-data-table>
    </div>
    <!-- 修改用户信息弹窗 -->
    <n-modal
      title="修改用户信息"
      :mask-closable="false"
      preset="dialog"
      :show-icon="false"
      :style="{ width: '600px' }"
      v-model:show="modalShow"
    >
      <n-form
        ref="formRef"
        :label-width="100"
        :rules="formInfo.rules"
        :model="formInfo.data"
        style="height: 500px; margin-top: 20px"
        label-placement="left"
      >
        <n-form-item label="用户昵称:" path="nickName">
          <n-input v-model:value="formInfo.data.nickName" disabled />
        </n-form-item>
        <n-form-item label="微信ID:" path="openId">
          <n-input v-model:value="formInfo.data.openId" disabled />
        </n-form-item>
        <n-form-item label="手机号:" path="telephone">
          <n-input v-model:value="formInfo.data.telephone" maxlength="11" placeholder="请输入手机号" />
        </n-form-item>
        <n-form-item label="选择分组:" path="groupId">
          <n-select filterable :options="groupList" v-model:value="formInfo.data.groupId" clearable />
        </n-form-item>
        <n-form-item label="身份类型:" path="roleType">
          <n-select filterable :options="statusList" v-model:value="formInfo.data.roleType" clearable />
        </n-form-item>
      </n-form>
      <template #action>
        <n-space>
          <n-button @click="() => (modalShow = false)">取消</n-button>
          <n-button type="primary" @click="handleClick('submit')">确定 </n-button>
        </n-space>
      </template>
    </n-modal>
  </div>
</template>

<script>
import { toRefs, onMounted, reactive, h, ref, nextTick } from 'vue';
import { Search, Add } from '@vicons/ionicons5';
import { NButton, NImage } from 'naive-ui';
import { isMobile } from '@/utils/validate';
import { rzUserSearch, rzUserDelete, rzUserAuth, rzUserUpdate, userGroupSearch } from '@/services';

export default {
  name: 'fansInfoUser',
  components: { Search, Add, NButton },
  setup() {
    const allData = reactive({
      searchVal1: null,
      modalShow: false,
      groupList: [],
      authList: [
        { value: 0, label: '未认证' },
        { value: 1, label: '已认证' },
        { value: 2, label: '认证中' },
      ],
      sexList: [
        { value: 0, label: '男' },
        { value: 1, label: '女' },
      ],
      statusList: [
        { value: 0, label: '普通大众' },
        { value: 1, label: '水务局' },
        { value: 2, label: '项目公司' },
      ],
      formInfo: {
        data: {
          roleType: '',
          telephone: '',
          openId: '',
          nickName: '',
          groupId: '',
          gender: '',
        },
        rules: {
          telephone: [
            {
              required: true,
              trigger: ['blur'],
              validator: (rule, value) => {
                return new Promise((resolve, reject) => {
                  if (value == null) {
                    reject(Error('请输入手机号'));
                  } else if (!isMobile(value)) {
                    reject(Error('手机号格式错误'));
                  } else {
                    resolve();
                  }
                });
              },
            },
          ],
          roleType: {
            type: 'number',
            required: true,
            trigger: ['change'],
            message: '请选择身份类型',
          },
          gender: {
            required: true,
            trigger: ['change'],
            message: '请选择性别',
          },
        },
      },
      tableLoading: true,
      tableData: [],
      columns: [
        {
          title: '序号',
          align: 'center',
          width: '80',
          render(row, index) {
            return (paginationReactive.page - 1) * paginationReactive.pageSize + index + 1;
          },
        },
        {
          title: '用户头像',
          align: 'center',
          key: 'headImg',
          render(row) {
            let imgs = h(NImage, {
              width: 50,
              height: 50,
              style: 'margin-right:5px;border-radius:50%;',
              src: row.headImg
                ? row.headImg
                : 'https://newfiber-cqjb-1255570142.cos.ap-chengdu.myqcloud.com/huangji/upload/file/2302031457231601406063.jpg',
            });
            return imgs;
          },
        },
        { title: '用户昵称', align: 'center', key: 'nickName' },
        {
          title: '性别',
          align: 'center',
          key: 'gender',
          render(row) {
            return row.gender == 0 ? '男' : '女';
          },
        },
        {
          title: '手机号',
          align: 'center',
          key: 'telephone',
          width: '120',
          render(row) {
            let phone = '';
            if (row.telephone) {
              phone = row.telephone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
            } else {
              phone = row.telephone;
            }
            return phone;
          },
        },
        { title: '邮箱', align: 'center', key: 'email' },
        { title: 'openId', align: 'center', key: 'openId' },
        {
          title: '所属分组',
          align: 'center',
          key: 'groupId',
          render(row) {
            if (allData.groupList.length == 0 || row.groupId == null) return;
            let data = allData.groupList.filter((item) => item.value == row.groupId);
            return data[0].label;
          },
        },
        {
          title: '认证状态',
          align: 'center',
          key: 'authStatus',
          render(row) {
            return row.authStatus == 0 ? '未认证' : row.authStatus == 1 ? '已认证' : '认证中';
          },
        },
        {
          title: '身份类型',
          align: 'center',
          key: 'roleType',
          render(row) {
            return row.roleType == 0 ? '普通大众' : row.roleType == 1 ? '水务局' : '项目公司';
          },
        },
        {
          title: '提交时间',
          align: 'center',
          key: 'createTime',
          width: '160',
        },
        {
          title: '操作',
          key: 'actions',
          width: '220',
          align: 'center',
          render(row) {
            let arrs = [...allData.actionColumn];
            let obj = {
              size: 'small',
              btnType: 'primary',
              type: 'attestation',
              default: row.authStatus == 0 || row.authStatus == 2 ? '认证' : '取消认证',
            };
            arrs.unshift(obj);
            const btn = arrs.map((item, index) => {
              return h(
                NButton,
                {
                  text: true,
                  size: item.size,
                  style: {
                    margin: '10px',
                  },
                  type: item.btnType,
                  onClick: () => handleClick(item.type, row),
                },
                { default: () => item.default }
              );
            });
            return btn;
          },
        },
      ],
      actionColumn: [
        {
          size: 'small',
          btnType: 'success',
          type: 'edit',
          default: '修改',
        },
        {
          size: 'small',
          btnType: 'error',
          type: 'delete',
          default: '删除',
        },
      ],
    });
    //分页
    const paginationReactive = reactive({
      page: 1,
      pageSize: 10,
      showSizePicker: true,
      pageSizes: [10, 20, 50],
      showQuickJumper: true,
      pageCount: 0,
      itemCount: 0,
      prefix: () => {
        return '共 ' + paginationReactive.itemCount + ' 项';
      },
      onChange: (page) => {
        paginationReactive.page = page;
        getTableData();
      },
      onPageSizeChange: (pageSize) => {
        paginationReactive.pageSize = pageSize;
        paginationReactive.page = 1;
        getTableData();
      },
    });
    const getTableData = async () => {
      allData.tableLoading = true;
      let pramas = {
        current: paginationReactive.page,
        size: paginationReactive.pageSize,
        nickName: allData.searchVal1,
      };
      let res = await rzUserSearch(pramas);
      if (res && res.code == 200) {
        allData.tableData = res.data.records;
        paginationReactive.pageCount = res.data.pages;
        paginationReactive.itemCount = res.data.total;
      }
      allData.tableLoading = false;
    };
    const formRef = ref(null);
    // 点击事件
    const handleClick = async (type, row) => {
      switch (type) {
        case 'search':
          paginationReactive.page = 1;
          getTableData();
          break;
        case 'edit':
          allData.modalShow = true;
          allData.formInfo.data = { ...row };
          break;
        case 'attestation':
          if (row.authStatus == 0 || row.authStatus == 2) {
            // 认证
            $dialog.success({
              title: '认证',
              content: '确定该用户通过认证?',
              positiveText: '确定',
              negativeText: '取消',
              onPositiveClick: async () => {
                // 确定
                let params = {
                  authStatus: 1,
                  id: row.id,
                };
                let res = await rzUserAuth(params);
                if (res && res.code === 200) {
                  $message.success('操作成功');
                  getTableData();
                }
              },
            });
          } else {
            // 取消认证
            $dialog.success({
              title: '取消认证',
              content: '确定取消该用户认证?',
              positiveText: '确定',
              negativeText: '取消',
              onPositiveClick: async () => {
                // 确定
                let params = {
                  authStatus: 0,
                  id: row.id,
                };
                let res = await rzUserAuth(params);
                if (res && res.code === 200) {
                  $message.success('操作成功');
                  getTableData();
                }
              },
            });
          }
          break;
        case 'submit':
          formRef.value.validate((errors) => {
            if (!errors) {
              submitData();
            } else {
              $message.error('验证失败,请检查必填项');
            }
          });
          break;
        case 'delete':
          $dialog.info({
            title: '提示',
            content: `确定删除该数据吗?`,
            positiveText: '确定',
            negativeText: '取消',
            onPositiveClick: () => {
              let ids = [row.id];
              dataDel(ids);
            },
          });
          break;
      }
    };
    // 删除数据
    async function dataDel(ids) {
      let res = await rzUserDelete({ ids: ids });
      if (res && res.code === 200) {
        $message.success('删除成功');
        getTableData();
      }
    }
    // 提交数据
    async function submitData() {
      let parmas = { ...allData.formInfo.data };
      let res = await rzUserUpdate(parmas);
      if (res && res.code == 200) {
        $message.success('操作成功');
        getTableData();
        allData.modalShow = false;
      }
    }
    // 获取组信息
    async function getGroup() {
      allData.groupList = [];
      let params = { current: 0, size: 100 };
      let res = await userGroupSearch(params);
      if (res && res.code == 200) {
        let datas = res.data.records;
        if (datas.length == 0) return;
        datas.map((item) => {
          allData.groupList.push({
            value: item.id,
            label: item.groupName,
          });
        });
      }
    }
    onMounted(() => {
      getGroup();
      getTableData();
    });
    return {
      ...toRefs(allData),
      pagination: paginationReactive,
      handleClick,
      getTableData,
      formRef,
    };
  },
};
</script>

<style lang="less" scoped>
.fansInfoUser {
  width: 100%;
  .searchBoxs {
    margin: 10px;
  }
}
</style>