CVIDraw.vue 10.1 KB
<template>
  <div class="card">
    <Card :bordered="false" class="card">
      <div style="margin-left: -40px">
        <BasicForm @register="registerForm">
          <template #logoUpload>
            <ContentUploadText>
              <template #uploadImg>
                <CustomUploadComp :imgUrl="logoPic" @setImg="handleSetLogoImgUrl" />
              </template>
              <template #uploadText>
                <div class="box-outline"> 支持.PNG、.JPG格式,建议尺寸为32*32px,大小不超过5M </div>
              </template>
            </ContentUploadText>
          </template>
          <template #iconUpload>
            <ContentUploadText>
              <template #uploadImg>
                <Upload
                  name="avatar"
                  list-type="picture-card"
                  class="avatar-uploader"
                  :show-upload-list="false"
                  :customRequest="customUploadIconPic"
                  :before-upload="beforeUploadIconPic"
                >
                  <div v-if="iconPic" class="relative">
                    <img :src="iconPic" class="m-auto fill-img" />
                    <CloseCircleOutlined class="absolute icon-delete" @click.stop="handleDelete" />
                    <div style="background-color: #ccc; margin-top: 20px">重新上传</div>
                  </div>
                  <div v-else>
                    <div>
                      <Spin v-if="loading1" tip="正在上传中..." />
                      <PlusOutlined v-else />
                      <div class="ant-upload-text">上传</div>
                    </div>
                  </div></Upload
                >
              </template>
              <template #uploadText>
                <div class="box-outline"> 支持.ICO格式,建议尺寸为16*16px </div>
              </template>
            </ContentUploadText>
          </template>
          <template #bgUpload>
            <ContentUploadText>
              <template #uploadImg>
                <CustomUploadComp :imgUrl="bgPic" @setImg="handleSetBgImgUrl" />
              </template>
              <template #uploadText>
                <div class="box-outline">
                  支持.PNG、.JPG格式,建议尺寸为1920*1080px以上,大小不超过5M
                </div>
              </template>
            </ContentUploadText>
          </template>
          <template #colorInput="{ model, field }"
            ><Input disabled v-model:value="model[field]">
              <template #prefix> <input type="color" v-model="model[field]" /> </template
            ></Input>
          </template>
        </BasicForm>
      </div>
    </Card>
    <Loading v-bind="compState" />
    <Authority value="api:yt:platform:update:update">
      <a-button @click="handleUpdateInfo" type="primary" class="mt-4">保存信息</a-button>
    </Authority>
    <Authority value="api:yt:platform:data_reset:reset">
      <a-button @click="handleResetInfo" type="primary" class="ml-4">恢复默认设置</a-button>
    </Authority>
  </div>
</template>

<script lang="ts">
  import { defineComponent, ref, onMounted } from 'vue';
  import { Card, Upload, Input, Spin } from 'ant-design-vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { schemas } from '../config/CVIDraw.config';
  import { Loading } from '/@/components/Loading/index';
  import { useMessage } from '/@/hooks/web/useMessage';
  // import type { FileItem } from '/@/components/Upload/src/typing';
  import { iconUpload, getPlatForm, updatePlatForm, resetPlateInfo } from '/@/api/oem/index';
  import { PlusOutlined, CloseCircleOutlined } from '@ant-design/icons-vue';
  import { useUserStore } from '/@/store/modules/user';
  import { createLocalStorage } from '/@/utils/cache/index';
  import { Authority } from '/@/components/Authority';
  import ContentUploadText from './ContentUploadText.vue';
  import { CustomUploadComp } from './customUplaod/index';
  import { buildUUID } from '/@/utils/uuid';
  import { FileItem } from '../types';
  import { deleteFilePath } from '/@/api/oss/ossFileUploader';

  export default defineComponent({
    components: {
      BasicForm,
      Card,
      Loading,
      Upload,
      Input,
      PlusOutlined,
      Authority,
      ContentUploadText,
      Spin,
      CustomUploadComp,
      CloseCircleOutlined,
    },
    setup() {
      const loading = ref(false);
      const loading1 = ref(false);
      const loading2 = ref(false);
      const compState = ref({
        absolute: false,
        loading: false,
        tip: '拼命加载中...',
      });
      const { createMessage } = useMessage();
      const userStore = useUserStore();
      const storage = createLocalStorage();
      const [registerForm, { getFieldsValue, setFieldsValue, resetFields }] = useForm({
        schemas,
        showSubmitButton: false,
        showResetButton: false,
        labelWidth: 150,
        wrapperCol: {
          span: 10,
        },
      });

      const logoPic = ref();
      const iconPic = ref();
      const bgPic = ref();

      const handleSetBgImgUrl = (d) => {
        bgPic.value = d;
      };

      const handleSetLogoImgUrl = (d) => {
        logoPic.value = d;
      };
      // Icon上传
      async function customUploadIconPic({ file }) {
        if (beforeUploadIconPic(file)) {
          iconPic.value = '';
          loading1.value = true;
          const formData = new FormData();
          formData.append('file', file);
          const response = await iconUpload(formData);
          if (response.fileStaticUri) {
            iconPic.value = response.fileStaticUri;
            loading1.value = false;
          }
        }
      }
      const beforeUploadIconPic = (file: FileItem) => {
        const reg = /.(icon|ico)$/i;
        if (!reg.test(file.name)) {
          createMessage.error('只能上传.ico图片文件!');
          return false;
        }
        const isLt2M = (file.size as number) / 1024 < 500;
        if (!isLt2M) {
          createMessage.error('图片大小不能超过500KB!');
        }
        return isLt2M;
      };
      // 更新
      const handleUpdateInfo = async () => {
        try {
          const fieldValue = getFieldsValue();
          if (Reflect.has(fieldValue, 'logo')) {
            const file = (fieldValue.logo || []).at(0) || {};
            fieldValue.logo = file.url || null;
          }
          if (Reflect.has(fieldValue, 'icon')) {
            const file = (fieldValue.icon || []).at(0) || {};
            fieldValue.icon = file.url || null;
          }
          if (Reflect.has(fieldValue, 'background')) {
            const file = (fieldValue.background || []).at(0) || {};
            fieldValue.background = file.url || null;
          }
          compState.value.loading = true;
          if (Reflect.has(fieldValue, 'deleteLogoUrl') && fieldValue.deleteLogoUrl) {
            await deleteFilePath(fieldValue?.deleteLogoUrl);
            Reflect.deleteProperty(fieldValue, 'deleteLogoUrl');
          }
          if (Reflect.has(fieldValue, 'deleteIconUrl') && fieldValue.deleteIconUrl) {
            await deleteFilePath(fieldValue?.deleteIconUrl);
            Reflect.deleteProperty(fieldValue, 'deleteIconUrl');
          }
          if (Reflect.has(fieldValue, 'deleteBackgroundUrl') && fieldValue.deleteBackgroundUrl) {
            await deleteFilePath(fieldValue?.deleteBackgroundUrl);
            Reflect.deleteProperty(fieldValue, 'deleteBackgroundUrl');
          }

          await updatePlatForm({
            ...fieldValue,
            logo: fieldValue.logo || '',
            icon: fieldValue.icon || '',
            background: fieldValue.background || '',
          });
          compState.value.loading = false;
          createMessage.success('保存信息成功');

          setPlatFormInfo(fieldValue);
        } catch (e) {
          createMessage.error('保存信息失败');
        }
      };
      // 设置平台信息
      function setPlatFormInfo(newFieldValue) {
        // 保存store
        userStore.setPlatInfo(newFieldValue);
        // 保存本地缓存
        storage.set('platformInfo', newFieldValue);
      }

      // 删除浏览器ico图标
      const handleDelete = () => {
        iconPic.value = '';
      };

      onMounted(async () => {
        const res = await getPlatForm();
        if (res.logo) {
          res.logo = [{ uid: buildUUID(), name: 'name', url: res.logo }] as any;
        }
        if (res.icon) {
          res.icon = [{ uid: buildUUID(), name: 'name', url: res.icon }] as any;
        }
        if (res.background) {
          res.background = [{ uid: buildUUID(), name: 'name', url: res.background }] as any;
        }
        setFieldsValue(res);
        logoPic.value = res.logo;
        iconPic.value = res.icon;
        bgPic.value = res.background;
      });
      const handleResetInfo = async () => {
        try {
          compState.value.loading = true;
          await resetPlateInfo();
          compState.value.loading = false;
          createMessage.success('恢复出厂设置成功');
          resetFields();
          const res = await getPlatForm();
          setFieldsValue(res);
          logoPic.value = res.logo;
          iconPic.value = res.icon;
          bgPic.value = res.background;
        } catch (e) {
          compState.value.loading = false;
          createMessage.error('恢复出厂设置失败');
        }
      };
      return {
        registerForm,
        logoPic,
        iconPic,
        bgPic,
        customUploadIconPic,
        beforeUploadIconPic,
        compState,
        handleUpdateInfo,
        loading,
        loading1,
        loading2,
        handleResetInfo,
        handleSetBgImgUrl,
        handleSetLogoImgUrl,
        handleDelete,
      };
    },
  });
</script>

<style lang="less" scoped>
  .ant-upload-select-picture-card i {
    font-size: 32px;
    color: #999;
  }

  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }

  .box-outline {
    border: 1px dashed #d9d9d9;
  }

  .fill-img {
    width: 100%;
    height: 100%;
  }

  .icon-delete {
    top: -13px;
    right: -13px;
    color: rgb(146, 145, 145);
    font-size: 1rem;
    z-index: 9999;
  }
</style>