EnterpriseInfo.vue 9.03 KB
<template>
  <div class="card">
    <Card :bordered="false" class="card">
      <BasicForm @register="registerForm">
        <template #qrcode>
          <Upload
            name="avatar"
            list-type="picture-card"
            class="avatar-uploader"
            :show-upload-list="false"
            :customRequest="customUploadqrcodePic"
            :before-upload="beforeUploadqrcodePic"
          >
            <img v-if="qrcodePic" :src="qrcodePic" alt="avatar" />
            <div v-else>
              <div style="margin-top: 30px">
                <PlusOutlined style="font-size: 30px" />
              </div>
              <div
                class="ant-upload-text flex"
                style="width: 280px; height: 130px; align-items: center"
              >
                支持.PNG、.JPG、.SVG格式,建议尺寸为300px × 300px(及以上),大小不超过2M。</div
              >
            </div>
          </Upload>
        </template></BasicForm
      ></Card
    >
    <Loading v-bind="compState" />
    <a-button
      @click="handleUpdateInfo"
      size="large"
      type="primary"
      style="margin-top: 20px; background-color: #2950f7; border-radius: 5px"
      >更新基本信息</a-button
    >
  </div>
</template>

<script lang="ts">
  import { defineComponent, onMounted, ref } from 'vue';
  import { Card, Upload } from 'ant-design-vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { schemas } from '../config/enterPriseInfo.config';
  import { getEnterPriseDetail, updateEnterPriseDetail } from '/@/api/oem/index';
  import { Loading } from '/@/components/Loading';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getTownChild } from '/@/api/oem/index';
  import { useUserStore } from '/@/store/modules/user';
  import { createLocalStorage } from '/@/utils/cache';
  import { PlusOutlined } from '@ant-design/icons-vue';
  import type { FileItem } from '/@/components/Upload/src/typing';
  import { qrcodeUpload } from '/@/api/oem/index';
  export default defineComponent({
    components: {
      Card,
      BasicForm,
      Loading,
      Upload,
      PlusOutlined,
    },
    setup() {
      const compState = ref({
        absolute: false,
        loading: false,
        tip: '拼命加载中...',
      });
      const [registerForm, { getFieldsValue, setFieldsValue, updateSchema }] = useForm({
        labelWidth: 80,
        schemas,
        showResetButton: false,
        showSubmitButton: false,
        wrapperCol: {
          span: 10,
        },
      });
      const { createMessage } = useMessage();

      const qrcodePic = ref();
      const customUploadqrcodePic = async ({ file }) => {
        if (beforeUploadqrcodePic(file)) {
          const formData = new FormData();
          formData.append('file', file);
          const response = await qrcodeUpload(formData);
          if (response.fileStaticUri) {
            qrcodePic.value = response.fileStaticUri;
          }
        }
      };
      const beforeUploadqrcodePic = (file: FileItem) => {
        const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
        if (!isJpgOrPng) {
          createMessage.error('只能上传图片文件!');
        }
        const isLt2M = (file.size as number) / 1024 / 1024 < 2;
        if (!isLt2M) {
          createMessage.error('图片大小不能超过2MB!');
        }
        return isJpgOrPng && isLt2M;
      };
      // 更新
      const handleUpdateInfo = async () => {
        try {
          compState.value.loading = true;
          const fieldsValue = getFieldsValue();
          const newFieldValue = {
            ...fieldsValue,
            codeProv: fieldsValue.nameProv,
            codeCity: fieldsValue.nameCity,
            codeCoun: fieldsValue.nameCoun,
            codeTown: fieldsValue.nameTown,
            qrCode: qrcodePic.value,
          };
          await updateEnterPriseDetail(newFieldValue);
          compState.value.loading = false;
          createMessage.success('更新信息成功');
          setEnterPriseInfo(newFieldValue);
        } catch (e) {
          createMessage.error('更新信息失败');
        }
      };

      const userStore = useUserStore();
      const storage = createLocalStorage();

      // 设置企业信息
      function setEnterPriseInfo(newFieldValue) {
        // 保存store
        userStore.setEnterPriseInfo(newFieldValue);
        // 保存本地缓存
        storage.set('enterpriseInfo', newFieldValue);
      }

      // 地区显示回显和数据联动
      async function updateCityData(
        codeProv: string,
        codeCity: string,
        codeCoun: string,
        codeTown: string
      ) {
        const nameCity = await getTownChild('codeProv', codeProv);
        const nameCoun = await getTownChild('codeCity', codeCity);
        const nameTown = await getTownChild('codeCoun', codeCoun);
        nameCity.forEach((item) => {
          item.label = item.nameCity;
          item.value = item.codeCity;
        });
        nameCoun.forEach((item) => {
          item.label = item.nameCoun;
          item.value = item.codeCoun;
        });
        nameTown.forEach((item) => {
          item.label = item.nameTown;
          item.value = item.codeTown;
        });
        setFieldsValue({
          nameProv: codeProv,
          nameCity: codeCity,
          nameCoun: codeCoun,
          nameTown: codeTown,
        });
        updateSchema({
          field: 'nameTown',
          componentProps: {
            options: nameTown,
          },
        });
        updateSchema({
          field: 'nameCoun',
          componentProps: {
            options: nameCoun,
            async onChange(value) {
              if (value === undefined) {
                setFieldsValue({
                  nameTown: undefined,
                });
                updateSchema({
                  field: 'nameTown',
                  componentProps: {
                    options: [],
                  },
                });
              }
              let nameTown = await getTownChild('codeCoun', value);
              nameTown.forEach((item) => {
                item.label = item.nameTown;
                item.value = item.codeTown;
              });
              setFieldsValue({
                nameTown: undefined,
              });
              updateSchema({
                field: 'nameTown',
                componentProps: {
                  placeholder: '请选择街道/城镇',
                  options: nameTown,
                },
              });
            },
          },
        });
        updateSchema({
          field: 'nameCity',
          componentProps: ({ formModel }) => {
            return {
              options: nameCity,
              onChange: async (value) => {
                let nameCoun = await getTownChild('codeCity', value);
                if (value === undefined) {
                  formModel.nameCoun = undefined; //  reset city value
                  formModel.nameTown = undefined;
                  nameCoun = [];
                  updateSchema({
                    field: 'nameTown',
                    componentProps: {
                      options: [],
                    },
                  });
                }
                nameCoun.forEach((item) => {
                  item.label = item.nameCoun;
                  item.value = item.codeCoun;
                });
                formModel.nameCoun = undefined; //  reset city value
                formModel.nameTown = undefined;
                updateSchema({
                  field: 'nameCoun',
                  componentProps: {
                    // 请选择区
                    options: nameCoun,
                    async onChange(value) {
                      let nameTown = await getTownChild('codeCoun', value);
                      if (value === undefined) {
                        formModel.nameTown = undefined;
                        nameTown = [];
                      }
                      nameTown.forEach((item) => {
                        item.label = item.nameTown;
                        item.value = item.codeTown;
                      });

                      formModel.nameTown = undefined;
                      updateSchema({
                        field: 'nameTown',
                        componentProps: {
                          placeholder: '请选择街道/城镇',
                          options: nameTown,
                        },
                      });
                    },
                  },
                });
              },
            };
          },
        });
      }

      onMounted(async () => {
        const res = await getEnterPriseDetail();
        const { codeProv, codeCity, codeCoun, codeTown } = res.sysTown;
        updateCityData(codeProv, codeCity, codeCoun, codeTown);
        setFieldsValue(res);
        qrcodePic.value = res.qrCode;
      });

      return {
        registerForm,
        compState,
        qrcodePic,
        handleUpdateInfo,
        customUploadqrcodePic,
        beforeUploadqrcodePic,
      };
    },
  });
</script>

<style lang="less" scoped></style>