123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- # -*- codeing = utf-8 -*-
- # @Time : 2022/11/4 16:22
- # @Author : Clown
- # @File : EcCRMtest.py
- # @Software : PyCharm
- import requests
- import hashlib
- import time
- import csv
- import json
- import pandas as pd
- def get_sign(app_id, app_secret, timestamp):
- '''
- :param app_id: 应用ip 914890189023739904
- :param app_secret: 应用密钥 Ur2K41t71RrxYn7eWhN
- :param timestamp: 时间戳 毫秒
- :return: 加密密文
- '''
- str = 'appId=' + app_id + '&appSecret=' + app_secret + '&timeStamp=' + timestamp
- hl = hashlib.md5()
- hl.update(str.encode(encoding='utf-8'))
- return hl.hexdigest().upper()
- #查询招商信息,followUserId可输入人名及id
- def getUserInfo(corpid,sign,timestamp,followUserId):
- #获取组织架构信息,主要是对应上id和人名
- url = 'https://open.workec.com/v2/org/struct/info'
- params = 'needUser=true'
- headers = {'Content-Type': 'application/json',
- 'X-Ec-Cid': corpid,
- 'X-Ec-Sign': sign,
- 'X-Ec-TimeStamp': timestamp}
- resp = requests.get(url,params=params,headers=headers).json()
- user_list = resp['data']['users']
- df = pd.DataFrame(user_list)
- try:
- #人名查询返回id
- userId = list(df[df['userName']==followUserId]['userId'])[0]
- # print(type(userId))
- except:
- try:
- userId = list (df[df['userId'] == followUserId]['userName'])[0]
- except:
- userId = 'Null'
- return userId
- def addCustomers(corpid,sign,timestamp,customerList):
- #新增客户
- #来源渠道备注:47625为400电话,
- #customerList = [{}]
- url = 'https://open.workec.com/v2/customer/addCustomer'
- headers = {'Content-Type': 'application/json',
- 'X-Ec-Cid': corpid,
- 'X-Ec-Sign': sign,
- 'X-Ec-TimeStamp': timestamp}
- json_params = {'optUserId':17409174,
- 'list':customerList,
- 'notify':False,
- 'repeat':False,
- 'supply_detail':False}
- resp = requests.post(url,headers=headers,json=json_params).json()
- failureList = resp['data']['failureList']
- successIdList = resp['data']['successIdList']
- if len(successIdList) == 1:
- print('ok')
- crmId = str(successIdList[0]['crmId'])
- elif len(failureList) == 1:
- print('error')
- crmId = 'returnNull'
- return crmId
- #更新客户跟进人
- def updateCsFollower(corpid,sign,timestamp,crmId,followUserIds):
- url = 'https://open.workec.com/v2/customer/change/user'
- headers = {'Content-Type': 'application/json',
- 'X-Ec-Cid': corpid,
- 'X-Ec-Sign': sign,
- 'X-Ec-TimeStamp': timestamp}
- json_params = {'optUserId': 17409174,
- 'crmIds': crmId,
- 'followUserId': followUserIds}
- resp = requests.post (url, headers=headers, json=json_params).json ()
- success = resp['data']['success']
- return success
- #判断是否有人跟进
- def selectHSCInfo(corpid,sign,timestamp,mobile):
- url = 'https://open.workec.com/v2/customer/queryList'
- headers = {'Content-Type': 'application/json',
- 'X-Ec-Cid': corpid,
- 'X-Ec-Sign': sign,
- 'X-Ec-TimeStamp': timestamp}
- # json_params = {"optType":{"mobile":str(mobile)},"pageNo":1,"pageSize":200}
- json_params = {"mobile": str (mobile)}
- try:
- resp = requests.post (url, headers=headers, json=json_params).json ()
- # crmId
- crmId = resp['data']['list'][0]['crmId']
- crmName = resp['data']['list'][0]['name']
- # 无人跟进时existResult返回0
- followUserId = resp['data']['list'][0]['followUserId']
- if followUserId == 0:
- #最后跟进人id及名字
- lastFollowerId = resp['data']['list'][0]['lastFollowUserId']
- lastFollowerName = getUserInfo (corpid, sign, timestamp, lastFollowerId)
- if lastFollowerName == 'Null':
- # 跟进人已离职
- existResult = {'result':'hscout','followerName':'','crmId':crmId,'crmName':crmName}
- else:
- #跟进人在职
- existResult = {'result': 'hscin', 'followerName': lastFollowerName, 'crmId': crmId,'crmName':crmName}
- else:
- existResult = {'result': 'no', 'followerName': followUserId, 'crmId': crmId,'crmName':crmName}
- except:
- #如果参数传入错误返回Null
- existResult = {'result': 'error', 'followerName': '', 'crmId': 'selectHSCInfoError'}
- return existResult
- #查询客户
- def selectCustomers(corpid,sign,timestamp,mobile):
- url = 'https://open.workec.com/v2/customer/queryExist'
- headers = {'Content-Type': 'application/json',
- 'X-Ec-Cid': corpid,
- 'X-Ec-Sign': sign,
- 'X-Ec-TimeStamp': timestamp}
- json_params = {'mobile':str(mobile)}
- resp = requests.post (url, headers=headers, json=json_params).json ()
- results = resp['data'][0]['list']
- if len(results) == 0:
- existResult = {'result':'ok','followerName':'','crmId':''}
- else:
- existResult = selectHSCInfo(corpid,sign,timestamp,mobile)
- return existResult
- def runAdd(customers_info_dict_list):
- corpid = '17409173'
- timestamp = str (int (round (time.time () * 1000)))
- app_id = '914890189023739904'
- app_secret = 'Ur2K41t71RrxYn7eWhN'
- sign = get_sign (app_id, app_secret, str (timestamp))
- retrunResult = runSelect(customers_info_dict_list) #existResult = {'result':'ok','followerName':'','crmId':''}
- result = retrunResult['result']
- if result == 'ok':
- try:
- followUsername = customers_info_dict_list[0]['followUserId']
- followUserIds = getUserInfo (corpid, sign, timestamp, followUsername)
- customers_info_dict_list[0]['followUserId'] = followUserIds
- crmId = addCustomers (corpid, sign, timestamp, customers_info_dict_list)
- except Exception as e:
- print ('input Info Error!')
- crmId = 'serverNull'
- elif result == 'hscin':
- #更新公海里的客户至私海,最后一次跟进人在职
- followUsername = retrunResult['followerName']
- crmId = retrunResult['crmId']
- followUserIds = getUserInfo (corpid, sign, timestamp, followUsername)
- success = updateCsFollower (corpid, sign, timestamp, crmId, followUserIds)
- if success == 1:
- crmId = crmId
- else:
- crmId = 'serverNull'
- elif result == 'hscout':
- # 更新公海里的客户至私海,最后一次跟进人离职
- crmId = retrunResult['crmId']
- followUsername = customers_info_dict_list[0]['followUserId']
- followUserIds = getUserInfo (corpid, sign, timestamp, followUsername)
- success = updateCsFollower (corpid, sign, timestamp, crmId, followUserIds)
- if success == 1:
- crmId = crmId
- else:
- crmId = 'serverNull'
- elif result == 'no':
- crmId = retrunResult['crmId']
- else:
- crmId = 'serverNull'
- return crmId
- # 查询客户标签
- def selectCustomerLabel(corpid,sign,timestamp,crmId,labelsDict):
- url = 'https://open.workec.com/v2/customer/queryLabel'
- headers = {'Content-Type': 'application/json',
- 'X-Ec-Cid': corpid,
- 'X-Ec-Sign': sign,
- 'X-Ec-TimeStamp': timestamp}
- # json_params = {"optType":{"mobile":str(mobile)},"pageNo":1,"pageSize":200}
- json_params = {"crmIds": str (crmId)}
- try:
- resp = requests.post (url, headers=headers, json=json_params).json ()
- data = resp['data'][0]
- labelIds = data['labelIds']
- labelIds = labelIds.split(',')
- labelMsgl = []
- for labelId in labelIds:
- labelInfo = labelsDict[labelId]
- labelGroupName = labelInfo['labelGroupName']
- labelName = labelInfo['labelName']
- labelMsg = f'【{labelGroupName}-{labelName}】'
- labelMsgl.append(labelMsg)
- labelMsgS = ','.join(labelMsgl)
- result = 'ok'
- except (IndexError):
- result = 'ok'
- labelMsgS = '【无标签】'
- except:
- result = 'error'
- labelMsgS = '【selectCustomerLabel-无响应】'
- labelMsgS = {'result':result,'label_msgs':labelMsgS}
- return labelMsgS
- # 查询所有标签
- def selectLabels(corpid,sign,timestamp):
- url = 'https://open.workec.com/v2/config/getLabelInfo'
- headers = {'Content-Type': 'application/json',
- 'X-Ec-Cid': corpid,
- 'X-Ec-Sign': sign,
- 'X-Ec-TimeStamp': timestamp}
- json_params = {"groupValue": ''}
- try:
- resp = requests.get (url, headers=headers, json=json_params).json ()
- dict_out = {}
- for i in resp['data']:
- labelGroupId = i['labelGroupId']
- labelGroupName = i['labelGroupName']
- labelList = i['labelList']
- for label in labelList:
- labelId = label['labelId']
- labelName = label['labelName']
- dict_out[str(labelId)] = {'labelName':labelName,'labelGroupId':labelGroupId,'labelGroupName':labelGroupName}
- except:
- dict_out = {'0':''}
- return dict_out
- def selectCustomerLabelByCrmId(crmId):
- '''
- 返回3种结果,有标签和无标签,及报错
- '''
- corpid = '17409173'
- timestamp = str (int (round (time.time () * 1000)))
- app_id = '914890189023739904'
- app_secret = 'Ur2K41t71RrxYn7eWhN'
- try:
- sign = get_sign (app_id, app_secret, str (timestamp))
- labelsDict = selectLabels (corpid, sign, timestamp)
- labelMsgS = selectCustomerLabel (corpid, sign, timestamp, crmId, labelsDict)
- except:
- labelMsgS = {'result':'error','label_msgs':'【selectCustomerLabelByCrmId-无响应】'}
- return labelMsgS
- def runSelect(mobile_info_dict):
- '''
- 返回4种结果:ok表示新增、hscin表示在公海且最后一次跟进人在职、hscout表示在公海且最后一次跟进人离职、no表示在私海
- '''
- corpid = '17409173'
- timestamp = str (int (round (time.time () * 1000)))
- app_id = '914890189023739904'
- app_secret = 'Ur2K41t71RrxYn7eWhN'
- sign = get_sign (app_id, app_secret, str (timestamp))
- try:
- mobile = mobile_info_dict['mobile']
- existResult = selectCustomers (corpid, sign, timestamp, mobile)
- except:
- print ('input Info Error!')
- existResult= {'result': 'error', 'followerName': '', 'crmId': 'selectCustomersError'}
- return existResult
- '''
- @app.route ('/addcustomers',methods=['GET', 'POST'])
- def addcustomers():
- if request.method == 'POST':
- form_data = request.get_data (as_text=True)
- form_data = json.loads(form_data)
- customers_info_dict_list = [{"followUserId": form_data['followUserId'],
- "mobile": form_data['mobile'],
- "name": form_data['name'],
- 'memo': form_data['memo']}]
-
- print(customers_info_dict)
- else:
- form_data = ''
- return form_data
- '''
- if __name__ == '__main__':
- corpid = '17409173'
- timestamp = str (int (round (time.time () * 1000)))
- app_id = '914890189023739904'
- app_secret = 'Ur2K41t71RrxYn7eWhN'
- sign = get_sign (app_id, app_secret, str (timestamp))
- if 1==0:
- list_a = [{
- "followUserId": '谢总',
- "mobile": "13923899520",
- "name": "测试数据请忽略",
- 'memo': '400热线'
- }]
- json.dumps (list_a,ensure_ascii=False)
- # runAdd (list_a)
- print (getUserInfo (corpid, sign, timestamp, '谢总'))
- # mobile_info_dict = {'mobile':'13923899520'}
- # print (selectHSCInfo (corpid, sign, timestamp, mobile_info_dict['mobile']))
- # print(runSelect(mobile_info_dict))
- url = 'http://124.222.188.59:63307/selectcustomers'
- json_params = {'mobile':'13923899520'}
- resp = requests.post(url,json=json_params).text
- print(resp)
- else:
- crmId = '6567111994'
- # a = selectCustomerLabelByCrmId(crmId)
- # print(a)
- url = 'http://124.222.188.59:63307/selectcustomerlabelbycrmid'
- json_params = {"crmId": "6567111994"}
- resp = requests.post (url, json=json_params).text
- print (resp)
- followUserIds = getUserInfo(corpid, sign, timestamp, '张磊')
- mobile = '13834859678'
- a = selectCustomers(corpid, sign, timestamp, mobile)
- print(followUserIds,a)
- followUserIds = '19710025'
- updateCsFollower(corpid, sign, timestamp, crmId, followUserIds)
|