import requests
import json
import datetime
# 常量需自定义
ID =
SECRET =
SECRET1 =
AGENTID =
APPID =
############################# 获取token ###############################
def get_token(_secret):
url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}".format(ID,_secret)
headers = {'Content-Type': 'application/json'}
body = {}
response = requests.post(url, data = json.dumps(body), headers = headers)
res = (dict(json.loads(response.text))).get("access_token")
#print("token: "+res)
return res
############################# 获取部门列表 ###############################
def get_simplelist_all(_token,_department_id):
url = "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={}&id={}".format(_token,_department_id)
headers = {'Content-Type': 'application/json'}
body = {}
response = requests.post(url, data = json.dumps(body), headers = headers)
res = dict(json.loads(response.text))
#print(res)
return res
############################# 获取子部门ID列表 ###############################
def get_simplelist(_token,_department_id):
url = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token={}&id={}".format(_token,_department_id)
headers = {'Content-Type': 'application/json'}
body = {}
response = requests.post(url, data = json.dumps(body), headers = headers)
res = dict(json.loads(response.text))
#print(res)
return res.get("department_id")
############################# 创建部门 ###############################
def create_department(_token,_name,_department_id):
url = "https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token={}&id={}".format(_token,_department_id)
headers = {'Content-Type': 'application/json'}
body = {}
response = requests.post(url, data = json.dumps(body), headers = headers)
res = dict(json.loads(response.text))
print(res)
return res
############################# 获取userid ###############################
def get_user_id(_token,_tele):
url = "https://qyapi.weixin.qq.com/cgi-bin//user/getuserid?access_token={}".format(_token)
headers = {'Content-Type': 'application/json'}
body = {'mobile':_tele}
response = requests.post(url, data = json.dumps(body), headers = headers)
res = dict(json.loads(response.text))
print(res)
print(res.get("userid"))
return res.get("userid")
############################# 获取部门成员 ###############################
def get_department_users(_token,_id):
url = "https://qyapi.weixin.qq.com/cgi-bin/user/list?access_token={}&department_id={}".format(_token,_id)
headers = {'Content-Type': 'application/json'}
body = {}
response = requests.post(url, data = json.dumps(body), headers = headers)
res = dict(json.loads(response.text)).get("userlist")
#print(res)
return res
############################# 获取全部成员 ###############################
def get_all_user(_token):
# 遍历部门 + 成员信息
dList = get_simplelist(token,'')
for d in dList:
dId = d.get('id')
dUsers = get_department_users(_token,dId)
for u in dUsers:
print(u)
# 部门详情
############################# 发送消息 ###############################
def send_msg(_token,_userid):
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}&debug=1".format(_token)
headers = {'Content-Type': 'application/json'}
body = {
"touser" : _userid,
"agentid" : AGENTID,
"msgtype" : "miniprogram_notice",
"miniprogram_notice" : {
"appid": APPID,
"title": "营销员奖励通知",
"description":datetime.datetime.now().strftime("%Y年%m月%d日 %H:%M"),
"content_item": [
{
"key": "购买人",
"value": "xxx"
},
{
"key": "商品名",
"value": "xxxxx商品"
},
{
"key": "预计奖励",
"value": "xxxx元"
},
]
},
"enable_id_trans": 0,
"enable_duplicate_check": 0,
"duplicate_check_interval": 1800
}
response = requests.post(url, data = json.dumps(body), headers = headers)
res = dict(json.loads(response.text))
print(res)
##############################################################################