Demo调用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测试程序
参考:CSFramework.WebApi后端框架 - 客户端测试程序(CS.WebApi.Tester)
https://www.cscode.net/archive/webapi/1631027972.html
四、附:Cmd_Test_Customer.cs 业务层(命令层)
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)
};
}
}
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
WebApi C/S框架网