Ejemplo n.º 1
0
        /// <summary>
        /// 调用租户接口推送数据
        /// </summary>
        /// <param name="url">租户接口url</param>
        /// <param name="type">修改值</param>
        /// <param name="value">修改类型</param>
        /// <param name="msg">文字说明</param>
        /// <param name="appId">应用接入id</param>
        /// <param name="appSecret">密钥</param>
        /// <returns></returns>
        public string PostDataUrl(string url, int type, int value, string msg, string appSecret, string studentNo, string projectCode, int classId)
        {
            string result     = string.Empty;
            string postString = string.Empty;


            try
            {
                string timeStamp = GetTimeStamp().ToString();                 //时间戳
                string nonce     = new Random().NextDouble().ToString();      //随机数
                string signature = GetSignature(appSecret, timeStamp, nonce); //签名加密字符串


                System.Net.WebClient WebClientObj = new System.Net.WebClient();
                WebClientObj.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                NameValueCollection PostVars = new NameValueCollection();
                postString = "type=" + HttpUtility.UrlEncode(type.ToString(), UTF8Encoding.UTF8) + "&value=" + HttpUtility.UrlEncode(value.ToString(), UTF8Encoding.UTF8) + "&message=" + HttpUtility.UrlEncode(msg, UTF8Encoding.UTF8) + "&signature=" + HttpUtility.UrlEncode(signature, UTF8Encoding.UTF8) + "&timestamp=" + HttpUtility.UrlEncode(timeStamp, UTF8Encoding.UTF8) + "&nonce=" + HttpUtility.UrlEncode(nonce, UTF8Encoding.UTF8) + "&studentno=" + HttpUtility.UrlEncode(studentNo, UTF8Encoding.UTF8) + "&projectcode=" + HttpUtility.UrlEncode(projectCode, UTF8Encoding.UTF8) + "&classsid=" + HttpUtility.UrlEncode(classId.ToString(), UTF8Encoding.UTF8);
                byte[] postData     = Encoding.UTF8.GetBytes(postString);
                byte[] responseData = WebClientObj.UploadData(url, "POST", postData);
                result = Encoding.UTF8.GetString(responseData);

                CommLog.WriteSystemLog("推送结果result=" + result + ";posturl=" + url + "?" + postString);
            }
            catch (Exception ex)
            {
                CommLog.WriteExceptionLog("调用租户接口推送数据时出现异常:" + ex.Message + ";posturl=" + url + "?" + postString);
                result = string.Empty;
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 推送成功或失败后,更新NetInterface记录状态
        /// </summary>
        /// <param name="type">1发送成功 0发送失败</param>
        /// <param name="id">记录id</param>
        /// <param name="sendTimes">发送次数</param>
        public void UpdateSendStatus(int type, long id, int sendTimes)
        {
            string sqlStr = string.Empty;

            try
            {
                if (type == 1)
                {
                    //发送成功
                    if (sendTimes == 0)
                    {
                        //首次发送
                        sqlStr = string.Format("update dbo.NetInterface set [Status]=1,SendTime='{0}',SendTimes=1 where Id={1}", DateTime.Now.ToString(), id);
                    }
                    else
                    {
                        //重复发送
                        sqlStr = string.Format("update dbo.NetInterface set [Status]=1,UpdateTime='{0}',SendTimes=SendTimes+1 where Id={1}", DateTime.Now.ToString(), id);
                    }
                }
                else
                {
                    //发送失败
                    if (sendTimes == 0)
                    {
                        //发送次数加1 修改发送时间
                        sqlStr = string.Format("update dbo.NetInterface set SendTime='{0}',SendTimes=1 where Id={1}", DateTime.Now.ToString(), id);
                    }
                    else
                    {
                        if (sendTimes < 2)
                        {
                            //发送次数加1 修改更新时间
                            sqlStr = string.Format("update dbo.NetInterface set UpdateTime='{0}',SendTimes=SendTimes+1 where Id={1}", DateTime.Now.ToString(), id);
                        }
                        else
                        {
                            //发送次数加1,修改更新时间,将状态置为2
                            sqlStr = string.Format("update dbo.NetInterface set [Status]=2,UpdateTime='{0}',SendTimes=SendTimes+1 where Id={1}", DateTime.Now.ToString(), id);
                        }
                    }
                }

                SqlHelper.ExecuteNonQuery(SqlHelper.connString, CommandType.Text, sqlStr);
            }
            catch (Exception ex)
            {
                CommLog.WriteExceptionLog("推送成功或失败后,更新NetInterface记录状态时出现异常:" + ex.Message + ";  sql=" + sqlStr);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 根据租户id获取租户的回调所需的信息
        /// </summary>
        /// <param name="tenantId"></param>
        /// <returns></returns>
        public DataTable GetCallbackParams(int tenantId)
        {
            DataTable dt     = null;
            string    sqlStr = string.Empty;

            try
            {
                sqlStr = string.Format("select top 1 TokenUrl,AesKey from dbo.NetTenant with(nolock) where IsDelete=0 and Id={0} Order by Id desc", tenantId);
                dt     = SqlHelper.ExecuteTable(SqlHelper.connString, sqlStr);
            }
            catch (Exception ex)
            {
                CommLog.WriteExceptionLog("根据租户id获取租户的回调所需的信息时出现异常:" + ex.Message + "; sql=" + sqlStr);
                dt = null;
            }
            return(dt);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 获取需要回调的前100条记录
        /// </summary>
        /// <returns></returns>
        public DataTable GetCallbackRecords()
        {
            DataTable dt     = null;
            string    sqlStr = string.Empty;

            try
            {
                sqlStr = "select top 100 * from dbo.NetInterface with(nolock) where [Status]=0 and SendTimes<3 order by Id asc";
                dt     = SqlHelper.ExecuteTable(SqlHelper.connString, sqlStr);
            }
            catch (Exception ex)
            {
                CommLog.WriteExceptionLog("获取需要回调的前100条记录时出现异常:" + ex.Message + ";  sql=" + sqlStr);
                dt = null;
            }
            return(dt);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 定时器执行方法
        /// </summary>
        private static void CallBackInterface(object sender, ElapsedEventArgs e)
        {
            CommLog.WriteSystemLog("开始本次执行...");
            BasicClass basic = new BasicClass();
            //查询数据库中前100条需要回调的记录
            DataTable dt_records = basic.GetCallbackRecords();

            if (dt_records != null && dt_records.Rows.Count > 0)
            {
                string    url         = string.Empty; //接口地址
                int       tenantId    = 0;            //租户id
                string    appSecret   = string.Empty; //密钥
                string    studentNo   = string.Empty; //学号
                string    projectCode = string.Empty; //项目编号
                int       type        = -1;           //修改类型:  0修改审核状态  1修改学员状态(冻结/恢复)
                int       value       = -1;           //修改值:type为0时即修改审核状态 0审核中 1审核通过 2审核未通过   type为1时即修改学员状态  0恢复 1冻结
                string    msg         = string.Empty; //修改状态说明文字
                int       sendTimes   = 0;            //推送次数
                long      id          = 0;
                int       classId     = 0;            //班级id
                DataTable dt_tenant   = null;
                string    sendResult  = string.Empty;
                for (int i = 0; i < dt_records.Rows.Count; i++)
                {
                    tenantId = Convert.ToInt32(dt_records.Rows[i]["TenantId"]);
                    id       = Convert.ToInt64(dt_records.Rows[i]["Id"]);
                    if (tenantId > 0)
                    {
                        dt_tenant = basic.GetCallbackParams(tenantId);
                        if (dt_tenant != null && dt_tenant.Rows.Count > 0)
                        {
                            url       = dt_tenant.Rows[0]["TokenUrl"].ToString();
                            appSecret = dt_tenant.Rows[0]["AesKey"].ToString();
                        }
                    }
                    else
                    {
                        continue;
                    }
                    type        = Convert.ToInt32(dt_records.Rows[i]["Type"]);
                    value       = Convert.ToInt32(dt_records.Rows[i]["Value"]);
                    msg         = dt_records.Rows[i]["ExplainMsg"].ToString();
                    sendTimes   = Convert.ToInt32(dt_records.Rows[i]["SendTimes"]);
                    studentNo   = dt_records.Rows[i]["StudentNo"].ToString();
                    projectCode = dt_records.Rows[i]["ProjectCode"].ToString();
                    classId     = dt_records.Rows[i].IsNull("ClassId")?0: Convert.ToInt32(dt_records.Rows[i]["ClassId"]);
                    if (!string.IsNullOrEmpty(url) && type > -1 && value > -1 && !string.IsNullOrEmpty(appSecret))
                    {
                        sendResult = basic.PostDataUrl(url, type, value, msg, appSecret, studentNo, projectCode, classId);
                        if (!string.IsNullOrEmpty(sendResult))
                        {
                            var postResult = basic.JsonStr2Obj <PostResult>(sendResult);
                            if (postResult != null)
                            {
                                //推送成功
                                basic.UpdateSendStatus(1, id, sendTimes);
                            }
                            else
                            {
                                //推送失败
                                basic.UpdateSendStatus(0, id, sendTimes);
                            }
                        }
                        else
                        {
                            //推送失败
                            basic.UpdateSendStatus(0, id, sendTimes);
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
            }

            CommLog.WriteSystemLog("结束本次执行...");
            CommLog.WriteSystemLog("--------------------------------------------------");
        }