924 lines
37 KiB
C#
924 lines
37 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Data;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Net;
|
||
using System.Net.Security;
|
||
using System.Security.Cryptography;
|
||
using System.Security.Cryptography.X509Certificates;
|
||
using System.Text;
|
||
using System.Web;
|
||
using System.Web.Script.Serialization;
|
||
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Linq;
|
||
using SuperMap.RealEstate.Utility;
|
||
using SuperMap.RealEstate.Web.UI.WebControls;
|
||
using SuperMap.RealEstate.MobileServicePlatform.Business;
|
||
using Business = SuperMap.RealEstate.HighWay.Storage.Business;
|
||
using WxPayAPI;
|
||
|
||
namespace MobileServicePlatform.Common
|
||
{
|
||
public class Common
|
||
{
|
||
//AppID(应用ID)
|
||
public static string AppID = ConfigurationManager.AppSettings["AppID"];
|
||
//AppSecret(应用密钥)
|
||
public static string AppSecret = ConfigurationManager.AppSettings["AppSecret"];
|
||
public static string _Checkout_Success = "4GPOsrtDZ9VVJmwQ_9r-ZfyrzLCtx5E_JhjUfBFqBJA";
|
||
public static string _NewOrder = "xizNAvV2_Q6mBC-TblIw7-Zr2zhUDZOEr4meERLTdbg";
|
||
public static string _BaseUrl = "https://www.ffdzpay.com/gateway/pay";
|
||
public static string _Weatherhost = "http://jisutqybmf.market.alicloudapi.com";
|
||
public static string _WeatherAppCode = "99f7d21d35e647e98706f75109f4b655";
|
||
public const string charSet = "1,2,3,4,5,6,7,8,9";
|
||
|
||
#region 方法 -> Get方式获取参数
|
||
public static string HttpPost(string Url, string postDataStr)
|
||
{
|
||
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
|
||
request.Method = "POST";
|
||
request.ContentType = "application/json; charset=utf-8";
|
||
request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
|
||
StreamWriter writer = new StreamWriter(request.GetRequestStream());
|
||
writer.Write(postDataStr);
|
||
writer.Flush();
|
||
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
||
string encoding = response.ContentEncoding;
|
||
if (encoding == null || encoding.Length < 1)
|
||
{
|
||
encoding = "UTF-8"; //默认编码
|
||
}
|
||
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
|
||
string retString = reader.ReadToEnd();
|
||
return retString;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 获取通道秘钥
|
||
public static string GetAccess(TextBoxEx RefreshTime = null, string _AppID = "", string _AppSecret = "")
|
||
{
|
||
string url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" +
|
||
(string.IsNullOrEmpty(_AppID) ? AppID : _AppID) + "&secret=" + (string.IsNullOrEmpty(_AppSecret) ? AppSecret : _AppSecret);
|
||
//根据url创建HttpWebRequest对象
|
||
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
|
||
objRequest.Method = "get";
|
||
//读取服务器返回信息
|
||
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
|
||
//using作为语句,用于定义一个范围,在此范围的末尾将释放对象
|
||
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
|
||
{
|
||
//ReadToEnd适用于小文件的读取,一次性的返回整个文件
|
||
JObject _JObject = (JObject)JsonConvert.DeserializeObject(sr.ReadToEnd());
|
||
try
|
||
{
|
||
string access_token = _JObject["access_token"].ToString(),
|
||
LastTime = _JObject["expires_in"].ToString();
|
||
if (RefreshTime != null)
|
||
{
|
||
RefreshTime.Text = DateTime.Now.AddSeconds(double.Parse(LastTime)).ToString();
|
||
}
|
||
return access_token;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ErrorLogHelper.Write(ex);
|
||
string Message = "错误代码:" + _JObject["errcode"].ToString() + ",错误原因:" + _JObject["errmsg"].ToString();
|
||
return "0|" + Message;
|
||
}
|
||
sr.Close();
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 获取json数据
|
||
public static string GetJsonString(string SendType, string openid, string url,
|
||
Business.RTWECHATPUSH _RTWECHATPUSH, DataTable _DataTable, DataRow _DataRow)
|
||
{
|
||
string result = "", data = "", dataString = "";
|
||
|
||
switch (SendType)
|
||
{
|
||
case "Checkout_Success":
|
||
//首行内容
|
||
data += "\"first\": {\"value\":\"" + (string.IsNullOrEmpty(_RTWECHATPUSH.PUSH_TITLE) ?
|
||
(_RTWECHATPUSH.USER_NAME == "" ? "" : _RTWECHATPUSH.USER_NAME + ",您已下单成功!\\n") :
|
||
_RTWECHATPUSH.PUSH_TITLE + "\\n") + "\",\"color\":\"#173177\"},";
|
||
//固定内容
|
||
data += "\"keyword1\": {\"value\":\"" + _DataRow["PURCHASE_CODE"].ToString() + "\",\"color\":\"#173177\"},";
|
||
data += "\"keyword2\": {\"value\":\"" + _DataRow["PURCHASE_DATE"].ToString() + "\",\"color\":\"#173177\"},";
|
||
data += "\"keyword3\": {\"value\":\"" + _DataRow["PURCHASE_TOTALPRICE"].ToString() + "\",\"color\":\"#173177\"},";
|
||
data += "\"keyword4\": {\"value\":\"预付款\",\"color\":\"#173177\"},";
|
||
|
||
if (string.IsNullOrEmpty(_RTWECHATPUSH.PUSH_LASTCONTENT))
|
||
{
|
||
dataString = "\\n感谢您的惠顾";
|
||
}
|
||
else
|
||
{
|
||
dataString = "\\n" + _RTWECHATPUSH.PUSH_LASTCONTENT;
|
||
}
|
||
data += "\"remark\": {\"value\":\"" + dataString + "\",\"color\":\"#173177\"}";
|
||
result += "{\"touser\":\"" + openid + "\",\"template_id\":\"" + _Checkout_Success +
|
||
"\",\"url\":\"" + url + "\",\"data\":{";
|
||
break;
|
||
case "NewOrder":
|
||
//首行内容
|
||
data += "\"first\": {\"value\":\"" + (string.IsNullOrEmpty(_RTWECHATPUSH.PUSH_TITLE) ?
|
||
(_RTWECHATPUSH.USER_NAME == "" ? "" : _RTWECHATPUSH.USER_NAME + ",您的店铺有新订单产生!\\n") :
|
||
_RTWECHATPUSH.PUSH_TITLE + "\\n") + "\",\"color\":\"#173177\"},";
|
||
//固定内容
|
||
data += "\"keyword1\": {\"value\":\"" + _DataRow["SERVERPARTSHOP_NAME"].ToString() + "\",\"color\":\"#173177\"},";
|
||
data += "\"keyword2\": {\"value\":\"" + (_DataTable.Rows.Count == 1 ? _DataRow["COMMODITY_NAME"].ToString() :
|
||
_DataRow["COMMODITY_NAME"].ToString() + "等共" + _DataRow["RECORD_COUNT"].ToString() + "件商品") + "\",\"color\":\"#173177\"},";
|
||
data += "\"keyword3\": {\"value\":\"" + _DataRow["PURCHASE_DATE"].ToString() + "\",\"color\":\"#173177\"},";
|
||
data += "\"keyword4\": {\"value\":\"" + _DataRow["PURCHASE_TOTALPRICE"].ToString() + "\",\"color\":\"#173177\"},";
|
||
data += "\"keyword5\": {\"value\":\"预付款\",\"color\":\"#173177\"},";
|
||
|
||
if (string.IsNullOrEmpty(_RTWECHATPUSH.PUSH_LASTCONTENT))
|
||
{
|
||
dataString = "\\n感谢您的使用,祝您生意兴隆";
|
||
}
|
||
else
|
||
{
|
||
dataString = "\\n" + _RTWECHATPUSH.PUSH_LASTCONTENT;
|
||
}
|
||
data += "\"remark\": {\"value\":\"" + dataString + "\",\"color\":\"#173177\"}";
|
||
|
||
result += "{\"touser\":\"" + openid + "\",\"template_id\":\"" + _NewOrder +
|
||
"\",\"url\":\"" + url + "\",\"data\":{";
|
||
break;
|
||
}
|
||
|
||
return result + data + "}}";
|
||
}
|
||
|
||
public static string GetJsonString(string SendType, string openid, string url,
|
||
RTWECHATPUSH _RTWECHATPUSH, GROUPPUSHDETAIL _GROUPPUSHDETAIL, int TotalRowCount)
|
||
{
|
||
string result = "", data = "", dataString = "";
|
||
|
||
//首行内容
|
||
data += "\"first\": {\"value\":\"" + (string.IsNullOrEmpty(_RTWECHATPUSH.PUSH_TITLE) ?
|
||
(_RTWECHATPUSH.USER_NAME == "" ? "" : _RTWECHATPUSH.USER_NAME + "," + _GROUPPUSHDETAIL.PUSH_TITLE + "\\n") :
|
||
_RTWECHATPUSH.PUSH_TITLE + "\\n") + "\",\"color\":\"#fc6e4b\"},";
|
||
for (int ContentCount = 0; ContentCount < TotalRowCount; ContentCount++)
|
||
{
|
||
switch (ContentCount)
|
||
{
|
||
case 0:
|
||
//固定内容
|
||
data += "\"keyword1\": {\"value\":\"" + _GROUPPUSHDETAIL.PUSH_FIRSTCONTENT + "\",\"color\":\"#173177\"},";
|
||
break;
|
||
case 1:
|
||
data += "\"keyword2\": {\"value\":\"" + _GROUPPUSHDETAIL.PUSH_SECONDCONTENT + "\",\"color\":\"#173177\"},";
|
||
break;
|
||
case 2:
|
||
data += "\"keyword3\": {\"value\":\"" + _GROUPPUSHDETAIL.PUSH_THIRDCONTENT + "\",\"color\":\"#173177\"},";
|
||
break;
|
||
case 3:
|
||
data += "\"keyword4\": {\"value\":\"" + _GROUPPUSHDETAIL.PUSH_FOURTHCONTENT + "\",\"color\":\"#173177\"},";
|
||
break;
|
||
case 4:
|
||
data += "\"keyword5\": {\"value\":\"" + _GROUPPUSHDETAIL.PUSH_FIFTHCONTENT + "\",\"color\":\"#173177\"},";
|
||
break;
|
||
}
|
||
}
|
||
|
||
if (string.IsNullOrEmpty(_RTWECHATPUSH.PUSH_LASTCONTENT))
|
||
{
|
||
dataString = "\\n" + _GROUPPUSHDETAIL.PUSH_LASTCONTENT;
|
||
}
|
||
else
|
||
{
|
||
dataString = "\\n" + _RTWECHATPUSH.PUSH_LASTCONTENT;
|
||
}
|
||
data += "\"remark\": {\"value\":\"" + dataString + "\",\"color\":\"#fc6e4b\"}";
|
||
result += "{\"touser\":\"" + openid + "\",\"template_id\":\"" + SendType + "\",\"url\":\"" + url + "\",\"data\":{";
|
||
|
||
return result + data + "}}";
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 发送消息
|
||
public static bool StartToSend(string SendType, TextBoxEx access_token, RTWECHATPUSH _RTWECHATPUSH,
|
||
GROUPPUSHDETAIL _GROUPPUSHDETAIL, int TotalRowCount, TextBoxEx RefreshTime = null, string _AppID = "", string _AppSecret = "")
|
||
{
|
||
return StartToSend(SendType, "", access_token, _RTWECHATPUSH, _GROUPPUSHDETAIL, TotalRowCount, RefreshTime, _AppID, _AppSecret);
|
||
}
|
||
|
||
public static bool StartToSend(string SendType, string JumpUrl, TextBoxEx access_token, RTWECHATPUSH _RTWECHATPUSH,
|
||
GROUPPUSHDETAIL _GROUPPUSHDETAIL, int TotalRowCount, TextBoxEx RefreshTime = null, string _AppID = "", string _AppSecret = "")
|
||
{
|
||
bool Flag = false;
|
||
string result = "", postDataStr = "", url = "";
|
||
try
|
||
{
|
||
postDataStr = Common.GetJsonString(SendType, _RTWECHATPUSH.WECHAT_OPENID, JumpUrl, _RTWECHATPUSH,
|
||
_GROUPPUSHDETAIL, TotalRowCount);
|
||
try
|
||
{
|
||
url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token.Text;
|
||
result = Common.HttpPost(url, postDataStr);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
SuperMap.RealEstate.Utility.ErrorLogHelper.Write(ex);
|
||
}
|
||
JObject _JObject = (JObject)JsonConvert.DeserializeObject(result);
|
||
if (_JObject["errcode"].ToString() != "0")
|
||
{
|
||
if (_JObject["errcode"].ToString() == "40001")
|
||
{
|
||
access_token.Text = GetAccess(RefreshTime, _AppID, _AppSecret);
|
||
try
|
||
{
|
||
url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token.Text;
|
||
result = Common.HttpPost(url, postDataStr);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ErrorLogHelper.Write(ex);
|
||
}
|
||
_JObject = (JObject)JsonConvert.DeserializeObject(result);
|
||
if (_JObject["errcode"].ToString() == "0")
|
||
{
|
||
Flag = true;
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
Flag = true;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ErrorLogHelper.Write(ex);
|
||
}
|
||
return Flag;
|
||
}
|
||
#endregion
|
||
|
||
#region 对象 -> 通用对象
|
||
/// <summary>
|
||
/// 通用对象
|
||
/// </summary>
|
||
/// <param name="ResultCode">返回结果</param>
|
||
/// <param name="FirstParameter">参数1</param>
|
||
/// <param name="SecondParameter">参数2</param>
|
||
/// <param name="ThirdParameter">参数3</param>
|
||
/// <param name="FourthParameter">参数4</param>
|
||
/// <param name="FifthParameter">参数5</param>
|
||
/// <param name="ResultDesc">备注信息</param>
|
||
public class ResultObject
|
||
{
|
||
/// <summary>
|
||
/// 返回结果
|
||
/// </summary>
|
||
public string ResultCode
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
/// <summary>
|
||
/// 参数1
|
||
/// </summary>
|
||
public string FirstParameter
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
/// <summary>
|
||
/// 参数2
|
||
/// </summary>
|
||
public string SecondParameter
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
/// <summary>
|
||
/// 参数3
|
||
/// </summary>
|
||
public string ThirdParameter
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
/// <summary>
|
||
/// 参数4
|
||
/// </summary>
|
||
public string FourthParameter
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
/// <summary>
|
||
/// 参数5
|
||
/// </summary>
|
||
public string FifthParameter
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
/// <summary>
|
||
/// 参数6
|
||
/// </summary>
|
||
public string SixthParameter
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
/// <summary>
|
||
/// 参数7
|
||
/// </summary>
|
||
public string SeventhParameter
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
/// <summary>
|
||
/// 参数8
|
||
/// </summary>
|
||
public string EighthParameter
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 会员内码(明文)
|
||
/// </summary>
|
||
public int MemberShipID
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 省份编码
|
||
/// </summary>
|
||
public string Province_Code
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 商家权限集合
|
||
/// </summary>
|
||
public string MERCHANTS_ID
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 商家名称
|
||
/// </summary>
|
||
public string MERCHANTS_NAME
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 接口地址
|
||
/// </summary>
|
||
public string InterFaceAddress
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
/// <summary>
|
||
/// 备注
|
||
/// </summary>
|
||
public string ResultDesc
|
||
{
|
||
get;
|
||
set;
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 用户扫商家支付二维码
|
||
//用户扫商家支付二维码
|
||
//@type:wechat或alipay,通过get方式传递
|
||
//@discountAmt:优惠金额
|
||
//@memberCode:商家编号
|
||
//@notifyUrl:交易结果通知接口URL
|
||
//@orderNo:外部订单号
|
||
//@subject:交易对象
|
||
//@transAmt:实收金额
|
||
//返回JSON字符串
|
||
public static string customerscan(string type, string discountAmt, string memberCode,
|
||
string notifyUrl, string orderNo, string subject, string transAmt)
|
||
{
|
||
//Dictionary<string, string> dic = new Dictionary<string, string>();
|
||
//dic["discountAmt"] = discountAmt;
|
||
//dic["memberCode"] = memberCode;
|
||
//dic["notifyUrl"] = notifyUrl;
|
||
//dic["orderNo"] = orderNo;
|
||
//dic["subject"] = subject;
|
||
//dic["transAmt"] = transAmt;
|
||
//string param = createStr(dic);
|
||
string result = "";
|
||
//if (type == "wechat" || type == "alipay")
|
||
//{
|
||
// result = HttpPost(param, "open-api/unionpay/customerscan/type/" + type);
|
||
//}
|
||
if (type == "alipay")
|
||
{
|
||
//用户扫商家(正扫支付) 支付宝
|
||
result = InterfaceController.QRCodePayAlipay(memberCode, subject, notifyUrl, discountAmt, transAmt, orderNo).ToString();
|
||
}
|
||
else if (type == "wechat")
|
||
{
|
||
//用户扫商家(正扫支付) 微信
|
||
result = InterfaceController.QRCodePayWechat(memberCode, subject, notifyUrl, discountAmt, transAmt, orderNo).ToString();
|
||
}
|
||
return result;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 存储图片文件
|
||
public static bool UploadImg(byte[] fileBytes, string FilePath, string FileName)
|
||
{
|
||
bool flag = false;
|
||
try
|
||
{
|
||
string filePath = FilePath + FileName + ".jpg"; //图片要保存的路径及文件名
|
||
using (MemoryStream memoryStream = new MemoryStream(fileBytes))//1.定义并实例化一个内存流,以存放提交上来的字节数组。
|
||
{
|
||
using (FileStream fileUpload = new FileStream(filePath, FileMode.Create))//2.定义实际文件对象,保存上载的文件。
|
||
{
|
||
memoryStream.WriteTo(fileUpload); ///3.把内存流里的数据写入物理文件
|
||
flag = true;
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
SuperMap.RealEstate.Utility.ErrorLogHelper.Write(ex);
|
||
}
|
||
return flag;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 签名
|
||
public static string MakeSign(SortedDictionary<string, string> sParams, string key)
|
||
{
|
||
int i = 0;
|
||
string sign = string.Empty;
|
||
StringBuilder sb = new StringBuilder();
|
||
foreach (KeyValuePair<string, string> temp in sParams)
|
||
{
|
||
if (temp.Value == "" || temp.Value == null || temp.Key.ToLower() == "sign")
|
||
{
|
||
continue;
|
||
}
|
||
i++;
|
||
sb.Append(temp.Key.Trim() + "=" + temp.Value.Trim() + "&");
|
||
}
|
||
sb.Append("key=" + key.Trim() + "");
|
||
string signkey = sb.ToString();
|
||
sign = GetMD5(signkey, "utf-8");
|
||
return sign;
|
||
}
|
||
|
||
public static string MakeSign(WxPayData data, string key)
|
||
{
|
||
//签名
|
||
string str = data.ToUrl();
|
||
//在string后加入API KEY
|
||
str += "&key=" + key;
|
||
//MD5加密
|
||
var md5 = MD5.Create();
|
||
var bs = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
|
||
var sb = new StringBuilder();
|
||
foreach (byte b in bs)
|
||
{
|
||
sb.Append(b.ToString("x2"));
|
||
}
|
||
return sb.ToString().ToUpper();
|
||
}
|
||
|
||
public static string GetMD5(string encypStr, string charset)
|
||
{
|
||
string retStr;
|
||
MD5CryptoServiceProvider m5 = new MD5CryptoServiceProvider();
|
||
|
||
//创建md5对象
|
||
byte[] inputBye;
|
||
byte[] outputBye;
|
||
|
||
//使用GB2312编码方式把字符串转化为字节数组.
|
||
try
|
||
{
|
||
inputBye = Encoding.GetEncoding(charset).GetBytes(encypStr);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
inputBye = Encoding.GetEncoding("GB2312").GetBytes(encypStr);
|
||
}
|
||
outputBye = m5.ComputeHash(inputBye);
|
||
|
||
retStr = System.BitConverter.ToString(outputBye);
|
||
retStr = retStr.Replace("-", "").ToUpper();
|
||
return retStr;
|
||
}
|
||
|
||
public static string GetMD5(string str)
|
||
{
|
||
MD5 _MD5 = new MD5CryptoServiceProvider();
|
||
byte[] t = _MD5.ComputeHash(Encoding.GetEncoding("utf-8").GetBytes(str));
|
||
StringBuilder sb = new StringBuilder(32);
|
||
for (int i = 0; i < t.Length; i++)
|
||
{
|
||
sb.Append(t[i].ToString("x").PadLeft(2, '0'));
|
||
}
|
||
return sb.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将DateTime类型转换为long类型
|
||
/// </summary>
|
||
/// <param name="dt">时间</param>
|
||
/// <returns></returns>
|
||
public static long ConvertDataTimeLong(DateTime dt)
|
||
{
|
||
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
||
TimeSpan toNow = dt.Subtract(dtStart);
|
||
long timeStamp = toNow.Ticks;
|
||
timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 7));
|
||
return timeStamp;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 将Long类型转换为DateTime类型
|
||
/// </summary>
|
||
/// <param name="d">long</param>
|
||
/// <returns></returns>s
|
||
public static DateTime ConvertLongDateTime(long d)
|
||
{
|
||
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
||
long lTime = long.Parse(d + "0000000");
|
||
TimeSpan toNow = new TimeSpan(lTime);
|
||
DateTime dtResult = dtStart.Add(toNow);
|
||
return dtResult;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> HTTP报文请求
|
||
/// <summary>
|
||
/// HTTP报文请求
|
||
/// </summary>
|
||
/// <param name="str">签名</param>
|
||
/// <param name="RequestUrl">请求路径</param>
|
||
/// <param name="ContentType"></param>
|
||
/// <param name="timeout"></param>
|
||
/// <returns></returns>
|
||
public static string HttpUrlPost(string str, string RequestUrl, string ContentType = "application/x-www-form-urlencoded", int timeout = 0)
|
||
{
|
||
try
|
||
{
|
||
System.Net.CookieContainer cookieContainer = new System.Net.CookieContainer();
|
||
byte[] postBytes = System.Text.Encoding.UTF8.GetBytes(str);
|
||
// 设置提交的相关参数
|
||
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
||
System.Net.HttpWebRequest request = System.Net.WebRequest.Create(RequestUrl) as System.Net.HttpWebRequest;
|
||
if (timeout > 0)
|
||
{
|
||
request.Timeout = timeout * 1000;
|
||
}
|
||
request.Method = "POST";
|
||
request.KeepAlive = false;
|
||
request.ContentType = ContentType;
|
||
request.CookieContainer = cookieContainer;
|
||
request.ContentLength = postBytes.Length;
|
||
|
||
using (System.IO.Stream reqStream = request.GetRequestStream())
|
||
{
|
||
reqStream.Write(postBytes, 0, postBytes.Length);
|
||
}
|
||
using (System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse)
|
||
{
|
||
//在这里对接收到的页面内容进行处理
|
||
//直到request.GetResponse()程序才开始向目标网页发送post请求
|
||
System.IO.Stream responseStream = response.GetResponseStream();
|
||
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
|
||
string val = reader.ReadToEnd();
|
||
return val;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return ex.ToString();
|
||
}
|
||
}
|
||
|
||
public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
|
||
{
|
||
return true;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 获取请求地址
|
||
public static string GetPostUrl(SortedDictionary<string, string> ApiParam, string key, string type,
|
||
int timeOut, string format = "application/json;charset=UTF-8")
|
||
{
|
||
//报文创建
|
||
string MD5String = "", PostStr = "", ResString = "";
|
||
long timestamp = ConvertDataTimeLong(DateTime.Now);
|
||
PostStr = JsonConvert.SerializeObject(ApiParam);
|
||
MD5String = GetMD5(timestamp + key + PostStr) + ":" + timestamp;
|
||
//MD5String = timestamp + key + "{";
|
||
//foreach (KeyValuePair<string, string> kvp in ApiParam)
|
||
//{
|
||
// MD5String += "\"" + kvp.Key + "\":\"" + kvp.Value + "\",";
|
||
// PostStr += "\"" + kvp.Key + "\":\"" + kvp.Value + "\",";
|
||
//}
|
||
//MD5String = MD5String.TrimEnd(',') + "}";
|
||
//PostStr = "{" + PostStr.TrimEnd(',') + "}";
|
||
////创建签名
|
||
//MD5String = GetMD5(MD5String) + ":" + timestamp;
|
||
//请求报文,参数返回
|
||
ResString = HttpUrlPost(PostStr, _BaseUrl + "/" + type + "?sign=" + MD5String, format, timeOut);
|
||
return ResString;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 天气HTTP请求
|
||
public static StreamReader WeatherPostUrl(string path, string query = "", string body = "", string method = "GET")
|
||
{
|
||
String querys = query;
|
||
String bodys = body;
|
||
String url = _Weatherhost + path;
|
||
HttpWebRequest httpRequest = null;
|
||
HttpWebResponse httpResponse = null;
|
||
if (0 < querys.Length)
|
||
{
|
||
url = url + "?" + querys;
|
||
}
|
||
|
||
if (_Weatherhost.Contains("https://"))
|
||
{
|
||
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
|
||
httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));
|
||
}
|
||
else
|
||
{
|
||
httpRequest = (HttpWebRequest)WebRequest.Create(url);
|
||
}
|
||
httpRequest.Method = method;
|
||
httpRequest.Headers.Add("Authorization", "APPCODE " + _WeatherAppCode);
|
||
if (0 < bodys.Length)
|
||
{
|
||
byte[] data = Encoding.UTF8.GetBytes(bodys);
|
||
using (Stream stream = httpRequest.GetRequestStream())
|
||
{
|
||
stream.Write(data, 0, data.Length);
|
||
}
|
||
}
|
||
try
|
||
{
|
||
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
|
||
}
|
||
catch (WebException ex)
|
||
{
|
||
httpResponse = (HttpWebResponse)ex.Response;
|
||
}
|
||
Stream st = httpResponse.GetResponseStream();
|
||
StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
|
||
return reader;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> Json 字符串 转换为 DataTable数据集合
|
||
/// <summary>
|
||
/// Json 字符串 转换为 DataTable数据集合
|
||
/// </summary>
|
||
/// <param name="json"></param>
|
||
/// <returns></returns>
|
||
public static DataTable ToDataTable(string json)
|
||
{
|
||
DataTable dataTable = new DataTable(); //实例化
|
||
DataTable result;
|
||
try
|
||
{
|
||
JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
|
||
javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
|
||
if (!string.IsNullOrEmpty(json))
|
||
{
|
||
ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
|
||
if (arrayList.Count > 0)
|
||
{
|
||
foreach (Dictionary<string, object> dictionary in arrayList)
|
||
{
|
||
if (dictionary.Keys.Count == 0)
|
||
{
|
||
result = dataTable;
|
||
return result;
|
||
}
|
||
//Columns
|
||
if (dataTable.Columns.Count == 0)
|
||
{
|
||
foreach (string current in dictionary.Keys)
|
||
{
|
||
if (IsType(dictionary[current].GetType(), "System.Nullable`1[System.Int16]") ||
|
||
IsType(dictionary[current].GetType(), "System.Nullable`1[System.Int32]") ||
|
||
IsType(dictionary[current].GetType(), "System.Nullable`1[System.Int64]") ||
|
||
IsType(dictionary[current].GetType(), "System.Nullable`1[System.Double]") ||
|
||
IsType(dictionary[current].GetType(), "System.Nullable`1[System.Decimal]") ||
|
||
dictionary[current].GetType() == typeof(System.Int16) ||
|
||
dictionary[current].GetType() == typeof(System.Int32) ||
|
||
dictionary[current].GetType() == typeof(System.Int64) ||
|
||
dictionary[current].GetType() == typeof(System.Double) ||
|
||
dictionary[current].GetType() == typeof(System.Decimal))
|
||
{
|
||
dataTable.Columns.Add(current, typeof(System.Decimal));
|
||
}
|
||
else
|
||
{
|
||
dataTable.Columns.Add(current, dictionary[current].GetType());
|
||
}
|
||
}
|
||
}
|
||
//Rows
|
||
DataRow dataRow = dataTable.NewRow();
|
||
foreach (string current in dictionary.Keys)
|
||
{
|
||
try
|
||
{
|
||
dataRow[current] = dictionary[current];
|
||
}
|
||
catch { }
|
||
}
|
||
dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
|
||
}
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
SuperMap.RealEstate.Utility.ErrorLogHelper.Write(ex);
|
||
}
|
||
result = dataTable;
|
||
return result;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 生成验证码
|
||
/// <summary>
|
||
/// 生成验证码
|
||
/// <param name="n">位数</param>
|
||
/// <returns>验证码字符串</returns>
|
||
public static string CreateRandomCode(int n)
|
||
{
|
||
string[] CharArray = charSet.Split(',');
|
||
string randomCode = "";
|
||
int temp = -1;
|
||
Random rand = new Random();
|
||
for (int i = 0; i < n; i++)
|
||
{
|
||
if (temp != -1)
|
||
{
|
||
rand = new Random(i * temp * ((int)DateTime.Now.Ticks));
|
||
}
|
||
int t = rand.Next(CharArray.Length - 1);
|
||
if (temp == t)
|
||
{
|
||
return CreateRandomCode(n);
|
||
}
|
||
temp = t;
|
||
randomCode += CharArray[t];
|
||
}
|
||
return randomCode;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 获取get,post传参的值
|
||
/// <summary>
|
||
/// 获取get,post传参的值
|
||
/// </summary>
|
||
/// <param name="key">参数名</param>
|
||
/// <param name="defaultValue">参数为空时的默认值</param>
|
||
/// <returns></returns>
|
||
public static string Request(string key, string defaultValue = null)
|
||
{
|
||
|
||
var v = HttpContext.Current.Request.Form[key];
|
||
|
||
if (v == null)
|
||
{
|
||
v = HttpContext.Current.Request.QueryString[key];
|
||
}
|
||
|
||
return v ?? (defaultValue ?? string.Empty);
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> DataTable 排序
|
||
public static DataTable DataTableSort(DataTable dt, string sortBy, string tableName = null)
|
||
{
|
||
if (!string.IsNullOrEmpty(tableName))
|
||
{
|
||
dt.TableName = tableName;
|
||
}
|
||
dt.DefaultView.Sort = sortBy;
|
||
dt = dt.DefaultView.ToTable();
|
||
return dt;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 获取DataTable前几条数据
|
||
/// <summary>
|
||
/// 获取DataTable前几条数据
|
||
/// </summary>
|
||
/// <param name="TopItem">前N条数据</param>
|
||
/// <param name="oDT">源DataTable</param>
|
||
/// <returns></returns>
|
||
public static DataTable DtSelectTop(int TopItem, DataTable oDT)
|
||
{
|
||
if (oDT.Rows.Count < TopItem) return oDT;
|
||
|
||
DataTable NewTable = oDT.Clone();
|
||
DataRow[] rows = oDT.Select("1=1");
|
||
for (int i = 0; i < TopItem; i++)
|
||
{
|
||
NewTable.ImportRow((DataRow)rows[i]);
|
||
}
|
||
return NewTable;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 创建md5 16位大写加密
|
||
public static string CreateMD5(string text)
|
||
{
|
||
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
|
||
//换成utf8后对于中文加密也适用
|
||
byte[] output = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(text));
|
||
string pass2md5 = BitConverter.ToString(output, 4, 8).Replace("-", "");
|
||
//pass2md5 = pass2md5.ToUpper();
|
||
return pass2md5;
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 类型匹配
|
||
/// <summary>
|
||
/// 类型匹配
|
||
/// </summary>
|
||
/// <param name="type">类型</param>
|
||
/// <param name="typeName">类型名称</param>
|
||
/// <returns></returns>
|
||
public static bool IsType(Type type, string typeName)
|
||
{
|
||
if (type.ToString() == typeName)
|
||
return true;
|
||
if (type.ToString() == "System.Object")
|
||
return false;
|
||
return IsType(type.BaseType, typeName);
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> 订单状态
|
||
/// <summary>
|
||
/// 订单状态
|
||
/// </summary>
|
||
/// <param name="SaleBill_State">状态值</param>
|
||
/// <returns></returns>
|
||
public static string TransferBillState(string SaleBill_State)
|
||
{
|
||
switch (SaleBill_State)
|
||
{
|
||
case "1000":
|
||
return "订单申请";
|
||
case "1005":
|
||
return "待付款";
|
||
case "1010":
|
||
return "已付款";
|
||
case "2000":
|
||
return "制作中";
|
||
case "3000":
|
||
return "已完成";
|
||
case "9000":
|
||
return "订单关闭";
|
||
case "9999":
|
||
return "已退款";
|
||
default:
|
||
return "";
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
public static decimal Round(decimal d, int decimals)
|
||
{
|
||
decimal tenPow = Convert.ToDecimal(Math.Pow(10, decimals));
|
||
decimal scrD = d * tenPow + 0.5m;
|
||
return (Convert.ToDecimal(Math.Floor(Convert.ToDouble(scrD))) / tenPow);
|
||
}
|
||
}
|
||
} |