日常需要备份一些配置信息,写接口存到数据库太麻烦,干脆直接用iserver的接口存到数据库属性表里面吧。专门对GIS属性表的rest服务属性表增删改查做了一些示例,用到了fetch和jQuery。
另外把服务刷新和服务发布示例也加入进来了,和属性表增删改查没关系。
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="application/javascript" src="lib/jquery.js"></script>
<script type="application/javascript" src="lib/fetch.js"></script>
</head>
<body>
<input type="button" id="loginiserver" value="登录iserver">
<input type="button" id="applytoken" value="申请token(给发布服务使用)">
<input type="button" id="publishpgws" value="发布Postgresql工作空间为数据服务和地图服务">
<input type="button" id="refreshpgws" value="刷新Postgresql工作空间">
<br/>
<br/>
<input type="button" id="addtable" value="新建属性表">
<input type="button" id="deletetable" value="删除属性表">
<input type="button" id="getfiled" value="获取属性表字段">
<input type="button" id="getfiledmetadata" value="获取属性表字段元数据">
<input type="button" id="addfiled" value="新增字段">
<input type="button" id="putfiled" value="修改/新增字段">
<input type="button" id="deletefiled" value="删除字段">
<br/>
<input type="button" id="addrecord" value="新增记录(多条)">
<input type="button" id="putrecord" value="修改记录(多条)">
<input type="button" id="sqldeleterecord" value="SQL删除记录">
<input type="button" id="iddeleterecord" value="IDS删除记录">
<input type="button" id="idquery" value="ID查询记录">
<input type="button" id="sqlqueryall" value="SQL查询所有记录">
<input type="button" id="sqlquery" value="SQL查询符合条件的记录">
<script type="text/javascript">
let mytoken = "";//存储申请的token
// 账号登录iserver
$(document).ready(function () {
$("#loginiserver").click(function () {
fetch('http://localhost:8090/iserver/services/security/login.rjson', {
method: 'post',
body: JSON.stringify({
"username": "admin",
"password": "password",
"rememberme": "true"
}
),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
// 申请token令牌(可以供发布服务使用)
$(document).ready(function () {
$("#applytoken").click(function () {
fetch('http://localhost:8090/iserver/services/security/tokens.rjson', {
method: 'post',
body: JSON.stringify({
"username": "admin",
"password": "password",
"clientType": "NONE", //客户端类型:无限制
"expiration": "1000000" //token时长,单位为分钟
}
),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.text();
}).then(function (data) {
mytoken = data;
console.log(data);
})
});
});
// 发布postgresql工作空间为数据服务和地图服务,可编辑
//https://blog.csdn.net/shanshanqwertyuiop/article/details/127820542
$(document).ready(function () {
$("#publishpgws").click(function () {
let url = "http://localhost:8090/iserver/manager/workspaces.rjson?token=" + mytoken
console.log(url);
fetch(url, {
method: 'post',
body: JSON.stringify({
"datasourceInfos": [],
"workspaceConnectionInfo": "type=PGSQL;server=localhost:5432;database=postgres;name=tdzx_backup;password=postgres;username=postgres;driver=pgSQL Server",
"servicesTypes": ["RESTDATA", "RESTMAP"],
"isDataEditable": true,
"mapEditable": false,
"mapDpi": "0",
"isMultiInstance": false,
"instanceCount": 0,
"dataProviderDelayCommitSetting": {"enabled": false}
}),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
//刷新指定工作空间里的指定数据集。当通过iDesktop新增数据后需要刷新才会在iserver里显示。
// https://blog.csdn.net/supermapsupport/article/details/126247441?spm=1001.2014.3001.5501
$(document).ready(function () {
$("#refreshpgws").click(function () {
let url = "http://localhost:8090/iserver/manager/workspaces/workspacereload.rjson?token=" + mytoken
// console.log(url);
fetch(url, {
method: 'post',
body: JSON.stringify({
"workspaceConnectionInfo": "type=PGSQL;server=localhost:5432;database=postgres;name=tdzx_backup;password=postgres;username=postgres;driver=pgSQL Server",
"datasets": {
//"postgres": ["tt2"] //刷新指定数据源下的指定数据集。此行为空时会强制刷新所有数据源,但不计算几何图形边界。
},
"isForce": true //推荐true,强制刷新
}),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
$(document).ready(function () {
$("#addtable").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/datasources/postgres/datasets.json', {
method: 'post',
body: JSON.stringify({"datasetName": "tt2", "datasetType": "TABULAR"}),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
//删除数据集。对数据集地址执行删除操作。
$(document).ready(function () {
$("#deletetable").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/datasources/postgres/datasets/tt2.rjson', {
method: 'delete'
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
//获取字段名
$(document).ready(function () {
$("#getfiled").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/datasources/postgres/datasets/tt2/fields.rjson', {
method: 'get',
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
//获取字段元数据信息。url增加了参数。
$(document).ready(function () {
$("#getfiledmetadata").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/datasources/postgres/datasets/tt2/fields.rjson?returnAll=true', {
method: 'get'
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
/*新增文本型字段;8个参数为必填参数;修改字段元数据信息只需把post换成put*/
$(document).ready(function () {
$("#addfiled").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/datasources/postgres/datasets/tt2/fields.rjson', {
method: 'post',
body: JSON.stringify({
name: "text2", //字段名
caption: "text2", //别名
maxLength: 2500, //字段长度
type: "WTEXT", //文本型字段
defaultValue: "",
isRequired: false,
isSystemField: false,
isZeroLengthAllowed: true,
}),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
//修改/新增字段,url结尾包含了需要修改的字段名text2,当不存在该字段时为新增字段。
$(document).ready(function () {
$("#putfiled").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/datasources/postgres/datasets/tt2/fields/text2.rjson', {
method: 'put',
body: JSON.stringify({
name: "text2", //字段名
caption: "text2", //别名
maxLength: 2500, //字段长度
type: "WTEXT", //文本型字段
defaultValue: "",
isRequired: false,
isSystemField: false,
isZeroLengthAllowed: true,
}),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
//删除text2字段。url里包含了text2。
$(document).ready(function () {
$("#deletefiled").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/datasources/postgres/datasets/tt2/fields/text2.rjson', {
method: 'delete',
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
//新增记录:如下为同时新增两条记录
$(document).ready(function () {
$("#addrecord").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/datasources/postgres/datasets/tt2/features.rjson', {
method: 'post',
body: JSON.stringify([
{
"fieldNames": [
"text1",
"text2",
],
"fieldValues": [
"text1记录1",
"text2记录3",
]
},
{
"fieldNames": [
"text1",
"text2"
],
"fieldValues": [
"text1记录2",
"text2记录3"
],
}
]),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
//修改记录:如下为同时修改两条记录,需要指定ID号进行修改,url指定_method=PUT。
$(document).ready(function () {
$("#putrecord").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/datasources/postgres/datasets/tt2/features.rjson?_method=PUT', {
method: 'post',
body: JSON.stringify([
{
"ID": 1,
"fieldNames": [
"text1",
"text2",
],
"fieldValues": [
"text1记录1修改",
"text2记录1修改",
]
},
{
"ID": 2,
"fieldNames": [
"text1",
"text2"
],
"fieldValues": [
"text1记录2修改",
"text2记录2修改"
],
}
]),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
// 通过IDS删除记录
$(document).ready(function () {
$("#iddeleterecord").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/datasources/postgres/datasets/tt2/features.rjson?_method=DELETE&deleteMode=IDS', {
method: 'post',
body: JSON.stringify([5, 6, 7]),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
// 通过SQL语句删除记录
/*SQL写法1:"SmID <= 3"
* Sql写法2:多条件查询 "SmID <= 100"&&"TEXT1='text1记录1'"
* SQL写法3:“SMID < 2” 等同于 "SmID %26lt; 2" 等同于 "SmID < 2" (为转义写法,部分情况不识别特殊符号时需要转义。)*/
$(document).ready(function () {
$("#sqldeleterecord").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/datasources/postgres/datasets/tt2/features.rjson?_method=DELETE&deleteMode=SQL', {
method: 'post',
body: JSON.stringify({"attributeFilter": "SmID <= 100" && "TEXT1='text1记录1'"}),
// body: JSON.stringify({"attributeFilter":"SmID %26lt; 2"}),//SMID<2
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
//IDS查询。点线面需要返回geojson时地址结尾用.geojson。属性表用.rjson
$(document).ready(function () {
$("#idquery").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/featureResults.rjson?returnContent=true', {
method: 'post',
body: JSON.stringify({
"getFeatureMode": "ID",
"datasetNames": ["postgres:tt2"],
"ids": [1, 17]
}
),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
//SQL查询返回所有要素。点线面需要返回geojson时地址结尾用.geojson。属性表用.rjson
$(document).ready(function () {
$("#sqlqueryall").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/featureResults.rjson?returnContent=true', {
method: 'post',
body: JSON.stringify({
getFeatureMode: "SQL",
datasetNames: ["postgres:tt2"],
fromIndex: 0,
toIndex: -1,//全部返回的话必须是-1
maxFeatures: 2000000,//返回数量必须大于等于总数
queryParameter: {},//不能省略
hasGeometry: false
}
),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
//SQL查询返回符合条件的要素。点线面需要返回geojson时地址结尾用.geojson。属性表用.rjson
$(document).ready(function () {
$("#sqlquery").click(function () {
fetch('http://localhost:8090/iserver/services/data-tdzx_backup/rest/data/featureResults.rjson?returnContent=true', {
method: 'post',
body: JSON.stringify({
getFeatureMode: "SQL",
datasetNames: ["postgres:tt2"], //数据源:数据集
fromIndex: 0,
toIndex: -1,//全部返回的话必须是-1
maxFeatures: 2000000,//返回数量必须大于等于总数
hasGeometry: false,
queryParameter: {
orderBy: "MYNUM,SMID",//根据哪几个字段进行排序。不排序则此处值为null。排序字段必须为数值型。
ids: null, //id数组限制条件
name: "tt2", //数据集或者图层名称
attributeFilter: "SmID <= 100" && "TEXT1='text1记录1'", //SQL查询条件
linkItems: null,
joinItems: null,
fields: null //需要返回的字段数组限制条件
},
}
),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
}).then(function (data) {
return data.json();
}).then(function (data) {
console.log(data);
})
});
});
</script>
</body>
</html>