188 lines
10 KiB
Python
188 lines
10 KiB
Python
"""客户联系我管理"""
|
||||
|
|
import os
|
|||
|
|
from datetime import datetime
|
|||
|
|
|
|||
|
|
import django
|
|||
|
|
import requests
|
|||
|
|
from pydantic import BaseModel, Field
|
|||
|
|
|
|||
|
|
from sixGuoDjango.settings import BASE_DIR
|
|||
|
|
from wechat_token.token工具包 import sixToken
|
|||
|
|
from wechat_数据库.用户数据库.小程序登陆用户 import weiuser
|
|||
|
|
from wechat_项目工具包.数据库的工具包.更新用户积分 import miniUser用户角色
|
|||
|
|
|
|||
|
|
os.environ.setdefault(key="DJANGO_SETTINGS_MODULE", value=f"{BASE_DIR.name}.settings")
|
|||
|
|
django.setup()
|
|||
|
|
|
|||
|
|
import pydash
|
|||
|
|
|
|||
|
|
from requests import post
|
|||
|
|
|
|||
|
|
six六果对象xiao, six是否新创建值 = weiuser.objects.update_or_create(unionid="六果",
|
|||
|
|
defaults={
|
|||
|
|
"user_IP地址": None, "user_昵称": "六果", "user_用户当前的等级": 0, "user_省份": "六果", "user_城市": "六果", "user_区县": "六果",
|
|||
|
|
"user_街道信息": "六果", "user_头像": "https://hudie.xiao.band/%E6%9C%AA%E5%91%BD%E5%90%8D.png", "user_绑定企微usrID账号": "六果", "user_角色权限": "六果", "external_userid": "六果",
|
|||
|
|
"parent_id": "六果", "mini_openid": "wxf612e825377328f0", "wechat_openid": "六果", "wecom_openid": "wwbe6369dc26a9d83a", "codeConfigId": "六果", "codeRemark": "六果",
|
|||
|
|
"codeQrCode": "https://hudie.xiao.band/mini.jpg",
|
|||
|
|
"user_企微角色权限": "六果", "user_绑定企微UNIONID": "六果"
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
|
|||
|
|
class wecom客户联系_联系我_管理(BaseModel):
|
|||
|
|
"""
|
|||
|
|
https://developer.work.weixin.qq.com/document/path/92228#%E9%99%84%E5%BD%95
|
|||
|
|
"""
|
|||
|
|
addres_省: str = Field(..., description="addres_省")
|
|||
|
|
address_市: str = Field(..., description="address_市")
|
|||
|
|
address_区县: str = Field(..., description="address_区县")
|
|||
|
|
|
|||
|
|
def 配置客户联系_联系我_方式(self, state企微二维码绑定的唯一值: str, userid_list: list[str]):
|
|||
|
|
"""
|
|||
|
|
通过API添加的「联系我」不会在管理端进行展示,每个企业可通过API最多配置50万个「联系我」。
|
|||
|
|
用户需要妥善存储返回的config_id,config_id丢失可能导致用户无法编辑或删除「联系我」。
|
|||
|
|
临时会话模式不占用「联系我」数量,但每日最多添加10万个,并且仅支持单人。
|
|||
|
|
临时会话模式的二维码,添加好友完成后该二维码即刻失效。
|
|||
|
|
请求方式:POST(HTTPS)
|
|||
|
|
请求地址:https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_contact_way?access_token=ACCESS_TOK
|
|||
|
|
:return:
|
|||
|
|
:rtype:
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
# wecom_token = "500hJr2pfJIa5vCJoUOK_zJRcKG_4fKdC_pilCgsEds7lQ3VZGlg1FN6iGDKZsMpUqZO4Di9BJfEMxk7V01yz5NYNwaekvby9vw3gZBw40uXr-YqlAVkP6ZRA0Wf6IkHfiYUTDQAk1
|
|||
|
|
# -hUANg42srD6f3AfooEi1ZbbmAvXRjsPa17HVgzxLP7NnGTZPbaTOZRU2Cr269GjxZhgx0dVHu1Q"
|
|||
|
|
wecom_token = sixToken.get_wecom_token()
|
|||
|
|
uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_contact_way?access_token={wecom_token}"
|
|||
|
|
state = "oy4Jw6Lyfyj1VvgZFodzq9oqJzHE"
|
|||
|
|
请求数据 = {
|
|||
|
|
"type": 1, # 联系方式类型,1-单人, 2-多人
|
|||
|
|
"scene": 2, # 场景,1-在小程序中联系,2-通过二维码联系
|
|||
|
|
"style": 1, # 否 在小程序中联系时使用的控件样式,详见附表
|
|||
|
|
"remark": "渠道客户", # 否 联系方式的备注信息,用于助记,不超过30个字符
|
|||
|
|
"skip_verify": True, # 否 外部客户添加时是否无需验证,默认为true
|
|||
|
|
"state": state企微二维码绑定的唯一值, # 是 区分业务用的;
|
|||
|
|
"user": userid_list,
|
|||
|
|
}
|
|||
|
|
data = requests.post(url=uri, json=请求数据).json()
|
|||
|
|
print(data)
|
|||
|
|
config_id = pydash.get(obj=data, path="config_id")
|
|||
|
|
qr_code = pydash.get(obj=data, path="qr_code")
|
|||
|
|
|
|||
|
|
def 获取企业已配置的_联系我_列表(self, start时间戳):
|
|||
|
|
"""
|
|||
|
|
获取六果公司已经创建的全部的添加我联系我二维码 列表数据🆔
|
|||
|
|
"""
|
|||
|
|
wecom_token = sixToken.get_wecom_token() # list_contact_way
|
|||
|
|
print(datetime(year=2026, month=1, day=1).timestamp())
|
|||
|
|
start时间戳 = int(datetime(year=2026, month=1, day=1).timestamp())
|
|||
|
|
uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list_contact_way?access_token={wecom_token}"
|
|||
|
|
data = post(url=uri, json={"start_time": start时间戳}).json()
|
|||
|
|
print(data, "=========")
|
|||
|
|
return data
|
|||
|
|
|
|||
|
|
def 获取企业已配置的_联系我_方式(self, config_id: str):
|
|||
|
|
"""
|
|||
|
|
获取企业已配置的_联系我_方式
|
|||
|
|
:param config_id:
|
|||
|
|
:type config_id:
|
|||
|
|
"""
|
|||
|
|
wecom_token = sixToken.get_wecom_token()
|
|||
|
|
uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_contact_way?access_token={wecom_token}"
|
|||
|
|
请求数据 = {"config_id": config_id}
|
|||
|
|
data = post(url=uri, json=请求数据).json()
|
|||
|
|
return data
|
|||
|
|
|
|||
|
|
def 更新企业已配置的_联系我_方式(self):
|
|||
|
|
"""
|
|||
|
|
更新企业配置的「联系我」二维码和「联系我」小程序按钮中的信息,如使用人员和备注等。
|
|||
|
|
请求方式:POST(HTTPS)
|
|||
|
|
请求地址:https://qyapi.weixin.qq.com/cgi-bin/externalcontact/update_contact_way?access_token=ACCESS_TOKEN
|
|||
|
|
"""
|
|||
|
|
wecom_token = sixToken.get_wecom_token()
|
|||
|
|
uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/update_contact_way?access_token={wecom_token}"
|
|||
|
|
# oy4Jw6A1T3WkKOOhMxcbQTDSM8rs
|
|||
|
|
请求示例数据 = {"config_id": "9b18a3ac534e236162a850f77bdd20d0", "remark": "渠道客户", "skip_verify": True, "style": 1, "state": "oy4Jw6A1T3WkKOOhMxcbQTDSM8rs", "type": 2,
|
|||
|
|
"user": ["YuanBao1975"],
|
|||
|
|
}
|
|||
|
|
data = post(url=uri, json=请求示例数据).json()
|
|||
|
|
print("更新企业已配置的_联系我_方式", data)
|
|||
|
|
|
|||
|
|
def 删除企业已配置的_联系我_方式(self, config_id: str):
|
|||
|
|
"""
|
|||
|
|
删除一个已配置的「联系我」二维码或者「联系我」小程序按钮。
|
|||
|
|
请求方式:POST(HTTPS)
|
|||
|
|
请求地址:https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_contact_way?access_token=ACCESS_TOKEN
|
|||
|
|
"""
|
|||
|
|
print(config_id, type(config_id))
|
|||
|
|
wecom_token = sixToken.get_wecom_token()
|
|||
|
|
uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_contact_way?access_token={wecom_token}"
|
|||
|
|
请求数据 = {"config_id": config_id}
|
|||
|
|
data = post(url=uri, json=请求数据).json()
|
|||
|
|
print(data, "==========")
|
|||
|
|
return data
|
|||
|
|
|
|||
|
|
def 结束临时会话(self):
|
|||
|
|
"""
|
|||
|
|
结束临时会话
|
|||
|
|
"""
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
def 结束语定义(self):
|
|||
|
|
"""
|
|||
|
|
结束语定义
|
|||
|
|
"""
|
|||
|
|
pass
|
|||
|
|
|
|||
|
|
def 最终处理完整的企微二维码信息(self, start时间戳):
|
|||
|
|
# 调用获取所有的 原先创建的 列表 得到 config_id
|
|||
|
|
config_id_list_data = self.获取企业已配置的_联系我_列表(start时间戳=start时间戳)
|
|||
|
|
contact_way = pydash.get(obj=config_id_list_data, path="contact_way")
|
|||
|
|
for configID in contact_way:
|
|||
|
|
config_id = pydash.get(obj=configID, path="config_id")
|
|||
|
|
data_详情数据 = self.获取企业已配置的_联系我_方式(config_id=config_id)
|
|||
|
|
qr_code = pydash.get(obj=data_详情数据, path="contact_way.qr_code")
|
|||
|
|
state = pydash.get(obj=data_详情数据, path="contact_way.state")
|
|||
|
|
userID = pydash.get(obj=data_详情数据, path="contact_way.user.0")
|
|||
|
|
remark = pydash.get(obj=data_详情数据, path="contact_way.remark")
|
|||
|
|
print(qr_code)
|
|||
|
|
wei_obj对象, wei_obj首次创建 = weiuser.objects.get_or_create(unionid=state)
|
|||
|
|
if wei_obj首次创建:
|
|||
|
|
# 当对象是首次创建时:需要更新一下数据
|
|||
|
|
wei_obj对象.user_绑定企微usrID账号 = userID
|
|||
|
|
wei_obj对象.codeConfigId = config_id
|
|||
|
|
wei_obj对象.codeQrCode = qr_code
|
|||
|
|
wei_obj对象.codeRemark = remark
|
|||
|
|
wei_obj对象.parent_id = "六果" # 如果是首次创建就需要将当前对象的父元素 默认为 六果公司
|
|||
|
|
wei_obj对象.user_企微角色权限 = "企微员工"
|
|||
|
|
wei_obj对象.user_省份 = self.addres_省
|
|||
|
|
wei_obj对象.user_城市 = self.address_市
|
|||
|
|
wei_obj对象.user_区县 = self.address_区县
|
|||
|
|
wei_obj对象.user_绑定企微UNIONID = state
|
|||
|
|
wei_obj对象.save()
|
|||
|
|
print("首次创建,并且数据库更新成功了")
|
|||
|
|
else:
|
|||
|
|
wei_obj对象.user_绑定企微usrID账号 = userID
|
|||
|
|
wei_obj对象.codeConfigId = config_id
|
|||
|
|
wei_obj对象.codeQrCode = qr_code
|
|||
|
|
wei_obj对象.codeRemark = remark
|
|||
|
|
wei_obj对象.user_企微角色权限 = miniUser用户角色().企微员工
|
|||
|
|
wei_obj对象.parent_id = wei_obj对象
|
|||
|
|
wei_obj对象.user_用户当前的等级 = 1
|
|||
|
|
wei_obj对象.user_省份 = self.addres_省
|
|||
|
|
wei_obj对象.user_城市 = self.address_市
|
|||
|
|
wei_obj对象.user_区县 = self.address_区县
|
|||
|
|
wei_obj对象.user_绑定企微UNIONID = state
|
|||
|
|
wei_obj对象.save()
|
|||
|
|
print("企微员的用户已经存在,并且更新了相关的信息")
|
|||
|
|
# 接着 查询所有的 父元素为当前对象的所有用户,再将绑定的二维码信息,更新为当前的对象
|
|||
|
|
weiuser.objects.filter(parent=wei_obj对象).update(codeConfigId=wei_obj对象.codeConfigId, user_绑定企微usrID账号=userID,
|
|||
|
|
codeQrCode=wei_obj对象.codeQrCode, codeRemark=wei_obj对象.codeRemark)
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == '__main__':
|
|||
|
|
pass
|
|||
|
|
#
|
|||
|
|
# resData = wecom客户联系_联系我_管理().获取企业已配置的_联系我_方式( config_id="bbc5220b82e614ceeff98fd9f2b04827" )
|
|||
|
|
# wecom客户联系_联系我_管理().获取企业已配置的_联系我_列表()
|
|||
|
|
start时间戳 = datetime(year=2026, month=1, day=1).timestamp()
|
|||
|
|
wecom客户联系_联系我_管理(addres_省="江苏省", address_市="泰州市", address_区县="海陵区").最终处理完整的企微二维码信息(start时间戳=start时间戳) # wecom客户联系_联系我_管理().删除企业已配置的_联系我_方式(config_id="13ee9dcd9a4f7a50ab0f293af90daf3c")
|