api.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. # update: 2024.7.14
  2. """
  3. 中冶宝钢智慧研究院接口
  4. """
  5. from urllib import parse
  6. import requests
  7. import time
  8. import traceback
  9. import sys
  10. import importlib
  11. # sys.path.append(r'C:\mccbts-ar-py\module-method-python') # for 10.10.61.22
  12. sys.path.append(r'C:\mccbts-ar-py\module-method-python') # for 10.10.61.22
  13. # sys.path.append(r'D:\mccbts-ar-py\module-method-python') # for 192.168.103.34
  14. # methods = importlib.import_module(f"libraries.base_original")
  15. methods = importlib.import_module(f"xlib")
  16. class API(object):
  17. # def __init__(self, username='System001', password='Swdx@143',
  18. def __init__(self, username='', password=''):
  19. """
  20. 接口地址: https://eaiops.mccbts.com.cn/cigpf-gateway/cigpf-auth/login
  21. 接口说明: 用户认证接口,mcctest/Mcc@1234
  22. 接口地址: http://ycpx04.mccbts.com.cn/cnki-gw/expert/expert/getExpertList
  23. 接口说明:获取专家列表
  24. 接口地址: http://10.8.0.151:8000/datachange/release/interface
  25. 接口说明:该接口可以获取用户数据、部门数据
  26. """
  27. # --- define ---
  28. self.cigpf_api_service = f"https://eaiops.mccbts.com.cn"
  29. self.cnki_api_service = f"http://ycpx04.mccbts.com.cn"
  30. self.datachange_api_service = f"http://10.8.0.151:8000"
  31. self.LIST_USER_TOKEN = 'FA460AD97660475BBD8E1CF31BC7A49B'
  32. self.LIST_DEPT_TOKEN = '4412B9724D254E38B57014CF99EB0312'
  33. def get_user_info(self, username, password):
  34. """
  35. 获取用户信息
  36. return {
  37. 'success': True,
  38. 'message': '操作成功!',
  39. 'code': 200,
  40. 'result': {
  41. 'id': '1399259233832144898',
  42. 'username': 'mcctest',
  43. 'realname': '测试账号',
  44. 'avatar': '/cigpfupload/05a5b1ab84a446c4a2e126da89b0c0af.png',
  45. 'birthday': None,
  46. 'sex': None,
  47. 'thirdId': '',
  48. 'thirdType': 'qt',
  49. 'email': None,
  50. 'phone': '18210635895',
  51. 'orgCode': '1',
  52. 'orgCodeTxt': None,
  53. 'status': 1,
  54. 'delFlag': 0,
  55. 'workNo': '99999',
  56. 'post': None,
  57. 'telephone': None,
  58. 'createBy': '',
  59. 'createTime': None,
  60. 'updateBy': '张宇',
  61. 'updateTime': '2023-11-07 06:00:09',
  62. 'activitiSync': 1,
  63. 'userIdentity': 2,
  64. 'departIds': '1000051C',
  65. 'relTenantIds': None,
  66. 'clientId': None,
  67. 'userType': '2'
  68. },
  69. 'timestamp': 1699666081555
  70. }
  71. """
  72. url = f"{self.cigpf_api_service}/cigpf-gateway/cigpf-auth/login"
  73. # data = {
  74. # 'userName': 'mcctest', # 用户
  75. # 'passWord': 'Mcc@1234', # 密码
  76. # }
  77. data = {
  78. 'userName': username, # 用户
  79. 'passWord': password, # 密码
  80. }
  81. # print(f"API.get_user_info:data: {data}")
  82. print('API.get_user_info.url:', url)
  83. response = requests.post(url, json=data)
  84. if response.status_code > 300:
  85. print('API.get_user_info.result:', response.status_code)
  86. print('API.get_user_info.detail:', response.text)
  87. return {}
  88. else:
  89. print('API.get_user_info.result:', response.status_code)
  90. return response.json()
  91. def get_expert_type_tree_list(self, q=''):
  92. """
  93. 获取专家结构树
  94. """
  95. url = f"https://ycpx04.mccbts.com.cn/cnki-gw/expert/expertType/getExpertTypeTreeList"
  96. data = {
  97. 'keyword': q
  98. }
  99. response = requests.post(url, json=data)
  100. # print('API.test.url:', url)
  101. # print('API.test.data:', data)
  102. # print('API.test.code:', response.status_code)
  103. # print('API.test.text:', response.text)
  104. return response.json()
  105. def get_expert_list(self, page=1, rows=10):
  106. """
  107. 获取专家列表
  108. return {
  109. 'msg': '操作成功',
  110. 'code': 200,
  111. 'data': {
  112. 'total': 320,
  113. 'list': [
  114. {
  115. 'id': 407880669122629,
  116. 'expertId': 'b46f7f7e-2883-4281-ba92-0c72fb30a668',
  117. 'expertCode': '40886',
  118. 'expertName': '冯浩川',
  119. 'expertLogo': '',
  120. 'sex': 1,
  121. 'birthday': '1974-08-12 00:00:00',
  122. 'education': None,
  123. 'degree': None,
  124. 'colleges': '',
  125. 'politicalOutlook': '',
  126. 'academicTitle': '',
  127. 'expertTypeId': 'a0846382-dc33-44f5-990d-ea042ab90cf4',
  128. 'expertType': '工程机械组',
  129. 'workUnitOrCurrentPosition': '',
  130. 'professionalAndTechnicalFields': '',
  131. 'mainSocialPositions': '',
  132. 'personalHonors': '',
  133. 'project': [],
  134. 'brief': '',
  135. 'createTime': '2023-04-17 15:30:43',
  136. 'createUserId': '6867b1b3-024f-4065-b991-3ebd48df7b27',
  137. 'type': '',
  138. 'companyId': '45b9801f-d2ff-4886-8076-7f7d8b403e37',
  139. 'company': '协力生产分公司',
  140. 'departmentId': '301e6551-dfd6-4e3e-b7f5-be36e14f84a1',
  141. 'department': '协力_机械维修中心_管理组'
  142. }
  143. ]
  144. }
  145. }
  146. """
  147. url = f"{self.cnki_api_service}/cnki-gw/expert/expert/getExpertList"
  148. data = {
  149. 'pageDto': {
  150. 'page': page,
  151. 'rows': rows
  152. }
  153. }
  154. # print(f"API.get_user_info:data: {data}")
  155. print('API.get_expert_list.url:', url)
  156. response = requests.post(url, json=data)
  157. # --- check ---
  158. if response.status_code > 300:
  159. print('API.get_expert_list.result:', response.status_code)
  160. print('API.get_expert_list.detail:', response.text)
  161. return []
  162. # --- check ---
  163. data = response.json()
  164. if data.get('code') != 200:
  165. return []
  166. # --- check ---
  167. if not data:
  168. return []
  169. # print(data.get('data').get('total'))
  170. return data.get('data').get('list')
  171. def get_staff_list(self):
  172. """
  173. 获取员工列表
  174. return [
  175. {
  176. 'STATUS': '1',
  177. 'ORG_CODE': '1603605',
  178. 'UPDATE_BY': 'HR',
  179. 'CREATE_BY': 'HR',
  180. 'DEPART_NAME': '山西_天车二区_硅钢班',
  181. 'DEL_FLAG': '0',
  182. 'CREATE_TIME': 1659117651000,
  183. 'UPDATE_TIME': 1659117651000,
  184. 'ID': '1001G1AR',
  185. 'DEPART_NAME_ABBR': '山西_天车二区_硅钢班',
  186. 'C1': '1',
  187. 'PARENT_ID': '1001G1AM'
  188. }
  189. ]
  190. """
  191. url = f"{self.datachange_api_service}/datachange/release/interface"
  192. headers = {'Chinasofti-Access-Token': self.LIST_USER_TOKEN}
  193. print('API.get_staff_list.url:', url)
  194. print('API.get_staff_list.headers:', headers)
  195. response = requests.get(url, headers=headers)
  196. # --- check ---
  197. if response.status_code > 300:
  198. print('API.get_staff_list.result:', response.status_code)
  199. print('API.get_staff_list.detail:', response.text)
  200. return '请求请求失败', f"code: {response.status_code}, response: {response.text}"
  201. # --- check ---
  202. data = response.json()
  203. if data.get('code') != 0:
  204. # print('API.get_staff_list.detail:', response.text)
  205. return '接口返回异常', f"请求地址: {url}, 请求方式: GET, 请求参数: {headers}, 返回结果: {response.text}"
  206. return data.get('data')
  207. def get_department_list(self):
  208. """
  209. 获取部门列表
  210. return [
  211. {
  212. 'ORG_ID': '10012B4J',
  213. 'REALNAME': '张天勤',
  214. 'POST': '电焊工',
  215. 'PHONE': '15921009690',
  216. 'SEX': 1,
  217. 'UPDATE_BY': 'HR',
  218. 'DEL_FLAG': 0,
  219. 'UPDATE_TIME': 1661492148000,
  220. 'WORK_NO2': '983260',
  221. 'STATUS': '1',
  222. 'PKID': 'C1924F31EE850472E050080A5C00BCBA',
  223. 'ORG_CODE': '153420410',
  224. 'CREATE_BY': 'HR',
  225. 'CREATE_TIME': 1620205014000,
  226. 'ID': '10000RBZ',
  227. 'WORK_NO': '983260',
  228. 'BIRTHDAY': '1967-02-09'
  229. }
  230. ]
  231. """
  232. url = f"{self.datachange_api_service}/datachange/release/interface"
  233. headers = {'Chinasofti-Access-Token': self.LIST_DEPT_TOKEN}
  234. print('API.get_department_list.url:', url)
  235. print('API.get_department_list.headers:', headers)
  236. response = requests.get(url, headers=headers)
  237. print('API.get_department_list.status_code:', response.status_code)
  238. print('API.get_department_list.text:', response.text)
  239. # --- check ---
  240. if response.status_code > 300:
  241. # print('API.get_department_list.result:', response.status_code)
  242. # print('API.get_department_list.detail:', response.text)
  243. return {'code': -1, 'data': response.text}
  244. # --- check ---
  245. data = response.json()
  246. if data.get('code') != 0:
  247. # print('API.get_department_list.code:', data.get('code'))
  248. # print('API.get_department_list.data:', response.text)
  249. return {'code': -1, 'data': response.text}
  250. return {'code': 0, 'data': data.get('data')}
  251. def test(self):
  252. """
  253. """
  254. url = f"http://ycpx04.mccbts.com.cn/cnki-gw/expert/expertType/getExpertTypeList"
  255. data = {
  256. 'keyword': 'xxx'
  257. }
  258. response = requests.post(url, json=data)
  259. print('API.test.url:', url)
  260. print('API.test.data:', data)
  261. print('API.test.code:', response.status_code)
  262. print('API.test.text:', response.text)
  263. # res = res.json()
  264. # data = {
  265. # 'count': len(res['data']),
  266. # 'data': res['data']
  267. # }
  268. if __name__ == '__main__':
  269. # --- init ---
  270. api = API()
  271. # --- test ---
  272. items = api.get_staff_list()
  273. # items = api.get_department_list()
  274. print(items)
  275. # --- test ---
  276. # items = api.get_expert_list(page=1, rows=100000000)
  277. # for item in items:
  278. # if 'c0dbc699-1640-43c2-8459-ffadbaa2d966' not in item.get('expertTypeId'):
  279. # continue
  280. # print(item)
  281. # --- test ---
  282. # items = api.get_expert_type_tree_list()
  283. # print(items)
  284. # --- test --- 获取一级公司
  285. # d1 = list()
  286. # items = api.get_department_list().get('data')
  287. # for item in items:
  288. # # if item.get('ORG_CODE') == '191':
  289. # # print(item)
  290. # # if '总部' in item.get('DEPART_NAME'):
  291. # # if '总部' in item.get('DEPART_NAME'):
  292. # # print(item)
  293. # if len(item.get('ORG_CODE')) == 3 and item.get('ORG_CODE')[0] == '1':
  294. # # if item.get('ORG_CODE') in ['3', '4', '6']:
  295. # # print(item)
  296. # # d1.append(item.get('DEPART_NAME'))
  297. # print(item.get('ORG_CODE'), item.get('DEPART_NAME'))
  298. # print(d1, len(d1))
  299. # --- test ---
  300. # items = api.get_expert_type_tree_list().get('data')
  301. # print(items[0]['children'])
  302. # for i in items[0]['children']:
  303. # print(len(i.get('children')), i.get('count'))
  304. # if i.get('children'):
  305. # for j in i.get('children'):
  306. # print(len(j.get('children')), j.get('count'))