Пример #1
0
        private byte[] genfilename(string extInfo, byte[] req)
        {
            IDictionary <string, string> ext = DCUtil.ParaStrToDic(extInfo);

            if (!ext.ContainsKey("filecate"))
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo(),
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "请求参数filecate为空" },
                }
                           ));
            }
            string filecate = ext["filecate"];

            string filename = genFileNameInternal(filecate);

            return(PackOrb.PackRespose(
                       new HttpHeadInfo {
                StatusCode = HttpStatusCode.Succeed
            },
                       new ListDictionary {
                { "respnum", 1 },
                { "respmsg", "文件名生成成功" },
                { "filename", filename },
            }
                       ));
        }
Пример #2
0
        private byte[] savefile(string extInfo, byte[] req)
        {
            IDictionary <string, string> ext = DCUtil.ParaStrToDic(extInfo);

            if (!ext.ContainsKey("filecate"))
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo(),
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "请求参数filecate为空" },
                }
                           ));
            }
            string filecate = ext["filecate"];

            string filename = this.genFileNameInternal(filecate);
            string filepath = this.genFilePathInternal(filename);

            try
            {
                FileStream fs = new FileStream(Path.Combine(rootDir, filepath), FileMode.Create, FileAccess.Write);
                fs.Write(req, 0, req.Length);
                fs.Close();

                return(PackOrb.PackRespose(
                           new HttpHeadInfo {
                    StatusCode = HttpStatusCode.Succeed
                },
                           new ListDictionary {
                    { "respnum", req.Length },
                    { "respmsg", "文件保存成功" },
                    { "filename", filename },
                }
                           ));
            }
            catch (Exception ex)
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo {
                    StatusCode = HttpStatusCode.ServerSideError
                },
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "文件保存异常 " + ex.Message },
                }
                           ));
            }
        }
Пример #3
0
        private byte[] readfile(string extInfo, byte[] req)
        {
            IDictionary <string, string> ext = DCUtil.ParaStrToDic(extInfo);

            if (!ext.ContainsKey("filename"))
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo(),
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "请求参数filename为空" },
                }
                           ));
            }
            string filename = ext["filename"];

            int offset = 0;

            if (ext.ContainsKey("offset"))
            {
                offset = int.Parse(ext["offset"]);
            }

            int size = 0;

            if (ext.ContainsKey("size"))
            {
                size = int.Parse(ext["size"]);
            }

            string filepath = this.genFilePathInternal(filename);

            if (!File.Exists(filepath))
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo {
                    StatusCode = HttpStatusCode.NotFound
                },
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "文件不存在" },
                }
                           ));
            }

            FileStream fs = new FileStream(Path.Combine(rootDir, filepath), FileMode.Open, FileAccess.Read);

            if (size == 0)
            {
                long realSize = fs.Length;
                if (realSize > 1024 * 1024 * 20)
                {
                    return(PackOrb.PackRespose(
                               new HttpHeadInfo {
                        StatusCode = HttpStatusCode.TooLarge
                    },
                               new ListDictionary {
                        { "respnum", -2 },
                        { "respmsg", "文件超长" },
                    }
                               ));
                }

                size = (int)realSize;
            }

            byte[] result  = new byte[size];
            int    realLen = fs.Read(result, offset, size);

            if (realLen < size)
            {
                Array.Resize(ref result, realLen);
            }

            string contentType = this.judgeFileType(filename);

            return(PackOrb.PackRespose(
                       new HttpHeadInfo
            {
                StatusCode = HttpStatusCode.Succeed,
                ContentType = judgeFileType(filename),
            },
                       result));
        }
Пример #4
0
        private byte[] appendfile(string extInfo, byte[] req)
        {
            IDictionary <string, string> ext = DCUtil.ParaStrToDic(extInfo);

            if (!ext.ContainsKey("filename"))
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo(),
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "请求参数filename为空" },
                }
                           ));
            }
            string filename = ext["filename"];

            string filepath = this.genFilePathInternal(filename);

            try
            {
                FileStream fs = new FileStream(Path.Combine(rootDir, filepath), FileMode.OpenOrCreate, FileAccess.Write);
                if (ext.ContainsKey("offset"))
                {
                    long offset = Convert.ToInt64(ext["offset"]);
                    if (offset > fs.Length)
                    {
                        int gap = (int)(offset - fs.Length);
                        fs.Seek(0, SeekOrigin.End);
                        fs.Write(new byte[gap], 0, gap);
                    }
                    else if (offset < fs.Length)
                    {
                        fs.Seek(offset, SeekOrigin.Begin);
                    }
                }
                fs.Write(req, 0, req.Length);
                fs.Close();

                return(PackOrb.PackRespose(
                           new HttpHeadInfo {
                    StatusCode = HttpStatusCode.Succeed
                },
                           new ListDictionary {
                    { "respnum", req.Length },
                    { "respmsg", "文件追加成功" },
                    { "filename", filename },
                }
                           ));
            }
            catch (Exception ex)
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo {
                    StatusCode = HttpStatusCode.ServerSideError
                },
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "文件追加异常 " + ex.Message },
                }
                           ));
            }
        }
Пример #5
0
        private byte[] dic_list(string extInfo, byte[] req)
        {
            DicListQry para;

            byte[] checkResult = PackOrb.CheckRequest <DicListQry>(req, out para,
                                                                   new string[] {
                "dicname",
            });
            if (checkResult != null)
            {
                return(checkResult);
            }

            #region 查询字典信息

            try
            {
                string dicTbl = null;
                switch (para.dicname)
                {
                case "蔬菜名称":
                    dicTbl = "t_dic_vegname";
                    break;

                case "蔬菜种类":
                    dicTbl = "t_dic_vegtype";
                    break;

                default:
                    return(PackOrb.PackRespose(
                               new HttpHeadInfo
                    {
                        StatusCode = HttpStatusCode.ServerSideError,
                    },
                               new ListDictionary {
                        { "respnum", -2 },
                        { "respmsg", "不存在此字典" },
                    }
                               ));
                }
                QueryCommandConfig CmdConfig = new QueryCommandConfig(string.Format("select * from {0} order by code", dicTbl));
                DataTable          dt        = db.GetDataTable(CmdConfig);

                return(PackOrb.PackRespose(
                           new HttpHeadInfo
                {
                    StatusCode = HttpStatusCode.Succeed,
                },
                           new ListDictionary {
                    { "respnum", dt.Rows.Count },
                    { "respmsg", "获取字典成功" },
                    { "count", dt.Rows.Count },
                    { "data", dt },
                }
                           ));
            }
            catch (Exception e)
            {
                DCLogger.LogError(e.Message);

                return(PackOrb.PackRespose(
                           new HttpHeadInfo
                {
                    StatusCode = HttpStatusCode.ServerSideError,
                },
                           new ListDictionary {
                    { "respnum", -2 },
                    { "respmsg", e.Message },
                }
                           ));
            }

            #endregion
        }
        private static void issueResponse(HttpListenerRequest request, HttpListenerResponse response, ActResult rst)
        {
            if (rst.Exception != null)
            {
                rst.ResultData = PackOrb.PackRespose(
                    new HttpHeadInfo {
                    StatusCode = BizUtils.Rest.HttpStatusCode.ServerSideError
                },
                    new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", rst.Exception },
                }
                    );

                DCLogger.LogError("issueResponse get a biz srv error:{0}", rst.Exception.Message);
            }

            // 设置回应头部内容,长度,编码
            int          httpHeadInfoLength = BitConverter.ToInt32(rst.ResultData, 0);
            HttpHeadInfo httpHeadInfo       = HttpHeadInfo.FromBytes(rst.ResultData, 4, httpHeadInfoLength);
            int          rawBytesIndex      = httpHeadInfoLength + 4;

            response.ContentLength64 = rst.ResultData.LongLength - rawBytesIndex;
            response.ContentType     = httpHeadInfo.ContentType;
            response.StatusCode      = (int)httpHeadInfo.StatusCode;
            // 输出回应内容
            try
            {
                using (BinaryWriter writer = new BinaryWriter(response.OutputStream))
                {
                    writer.Write(rst.ResultData, rawBytesIndex, (int)response.ContentLength64);
                }
            }
            catch (Exception ex)
            {
                DCLogger.LogError(ex.Message);
            }

            if (response.ContentType.Equals(HttpContentType.Json))
            {
                if (response.ContentLength64 > logTxtLen)
                {
                    if (response.StatusCode == (int)BizUtils.Rest.HttpStatusCode.Succeed)
                    {
                        DCLogger.LogTrace(string.Format(
                                              "{0}<--{1}:::{2}",
                                              request.RemoteEndPoint,
                                              request.RawUrl,
                                              Global.Encoding.GetString(rst.ResultData, rawBytesIndex, logTxtLen) + "..."
                                              )
                                          );
                    }
                    else
                    {
                        DCLogger.LogWarn(string.Format(
                                             "{0}<--{1}:::{2}",
                                             request.RemoteEndPoint,
                                             request.RawUrl,
                                             Global.Encoding.GetString(rst.ResultData, rawBytesIndex, logTxtLen) + "..."
                                             )
                                         );
                    }
                }
                else
                {
                    if (response.StatusCode == (int)BizUtils.Rest.HttpStatusCode.Succeed)
                    {
                        DCLogger.LogTrace(string.Format(
                                              "{0}<--{1}:::{2}",
                                              request.RemoteEndPoint,
                                              request.RawUrl,
                                              Global.Encoding.GetString(rst.ResultData, rawBytesIndex, (int)response.ContentLength64)
                                              )
                                          );
                    }
                    else
                    {
                        DCLogger.LogWarn(string.Format(
                                             "{0}<--{1}:::{2}",
                                             request.RemoteEndPoint,
                                             request.RawUrl,
                                             Global.Encoding.GetString(rst.ResultData, rawBytesIndex, (int)response.ContentLength64)
                                             )
                                         );
                    }
                }
            }
        }
Пример #7
0
        private byte[] manage_register(string extInfo, byte[] req)
        {
            RegisterData para;

            byte[] checkResult = PackOrb.CheckRequest <RegisterData>(req, out para,
                                                                     new string[] {
                "MobileNumber",
                "Account",
                "Password",
            });
            if (checkResult != null)
            {
                return(checkResult);
            }
            if (!Regex.IsMatch(para.Account, "^[A-Za-z][A-Za-z0-9_]*$"))
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo
                {
                    StatusCode = HttpStatusCode.BadRequest,
                },
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "帐号必须以英文字母开始,且只能由英文、数字和下划线组成" },
                }
                           ));
            }
            if (!Regex.IsMatch(para.MobileNumber, "^[0-9]*$"))
            {
                return(PackOrb.PackRespose(
                           new HttpHeadInfo
                {
                    StatusCode = HttpStatusCode.BadRequest,
                },
                           new ListDictionary {
                    { "respnum", -1 },
                    { "respmsg", "手机号必须以数字组成" },
                }
                           ));
            }

            #region 保存register信息

            long   ret     = 0;
            string mb_guid = Guid.NewGuid().ToString("N");
            try
            {
                QueryCommandConfig QryCmdConfig = new QueryCommandConfig("select count(*) from t_mobapp_reginfo where MobileNumber = @MobileNumber or Account = @Account");
                QryCmdConfig.Params["MobileNumber"] = para.MobileNumber;
                QryCmdConfig.Params["Account"]      = para.Account;
                object ob = db.ExecuteScalar(QryCmdConfig);
                if (ob != null && Int32.Parse(ob.ToString()) > 0)
                {
                    return(PackOrb.PackRespose(
                               new HttpHeadInfo
                    {
                        StatusCode = HttpStatusCode.BadRequest,
                    },
                               new ListDictionary {
                        { "respnum", -1 },
                        { "respmsg", "该手机号或帐号已注册" },
                    }
                               ));
                }
                CommandConfig CmdConfig = new CommandConfig("t_mobapp_reginfo");
                CmdConfig.Params["mb_guid"]      = mb_guid;
                CmdConfig.Params["MobileNumber"] = para.MobileNumber;
                CmdConfig.Params["Account"]      = para.Account;
                CmdConfig.Params["Password"]     = para.Password;
                ret -= db.ExecuteInsert(CmdConfig);
            }
            catch (Exception e)
            {
                DCLogger.LogError(e.Message);

                return(PackOrb.PackRespose(
                           new HttpHeadInfo
                {
                    StatusCode = HttpStatusCode.ServerSideError,
                },
                           new ListDictionary {
                    { "respnum", -2 },
                    { "respmsg", e.Message },
                }
                           ));
            }

            return(PackOrb.PackRespose(
                       new HttpHeadInfo
            {
                StatusCode = HttpStatusCode.Succeed,
            },
                       new ListDictionary {
                { "respnum", ret },
                { "respmsg", "用户注册成功" },
                { "mb_guid", mb_guid },
            }
                       ));

            #endregion
        }
Пример #8
0
        private byte[] manage_login(string extInfo, byte[] req)
        {
            ClientLogin para;

            byte[] checkResult = PackOrb.CheckRequest <ClientLogin>(req, out para,
                                                                    new string[] {
                "LoginName",
                "Password",
            });
            if (checkResult != null)
            {
                return(checkResult);
            }

            #region 登录验证

            try
            {
                string loginCol = "Account";
                if (Regex.IsMatch(para.LoginName, "^[0-9]*$"))
                {
                    loginCol = "MobileNumber";
                }
                QueryCommandConfig CmdConfig = new QueryCommandConfig(string.Format("select mb_guid,Password from t_mobapp_reginfo where {0} = @LoginName", loginCol));
                CmdConfig.Params["LoginName"] = para.LoginName;
                DataTable dt = db.GetDataTable(CmdConfig);
                if (dt.Rows.Count == 0)
                {
                    return(PackOrb.PackRespose(
                               new HttpHeadInfo
                    {
                        StatusCode = HttpStatusCode.BadRequest,
                    },
                               new ListDictionary {
                        { "respnum", -1 },
                        { "respmsg", "未找到此用户" },
                    }
                               ));
                }
                if (dt.Rows[0]["Password"].ToString().Equals(para.Password))
                {
                    return(PackOrb.PackRespose(
                               new HttpHeadInfo
                    {
                        StatusCode = HttpStatusCode.Succeed,
                    },
                               new ListDictionary {
                        { "respnum", 1 },
                        { "respmsg", "用户登录成功" },
                        { "mb_guid", dt.Rows[0]["mb_guid"].ToString() },
                    }
                               ));
                }
                else
                {
                    return(PackOrb.PackRespose(
                               new HttpHeadInfo
                    {
                        StatusCode = HttpStatusCode.BadRequest,
                    },
                               new ListDictionary {
                        { "respnum", -1 },
                        { "respmsg", "密码错误" },
                    }
                               ));
                }
            }
            catch (Exception e)
            {
                DCLogger.LogError(e.Message);

                return(PackOrb.PackRespose(
                           new HttpHeadInfo
                {
                    StatusCode = HttpStatusCode.ServerSideError,
                },
                           new ListDictionary {
                    { "respnum", -2 },
                    { "respmsg", e.Message },
                }
                           ));
            }

            #endregion
        }