EcCRMtestV1.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. # -*- codeing = utf-8 -*-
  2. # @Time : 2022/11/4 16:22
  3. # @Author : Clown
  4. # @File : EcCRMtest.py
  5. # @Software : PyCharm
  6. import requests
  7. import hashlib
  8. import time
  9. import csv
  10. import json
  11. import pandas as pd
  12. def get_sign(app_id, app_secret, timestamp):
  13. '''
  14. :param app_id: 应用ip 914890189023739904
  15. :param app_secret: 应用密钥 Ur2K41t71RrxYn7eWhN
  16. :param timestamp: 时间戳 毫秒
  17. :return: 加密密文
  18. '''
  19. str = 'appId=' + app_id + '&appSecret=' + app_secret + '&timeStamp=' + timestamp
  20. hl = hashlib.md5()
  21. hl.update(str.encode(encoding='utf-8'))
  22. return hl.hexdigest().upper()
  23. #查询招商信息,followUserId可输入人名及id
  24. def getUserInfo(corpid,sign,timestamp,followUserId):
  25. #获取组织架构信息,主要是对应上id和人名
  26. url = 'https://open.workec.com/v2/org/struct/info'
  27. params = 'needUser=true'
  28. headers = {'Content-Type': 'application/json',
  29. 'X-Ec-Cid': corpid,
  30. 'X-Ec-Sign': sign,
  31. 'X-Ec-TimeStamp': timestamp}
  32. resp = requests.get(url,params=params,headers=headers).json()
  33. user_list = resp['data']['users']
  34. df = pd.DataFrame(user_list)
  35. try:
  36. #人名查询返回id
  37. userId = list(df[df['userName']==followUserId]['userId'])[0]
  38. # print(type(userId))
  39. except:
  40. try:
  41. userId = list (df[df['userId'] == followUserId]['userName'])[0]
  42. except:
  43. userId = 'Null'
  44. return userId
  45. def addCustomers(corpid,sign,timestamp,customerList):
  46. #新增客户
  47. #来源渠道备注:47625为400电话,
  48. #customerList = [{}]
  49. url = 'https://open.workec.com/v2/customer/addCustomer'
  50. headers = {'Content-Type': 'application/json',
  51. 'X-Ec-Cid': corpid,
  52. 'X-Ec-Sign': sign,
  53. 'X-Ec-TimeStamp': timestamp}
  54. json_params = {'optUserId':17409174,
  55. 'list':customerList,
  56. 'notify':False,
  57. 'repeat':False,
  58. 'supply_detail':False}
  59. resp = requests.post(url,headers=headers,json=json_params).json()
  60. failureList = resp['data']['failureList']
  61. successIdList = resp['data']['successIdList']
  62. if len(successIdList) == 1:
  63. print('ok')
  64. crmId = str(successIdList[0]['crmId'])
  65. elif len(failureList) == 1:
  66. print('error')
  67. crmId = 'returnNull'
  68. return crmId
  69. #更新客户跟进人
  70. def updateCsFollower(corpid,sign,timestamp,crmId,followUserIds):
  71. url = 'https://open.workec.com/v2/customer/change/user'
  72. headers = {'Content-Type': 'application/json',
  73. 'X-Ec-Cid': corpid,
  74. 'X-Ec-Sign': sign,
  75. 'X-Ec-TimeStamp': timestamp}
  76. json_params = {'optUserId': 17409174,
  77. 'crmIds': crmId,
  78. 'followUserId': followUserIds}
  79. resp = requests.post (url, headers=headers, json=json_params).json ()
  80. success = resp['data']['success']
  81. return success
  82. #判断是否有人跟进
  83. def selectHSCInfo(corpid,sign,timestamp,mobile):
  84. url = 'https://open.workec.com/v2/customer/queryList'
  85. headers = {'Content-Type': 'application/json',
  86. 'X-Ec-Cid': corpid,
  87. 'X-Ec-Sign': sign,
  88. 'X-Ec-TimeStamp': timestamp}
  89. # json_params = {"optType":{"mobile":str(mobile)},"pageNo":1,"pageSize":200}
  90. json_params = {"mobile": str (mobile)}
  91. try:
  92. resp = requests.post (url, headers=headers, json=json_params).json ()
  93. # crmId
  94. crmId = resp['data']['list'][0]['crmId']
  95. crmName = resp['data']['list'][0]['name']
  96. # 无人跟进时existResult返回0
  97. followUserId = resp['data']['list'][0]['followUserId']
  98. if followUserId == 0:
  99. #最后跟进人id及名字
  100. lastFollowerId = resp['data']['list'][0]['lastFollowUserId']
  101. lastFollowerName = getUserInfo (corpid, sign, timestamp, lastFollowerId)
  102. if lastFollowerName == 'Null':
  103. # 跟进人已离职
  104. existResult = {'result':'hscout','followerName':'','crmId':crmId,'crmName':crmName}
  105. else:
  106. #跟进人在职
  107. existResult = {'result': 'hscin', 'followerName': lastFollowerName, 'crmId': crmId,'crmName':crmName}
  108. else:
  109. existResult = {'result': 'no', 'followerName': followUserId, 'crmId': crmId,'crmName':crmName}
  110. except:
  111. #如果参数传入错误返回Null
  112. existResult = {'result': 'error', 'followerName': '', 'crmId': 'selectHSCInfoError'}
  113. return existResult
  114. #查询客户
  115. def selectCustomers(corpid,sign,timestamp,mobile):
  116. url = 'https://open.workec.com/v2/customer/queryExist'
  117. headers = {'Content-Type': 'application/json',
  118. 'X-Ec-Cid': corpid,
  119. 'X-Ec-Sign': sign,
  120. 'X-Ec-TimeStamp': timestamp}
  121. json_params = {'mobile':str(mobile)}
  122. resp = requests.post (url, headers=headers, json=json_params).json ()
  123. results = resp['data'][0]['list']
  124. if len(results) == 0:
  125. existResult = {'result':'ok','followerName':'','crmId':''}
  126. else:
  127. existResult = selectHSCInfo(corpid,sign,timestamp,mobile)
  128. return existResult
  129. def runAdd(customers_info_dict_list):
  130. corpid = '17409173'
  131. timestamp = str (int (round (time.time () * 1000)))
  132. app_id = '914890189023739904'
  133. app_secret = 'Ur2K41t71RrxYn7eWhN'
  134. sign = get_sign (app_id, app_secret, str (timestamp))
  135. retrunResult = runSelect(customers_info_dict_list) #existResult = {'result':'ok','followerName':'','crmId':''}
  136. result = retrunResult['result']
  137. if result == 'ok':
  138. try:
  139. followUsername = customers_info_dict_list[0]['followUserId']
  140. followUserIds = getUserInfo (corpid, sign, timestamp, followUsername)
  141. customers_info_dict_list[0]['followUserId'] = followUserIds
  142. crmId = addCustomers (corpid, sign, timestamp, customers_info_dict_list)
  143. except Exception as e:
  144. print ('input Info Error!')
  145. crmId = 'serverNull'
  146. elif result == 'hscin':
  147. #更新公海里的客户至私海,最后一次跟进人在职
  148. followUsername = retrunResult['followerName']
  149. crmId = retrunResult['crmId']
  150. followUserIds = getUserInfo (corpid, sign, timestamp, followUsername)
  151. success = updateCsFollower (corpid, sign, timestamp, crmId, followUserIds)
  152. if success == 1:
  153. crmId = crmId
  154. else:
  155. crmId = 'serverNull'
  156. elif result == 'hscout':
  157. # 更新公海里的客户至私海,最后一次跟进人离职
  158. crmId = retrunResult['crmId']
  159. followUsername = customers_info_dict_list[0]['followUserId']
  160. followUserIds = getUserInfo (corpid, sign, timestamp, followUsername)
  161. success = updateCsFollower (corpid, sign, timestamp, crmId, followUserIds)
  162. if success == 1:
  163. crmId = crmId
  164. else:
  165. crmId = 'serverNull'
  166. elif result == 'no':
  167. crmId = retrunResult['crmId']
  168. else:
  169. crmId = 'serverNull'
  170. return crmId
  171. # 查询客户标签
  172. def selectCustomerLabel(corpid,sign,timestamp,crmId,labelsDict):
  173. url = 'https://open.workec.com/v2/customer/queryLabel'
  174. headers = {'Content-Type': 'application/json',
  175. 'X-Ec-Cid': corpid,
  176. 'X-Ec-Sign': sign,
  177. 'X-Ec-TimeStamp': timestamp}
  178. # json_params = {"optType":{"mobile":str(mobile)},"pageNo":1,"pageSize":200}
  179. json_params = {"crmIds": str (crmId)}
  180. try:
  181. resp = requests.post (url, headers=headers, json=json_params).json ()
  182. data = resp['data'][0]
  183. labelIds = data['labelIds']
  184. labelIds = labelIds.split(',')
  185. labelMsgl = []
  186. for labelId in labelIds:
  187. labelInfo = labelsDict[labelId]
  188. labelGroupName = labelInfo['labelGroupName']
  189. labelName = labelInfo['labelName']
  190. labelMsg = f'【{labelGroupName}-{labelName}】'
  191. labelMsgl.append(labelMsg)
  192. labelMsgS = ','.join(labelMsgl)
  193. result = 'ok'
  194. except (IndexError):
  195. result = 'ok'
  196. labelMsgS = '【无标签】'
  197. except:
  198. result = 'error'
  199. labelMsgS = '【selectCustomerLabel-无响应】'
  200. labelMsgS = {'result':result,'label_msgs':labelMsgS}
  201. return labelMsgS
  202. # 查询所有标签
  203. def selectLabels(corpid,sign,timestamp):
  204. url = 'https://open.workec.com/v2/config/getLabelInfo'
  205. headers = {'Content-Type': 'application/json',
  206. 'X-Ec-Cid': corpid,
  207. 'X-Ec-Sign': sign,
  208. 'X-Ec-TimeStamp': timestamp}
  209. json_params = {"groupValue": ''}
  210. try:
  211. resp = requests.get (url, headers=headers, json=json_params).json ()
  212. dict_out = {}
  213. for i in resp['data']:
  214. labelGroupId = i['labelGroupId']
  215. labelGroupName = i['labelGroupName']
  216. labelList = i['labelList']
  217. for label in labelList:
  218. labelId = label['labelId']
  219. labelName = label['labelName']
  220. dict_out[str(labelId)] = {'labelName':labelName,'labelGroupId':labelGroupId,'labelGroupName':labelGroupName}
  221. except:
  222. dict_out = {'0':''}
  223. return dict_out
  224. def selectCustomerLabelByCrmId(crmId):
  225. '''
  226. 返回3种结果,有标签和无标签,及报错
  227. '''
  228. corpid = '17409173'
  229. timestamp = str (int (round (time.time () * 1000)))
  230. app_id = '914890189023739904'
  231. app_secret = 'Ur2K41t71RrxYn7eWhN'
  232. try:
  233. sign = get_sign (app_id, app_secret, str (timestamp))
  234. labelsDict = selectLabels (corpid, sign, timestamp)
  235. labelMsgS = selectCustomerLabel (corpid, sign, timestamp, crmId, labelsDict)
  236. except:
  237. labelMsgS = {'result':'error','label_msgs':'【selectCustomerLabelByCrmId-无响应】'}
  238. return labelMsgS
  239. def runSelect(mobile_info_dict):
  240. '''
  241. 返回4种结果:ok表示新增、hscin表示在公海且最后一次跟进人在职、hscout表示在公海且最后一次跟进人离职、no表示在私海
  242. '''
  243. corpid = '17409173'
  244. timestamp = str (int (round (time.time () * 1000)))
  245. app_id = '914890189023739904'
  246. app_secret = 'Ur2K41t71RrxYn7eWhN'
  247. sign = get_sign (app_id, app_secret, str (timestamp))
  248. try:
  249. mobile = mobile_info_dict['mobile']
  250. existResult = selectCustomers (corpid, sign, timestamp, mobile)
  251. except:
  252. print ('input Info Error!')
  253. existResult= {'result': 'error', 'followerName': '', 'crmId': 'selectCustomersError'}
  254. return existResult
  255. '''
  256. @app.route ('/addcustomers',methods=['GET', 'POST'])
  257. def addcustomers():
  258. if request.method == 'POST':
  259. form_data = request.get_data (as_text=True)
  260. form_data = json.loads(form_data)
  261. customers_info_dict_list = [{"followUserId": form_data['followUserId'],
  262. "mobile": form_data['mobile'],
  263. "name": form_data['name'],
  264. 'memo': form_data['memo']}]
  265. print(customers_info_dict)
  266. else:
  267. form_data = ''
  268. return form_data
  269. '''
  270. if __name__ == '__main__':
  271. corpid = '17409173'
  272. timestamp = str (int (round (time.time () * 1000)))
  273. app_id = '914890189023739904'
  274. app_secret = 'Ur2K41t71RrxYn7eWhN'
  275. sign = get_sign (app_id, app_secret, str (timestamp))
  276. if 1==0:
  277. list_a = [{
  278. "followUserId": '谢总',
  279. "mobile": "13923899520",
  280. "name": "测试数据请忽略",
  281. 'memo': '400热线'
  282. }]
  283. json.dumps (list_a,ensure_ascii=False)
  284. # runAdd (list_a)
  285. print (getUserInfo (corpid, sign, timestamp, '谢总'))
  286. # mobile_info_dict = {'mobile':'13923899520'}
  287. # print (selectHSCInfo (corpid, sign, timestamp, mobile_info_dict['mobile']))
  288. # print(runSelect(mobile_info_dict))
  289. url = 'http://124.222.188.59:63307/selectcustomers'
  290. json_params = {'mobile':'13923899520'}
  291. resp = requests.post(url,json=json_params).text
  292. print(resp)
  293. else:
  294. crmId = '6567111994'
  295. # a = selectCustomerLabelByCrmId(crmId)
  296. # print(a)
  297. url = 'http://124.222.188.59:63307/selectcustomerlabelbycrmid'
  298. json_params = {"crmId": "6567111994"}
  299. resp = requests.post (url, json=json_params).text
  300. print (resp)
  301. followUserIds = getUserInfo(corpid, sign, timestamp, '张磊')
  302. mobile = '13834859678'
  303. a = selectCustomers(corpid, sign, timestamp, mobile)
  304. print(followUserIds,a)
  305. followUserIds = '19710025'
  306. updateCsFollower(corpid, sign, timestamp, crmId, followUserIds)