#region 公众号系统参数/// <summary>/// 更新系统配置文件/// </summary>internal void updateConfig()
{
ClassData data = new ClassData();
DataSet ds = data.RunProcReturn("SELECT appid,appsecret FROM config WHERE id=1");
int ln = ds.Tables[0].Rows.Count;
if (ln == 1)
{
string appID = ds.Tables[0].Rows[0][0].ToString();
string appSecret = ds.Tables[0].Rows[0][1].ToString();
string url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", appID, appSecret);
string htmlstr = GetWebInfo(url);
string access_token = "";
try
{
var DynamicObject = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(htmlstr);
access_token = DynamicObject.access_token;
}
catch
{
}
if (access_token != null && access_token.Trim() != "")
{
url = string.Format("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={0}&type=jsapi", access_token);
htmlstr = GetWebInfo(url);
string jsapi_ticket = "";
try
{
var DynamicObject2 = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(htmlstr);
jsapi_ticket = DynamicObject2.ticket;
}
catch
{
}
string sql = "UPDATE config SET access_token=@access_token,tokentime=GETDATE() ";
if (jsapi_ticket != null && jsapi_ticket.Trim() != "")
{
sql += ",jsapi_ticket=@jsapi_ticket,tickettime=GETDATE() ";
}
sql += " WHERE id=1";
SqlParameter[] prams = {
data.MakeInParam("@access_token",SqlDbType.VarChar ,255, access_token),
data.MakeInParam("@jsapi_ticket",SqlDbType.VarChar ,255,jsapi_ticket==null?"": jsapi_ticket)
};
ln = data.RunProc(sql, prams);
}
}
}
/// <summary>/// 获取网页结果/// </summary>/// <param name="url"></param>/// <returns></returns>internal string GetWebInfo(string url)
{
string htmlstr = "";
try
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("utf-8");
System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), encoding);
htmlstr = reader.ReadToEnd();
reader.Dispose();
reader.Close();
response.Close();
}
catch (Exception ex)
{
htmlstr = ex.Message;
}
return htmlstr;
}
/// <summary>/// post提交网页信息/// </summary>/// <param name="url"></param>/// <param name="postData"></param>/// <returns></returns>internal string Post(string url, string postData)
{
string htmlstr = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("");
byte[] data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
var response = (HttpWebResponse)request.GetResponse();
htmlstr = new System.IO.StreamReader(response.GetResponseStream()).ReadToEnd();
return htmlstr;
}
/// <summary>/// 获取微信公众号access_token/// </summary>/// <param name="msg"></param>/// <param name="access_token"></param>/// <returns></returns>public bool UserWxAccessTokenGet(out string msg, out string access_token)
{
bool mk = false;
msg = "";
access_token = "";
ClassData data = new ClassData();
try
{
bool h = false;
while (!h)
{
string sql = "SELECT tokentime,access_token FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
if (ds.Tables[0].Rows[0][0] == null || ds.Tables[0].Rows[0][0] == DBNull.Value)
{
updateConfig();
}
else
{
if (Convert.ToDateTime(ds.Tables[0].Rows[0][0]).AddMinutes(115) <= DateTime.Now)
{
updateConfig();
}
else
{
access_token = ds.Tables[0].Rows[0][1].ToString();
if (op.checkNull(access_token))
{
updateConfig();
}
else
{
h = true;
mk = true;
}
}
}
}
else
{
msg = "配置不存在!";
break;
}
}
}
catch (Exception ex)
{
msg = ex.Message; op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return mk;
}
/// <summary>/// 获取微信公众号APPid/// </summary>/// <returns></returns>public string APPid()
{
try
{
ClassData data = new ClassData();
string sql = "SELECT appid FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return ds.Tables[0].Rows[0][0].ToString();
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return "";
}
/// <summary>/// 微信小程序APPid/// </summary>/// <returns></returns>public string WxAppId()
{
try
{
ClassData data = new ClassData();
string sql = "SELECT wxappid FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return ds.Tables[0].Rows[0][0].ToString();
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return "";
}
/// <summary>/// 微信小程序App Secre/// </summary>/// <returns></returns>public string WxAppSecret()
{
try
{
ClassData data = new ClassData();
string sql = "SELECT wxappsecret FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return ds.Tables[0].Rows[0][0].ToString();
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return "";
}
/// <summary>/// 百度小程序APPid/// </summary>/// <returns></returns>public string BdAppId()
{
try
{
ClassData data = new ClassData();
string sql = "SELECT bdappid FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return ds.Tables[0].Rows[0][0].ToString();
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return "";
}
/// <summary>/// 百度小程序App Key/// </summary>/// <returns></returns>public string BdAppKey()
{
try
{
ClassData data = new ClassData();
string sql = "SELECT bdappkey FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return ds.Tables[0].Rows[0][0].ToString();
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return "";
}
/// <summary>/// 百度App Secret (智能小程序密匙):/// </summary>/// <returns></returns>public string BdAppSecret()
{
try
{
ClassData data = new ClassData();
string sql = "SELECT bdappsecret FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return ds.Tables[0].Rows[0][0].ToString();
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return "";
}
/// <summary>/// 百度支付dealId (dealId):/// </summary>/// <returns></returns>public string bdpaydealId()
{
try
{
ClassData data = new ClassData();
string sql = "SELECT bdpaydealId FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return ds.Tables[0].Rows[0][0].ToString();
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return "";
}
/// <summary>/// 百度支付appKey (bdpayappKey):/// </summary>/// <returns></returns>public string bdpayappKey()
{
try
{
ClassData data = new ClassData();
string sql = "SELECT bdpayappKey FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return ds.Tables[0].Rows[0][0].ToString();
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return "";
}
/// <summary>/// 百度支付公钥 (publickey):/// </summary>/// <returns></returns>public string bdpaypublickey()
{
try
{
ClassData data = new ClassData();
string sql = "SELECT bdpaypublickey FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return ds.Tables[0].Rows[0][0].ToString();
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return "";
}
/// <summary>/// 百度支付私钥 (rsa_private_key):/// </summary>/// <returns></returns> public string bd_rsa_private_key()
{
try
{
ClassData data = new ClassData();
string sql = "SELECT rsa_private_key FROM [config] WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return ds.Tables[0].Rows[0][0].ToString();
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return "";
}
/// <summary>/// 获取微信运费/// </summary>/// <returns></returns>public decimal Freight()
{
decimal appid = 12;
try
{
ClassData data = new ClassData();
string sql = "SELECT freight FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return Convert.ToDecimal(ds.Tables[0].Rows[0][0]);
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return appid;
}
/// <summary>/// 获取免运费金额/// </summary>/// <returns></returns>public decimal FreeFreight()
{
decimal appid = 10000;
try
{
ClassData data = new ClassData();
string sql = "SELECT freefreight FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return Convert.ToDecimal(ds.Tables[0].Rows[0][0]);
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return appid;
}
/// <summary>/// 获取微信公众号APPid/// </summary>/// <returns></returns>public string APPSECRET()
{
string APPSECRET = "";
try
{
ClassData data = new ClassData();
string sql = "SELECT APPSECRET FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
return ds.Tables[0].Rows[0][0].ToString();
}
}
catch (Exception ex)
{
op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return APPSECRET;
}
/// <summary>/// 获取微信公众号jsapi_ticket/// </summary>/// <param name="msg"></param>/// <param name="jsapi_ticket"></param>/// <returns></returns>public bool UserWxJsapiTicketGet(out string msg, out string jsapi_ticket)
{
bool mk = false;
msg = "";
jsapi_ticket = "";
try
{
ClassData data = new ClassData();
bool h = false;
while (!h)
{
string sql = "SELECT tickettime,jsapi_ticket FROM config WHERE id=1";
DataSet ds = data.RunProcReturn(sql);
if (ds.Tables[0].Rows.Count == 1)
{
if (ds.Tables[0].Rows[0][0] == null || ds.Tables[0].Rows[0][0] == DBNull.Value)
{
updateConfig();
}
else
{
if (Convert.ToDateTime(ds.Tables[0].Rows[0][0]).AddMinutes(115) <= DateTime.Now)
{
updateConfig();
}
else
{
jsapi_ticket = ds.Tables[0].Rows[0][1].ToString();
if (op.checkNull(jsapi_ticket))
{
updateConfig();
}
else
{
h = true;
mk = true;
}
}
}
}
else
{
msg = "配置不存在!";
break;
}
}
}
catch (Exception ex)
{
msg = ex.Message; op.errorMail(ex.Message + "错误原因:" + ex.StackTrace);
}
return mk;
}
#endregion
C#开发小程序后台常用后台代码参考一
.NET
313
0
0
2022-04-01
标签
C#