大家都知道,云星空是支持Web API的。在云社区搜查 Python 、API 两个关键词,就会出现相应的显示如下。
感谢Cand,给我们提供了这么好的内容。
我这段时间是做数据迁移的,把WISE数据迁移到云星空。迁完是已审核状态的,有时候想删除整个业务对象的单据,要进去业务对象,然后反审核,然后删除。我觉得有点慢,所以就用Python来执行这些动作了,用Python写代码,的确是人生苦短,我用python。
代码如下,可以与上面的帖子进行对比验证,Python版本是3.7.5import requests
import json
acctid="" #数据中心
username="" #你的账号
password="" #账号密码
IP=r"" #登录网址
#构造一个类,可以在类中执行登录、保存、查看等操作,cookies就可以共享了,不用重复登录了。
class CloudClient():
cookies =requests.cookies
#headers可以模拟任意浏览器的登录
#使用raw string,不用考虑转义
headers = { "referer": r"https://kdsrvc.opstest.ik3cloud.com/k3cloud/html5/content/themes/kdcss.standard.min.css?ver=7.5.1658.5",
"user-agent": r"Mozilla/5.0(Windows NT 6.1;WOW64) AppleWebKit/537.36(KHTML, like Gecko) Chrome/74.0.3729.169 Safari / 537.36" }
IP = r""
#绑定执行方法,
dict_method = {"Login":r"Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc",
"Save":r"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc",
"View":r"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.View.common.kdsvc",
"Submit":r"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc",
"Audit":r"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc",
"UnAudit":r"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.UnAudit.common.kdsvc",
"StatusConvert":r"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.StatusConvert.common.kdsvc",
"ExecuteBillQuery":r"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc",
"BatchSave":r"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.BatchSave.common.kdsvc",
"Delete":"Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Delete.common.kdsvc"
}
#防止用户最后输入了/,如有,就去掉
def setIp(self,IP):
if IP[-1]==r'/':
IP=IP[:-1]
self.IP= IP
def login(self,acctid,username,password,lcid=2052):
cookies = None
#写入登录信息
login_data = {"acctid": acctid, "username": username, "password": password, "lcid": lcid}
# 获得执行url
login_url = self.IP +r'/'+ self.dict_method["Login"]
login_response = requests.post(url=login_url,headers=self.headers , data=login_data)
self.cookies = login_response.cookies
#methodName说明是执行什么操作
def method(self,methodName,post_data):
url = self.IP + r'/' + self.dict_method[methodName]
response = requests.post(url=url, data=post_data,cookies=self.cookies,headers=self.headers)
return response
#该函数先查询单据内码,然后反审这些单据,然后删除。
#FormId是业务对象的FormId
#FieldKey是内码标识,由不同的业务对象确定
#FilterString是过滤条件
#该函数把这个删除抽象成只需要输入3个字符串了
def BaseUnAuditAndDelete(FormId,FieldKey,FilterString):
Cloudclient = CloudClient()
Cloudclient.setIp(IP)
Cloudclient.login(acctid, username, password)
# 通过单据查询,获得FID列表
BillQuery_data = {
"data": json.dumps({"FormId": FormId, "FieldKeys": FieldKey, "FilterString": FilterString})
}
response = Cloudclient.method("ExecuteBillQuery", BillQuery_data)
#操作成功的话,list_FId是列表类型,json.loads来解析
list_FId = json.loads(response.text)
print("Query:"+str(list_FId))
#把列表转换为字符串,去掉最后的 ,号
str_Fid = ""
for Fid in list_FId:
str_Fid = str_Fid + "," + str(Fid[0])
str_Fid = str_Fid[1:] # 通过得FID列表,反审单据
UnAudit_data = {"FormId": FormId, "Data": json.dumps({"Ids": str_Fid})}
response = Cloudclient.method("UnAudit", UnAudit_data)
# 通过得FID列表,删除单据
Delete_data = {
"FormId": FormId, "Data": json.dumps({"Ids": str_Fid})
}
response = Cloudclient.method("Delete", Delete_data) print(Delete_data)
#输出最后的信息
print(json.dumps(response.text).encode("utf-8").decode("unicode_escape"))
if __name__=="__main__":
dict_Form ={}
dict_Form["BM"] = {"FormId":"BD_Department","FieldKey":"FDEPTID","FilterString":""}
dict_Form["JLDW"] = {"FormId": "BD_UNIT", "FieldKey": "FUNITID", "FilterString": "FIsSystemSet=0"}
dict_Form["JLDWZ"] = {"FormId": "BD_UNITGROUP", "FieldKey": "FUNITGROUPID", "FilterString": "FIsSystemSet=0"}
dict_Form["YG"] = {"FormId": "BD_Empinfo", "FieldKey": "FMasterId", "FilterString": ""}
dict_Form["BDFS"] = {"FormId": "FA_ALTERMODE", "FieldKey": "FID", "FilterString": "FIsSysPreset=0"}
dict_Form["JSFS"] = {"FormId": "BD_SETTLETYPE", "FieldKey": "FID", "FilterString": "FIsSysPreset=0"}
#如果在列表中添加其他Key,那就可以自动删除多个业务对的单据了。
for key in ["JLDW"]:
Exe = dict_Form[key]
BaseUnAuditAndDelete(Exe["FormId"],Exe["FieldKey"],Exe["FilterString"])