You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using DSWeb.MvcShipping.DAL.Chfee_AuditDAL;
using DSWeb.Areas.Account.Models.MsOpBill;
using System.Text;
using System.Configuration;
using DSWeb.Areas.Mobile.Models.Login;
using DSWeb.TruckMng.Helper;
namespace DSWeb.Areas.Mobile.DAL
{
public class AccessTokenDAL
{
//获取AccessToken
public static string GetAccessToken(){
string sql = "select top 1 AccessToken from sys_WeChatConfig order by updatetime desc";
Database db = DatabaseFactory.CreateDatabase();
DBLog.Log("GetAccessToken","sql",sql);
var token = db.ExecuteScalar(CommandType.Text, sql) ;
if (token==null||token==DBNull.Value || token.ToString()=="")
{
DBLog.Log("RefrashAccessToken","before");
token = RefrashAccessToken();
DBLog.Log("RefrashAccessToken", "end", token.ToString());
}
DBLog.Log("GetAccessToken","end");
return token.ToString();
}
//设置AccessToken
public static bool SetAccessToken (string token)
{
string sqlins = "insert into sys_WeChatConfig (accesstoken) values('" + token + "')";
Database db = DatabaseFactory.CreateDatabase();
int rst = db.ExecuteNonQuery(CommandType.Text,sqlins);
return rst>0;
}
//刷新AccessToken
public static string RefrashAccessToken ( ) {
string appid = ConfigurationManager.AppSettings["appid"].ToString();
string secret = ConfigurationManager.AppSettings["secret"].ToString();
string url = string.Format(@"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}",appid,secret);
string respStr = HttpHelper.HttpGet(url);
DBLog.Log("RefrashAccessToken", "respStr", respStr);
AccessTokenModel token = JsonConvert.Deserialize<AccessTokenModel>(respStr);
Database db = DatabaseFactory.CreateDatabase();
DBLog.Log("RefrashAccessToken","token",token.access_token);
string sqlins = "insert into sys_WeChatConfig (accesstoken) values('"+token.access_token+"')";
int rst = db.ExecuteNonQuery(CommandType.Text,sqlins);
if (rst>0)
{
return token.access_token;
}
else
{
return "";
}
}
}
}