Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
Education
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄奎
Education
Commits
10a039dc
Commit
10a039dc
authored
Sep 28, 2020
by
黄奎
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增方法
parent
b63c7b0c
Show whitespace changes
Inline
Side-by-side
Showing
30 changed files
with
59 additions
and
3492 deletions
+59
-3492
Config.cs
Edu.Common/Config.cs
+2
-53
GlobalKey.cs
Edu.Common/GlobalKey.cs
+1
-7
CommonHelper.cs
Edu.Common/Plugin/CommonHelper.cs
+0
-216
ConvertHelper.cs
Edu.Common/Plugin/ConvertHelper.cs
+2
-105
DateConvert.cs
Edu.Common/Plugin/DateConvert.cs
+0
-33
EnumHelper.cs
Edu.Common/Plugin/EnumHelper.cs
+1
-71
FileHelper.cs
Edu.Common/Plugin/FileHelper.cs
+0
-193
HtmlHelper.cs
Edu.Common/Plugin/HtmlHelper.cs
+1
-1
HttpHelper.cs
Edu.Common/Plugin/HttpHelper.cs
+2
-318
IPHelper.cs
Edu.Common/Plugin/IPHelper.cs
+0
-90
LogHelper.cs
Edu.Common/Plugin/LogHelper.cs
+0
-1
NetFileHelper.cs
Edu.Common/Plugin/NetFileHelper.cs
+1
-6
RSAHelper.cs
Edu.Common/Plugin/RSAHelper.cs
+0
-53
SecurityHelper.cs
Edu.Common/Plugin/SecurityHelper.cs
+0
-19
StringHelper.cs
Edu.Common/Plugin/StringHelper.cs
+0
-1490
ValidateHepler.cs
Edu.Common/Plugin/ValidateHepler.cs
+0
-422
WeiXinHelper.cs
Edu.Common/Plugin/WeiXinHelper.cs
+0
-374
WordTemplateHelper.cs
Edu.Common/Plugin/WordTemplateHelper.cs
+0
-4
RB_Course_Source.cs
Edu.Model/Entity/Course/RB_Course_Source.cs
+4
-0
RB_Course_Category_ViewModel.cs
Edu.Model/ViewModel/Course/RB_Course_Category_ViewModel.cs
+4
-3
RB_Course_ViewModel.cs
Edu.Model/ViewModel/Course/RB_Course_ViewModel.cs
+4
-3
RB_Question_Point_ViewModel.cs
Edu.Model/ViewModel/Question/RB_Question_Point_ViewModel.cs
+5
-3
RB_Question_ViewModel.cs
Edu.Model/ViewModel/Question/RB_Question_ViewModel.cs
+4
-3
RB_Assist_ViewModel.cs
Edu.Model/ViewModel/User/RB_Assist_ViewModel.cs
+6
-4
RB_Group_ViewModel.cs
Edu.Model/ViewModel/User/RB_Group_ViewModel.cs
+4
-3
RB_Manager_ViewModel.cs
Edu.Model/ViewModel/User/RB_Manager_ViewModel.cs
+4
-3
RB_School_ViewModel.cs
Edu.Model/ViewModel/User/RB_School_ViewModel.cs
+4
-3
RB_Student_ViewModel.cs
Edu.Model/ViewModel/User/RB_Student_ViewModel.cs
+4
-3
RB_Teacher_ViewModel.cs
Edu.Model/ViewModel/User/RB_Teacher_ViewModel.cs
+6
-4
appsettings.json
Edu.WebApi/appsettings.json
+0
-4
No files found.
Edu.Common/Config.cs
View file @
10a039dc
...
...
@@ -6,7 +6,6 @@ using System.Text.RegularExpressions;
namespace
Edu.Common
{
/// <summary>
/// 全局配置
/// </summary>
...
...
@@ -73,16 +72,6 @@ namespace Edu.Common
return
valueStr
;
}
/// <summary>
/// 获取配置
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public
static
string
GetAppSetting
(
string
key
)
{
return
ReadConfigKey
(
key
);
}
/// <summary>
/// 异常日志保存路径
/// </summary>
...
...
@@ -142,17 +131,6 @@ namespace Edu.Common
}
}
/// <summary>
/// 接口请求过期时间,单位为秒
/// </summary>
public
static
int
ApiExpirTime
{
get
{
Int32
.
TryParse
(
ReadConfigKey
(
"ApiExpirTime"
),
out
int
ApiExpirTime
);
return
ApiExpirTime
;
}
}
/// <summary>
/// 阿里云oss域名
...
...
@@ -229,7 +207,6 @@ namespace Edu.Common
}
}
/// <summary>
/// 上传文件预览站点
/// </summary>
...
...
@@ -241,22 +218,6 @@ namespace Edu.Common
}
}
/// <summary>
/// 导出文件保存路径
/// </summary>
public
static
string
FileExportPath
{
get
{
string
fileExportPath
=
Path
.
Combine
(
AppDomain
.
CurrentDomain
.
BaseDirectory
,
"ExportFile"
);
if
(!
Directory
.
Exists
(
fileExportPath
))
{
Directory
.
CreateDirectory
(
fileExportPath
);
}
return
fileExportPath
;
}
}
/// <summary>
/// 获取虚拟目录
/// </summary>
...
...
@@ -290,17 +251,6 @@ namespace Edu.Common
}
}
/// <summary>
/// App支付路径
/// </summary>
public
static
string
APPPay
{
get
{
return
""
;
}
}
/// <summary>
/// 默认密码
/// </summary>
...
...
@@ -308,6 +258,5 @@ namespace Edu.Common
{
get
{
return
"123456"
;
}
}
}
}
\ No newline at end of file
Edu.Common/GlobalKey.cs
View file @
10a039dc
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
Edu.Common
namespace
Edu.Common
{
/// <summary>
/// 全局Key管理
...
...
Edu.Common/Plugin/CommonHelper.cs
deleted
100644 → 0
View file @
b63c7b0c
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Xml.Serialization
;
namespace
Edu.Common.Plugin
{
/// <summary>
/// 公共帮助类
/// </summary>
public
class
CommonHelper
{
/// <summary>
/// 获取oss文件全路径地址
/// </summary>
/// <param name="fileList"></param>
/// <returns></returns>
public
static
List
<
string
>
GetOssFileFullPath
(
List
<
string
>
fileList
)
{
List
<
string
>
resultFileList
=
new
List
<
string
>();
foreach
(
var
item
in
fileList
)
{
resultFileList
.
Add
(
Config
.
GetFileUrl
(
item
));
}
return
resultFileList
;
}
/// <summary>
/// 根据出生日期判断人的类型
/// </summary>
/// <param name="birthDate">出生日期</param>
/// <returns></returns>
public
static
string
CalculateAgeCorrect
(
DateTime
birthDate
)
{
DateTime
now
=
DateTime
.
Now
;
int
age
=
now
.
Year
-
birthDate
.
Year
;
if
(
now
.
Month
<
birthDate
.
Month
||
(
now
.
Month
==
birthDate
.
Month
&&
now
.
Day
<
birthDate
.
Day
))
age
--;
if
(
age
<
2
)
{
return
"婴儿"
;
}
else
if
(
2
<=
age
&&
age
<
12
)
{
return
"小孩"
;
}
else
{
return
"成人"
;
}
}
/// <summary>
/// 根据出生日期判断人的类型
/// </summary>
/// <param name="birthDate">出生日期</param>
/// <returns></returns>
public
static
int
GetAgeCorrect
(
DateTime
birthDate
)
{
DateTime
now
=
DateTime
.
Now
;
int
age
=
now
.
Year
-
birthDate
.
Year
;
if
(
now
.
Month
<
birthDate
.
Month
||
(
now
.
Month
==
birthDate
.
Month
&&
now
.
Day
<
birthDate
.
Day
))
{
age
--;
}
if
(
age
<=
11
)
{
return
3
;
}
else
if
(
12
<=
age
&&
age
<
18
)
{
return
2
;
}
else
{
return
1
;
}
}
/// <summary>
/// 根据URL地址获取文件路径
/// </summary>
/// <param name="url">文件路劲</param>
/// <returns></returns>
public
static
string
GetFilePath
(
string
url
)
{
string
path
=
string
.
Empty
;
try
{
Uri
uri
=
new
Uri
(
url
);
path
=
uri
.
AbsolutePath
;
}
catch
{
}
return
path
;
}
/// <summary>
/// 根据日期获取星期几
/// </summary>
/// <param name="date">日期</param>
/// <returns></returns>
public
static
int
GetWeekNow
(
DateTime
date
)
{
string
strWeek
=
date
.
DayOfWeek
.
ToString
();
int
weekDay
=
0
;
switch
(
strWeek
)
{
case
"Monday"
:
weekDay
=
1
;
break
;
case
"Tuesday"
:
weekDay
=
2
;
break
;
case
"Wednesday"
:
weekDay
=
3
;
break
;
case
"Thursday"
:
weekDay
=
4
;
break
;
case
"Friday"
:
weekDay
=
5
;
break
;
case
"Saturday"
:
weekDay
=
6
;
break
;
case
"Sunday"
:
weekDay
=
7
;
break
;
}
return
weekDay
;
}
/// <summary>
/// 根据日期获取星期几
/// </summary>
/// <param name="date">日期</param>
/// <returns></returns>
public
static
string
GetWeekDay
(
DateTime
date
)
{
string
strWeek
=
date
.
DayOfWeek
.
ToString
();
string
weekDay
=
""
;
switch
(
strWeek
)
{
case
"Monday"
:
weekDay
=
"一"
;
break
;
case
"Tuesday"
:
weekDay
=
"二"
;
break
;
case
"Wednesday"
:
weekDay
=
"三"
;
break
;
case
"Thursday"
:
weekDay
=
"四"
;
break
;
case
"Friday"
:
weekDay
=
"五"
;
break
;
case
"Saturday"
:
weekDay
=
"六"
;
break
;
case
"Sunday"
:
weekDay
=
"日"
;
break
;
}
return
weekDay
;
}
/// <summary>
/// 判断是否为时间格式
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public
static
bool
IsTime
(
string
value
)
{
bool
flag
=
true
;
try
{
Convert
.
ToDateTime
(
"2019-01-01 "
+
value
.
Trim
());
}
catch
(
Exception
)
{
flag
=
false
;
}
return
flag
;
}
/// <summary>
/// 时间格式化(dd+英文月+YY)
/// </summary>
/// <returns></returns>
public
static
string
DateTimeFormat
(
DateTime
dt
)
{
string
str
=
string
.
Empty
;
try
{
int
month
=
dt
.
Month
;
switch
(
month
)
{
case
1
:
str
=
dt
.
ToString
(
"dd"
)
+
"Jan"
+
dt
.
Year
.
ToString
().
Substring
(
2
);
break
;
case
2
:
str
=
dt
.
ToString
(
"dd"
)
+
"Feb"
+
dt
.
Year
.
ToString
().
Substring
(
2
);
break
;
case
3
:
str
=
dt
.
ToString
(
"dd"
)
+
"Mar"
+
dt
.
Year
.
ToString
().
Substring
(
2
);
break
;
case
4
:
str
=
dt
.
ToString
(
"dd"
)
+
"Apr"
+
dt
.
Year
.
ToString
().
Substring
(
2
);
break
;
case
5
:
str
=
dt
.
ToString
(
"dd"
)
+
"May"
+
dt
.
Year
.
ToString
().
Substring
(
2
);
break
;
case
6
:
str
=
dt
.
ToString
(
"dd"
)
+
"Jun"
+
dt
.
Year
.
ToString
().
Substring
(
2
);
break
;
case
7
:
str
=
dt
.
ToString
(
"dd"
)
+
"Jul"
+
dt
.
Year
.
ToString
().
Substring
(
2
);
break
;
case
8
:
str
=
dt
.
ToString
(
"dd"
)
+
"Aug"
+
dt
.
Year
.
ToString
().
Substring
(
2
);
break
;
case
9
:
str
=
dt
.
ToString
(
"dd"
)
+
"Sept"
+
dt
.
Year
.
ToString
().
Substring
(
2
);
break
;
case
10
:
str
=
dt
.
ToString
(
"dd"
)
+
"Oct"
+
dt
.
Year
.
ToString
().
Substring
(
2
);
break
;
case
11
:
str
=
dt
.
ToString
(
"dd"
)
+
"Nov"
+
dt
.
Year
.
ToString
().
Substring
(
2
);
break
;
case
12
:
str
=
dt
.
ToString
(
"dd"
)
+
"Dec"
+
dt
.
Year
.
ToString
().
Substring
(
2
);
break
;
}
}
catch
{
}
return
str
;
}
/// <summary>
/// 克隆对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public
static
T
Clone
<
T
>(
T
obj
)
{
T
ret
=
default
;
if
(
obj
!=
null
)
{
XmlSerializer
cloner
=
new
XmlSerializer
(
typeof
(
T
));
MemoryStream
stream
=
new
MemoryStream
();
cloner
.
Serialize
(
stream
,
obj
);
stream
.
Seek
(
0
,
SeekOrigin
.
Begin
);
ret
=
(
T
)
cloner
.
Deserialize
(
stream
);
}
return
ret
;
}
}
}
\ No newline at end of file
Edu.Common/Plugin/ConvertHelper.cs
View file @
10a039dc
...
...
@@ -10,117 +10,14 @@ namespace Edu.Common
/// <summary>
/// 类型转换帮助类
/// </summary>
public
class
ConvertHelper
public
static
class
ConvertHelper
{
/// <summary>
/// 转换字符流成字符串
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>
public
static
string
ToString
(
byte
[]
bytes
)
{
var
sb
=
new
StringBuilder
(
64
);
foreach
(
byte
iByte
in
bytes
)
sb
.
AppendFormat
(
"{0:x2}"
,
iByte
);
return
sb
.
ToString
();
}
/// <summary>
/// 转换成Byte数组
/// </summary>
/// <param name="hexString"></param>
/// <returns></returns>
public
static
byte
[]
ToBytes
(
string
hexString
)
{
if
(
hexString
==
null
)
return
null
;
var
data
=
new
byte
[
hexString
.
Length
/
2
];
int
h
,
l
;
for
(
var
i
=
0
;
i
<
data
.
Length
;
i
++)
{
h
=
(
hexString
[
2
*
i
]
>
57
?
hexString
[
2
*
i
]
-
'A'
+
10
:
hexString
[
2
*
i
]
-
'0'
)
<<
4
&
0x000000F0
;
l
=
(
hexString
[
2
*
i
+
1
]
>
57
?
hexString
[
2
*
i
+
1
]
-
'A'
+
10
:
hexString
[
2
*
i
+
1
]
-
'0'
)
&
0x0000000F
;
data
[
i
]
=
(
byte
)(
h
|
l
);
}
return
data
;
}
/// <summary>
/// 类型转换
/// </summary>
/// <typeparam name="T">要转换类型</typeparam>
/// <param name="obj">要转换的值</param>
/// <returns></returns>
public
static
T
ConvertTo
<
T
>(
object
obj
)
{
if
(
obj
==
null
)
return
default
;
//返回默认值
Type
tp
=
typeof
(
T
);
//泛型Nullable判断,取其中的类型
if
(
tp
.
IsGenericType
)
{
tp
=
tp
.
GetGenericArguments
()[
0
];
}
//string直接返回转换
if
(
tp
.
Name
.
ToLower
()
==
"string"
)
{
return
(
T
)
obj
;
}
//反射获取TryParse方法
var
TryParse
=
tp
.
GetMethod
(
"TryParse"
,
BindingFlags
.
Public
|
BindingFlags
.
Static
,
Type
.
DefaultBinder
,
new
Type
[]
{
typeof
(
string
),
tp
.
MakeByRefType
()
},
new
ParameterModifier
[]
{
new
ParameterModifier
(
2
)
});
var
parameters
=
new
object
[]
{
obj
,
Activator
.
CreateInstance
(
tp
)
};
try
{
bool
success
=
(
bool
)
TryParse
.
Invoke
(
null
,
parameters
);
//成功返回转换后的值,否则返回类型的默认值
if
(
success
)
{
return
(
T
)
parameters
[
1
];
}
return
default
;
}
catch
{
return
default
;
}
}
/// <summary>
/// 转Int
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public
static
int
ConvertInt
(
object
obj
)
{
return
ConvertTo
<
int
>(
obj
);
}
/// <summary>
/// 转Int
/// </summary>
/// <param name="obj"></param>
/// <param name="defaultValue">默认值</param>
/// <returns></returns>
public
static
int
ToInt
(
object
obj
,
int
defaultValue
=
0
)
{
int
result
=
defaultValue
;
if
(
obj
!=
null
)
{
Int32
.
TryParse
(
obj
.
ToString
(),
out
result
);
}
return
result
;
}
/// <summary>
/// 格式化时间【yyyy-MM-dd HH:mm:ss】
/// </summary>
/// <param name="time"></param>
/// <returns></returns>
public
static
string
FormatTime
(
object
time
)
public
static
string
FormatTime
(
this
object
time
)
{
string
timeStr
=
""
;
if
(
time
!=
null
)
...
...
Edu.Common/Plugin/DateConvert.cs
deleted
100644 → 0
View file @
b63c7b0c
using
Newtonsoft.Json.Converters
;
namespace
Edu.Common.Plugin
{
/// <summary>
/// 格式化时间【yyyy-MM-dd HH:mm:ss】
/// </summary>
public
class
DateConvert
:
IsoDateTimeConverter
{
/// <summary>
/// 格式化时间
/// </summary>
public
DateConvert
()
{
base
.
DateTimeFormat
=
"yyyy-MM-dd HH:mm:ss"
;
}
}
/// <summary>
/// 格式化日期【yyyy-MM-dd】
/// </summary>
public
class
DateConvertTwo
:
IsoDateTimeConverter
{
/// <summary>
/// 格式化时间
/// </summary>
public
DateConvertTwo
()
{
base
.
DateTimeFormat
=
"yyyy-MM-dd"
;
}
}
}
Edu.Common/Plugin/EnumHelper.cs
View file @
10a039dc
...
...
@@ -9,26 +9,6 @@ namespace Edu.Common.Plugin
/// </summary>
public
static
class
EnumHelper
{
/// <summary>
/// 获取枚举值集合
/// </summary>
/// <param name="enumType"></param>
/// <returns></returns>
public
static
Dictionary
<
string
,
string
>
GetEnumList
(
Type
enumType
)
{
Dictionary
<
string
,
string
>
dic
=
new
Dictionary
<
string
,
string
>();
FieldInfo
[]
fieldinfos
=
enumType
.
GetFields
();
foreach
(
FieldInfo
field
in
fieldinfos
)
{
if
(
field
.
FieldType
.
IsEnum
)
{
Object
[]
objs
=
field
.
GetCustomAttributes
(
typeof
(
EnumFieldAttribute
),
false
);
dic
.
Add
(((
EnumFieldAttribute
)
objs
[
0
]).
Name
,
field
.
GetRawConstantValue
().
ToString
());
}
}
return
dic
;
}
/// <summary>
/// 获取枚举列表
/// </summary>
...
...
@@ -54,41 +34,13 @@ namespace Edu.Common.Plugin
return
list
;
}
/// <summary>
/// 获取枚举值的名称
/// </summary>
/// <param name="e"></param>
/// <returns></returns>
public
static
string
GetEnumName
(
object
e
)
{
if
(
e
==
null
)
{
return
""
;
}
//获取字段信息
System
.
Reflection
.
FieldInfo
[]
ms
=
e
.
GetType
().
GetFields
();
foreach
(
System
.
Reflection
.
FieldInfo
f
in
ms
)
{
//判断名称是否相等
if
(
f
.
Name
!=
e
.
ToString
())
continue
;
//反射出自定义属性
object
[]
attrs
=
f
.
GetCustomAttributes
(
typeof
(
EnumFieldAttribute
),
true
);
if
(
attrs
.
Length
>
0
)
{
EnumFieldAttribute
attr
=
attrs
[
0
]
as
EnumFieldAttribute
;
return
attr
.
Name
;
}
}
return
""
;
}
/// <summary>
/// 获取枚举特性描述值
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public
static
string
GetEnum
Name
(
this
System
.
Enum
obj
)
public
static
string
To
Name
(
this
System
.
Enum
obj
)
{
if
(
obj
==
null
)
{
...
...
@@ -123,28 +75,6 @@ namespace Edu.Common.Plugin
{
return
Convert
.
ToInt32
(
obj
);
}
/// <summary>
/// 将值转成枚举
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public
static
T
ToEnum
<
T
>(
this
string
obj
)
where
T
:
struct
{
if
(
string
.
IsNullOrEmpty
(
obj
))
{
return
default
;
}
try
{
return
(
T
)
System
.
Enum
.
Parse
(
typeof
(
T
),
obj
,
true
);
}
catch
(
Exception
)
{
return
default
;
}
}
}
/// <summary>
...
...
Edu.Common/Plugin/FileHelper.cs
deleted
100644 → 0
View file @
b63c7b0c
using
Edu.Common
;
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Net
;
using
System.Text
;
namespace
Edu.Common.Plugin
{
/// <summary>
/// 文件操作帮助类
/// </summary>
public
class
FileHelper
{
/// <summary>
/// 生成图片
/// </summary>
/// <param name="url"></param>
public
static
void
CreateImage
(
string
url
)
{
string
tempPath
=
GetFileUrl
(
url
);
string
path
=
Environment
.
CurrentDirectory
+
tempPath
;
//下载到的地址+文件名
HttpWebRequest
request
=
WebRequest
.
Create
(
url
)
as
HttpWebRequest
;
//发送请求并获取相应回应数据
HttpWebResponse
response
=
request
.
GetResponse
()
as
HttpWebResponse
;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream
responseStream
=
response
.
GetResponseStream
();
string
newFilePath
=
path
.
Substring
(
0
,
path
.
LastIndexOf
(
"/"
));
if
(!
System
.
IO
.
Directory
.
Exists
(
newFilePath
))
//如果不存在就创建file文件夹
{
System
.
IO
.
Directory
.
CreateDirectory
(
newFilePath
);
}
//创建本地文件写入流
Stream
stream
=
new
FileStream
(
path
,
FileMode
.
Create
);
byte
[]
bArr
=
new
byte
[
1024
];
int
size
=
responseStream
.
Read
(
bArr
,
0
,
(
int
)
bArr
.
Length
);
while
(
size
>
0
)
{
stream
.
Write
(
bArr
,
0
,
size
);
size
=
responseStream
.
Read
(
bArr
,
0
,
(
int
)
bArr
.
Length
);
}
stream
.
Close
();
responseStream
.
Close
();
}
/// <summary>
/// 获取Oss文件全路径
/// </summary>
/// <param name="path">文件相对路径</param>
/// <returns></returns>
public
static
string
GetFileUrl
(
string
path
)
{
return
path
.
Replace
(
"http://auth-zjhejiang-com.oss-cn-hangzhou.aliyuncs.com"
,
""
)
.
Replace
(
"https://cdnimg.iotweixin.com"
,
""
).
Replace
(
"http://img.weibaoge.cn"
,
""
).
Replace
(
"http://v4test.zjhejiang.com"
,
""
)
.
Replace
(
"http://v4test1.oss-cn-hangzhou.aliyuncs.com"
,
""
).
Replace
(
"https://viitto-1301420277.cos.ap-chengdu.myqcloud.com"
,
""
);
}
/// <summary>
/// 网站文件夹虚拟目录
/// </summary>
public
static
string
VirtualDirectory
=
Config
.
VirtualDirectory
;
/// <summary>
/// 行程特色删除指定文件夹文件
/// </summary>
/// <param name="filePath">文件夹路径</param>
public
static
void
DeleteFile
(
string
filePath
)
{
string
newPath
=
AppContext
.
BaseDirectory
+
"\\"
+
filePath
;
DirectoryInfo
theFolder
=
new
DirectoryInfo
(
newPath
);
if
(
theFolder
.
Exists
)
{
FileSystemInfo
[]
files
=
theFolder
.
GetFileSystemInfos
();
for
(
int
i
=
0
;
i
<
files
.
Length
;
i
++)
{
//是文件
if
(
files
[
i
]
is
FileInfo
file
)
{
file
.
Delete
();
}
}
}
}
/// <summary>
/// 删除文件扩展方法
/// </summary>
/// <param name="filePath"></param>
public
static
void
DeleteFileExt
(
string
filePath
)
{
string
newPath
=
AppContext
.
BaseDirectory
+
"\\"
+
VirtualDirectory
+
filePath
;
File
.
Delete
(
newPath
);
}
/// <summary>
/// 根据TCID获取行程特色图片
/// </summary>
/// <param name="TCID"></param>
/// <returns></returns>
public
static
List
<
string
>
GetTripFileList
(
int
TCID
)
{
string
filepath
=
VirtualDirectory
+
"/Feature/"
+
TCID
+
"/"
;
List
<
string
>
fileList
=
new
List
<
string
>();
DirectoryInfo
theFolder
=
new
DirectoryInfo
(
AppContext
.
BaseDirectory
+
"\\"
+
filepath
);
if
(!
theFolder
.
Exists
)
{
theFolder
.
Create
();
}
FileSystemInfo
[]
files
=
theFolder
.
GetFileSystemInfos
();
for
(
int
i
=
0
;
i
<
files
.
Length
;
i
++)
{
//是文件
if
(
files
[
i
]
is
FileInfo
file
)
{
fileList
.
Add
(
Config
.
ViewFileSiteUrl
+
"/Feature/"
+
TCID
+
"/"
+
file
.
Name
);
}
}
return
fileList
;
}
/// <summary>
/// 批量删除文件
/// </summary>
/// <param name="fileList"></param>
public
static
void
DeleteFileList
(
List
<
string
>
fileList
)
{
foreach
(
var
item
in
fileList
)
{
string
newPath
=
AppContext
.
BaseDirectory
+
"\\"
+
VirtualDirectory
+
item
;
File
.
Delete
(
newPath
);
}
}
/// <summary>
/// 文件移动
/// </summary>
/// <param name="filePath"></param>
/// <param name="newFilePath"></param>
public
static
void
FileCopy
(
string
filePath
,
string
newFilePath
)
{
string
newTempFilePath
=
filePath
.
Substring
(
filePath
.
IndexOf
(
"/"
));
;
var
strPath
=
AppContext
.
BaseDirectory
+
"\\"
+
VirtualDirectory
+
newTempFilePath
;
//System.Web.HttpContext.Current.Server.MapPath("~/" + VirtualDirectory + newTempFilePath);
string
newfilePath
=
AppContext
.
BaseDirectory
+
"\\"
+
VirtualDirectory
+
newFilePath
.
Substring
(
0
,
strPath
.
LastIndexOf
(
'\\'
)
-
1
);
//System.Web.HttpContext.Current.Server.MapPath("~/" + VirtualDirectory) + newFilePath.Substring(0, strPath.LastIndexOf('\\') - 1);
if
(!
Directory
.
Exists
(
newfilePath
))
{
Directory
.
CreateDirectory
(
newfilePath
);
}
string
sourceFile
=
AppContext
.
BaseDirectory
+
"\\"
+
VirtualDirectory
+
newTempFilePath
;
// System.Web.HttpContext.Current.Server.MapPath("~/" + VirtualDirectory + newTempFilePath);
string
targetFile
=
AppContext
.
BaseDirectory
+
"\\"
+
VirtualDirectory
+
newFilePath
;
//System.Web.HttpContext.Current.Server.MapPath("~/" + VirtualDirectory + newFilePath);
File
.
Move
(
sourceFile
,
targetFile
);
}
/// <summary>
/// 判断文件是否存在
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
public
static
bool
FileExists
(
string
filePath
)
{
return
File
.
Exists
(
AppContext
.
BaseDirectory
+
"\\"
+
VirtualDirectory
+
filePath
);
//return File.Exists(System.Web.HttpContext.Current.Server.MapPath("~/" + VirtualDirectory + filePath));
}
/// <summary>
/// 写byte[]到fileName
/// </summary>
/// <param name="fileName">保存至硬盘路径</param>
/// <param name="pReadByte">byte[]</param>
/// <returns></returns>
public
static
bool
WriteByteToFile
(
string
fileName
,
byte
[]
pReadByte
)
{
FileStream
pFileStream
=
null
;
try
{
pFileStream
=
new
FileStream
(
fileName
,
FileMode
.
OpenOrCreate
);
pFileStream
.
Write
(
pReadByte
,
0
,
pReadByte
.
Length
);
}
catch
{
return
false
;
}
finally
{
if
(
pFileStream
!=
null
)
pFileStream
.
Close
();
}
return
true
;
}
}
}
Edu.Common/Plugin/HtmlHelper.cs
View file @
10a039dc
Edu.Common/Plugin/HttpHelper.cs
View file @
10a039dc
using
Newtonsoft.Json.Linq
;
using
System
;
using
System.Drawing
;
using
System
;
using
System.IO
;
using
System.Net
;
using
System.Net.Http
;
using
System.Text
;
namespace
Edu.Common.Plugin
...
...
@@ -122,21 +119,13 @@ namespace Edu.Common.Plugin
HttpWebRequest
req
=
(
HttpWebRequest
)
WebRequest
.
Create
(
url
);
req
.
Method
=
"POST"
;
req
.
ContentType
=
"application/x-www-form-urlencoded"
;
//req.Headers.Add("Accept", "application/json");
//req.Headers.Add("Content-Type", "application/json");
byte
[]
data
=
Encoding
.
UTF8
.
GetBytes
(
postDataStr
);
req
.
ContentLength
=
data
.
Length
;
using
(
Stream
reqStream
=
req
.
GetRequestStream
())
{
reqStream
.
Write
(
data
,
0
,
data
.
Length
);
reqStream
.
Close
();
}
HttpWebResponse
rsp
=
(
HttpWebResponse
)
req
.
GetResponse
();
if
(
string
.
IsNullOrWhiteSpace
(
rsp
.
CharacterSet
))
{
...
...
@@ -203,147 +192,6 @@ namespace Edu.Common.Plugin
}
}
/// <summary>
/// 根据地址获取经度纬度
/// </summary>
/// <param name="address"></param>
/// <returns></returns>
public
static
string
[]
GetGeocoderLatitude
(
string
address
)
{
string
apiKey
=
"7d9fbeb43e975cd1e9477a7e5d5e192a"
;
var
returnStr
=
new
string
[
2
]
{
""
,
""
};
HttpClient
client
=
new
HttpClient
();
string
url
=
"http://api.map.baidu.com/geocoder?address="
+
address
+
"&output=json&key="
+
apiKey
;
try
{
string
apiResult
=
client
.
GetStringAsync
(
url
).
Result
;
var
apiJson
=
JObject
.
Parse
(
apiResult
);
var
resultJson
=
JObject
.
Parse
(
apiJson
[
"result"
].
ToString
());
var
locationJson
=
JObject
.
Parse
(
resultJson
[
"location"
].
ToString
());
var
lng
=
locationJson
[
"lng"
].
ToString
();
//经度
var
lat
=
locationJson
[
"lat"
].
ToString
();
//纬度
returnStr
[
0
]
=
lng
;
returnStr
[
1
]
=
lat
;
}
catch
{
}
return
returnStr
;
}
/// <summary>
/// 根据经纬度获取详细地址location
/// </summary>
/// <param name="latlng"></param>
/// <returns></returns>
public
static
string
GetGeocoderAddress
(
string
latlng
)
{
string
apiKey
=
"7d9fbeb43e975cd1e9477a7e5d5e192a"
;
HttpClient
client
=
new
HttpClient
();
string
url
=
"http://api.map.baidu.com/geocoder?location="
+
latlng
+
"&output=json&key="
+
apiKey
;
try
{
string
apiResult
=
client
.
GetStringAsync
(
url
).
Result
;
var
apiJson
=
JObject
.
Parse
(
apiResult
);
var
resultJson
=
JObject
.
Parse
(
apiJson
[
"result"
].
ToString
());
var
locationJson
=
JObject
.
Parse
(
resultJson
[
"addressComponent"
].
ToString
());
var
province
=
locationJson
[
"province"
].
ToString
();
//省份
return
province
;
}
catch
{
}
return
""
;
}
/// <summary>
/// 执行HTTP POST请求。
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="postDataStr">请求地址</param>
/// <returns>HTTP响应</returns>
public
static
string
HttpPostForWXQRCode
(
string
url
,
string
postDataStr
)
{
HttpWebRequest
req
=
(
HttpWebRequest
)
WebRequest
.
Create
(
url
);
req
.
Method
=
"POST"
;
req
.
ContentType
=
"application/octet-stream"
;
req
.
ContentLength
=
Encoding
.
UTF8
.
GetByteCount
(
postDataStr
);
Stream
myRequestStream
=
req
.
GetRequestStream
();
StreamWriter
myStreamWriter
=
new
StreamWriter
(
myRequestStream
,
Encoding
.
GetEncoding
(
"gb2312"
));
myStreamWriter
.
Write
(
postDataStr
);
myStreamWriter
.
Close
();
HttpWebResponse
rsp
=
(
HttpWebResponse
)
req
.
GetResponse
();
if
(
string
.
IsNullOrWhiteSpace
(
rsp
.
CharacterSet
))
{
return
GetResponseAsStringBase64
(
rsp
);
}
else
{
return
GetResponseAsStringBase64
(
rsp
);
}
}
/// <summary>
/// 执行HTTP POST请求。
/// </summary>
/// <param name="url">请求地址</param>
/// <param name="postDataStr">请求地址</param>
/// <returns>HTTP响应</returns>
public
static
string
HttpPostForGetWXQRCodePath
(
string
url
,
string
postDataStr
,
string
path
)
{
HttpWebRequest
req
=
(
HttpWebRequest
)
WebRequest
.
Create
(
url
);
req
.
Method
=
"POST"
;
req
.
ContentType
=
"application/octet-stream"
;
req
.
ContentLength
=
Encoding
.
UTF8
.
GetByteCount
(
postDataStr
);
Stream
myRequestStream
=
req
.
GetRequestStream
();
StreamWriter
myStreamWriter
=
new
StreamWriter
(
myRequestStream
,
Encoding
.
GetEncoding
(
"gb2312"
));
myStreamWriter
.
Write
(
postDataStr
);
myStreamWriter
.
Close
();
HttpWebResponse
rsp
=
(
HttpWebResponse
)
req
.
GetResponse
();
if
(
string
.
IsNullOrWhiteSpace
(
rsp
.
CharacterSet
))
{
return
GetResponseAsStringBase64_V2
(
rsp
,
path
);
}
else
{
return
GetResponseAsStringBase64_V2
(
rsp
,
path
);
}
}
/// <summary>
/// 获取微店小程序码
/// </summary>
/// <param name="url"></param>
/// <param name="postDataStr"></param>
/// <param name="path"></param>
/// <returns></returns>
public
static
string
HttpPostForGetWXQRCodePathSmallShops
(
string
url
,
string
postDataStr
,
string
path
,
string
ssPhoto
)
{
HttpWebRequest
req
=
(
HttpWebRequest
)
WebRequest
.
Create
(
url
);
req
.
Method
=
"POST"
;
req
.
ContentType
=
"application/octet-stream"
;
req
.
ContentLength
=
Encoding
.
UTF8
.
GetByteCount
(
postDataStr
);
Stream
myRequestStream
=
req
.
GetRequestStream
();
StreamWriter
myStreamWriter
=
new
StreamWriter
(
myRequestStream
,
Encoding
.
GetEncoding
(
"gb2312"
));
myStreamWriter
.
Write
(
postDataStr
);
myStreamWriter
.
Close
();
HttpWebResponse
rsp
=
(
HttpWebResponse
)
req
.
GetResponse
();
if
(
string
.
IsNullOrWhiteSpace
(
rsp
.
CharacterSet
))
{
return
GetResponseAsStringBase64_V3
(
rsp
,
path
,
ssPhoto
);
}
else
{
return
GetResponseAsStringBase64_V3
(
rsp
,
path
,
ssPhoto
);
}
}
/// <summary>
/// 把响应流转换为文本
/// </summary>
...
...
@@ -370,76 +218,6 @@ namespace Edu.Common.Plugin
}
}
/// <summary>
/// 把响应流转换为文本
/// </summary>
/// <param name="rsp"></param>
/// <param name="path"></param>
/// <returns></returns>
private
static
string
GetResponseAsStringBase64_V2
(
HttpWebResponse
rsp
,
string
path
)
{
System
.
IO
.
Stream
stream
=
null
;
try
{
// 以字符流的方式读取HTTP响应
stream
=
rsp
.
GetResponseStream
();
return
ReadImageForStream_V2
(
stream
,
path
);
}
catch
(
Exception
ex
)
{
LogHelper
.
Write
(
ex
,
"GetResponseAsStringBase64_V2"
);
return
""
;
}
finally
{
// 释放资源
if
(
stream
!=
null
)
stream
.
Close
();
if
(
rsp
!=
null
)
rsp
.
Close
();
}
}
/// <summary>
/// 把响应流转换为文本
/// </summary>
/// <param name="rsp"></param>
/// <param name="path"></param>
/// <param name="ssPhoto"></param>
/// <returns></returns>
private
static
string
GetResponseAsStringBase64_V3
(
HttpWebResponse
rsp
,
string
path
,
string
ssPhoto
)
{
System
.
IO
.
Stream
stream
=
null
;
try
{
// 以字符流的方式读取HTTP响应
stream
=
rsp
.
GetResponseStream
();
return
ReadImageForStream_V3
(
stream
,
path
,
ssPhoto
);
}
catch
(
Exception
ex
)
{
LogHelper
.
Write
(
ex
,
"GetResponseAsStringBase64_V3"
);
return
""
;
}
finally
{
// 释放资源
if
(
stream
!=
null
)
stream
.
Close
();
if
(
rsp
!=
null
)
rsp
.
Close
();
}
}
/// <summary>
/// 获取
/// </summary>
/// <param name="imgstream"></param>
/// <returns></returns>
public
static
string
ReadImageForStream_V2
(
Stream
imgstream
,
string
path
)
{
System
.
Drawing
.
Image
result
=
System
.
Drawing
.
Image
.
FromStream
(
imgstream
);
System
.
Drawing
.
Bitmap
bit
=
new
System
.
Drawing
.
Bitmap
(
result
);
bit
.
Save
(
path
,
System
.
Drawing
.
Imaging
.
ImageFormat
.
Jpeg
);
return
path
;
}
/// <summary>
/// 获取
/// </summary>
...
...
@@ -476,99 +254,5 @@ namespace Edu.Common.Plugin
}
return
UserPhoto
;
}
/// <summary>
/// 获取小程序码 并且替换中间图标
/// </summary>
/// <param name="imgstream"></param>
/// <returns></returns>
public
static
string
ReadImageForStream_V3
(
Stream
imgstream
,
string
path
,
string
ssPhoto
)
{
System
.
Drawing
.
Image
result
=
System
.
Drawing
.
Image
.
FromStream
(
imgstream
);
if
(!
string
.
IsNullOrEmpty
(
ssPhoto
))
{
WebRequest
wreq
=
WebRequest
.
Create
(
ssPhoto
);
HttpWebResponse
wresp
=
(
HttpWebResponse
)
wreq
.
GetResponse
();
Stream
s
=
wresp
.
GetResponseStream
();
var
img
=
System
.
Drawing
.
Image
.
FromStream
(
s
);
result
=
CombinImage
(
result
,
img
);
}
System
.
Drawing
.
Bitmap
bit
=
new
System
.
Drawing
.
Bitmap
(
result
);
bit
.
Save
(
path
,
System
.
Drawing
.
Imaging
.
ImageFormat
.
Jpeg
);
return
path
;
}
/// <summary>
/// 图片处理为圆形
/// </summary>
/// <param name="img"></param>
/// <param name="rec"></param>
/// <param name="size"></param>
/// <returns></returns>
public
static
Image
CutEllipse
(
Image
img
,
Rectangle
rec
,
Size
size
)
{
Bitmap
bitmap
=
new
Bitmap
(
size
.
Width
,
size
.
Height
);
try
{
using
Graphics
g
=
Graphics
.
FromImage
(
bitmap
);
using
TextureBrush
br
=
new
TextureBrush
(
img
,
System
.
Drawing
.
Drawing2D
.
WrapMode
.
Clamp
,
rec
);
br
.
ScaleTransform
(
bitmap
.
Width
/
(
float
)
rec
.
Width
,
bitmap
.
Height
/
(
float
)
rec
.
Height
);
g
.
SmoothingMode
=
System
.
Drawing
.
Drawing2D
.
SmoothingMode
.
AntiAlias
;
g
.
FillEllipse
(
br
,
new
Rectangle
(
Point
.
Empty
,
size
));
}
catch
(
Exception
)
{
return
img
;
}
return
bitmap
;
}
/// <summary>
/// 调用此函数后使此两种图片合并,类似相册,有个
/// 背景图,中间贴自己的目标图片
/// </summary>
/// <param name="imgBack">粘贴的源图片</param>
/// <param name="destImg">粘贴的目标图片</param>
public
static
Image
CombinImage
(
Image
imgBack
,
Image
img
)
{
int
Size
=
img
.
Width
;
if
(
img
.
Height
<
img
.
Width
)
{
Size
=
img
.
Height
;
}
img
=
CutEllipse
(
img
,
new
Rectangle
(
0
,
0
,
Size
,
Size
),
new
Size
(
Size
,
Size
));
Graphics
g
=
Graphics
.
FromImage
(
imgBack
);
g
.
DrawImage
(
imgBack
,
0
,
0
,
imgBack
.
Width
,
imgBack
.
Height
);
//g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
//g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框
//g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
Size
=
imgBack
.
Width
/
2
;
g
.
DrawImage
(
img
,
imgBack
.
Width
/
2
-
(
Size
/
2
)
+
5
,
imgBack
.
Width
/
2
-
(
Size
/
2
)
+
5
,
Size
-
10
,
Size
-
10
);
GC
.
Collect
();
return
imgBack
;
}
public
static
string
CombinImage2
()
{
var
imgBack
=
Image
.
FromFile
(
"E:/LDWork/LiuDongWork/MallFB/upfile/temporary/123.jpg"
);
//var img = Image.FromFile("https://wx.qlogo.cn/mmopen/vi_32/6d2FEzo0bpq8NcQ5sica1EictZLcaeCMboVnxdSqnaxuVPdp0GVuDDRJicfIUQnDibEJSEApbQ81icvBtSU80Pwkddw/132"); //微店图片
WebRequest
wreq
=
WebRequest
.
Create
(
"https://wx.qlogo.cn/mmopen/vi_32/6d2FEzo0bpq8NcQ5sica1EictZLcaeCMboVnxdSqnaxuVPdp0GVuDDRJicfIUQnDibEJSEApbQ81icvBtSU80Pwkddw/132"
);
HttpWebResponse
wresp
=
(
HttpWebResponse
)
wreq
.
GetResponse
();
Stream
s
=
wresp
.
GetResponseStream
();
var
img
=
System
.
Drawing
.
Image
.
FromStream
(
s
);
//img = CutEllipse(img, new Rectangle(120, 120, r, r), new Size(r, r));
img
=
CutEllipse
(
img
,
new
Rectangle
(
0
,
0
,
img
.
Width
,
img
.
Height
),
new
Size
(
img
.
Width
,
img
.
Height
));
Graphics
g
=
Graphics
.
FromImage
(
imgBack
);
g
.
DrawImage
(
imgBack
,
0
,
0
,
imgBack
.
Width
,
imgBack
.
Height
);
//g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
//g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框
//g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
g
.
DrawImage
(
img
,
imgBack
.
Width
/
2
-
img
.
Width
/
2
,
imgBack
.
Width
/
2
-
img
.
Width
/
2
,
img
.
Width
,
img
.
Height
);
GC
.
Collect
();
System
.
Drawing
.
Bitmap
bit
=
new
System
.
Drawing
.
Bitmap
(
imgBack
);
bit
.
Save
(
"E:/LDWork/LiuDongWork/MallFB/upfile/temporary/123333333.jpg"
,
System
.
Drawing
.
Imaging
.
ImageFormat
.
Jpeg
);
return
""
;
}
}
}
\ No newline at end of file
Edu.Common/Plugin/IPHelper.cs
deleted
100644 → 0
View file @
b63c7b0c
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Text.RegularExpressions
;
using
System.Threading.Tasks
;
using
System.Web
;
namespace
Edu.Common.Plugin
{
/// <summary>
/// IP转化帮助类
/// </summary>
public
class
IPHelper
{
/// <summary>
/// 判断是否是IP地址
/// </summary>
/// <param name="ipAddress"></param>
/// <returns></returns>
private
static
bool
IsIPAddress
(
string
ipAddress
)
{
if
(
string
.
IsNullOrEmpty
(
ipAddress
))
{
return
false
;
}
ipAddress
=
ipAddress
.
Trim
();
if
(
ipAddress
.
Length
<
7
||
ipAddress
.
Length
>
15
)
{
return
false
;
}
string
pattern
=
@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"
;
Regex
regex
=
new
Regex
(
pattern
,
RegexOptions
.
IgnoreCase
);
return
regex
.
IsMatch
(
ipAddress
);
}
/// <summary>
/// 判断是否是内网IP
/// </summary>
/// <param name="ipAddress"></param>
/// <returns></returns>
private
static
bool
IsInnerIP
(
String
ipAddress
)
{
long
ipNum
=
GetIpNum
(
ipAddress
);
/***** ***** ***** ***** ***** *****
私有IP:
A类 10.0.0.0-10.255.255.255
B类 172.16.0.0-172.31.255.255
C类 192.168.0.0-192.168.255.255
还有127.0.0.1这个环回地址
***** ***** ***** ***** ***** *****/
long
aBegin
=
GetIpNum
(
"10.0.0.0"
);
long
aEnd
=
GetIpNum
(
"10.255.255.255"
);
long
bBegin
=
GetIpNum
(
"172.16.0.0"
);
long
bEnd
=
GetIpNum
(
"172.31.255.255"
);
long
cBegin
=
GetIpNum
(
"192.168.0.0"
);
long
cEnd
=
GetIpNum
(
"192.168.255.255"
);
bool
isInnerIp
=
IsInner
(
ipNum
,
aBegin
,
aEnd
)
||
IsInner
(
ipNum
,
bBegin
,
bEnd
)
||
IsInner
(
ipNum
,
cBegin
,
cEnd
)
||
ipAddress
.
Equals
(
"127.0.0.1"
);
return
isInnerIp
;
}
/// <summary>
/// 获取IP地址
/// </summary>
/// <param name="ipAddress"></param>
/// <returns></returns>
private
static
long
GetIpNum
(
String
ipAddress
)
{
String
[]
ip
=
ipAddress
.
Split
(
'.'
);
long
a
=
int
.
Parse
(
ip
[
0
]);
long
b
=
int
.
Parse
(
ip
[
1
]);
long
c
=
int
.
Parse
(
ip
[
2
]);
long
d
=
int
.
Parse
(
ip
[
3
]);
long
ipNum
=
a
*
256
*
256
*
256
+
b
*
256
*
256
+
c
*
256
+
d
;
return
ipNum
;
}
/// <summary>
/// 判断是否是整数
/// </summary>
/// <param name="userIp"></param>
/// <param name="begin"></param>
/// <param name="end"></param>
/// <returns></returns>
private
static
bool
IsInner
(
long
userIp
,
long
begin
,
long
end
)
{
return
(
userIp
>=
begin
)
&&
(
userIp
<=
end
);
}
}
}
\ No newline at end of file
Edu.Common/Plugin/LogHelper.cs
View file @
10a039dc
using
System
;
using
System.Configuration
;
using
System.IO
;
using
System.Threading.Tasks
;
...
...
Edu.Common/Plugin/NetFileHelper.cs
View file @
10a039dc
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
using
System.Runtime.InteropServices
;
using
System.Runtime.InteropServices
;
namespace
Edu.Common.Plugin
{
...
...
Edu.Common/Plugin/RSAHelper.cs
deleted
100644 → 0
View file @
b63c7b0c
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Reflection
;
using
System.Security.Cryptography
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
Edu.Common.Plugin
{
/// <summary>
/// 枚举帮助类
/// </summary>
public
class
RSAHelper
{
//Erp公钥
static
readonly
string
TargetPublicKey
=
@"<RSAKeyValue><Modulus>qr3Hipzvvn9dsFn1juGEM5vHiYl4whHnJ7DFqC4ZVAotL0bRCZd7vWvPl53AJY9p8u2hNgPP3J6/I5NairyCiizW++8kCPue1Lpq9qc9ueBwij1bo5Sqi1nswpJsKyd9kOD5aOzqrnuhKTLYMZN9fkHl3L4wjpuVz1xlTgpYdxc=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>"
;
//小程序私钥
//static string PublicKey = @"<RSAKeyValue><Modulus>4W8WliHbSkVE20kIze0KvCIOJgwG4PSRHHb6LNjc3smttrU952pGFi7g7dly1tj+oNUpCB5Ba5a1EUQI9vlfCXUYRICdiNneW6T78BFED5x8HZp8JUCMeatD8wFrKPVV4twaj2QtqGsTw60ZVmVFzHi/eNLVBYvfnzKCGCp0dG8=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
static
readonly
string
PrivateKey
=
@"<RSAKeyValue><Modulus>4W8WliHbSkVE20kIze0KvCIOJgwG4PSRHHb6LNjc3smttrU952pGFi7g7dly1tj+oNUpCB5Ba5a1EUQI9vlfCXUYRICdiNneW6T78BFED5x8HZp8JUCMeatD8wFrKPVV4twaj2QtqGsTw60ZVmVFzHi/eNLVBYvfnzKCGCp0dG8=</Modulus><Exponent>AQAB</Exponent><P>97qvHs/ptDJiw+FGvqvNLOJX1TJPbed9sfNNEhAZe+x/XPQfnzu0IxEkIxH/BL29gVryWExH8b/ZcgQuXCpUsw==</P><Q>6PXZPVntcKdo4ZlbQAU/+VI47rAZGkNtW/w25UVGrVC31Zc3GSqCVlKZgccRe6/PLrjOUBKDRYiYd3Yj+azXVQ==</Q><DP>ciyzW3Md1jRGutrQHT7XUHF9Y8BNRW0kzGhDRCjxZeEpDjFhhaUhr+vNiPBZZdkBR2YgPbviiLQQRvFQYKAN6w==</DP><DQ>AgBSrG9AVe5qPT3nBcbFxOuK56oFnS8lGlwxUIjm0YhW+/O/mmy+D+XHwdCm+swI2Jrn0tJF5GwG+0e9iWCNvQ==</DQ><InverseQ>WsKgSM0RiMFpXGoyL8bqZi2q1MAB+iz4WXPcL/OmHKluo8wuK/Pk+zOKkyjMO91tKXhjLOd7xpVb1AIsk/fAeQ==</InverseQ><D>daiCNFd1WZ0vo/fJZZkLWke39LmgOXbL6fxa9F83X/wI82xC4+4+qENNWDZ+zo2w0AaRBNCtulaNKHbG6wgaRh15yuOHkr90oydquHHPUqCjFWnIEYx2NnX85HtyBTwowCeePajCTsEi6vMRgKlBCIFWxqc6z5To1lTgfqtKb8k=</D></RSAKeyValue>"
;
/// <summary>
/// 加密
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public
static
byte
[]
EncryptData
(
byte
[]
data
)
{
RSACryptoServiceProvider
rsa
=
new
RSACryptoServiceProvider
();
//将公钥导入到RSA对象中,准备加密;
rsa
.
FromXmlString
(
TargetPublicKey
);
//对数据data进行加密,并返回加密结果;
//第二个参数用来选择Padding的格式
byte
[]
buffer
=
rsa
.
Encrypt
(
data
,
false
);
return
buffer
;
}
/// <summary>
/// 解密
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public
static
byte
[]
DecryptData
(
byte
[]
data
)
{
RSACryptoServiceProvider
rsa
=
new
RSACryptoServiceProvider
(
1024
);
//将私钥导入RSA中,准备解密;
rsa
.
FromXmlString
(
PrivateKey
);
//对数据进行解密,并返回解密结果;
return
rsa
.
Decrypt
(
data
,
false
);
}
}
}
Edu.Common/Plugin/SecurityHelper.cs
View file @
10a039dc
...
...
@@ -5,25 +5,6 @@ using System.Text;
namespace
Edu.Common.Plugin
{
/// <summary>
/// 加密方式
/// </summary>
public
enum
EncryptMode
{
/// <summary>
/// 不加密
/// </summary>
None
=
0
,
/// <summary>
/// 16位MD5 -- (常规)
/// </summary>
MD5_16
=
1
,
/// <summary>
/// 32位MD5
/// </summary>
MD5_32
=
3
}
/// <summary>
/// 安全相关辅助类
/// </summary>
...
...
Edu.Common/Plugin/StringHelper.cs
deleted
100644 → 0
View file @
b63c7b0c
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Specialized
;
using
System.IO
;
using
System.Reflection
;
using
System.Text
;
using
System.Text.RegularExpressions
;
using
System.Web
;
namespace
Edu.Common.Plugin
{
/// <summary>
/// 字符相关辅助类
/// </summary>
public
class
StringHelper
{
#
region
"随机数"
/// <summary>
/// 随机生成8位随机数
/// </summary>
/// <returns></returns>
public
static
string
GenerateGuid8String
()
{
string
guid
=
GenerateGuid16String
(
Guid
.
NewGuid
());
return
guid
.
Substring
(
0
,
8
);
}
/// <summary>
/// Guid to string(16)
/// </summary>
public
static
string
GenerateGuid16String
()
{
return
GenerateGuid16String
(
Guid
.
NewGuid
());
}
/// <summary>
/// Guid to string(16)
/// </summary>
public
static
string
GenerateGuid16String
(
Guid
guid
)
{
long
i
=
1
;
foreach
(
byte
b
in
guid
.
ToByteArray
())
i
*=
((
int
)
b
+
1
);
return
string
.
Format
(
"{0:x}"
,
i
-
DateTime
.
Now
.
Ticks
);
}
#
endregion
#
region
"日期相关"
/// <summary>
/// 获取星期的字符串
/// </summary>
/// <param name="day">指定日期</param>
/// <returns></returns>
public
static
string
GetWeekChar
(
DateTime
day
)
{
string
[]
str
=
new
string
[]
{
"日"
,
"一"
,
"二"
,
"三"
,
"四"
,
"五"
,
"六"
};
int
week
=
int
.
Parse
(
day
.
DayOfWeek
.
ToString
(
"D"
));
return
"星期"
+
str
[
week
];
}
/// <summary>
/// 获取周几的字符串
/// </summary>
/// <param name="day">指定日期</param>
/// <returns></returns>
public
static
string
GetWeek
(
DateTime
day
)
{
string
[]
str
=
new
string
[]
{
"日"
,
"一"
,
"二"
,
"三"
,
"四"
,
"五"
,
"六"
};
int
week
=
int
.
Parse
(
day
.
DayOfWeek
.
ToString
(
"D"
));
return
"周"
+
str
[
week
];
}
/// <summary>
/// 获取星期几[英文版]
/// </summary>
/// <param name="day"></param>
/// <returns></returns>
public
static
string
GetWeek_eg
(
DateTime
day
)
{
string
[]
str
=
new
string
[]
{
"Sun"
,
"Mon"
,
"Tues"
,
"Wed"
,
"Thu"
,
"Fri"
,
"Sat"
};
int
week
=
int
.
Parse
(
day
.
DayOfWeek
.
ToString
(
"D"
));
return
str
[
week
];
}
/// <summary>
/// 获取星期几的字符串
/// </summary>
/// <param name="day">指定日期</param>
/// <returns></returns>
public
static
string
GetWeekDay
(
DateTime
day
)
{
string
[]
str
=
new
string
[]
{
"日"
,
"一"
,
"二"
,
"三"
,
"四"
,
"五"
,
"六"
};
int
week
=
int
.
Parse
(
day
.
DayOfWeek
.
ToString
(
"D"
));
return
"星期"
+
str
[
week
];
}
/// <summary>
/// 得到两个日期总天数
/// </summary>
/// <param name="dStart">开始日期</param>
/// <param name="dEnd">结束日期</param>
/// <returns></returns>
public
static
int
GetDateDayInterval
(
DateTime
?
dStart
,
DateTime
?
dEnd
)
{
if
(!
dStart
.
HasValue
||
!
dEnd
.
HasValue
)
return
0
;
//TimeSpan ts = Convert.ToDateTime(dEnd.Value.ToString("yyyy-MM-dd"))-(Convert.ToDateTime(dStart.Value.ToString("yyyy-MM-dd")));
int
TotalDay
=
0
;
while
(
true
)
{
if
(
dStart
.
Value
.
ToString
(
"yyyy-MM-dd"
)
==
dEnd
.
Value
.
ToString
(
"yyyy-MM-dd"
))
{
TotalDay
+=
1
;
break
;
}
else
{
TotalDay
+=
1
;
}
dStart
=
dStart
.
Value
.
AddDays
(
1
);
}
return
TotalDay
;
}
/// <summary>
/// 得到两个日期之差的时间戳
/// </summary>
/// <param name="dStart">开始日期</param>
/// <param name="dEnd">结束日期</param>
/// <returns></returns>
public
static
TimeSpan
GetDayTimeSpan
(
DateTime
?
dStart
,
DateTime
?
dEnd
)
{
if
(!
dStart
.
HasValue
||
!
dEnd
.
HasValue
)
return
new
TimeSpan
();
return
dEnd
.
Value
-
dStart
.
Value
;
}
/// <summary>
/// 格式化日期为字符串2018-01-01 12:00:00
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public
static
string
FormatDateTimeStr
(
object
date
)
{
try
{
DateTime
.
TryParse
(
date
.
ToString
(),
out
DateTime
dt
);
return
dt
.
ToString
(
"yyyy-MM-dd HH:mm:ss"
);
}
catch
{
}
return
""
;
}
/// <summary>
/// 格式化日期
/// </summary>
/// <param name="date"></param>
/// <returns></returns>
public
static
string
FormatDate
(
object
date
)
{
try
{
DateTime
.
TryParse
(
date
.
ToString
(),
out
DateTime
dt
);
return
dt
.
ToString
(
"yyyy-MM-dd"
);
}
catch
{
}
return
""
;
}
/// <summary>
/// 时间戳转换为时间
/// </summary>
/// <param name="ticks"></param>
/// <returns></returns>
public
static
DateTime
GetDateTimeByTicks
(
string
ticks
)
{
try
{
DateTime
startTime
=
TimeZoneInfo
.
ConvertTime
(
new
System
.
DateTime
(
1970
,
1
,
1
),
TimeZoneInfo
.
Local
);
// 当地时区
DateTime
dt
=
startTime
.
AddMilliseconds
(
long
.
Parse
(
ticks
));
return
dt
;
}
catch
(
Exception
)
{
return
new
DateTime
(
1970
,
1
,
1
);
}
}
/// <summary>
/// 获取年份月份中的最后一天
/// </summary>
/// <param name="year">年份</param>
/// <param name="month">月份</param>
/// <returns></returns>
public
static
DateTime
GetMonthLastDay
(
int
year
,
int
month
)
{
return
new
DateTime
(
year
,
month
,
1
).
AddMonths
(
1
).
AddDays
(-
1
);
}
/// <summary>
/// 时间
/// </summary>
/// <param name="Time"></param>
/// <returns></returns>
public
static
string
GetTypeOfMeal
(
DateTime
?
Time
)
{
string
returnStr
=
""
;
if
(
Time
.
HasValue
)
{
if
(
Time
.
Value
.
Hour
<
10
)
{
returnStr
=
"早餐"
;
}
else
if
(
Time
.
Value
.
Hour
>=
10
&&
Time
.
Value
.
Hour
<
14
)
{
returnStr
=
"午餐"
;
}
else
{
returnStr
=
"晚餐"
;
}
}
return
returnStr
;
}
/// <summary>
/// 时间
/// </summary>
/// <param name="Time"></param>
/// <returns></returns>
public
static
string
GetTypeOfTime
(
DateTime
?
Time
)
{
string
returnStr
=
""
;
if
(
Time
.
HasValue
)
{
if
(
Time
.
Value
.
Hour
<
10
)
{
returnStr
=
"早上"
;
}
else
if
(
Time
.
Value
.
Hour
>=
10
&&
Time
.
Value
.
Hour
<
14
)
{
returnStr
=
"中午"
;
}
else
{
returnStr
=
"晚上"
;
}
}
return
returnStr
;
}
/// <summary>
/// 获取星期简写
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public
static
string
GetWeek
(
string
value
)
{
string
dateStr
=
""
;
switch
(
value
.
ToLower
())
{
case
"monday"
:
dateStr
=
"Mon"
;
break
;
case
"tuesday"
:
dateStr
=
"Tues"
;
break
;
case
"wednesday"
:
dateStr
=
"Wed"
;
break
;
case
"thursday"
:
dateStr
=
"Thu"
;
break
;
case
"friday"
:
dateStr
=
"Fri"
;
break
;
case
"saturday"
:
dateStr
=
"Sat"
;
break
;
case
"sunday"
:
dateStr
=
"Sun"
;
break
;
default
:
break
;
}
return
dateStr
.
ToUpper
();
}
/// <summary>
/// 根据日期获取星期几
/// </summary>
/// <param name="date">日期</param>
/// <returns></returns>
public
static
int
GetWeekDayNum
(
DateTime
date
)
{
int
week
=
1
;
var
dt
=
date
.
DayOfWeek
.
ToString
();
switch
(
dt
)
{
case
"Monday"
:
week
=
1
;
break
;
case
"Tuesday"
:
week
=
2
;
break
;
case
"Wednesday"
:
week
=
3
;
break
;
case
"Thursday"
:
week
=
4
;
break
;
case
"Friday"
:
week
=
5
;
break
;
case
"Saturday"
:
week
=
6
;
break
;
case
"Sunday"
:
week
=
7
;
break
;
}
return
week
;
}
/// <summary>
/// 获取月份简写
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public
static
string
GetDate
(
string
value
)
{
string
dateStr
=
""
;
switch
(
value
)
{
case
"01"
:
dateStr
=
"JAN"
;
break
;
case
"02"
:
dateStr
=
"FEB"
;
break
;
case
"03"
:
dateStr
=
"MAR"
;
break
;
case
"04"
:
dateStr
=
"APR"
;
break
;
case
"05"
:
dateStr
=
"MAY"
;
break
;
case
"06"
:
dateStr
=
"JUN"
;
break
;
case
"07"
:
dateStr
=
"JUL"
;
break
;
case
"08"
:
dateStr
=
"AUG"
;
break
;
case
"09"
:
dateStr
=
"SEP"
;
break
;
case
"10"
:
dateStr
=
"OCT"
;
break
;
case
"11"
:
dateStr
=
"NOV"
;
break
;
case
"12"
:
dateStr
=
"DEC"
;
break
;
default
:
break
;
}
return
dateStr
;
}
/// <summary>
/// 取得某月的第一天
/// </summary>
/// <param name="datetime">要取得月份第一天的时间</param>
/// <returns></returns>
public
static
DateTime
FirstDayOfMonth
(
DateTime
datetime
)
{
return
datetime
.
AddDays
(
1
-
datetime
.
Day
);
}
/// <summary>
/// 取得某月的最后一天
/// </summary>
/// <param name="datetime">要取得月份最后一天的时间</param>
/// <returns></returns>
public
static
DateTime
LastDayOfMonth
(
DateTime
datetime
)
{
return
datetime
.
AddDays
(
1
-
datetime
.
Day
).
AddMonths
(
1
).
AddDays
(-
1
);
}
/// <summary>
/// 取得上个月第一天
/// </summary>
/// <param name="datetime">要取得上个月第一天的当前时间</param>
/// <returns></returns>
public
static
DateTime
FirstDayOfPreviousMonth
(
DateTime
datetime
)
{
return
datetime
.
AddDays
(
1
-
datetime
.
Day
).
AddMonths
(-
1
);
}
/// <summary>
/// 取得上个月的最后一天
/// </summary>
/// <param name="datetime">要取得上个月最后一天的当前时间</param>
/// <returns></returns>
public
static
DateTime
LastDayOfPrdviousMonth
(
DateTime
datetime
)
{
return
datetime
.
AddDays
(
1
-
datetime
.
Day
).
AddDays
(-
1
);
}
/// <summary>
/// 获取某月总天数
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public
static
int
GetMonthTotalDay
(
DateTime
dt
)
{
int
day
=
dt
.
AddDays
(
1
-
dt
.
Day
).
AddMonths
(
1
).
AddDays
(-
1
).
Day
;
return
day
;
}
/// <summary>
/// 获取日期之间的周六周天的天数
/// </summary>
/// <param name="startTime"></param>
/// <param name="endTime"></param>
/// <returns></returns>
public
static
int
TotalDay
(
DateTime
startTime
,
DateTime
endTime
)
{
int
TotalDay
=
0
;
int
dayCount
=
(
endTime
-
startTime
).
Days
;
if
(
dayCount
==
1
)
{
string
dt
=
startTime
.
DayOfWeek
.
ToString
();
if
(
dt
==
"Friday"
||
dt
==
"Saturday"
)
{
TotalDay
=
2
;
}
else
if
(
dt
==
"Sunday"
)
{
TotalDay
=
1
;
}
}
else
{
int
Saturday
=
0
;
int
Sunday
=
0
;
for
(
int
i
=
0
;
i
<
dayCount
;
i
++)
{
string
dt
=
startTime
.
AddDays
(
i
+
1
).
DayOfWeek
.
ToString
();
switch
(
dt
)
{
case
"Saturday"
:
Saturday
+=
1
;
break
;
case
"Sunday"
:
Sunday
+=
1
;
break
;
}
}
if
(
Sunday
==
0
&&
Saturday
==
1
)
//截止日期只有一个周六
{
TotalDay
=
2
;
}
else
if
(
Sunday
==
1
&&
Saturday
==
0
)
// 截止日期只有一个周天
{
TotalDay
=
1
;
}
else
{
if
(
Saturday
>
Sunday
)
{
TotalDay
=
Saturday
+
Sunday
+
2
;
}
else
{
TotalDay
=
Saturday
+
Sunday
+
1
;
}
}
}
return
TotalDay
;
}
public
static
string
DateStringFromNow
(
DateTime
dt
)
{
TimeSpan
span
=
DateTime
.
Now
-
dt
;
if
(
span
.
TotalDays
>
60
)
{
return
dt
.
ToShortDateString
();
}
else
if
(
span
.
TotalDays
>
30
)
{
return
"1个月前"
;
}
else
if
(
span
.
TotalDays
>
14
)
{
return
"2周前"
;
}
else
if
(
span
.
TotalDays
>
7
)
{
return
"1周前"
;
}
else
if
(
span
.
TotalDays
>
1
)
{
return
string
.
Format
(
"{0}天前"
,
(
int
)
Math
.
Floor
(
span
.
TotalDays
));
}
else
if
(
span
.
TotalHours
>
1
)
{
return
string
.
Format
(
"{0}小时前"
,
(
int
)
Math
.
Floor
(
span
.
TotalHours
));
}
else
if
(
span
.
TotalMinutes
>
1
)
{
return
string
.
Format
(
"{0}分钟前"
,
(
int
)
Math
.
Floor
(
span
.
TotalMinutes
));
}
else
if
(
span
.
TotalSeconds
>=
1
)
{
return
string
.
Format
(
"{0}秒前"
,
(
int
)
Math
.
Floor
(
span
.
TotalSeconds
));
}
else
{
return
"1秒前"
;
}
}
#
endregion
#
region
字符转换
/// <summary>
/// 为字符串左边填充字符
/// <example>
/// 111 LPAD(,'0',8) --> 00000111
/// </example>
/// </summary>
/// <param name="str">字符串</param>
/// <param name="swap">替换的字符</param>
/// <param name="length">长度</param>
/// <returns></returns>
public
static
string
LPAD
(
object
str
,
string
swap
,
int
length
)
{
string
s
=
str
.
ToString
();
for
(
int
i
=
0
;
i
<
length
-
str
.
ToString
().
Length
;
i
++)
{
s
=
swap
+
s
;
}
return
s
;
}
/// <summary>
/// 填充字符到字符串左边
/// </summary>
/// <param name="str"></param>
/// <param name="swap">填充的字符</param>
/// <param name="len">长度</param>
/// <returns></returns>
public
static
string
LFillChar
(
string
str
,
string
swap
,
int
len
)
{
for
(
int
i
=
0
;
i
<
len
;
i
++)
{
str
=
swap
+
str
;
}
return
str
;
}
/// <summary>
/// 获取数字的中文字符
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public
static
string
GetNumberChar
(
int
number
)
{
string
[]
str
=
new
string
[]
{
"一"
,
"二"
,
"三"
,
"四"
,
"五"
,
"六"
,
"七"
,
"八"
,
"九"
,
"十"
,
"十一"
,
"十二"
};
if
(
number
<
0
&&
number
>
str
.
Length
)
{
return
"{没有该数字的中文名称}"
;
}
return
str
[
number
-
1
];
}
/// <summary>
/// 转换字符串为整形值
/// </summary>
/// <param name="val"></param>
/// <param name="invaildReturnVal">当数值无效返回值</param>
/// <returns></returns>
public
static
int
ParseInt
(
string
val
,
int
invaildReturnVal
)
{
if
(
string
.
IsNullOrEmpty
(
val
)
||
!
ValidateHepler
.
IsNumber
(
val
))
return
invaildReturnVal
;
return
int
.
Parse
(
val
);
}
/// <summary>
/// 字符串转化为字节
/// </summary>
/// <param name="Str"></param>
/// <returns></returns>
public
static
byte
[]
ToByte
(
string
Str
)
{
return
Convert
.
FromBase64String
(
Str
);
}
/// <summary>
/// 字节转化为字符串
/// </summary>
/// <param name="Byt"></param>
/// <returns></returns>
public
static
string
ToStr
(
Byte
[]
Byt
)
{
return
Convert
.
ToBase64String
(
Byt
);
}
/// <summary>
/// 正常转换为字节
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public
static
Byte
[]
NormalToByte
(
string
str
)
{
return
Encoding
.
Default
.
GetBytes
(
str
);
}
/// <summary>
/// 正常专号为字符串
/// </summary>
/// <param name="Byt"></param>
/// <returns></returns>
public
static
string
NormalToStr
(
byte
[]
Byt
)
{
return
Encoding
.
Default
.
GetString
(
Byt
);
}
/// <summary>
/// 截取指定长度字符
/// </summary>
/// <param name="str"></param>
/// <param name="len"></param>
/// <returns></returns>
public
static
string
Substring
(
string
str
,
int
len
)
{
return
Substring
(
str
,
len
,
""
);
}
/// <summary>
/// 截取指定长度字符,字符过长可填充指定符号
/// </summary>
/// <param name="str"></param>
/// <param name="len"></param>
/// <param name="endFillChar"></param>
/// <returns></returns>
public
static
string
Substring
(
string
str
,
int
len
,
string
endFillChar
)
{
if
(
str
.
Length
<
len
)
return
str
;
else
return
str
.
Substring
(
0
,
len
)
+
endFillChar
;
}
/// <summary>
/// 移除字符串末尾指定字符
/// </summary>
/// <param name="str">需要移除的字符串</param>
/// <param name="value">指定字符</param>
/// <returns>移除后的字符串</returns>
public
static
string
RemoveLastChar
(
string
str
,
string
value
)
{
int
_finded
=
str
.
LastIndexOf
(
value
);
if
(
_finded
!=
-
1
)
{
return
str
.
Substring
(
0
,
_finded
);
}
return
str
;
}
/// <summary>
/// 过滤HTML标志
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public
static
string
FilterHtml
(
string
html
)
{
if
(
string
.
IsNullOrWhiteSpace
(
html
))
{
return
""
;
}
//删除脚本
html
=
Regex
.
Replace
(
html
,
@"<script[^>]*?>.*?</script>"
,
""
,
RegexOptions
.
IgnoreCase
);
//删除HTML
html
=
Regex
.
Replace
(
html
,
@"<(.[^>]*)>"
,
""
,
RegexOptions
.
IgnoreCase
);
//Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
html
=
Regex
.
Replace
(
html
,
@"-->"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"<!--.*"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(quot|#34);"
,
"\""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(amp|#38);"
,
"&"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(lt|#60);"
,
"<"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(gt|#62);"
,
">"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(nbsp|#160);"
,
" "
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(iexcl|#161);"
,
"\xa1"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(cent|#162);"
,
"\xa2"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(pound|#163);"
,
"\xa3"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(copy|#169);"
,
"\xa9"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&#(\d+);"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"\r"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"\n"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"\\"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@";"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@":"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"<p>"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"“"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"</p>"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@" "
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
html
.
Replace
(
"<"
,
""
);
html
=
html
.
Replace
(
">"
,
""
);
html
=
html
.
Replace
(
"\r\n"
,
""
);
return
html
;
}
/// <summary>
/// 转换HTML的特殊代码为实际字符
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public
static
string
HtmlToChar
(
object
html
)
{
string
str
=
html
.
ToString
();
str
=
str
.
Replace
(
"<br>"
,
"\n"
);
str
=
str
.
Replace
(
">"
,
">"
);
str
=
str
.
Replace
(
"<"
,
"<"
);
str
=
str
.
Replace
(
" "
,
" "
);
str
=
str
.
Replace
(
"""
,
"\""
);
str
=
Regex
.
Replace
(
str
,
"<[^>]+>"
,
""
);
return
str
;
}
/// <summary>
/// 字符串转化为HTML
/// </summary>
/// <param name="str">字符串</param>
/// <returns></returns>
public
static
string
CharToHtml
(
object
str
)
{
if
(
str
==
null
)
return
""
;
string
c
=
str
.
ToString
();
c
=
c
.
Replace
(
"<"
,
@"<"
);
c
=
c
.
Replace
(
">"
,
@">"
);
c
=
c
.
Replace
(
"\r\n"
,
@"<br/>"
);
c
=
c
.
Replace
(
" "
,
" "
);
return
c
;
}
/// <summary>
/// App过滤HTML的特殊代码
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public
static
string
AppHtmlFilterr
(
string
html
)
{
if
(
string
.
IsNullOrWhiteSpace
(
html
))
{
return
""
;
}
html
=
RemoveLastChar
(
html
,
"</p>"
);
html
=
html
.
Replace
(
"<br>"
,
"\n"
);
html
=
html
.
Replace
(
"</p>"
,
"</p>\n"
);
html
=
html
.
Replace
(
"</li>"
,
"</li>\n"
);
//删除脚本
html
=
Regex
.
Replace
(
html
,
@"<script[^>]*?>.*?</script>"
,
""
,
RegexOptions
.
IgnoreCase
);
//删除HTML
html
=
Regex
.
Replace
(
html
,
@"<(.[^>]*)>"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"-->"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"<!--.*"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(quot|#34);"
,
"\""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(amp|#38);"
,
"&"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(lt|#60);"
,
"<"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(gt|#62);"
,
">"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(nbsp|#160);"
,
" "
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(iexcl|#161);"
,
"\xa1"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(cent|#162);"
,
"\xa2"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(pound|#163);"
,
"\xa3"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(copy|#169);"
,
"\xa9"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&#(\d+);"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"\r"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"\\"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@";"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@":"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"<p>"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"“"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"</p>"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@" "
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
html
.
Replace
(
"<"
,
""
);
html
=
html
.
Replace
(
">"
,
""
);
return
html
;
}
/// <summary>
/// 替换成Word换行符
/// </summary>
/// <param name="html"></param>
/// <returns></returns>
public
static
string
WordHtmlFilter
(
string
html
)
{
if
(
string
.
IsNullOrWhiteSpace
(
html
))
{
return
""
;
}
html
=
RemoveLastChar
(
html
,
"</p>"
);
html
=
html
.
Replace
(
"</p>"
,
"</p>"
);
html
=
html
.
Replace
(
"</li>"
,
"</li>"
);
//删除脚本
html
=
Regex
.
Replace
(
html
,
@"<script[^>]*?>.*?</script>"
,
""
,
RegexOptions
.
IgnoreCase
);
//删除HTML
html
=
Regex
.
Replace
(
html
,
@"<(.[^>]*)>"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"-->"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"<!--.*"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(quot|#34);"
,
"\""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(amp|#38);"
,
"&"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(lt|#60);"
,
"<"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(gt|#62);"
,
">"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(nbsp|#160);"
,
" "
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(iexcl|#161);"
,
"\xa1"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(cent|#162);"
,
"\xa2"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(pound|#163);"
,
"\xa3"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&(copy|#169);"
,
"\xa9"
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"&#(\d+);"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"\r"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"\\"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@";"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@":"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"<p>"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"“"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@"</p>"
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
Regex
.
Replace
(
html
,
@" "
,
""
,
RegexOptions
.
IgnoreCase
);
html
=
html
.
Replace
(
"<"
,
""
);
html
=
html
.
Replace
(
">"
,
""
);
return
html
;
}
#
endregion
#
region
url
相关
/// <summary>
/// 为url附加参数,自动识别?号
/// </summary>
/// <param name="url">原始网址</param>
/// <param name="paramName">URL参数名称</param>
/// <param name="paramValue">参数值不做urlenode处理,有需要外部处理</param>
public
static
string
AppendUrlParam
(
string
url
,
string
paramName
,
object
paramValue
)
{
string
parm
=
string
.
Format
(
"{0}={1}"
,
paramName
,
paramValue
.
ToString
());
if
(!
url
.
Contains
(
"?"
))
url
+=
"?"
;
if
(
url
.
Contains
(
"&"
))
url
+=
"&"
;
url
+=
parm
;
return
url
;
}
/// <summary>
/// urlEncode字符串编码,转换为大写格式
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public
static
string
UrlEncodeToUpperCase
(
string
value
)
{
value
=
System
.
Web
.
HttpUtility
.
UrlEncode
(
value
,
Encoding
.
UTF8
);
byte
[]
buf
=
Encoding
.
ASCII
.
GetBytes
(
value
);
// value.GetBytes("ASCII");//这里是我个人的扩展方法,等同于Encoding.ASCII.GetBytes(str)
for
(
int
i
=
0
;
i
<
buf
.
Length
;
i
++)
if
(
buf
[
i
]
==
'%'
)
{
if
(
buf
[
i
+
1
]
>=
'a'
)
buf
[
i
+
1
]
-=
32
;
if
(
buf
[
i
+
2
]
>=
'a'
)
buf
[
i
+
2
]
-=
32
;
i
+=
2
;
}
return
Encoding
.
ASCII
.
GetString
(
buf
);
}
/// <summary>
/// urlEncode字符串编码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public
static
string
UrlEncode
(
string
str
)
{
if
(!
string
.
IsNullOrEmpty
(
str
))
{
return
System
.
Web
.
HttpUtility
.
UrlEncode
(
str
).
Replace
(
"+"
,
"%20"
);
}
else
{
return
""
;
}
}
/// <summary>
/// urlDecode字符串编码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public
static
string
UrlDecode
(
string
str
)
{
if
(!
string
.
IsNullOrEmpty
(
str
))
{
return
System
.
Web
.
HttpUtility
.
UrlDecode
(
str
);
}
else
{
return
""
;
}
}
/// <summary>
/// 字符串转Unicode
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public
static
string
ToUnicodeString
(
string
str
)
{
StringBuilder
strResult
=
new
StringBuilder
();
if
(!
string
.
IsNullOrEmpty
(
str
))
{
for
(
int
i
=
0
;
i
<
str
.
Length
;
i
++)
{
strResult
.
Append
(
"\\u"
);
strResult
.
Append
(((
int
)
str
[
i
]).
ToString
(
"x"
));
}
}
return
strResult
.
ToString
();
}
/// <summary>
/// Unicode转字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public
static
string
FromUnicodeString
(
string
str
)
{
//最直接的方法Regex.Unescape(str);
StringBuilder
strResult
=
new
StringBuilder
();
if
(!
string
.
IsNullOrEmpty
(
str
))
{
string
[]
strlist
=
str
.
Replace
(
"\\"
,
""
).
Split
(
'u'
);
try
{
for
(
int
i
=
1
;
i
<
strlist
.
Length
;
i
++)
{
int
charCode
=
Convert
.
ToInt32
(
strlist
[
i
],
16
);
strResult
.
Append
((
char
)
charCode
);
}
}
catch
{
return
Regex
.
Unescape
(
str
);
}
}
return
strResult
.
ToString
();
}
#
endregion
#
region
数组字符串
/// <summary>
/// 把数组格式化为字符串 [1,2,3,4]
/// </summary>
/// <param name="val">数组</param>
/// <returns>
/// 格式 : 1,2,3,4
/// </returns>
public
static
string
ArrayToString
(
string
[]
val
)
{
if
(
val
==
null
||
val
.
Length
==
0
)
return
null
;
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
val
.
Length
;
i
++)
{
if
(
i
!=
val
.
Length
-
1
)
sb
.
Append
(
string
.
Format
(
"{0},"
,
val
[
i
].
Trim
()));
else
sb
.
Append
(
val
[
i
].
Trim
());
}
return
sb
.
ToString
();
}
/// <summary>
/// 把数组格式化为字符串 [1,2,3,4]
/// </summary>
/// <param name="val">数组</param>
/// <returns>
/// 格式 : 1,2,3,4
/// </returns>
public
static
string
ArrayToString
(
int
[]
val
)
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
val
.
Length
;
i
++)
{
if
(
i
!=
val
.
Length
-
1
)
sb
.
Append
(
string
.
Format
(
"{0},"
,
val
[
i
]));
else
sb
.
Append
(
val
[
i
]);
}
return
sb
.
ToString
();
}
/// <summary>
/// 把数组格式化为字符串 [1,2,3,4]
/// </summary>
/// <param name="val">数组</param>
/// <param name="split">间隔符号</param>
/// <returns>
/// 格式 : 1,2,3,4
/// </returns>
public
static
string
ArrayToString
(
string
[]
val
,
string
split
)
{
if
(
val
==
null
||
val
.
Length
==
0
)
return
null
;
StringBuilder
sb
=
new
StringBuilder
();
for
(
int
i
=
0
;
i
<
val
.
Length
;
i
++)
{
if
(
i
!=
val
.
Length
-
1
)
sb
.
Append
(
val
[
i
].
Trim
()
+
split
);
else
sb
.
Append
(
val
[
i
].
Trim
());
}
return
sb
.
ToString
();
}
/// <summary>
/// 将字符串转换为数组
/// </summary>
/// <param name="str">eg: (1,2,3,4)</param>
/// <returns></returns>
public
static
int
[]
StringToArray
(
string
str
)
{
string
[]
ary
=
str
.
Split
(
','
);
int
[]
val
=
new
int
[
ary
.
Length
];
for
(
int
i
=
0
;
i
<
ary
.
LongLength
;
i
++)
{
val
[
i
]
=
int
.
Parse
(
ary
[
i
]);
}
return
val
;
}
/// <summary>
/// 将字符串转换为List int eg: (1,2,3,4)
/// </summary>
/// <param name="str">eg: (1,2,3,4)</param>
/// <returns></returns>
public
static
List
<
int
>
StringToListInt
(
string
str
)
{
string
[]
ary
=
str
.
Split
(
','
);
List
<
int
>
val
=
new
List
<
int
>();
for
(
int
i
=
0
;
i
<
ary
.
LongLength
;
i
++)
{
val
.
Add
(
Convert
.
ToInt32
(
ary
[
i
]));
}
return
val
;
}
/// <summary>
/// 将字符串转换为List int
/// </summary>
/// <param name="str">eg: (1,2,3,4)</param>
/// <returns></returns>
public
static
List
<
string
>
StringToListString
(
string
str
)
{
string
[]
ary
=
str
.
Split
(
','
);
List
<
string
>
val
=
new
List
<
string
>();
for
(
int
i
=
0
;
i
<
ary
.
LongLength
;
i
++)
{
val
.
Add
(
ary
[
i
]);
}
return
val
;
}
#
endregion
#
region
验证码
/// <summary>
/// 生成随机验证码
/// </summary>
/// <param name="codeCount">验证码长度</param>
/// <returns></returns>
public
static
string
CreateRandomCode
(
int
codeCount
)
{
string
allChar
=
"0,1,2,3,4,5,6,7,8,9"
;
string
[]
allCharArray
=
allChar
.
Split
(
','
);
string
randomCode
=
""
;
int
temp
=
-
1
;
Random
rand
=
new
Random
();
for
(
int
i
=
0
;
i
<
codeCount
;
i
++)
{
if
(
temp
!=
-
1
)
{
rand
=
new
Random
(
i
*
temp
*
((
int
)
DateTime
.
Now
.
Ticks
));
}
int
t
=
rand
.
Next
(
10
);
if
(
temp
==
t
)
{
return
CreateRandomCode
(
codeCount
);
}
temp
=
t
;
randomCode
+=
allCharArray
[
t
];
}
return
randomCode
;
}
#
endregion
#
region
文件类型相关
/// <summary>
/// 验证是否为视频文件
/// </summary>
/// <param name="fileName">文件名</param>
/// <returns></returns>
public
static
bool
IsVideo
(
string
fileName
)
{
string
fileExtension
=
Path
.
GetExtension
(
fileName
).
ToLower
();
string
allFileExtension
=
@".drc,.dsm,.dsv,.dsa,.dss.vob,.ifo,.d2v,.flv,.fli,.flc,.flic,.ivf,.mkv,.mpg,.mpeg,.mpe,.m1v,.m2v,.mpv2,.mp2v,.dat,.ts,.tp .tpr,.pva,.pss,.mp4,.m4v,.m4p,.m4b,.3gp,.3gpp,.3g2,.3gp2,.ogm,.mov,.qt,.amr,.ratdvd,.rt,.rp,.smi,.smil,.rm,.ram,.rmvb,.rpm,.roq,.swf,.smk,.bik,.wmv,.wmp,.wm,.asf,.avi,.asx,.m3u,.pls,.wvx,.wax,.wmx,.mpcpl"
;
return
allFileExtension
.
Contains
(
fileExtension
);
}
/// <summary>
/// 验证是否为音频文件
/// </summary>
/// <param name="fileName">文件名</param>
/// <returns></returns>
public
static
bool
IsVoice
(
string
fileName
)
{
string
fileExtension
=
Path
.
GetExtension
(
fileName
).
ToLower
();
string
allFileExtension
=
@".wav,.wma,.ra,.ogg,.mpc,.m4a,.aac,.mpa,.mp2,.m1a,.m2a,.mp3,.mid,.midi,.rmi,.mka,.ac3,.dts,.cda,.au,.snd,.aif,.aifc,.aiff "
;
return
allFileExtension
.
Contains
(
fileExtension
);
}
/// <summary>
/// 验证是否为图片文件
/// </summary>
/// <param name="fileName">文件名</param>
/// <returns></returns>
public
static
bool
IsImg
(
string
fileName
)
{
string
fileExtension
=
Path
.
GetExtension
(
fileName
).
ToLower
();
string
allFileExtension
=
@".bmp,.jpg,.png,.tiff,.gif,.pcx,.tga,.exif,.fpx,.svg,.psd,.cdr,.pcd,.dxf,.ufo,.eps,.ai,.raw,.WMF,webp"
;
return
allFileExtension
.
Contains
(
fileExtension
);
}
#
endregion
#
region
Url
获取
Get
参数
/// <summary>
/// 将查询字符串解析转换为名值集合.
/// </summary>
/// <param name="queryString"></param>
/// <returns></returns>
public
static
NameValueCollection
GetQueryString
(
string
queryString
)
{
return
GetQueryString
(
queryString
,
null
,
true
);
}
/// <summary>
/// 将查询字符串解析转换为名值集合.
/// </summary>
/// <param name="queryString"></param>
/// <param name="encoding"></param>
/// <param name="isEncoded"></param>
/// <returns></returns>
public
static
NameValueCollection
GetQueryString
(
string
queryString
,
Encoding
encoding
,
bool
isEncoded
)
{
queryString
=
queryString
.
TrimStart
(
'?'
);
NameValueCollection
result
=
new
NameValueCollection
(
StringComparer
.
OrdinalIgnoreCase
);
if
(!
string
.
IsNullOrEmpty
(
queryString
))
{
int
count
=
queryString
.
Length
;
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
int
startIndex
=
i
;
int
index
=
-
1
;
while
(
i
<
count
)
{
char
item
=
queryString
[
i
];
if
(
item
==
'='
)
{
if
(
index
<
0
)
{
index
=
i
;
}
}
else
if
(
item
==
'&'
)
{
break
;
}
i
++;
}
string
key
=
null
;
string
value
=
null
;
if
(
index
>=
0
)
{
key
=
queryString
[
startIndex
..
index
];
value
=
queryString
.
Substring
(
index
+
1
,
(
i
-
index
)
-
1
);
}
else
{
key
=
queryString
[
startIndex
..
i
];
}
if
(
isEncoded
)
{
result
[
MyUrlDeCode
(
key
,
encoding
)]
=
MyUrlDeCode
(
value
,
encoding
);
}
else
{
result
[
key
]
=
value
;
}
if
((
i
==
(
count
-
1
))
&&
(
queryString
[
i
]
==
'&'
))
{
result
[
key
]
=
string
.
Empty
;
}
}
}
return
result
;
}
/// <summary>
/// 解码URL.
/// </summary>
/// <param name="encoding">null为自动选择编码</param>
/// <param name="str"></param>
/// <returns></returns>
public
static
string
MyUrlDeCode
(
string
str
,
Encoding
encoding
)
{
if
(
encoding
==
null
)
{
Encoding
utf8
=
Encoding
.
UTF8
;
//首先用utf-8进行解码
string
code
=
HttpUtility
.
UrlDecode
(
str
.
ToUpper
(),
utf8
);
//将已经解码的字符再次进行编码.
string
encode
=
HttpUtility
.
UrlEncode
(
code
,
utf8
).
ToUpper
();
if
(
str
==
encode
)
encoding
=
Encoding
.
UTF8
;
else
encoding
=
Encoding
.
GetEncoding
(
"gb2312"
);
}
return
HttpUtility
.
UrlDecode
(
str
,
encoding
);
}
#
endregion
/// <summary>
/// 26个数字转换为对应的26个字母
/// </summary>
/// <param name="NUM"></param>
/// <returns></returns>
public
static
string
ReturnEnChar
(
int
NUM
)
{
string
EnChar
=
""
;
switch
(
NUM
)
{
case
1
:
EnChar
=
"A"
;
break
;
case
2
:
EnChar
=
"B"
;
break
;
case
3
:
EnChar
=
"C"
;
break
;
case
4
:
EnChar
=
"D"
;
break
;
case
5
:
EnChar
=
"E"
;
break
;
case
6
:
EnChar
=
"F"
;
break
;
case
7
:
EnChar
=
"G"
;
break
;
case
8
:
EnChar
=
"H"
;
break
;
case
9
:
EnChar
=
"I"
;
break
;
case
10
:
EnChar
=
"J"
;
break
;
case
11
:
EnChar
=
"K"
;
break
;
case
12
:
EnChar
=
"L"
;
break
;
case
13
:
EnChar
=
"M"
;
break
;
case
14
:
EnChar
=
"N"
;
break
;
case
15
:
EnChar
=
"O"
;
break
;
case
16
:
EnChar
=
"P"
;
break
;
case
17
:
EnChar
=
"Q"
;
break
;
case
18
:
EnChar
=
"R"
;
break
;
case
19
:
EnChar
=
"S"
;
break
;
case
20
:
EnChar
=
"T"
;
break
;
case
21
:
EnChar
=
"U"
;
break
;
case
22
:
EnChar
=
"V"
;
break
;
case
23
:
EnChar
=
"W"
;
break
;
case
24
:
EnChar
=
"X"
;
break
;
case
25
:
EnChar
=
"Y"
;
break
;
case
26
:
EnChar
=
"Z"
;
break
;
}
return
EnChar
;
}
/// <summary>
/// 获取文件内容
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public
static
string
GetFileContent
(
string
filePath
)
{
if
(
File
.
Exists
(
filePath
))
{
string
content
=
File
.
ReadAllText
(
filePath
);
return
content
;
}
else
{
return
""
;
}
}
/// <summary>
/// 获取文件内容
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public
static
string
GetFileContent
(
string
filePath
,
Encoding
encoding
)
{
if
(
File
.
Exists
(
filePath
))
{
string
content
=
File
.
ReadAllText
(
filePath
,
encoding
);
return
content
;
}
else
{
return
""
;
}
}
/// <summary>
/// 替换索引出为“*”
/// </summary>
/// <param name="Input">输入字符串</param>
/// <returns></returns>
public
static
string
GetStarString
(
string
Input
)
{
string
Output
;
switch
(
Input
.
Length
)
{
case
1
:
Output
=
"*"
;
break
;
case
2
:
Output
=
Input
[
0
]
+
"*"
;
break
;
case
0
:
Output
=
""
;
break
;
default
:
Output
=
Input
.
Substring
(
0
,
1
);
for
(
int
i
=
0
;
i
<
Input
.
Length
-
2
;
i
++)
{
Output
+=
"*"
;
}
Output
+=
Input
.
Substring
(
Input
.
Length
-
1
,
1
);
break
;
}
return
Output
;
}
/// <summary>
/// 判断是否有中文
/// </summary>
/// <param name="inputStr"></param>
/// <returns></returns>
public
static
bool
IsChinese
(
string
inputStr
)
{
return
System
.
Text
.
RegularExpressions
.
Regex
.
IsMatch
(
inputStr
,
@"^[\u4e00-\u9fa5]+$"
);
}
/// <summary>
/// 去掉查询的不可见字符
/// </summary>
/// <param name="inputStr"></param>
/// <returns></returns>
public
static
string
GetNDateStr
(
string
inputStr
)
{
string
str
=
inputStr
;
if
(
str
.
Contains
(
"\u200e"
)
||
str
.
Contains
(
"年"
)
||
str
.
Contains
(
"月"
)
||
str
.
Contains
(
"日"
))
{
str
=
str
.
Replace
(
"\u200e"
,
""
).
Replace
(
"年"
,
"-"
).
Replace
(
"月"
,
"-"
).
Replace
(
"日"
,
""
);
}
return
str
;
}
}
}
Edu.Common/Plugin/ValidateHepler.cs
deleted
100644 → 0
View file @
b63c7b0c
using
System
;
using
System.Reflection
;
using
System.Text.RegularExpressions
;
namespace
Edu.Common.Plugin
{
/// <summary>
/// 验证类
/// </summary>
public
static
class
ValidateHepler
{
/// <summary>
/// 返回ok为参数合法,其余为不合法
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public
static
string
Validate
(
this
object
obj
)
{
Type
type
=
obj
.
GetType
();
PropertyInfo
[]
fields
=
type
.
GetProperties
();
foreach
(
var
item
in
fields
)
{
if
(
item
.
IsDefined
(
typeof
(
AbstractValidateAttribute
),
true
))
{
object
[]
validateAttribute
=
item
.
GetCustomAttributes
(
typeof
(
AbstractValidateAttribute
),
true
);
foreach
(
var
validate
in
validateAttribute
)
{
AbstractValidateAttribute
val
=
(
AbstractValidateAttribute
)
validate
;
string
message
=
val
.
CheckValiadte
(
item
.
Name
,
item
.
GetValue
(
obj
));
if
(!
message
.
Equals
(
"ok"
))
{
return
message
;
}
}
}
}
return
"ok"
;
}
/// <summary>
/// 验证字符串是否数字
/// </summary>
/// <param name="val"></param>
/// <returns></returns>
public
static
bool
IsNumber
(
string
val
)
{
if
(
val
==
"0"
)
return
true
;
if
(
string
.
IsNullOrEmpty
(
val
))
return
false
;
return
Regex
.
IsMatch
(
val
,
@"^[1-9]\d*\.?[0]*$"
);
}
/// <summary>
/// 是否Email
/// </summary>
/// <param name="email"></param>
/// <returns></returns>
public
static
bool
IsEmail
(
string
email
)
{
return
Regex
.
IsMatch
(
email
,
@"^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$"
,
RegexOptions
.
IgnoreCase
);
}
/// <summary>
/// 是否网页地址
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public
static
bool
IsURL
(
string
url
)
{
return
Regex
.
IsMatch
(
url
,
@"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*[^\.\,\)\(\s]$"
);
}
/// <summary>
/// 是否手机号
/// </summary>
/// <param name="mobileNumber">手机号</param>
/// <returns>是否有效的日期</returns>
public
static
bool
IsMobileNumber
(
string
mobileNumber
)
{
if
(
string
.
IsNullOrWhiteSpace
(
mobileNumber
))
{
return
false
;
}
if
(
Regex
.
IsMatch
(
mobileNumber
,
"^1[3456789]\\d{9}$"
,
RegexOptions
.
IgnoreCase
))
{
return
true
;
}
else
{
return
false
;
}
}
/// <summary>
/// 判断传入的参数是否有效的日期格式
/// </summary>
/// <param name="dateTime">需要判断的字符串</param>
/// <returns>是否有效的日期</returns>
public
static
bool
IsDateTime
(
string
dateTime
)
{
try
{
DateTime
dteConvert
=
DateTime
.
Parse
(
dateTime
.
ToString
());
return
true
;
}
catch
(
FormatException
)
{
return
false
;
}
}
}
/// <summary>
/// 属性特性验证
/// </summary>
[
AttributeUsage
(
AttributeTargets
.
Property
)]
public
abstract
class
AbstractValidateAttribute
:
System
.
Attribute
{
/// <summary>
/// 成功提示信息
/// </summary>
protected
string
successMessage
=
"ok"
;
/// <summary>
/// 显示信息
/// </summary>
public
string
showMessage
=
""
;
/// <summary>
/// 验证
/// </summary>
/// <param name="name"></param>
/// <param name="value">值</param>
/// <returns></returns>
public
abstract
string
CheckValiadte
(
string
name
,
object
value
);
}
/// <summary>
/// 整形数字范围验证
/// </summary>
public
class
IntRangeValidateAttribute
:
AbstractValidateAttribute
{
private
readonly
int
_min
=
0
;
private
readonly
int
_max
=
0
;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="min"></param>
/// <param name="max"></param>
public
IntRangeValidateAttribute
(
int
min
,
int
max
)
{
this
.
_min
=
min
;
this
.
_max
=
max
;
}
/// <summary>
/// 整形数字范围验证
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
public
override
string
CheckValiadte
(
string
name
,
object
value
)
{
if
(
value
==
null
||
string
.
IsNullOrWhiteSpace
(
value
.
ToString
()))
{
return
successMessage
;
}
int
_value
=
(
int
)
value
;
if
(
_value
>=
_min
&&
_value
<=
_max
)
{
return
successMessage
;
}
else
{
if
(!
string
.
IsNullOrWhiteSpace
(
showMessage
))
{
return
showMessage
;
}
else
{
return
$"参数
{
name
}
的值范围为
{
_min
}
到
{
_max
}
"
;
}
}
}
}
/// <summary>
/// 字符串必填验证
/// </summary>
public
class
RequiredValidateAttribute
:
AbstractValidateAttribute
{
/// <summary>
/// 字符串验证
/// </summary>
/// <param name="name">提示名称</param>
/// <param name="value">值</param>
/// <returns></returns>
public
override
string
CheckValiadte
(
string
name
,
object
value
)
{
if
(
value
==
null
||
string
.
IsNullOrWhiteSpace
(
value
.
ToString
()))
{
if
(!
string
.
IsNullOrWhiteSpace
(
showMessage
))
{
return
showMessage
;
}
else
{
return
$"参数
{
name
}
为必填字段"
;
}
}
else
{
return
successMessage
;
}
}
}
/// <summary>
/// 字符串范围验证
/// </summary>
public
class
StringRangeValidateAttribute
:
AbstractValidateAttribute
{
private
readonly
int
_min
=
0
;
private
readonly
int
_max
=
0
;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="min"></param>
/// <param name="max"></param>
public
StringRangeValidateAttribute
(
int
min
,
int
max
)
{
this
.
_min
=
min
;
this
.
_max
=
max
;
}
/// <summary>
/// 字符串范围验证
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
public
override
string
CheckValiadte
(
string
name
,
object
value
)
{
if
(
value
==
null
||
string
.
IsNullOrWhiteSpace
(
value
.
ToString
()))
{
return
successMessage
;
}
string
_value
=
value
.
ToString
();
if
(
_value
.
Length
>=
_min
&&
_value
.
Length
<=
_max
)
{
return
successMessage
;
}
else
{
if
(!
string
.
IsNullOrWhiteSpace
(
showMessage
))
{
return
showMessage
;
}
else
{
return
$"参数
{
name
}
的长度范围为
{
_min
}
到
{
_max
}
个字符"
;
}
}
}
}
/// <summary>
/// 数字校验
/// </summary>
public
class
IsNumberValidateAttribute
:
AbstractValidateAttribute
{
/// <summary>
/// 数字校验
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
public
override
string
CheckValiadte
(
string
name
,
object
value
)
{
if
(
value
==
null
||
string
.
IsNullOrWhiteSpace
(
value
.
ToString
()))
{
return
successMessage
;
}
string
number
=
value
.
ToString
();
if
(!
Regex
.
IsMatch
(
number
,
@"^[0-9]\d*\.?[0]*$"
))
{
if
(!
string
.
IsNullOrWhiteSpace
(
showMessage
))
{
return
showMessage
;
}
else
{
return
$"参数
{
name
}
必须为数字"
;
}
}
else
{
return
successMessage
;
}
}
}
/// <summary>
/// 邮箱校验
/// </summary>
public
class
IsEmailValidateAttribute
:
AbstractValidateAttribute
{
/// <summary>
/// 邮箱校验
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
public
override
string
CheckValiadte
(
string
name
,
object
value
)
{
if
(
value
==
null
||
string
.
IsNullOrWhiteSpace
(
value
.
ToString
()))
{
return
successMessage
;
}
string
email
=
value
.
ToString
();
if
(
string
.
IsNullOrEmpty
(
email
))
{
return
showMessage
;
}
else
{
if
(!
Regex
.
IsMatch
(
email
,
@"^([0-9a-z]+[-._+&])*[0-9a-z]+@([-0-9a-z]+[.])+[a-z]{2,6}$"
,
RegexOptions
.
IgnoreCase
))
{
if
(!
string
.
IsNullOrWhiteSpace
(
showMessage
))
{
return
showMessage
;
}
else
{
return
$"参数
{
name
}
必须为合法邮箱"
;
}
}
else
{
return
successMessage
;
}
}
}
}
/// <summary>
/// 时间类型验证
/// </summary>
public
class
IsDateTimeValidateAttribute
:
AbstractValidateAttribute
{
/// <summary>
/// 时间类型验证
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
public
override
string
CheckValiadte
(
string
name
,
object
value
)
{
if
(
value
==
null
||
string
.
IsNullOrWhiteSpace
(
value
.
ToString
()))
{
return
successMessage
;
}
try
{
Convert
.
ToDateTime
(
value
.
ToString
());
return
successMessage
;
}
catch
(
Exception
)
{
return
$"参数
{
name
}
必须为日期格式!"
;
}
}
}
/// <summary>
/// 正则表达式验证
/// </summary>
public
class
RegularValidateAttribute
:
AbstractValidateAttribute
{
private
readonly
string
pattern
=
""
;
/// <summary>
///
/// </summary>
/// <param name="pattern">验证规则</param>
public
RegularValidateAttribute
(
string
pattern
)
{
this
.
pattern
=
pattern
;
}
/// <summary>
/// 正则验证
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <returns></returns>
public
override
string
CheckValiadte
(
string
name
,
object
value
)
{
if
(
value
==
null
||
string
.
IsNullOrWhiteSpace
(
value
.
ToString
()))
{
return
successMessage
;
}
if
(!
Regex
.
IsMatch
(
value
.
ToString
(),
pattern
,
RegexOptions
.
IgnoreCase
))
{
if
(!
string
.
IsNullOrWhiteSpace
(
showMessage
))
{
return
showMessage
;
}
else
{
return
$"参数
{
name
}
必不合法"
;
}
}
else
{
return
successMessage
;
}
}
}
}
\ No newline at end of file
Edu.Common/Plugin/WeiXinHelper.cs
deleted
100644 → 0
View file @
b63c7b0c
using
Newtonsoft.Json.Linq
;
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Net
;
using
System.Text
;
using
System.Xml
;
namespace
Edu.Common.Plugin
{
public
class
WeiXinHelper
{
static
readonly
List
<
WeChatMiniAppMessage
>
weChatMiniAppMessages
=
new
List
<
WeChatMiniAppMessage
>();
static
WeiXinHelper
()
{
// //(拼团失败通知) 980(拼团成功通知)
weChatMiniAppMessages
.
Add
(
new
WeChatMiniAppMessage
()
{
Tid
=
"1953"
,
Title
=
"拼团失败通知"
,
KidList
=
new
List
<
int
>()
{
1
,
2
,
3
},
SceneDesc
=
""
,
});
weChatMiniAppMessages
.
Add
(
new
WeChatMiniAppMessage
()
{
Tid
=
"980"
,
Title
=
"拼团成功通知"
,
KidList
=
new
List
<
int
>()
{
1
,
2
,
3
},
SceneDesc
=
""
,
});
}
/// <summary>
/// 获取微信用户OpenId
/// </summary>
/// <param name="AppId"></param>
/// <param name="AppSecret"></param>
/// <param name="Code"></param>
/// <returns></returns>
public
static
string
GetWeChatOpenId
(
string
AppId
,
string
AppSecret
,
string
Code
)
{
string
result
=
""
;
string
openid
=
""
;
try
{
//请求路径
string
url
=
"https://api.weixin.qq.com/sns/jscode2session?appid="
+
AppId
+
"&secret="
+
AppSecret
+
"&js_code="
+
Code
+
"&grant_type=authorization_code"
;
result
=
Common
.
Plugin
.
HttpHelper
.
HttpGet
(
url
);
if
(
result
!=
null
&&
!
string
.
IsNullOrEmpty
(
result
))
{
JObject
jObj
=
JObject
.
Parse
(
result
);
openid
=
jObj
[
"openid"
].
ToString
();
}
}
catch
(
Exception
ex
)
{
Common
.
Plugin
.
LogHelper
.
Write
(
ex
,
"GetWeChatOpenId:result="
+
result
);
}
return
openid
;
}
/// <summary>
/// 获取微信订阅消息模板
/// </summary>
/// <param name="AppId"></param>
/// <param name="AppSecret"></param>
/// <returns></returns>
public
static
List
<
WeChatMiniAppMessage
>
GetWeChatTemplateMessage
(
string
AppId
,
string
AppSecret
)
{
string
result
=
""
;
string
access_token
=
""
;
try
{
//请求路径
string
url
=
string
.
Format
(
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}"
,
AppId
,
AppSecret
);
result
=
Common
.
Plugin
.
HttpHelper
.
HttpGet
(
url
);
if
(
result
!=
null
&&
!
string
.
IsNullOrEmpty
(
result
))
{
JObject
jObj
=
JObject
.
Parse
(
result
);
access_token
=
jObj
[
"access_token"
].
ToString
();
}
if
(!
string
.
IsNullOrWhiteSpace
(
access_token
))
{
string
addTemplate
=
string
.
Format
(
"https://api.weixin.qq.com/wxaapi/newtmpl/addtemplate?access_token={0}"
,
access_token
);
foreach
(
var
item
in
weChatMiniAppMessages
)
{
result
=
""
;
string
newParameters
=
string
.
Format
(
"tid={0}"
,
item
.
Tid
);
if
(
item
.
KidList
!=
null
&&
item
.
KidList
.
Count
>
0
)
{
int
index
=
0
;
foreach
(
var
subItem
in
item
.
KidList
)
{
newParameters
+=
string
.
Format
(
"&kidList[{0}]={1}"
,
index
,
subItem
);
index
++;
}
}
newParameters
+=
string
.
Format
(
"&sceneDesc={0}"
,
item
.
Title
);
result
=
Common
.
Plugin
.
HttpHelper
.
HttpPostStr
(
addTemplate
,
newParameters
);
if
(!
string
.
IsNullOrWhiteSpace
(
result
))
{
JObject
jObj
=
JObject
.
Parse
(
result
);
item
.
MessageTemplateId
=
jObj
[
"priTmplId"
].
ToString
();
}
}
}
}
catch
(
Exception
ex
)
{
Common
.
Plugin
.
LogHelper
.
Write
(
ex
,
"GetWeChatAccessToken:result="
+
result
);
}
return
weChatMiniAppMessages
;
}
}
/// <summary>
/// 微信小程序订阅消息实体
/// </summary>
public
class
WeChatMiniAppMessage
{
/// <summary>
/// 模板标题Id
/// </summary>
public
string
Tid
{
get
;
set
;
}
/// <summary>
/// 模板名称
/// </summary>
public
string
Title
{
get
;
set
;
}
/// <summary>
/// 开发者自行组合好的模板关键词列表
/// </summary>
public
List
<
int
>
KidList
{
get
;
set
;
}
/// <summary>
/// 服务场景描述,15个字以内
/// </summary>
public
string
SceneDesc
{
get
;
set
;
}
/// <summary>
/// 模板消息Id
/// </summary>
public
string
MessageTemplateId
{
get
;
set
;
}
}
/// <summary>
/// 获取用心信息帮助类
/// </summary>
public
class
GetUsersHelper
{
/// <summary>
/// 获取链接返回数据
/// </summary>
/// <param name="Url">链接</param>
/// <param name="type">请求类型</param>
/// <returns></returns>
public
string
GetUrltoHtml
(
string
Url
,
string
type
)
{
try
{
System
.
Net
.
WebRequest
wReq
=
System
.
Net
.
WebRequest
.
Create
(
Url
);
System
.
Net
.
WebResponse
wResp
=
wReq
.
GetResponse
();
System
.
IO
.
Stream
respStream
=
wResp
.
GetResponseStream
();
using
System.IO.StreamReader
reader
=
new
System
.
IO
.
StreamReader
(
respStream
,
Encoding
.
GetEncoding
(
type
));
return
reader
.
ReadToEnd
();
}
catch
(
System
.
Exception
ex
)
{
return
ex
.
Message
;
}
}
}
#
region
实体类
/// <summary>
/// 微信小程序验证返回结果
/// </summary>
public
class
Result
{
/// <summary>
/// openid
/// </summary>
public
string
Openid
{
get
;
set
;
}
/// <summary>
/// session_key
/// </summary>
public
string
Session_key
{
get
;
set
;
}
/// <summary>
/// 错误状态码
/// </summary>
public
string
Errcode
{
get
;
set
;
}
/// <summary>
/// 错误提示信息
/// </summary>
public
string
Errmsg
{
get
;
set
;
}
}
#
endregion
public
class
WxPayData
{
public
const
string
SIGN_TYPE_MD5
=
"MD5"
;
public
const
string
SIGN_TYPE_HMAC_SHA256
=
"HMAC-SHA256"
;
public
WxPayData
()
{
}
//采用排序的Dictionary的好处是方便对数据包进行签名,不用再签名之前再做一次排序
private
readonly
SortedDictionary
<
string
,
object
>
m_values
=
new
SortedDictionary
<
string
,
object
>();
/**
* 设置某个字段的值
* @param key 字段名
* @param value 字段值
*/
public
void
SetValue
(
string
key
,
object
value
)
{
m_values
[
key
]
=
value
;
}
/**
* 根据字段名获取某个字段的值
* @param key 字段名
* @return key对应的字段值
*/
public
object
GetValue
(
string
key
)
{
m_values
.
TryGetValue
(
key
,
out
object
o
);
return
o
;
}
/**
* 判断某个字段是否已设置
* @param key 字段名
* @return 若字段key已被设置,则返回true,否则返回false
*/
public
bool
IsSet
(
string
key
)
{
m_values
.
TryGetValue
(
key
,
out
object
o
);
if
(
null
!=
o
)
return
true
;
else
return
false
;
}
/**
* @将Dictionary转成xml
* @return 经转换得到的xml串
* @throws WxPayException
**/
public
string
ToXml
()
{
//数据为空时不能转化为xml格式
if
(
0
==
m_values
.
Count
)
{
//_logger.LogError(this.GetType().ToString(), "WxPayData数据为空!");
throw
new
Exception
(
"WxPayData数据为空!"
);
}
string
xml
=
"<xml>"
;
foreach
(
KeyValuePair
<
string
,
object
>
pair
in
m_values
)
{
//字段值不能为null,会影响后续流程
if
(
pair
.
Value
==
null
)
{
//_logger.LogError(this.GetType().ToString(), "WxPayData内部含有值为null的字段!");
throw
new
Exception
(
"WxPayData内部含有值为null的字段!"
);
}
if
(
pair
.
Value
.
GetType
()
==
typeof
(
int
))
{
xml
+=
"<"
+
pair
.
Key
+
">"
+
pair
.
Value
+
"</"
+
pair
.
Key
+
">"
;
}
else
if
(
pair
.
Value
.
GetType
()
==
typeof
(
string
))
{
xml
+=
"<"
+
pair
.
Key
+
">"
+
"<![CDATA["
+
pair
.
Value
+
"]]></"
+
pair
.
Key
+
">"
;
}
else
//除了string和int类型不能含有其他数据类型
{
//_logger.LogError(this.GetType().ToString(), "WxPayData字段数据类型错误!");
throw
new
Exception
(
"WxPayData字段数据类型错误!"
);
}
}
xml
+=
"</xml>"
;
return
xml
;
}
public
SortedDictionary
<
string
,
object
>
NoSignFromXml
(
string
xml
)
{
if
(
string
.
IsNullOrEmpty
(
xml
))
{
//_logger.LogError(this.GetType().ToString(), "将空的xml串转换为WxPayData不合法!");
throw
new
Exception
(
"将空的xml串转换为WxPayData不合法!"
);
}
XmlDocument
xmlDoc
=
new
XmlDocument
();
xmlDoc
.
LoadXml
(
xml
);
XmlNode
xmlNode
=
xmlDoc
.
FirstChild
;
//获取到根节点<xml>
XmlNodeList
nodes
=
xmlNode
.
ChildNodes
;
foreach
(
XmlNode
xn
in
nodes
)
{
XmlElement
xe
=
(
XmlElement
)
xn
;
m_values
[
xe
.
Name
]
=
xe
.
InnerText
;
//获取xml的键值对到WxPayData内部的数据中
}
return
m_values
;
}
/**
* @Dictionary格式转化成url参数格式
* @ return url格式串, 该串不包含sign字段值
*/
public
string
ToUrl
()
{
string
buff
=
""
;
foreach
(
KeyValuePair
<
string
,
object
>
pair
in
m_values
)
{
if
(
pair
.
Value
==
null
)
{
//_logger.LogError(this.GetType().ToString(), "WxPayData内部含有值为null的字段!");
throw
new
Exception
(
"WxPayData内部含有值为null的字段!"
);
}
if
(
pair
.
Key
!=
"sign"
&&
pair
.
Value
.
ToString
()
!=
""
)
{
buff
+=
pair
.
Key
+
"="
+
pair
.
Value
+
"&"
;
}
}
buff
=
buff
.
Trim
(
'&'
);
return
buff
;
}
public
bool
CheckSign
()
{
////如果没有设置签名,则跳过检测
//if (!IsSet("sign"))
//{
// //_logger.LogError(this.GetType().ToString(), "WxPayData签名存在但不合法!");
// throw new Exception("WxPayData签名存在但不合法!");
//}
////如果设置了签名但是签名为空,则抛异常
//else if (GetValue("sign") == null || GetValue("sign").ToString() == "")
//{
// //_logger.LogError(this.GetType().ToString(), "WxPayData签名存在但不合法!");
// throw new Exception("WxPayData签名存在但不合法!");
//}
////获取接收到的签名
//string return_sign = GetValue("sign").ToString();
////在本地计算新的签名
//string cal_sign = payReq.CreateMd5Sign(_accessor, model.WeChatApiSecret);
return
true
;
//if (cal_sign == return_sign)
//{
// return true;
//}
//_logger.LogError(this.GetType().ToString(), "WxPayData签名验证错误!");
throw
new
Exception
(
"WxPayData签名验证错误!"
);
}
}
}
Edu.Common/Plugin/WordTemplateHelper.cs
View file @
10a039dc
using
NPOI.OpenXmlFormats.Wordprocessing
;
using
NPOI.XWPF.UserModel
;
using
System
;
using
System.Collections.Generic
;
using
System.IO
;
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
namespace
Edu.Common.Plugin
{
...
...
Edu.Model/Entity/Course/RB_Course_Source.cs
View file @
10a039dc
...
...
@@ -96,5 +96,9 @@ namespace Edu.Model.Entity.Course
/// </summary>
public
DateStateEnum
Status
{
get
;
set
;
}
/// <summary>
/// 是否允许下载(1-允许,0-不允许)
/// </summary>
public
int
IsDownLoad
{
get
;
set
;
}
}
}
\ No newline at end of file
Edu.Model/ViewModel/Course/RB_Course_Category_ViewModel.cs
View file @
10a039dc
using
System
;
using
Edu.Common
;
using
System
;
namespace
Edu.Model.ViewModel.Course
{
...
...
@@ -20,7 +21,7 @@ namespace Edu.Model.ViewModel.Course
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
CreateTime
);
return
this
.
CreateTime
.
FormatTime
(
);
}
}
...
...
@@ -36,7 +37,7 @@ namespace Edu.Model.ViewModel.Course
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
UpdateTime
);
return
this
.
UpdateTime
.
FormatTime
(
);
}
}
}
...
...
Edu.Model/ViewModel/Course/RB_Course_ViewModel.cs
View file @
10a039dc
using
System
;
using
Edu.Common
;
using
System
;
namespace
Edu.Model.ViewModel.Course
{
...
...
@@ -21,11 +22,11 @@ namespace Edu.Model.ViewModel.Course
/// <summary>
/// 创建时间字符串
/// </summary>
public
string
CreateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
CreateTime
);
}
}
public
string
CreateTimeStr
{
get
{
return
this
.
CreateTime
.
FormatTime
();
}
}
/// <summary>
/// 更新时间
/// </summary>
public
string
UpdateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
UpdateTime
);
}
}
public
string
UpdateTimeStr
{
get
{
return
this
.
UpdateTime
.
FormatTime
(
);
}
}
}
}
\ No newline at end of file
Edu.Model/ViewModel/Question/RB_Question_Point_ViewModel.cs
View file @
10a039dc
namespace
Edu.Model.ViewModel.Question
using
Edu.Common
;
namespace
Edu.Model.ViewModel.Question
{
/// <summary>
/// 题目知识点视图实体类
...
...
@@ -18,11 +20,11 @@
/// <summary>
/// 创建时间字符串
/// </summary>
public
string
CreateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
CreateTime
);
}
}
public
string
CreateTimeStr
{
get
{
return
this
.
CreateTime
.
FormatTime
();
}
}
/// <summary>
/// 更新时间字符串
/// </summary>
public
string
UpdateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
UpdateTime
);
}
}
public
string
UpdateTimeStr
{
get
{
return
this
.
UpdateTime
.
FormatTime
(
);
}
}
}
}
Edu.Model/ViewModel/Question/RB_Question_ViewModel.cs
View file @
10a039dc
using
System.Collections.Generic
;
using
Edu.Common
;
using
System.Collections.Generic
;
namespace
Edu.Model.ViewModel.Question
{
...
...
@@ -20,12 +21,12 @@ namespace Edu.Model.ViewModel.Question
/// <summary>
/// 创建时间字符串
/// </summary>
public
string
CreateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
CreateTime
);
}
}
public
string
CreateTimeStr
{
get
{
return
this
.
CreateTime
.
FormatTime
(
);
}
}
/// <summary>
/// 更新时间字符串
/// </summary>
public
string
UpdateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
UpdateTime
);
}
}
public
string
UpdateTimeStr
{
get
{
return
this
.
UpdateTime
.
FormatTime
(
);
}
}
/// <summary>
/// 答案列表
...
...
Edu.Model/ViewModel/User/RB_Assist_ViewModel.cs
View file @
10a039dc
using
System
;
using
Edu.Common
;
using
Edu.Common.Plugin
;
using
System
;
namespace
Edu.Model.ViewModel.User
{
...
...
@@ -26,7 +28,7 @@ namespace Edu.Model.ViewModel.User
/// <summary>
/// 创建时间字符串
/// </summary>
public
string
CreateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
CreateTime
);
}
}
public
string
CreateTimeStr
{
get
{
return
this
.
CreateTime
.
FormatTime
(
);
}
}
/// <summary>
/// 更新人
...
...
@@ -36,11 +38,11 @@ namespace Edu.Model.ViewModel.User
/// <summary>
/// 更新时间
/// </summary>
public
string
UpdateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
UpdateTime
);
}
}
public
string
UpdateTimeStr
{
get
{
return
this
.
UpdateTime
.
FormatTime
(
);
}
}
/// <summary>
/// 审核状态字符串
/// </summary>
public
string
AuditStatusStr
{
get
{
return
Common
.
Plugin
.
EnumHelper
.
GetEnumName
(
this
.
AuditStatus
);
}
}
public
string
AuditStatusStr
{
get
{
return
this
.
AuditStatus
.
ToName
(
);
}
}
}
}
\ No newline at end of file
Edu.Model/ViewModel/User/RB_Group_ViewModel.cs
View file @
10a039dc
using
System
;
using
Edu.Common
;
using
System
;
namespace
Edu.Model.ViewModel.User
{
...
...
@@ -20,7 +21,7 @@ namespace Edu.Model.ViewModel.User
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
CreateTime
);
return
this
.
CreateTime
.
FormatTime
(
);
}
}
...
...
@@ -39,7 +40,7 @@ namespace Edu.Model.ViewModel.User
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
UpdateTime
);
return
this
.
UpdateTime
.
FormatTime
(
);
}
}
}
...
...
Edu.Model/ViewModel/User/RB_Manager_ViewModel.cs
View file @
10a039dc
using
System
;
using
Edu.Common
;
using
System
;
namespace
Edu.Model.ViewModel.User
{
...
...
@@ -16,7 +17,7 @@ namespace Edu.Model.ViewModel.User
/// <summary>
/// 创建时间字符串
/// </summary>
public
string
CreateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
CreateTime
);
}
}
public
string
CreateTimeStr
{
get
{
return
this
.
CreateTime
.
FormatTime
(
);
}
}
/// <summary>
/// 更新人
...
...
@@ -26,6 +27,6 @@ namespace Edu.Model.ViewModel.User
/// <summary>
/// 更新时间
/// </summary>
public
string
UpdateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
UpdateTime
);
}
}
public
string
UpdateTimeStr
{
get
{
return
this
.
UpdateTime
.
FormatTime
(
);
}
}
}
}
Edu.Model/ViewModel/User/RB_School_ViewModel.cs
View file @
10a039dc
using
System
;
using
Edu.Common
;
using
System
;
namespace
Edu.Model.ViewModel.User
{
...
...
@@ -25,7 +26,7 @@ namespace Edu.Model.ViewModel.User
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
CreateTime
);
return
this
.
CreateTime
.
FormatTime
(
);
}
}
...
...
@@ -41,7 +42,7 @@ namespace Edu.Model.ViewModel.User
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
UpdateTime
);
return
this
.
UpdateTime
.
FormatTime
(
);
}
}
}
...
...
Edu.Model/ViewModel/User/RB_Student_ViewModel.cs
View file @
10a039dc
using
System
;
using
Edu.Common
;
using
System
;
namespace
Edu.Model.ViewModel.User
{
...
...
@@ -16,7 +17,7 @@ namespace Edu.Model.ViewModel.User
/// <summary>
/// 创建时间
/// </summary>
public
string
CreateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
CreateTime
);
}
}
public
string
CreateTimeStr
{
get
{
return
this
.
CreateTime
.
FormatTime
(
);
}
}
/// <summary>
/// 修改人姓名
...
...
@@ -26,7 +27,7 @@ namespace Edu.Model.ViewModel.User
/// <summary>
/// 修改时间
/// </summary>
public
string
UpdateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
UpdateTime
);
}
}
public
string
UpdateTimeStr
{
get
{
return
this
.
UpdateTime
.
FormatTime
(
);
}
}
/// <summary>
/// 禁用、启用字符串
...
...
Edu.Model/ViewModel/User/RB_Teacher_ViewModel.cs
View file @
10a039dc
using
System
;
using
Edu.Common
;
using
Edu.Common.Plugin
;
using
System
;
namespace
Edu.Model.ViewModel.User
{
...
...
@@ -26,7 +28,7 @@ namespace Edu.Model.ViewModel.User
/// <summary>
/// 创建时间
/// </summary>
public
string
CreateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
CreateTime
);
}
}
public
string
CreateTimeStr
{
get
{
return
this
.
CreateTime
.
FormatTime
(
);
}
}
/// <summary>
/// 修改人
...
...
@@ -36,12 +38,12 @@ namespace Edu.Model.ViewModel.User
/// <summary>
/// 修改时间
/// </summary>
public
string
UpdateTimeStr
{
get
{
return
Common
.
ConvertHelper
.
FormatTime
(
this
.
UpdateTime
);
}
}
public
string
UpdateTimeStr
{
get
{
return
this
.
UpdateTime
.
FormatTime
(
);
}
}
/// <summary>
/// 审核状态字符串
/// </summary>
public
string
AuditStatusStr
{
get
{
return
Common
.
Plugin
.
EnumHelper
.
GetEnumName
(
this
.
AuditStatus
);
}
}
public
string
AuditStatusStr
{
get
{
return
this
.
AuditStatus
.
ToName
(
);
}
}
/// <summary>
/// 课程数量
...
...
Edu.WebApi/appsettings.json
View file @
10a039dc
...
...
@@ -12,16 +12,12 @@
},
"JwtSecretKey"
:
"@VIITTOREBORN*2018"
,
"JwtExpirTime"
:
2592000
,
"ApiExpirTime"
:
2592000
,
"AllowedHosts"
:
"*"
,
"OpenValidation"
:
"False"
,
"UploadSiteUrl"
:
"http://192.168.1.214:8120"
,
"ViewFileSiteUrl"
:
"https://viitto-1301420277.cos.ap-chengdu.myqcloud.com"
,
"ImKey"
:
"b612b31e837c79c68f141aeb719d2b20"
,
"ImSecret"
:
"66000451fb72"
,
"Mongo"
:
"mongodb://47.96.25.130:27017"
,
"MongoDBName"
:
"Edu"
,
"RabbitMqConfig"
:
{
"HostName"
:
"47.96.25.130"
,
"VirtualHost"
:
"/"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment