setTokenAndInitSystem.js 3.0 KB
Newer Older
钟是志's avatar
钟是志 committed
1 2 3 4 5 6 7
import { getUrlInfo } from '@/webPublic/one_stop_public/DetailForAudit/utils';
import React from 'react';
import { getToken, setToken } from '@/webPublic/one_stop_public/utils/token';
import { getInfo } from '@/highOrderComponent/Service';
import { Divider } from 'antd';
import config from '@/config/config';
import {
8 9
  setSysConfig,
  getSysConfig as getSysConfigLocalStorage,
钟是志's avatar
钟是志 committed
10 11
} from '@/webPublic/zyd_private/utils/basiclayout';
import { getOneStopConfig } from '@/webPublic/zyd_public/utils/utils';
12
import { getIsYsgz } from '@/webPublic/zyd_public/utils/getSchoolType';
钟是志's avatar
钟是志 committed
13

钟是志's avatar
钟是志 committed
14
function formatUploadFile(fileList) {
15 16 17 18 19 20 21 22 23
  if (!fileList || !Array.isArray(fileList)) return [];
  return JSON.stringify(
    fileList.map((i) => ({
      uid: i.uid,
      name: i.name,
      status: i.status,
      url: i.filePath || i.response.filePath,
    })),
  );
钟是志's avatar
钟是志 committed
24 25
}

钟是志's avatar
钟是志 committed
26
export const valueTypes = {
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  0: {
    name: '文本',
    inputComponent: 'input',
  },
  1: {
    name: '文件',
    inputComponent: 'upload',
    formatToServer: formatUploadFile, // 新增或保存时候转换格式
    formatToEdit: (str) => {
      const fileArr = JSON.parse(str);
      if (!Array.isArray(fileArr)) return [];
      return fileArr;
    }, //  编辑时,转换成可以在表单回显的格式
    render: (val) => {
      //   在列表上的显示方式
      return JSON.parse(val)
        .map((item, i) => (
          <>
            {i > 0 && <Divider type="vertical"/>}
            <a
              target="_blank"
              rel="noopener noreferrer"
              key={item.uid}
              href={item.url && (item.url.startsWith('http') ? item.url : config.dfs + item.url)}>
              {item.name}
            </a>
          </>
        ));
    },
  },
  2: {
    name: '富文本',
    inputComponent: 'input',
  },
钟是志's avatar
钟是志 committed
61 62 63
};

export function getSysConfig() {
64 65 66 67 68 69

  let url = '/WebConfigApi/getPage?oauthPub=true';
  if(getIsYsgz()){
    url = '/WebConfigApi/getPage';
  }
  return getInfo({ pageSize: 10000 }, url)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    .then((res) => {
      if (res && res.rows && res.rows.length) {
        const formattered = res.rows.reduce((acc, cur) => {
          let index = cur.valueType || '0';
          const { formatToEdit } = valueTypes[index];
          let { value } = cur;
          if (formatToEdit && typeof formatToEdit === 'function') {
            value = formatToEdit(value);
          }
          return {
            ...acc,
            [cur.dictCode]: value,
          };
        }, {});
        setSysConfig(formattered);
        return formattered;
      }
    });
钟是志's avatar
钟是志 committed
88
}
89 90 91 92 93 94 95 96

export function setTokenByUrl(){
  let token = getUrlInfo().token;
  if (token && token.length > 10 && getToken() !== token) {
    setToken(token);
  }
}

97 98 99 100 101 102 103 104 105 106
export default async function setTokenAndInitSystem() {
  let token = getUrlInfo().token;
  if (token && token.length > 10 && getToken() !== token) {
    return new Promise((resolve, reject) => {
      resolve(Promise.all([setToken(token), getSysConfig(), getOneStopConfig()]));
    });
  }
  return new Promise((resolve, reject) => {
    resolve(true);
  });
钟是志's avatar
钟是志 committed
107 108

}