config.ts 1.98 KB
import { AttributeData, Option, QueryForPaginationParams, QueryForParams, ServiceResponse } from '@/types/global';
import { SystemConfig } from '@/types/system-config';
import axios from 'axios';
import { first } from 'lodash';

export default class useConfigApi {
  static statusOptions = [
    { label: '启用', value: 1 },
    { label: '禁用', value: 0 },
  ];

  static contentOptions = [
    { label: '无内容', value: 'none' },
    {
      label: '文本内容',
      value: 'string',
      children: [
        { label: '短文本', value: 'input' },
        { label: '长文本', value: 'textarea' },
      ],
    },
    {
      label: '上传链接',
      value: 'upload',
      children: [
        { label: '图片链接', value: 'image' },
        { label: '视频链接', value: 'video' },
        { label: '音频链接', value: 'audio' },
        { label: '文件链接', value: 'file' },
      ],
    },
  ];

  static get(params?: QueryForParams | QueryForPaginationParams): Promise<ServiceResponse<SystemConfig[]>> {
    return axios.get('system/configs', { params });
  }

  static async create(data: AttributeData) {
    return axios.post('system/configs', data).then((res) => Promise.resolve(res.data));
  }

  static async update(id: number, data: AttributeData) {
    return axios.put(`system/configs/${id}`, data).then((res) => Promise.resolve(res.data));
  }

  static async changeStatus(id: number, status: number) {
    return axios.put(`system/configs/${id}/change-status`, { status });
  }

  static async getOne(params?: QueryForParams): Promise<SystemConfig | undefined> {
    return useConfigApi.get({ ...params, parent_id: '', fetchType: 'all' }).then(({ data }) => first(data));
  }

  static async getOption(params?: QueryForParams): Promise<Option[]> {
    return useConfigApi.get({ parent_id: '', ...params, fetchType: 'all' }).then(({ data }) =>
      data?.map((item) => {
        return {
          value: item.identifier,
          label: item.name,
        };
      })
    );
  }
}