webapi.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import requests, json
  2. from hytest.common import *
  3. from cfg import cfg
  4. from requests.packages import urllib3
  5. #存放公用方法
  6. # 存储 全局共享 数据
  7. GSTORE = {}
  8. class APIMgr():
  9. #打印https请求与消息
  10. def printRequest(self,req):
  11. if req.body==None:
  12. msgBody=''
  13. else:
  14. msgBody=req.body
  15. self.ui.outputWindow.append(
  16. '{}\n{}\n{}\n\n{}'.format(
  17. '\n\n-------发送请求--------',
  18. req.method+''+req.url,
  19. '\n'.join('{}:{}'.format(k,v) for k,v in req.headers.items()),
  20. msgBody,
  21. ))
  22. # 打印http相应消息的函数
  23. def printResponse(self, response):
  24. print('\n\n----- https response begin -----')
  25. print(response.status_code)
  26. # print(response.headers)
  27. for k, v in response.headers.items():
  28. print(f'{k}:{v}')
  29. print(response.content.decode('utf8'))
  30. print('----- https response end-----\n\n')
  31. headers = {
  32. "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36 Edg/114.0.1823.67"
  33. }
  34. #headers设置为全局变量
  35. GSTORE['headers'] = headers
  36. # session对象设置为全局变量
  37. s = requests.Session()
  38. GSTORE['s'] = s
  39. #登录接口
  40. def mgr_login(self, phone='18211989987', password='123456',useproxies=False):
  41. headers=GSTORE['headers']
  42. s = GSTORE['s']
  43. if useproxies:
  44. self.s.proxies.update({'http':'127.0.0.1:8888'})
  45. response = self.s.post(f"{cfg.target_host}/phone/login",headers=headers,data=
  46. {
  47. 'reqType': 'phoneLogin',
  48. 'isAutoLogin': 'false',
  49. 'phone':phone,
  50. 'password':password
  51. })
  52. self.printResponse(response)
  53. # 把response对象返回出去
  54. return response
  55. def mgr_logout(self):
  56. url = f"{cfg.target_host}/front/signOut"
  57. headers = GSTORE['headers']
  58. s = GSTORE['s']
  59. s.post(url=url, headers=headers)
  60. # self.printResponse(res)
  61. # return res
  62. #招标搜索
  63. def bidsearch(self, keywords="建筑", publishtime="fiveyear", selectType="content"):
  64. #使用全局变量
  65. headers = GSTORE['headers']
  66. params={"keywords":keywords,"publishtime":publishtime,"timeslot":"","area":"","subtype":"","minprice":"","maxprice":"","industry":"","buyerclass":"","buyertel":"","winnertel":"","selectType":selectType,"notkey":"","fileExists":"0","city":"","searchGroup":"0","searchMode":"0","wordsMode":"0","additionalWords":""}
  67. #保存session
  68. session = self.s
  69. response = session.post(f"{cfg.target_host}/jylab/supsearch/index.html", headers=headers, params=params)
  70. self.printResponse(response)
  71. return response
  72. # 企业搜索
  73. def enterpriseSearch(self, match="北京剑鱼信息技术有限公司河南分公司", matchType="A", pageSize="10", pageNum="0"):
  74. # 使用全局变量
  75. headers = GSTORE['headers']
  76. # 保存session
  77. session = self.s
  78. response = session.post(f"{cfg.target_host}/publicapply/enterpriseSearch/doQuery", headers=headers, data={
  79. 'match': match,
  80. 'matchType': matchType,
  81. 'pageSize': pageSize,
  82. 'pageNum': pageNum
  83. })
  84. self.printResponse(response)
  85. return response
  86. # 供应搜索
  87. def supplySearch(self, keywords="PH计", searchType="title", province="", city="", time="", status="0",
  88. pageSize=50, pageIndex=1):
  89. headers = GSTORE['headers']
  90. headers['Content-Type'] = 'application/json' # 添加Content-Type头部
  91. url = f"{cfg.target_host}/jyinfo/supplySearch"
  92. data = {
  93. "keywords": keywords,
  94. "searchType": searchType,
  95. "province": province,
  96. "city": city,
  97. "time": time,
  98. "status": status,
  99. "pageSize": pageSize,
  100. "pageIndex": pageIndex
  101. }
  102. session=self.s
  103. response = session.post(url=url, headers=headers, data=json.dumps(data))
  104. self.printResponse(response)
  105. return response
  106. #采购单位搜索
  107. def buyersousuo(self, buyerName, province=[], city=[], buyerClass=[], isCheckFollow=True, isCheckReceive=True,
  108. isContact=0, pageSize=10,pageNum=1):
  109. url = f"{cfg.target_host}/jyapi/jybx/buyer/eType/buyerList"
  110. data = {
  111. "buyerName": buyerName,
  112. "province": province,
  113. "city": city,
  114. "buyerClass": buyerClass,
  115. "isCheckFollow": isCheckFollow,
  116. "isCheckReceive": isCheckReceive,
  117. "isContact": isContact,
  118. "pageSize": pageSize,
  119. "pageNum":pageNum
  120. }
  121. response = requests.post(url=url, json=data, headers=self.headers)
  122. self.printResponse(response)
  123. return response
  124. #接口数据传值常用三种方式:urlencoded---params,键值对---data,json格式---json
  125. #获取推送记录接口
  126. def push_list(self):
  127. headers=GSTORE['headers']
  128. s = GSTORE['s']
  129. response = self.s.post(f"{cfg.target_host}/jyapi/jybx/subscribe/fType/list",headers=headers,json=
  130. {"pageNum": 1, "pageSize": 50, "format": "table", "area": "", "selectTime": "all", "city": "", "buyerClass": "",
  131. "subtype": "", "industry": "", "keyWords": "", "fileExists": "", "price": "", "source": "", "exportNum": "",
  132. "vt": ""})
  133. return response
  134. #不登录招标搜索
  135. def notloggedin_search(self, keyword='科技', publishtime='thisyear', selectType='content,title'):
  136. headers = GSTORE['headers']
  137. params={"keywords": keyword , "publishtime": publishtime, "timeslot": "", "area": "", "subtype": "",
  138. "minprice": "", "maxprice": "", "industry": "", "buyerclass": "", "buyertel": "", "winnertel": "",
  139. "selectType": selectType, "notkey": "", "fileExists": "0", "city": "", "searchGroup": "0",
  140. "searchMode": "0", "wordsMode": "0", "additionalWords": ""}
  141. response = requests.post(f"{cfg.target_host}/jylab/supsearch/index.html", headers=headers, params=params)
  142. response.raise_for_status() # 如果请求失败,会抛出异常
  143. return response
  144. #不登录采购单位搜索
  145. def notloggedin_buysearch(self,keyword):
  146. headers = GSTORE['headers']
  147. s = GSTORE['s']
  148. data = {"buyerName":keyword,"province":[],"city":[],"buyerClass":[],"isCheckFollow":False,"isCheckReceive":False,"isContact":0,"pageSize":10,"pageNum":1}
  149. response =s.post(f"{cfg.target_host}/jyapi/jybx/buyer/eType/buyerList", headers=headers, json=data)
  150. # response.raise_for_status() # 如果请求失败,会抛出异常
  151. return response
  152. #消息中心列表
  153. def get_messagelist(self):
  154. headers = GSTORE['headers']
  155. s = GSTORE['s']
  156. response=s.post(f"{cfg.target_host}/jymessageCenter/messageList",headers=headers,params=
  157. {"needFilter":"1","msgType":"0","isRead":"-1","offset":"1","pageSize":"5"})
  158. return response
  159. # 用户中台
  160. def get_userCenter(self):
  161. hearders = GSTORE['headers']
  162. s = GSTORE['s']
  163. response = s.post(f"{cfg.target_host}/userCenter/workDesktop/menuInfo", headers=hearders)
  164. return response
  165. # 我的订单
  166. def get_myOrder(self):
  167. hearders = GSTORE['headers']
  168. s = GSTORE['s']
  169. params = {
  170. "type": 0,
  171. "pageNum": 1,
  172. "fromPage": "pc",
  173. "page_size": 10
  174. }
  175. response = s.post(f"{cfg.target_host}/subscribepay/orderListDetails/myOrder", headers=hearders, params=params)
  176. return response
  177. apimgr = APIMgr()