分支自 SuZhouGuanHong/TaiYuanTaiZhong

dengjunjie
2024-01-16 5884c9023393061afbe6d3d6e709e53e672ddde8
1
2
3
4
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
let base = {
  isPhone(val) {
    return /^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(val)
  },
  isDecimal(val) {
    return /(^[\-0-9][0-9]*(.[0-9]+)?)$/.test(val);
  },
  isNumber(val) {
    return /(^[\-0-9][0-9]*([0-9]+)?)$/.test(val);
  },
  isMail(val) {
    return /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(val);
  },
  isUrl(url) {
    return this.checkUrl(url);
  },
  checkUrl(url) {
    //url= 协议://(ftp的登录信息)[IP|域名](:端口号)(/或?请求参数)
    var strRegex =
      "^((https|http|ftp)://)?" + //(https或http或ftp):// 可有可无
      "(([\\w_!~*'()\\.&=+$%-]+: )?[\\w_!~*'()\\.&=+$%-]+@)?" + //ftp的user@  可有可无
      "(([0-9]{1,3}\\.){3}[0-9]{1,3}" + // IP形式的URL- 3位数字.3位数字.3位数字.3位数字
      "|" + // 允许IP和DOMAIN(域名)
      "(localhost)|" + //匹配localhost
      "([\\w_!~*'()-]+\\.)*" + // 域名- 至少一个[英文或数字_!~*\'()-]加上.
      "\\w+\\." + // 一级域名 -英文或数字  加上.
      "[a-zA-Z]{1,6})" + // 顶级域名- 1-6位英文
      "(:[0-9]{1,5})?" + // 端口- :80 ,1-5位数字
      "((/?)|" + // url无参数结尾 - 斜杆或这没有
      "(/[\\w_!~*'()\\.;?:@&=+$,%#-]+)+/?)$"; //请求参数结尾- 英文或数字和[]内的各种字符
    var re = new RegExp(strRegex, "i"); //i不区分大小写
    //将url做uri转码后再匹配,解除请求参数中的中文和空字符影响
    if (re.test(encodeURI(url))) {
      return true;
    }
    return false;
  },
  matchUrlIp(url, ip) { //url使用是否使用的当前ip
    if (!url || !ip) {
      return false;
    }
    return url.indexOf(ip.replace('https://', '').replace('http://', '')) >= 0
  },
  getImgSrc(src, httpUrl) {
    if (this.isUrl(src)) {
      return src;
    }
    if (httpUrl) {
      return httpUrl + src;
    }
    return src;
  },
  previewImg(src, httpUrl) { //图片预览,目前只支持单图片预览
    if (src && !this.isUrl(src) && httpUrl) {
      if (src.substr(0, 1) == "/" && httpUrl.substr(httpUrl.length - 1, 1) == "/") {
        src = src.substr(1)
      }
      src = (httpUrl + src);
    }
    let id = "vol-preview";
    let $div = document.getElementById(id);
    if (!$div) {
      $div = document.createElement("div");
      $div.setAttribute("id", "vol-preview");
      let $mask = document.createElement("div");
      $mask.style.position = "absolute";
      $mask.style.width = "100%";
      $mask.style.height = "100%";
      $mask.style.background = "black"
      $mask.style.opacity = "0.6";
      $div.appendChild($mask);
      $div.style.position = "fixed";
      $div.style.width = "100%";
      $div.style.height = "100%";
      // $div.style.overflow = "scroll";
      $div.style.top = 0;
      $div.style['z-index'] = 9999999;
      let $img = document.createElement("img");
      $img.setAttribute("class", "vol-preview-img");
      $img.style.position = "absolute";
      $img.style.top = "50%";
      $img.style.left = "50%";
      $img.style['max-width'] = "90%";
      $img.style['max-height'] = "90%";
      $img.style.transform = "translate(-50%,-50%)";
      // $img.src = src;
      $img.setAttribute("src", src);
      $div.appendChild($img);
      $div.addEventListener("click", function () {
        this.style.display = "none";
      })
      document.body.appendChild($div);
      return;
    }
    let $img1 = document.body.appendChild($div).querySelector(".vol-preview-img");
    // img.src = src;
    $img1.setAttribute("src", src);
    $div.style.display = "block";
  },
  //下载文件 $element 标签, url完整url, fileName 文件名, header 以key/value传值
  //backGroundUrl 后台url,如果后台url直接从后台下载,其他全部通过点击a标签下载
  dowloadFile(url, fileName, header, backGroundUrl) {
    if (!url) {
      alert('此文件没有url不能下载')
      return;
    }
    let $element = document.getElementById('dowonloadfile-a');
    if (!$element) {
      $element = document.createElement('a');
      $element.setAttribute("id", "dowonloadfile-a");
      document.body.append($element);
    }
    //url为一个完整的地址,并且不是后台api的地址,直接点击a标签下载
    // if (this.isUrl(url) && !this.matchUrlIp(url, backGroundUrl)) {
    // $element.href = url;
    // $element.click();
    // return;
    //  }
 
    if (!this.isUrl(url)) {
      if (!this.isUrl(backGroundUrl + url)) {
        console.log("文件路径不正确");
        alert('文件路径不正确')
        return;
      }
      url = backGroundUrl + url;
    }
    $element.href = url;
    $element.click();
    return;
 
    //通过后台api服务器下载
    if (!this.isUrl(url)) {
      if (!this.isUrl(backGroundUrl + url)) {
        alert('当前下载的文件url【' + url + '】不正确')
        return;
      }
      url = backGroundUrl + url;
    }
    let xmlResquest = new XMLHttpRequest();
    xmlResquest.open("GET", url, true);
    xmlResquest.setRequestHeader("Content-type", "application/json");
    if (header && typeof header == 'object') {
      for (const key in header) {
        xmlResquest.setRequestHeader(
          key,
          header[key]
        );
      }
    }
 
    xmlResquest.responseType = "blob";
    xmlResquest.onload = function (oEvent) {
      if (xmlResquest.status != 200) {
        return alert('没有下载到此文件的信息')
      }
      let content = xmlResquest.response;
      $element.download = fileName;
      let blob = new Blob([content]);
      $element.href = URL.createObjectURL(blob);
      $element.click();
    };
    xmlResquest.send();
  },
  downloadImg(data) {
    if (!data.url || !data.callback || typeof data.callback != 'function') {
      return;
    }
    //url, backGroundUrl, header, callback
    if (this.isUrl(data.url) && !this.matchUrlIp(data.url, data.backGroundUrl)) {
      return data.url;
    }
    //通过后台api服务器下载
    if (!this.isUrl(data.url)) {
      if (!this.isUrl(data.backGroundUrl + data.url)) {
        return;
      }
      data.url = data.backGroundUrl + data.url;
    }
    var xmlResquest = new XMLHttpRequest();
    xmlResquest.open("get", data.url, true);
    xmlResquest.responseType = "blob";
    xmlResquest.setRequestHeader("Content-Type", "application/json");
    if (data.header && typeof data.header == 'object') {
      for (const key in data.header) {
        xmlResquest.setRequestHeader(
          key,
          data.header[key]
        );
      }
    }
    xmlResquest.onload = function () {
      if (this.status == 200) {
        var blob = this.response;
        callback(window.URL.createObjectURL(blob));
      }
    };
    xmlResquest.send();
  },
  //2020.06.01增加通用方法,将普通对象转换为tree结构
  //data数据格式[
  //     { name: 'tree1', id: 1, parentId: 0 },
  //     { name: 'tree2', id: 2, parentId: 0 }]
 
  //1、id与parentId这两个字段必须有
  //2、树形tree需要注意Id与parentId循环依赖的问题
  //3、callback每次生成一新的节点的时回调的方法
  convertTree(data, callback) {
    var root_data = [];
    data.forEach(x => {
      if (!x.hidden && (x.id === x.parentId || !data.some(s => { return x.parentId == s.id }))) {
        x.children = [];
        x.isRoot = true;
        callback && callback(x, data, true);
        root_data.push(x);
        getTree(x.id, x, data, callback);
      }
    });
    return root_data;
  }
 
}
export default base;
 
//2020.06.01增加通用方法,将普通对象转换为tree结构
function getTree(id, node, data, callback) {
  data.forEach(x => {
    if (!x.hidden && x.parentId == id) {
      if (!node.children) node.children = [];
      callback && callback(x, node, false);
      node.children.push(x);
      getTree(x.id, x, data,callback);
    }
  });
}