Пример #1
0
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.weixin_access_token GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder str1   = new StringBuilder();

            Model.weixin_access_token model = new Model.weixin_access_token();
            //利用反射获得属性的所有公共属性
            PropertyInfo[] pros = model.GetType().GetProperties();
            foreach (PropertyInfo p in pros)
            {
                str1.Append(p.Name + ",");//拼接字段
            }
            strSql.Append("select top 1 " + str1.ToString().Trim(','));
            strSql.Append(" from " + databaseprefix + "weixin_access_token");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters =
            {
                new SqlParameter("@id", SqlDbType.Int, 4)
            };
            parameters[0].Value = id;
            DataTable dt = DbHelperSQL.Query(strSql.ToString(), parameters).Tables[0];

            if (dt.Rows.Count > 0)
            {
                return(DataRowToModel(dt.Rows[0]));
            }
            else
            {
                return(null);
            }
        }
Пример #2
0
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.weixin_access_token model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder str1   = new StringBuilder();

            //利用反射获得属性的所有公共属性
            PropertyInfo[]      pros  = model.GetType().GetProperties();
            List <SqlParameter> paras = new List <SqlParameter>();

            strSql.Append("update " + databaseprefix + "weixin_access_token set ");
            foreach (PropertyInfo pi in pros)
            {
                //如果不是主键则追加sql字符串
                if (!pi.Name.Equals("id"))
                {
                    //判断属性值是否为空
                    if (pi.GetValue(model, null) != null)
                    {
                        str1.Append(pi.Name + "=@" + pi.Name + ",");                          //声明参数
                        paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值
                    }
                }
            }
            strSql.Append(str1.ToString().Trim(','));
            strSql.Append(" where id=@id ");
            paras.Add(new SqlParameter("@id", model.id));
            return(DbHelperSQL.ExecuteSql(strSql.ToString(), paras.ToArray()) > 0);
        }
Пример #3
0
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.weixin_access_token model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into " + databaseprefix + "weixin_access_token(");
            strSql.Append("account_id,access_token,expires_in,count,add_time)");
            strSql.Append(" values (");
            strSql.Append("@account_id,@access_token,@expires_in,@count,@add_time)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters =
            {
                new SqlParameter("@account_id",   SqlDbType.Int,         4),
                new SqlParameter("@access_token", SqlDbType.NVarChar, 1000),
                new SqlParameter("@expires_in",   SqlDbType.Int,         4),
                new SqlParameter("@count",        SqlDbType.Int,         4),
                new SqlParameter("@add_time",     SqlDbType.DateTime)
            };
            parameters[0].Value = model.account_id;
            parameters[1].Value = model.access_token;
            parameters[2].Value = model.expires_in;
            parameters[3].Value = model.count;
            parameters[4].Value = model.add_time;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);

            if (obj == null)
            {
                return(0);
            }
            else
            {
                return(Convert.ToInt32(obj));
            }
        }
Пример #4
0
 /// <summary>
 /// 得到一个对象实体
 /// </summary>
 public Model.weixin_access_token DataRowToModel(DataRow row)
 {
     Model.weixin_access_token model = new Model.weixin_access_token();
     if (row != null)
     {
         if (row["id"] != null && row["id"].ToString() != "")
         {
             model.id = int.Parse(row["id"].ToString());
         }
         if (row["account_id"] != null && row["account_id"].ToString() != "")
         {
             model.account_id = int.Parse(row["account_id"].ToString());
         }
         if (row["access_token"] != null)
         {
             model.access_token = row["access_token"].ToString();
         }
         if (row["expires_in"] != null && row["expires_in"].ToString() != "")
         {
             model.expires_in = int.Parse(row["expires_in"].ToString());
         }
         if (row["count"] != null && row["count"].ToString() != "")
         {
             model.count = int.Parse(row["count"].ToString());
         }
         if (row["add_time"] != null && row["add_time"].ToString() != "")
         {
             model.add_time = DateTime.Parse(row["add_time"].ToString());
         }
     }
     return(model);
 }
Пример #5
0
 /// <summary>
 /// �õ�һ������ʵ��
 /// </summary>
 public Model.weixin_access_token DataRowToModel(DataRow row)
 {
     Model.weixin_access_token model = new Model.weixin_access_token();
     if (row != null)
     {
         if (row["id"] != null && row["id"].ToString() != "")
         {
             model.id = int.Parse(row["id"].ToString());
         }
         if (row["account_id"] != null && row["account_id"].ToString() != "")
         {
             model.account_id = int.Parse(row["account_id"].ToString());
         }
         if (row["access_token"] != null)
         {
             model.access_token = row["access_token"].ToString();
         }
         if (row["expires_in"] != null && row["expires_in"].ToString() != "")
         {
             model.expires_in = int.Parse(row["expires_in"].ToString());
         }
         if (row["count"] != null && row["count"].ToString() != "")
         {
             model.count = int.Parse(row["count"].ToString());
         }
         if (row["add_time"] != null && row["add_time"].ToString() != "")
         {
             model.add_time = DateTime.Parse(row["add_time"].ToString());
         }
     }
     return model;
 }
Пример #6
0
 /// <summary>
 /// ��������һ������
 /// </summary>
 public int Add(string access_token)
 {
     Model.weixin_access_token model = new Model.weixin_access_token();
     model.access_token = access_token;
     model.count = 1;
     model.expires_in = 1200; //1200��
     model.add_time = DateTime.Now;
     return dal.Add(model);
 }
Пример #7
0
 /// <summary>
 /// 快速增加一条数据
 /// </summary>
 public int Add(string access_token)
 {
     Model.weixin_access_token model = new Model.weixin_access_token();
     model.access_token = access_token;
     model.count        = 1;
     model.expires_in   = 1200; //1200秒
     model.add_time     = DateTime.Now;
     return(dal.Add(model));
 }
Пример #8
0
        BLL.weixin_account accountBLL = new BLL.weixin_account();         //公众平台账户

        /// <summary>
        /// 及时获得access_token值
        /// access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒,
        /// 重复获取将导致上次获取的access_token失效。
        /// 每日限额获取access_token.我们将access_token保存到数据库里,间隔时间为20分钟,从微信公众平台获得一次。
        /// </summary>
        public string GetAccessToken(int accountId, out string error)
        {
            string access_token = string.Empty;

            error = string.Empty;
            try
            {
                Model.weixin_account accountModel = accountBLL.GetModel(accountId); //公众平台账户信息
                if (string.IsNullOrEmpty(accountModel.appid) || string.IsNullOrEmpty(accountModel.appsecret))
                {
                    error = "AppId或者AppSecret未填写,请在补全信息!";
                    return(string.Empty);
                }
                //没有找到该账户则获取AccessToKen写入存储1200秒
                if (!tokenBLL.ExistsAccount(accountId))
                {
                    var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(accountModel.appid, accountModel.appsecret);
                    access_token = result.access_token;
                    tokenBLL.Add(accountId, access_token);
                    return(access_token);
                }
                //获取公众账户的实体
                Model.weixin_access_token tokenModel = tokenBLL.GetAccountModel(accountId);
                //计算时间判断是否过期
                TimeSpan ts           = DateTime.Now - tokenModel.add_time;
                double   chajunSecond = ts.TotalSeconds;
                if (string.IsNullOrEmpty(tokenModel.access_token) || chajunSecond >= tokenModel.expires_in)
                {
                    //从微信平台重新获得AccessToken
                    var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(accountModel.appid, accountModel.appsecret);
                    access_token = result.access_token;
                    //更新到数据库里的AccessToken
                    tokenModel.access_token = access_token;
                    tokenModel.add_time     = DateTime.Now;
                    tokenBLL.Update(tokenModel);
                }
                else
                {
                    access_token = tokenModel.access_token;
                }
            }
            catch (Exception ex)
            {
                error = "获取AccessToken出错:" + ex.Message;
            }
            return(access_token);
        }
Пример #9
0
 /// <summary>
 /// 将对象转换实体
 /// </summary>
 public Model.weixin_access_token DataRowToModel(DataRow row)
 {
     Model.weixin_access_token model = new Model.weixin_access_token();
     if (row != null)
     {
         //利用反射获得属性的所有公共属性
         Type modelType = model.GetType();
         for (int i = 0; i < row.Table.Columns.Count; i++)
         {
             //查找实体是否存在列表相同的公共属性
             PropertyInfo proInfo = modelType.GetProperty(row.Table.Columns[i].ColumnName);
             if (proInfo != null && row[i] != DBNull.Value)
             {
                 proInfo.SetValue(model, row[i], null);//用索引值设置属性值
             }
         }
     }
     return(model);
 }
Пример #10
0
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.weixin_access_token model)
        {
            int newId;

            using (OleDbConnection conn = new OleDbConnection(DbHelperOleDb.connectionString))
            {
                conn.Open();
                using (OleDbTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into " + databaseprefix + "weixin_access_token(");
                        strSql.Append("account_id,access_token,expires_in,[count],add_time)");
                        strSql.Append(" values (");
                        strSql.Append("@account_id,@access_token,@expires_in,@count,@add_time)");
                        OleDbParameter[] parameters =
                        {
                            new OleDbParameter("@account_id",   OleDbType.Integer,    4),
                            new OleDbParameter("@access_token", OleDbType.VarChar, 1000),
                            new OleDbParameter("@expires_in",   OleDbType.Integer,    4),
                            new OleDbParameter("@count",        OleDbType.Integer,    4),
                            new OleDbParameter("@add_time",     OleDbType.Date)
                        };
                        parameters[0].Value = model.account_id;
                        parameters[1].Value = model.access_token;
                        parameters[2].Value = model.expires_in;
                        parameters[3].Value = model.count;
                        parameters[4].Value = model.add_time;
                        DbHelperOleDb.ExecuteSql(conn, trans, strSql.ToString(), parameters);
                        //取得新插入的ID
                        newId = GetMaxId(conn, trans);
                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return(-1);
                    }
                }
            }
            return(newId);
        }
Пример #11
0
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.weixin_access_token model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder str1   = new StringBuilder(); //数据字段
            StringBuilder str2   = new StringBuilder(); //数据参数

            //利用反射获得属性的所有公共属性
            PropertyInfo[]      pros  = model.GetType().GetProperties();
            List <SqlParameter> paras = new List <SqlParameter>();

            strSql.Append("insert into " + databaseprefix + "weixin_access_token(");
            foreach (PropertyInfo pi in pros)
            {
                //如果不是主键则追加sql字符串
                if (!pi.Name.Equals("id"))
                {
                    //判断属性值是否为空
                    if (pi.GetValue(model, null) != null)
                    {
                        str1.Append(pi.Name + ",");                                           //拼接字段
                        str2.Append("@" + pi.Name + ",");                                     //声明参数
                        paras.Add(new SqlParameter("@" + pi.Name, pi.GetValue(model, null))); //对参数赋值
                    }
                }
            }
            strSql.Append(str1.ToString().Trim(','));
            strSql.Append(") values (");
            strSql.Append(str2.ToString().Trim(','));
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY;");
            object obj = DbHelperSQL.GetSingle(strSql.ToString(), paras.ToArray());

            if (obj == null)
            {
                return(0);
            }
            else
            {
                return(Convert.ToInt32(obj));
            }
        }
Пример #12
0
        /// <summary>
        ///【强制刷新】access_token值
        /// access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token。正常情况下access_token有效期为7200秒,
        /// 重复获取将导致上次获取的access_token失效。
        /// 每日限额获取access_token.我们将access_token保存到数据库里,间隔时间为20分钟,从微信公众平台获得一次。
        /// </summary>
        /// <returns></returns>
        public string FlushAccessToken(int accountId, out string error)
        {
            string access_token = string.Empty;

            error = string.Empty;
            try
            {
                Model.weixin_account accountModel = accountBLL.GetModel(accountId); //公众平台账户信息
                if (string.IsNullOrEmpty(accountModel.appid) || string.IsNullOrEmpty(accountModel.appsecret))
                {
                    error = "AppId或者AppSecret未填写,请在补全信息!";
                    return("");
                }

                var result = Senparc.Weixin.MP.CommonAPIs.CommonApi.GetToken(accountModel.appid, accountModel.appsecret);
                access_token = result.access_token;

                //没有找到该账户则获取AccessToKen写入存储1200秒
                if (!tokenBLL.ExistsAccount(accountId))
                {
                    tokenBLL.Add(accountId, access_token);
                }
                else
                {
                    //获取公众账户的实体
                    Model.weixin_access_token tokenModel = tokenBLL.GetAccountModel(accountId);
                    //更新到数据库里的AccessToken
                    tokenModel.access_token = access_token;
                    tokenModel.add_time     = DateTime.Now;
                    tokenBLL.Update(tokenModel);
                }
            }
            catch (Exception ex)
            {
                error = "获得AccessToken出错:" + ex.Message;
            }
            return(access_token);
        }
Пример #13
0
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.weixin_access_token model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update " + databaseprefix + "weixin_access_token set ");
            strSql.Append("account_id=@account_id,");
            strSql.Append("access_token=@access_token,");
            strSql.Append("expires_in=@expires_in,");
            strSql.Append("count=@count,");
            strSql.Append("add_time=@add_time");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters =
            {
                new SqlParameter("@account_id",   SqlDbType.Int,          4),
                new SqlParameter("@access_token", SqlDbType.NVarChar,  1000),
                new SqlParameter("@expires_in",   SqlDbType.Int,          4),
                new SqlParameter("@count",        SqlDbType.Int,          4),
                new SqlParameter("@add_time",     SqlDbType.DateTime),
                new SqlParameter("@id",           SqlDbType.Int, 4)
            };
            parameters[0].Value = model.account_id;
            parameters[1].Value = model.access_token;
            parameters[2].Value = model.expires_in;
            parameters[3].Value = model.count;
            parameters[4].Value = model.add_time;
            parameters[5].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

            if (rows > 0)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Пример #14
0
 /// <summary>
 /// 更新一条数据
 /// </summary>
 public bool Update(Model.weixin_access_token model)
 {
     return(dal.Update(model));
 }
Пример #15
0
 /// <summary>
 /// 增加一条数据
 /// </summary>
 public int Add(Model.weixin_access_token model)
 {
     return(dal.Add(model));
 }