dbController.py0512 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import pymysql;
  2. from DBUtils.PooledDB import PooledDB;
  3. pool = PooledDB(pymysql,20,host='localhost',user='root',passwd='root',db='crt',port=3306,charset="utf8");
  4. import logApi
  5. logger = logApi.logger
  6. import os
  7. from ini_op import Config;
  8. import json
  9. import time
  10. import requests
  11. base_dir = os.path.dirname(os.path.abspath(__file__))
  12. config = Config(base_dir+"/config.ini");
  13. tokenApiUrl = config.get("baseconf", "tokenApiUrl");
  14. class DbController():
  15. def getToken(self):
  16. token = ""
  17. headers = {"Content-Type": "application/json",
  18. "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8"}
  19. param = {
  20. "grant_type": config.get("baseconf", "grant_type"),
  21. "client_id": config.get("baseconf", "client_id"),
  22. "client_secret": config.get("baseconf", "client_secret"),
  23. "tenant_id": config.get("baseconf", "tenant_id"),
  24. "timestamp": int(int(round(time.time() * 1000))),
  25. "nonce": config.get("baseconf", "nonce")
  26. }
  27. try:
  28. result = requests.post(tokenApiUrl, json=param, headers=headers, timeout=10)
  29. data = json.loads(result.text)
  30. errorCode = data["errcode"];
  31. if errorCode == '0':
  32. token = data["data"]["access_token"];
  33. except:
  34. logger.error("获取token失败 ")
  35. return token;
  36. def getBussinessNum(self,cerf_id):
  37. business_num = "";
  38. try:
  39. self.conn = pool.connection();
  40. self.cursor = self.conn.cursor()
  41. sql = "select business_num from querycustomer where cerf_id='"+cerf_id+"' order by biz_id desc";
  42. res = self.cursor.execute(sql);
  43. data = self.cursor.fetchone();
  44. if len(data)>0:
  45. business_num = data[0]
  46. except:
  47. logger.error(" getBussinessNum error")
  48. self.cursor.close();
  49. self.conn.close();
  50. return business_num;