Demo调用WebApi接口 - CSFramework.WebApi后端开发框架


CSFramework.WebApi后端开发框架接口调用教程-开发框架文库

Demo调用WebApi接口 - CSFramework.WebApi后端开发框架

一、调用登录接口

模拟Web端登录

Web网页端登录,通常是使用【手机号码】或【电子邮箱】登录,参考代码:

C# 全选
 public class TestWebLogin
    {
        /// <summary>
        /// 模拟Web端登录
        /// </summary>
        /// <returns></returns>
        public static ModelUser Test()
        {
            string url = "http://localhost:19893/api/gate/loginweb";

            //登录请求对象
            ModelRequestLogin4Web request = new ModelRequestLogin4Web
            {
                ApiKey = PrivateData.API_KEY,
                UserID = "1388888888",//手机号码,对应Api_User.Phone
                Password = "cf79ae6addba60ad018347359bd144d2",
                Timestamp = DateTime.Now.ToString("yyyyMMddHHmmssfff"),
            };

            string json = JsonConvert.SerializeObject(request);

            //POST, 调用WebApi接口,并返回json数据
            string result = WebApiTools.Post(url, json);

            //JSON转换为对象
            ModelResponse response = JsonConvert.DeserializeObject<ModelResponse>(result);
            if (response.DataIsEncrypted)
            {
                response.Data = System.Web.HttpUtility.UrlDecode(response.Data);
                response.Data = CryptoHelper.DESDecrypt(response.Data, PrivateData.DES_Key, PrivateData.DES_iv);
            }

            //返回的用户令牌数据
            ModelUser user = JsonConvert.DeserializeObject<ModelUser>(response.Data);

            return user;
        }
    }

 

模拟Winform客户端\APP移动端登录

C# 全选
/// <summary>
    /// 模拟Winform客户端\APP移动端登录
    /// </summary>
    public class TestWinformLogin
    {

        public static ModelUser Test()
        {
            string apikey = "9d869848-89d4-4bf4-9d5a-01158a9a6812";
            string secret = "10ab9534-b1b5-44d9-ac05-edf68ce7d50a";

            string url = "http://localhost:19893/api/gate/login";

            //数据对象
            ModelUserLogin M = new ModelUserLogin
            {
                UserID = "13888889999",
                Password = "12345234523542354",
                DeviceMID = "我的设备识别码",//Winform客户端:CPU+硬盘编号,移动端:手机设备号,Web端:浏览器信息
                Version = "1.928"//软件版本号
            };

            string data = JsonConvert.SerializeObject(M, JsonSettings.Current);

            //主体对象
            ModelRequest request = new ModelRequest();
            request.Timestamp = DateTime.Now.ToString("yyyyMMddHHmmssfff");
            request.ApiKey = apikey;
            request.Data = data;
            request.Sign = CryptoHelper.ToMD5(request.ApiKey + request.Data + secret + request.Timestamp);//Sign数字签名

            string json = JsonConvert.SerializeObject(request);

            //POST, 调用WebApi接口,并返回json数据
            string result = WebApiTools.Post(url, json);

            //JSON转换为对象
            ModelResponse response = JsonConvert.DeserializeObject<ModelResponse>(result);

            //返回的用户令牌数据
            ModelUser user = JsonConvert.DeserializeObject<ModelUser>(response.Data);

            return user;
        }

    }

 

二、调用业务接口

新增客户

C# 全选
  internal static Entity_Customer Add(ModelUser userToken, ref string result)
        {
            //实例化客户对象
            Entity_Customer customer = new Entity_Customer
            {
                CustomerCode = DateTime.Now.ToString("yyyyMMddHHmmss"),
                NativeName = "客户名称:珠海市喜鹊信息技术有限公司",
                Address1 = "客户地址:珠海市",
            };

            string url = "http://localhost:19893/api/do/WebClientCall";

            //动态令牌方式传入WebApi接口的参数模型
            ModelRequestToken M = new ModelRequestToken
            {
                Timestamp = DateTime.Now.ToString("yyyyMMddHHmmss"),//时间戳
                Action = 820000,
                Operation = "Add",//操作指令, Add=新增
                Token = userToken.Token,//令牌
                UserID = userToken.Account,//用户账号
                Data = JsonConvert.SerializeObject(customer),//客户实体对象,转JOSN格式
            };

            //请求转换为JSON
            string json = JsonConvert.SerializeObject(M, JsonSettings.Current);
            result = WebApiTools.Post(url, json);
            return customer;
        }

删除客户

C# 全选
 /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="userToken">当前登录用户</param>
        /// <param name="customerCode">客户编码</param>
        /// <returns></returns>
        internal static string Delete(ModelUser userToken, string customerCode)
        {
            string url = "http://localhost:19893/api/do/WebClientCall";

            //动态令牌方式传入WebApi接口的参数模型
            ModelRequestToken M = new ModelRequestToken
            {
                Timestamp = DateTime.Now.ToString("yyyyMMddHHmmss"),//时间戳
                Action = 820000,
                Operation = "Delete",//操作指令, Delete=删除
                Token = userToken.Token,//令牌
                UserID = userToken.Account,//用户账号
                Data = customerCode,//客户编码
            };

            string json = JsonConvert.SerializeObject(M, JsonSettings.Current);
            string result = WebApiTools.Post(url, json);
            return result;
        }

 

查询客户详情

C# 全选
/// <summary>
        /// 获取客户详情
        /// </summary>
        /// <param name="userToken">当前系统登录用户</param>
        /// <param name="customerCode">客户编码</param>
        /// <returns></returns>
        public static string View(ModelUser userToken, string customerCode)
        {
            string url = "http://localhost:19893/api/do/WebClientCall";

            //动态令牌方式传入WebApi接口的参数模型
            ModelRequestToken M = new ModelRequestToken
            {
                Timestamp = DateTime.Now.ToString("yyyyMMddHHmmss"),//时间戳
                Action = 820000,
                Operation = "View",
                Token = userToken.Token,//令牌
                UserID = userToken.Account,//用户账号
                Data = customerCode//客户编码
            };

            string json = JsonConvert.SerializeObject(M, JsonSettings.Current);
            string result = WebApiTools.Post(url, json);
            return result;
        }

 

三、各种应用场景接口调用

参考WebApi测试程序

Demo调用WebApi接口 - CSFramework.WebApi后端开发框架

参考:CSFramework.WebApi后端框架 - 客户端测试程序(CS.WebApi.Tester)

https://www.cscode.net/archive/webapi/1631027972.html

四、附:Cmd_Test_Customer.cs 业务层(命令层)

Demo调用WebApi接口 - CSFramework.WebApi后端开发框架

 

C# 全选
 /// <summary>
        /// 执行命令
        /// </summary>
        /// <returns></returns>
        public override IUserResponse Execute()
        {
            //新增客户
            if (_UserRequest.Operation == CommandOperation.Add.ToString())
            {
                Entity_Customer customer = _UserRequest.GetDataObject<Entity_Customer>();

                //新方法,使用DbFramework                
                bool ok = _DAL.AddCustomer(customer);
                return new ModelResponse { Code = ok ? 0 : -1, Message = "操作" + (ok ? "成功" : "失败") };
            }

            //删除客户
            if (_UserRequest.Operation == CommandOperation.Delete.ToString())
            {
                bool ok = _DAL.Delete(_UserRequest.Data);
                return new ModelResponse { Code = ok ? 0 : -1, Message = "操作" + (ok ? "成功" : "失败") };
            }

            //根据主键值获取详情
            if (EqualsOP(_UserRequest.Operation, CommandOperation.View))//GetDataByKey
            {
                Entity_Customer customer = _DAL.Get(_UserRequest.Data);
                return new ModelResponse
                {
                    Code = 0,
                    Message = "操作成功",
                    Data = JsonConvert.SerializeObject(customer)
                };
            }
}

 

内容图片-底图-高效开发.追求卓越-宽-开发框架文库

 

C/S框架网|原创精神.创造价值.打造精品

 

版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
C/S框架网
上一篇:Demo开发环境配置 - CSFramework.WebApi后端开发框架
下一篇:申请CSFramework.WebApi开发框架开发体验版Demo版本
评论列表

发表评论

评论内容
昵称:
关联文章

Demo调用WebApi接口 - CSFramework.WebApi开发框架
Demo开发环境配置 - CSFramework.WebApi开发框架
CSFramework.WebApi服务器框架:客户调用WebApi接口方式(签名+Token令牌)
CSFramework.WebApi框架提供两种接口调用方式
CSFramework.WebApi开发框架模拟Web用户登录、调用WebApi接口增删改查数据
WebApi框架服务框架开发微信小程序订阅消息接口
Web开发框架-VUE使用Axios调用WebAPI接口
CSFramework.WebApi开发框架APP框架
C#.NET WebApi接口搭建教程,WebApi接口开发实例
客户测试程序(CS.WebApi.Tester)- CSFramework.WebApi框架
测试DoController控制器 - CSFramework.WebApi框架
WebApi NETCore框架 - APIProviderFactory 调用WebApi接口
GateController – 系统安全门禁控制器 - CSFramework.WebApi框架
基于Web前端用户调用CSFramework.WebApi服务登录登出接口实现
CSFramework.WebApi框架Log日志配置详解
CSFramework.WebApi框架软件截图
CSFramework.WebApi框架 - 开发者账号(公钥ApiKey与私钥SecretKey)
软件开发框架-WebApi框架培训大纲(1)
CSFramework.WebApi开发框架服务器宿主承载方式
WebApi框架Token身份认证,Api接口Token验证