开发日记:接口开发设计

接口开发使用规则

业务术语:


请求:通过HTTP协议把需要传输的数据发送给接收方的过程。

返回:根据得到的数据处理完成后,将处理完成的结果反馈给接收方。

敏感词:带有敏感政治倾向,暴力倾向,不健康色彩或不文明的词。

特殊字符:特殊字符包括:^ | $ # 等。

操作结果:成功失败,成功返回结果,失败返回结果错误信息。

操作流程:执行之前,执行之后,正在执行。

接口编写规则:


1. 所有的接口必须包含method和version参数。

2. 所有的接口增加了版本号管理,以便适应多个不同版本客户端的使用。

3. 统一的接口调用方式,减少切换思维导致编码速度下降。

4. 接口返回结果统一使用标准的JSON格式,暂时不考虑使用XML格式。

5. 所有的接口必须要有日志,方便维护。

接口接入规则:


1. 配置合作着接入ID必须是唯一的。

2. 所有的接口必须包含method和version参数。

3. 参数列表中,不可空的参数必须配置。

4. 参数列表中,可空的但需要多选一的多个参数中,必须配置至少一个。

5. 字符串转化成字节流时指定的字符集必须与接口一直,常用的字符串编码有:(ASCII/Unicode/UTF-8)。

6. 接口数据传输必须使用http协议,支持get或post方式提交。

7. 保证网络顺畅,防止重复提交。

8. 如果响应时间过长直接导致数据返回失败,提示接收端重新提交。

接口编码设计:


使用抽象依赖倒置思维进行接口设计

· 定义一个通用抽象类

public interface IService

{

/// <summary>

/// 调用服务。

/// </summary>

void Invoke(HttpContext context);

}

public abstract class Service

{

protected abstract void Execute(HttpContext context);

public void Invoke(HttpContext context)

{

this.Execute(context);

}

}

· 所有接口继承这个抽象类 如下:

public class CompanyPersonnelOmitTotal : Service

{

protected override void Execute(HttpContext context)

{

//默认执行1.0版本的业务逻辑

string ver = LRequest.GetString("version");

if (ver == "2.0"){

//在这里编写业务规则

}else{

//在这里编写业务规则

}

}

}
· 上位机调用接口API
public class TestService : Service

{

protected override void Execute(HttpContext context)

{

lock (this)

{

string sMothod = LRequest.GetString("method");

IPatrolNew p = ClassFactory(LRequest.GetString("version"));

var result = (Result)MethodCaller.CallMethod(p, sMothod, context);

context.Response.Clear();

if (result.ResObject != null)

{

if (result.ResObject is DataTable)

{

var table = result.ResObject as DataTable;

string strjson = JsonHelper.DataTableToJSONStr(table).ToString();

context.Response.Write(strjson);

}

else if (result.ResObject is string)

{

string strjson = result.ResObject as string;

context.Response.Write(strjson);

}

}

else

{

context.Response.Write(result.StatusCode.ToString());

}

}

}

private IPatrolNew ClassFactory(string version)

{

IPatrolNew p;

switch (version)

{

case "1.0":

p = new Patrol2();

break;

default:

p = new Patrol2();

break;

}

return p;

}

}

· 使用IHttpHandler方式对外的代码

public class LCLPolling : IHttpHandler

{

public void ProcessRequest(HttpContext context)

{

var ser = new TestService();

ser.Invoke(context);

}

}

接口调用示例:

以下是一个接口的描述

clip_image002

Android接口调用示例:

ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();

params.add(new BasicNameValuePair("method"," GetCompanyPersonnelOmitTotal"));

params.add(new BasicNameValuePair("departId","725"));

params.add(new BasicNameValuePair("dateType","currMonth"));

try {

String result = HttpUtilByPost.httpPostData("http:192.168.1.118:6666\LAndroid\LCLPolling.ashx ", params);

JSONObject json = new JSONObject(result);

if(json.length() == 0){

System.out.println("empty json");

return;

}

JSONArray array = json.optJSONArray("object");

for(int i = 0;i < array.length(); i++ ){

JSONObject obj = array.getJSONObject(i);

}

} catch (Exception e) {

e.printStackTrace();

}

调用 人员漏巡统计 接口示例

JQuery接口调用示例:

<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>

<script type="text/javascript" language="javascript">

$.ajax({

type: "POST",

url: "http:192.168.1.118:6666/LAndroid/LCLPolling.ashx",

data: "method=GetEmployeesAlarmStat&departId=725&dateType=currMonth",

cache: false,

async: false,

dataType: "text",

success: function (data) {

document.write(data);

},

error: function () {

alert("error");

}

});

</script>

时间查询接口参数设计

datetype

  
 

curryear

本年
 

currmonth

本月
 

quarter

 
  

yearindex代表是那一年,默认当前年度

  

quarterindex代表第几季度,默认全部

 

year

 
  

yearindex代表是那一年,默认当前年度

 

month

 
  

yearindex代表是那一年,默认当前年度

  

monthIndex代表那月,默认全部

 

date

 
  

startdate开始时间,时间格式:yyyy-MM-dd HH:mm:ss

  

enddate结束时间

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值