WYB
2021-03-22 91b8cdad021ab052e4991f3d41834a6f0ddc36b8
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Security.Cryptography;
using System.Data;
using System.Web;
 
namespace JiepeiWMS.Extends
{
    public static class Extension
    {
        /// <summary>
        /// 安全输出字符串(页面内容)
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string _ToSafe(this string s)
        {
            return System.Web.HttpUtility.HtmlEncode(s);
        }
 
        /// <summary>
        /// 返回字符并编码
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string _ToStringSafe(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return "";
            }
            return _ToSafe(s);
        }
 
 
        /// <summary>
        /// 去除空格并编码
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string _TrimSafe(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return "";
            }
            return _ToSafe(s.Trim());
        }
 
 
        /// <summary>
        /// 转换INT类型
        /// </summary>
        /// <param name="s"></param>
        /// <param name="defautValue"></param>
        /// <returns></returns>
        public static int _ToInt32(this string s, int defautValue)
        {
            int result = defautValue;
            if (string.IsNullOrWhiteSpace(s))
            {
                return result;
            }
            if (int.TryParse(s, out result))
            {
                return result;
            }
            return defautValue;
        }
 
        /// <summary>
        /// 转换INT类型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int _ToInt32(this string s)
        {
            return _ToInt32(s, 0);
        }
 
        /// <summary>
        /// 转换INT类型
        /// </summary>
        /// <param name="s"></param>
        /// <param name="defautValue"></param>
        /// <returns></returns>
        public static long _ToInt64(this string s, long defautValue)
        {
            long result = defautValue;
            if (string.IsNullOrWhiteSpace(s))
            {
                return result;
            }
            if (long.TryParse(s, out result))
            {
                return result;
            }
            return defautValue;
        }
 
        /// <summary>
        /// 转换INT类型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static long _ToInt64(this string s)
        {
            return _ToInt64(s, 0);
        }
 
        /// <summary>
        /// 转换成FLOAT类型
        /// </summary>
        /// <param name="s"></param>
        /// <param name="defautValue"></param>
        /// <returns></returns>
        public static float _ToFloat(this string s, float defautValue)
        {
            float result = defautValue;
            float.TryParse(s, out result);
            return result;
        }
 
        /// <summary>
        /// 转换成FLOAT类型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static float _ToFloat(this string s)
        {
 
            return _ToFloat(s, 0);
        }
 
        /// <summary>
        /// 转换成Double类型
        /// </summary>
        /// <param name="s"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static double _ToDouble(this string s, double defaultValue)
        {
            double result = 0;
            if (double.TryParse(s, out result))
            {
                return result;
            }
            return defaultValue;
        }
        /// <summary>
        /// 转换成Double类型
        /// </summary>
        /// <param name="s"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static double? _ToDouble(this object s, double defaultValue)
        {
            double result = 0;
            if (double.TryParse(s.ToString(), out result))
            {
                return result;
            }
            return defaultValue;
        }
 
 
        /// <summary>
        /// 转换成Double类型
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static double _ToDouble(this string s)
        {
            return _ToDouble(s, 0);
        }
 
        public static decimal _ToDecimal(this string Txt, decimal defaultValue)
        {
 
            decimal result = 0;
            if (decimal.TryParse(Txt, out result))
            {
                return result;
            }
            return defaultValue;
        }
 
        public static decimal _ToDecimal(this string s)
        {
 
            return _ToDecimal(s, 0);
        }
 
 
        public static byte _ToByte(this string s)
        {
            byte b = 0;
            byte.TryParse(s, out b);
            return b;
        }
 
        /// <summary>
        /// 转换日期格式
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>, 
        public static DateTime _ToDateTime(this string s)
        {
            DateTime time;
            if (DateTime.TryParse(s, out time))
            {
                return time;
            }
            else
            {
                return _InitDateTime();
            }
        }
        public static DateTime _InitDateTime()
        {
            return DateTime.Parse("1900-01-01");
        }
 
        /// <summary>
        /// 过滤字符串s中左尖括号和右尖括号之间的内容
        /// add by sunyichao 2015-09-14
        /// </summary>
        /// <param name="s">待处理字符串</param>
        /// <param name="filerExtension">过滤字符串,为HTML标签的元素名,eg:过滤div,则传值div</param>
        /// <returns></returns>
        public static string _ToFilterHTMLElement(this string s, string filerExtension)
        {
            if (string.IsNullOrEmpty(s))
            {
                return "";
            }
            else
            {
                if (s.Contains("<"))
                {
                    s = new Regex(@"<" + filerExtension + "[^>]*>(.+?)</" + filerExtension + ">").Replace(s, "");
                }
 
                return s;
            }
 
        }
        public static string _ReplaceHtmlTag(this string html, int length = 0)
        {
            string strText = System.Text.RegularExpressions.Regex.Replace(html, "<[^>]+>", "");
            strText = System.Text.RegularExpressions.Regex.Replace(strText, "&[^;]+;", "");
 
            if (length > 0 && strText.Length > length)
                return strText.Substring(0, length);
 
            return strText;
        }
        public static string _ToUrlDecode(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return "";
            }
            return System.Web.HttpUtility.UrlDecode(s.Trim());
        }
        /// <summary>
        /// 例 : "3,2,3,1"字符串 返回 整数型List
        /// </summary>
        /// <param name="str"></param>
        /// <param name="split"></param>
        /// <returns></returns>
        public static List<int> _ToIntList(this string str, char split)
        {
            string[] arr = str.Split(split);
            List<int> list = new List<int>();
            foreach (string item in arr)
            {
                if (_ToInt32(item) > 0 && list.IndexOf(_ToInt32(item)) == -1)
                {
                    list.Add(_ToInt32(item));
                }
            }
 
            return list;
        }
 
        public static bool _ToBool(this string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return false;
            }
            s = s.ToLower();
            if (s == "true" || s == "1")
            {
                return true;
            }
            return false;
 
        }
        public static bool _IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }
        public static string _FormatWith(this string s, params object[] args)
        {
            return string.Format(s, args);
        }
        /// <summary>
        /// 数字转中文
        /// </summary>
        /// <param name="number">eg: 22</param>
        /// <returns></returns>
        public static string _NumberToChinese(this int number)
        {
            string res = string.Empty;
            string str = number.ToString();
            string schar = str.Substring(0, 1);
            switch (schar)
            {
                case "1":
                    res = "一";
                    break;
                case "2":
                    res = "二";
                    break;
                case "3":
                    res = "三";
                    break;
                case "4":
                    res = "四";
                    break;
                case "5":
                    res = "五";
                    break;
                case "6":
                    res = "六";
                    break;
                case "7":
                    res = "七";
                    break;
                case "8":
                    res = "八";
                    break;
                case "9":
                    res = "九";
                    break;
                default:
                    res = "零";
                    break;
            }
            if (str.Length > 1)
            {
                switch (str.Length)
                {
                    case 2:
                    case 6:
                        res += "十";
                        break;
                    case 3:
                    case 7:
                        res += "百";
                        break;
                    case 4:
                        res += "千";
                        break;
                    case 5:
                        res += "万";
                        break;
                    default:
                        res += "";
                        break;
                }
                res += _NumberToChinese(int.Parse(str.Substring(1, str.Length - 1)));
            }
            return res;
        }
 
        /// <summary>
        /// 随机打乱列表
        /// </summary>
        /// <param name="myList"></param>
        /// <returns></returns>
        public static List<string> _ToListRandom(this List<string> myList)
        {
 
            Random ran = new Random();
            List<string> newList = new List<string>();
            int index = 0;
            string temp = "";
            for (int i = 0; i < myList.Count; i++)
            {
 
                index = ran.Next(0, myList.Count);
                if (index != i)
                {
                    temp = myList[i];
                    myList[i] = myList[index];
                    myList[index] = temp;
                }
            }
            return myList;
        }
 
        #region 戴雁冰扩展
        /// <summary>
        /// 提取文件目录(\结尾)
        /// </summary>
        /// <param name="AbsFullFileName">包含绝对路径完整文件名</param>
        /// <returns>文件目录</returns>
        public static string _FthPath(this string AbsFullFileName)
        {
            string filename;
            return _FthPath(AbsFullFileName, out filename);
        }
        /// <summary>
        /// 提取文件目录(\结尾)
        /// </summary>
        /// <param name="AbsFullFileName">包含绝对路径完整文件名</param>
        /// <param name="FileName">文件名(无路径无扩展名)</param>
        /// <returns>文件目录</returns>
        public static string _FthPath(this string AbsFullFileName, out string FileName)
        {
            string extname;
            return _FthPath(AbsFullFileName, out FileName, out extname);
        }
        /// <summary>
        /// 提取文件目录(\结尾)
        /// </summary>
        /// <param name="AbsFullFileName">包含绝对路径完整文件名</param>
        /// <param name="FileName">文件名(无路径无扩展名)</param>
        /// <param name="ExtName">扩展名</param>
        /// <returns>文件目录</returns>
        public static string _FthPath(this string AbsFullFileName, out string FileName, out string ExtName)
        {
            //输出空
            if (string.IsNullOrEmpty(AbsFullFileName))
            {
                FileName = ExtName = ""; return "";
            }
            //找最后一级名称
            int nidx = AbsFullFileName.LastIndexOf("\\");
            if (nidx < 0) { nidx = AbsFullFileName.LastIndexOf("/"); }
            if (nidx < 0) { FileName = AbsFullFileName; }
            else { FileName = AbsFullFileName.Substring(nidx + 1); }
            //是否为路径
            int at = FileName.LastIndexOf(".");
            if (at < 0)
            {
                FileName = ExtName = ""; return AbsFullFileName.Substring(0, nidx + 1);
            }
            ExtName = FileName.Substring(at + 1);
            FileName = FileName.Substring(0, at);
            //输出文件名及路径
            return AbsFullFileName.Substring(0, nidx + 1);
        }
 
        /// <summary>
        /// 清除Html标签
        /// </summary>
        /// <param name="Html">Html代码</param>
        /// <returns></returns>
        public static string _ClrHtmlTags(this string Html)
        {
            return Regex.Replace(Html, @"\<[\/]?([^\s]+?)(?:[^""'>]*?([""'])[^""']+\2)*[^\>]*?>", string.Empty);
        }
        /// <summary>
        /// 复制当前字符串Num次并连接在一起
        /// </summary>
        /// <param name="Text">文本</param>
        /// <param name="Num">复制次数</param>
        /// <returns>文本</returns>
        public static string _CopyAndJoin(this string Text, int Num)
        {
            StringBuilder sb = new StringBuilder();
            for (var i = 0; i < Num; i++)
            {
                sb.Append(Text);
            }
            return sb.ToString();
        }
        /// <summary>
        /// 转换成整型数组
        /// </summary>
        /// <param name="Src">整型列表字符串</param>
        /// <param name="SeparatorChar">分隔符</param>
        /// <param name="IsClearError">是否移除转换错误的值</param>
        /// <param name="ErrorDefValue">错误时的默认值(只有IsClearError=false时有效)</param>
        /// <returns>整型列表</returns>
        public static List<int> _ToIntList(this string Src, char SeparatorChar = ',', bool IsClearError = true, int ErrorDefValue = 0)
        {
            List<int> l = new List<int>();
            Action<int> fn;
            if (IsClearError) { fn = t => { }; }
            else { fn = t => l.Add(t); }
            foreach (string i in Src.Split(SeparatorChar))
            {
                int t = 0;
                if (int.TryParse(i, out t)) { l.Add(t); }
                else { fn(ErrorDefValue); }
            }
            return l;
        }
        /// <summary>
        /// 设置网址参数
        /// </summary>
        /// <param name="UrlStr">网址字符串</param>
        /// <param name="Name"></param>
        /// <param name="Value"></param>
        /// <returns>网址字符串</returns>
        public static string _SetUrlParam(this string UrlStr, string Name, string Value)
        {
            var sp = UrlStr.Split('?');
            if (sp.Length > 1)
            {
                var query = Regex.Replace("&" + sp[1], "[?&]" + Name + "=[^&]*", "", RegexOptions.IgnoreCase);
                query = (query + "&" + Name + "=" + Value).TrimStart('&');
                return sp[0] + "?" + query;
            }
            else
            {
                return UrlStr + "?" + Name + "=" + Value;
            }
        }
        /// <summary>
        /// 转换成参数字典
        /// </summary>
        /// <param name="Url">完整或相对网址</param>
        /// <returns>返回字典</returns>
        public static Dictionary<string, string> _ToUrlParams(this string Url)
        {
            var dic = new Dictionary<string, string>();
            Regex.Replace(Url, @"[&?]([^?&=]*)(?:=([^&#]*)|$)", m =>
            {
                dic[m.Groups[1].Value.ToLower()] = m.Groups[2].Value;
                return string.Empty;
            }, RegexOptions.Multiline);
            return dic;
        }
        /// <summary>
        /// 过滤字符串中的数字并转换成数值
        /// </summary>
        /// <param name="Txt">文本</param>
        /// <param name="DefaultNum">默认值</param>
        /// <returns></returns>
        public static decimal _ToDecimalByFltNum(this string Txt, decimal DefaultNum = 0)
        {
            var str = Regex.Replace(Txt, @"[^\d]+", "");
            decimal val;
            if (decimal.TryParse(str, out val))
            {
                return val;
            }
            return DefaultNum;
        }
        /// <summary>
        /// 转换成数字(含小数点)列表(单个或多个非数字会转成一个",")
        /// </summary>
        /// <param name="NumStr">带数字的字符串</param>
        /// <returns></returns>
        public static string _ToNumListStr(this string NumStr)
        {
            return Regex.Replace(NumStr, @"[^\d\.\-\+]+", ",").Trim(',');
        }
        /// <summary>
        /// 转换成数值列表
        /// </summary>
        /// <param name="NumListStr">数字列表,如:13,23.3,23,45</param>
        /// <returns></returns>
        public static List<decimal> _ToDecimalList(this string NumListStr)
        {
            return NumListStr.Split(',').Select(t => _ToDecimal(t)).ToList();
        }
        /// <summary>
        /// 时间转时间戳
        /// </summary>
        public static long _ToTimestamp(this DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            return Convert.ToInt64((time - startTime).TotalMilliseconds);
        }
 
        /// <summary>
        /// 时间戳转时间  (适合时间戳是毫秒数的情况)
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public static DateTime _ToDateTime(this long timestamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            TimeSpan toNow = new TimeSpan(timestamp * 10000);
            return dtStart.Add(toNow);
        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static string _EcdMD5(this string Value, bool IsLower = true)
        {
            var str = string.Empty;
            using (var md5 = MD5.Create())
            {
                var result = md5.ComputeHash(Encoding.UTF8.GetBytes(Value));
                str = BitConverter.ToString(result);
                str = str.Replace("-", "");
            }
            return IsLower ? str.ToLower() : str;
        }
        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static List<T> _Select<T>(this DataRowCollection Rows, Func<DataRow, T> FnSelect)
        {
            var lst = new List<T>();
            foreach (DataRow row in Rows)
            {
                lst.Add(FnSelect(row));
            }
            return lst;
        }
 
        /**
      * 生成签名 
      */
        public static string GenSign(Dictionary<string, string> param, string apisec)
        {
            Dictionary<string, string> dictSort = param.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
            string str = apisec;
            string separator = "";
            foreach (KeyValuePair<string, string> kv in dictSort)
            {
                if (kv.Key == "signature")
                {
                    continue;
                }
                str += separator + kv.Key + "=" + kv.Value;
                separator = "&";
            }
            str += apisec;
            byte[] result = Encoding.UTF8.GetBytes(str);
            MD5 md5 = new MD5CryptoServiceProvider();
            return BitConverter.ToString(md5.ComputeHash(result)).Replace("-", "");
        }
 
        /**
  *  连接参数
  * 
  */
        public static string Paramjoin(Dictionary<string, string> urlparams = null)
        {
            string urlparam = "";
            if (urlparams != null && urlparams.Count > 0)
            {
                List<string> ulist = new List<string>();
                foreach (KeyValuePair<string, string> entry in urlparams)
                {
                    ulist.Add(String.Format("{0}={1}", HttpUtility.UrlEncode(entry.Key), HttpUtility.UrlEncode(entry.Value)));
                }
                urlparam = String.Join("&", ulist.ToArray());
            }
            return urlparam;
        }
 
        /// <summary>
        /// 是否可发短信
        /// </summary>
        /// <param name="checkStr"></param>
        /// <returns></returns>
        public static bool IsCanSendMobile(this string checkStr)
        {
            if (string.IsNullOrEmpty(checkStr)) { return false; }
            return Regex.IsMatch(checkStr, @"^1\d{10}(\/1\d{10}){0,2}$");
        }
 
        public static string ToUtf8(this string str)
        {
            UTF8Encoding utf8 = new UTF8Encoding();
            Byte[] encodedBytes = utf8.GetBytes(str);
            String decodedString = utf8.GetString(encodedBytes);
            return decodedString;
 
        }
        #endregion
 
    }
}