mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
可自定义数据节点
This commit is contained in:
@@ -21,7 +21,7 @@ SDK依赖了三个jar包
|
||||
- 参数:name 故事名称
|
||||
- 返回信息
|
||||
|
||||
```
|
||||
```json
|
||||
{
|
||||
"alipay_story_find_response": {
|
||||
"msg": "Success",
|
||||
|
69
doc/docs/files/10099_更改数据节点名称.md
Normal file
69
doc/docs/files/10099_更改数据节点名称.md
Normal file
@@ -0,0 +1,69 @@
|
||||
# 更改数据节点名称
|
||||
|
||||
针对`alipay.story.find`接口,它的返回结果如下:
|
||||
|
||||
```json
|
||||
{
|
||||
"alipay_story_find_response": {
|
||||
"msg": "Success",
|
||||
"code": "10000",
|
||||
"name": "白雪公主",
|
||||
"id": 1,
|
||||
"gmtCreate": 1554193987378
|
||||
},
|
||||
"sign": "xxxxx"
|
||||
}
|
||||
```
|
||||
|
||||
其中`alipay_story_find_response`是它的数据节点。规则是:
|
||||
|
||||
> 将接口名中的"."转换成"_",后面加上"_response"。
|
||||
|
||||
代码实现如下:
|
||||
|
||||
```java
|
||||
String method = "alipay.story.find";
|
||||
return method.replace('.', '_') + "_response";
|
||||
```
|
||||
|
||||
详见`DefaultDataNameBuilder.java`
|
||||
|
||||
如果要更改数据节点,比如`result`,可使用`CustomDataNameBuilder.java`。
|
||||
|
||||
```java
|
||||
@Configuration
|
||||
public class ZuulConfig extends AlipayZuulConfiguration {
|
||||
|
||||
static {
|
||||
...
|
||||
ApiConfig.getInstance().setDataNameBuilder(new CustomDataNameBuilder());
|
||||
...
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
设置后,网关统一的返回结果如下:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": {
|
||||
...
|
||||
},
|
||||
"sign": "xxxxx"
|
||||
}
|
||||
```
|
||||
|
||||
此外,构造方法可指定自定义字段名称:`new CustomDataNameBuilder("data");`。
|
||||
设置后,数据节点将变成`data`
|
||||
|
||||
```json
|
||||
{
|
||||
"data": {
|
||||
...
|
||||
},
|
||||
"sign": "xxxxx"
|
||||
}
|
||||
```
|
||||
|
||||
**注**:网关设置了CustomDataNameBuilder后,SDK也要做相应的更改:`SdkConfig.dataNameBuilder = new CustomDataNameBuilder();`
|
145
doc/docs/files/10100_对接前端.md
Normal file
145
doc/docs/files/10100_对接前端.md
Normal file
@@ -0,0 +1,145 @@
|
||||
# 对接前端
|
||||
|
||||
开放平台一般对接服务端应用,开发者使用SDK来调用接口。
|
||||
理论上来说,只要是客户端程序,都可以调用网关接口。因此,同样可以对接前端应用(H5、小程序、App)。
|
||||
|
||||
针对H5页面,配置方式如下:
|
||||
|
||||
- 网关关闭签名校验。`ApiConfig.getInstance().setIgnoreValidate(true);`
|
||||
|
||||
由于是网页调用接口,把秘钥放在前端意义不大,干脆直接关闭签名校验。
|
||||
|
||||
- 设置统一的数据节点
|
||||
|
||||
```java
|
||||
@Configuration
|
||||
public class ZuulConfig extends AlipayZuulConfiguration {
|
||||
|
||||
static {
|
||||
...
|
||||
ApiConfig.getInstance().setIgnoreValidate(true);
|
||||
ApiConfig.getInstance().setDataNameBuilder(new CustomDataNameBuilder());
|
||||
...
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
设置后,网关统一的返回结果如下:
|
||||
|
||||
```json
|
||||
{
|
||||
"result": {
|
||||
...
|
||||
},
|
||||
"sign": "xxxxx"
|
||||
}
|
||||
```
|
||||
|
||||
- 封装请求工具【可选】
|
||||
|
||||
封装请求,方便调用,针对vue的封装如下:
|
||||
|
||||
```js
|
||||
// 创建axios实例
|
||||
const client = axios.create({
|
||||
baseURL: process.env.BASE_API, // api 的 base_url
|
||||
timeout: 5000, // 请求超时时间
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
|
||||
})
|
||||
|
||||
const RequestUtil = {
|
||||
/**
|
||||
* 请求接口
|
||||
* @param method 接口名,如:goods.get,goods.get
|
||||
* @param version 版本号,如:1.0
|
||||
* @param data 请求数据,json格式
|
||||
* @param callback 成功回调
|
||||
* @param errorCallback 失败回调
|
||||
*/
|
||||
post: function(method, version, data, callback, errorCallback) {
|
||||
client.request({
|
||||
method: method,
|
||||
version: version,
|
||||
biz_content: JSON.stringify(data)
|
||||
}).then(function(response) {
|
||||
const resp = response.result
|
||||
const code = resp.code
|
||||
// 成功,网关正常且业务正常
|
||||
if (code === '10000' && !resp.sub_code) {
|
||||
callback(resp)
|
||||
} else {
|
||||
// 报错
|
||||
Message({
|
||||
message: resp.msg,
|
||||
type: 'error',
|
||||
duration: 5 * 1000
|
||||
})
|
||||
}
|
||||
})
|
||||
.catch(function(error) {
|
||||
console.log('err' + error) // for debug
|
||||
errorCallback && errorCallback(error)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default RequestUtil
|
||||
```
|
||||
|
||||
jQuery版本如下:
|
||||
|
||||
```js
|
||||
var RequestUtil = {
|
||||
/**
|
||||
* 请求接口
|
||||
* @param method 接口名,如:goods.get,goods.get
|
||||
* @param version 版本号,如:1.0
|
||||
* @param data 请求数据,json格式
|
||||
* @param callback 成功回调
|
||||
* @param errorCallback 失败回调
|
||||
*/
|
||||
post: function(method, version, data, callback, errorCallback) {
|
||||
$.ajax({
|
||||
url: 'http://localhost:8081/api' // 网关url
|
||||
, type: 'post'
|
||||
, headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
|
||||
, data: {
|
||||
method: method,
|
||||
version: version,
|
||||
biz_content: JSON.stringify(data)
|
||||
}
|
||||
,success:function(response){
|
||||
var resp = response.result
|
||||
var code = resp.code
|
||||
// 成功,网关正常且业务正常
|
||||
if (code === '10000' && !resp.sub_code) {
|
||||
callback(resp)
|
||||
} else {
|
||||
// 报错
|
||||
alert(resp.msg);
|
||||
}
|
||||
}
|
||||
, error: function(error) {
|
||||
errorCallback && errorCallback(error)
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
jQuery调用示例:
|
||||
|
||||
```js
|
||||
$(function () {
|
||||
var data = {
|
||||
id: 1
|
||||
,name: '葫芦娃'
|
||||
}
|
||||
RequestUtil.post('alipay.story.get', '1.0', data, function (result) {
|
||||
console.log(result)
|
||||
});
|
||||
})
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user