query_db.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import pymongo
  2. import pymysql
  3. from urllib import parse
  4. import time
  5. import requests
  6. import json
  7. import datetime
  8. import hashlib
  9. from requests.auth import HTTPBasicAuth
  10. DEBUG = False
  11. def user_info(query):
  12. if DEBUG:
  13. # 设置数据库连接
  14. connection = pymysql.connect(
  15. host="114.55.0.7",
  16. port=3306,
  17. user='root',
  18. password='yfkj@6020',
  19. database="smartfarming"
  20. )
  21. else:
  22. # 设置数据库连接
  23. connection = pymysql.connect(
  24. host='8.136.98.49',
  25. user='root',
  26. port=61882,
  27. password='yfkj@6020',
  28. database='smartfarming'
  29. )
  30. try:
  31. cursor = connection.cursor()
  32. # 使用模糊查询(如 '%query%')进行设备名称匹配
  33. sql_query = "SELECT uid, user_type FROM sa_device_user where user_type in (2,4) and real_name = %s"
  34. cursor.execute(sql_query,(query, ))
  35. result = cursor.fetchall()
  36. # 提取设备名称并返回
  37. if result:
  38. print(result)
  39. uid,user_type = result[0][0],result[0][1]
  40. if user_type == 2:
  41. rek = {"user_dealer": uid}
  42. else:
  43. rek = {"owner_uid": uid}
  44. return rek
  45. else:
  46. return {}
  47. except Exception as e:
  48. print(e.args)
  49. return {}
  50. finally:
  51. cursor.close()
  52. connection.close()
  53. def bigata_mongo():
  54. if DEBUG:
  55. user = parse.quote_plus("root")
  56. passwd = parse.quote_plus("yfkj@6020")
  57. myclient = pymongo.MongoClient("mongodb://{0}:{1}@114.55.0.7:27017/".format(user,passwd))
  58. else:
  59. user = parse.quote_plus("root")
  60. passwd = parse.quote_plus("yfkj@6020")
  61. myclient = pymongo.MongoClient("mongodb://{0}:{1}@8.136.98.49:57017/".format(user,passwd))
  62. return myclient.smartfarming
  63. def sim_query_new(iccid):
  64. finally_response = {"status": None, "total": -1, "used": -1, "expire": 0, "iccid": iccid, "code": True, "msg": f"卡号:{iccid}, 查询失败,请联系管理员!"}
  65. if not iccid:
  66. return {"code": True, "msg": "未传ICCID"}
  67. # 企鹏
  68. qp_appid = "102420177762"
  69. qp_app_secret = "6397d7e6a56589f1d93284e9800493e1"
  70. qp_now = lambda: int(round(time.time() * 1000))
  71. qp_data = f"appid={qp_appid}&iccid={iccid}&timestamp={qp_now()}{qp_app_secret}"
  72. sign = hashlib.sha256(qp_data.encode('utf-8')).hexdigest()
  73. data = {
  74. "appid": qp_appid,
  75. "iccid": iccid,
  76. "timestamp":qp_now(),
  77. "sign":sign
  78. }
  79. qp_url = "https://api.simboss.com/2.0/device/detail"
  80. try:
  81. qp_ret = requests.post(qp_url, data=data)
  82. qp_response = json.loads(qp_ret.text)
  83. qp_code = qp_response.get("code")
  84. except Exception as e:
  85. qp_code = "1"
  86. if qp_code == "0":
  87. qp_data = qp_response.get("data")
  88. qp_st_dict = {
  89. "TEST_READY_NAME": "可测试",
  90. "INVENTORY_NAME": "库存",
  91. "ACTIVATION_READY_NAME": "可激活",
  92. "ACTIVATED_NAME": "已激活",
  93. "DEACTIVATED_NAME": "已停卡",
  94. "RETIRED_NAME": "已销卡",
  95. "PURGED_NAME": "已清除",
  96. }
  97. return {
  98. "status": qp_st_dict.get(qp_data.get("deviceStatus")) ,
  99. "total": qp_data.get("totalDataVolume"),
  100. "used": qp_data.get("dataUsage") if qp_data.get("dataUsage") else 0,
  101. "expire": qp_data.get("expireDate"),
  102. "iccid": iccid,
  103. "package": qp_data.get("iratePlanName"),
  104. "company": "企鹏",
  105. "code": False
  106. }
  107. else:
  108. # 合宙
  109. hz_url = 'http://sim.brlink.cn/api/open/iotcard/card'
  110. hz_appkey = "iaO2DKgS8KdlnVgU"
  111. hz_appsecret = "qzKgO4sBdzMrjRwv9H22S9ufepNv8Hl5ehPqkYVD31DCICjyKwqUdj7zihQQKfgx"
  112. try:
  113. hz_ret = requests.post(
  114. hz_url,
  115. json={'iccid':iccid},
  116. auth=HTTPBasicAuth(hz_appkey, hz_appsecret),
  117. timeout=(5,10)
  118. )
  119. hz_response = json.loads(hz_ret.text)
  120. hz_code = hz_response.get("code")
  121. except Exception as e:
  122. hz_code = 1
  123. if hz_code == 0:
  124. hz_data = hz_response.get("data")
  125. hz_st_dict = {
  126. "0": "未知",
  127. "1": "测试期",
  128. "2": "沉默期",
  129. "3": "使用中",
  130. "4": "停机",
  131. "5": "停机保号",
  132. "6": "预销号",
  133. "7": "销号",
  134. }
  135. # 有效期
  136. expiry_date = hz_data.get("expiry_date")
  137. return {
  138. "status": hz_st_dict.get(str(hz_data.get("account_status", 0))),
  139. "total": hz_data.get("data_plan"),
  140. "used": hz_data.get("data_usage") if hz_data.get("data_usage") else 0,
  141. "expire": datetime.datetime.fromtimestamp(expiry_date).strftime("%Y-%m-%d %H:%M:%S"),
  142. "iccid": iccid,
  143. "company": "合宙",
  144. "code": False
  145. }
  146. else:
  147. # 信金
  148. xj_userid = 137
  149. xj_now = int(time.time())
  150. xj_apikey = "2ae46f82215a187ba22656db9a9848d8"
  151. xj_params = f"userId={xj_userid}&apikey={xj_apikey}&times={xj_now}"
  152. md5 = hashlib.md5()
  153. md5.update(xj_params.encode('utf-8'))
  154. xj_sign = (md5.hexdigest()).upper()
  155. url = f"http://hywx.xjict.com:32040/api/v1/getChaxun?userId={xj_userid}&cardno={iccid}&times={xj_now}&sign={xj_sign}"
  156. try:
  157. xj_ret = requests.get(
  158. url,
  159. timeout=(10,30)
  160. )
  161. xj_response = json.loads(xj_ret.text)
  162. xj_code = xj_response.get("code")
  163. except Exception as e:
  164. xj_code = 1
  165. if xj_code == 0:
  166. xj_st_dict = {
  167. "0":"未知",
  168. "00":"正常",
  169. "01":"单向停机",
  170. "02":"停机",
  171. "03":"预销号",
  172. "04":"销号/拆机",
  173. "05":"过户",
  174. "06":"休眠",
  175. "07":"待激",
  176. "08":"已停用",
  177. "09":"库存",
  178. "10":"已失效",
  179. "11":"违章停机",
  180. "12":"挂失",
  181. "13":"用户报停",
  182. "14":"测试",
  183. "99":"不存在"
  184. }
  185. try:
  186. xj_data = xj_response.get("data")
  187. autoname = xj_data.get("autoname")
  188. nub = None
  189. nub = 0
  190. if "M" in autoname:
  191. nubs = autoname.split("M")
  192. if nubs:
  193. nub = nubs[0]
  194. if nub and nub.isdigit():
  195. nub = int(nub)
  196. if "G" in autoname:
  197. nubs = autoname.split("G")
  198. if nubs:
  199. nub = nubs[0]
  200. if nub and nub.isdigit():
  201. nub = int(nub) * 1024
  202. return {
  203. "status": xj_st_dict.get(xj_data.get("state")),
  204. "total": nub,
  205. "used": int(xj_data.get("used")/1024) if xj_data.get("used") else 0,
  206. "expire": xj_data.get("expired_at"),
  207. "iccid": iccid,
  208. "package": autoname,
  209. "company": "信金",
  210. "code": False
  211. }
  212. except Exception as e:
  213. return finally_response
  214. return finally_response