Пример #1
0
        /// <summary>
        /// 成功登录后更新token,并退出原用户(仅限web后台系统使用)
        /// </summary>
        /// <param name="id">用户ID</param>
        /// <param name="client">客户端ID</param>
        /// 在此示范标准服务写法(参数验证,日志操作,数据库操作等)
        public static string UpdateUserToken(int id, string client)
        {
            if (id == 0)
            {
                return("请传入id参数");
            }
            if (string.IsNullOrWhiteSpace(client))
            {
                return("请传入client参数");
            }
            string token = "";

            try
            {
                string    sql   = string.Format("select top 1 * from sys_user_login where user_id = {0} order by create_time desc", id);
                string    error = "";
                DataTable dt    = DataBaseHelper.ExecuteTable(sql, out error);
                if (error == "")
                {
                    if (dt.Rows.Count != 0)
                    {
                        DataRow  dr      = dt.Rows[0];
                        DateTime oldTime = DateTime.Parse(dr["create_time"].ToString());
                        if (DateTime.Now - oldTime < new TimeSpan(token_time))
                        {
                            //上一个token还未到期,需要将上一个用户踢下线
                            string oldClient = dr["client_id"].ToString();
                        }
                    }
                    string where = "client_id = '" + client + "' and token = '" + new Guid().ToString() + "'";
                    sql          = string.Format("update sys_user_login set {0} where user_id = {1}", where, id);
                    token        = DataBaseHelper.ExecuteScalar(sql, out error).ToString();
                    if (error != "")
                    {
                        throw new Exception(error);
                    }
                }
                else
                {
                    throw new Exception(error);
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteErrorLog("token更新失败", "2001", ex.Message, ex.StackTrace);
            }

            return(token);
        }
Пример #2
0
        public static string SaveFile(out string str_error, string file_name, byte[] file_data)
        {
            str_error = "";
            string result = "";

            try
            {
                if (string.IsNullOrWhiteSpace(file_name))
                {
                    throw new Exception("请传入文件名");
                }

                if (file_data.Length <= 0)
                {
                    throw new Exception("请传入文件内容");
                }

                string directory = AppHome.Upload + DateTime.Now.ToString("yyyy_MM_dd") + "\\";

                if (!Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                string file_path = directory + file_name;

                using (FileStream fs = new FileStream(file_path, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(file_data, 0, file_data.Length);
                    fs.Flush();
                }
                result = file_path;
            }
            catch (Exception ex)
            {
                str_error = ex.Message;
                SystemLog.WriteErrorLog("查询用户信息失败", "1004", ex.Message, ex.StackTrace);
            }
            return(result);
        }
Пример #3
0
        public static bool SendMailRY(string mobile, int type, string templateId, string[] param = null)
        {
            string uri = "";

            if (type == 1)
            {
                uri = "http://api.sms.ronghub.com/sendCode.json";
            }
            else if (type == 2)
            {
                uri = "http://api.sms.ronghub.com/sendNotify.json";
            }
            int nonce = new Random().Next(9999);

            string timestamp = ((DateTime.Now.Ticks - new DateTime(1970, 1, 1, 0, 0, 0, 0).Ticks) / 10000).ToString();

            string signature = SHA1_Encrypt(ConfCenter.MailAppSecret + nonce + timestamp);

            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                request.Method = "Post";
                request.Host   = "api.sms.ronghub.com";
                request.Headers.Add("App-Key", ConfCenter.MailAppKey);
                request.Headers.Add("Nonce", nonce.ToString());
                request.Headers.Add("Timestamp", timestamp);
                request.Headers.Add("Signature", signature);
                request.ContentType = "application/x-www-form-urlencoded";
                Stream stream;
                stream = request.GetRequestStream();
                string p = "mobile=" + mobile + "&region=86&templateId=" + templateId;
                if (param != null)
                {
                    for (int i = 1; i <= param.Length; i++)
                    {
                        p += "&p" + i + "=" + param[i - 1];
                    }
                }
                byte[] content = Encoding.UTF8.GetBytes(p);
                stream.Write(content, 0, content.Length);
                stream.Close();
                HttpWebResponse response   = (HttpWebResponse)request.GetResponse();
                StreamReader    reader     = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string          strRtnHtml = reader.ReadToEnd();
                reader.Close();
                RYResult result = JsonConvert.DeserializeObject <RYResult>(strRtnHtml);
                if (result.code == 200 && result.sessionId != "")
                {
                    return(true);
                }
                else
                {
                    throw new Exception(strRtnHtml);
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteErrorLog("短信发送失败", "1010", ex.Message, ex.StackTrace);
            }

            return(false);
        }
Пример #4
0
        public static string upload(out string str_error, Stream file_stream)
        {
            str_error = "";
            try
            {
                string result = "";

                byte[] data = Misc.ToByteArray(file_stream);

                Encoding encoding = Encoding.UTF8;

                string content = encoding.GetString(data);

                string cloneContent = content;

                string delimiter = content.Substring(0, content.IndexOf("\r\n"));

                while (cloneContent.IndexOf("---------") != 0)
                {
                    cloneContent = cloneContent.Substring(delimiter.Length + 2);
                    int    index = cloneContent.IndexOf("\r\n\r\n");
                    string head  = cloneContent.Substring(0, index);

                    Match  nameMatch = new Regex(@"(?<=name\=\"")(.*?)(?=\"")").Match(head);
                    string file_name = nameMatch.Value.Trim().ToLower();

                    int startIndex = Misc.IndexOf(data, encoding.GetBytes(head), 0) +
                                     encoding.GetBytes(head).Length +
                                     encoding.GetBytes("\r\n\r\n").Length;
                    byte[] delimiterBytes = encoding.GetBytes("\r\n" + delimiter);
                    int    endIndex       = Misc.IndexOf(data, delimiterBytes, startIndex);

                    int contentLength = endIndex - startIndex;

                    byte[] fileData = new byte[contentLength];

                    Buffer.BlockCopy(data, startIndex, fileData, 0, contentLength);

                    string directory = AppHome.Upload + Guid.NewGuid() + "\\";

                    if (!Directory.Exists(directory))
                    {
                        Directory.CreateDirectory(directory);
                    }

                    string file = directory + file_name;

                    using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
                    {
                        fs.Write(fileData, 0, contentLength);
                        fs.Flush();
                    }

                    result += file.Substring(AppHome.BaseDirectory.Length - 1) + "*";

                    cloneContent = cloneContent.Substring(cloneContent.IndexOf(delimiter) + 9);
                }

                if (result.Length > 0)
                {
                    result = result.Substring(0, result.Length - 1);
                    result = result.Replace("\\", "/");
                }

                return(result);
            }
            catch (Exception ex)
            {
                str_error = ex.Message;
                SystemLog.WriteErrorLog("查询用户信息失败", "1004", ex.Message, ex.StackTrace);
            }
            return("");
        }