首页 > 代码库 > web api

web api

上面是我要完成的任务。

 

namespace FamilyGathering.API
{
    /// <summary>
    /// Project 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。
    // [System.Web.Script.Services.ScriptService]
    public class Project : System.Web.Services.WebService
    {

        public ZCSoapHeader header = new ZCSoapHeader();
        ////定义用户身份验证类变量header
        [System.Web.Services.Protocols.SoapHeader("header")]//用户身份验证的soap头
        [WebMethod(Description = "众筹项目接口")]
        public string Add()
        {
            string strResult = "";
            if (header.CheckUser())  //存在、帐号登录成功)
            {
                if (header.Project!=null)
                {
                    DAL.Entity.ApiProjectModel project = header.Project;
                    DAL.ApiProject ap = new ApiProject();
                    ap.ApiUserID = project.ApiUserID;
                    ap.ProjectName = project.ProjectName;
                    ap.Description = project.Description;
                    ap.CurrentAmount = project.CurrentAmount;
                    ap.TargetAmount = project.TargetAmount;
                    ap.Support = project.Support;
                    ap.EndTime = project.EndTime;
                    ap.ProjectSponsor = project.ProjectSponsor;
                    ap.State = project.State;
                    ap.Pic = project.Pic;
                    ap.ProjectUrl = project.ProjectUrl;
                    ap.PostDate = project.PostDate;
                    ap.Audit = project.Audit;
                    if (BLL.ApiProjectManager.CreateInstance().Add(ap) > 0)
                    {
                        strResult = "添加项目成功";
                    }
                    else
                    {
                        strResult = "添加项目失败";
                    }
                }
                else
                {
                    strResult = "项目信息为空";
                }
            }
            else
            {
                strResult = "帐号或者密码错误";
            }
            return strResult;
        }


        [WebMethod(Description = "上传图片接口")]
        public string UploadFile(byte[] fileBytes, string fileName)
        {
            try
            {
                MemoryStream memoryStream = new MemoryStream(fileBytes); //1.定义并实例化一个内存流,以存放提交上来的字节数组。   
                string strWjj = ConfigHelper.GetConfigString("picUpload") + "/ImgAPI";
                if (!Directory.Exists(strWjj))
                {
                    Directory.CreateDirectory(strWjj);
                }
                string strDate = DateTime.Now.ToString("yyyyMMdd");
                string strWjjx = ConfigHelper.GetConfigString("picUpload") + "/ImgAPI"+"/"+strDate;
                if (!Directory.Exists(strWjjx))
                {
                    Directory.CreateDirectory(strWjjx);
                }
                FileStream fileUpload = new FileStream(strWjjx +"\\"+ fileName, FileMode.Create); ///2.定义实际文件对象,保存上载的图片。   
                memoryStream.WriteTo(fileUpload); ///3.把内存流里的数据写入物理文件   
                memoryStream.Close();
                fileUpload.Close();
                fileUpload = null;
                memoryStream = null;
                return strDate+"/"+fileName;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }
}

 

namespace FamilyGathering.API
{
    public class ZCSoapHeader : SoapHeader
    {
        public string UserName{get;set;}
        public string PassWord { get; set; }
        public ApiProjectModel Project { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ZCSoapHeader()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">密码</param>
        public ZCSoapHeader(string userName, string passWord, ApiProjectModel project)
        {
            this.UserName = userName;
            this.PassWord = passWord;
            this.Project = project;
        }

        /// <summary>
        /// 用户安全验证
        /// </summary>
        /// <returns></returns>
        public bool CheckUser()
        {
            string strPwd = Common.Utils.MD5(Common.Utils.MD5(PassWord.Trim()));  //密码加密
            if (BLL.ApiUserInfoManager.CreateInstance().QueryBool(UserName, strPwd))  //存在
                return true;
            else
                return false;
        }   
    }
}

 

调用 :  先添加这个webservice服务。

 protected void btnUploadImages_Click(object sender, EventArgs e)
        {
            //身份验证、添加项目内容
            //调用接口传参
            ProjectService.ZCSoapHeader zsh = new ProjectService.ZCSoapHeader();
            zsh.UserName = "1";     //登陆用户名
            zsh.PassWord = "2";     //登录密码
            ApiProjectModel apm = new ApiProjectModel();
            apm.ApiUserID = 2;        //API用户ID
            apm.ProjectName = "1";    //项目名称
            apm.Description = "1";    //项目描述
            apm.CurrentAmount = 1;    //目前累计金额
            apm.TargetAmount = 1;     //目标金额
            apm.Support = 1;          //支持人数    
            apm.EndTime = DateTime.Now;    //结束时间
            apm.ProjectSponsor = "1";      //项目发起人
            apm.State = 1;                 //状态(1、预热中 2、筹资中 3、已成功 )
            apm.Pic = @"C:\Users\Administrator\Desktop\1004461W8-27.jpg";  //图片绝对路径
            apm.ProjectUrl = "1";   //项目地址
            apm.PostDate = DateTime.Now;    //提交时间
            apm.Audit = 0;//是否通过审核(0没有通过审核, 1通过审核)
            lbTxt.Text = AddProject(zsh,apm);  //验证用户名,并且添加项目成功. 并且返回结果
        }

        /// <summary>
        /// 添加项目
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="passWord"></param>
        /// <param name="apm">项目数据(ApiUserID 登录用户ID,ProjectName 项目描述,Description 项目描述,CurrentAmount目前累计金额,TargetAmount 目标金额,Support 支持人数,EndTime 结束时间,ProjectSponsor 项目发起人,State 状态(1、预热中 2、筹资中 3、已成功 ),Pic 图片地址(这里传值本地绝对路径),ProjectUrl 项目地址,PostDate 提交时间,Audit 是否通过审核(0没有通过审核, 1通过审核))</param>
        /// <returns>返回结果</returns>
        public string AddProject(ProjectService.ZCSoapHeader zsh, ApiProjectModel apm)
        {
            //身份验证、添加项目内容
            //调用接口传参
            ProjectService.ProjectSoapClient psc = new ProjectSoapClient();
            #region 上传图片到服务器
            if (uploadImages.FileBytes.Length > 0)    //有图片
            {
                string extension = System.IO.Path.GetExtension(uploadImages.PostedFile.FileName); //获取扩展名
            }
            else
            {
                return "请先选择图片";
            }
            FileInfo imgFile = new FileInfo(@"" + apm .Pic+ "");
            if (imgFile==null||imgFile.Length<=0)
            {
                return "请上传图片";
            }
            byte[] imgByte = new byte[imgFile.Length];//1.初始化用于存放图片的字节数组   
            System.IO.FileStream imgStream = imgFile.OpenRead();//2.初始化读取图片内容的文件流   
            imgStream.Read(imgByte, 0, Convert.ToInt32(imgFile.Length));//3.将图片内容通过文件流读取到字节数组   
            apm.Pic = psc.UploadFile(imgByte, DateTime.Now.ToString("hhmmss") + uploadImages.FileName);    //图片上传服务器,返回服务器保存图片路径  (图片地址)
            #endregion
            zsh.Project = apm;   //项目内容
            return psc.Add(zsh);  //验证用户名,并且添加项目成功. 并且返回结果
        }

 

 

好久好久没来写下自己所做的所新学的东西了.   今天又换了2家公司。  目前这家公司用mvc+ef开发项目在.     留笔~