示例#1
0
        public void SendSeatOutdateMessage(int roomNum, string roomName, int time, DateTime now)
        {
            string[]       sql = { "select t1.no,t1.seat_no,t1.time_end,t2.wechat from tb_seat_student as t1,tb_student as t2 where t1.room=@room and t1.time_end<@nw and t1.isoutdate=0 and t1.issend_" + time + "_message=0 and t1.no=t2.no",
                                   "update tb_seat_student set issend_" + time + "_message=1 where room=@room and time_end<@nw and isoutdate=0 and issend_" + time + "_message=0" };
            SqlParameter[] sp1 =
            {
                new SqlParameter("@room", roomNum),
                new SqlParameter("@nw",   now.AddMinutes(time))
            };
            DataTable   dt = SqlServerHelper.ExecuteDataTable(sql[0], sp1);
            SendMessage sd = new SendMessage();

            //发送消息
            foreach (DataRow item in dt.Rows)
            {
                sd.SendSeatOutDateMessage(item[0].ToString(), MyConvert.toString(item[3]), roomName, seat_no2tableseat(MyConvert.toInt(item[1])), time.ToString(), MyConvert.toString(item[2]));
            }
            //发送消息结束。
            SqlParameter[] sp2 =
            {
                new SqlParameter("@room", roomNum),
                new SqlParameter("@nw",   now.AddMinutes(time))
            };
            SqlServerHelper.ExecuteNonQuery(sql[1], sp2);
        }
 //获取操作微信公众平台需要的access_token信息。测试阶段,不必考虑安全性问题,直接在本地卡机上获取token即可。
 //等正式运行时在服务端获取token,然后存在数据库中。避免出现过多调用access_token而引起的次数超过微信限制或者其他安全问题。
 public string GetAccessToken()
 {
     if (token == null || DateTime.Now - gettime > TimeSpan.FromMinutes(110))
     {
         WebClient client = new WebClient();
         string    str    = client.DownloadString(new Uri("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx857e6ba0e902375f&secret=b0cf61ae49413c6235b2a9715fa4bc74"));
         JObject   res    = JObject.Parse(str);
         token   = MyConvert.toString(res["access_token"]);
         gettime = DateTime.Now;
     }
     return(token);
 }
        //获取操作微信公众平台需要的access_token信息。测试阶段,不必考虑安全性问题,直接在本地卡机上获取token即可。
        //等正式运行时在服务端获取token,然后存在数据库中。避免出现过多调用access_token而引起的次数超过微信限制或者其他安全问题。

        /// <summary>
        ///在Redis服务器中,AccessToken的有效期设置为6000秒,而微信服务器获取到的AccessToken有效期为7200秒,
        ///这样可以有效避免正在发送消息呢,AccessToken过期导致的发送消息失败,尝试从redis数据库中获取微信AccessToken,
        ///如果获取到的值为null,那么本地请求微信服务器获取一个AccessToken,然后更新Redis中的AccessToken.
        /// </summary>
        public SendMessage()
        {
            RedisHelper redis = new RedisHelper();

            token = redis.GetString("accessToken");
            if (token == null)
            {
                WebClient client = new WebClient();
                string    str    = client.DownloadString(new Uri("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx857e6ba0e902375f&secret=b0cf61ae49413c6235b2a9715fa4bc74"));
                JObject   res    = JObject.Parse(str);
                token = MyConvert.toString(res["access_token"]);
                redis.SetString("accessToken", token, 6000);
            }
        }
        public string FindOpenId(string str)
        {
            string sql = "select wechat from tb_student where no='" + str + "'";

            return(MyConvert.toString(SqlServerHelper.ExecuteSclar(sql)));
        }
        public string FindNo(string str)
        {
            string sql = "select no from tb_student where card='" + str + "'";

            return(MyConvert.toString(SqlServerHelper.ExecuteSclar(sql)));
        }
        public string FindRoomName(int roomNum)
        {
            string sql = "select name from tb_room where no=" + roomNum;

            return(MyConvert.toString(SqlServerHelper.ExecuteSclar(sql)));
        }