Demo调用WebApi接口 | CSFramework.WebApi后端开发框架|C/S开发框架
Demo调用WebApi接口 - CSFramework.WebApi后端开发框架
一、调用登录接口
模拟Web端登录
Web网页端登录,通常是使用【手机号码】或【电子邮箱】登录,参考代码:
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移动端登录
/// <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;
}
}
二、调用业务接口
新增客户
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;
}
删除客户
/// <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;
}
查询客户详情
/// <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测试程序
参考:CSFramework.WebApi后端框架 - 客户端测试程序(CS.WebApi.Tester)
https://www.cscode.net/archive/webapi/1631027972.html
四、附:Cmd_Test_Customer.cs 业务层(命令层)
/// <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)
};
}
}
非常感谢您阅读本文,"CSCODE.NET - C/S开发框架网 - 致力于为.NET开发者提供软件开发文档,分享经验.创造卓越!"
CSFramework.WebApi是服务端快速开发框架(后端框架),基于ASP.NET提供的WebAPI底层架构强大的编程能力,封装成为可复用、可扩展、可定制开发的服务端软件开发框架,助力您快速轻松搭建基于HTTP/HTTPS等协议以及支持多种客户端(如:APP、B/S、C/S、微信公众号、微信小程序等)各种跨平台移动终端的服务端应用程序。
官网为广大用户免费提供更多C#.NET技术文档与IT行业资讯,仅供参考使用和学习交流。若对您有帮助,可以点击收藏本文地址。您有任何意见或建议,请在评论区留言,也欢迎大家和我们交流互动,促进共同发展。
请扫二维码与产品经理沟通
CSCODE.NET竭诚为您服务,欢迎垂询!
产品经理能为您解答:Winform开发框架,C/S架构,软件开发平台相关技术。
官网:www.cscode.net
– 致力于为.NET开发者分享软件技术及开发经验
扫一扫加作者微信