index.js 8.96 KB
Newer Older
1
import React, { useState } from 'react';
田翔's avatar
田翔 committed
2
import { message } from 'antd';
3
import { widgetData } from '../constant'
4

田翔's avatar
田翔 committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
export function downloadFunc(url, name, target = '_self') {
  const a = document.createElement('a');
  a.href = url;
  a.target = target;
  a.download = name;
  a.click();
  a.remove();
}

/**
 *  @Description: 校验文件的名称是否包含特殊字符
 *  @Params: {Object: File} file file对象 { special:Boolean } 是否去除/的匹配
 *  @Date: 2021/12/8
 *  @Author: ChenLong
 *  @Return {Object} {type: error | success ,content: 提示...}
 * */
export function filenameVerification(file, special) {
  // 文件名含有特殊字符  提示不能上传   {+,:/?#[]@!$&\\*+;=}
  // 规则对象(flag)
  var flag = !special ? new RegExp('[`~!@#$^&*=|{}\':;\',\\[\\]/?~!@#¥&*——|{}【】‘;:”“\'。,、?]') : new RegExp('[`~!@#$^&*=|{}\':;\',\\[\\]?~!@#¥&*——|{}【】‘;:”“\'。,、?]');
  if (flag.test(file.name)) {
    return {
      type: 'error',
      content: `文件名格式错误,请检查文件名是否含有特殊字符${'~!@#$^&*=|{}\':;\',\\[\\]/?~!@#¥&*——|{}【】‘;:”“\'。,、?'}`,
    };
  }
  return {
    type: 'success',
    content: `上传成功!`,
  };
}

/**
 *  @Description: 判断数据类型
 *  @Params: 参数描述
 *  @Date: 2021/12/17
 *  @Author: ChenLong
 *  @Return: '[object 各自对相应的类型]','[object Function]', '[object Object]', '[object Array]', '[object String]', '[object Number]', '[object Boolean]', '[object Undefined]', '[object Null]', '[object Date]'
 * */
export function returnDataType(data) {
  return Object.prototype.toString.call(data);
}

export function parseSQL2JSON(str) {
  let finalArray = [];
  // 1.支持以下字符, > < 都包含等于
  // 2.关键字和关键字符必须前后空格
  // 3.仅支持单个关键字或者关键字符的筛选条件

  let queryWhere = [];

  /** @Tips: 台账名和字段的逻辑 */
  // 1. 解析台账名.字段名[.n](资产台账.编码[.n]) 是表明加载的台账和台账选择器返回的字段名,并且需要验证该字段【不可为空】
  let strArray = str.split('?');
  let accountNameAndField = strArray[0];

  let accountArray = accountNameAndField.split('.');
  let accountName = accountArray[0]; // 台账
  let returnField = accountArray[1]; // 需要返回的字段
  queryWhere.push({
    field: returnField,
    type: '不等于',
    value: '',
  });
  let isMultiple = accountArray.includes('n'); // 是否多选
  /** @End: 台账解析结束 */

  let otherCondition = strArray[1];
  /** @Tips: 1. 判断是否多条件 and */
  let otherConditionArray = otherCondition.split('and').map(item => item.trim());
  /** @Tips: 2. 解析规则,判断是否有关键词并组成JSON */
  otherConditionArray.forEach(item => {

  });

  return {
    accountName,
    isMultiple,
    returnField,
    queryWheres: [],
  };
}
田翔's avatar
田翔 committed
87

88 89 90 91 92 93
/**
 * @description 墨卡托转经纬度
 * @param {*} x 
 * @param {*} y 
 * @returns 
 */
94 95 96 97 98 99 100 101 102 103
export const mercatorToLngLat = (x, y) => {
  const EARTH_RAD = 6378137
  const radToAngle = (rad) => {
    return rad * (180 / Math.PI)
  }
  let lng = radToAngle(x) / EARTH_RAD
  let lat = radToAngle(2 * Math.atan(Math.exp(y / EARTH_RAD)) - Math.PI / 2)
  return [lng, lat]
}

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/**
 * @description 经纬度转墨卡托
 * @param {*} x 
 * @param {*} y 
 * @returns 
 */
export const lngmkt = (poi) => {
  var mercator = {};
  var earthRad = 6378137.0;
  mercator.x = ((poi.lng * Math.PI) / 180) * earthRad;
  var a = (poi.lat * Math.PI) / 180;
  mercator.y = (earthRad / 2) * Math.log((1.0 + Math.sin(a)) / (1.0 - Math.sin(a)));
  return mercator; //[12727039.383734727, 3579066.6894065146
};

田翔's avatar
田翔 committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132
//判断是否为JSON字符串
export const isJson = (json) => {
  if (typeof json === 'string') {
    try {
      let obj = JSON.parse(json)
      if (typeof obj === 'object' && obj) {
        return true
      }
    } catch (e) {
      return false
    }
  }
  return false
}
133 134

export const isObject = (obj) => typeof obj === 'object'
田翔's avatar
田翔 committed
135

136 137
export const isArray = (array) => Array.isArray(array) && array.length

田翔's avatar
田翔 committed
138 139 140 141 142 143 144 145 146 147
//防抖
export const debounce = (fn) => {
  let t = null
  return function (e) {
    const context = this
    const args = arguments
    if (t) {
      clearTimeout(t)
    }
    t = setTimeout(function () {
148 149 150 151
      if (typeof fn === 'function') {
        fn.call(context, ...args)
      }
    }, 300)
田翔's avatar
田翔 committed
152
  }
田翔's avatar
田翔 committed
153 154 155
}

//生成随机ID
田翔's avatar
田翔 committed
156
export function getNanoid(len = 6, left = '分组名称_') {
田翔's avatar
田翔 committed
157 158 159 160 161
  var orgStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  let returnStr = "";
  for (var i = 0; i < len; i++) {
    returnStr += orgStr.charAt(Math.floor((Math.random() * orgStr.length)));
  }
田翔's avatar
田翔 committed
162
  return `${left}${returnStr}`
田翔's avatar
田翔 committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176
}

//手动校验配置信息
export const getVerify = (json) => {
  if (isObject(json)) {
    let properties = json?.properties
    if (isObject(properties)) {
      for (let v in properties) {
        // if (properties[v].title === '(未分组)') {
        //   return message.error(`包含未分组字段,请将分组命名`)
        // }
        let child = properties[v]?.properties
        if (isObject(child)) {
          for (let s in child) {
177
            const { widget, sourceType, options, dictionary, tableName, fieldName, fieldParent, accountName, fieldshine } = child[s]
田翔's avatar
田翔 committed
178 179 180 181 182 183 184 185
            if (['ComboBox', 'RadioButton', 'CheckBox'].includes(widget)) {
              if (sourceType === '手动输入' && !options.length) {
                return message.error(`字段【${s}】配置,选项值必填!`)
              }
              if (sourceType === '数据字典' && !dictionary) {
                return message.error(`字段【${s}】配置,数据字典必填!`)
              }
              if (sourceType === '表数据' && !tableName && !fieldName) {
186 187 188 189 190 191
                return message.error(`字段【${s}】配置,表名与字段名必填!`)
              }
            }
            if (['RelationForm'].includes(widget)) {
              if (!child[s]['映射字段'].length || !child[s]['台账名称']) {
                return message.error(`字段【${s}】配置,台账名称与映射字段必填!`)
田翔's avatar
田翔 committed
192 193
              }
            }
194 195 196 197 198 199 200 201 202 203
            if (['RelevanceSelect'].includes(widget)) {
              if (!fieldParent || !dictionary) {
                return message.error(`字段【${s}】配置,父字段名与数据字典必填!`)
              }
            }
            if (['AccountSelector'].includes(widget)) {
              if (!accountName || !fieldshine) {
                return message.error(`字段【${s}】配置,台账名称与映射字段必填!`)
              }
            }
田翔's avatar
田翔 committed
204 205 206 207 208 209 210 211
          }
        }
      }
    }
  }
  return true
}

212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
const getWidgetSetting = (type, { title, tableName, tableType }) => {
  if (type === '日期') {
    return {
      title: title,
      "type": "string",
      "widget": "DateTime",
      "placeholder": "请选择日期",
      tableNameParent: tableName,
      tableTypeParent: tableType,
      "required": false,
      "disabled": false,
      "format": "date",
      "options": "默认为空",
      "groupStyle": {},
      "labelWidth": 110
    }
  } else if (type === '数值') {
    return {
田翔's avatar
田翔 committed
230
      title: title,
231 232 233 234 235 236 237 238 239 240 241 242 243 244
      "type": "string",
      "widget": "NumberInput",
      "presetValue": "0",
      "placeholder": "请输入内容",
      tableNameParent: tableName,
      tableTypeParent: tableType,
      "rules": [],
      "required": false,
      "disabled": false,
      "groupStyle": {},
      "labelWidth": 110,
      "IsSystemField": false
    }
  }
田翔's avatar
田翔 committed
245
  return {
246
    title: title,
田翔's avatar
田翔 committed
247 248 249
    "type": "string",
    "widget": "TextInput",
    "placeholder": "请输入内容",
250
    tableNameParent: tableName,
田翔's avatar
田翔 committed
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    tableTypeParent: tableType,
    "maxLength": 200,
    "rules": [],
    "uniqueVerify": null,
    "required": false,
    "disabled": false,
    "isStoreID": false,
    "isQRCode": false,
    "groupStyle": {},
    "labelWidth": 110
  }
}

//将字段设置
export const setFieldJson = (json, fieldName) => {
  let properties = json?.properties
  let keys = []
  let parent = {}
  let extaObj = {}
  Object.keys(properties).forEach(v => {
    parent[v] = properties[v]
    let child = properties[v]?.properties
    if (isObject(child)) {
      Object.keys(child).forEach(s => {
        keys.push(s)
      })
    }
  })
  fieldName.forEach(j => {
    if (!keys.includes(j.name)) {
281
      extaObj[j.name] = getWidgetSetting(j.shapeTip, { ...json, title: j.name })
田翔's avatar
田翔 committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295
    }
  })
  if (JSON.stringify(extaObj) !== '{}') {
    parent[getNanoid()] = {
      title: '(未分组)',
      type: "object",
      collapsed: false,
      properties: extaObj
    }
  }
  return {
    ...json,
    properties: parent,
  }
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
}

//字段名对应的形态
export const getFieldInfo = (formJson) => {
  let obj = {}
  let parent = formJson?.properties
  if (isObject(parent)) {
    for (let v in parent) {
      let child = parent[v]?.properties
      if (isObject(child)) {
        for (let s in child) {
          obj[s] = { ...child[s], ...widgetData[child?.[s]?.widget] }
        }
      }
    }
  }
  return obj
田翔's avatar
田翔 committed
313
}