import pydash import requests from rest_framework import status from rest_framework.decorators import action from rest_framework.request import Request from rest_framework.response import Response from rest_framework.viewsets import ViewSet from sixGuoDjango import ResData, BaseResponse from wechat_token.token工具包 import sixToken from wechat_数据库.企业微信数据库.推广二维码数据表 import wecom推广码 from wechat_数据库.用户数据库.小程序登陆用户 import weiuser class 联系我管理ViewSets(ViewSet): @action(detail=False, methods=["get", "post", "put", "delete"], url_path="add_contact_way") def 配置客户联系方式(self, request: Request): """对指定的企微员工,创建一个独立的二维码,带有分享信息""" # https://developer.work.weixin.qq.com/document/path/92228 wecom_token = sixToken.get_wecom_token() uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_contact_way?access_token={wecom_token}" state = request.data.get("state", "oy4Jw6Lyfyj1VvgZFodzq9oqJzHE") userID = request.data.get("userID", "HuDie_1") remark = request.data.get("remark", None) # 备注 请求数据 = { "type": 1, # 联系方式类型,1-单人, 2-多人 "scene": 2, # 场景,1-在小程序中联系,2-通过二维码联系 # "style": 1, # 否 在小程序中联系时使用的控件样式,详见附表 "remark": remark, # 否 联系方式的备注信息,用于助记,不超过30个字符 "skip_verify": True, # 否 外部客户添加时是否无需验证,默认为true "state": state, # 是 区分业务用的; "user": userID, # "party": [ # 否 使用该联系方式的部门id列表,只在type为2时有效 "is_exclusive": True, # 否 是否开启同一外部企业客户只能添加同一个员工,默认为否,开启后,同一个企业的客户会优先添加到同一个跟进人 } 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") wecom推广码.objects.update_or_create( codeConfigId=config_id, codeQrCode=qr_code, codeState=state, codeRemark=remark, codeUser=userID, wai外键weiuser表_id=state, ) httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump() return Response(data=httpResData, status=status.HTTP_200_OK) # @action(detail=False, methods=["get", "post", "put", "delete"], url_path="get_contact_way") def 获取企业已配置的方式(self, config_id): """根据 传递的 config_id 来拿到详细的数据""" # https://developer.work.weixin.qq.com/document/path/92228 wecom_token = sixToken.get_wecom_token() uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/get_contact_way?access_token={wecom_token}" data = requests.post(url=uri, json={"config_id": config_id}) config_id = pydash.get(obj=data.json(), path="contact_way.config_id") type = pydash.get(obj=data.json(), path="contact_way.type") qr_code = pydash.get(obj=data.json(), path="contact_way.qr_code") user = pydash.get(obj=data.json(), path="contact_way.user") state = pydash.get(obj=data.json(), path="contact_way.state") # 更新或者添加数据 remark = pydash.get(obj=data.json(), path="contact_way.remark") print("=====================", state) six六果默认对象, _ = weiuser.objects.get_or_create(unionid="六果") weiuser表对象, _ = weiuser.objects.get_or_create(unionid=state) if weiuser表对象.parent is None: # 判断当前用户是否有父级, 如果没有 则默认创建一个 为 六果的 父级 print("判断为None了", six六果默认对象, weiuser表对象) wecom推广码.objects.update_or_create( codeConfigId=config_id, codeQrCode=qr_code, codeState=state, codeRemark=remark, codeUser=user, code省="江苏省", code市="泰州市", code区="海陵区", ) weiuser表对象.codeQrCode = qr_code weiuser表对象.codeRemark = remark weiuser表对象.codeConfigId = config_id weiuser表对象.parent = six六果默认对象 weiuser表对象.save() else: print("判断不为None了", six六果默认对象, weiuser表对象) # 如果有 父级, 则直接处理数据 wecom推广码.objects.update_or_create( codeConfigId=config_id, codeQrCode=qr_code, codeState=state, codeRemark=remark, codeUser=user, wai外键weiuser表_id=state, code省="江苏省", code市="泰州市", code区="海陵区", ) weiuser表对象.codeQrCode = qr_code weiuser表对象.codeRemark = remark weiuser表对象.codeConfigId = config_id weiuser表对象.save() print("\n🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 \n", data.json(), "\n🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 \n") httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump() return Response(data=httpResData, status=status.HTTP_200_OK) @action(detail=False, methods=["get", "post", "put", "delete"], url_path="list_contact_way") def 获取企业已配置的列表(self, request: Request): """""" self_user_unionid = request.data.get("self_user_unionid", None) self_mini_openid = request.data.get("self_mini_openid", None) wecom_token = sixToken.get_wecom_token() uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/list_contact_way?access_token={wecom_token}" data = requests.post(url=uri) list_data = pydash.get(obj=data.json(), path="contact_way") for config_data in list_data: config_id = pydash.get(obj=config_data, path="config_id") self.获取企业已配置的方式(config_id=config_id) print("\n🍎 🍏 🍊 🍎 🍏 🍊获取企业已配置的列表 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 \n", data.json(), "\n🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 \n") httpResData = BaseResponse(resData=ResData(listData=[""], dictData=data.json()), ).model_dump() return Response(data=httpResData, status=status.HTTP_200_OK) @action(detail=False, methods=["get", "post", "put", "delete"], url_path="save_list_contact_way") def 获取全部配置列表并保存数据库(self, request: Request): """""" self_user_unionid = request.data.get("self_user_unionid", None) self_mini_openid = request.data.get("self_mini_openid", None) data = requests.post(url="https://xiao.sixgou.com/wecom_chat/contact/list_contact_way/") print("\n🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 \n", data) list_data_code = wecom推广码.objects.filter().values("codeConfigId") print(list_data_code) for config_id in list_data_code: codeConfigId = pydash.get(obj=config_id, path="codeConfigId") print(codeConfigId, "\n🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 \n") data = requests.post(url="https://xiao.sixgou.com/wecom_chat/contact/get_contact_way/", data={"config_id": codeConfigId}) httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump() return Response(data=httpResData, status=status.HTTP_200_OK) @action(detail=False, methods=["get", "post", "put", "delete"], url_path="update_contact_way") def 更新企业已配置联系我的方式(self, request: Request): """""" data = requests.post(url="https://xiao.sixgou.com/wecom_chat/contact/list_contact_way/") apiData = BaseResponse(**data.json()) print("\n🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 \n", apiData) httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump() return Response(data=httpResData, status=status.HTTP_200_OK) @action(detail=False, methods=["get", "post", "put", "delete"], url_path="del_contact_way") def 删除企业已配置的联系我的方式(self, request: Request): """""" self_user_unionid = request.data.get("self_user_unionid", None) self_mini_openid = request.data.get("self_mini_openid", None) wecom_token = sixToken.get_wecom_token() config_id = request.data.get("config_id", None) uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/del_contact_way?access_token={wecom_token}" config_id = request.data.get("config_id", None) json_data = { "config_id": config_id, } data = requests.post(url=uri, json=json_data) wecom推广码.objects.filter(codeConfigId=config_id).delete() print("\n🍎 🍏 🍊 🍎 🍏 🍊 🍎删除企业已配置的联系我的方式 🍏 🍊 🍎 🍏 🍊 🍎 🍏 \n", data.json(), "\n🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 🍊 🍎 🍏 \n") httpResData = BaseResponse(resData=ResData(listData=[""], dictData=data.json()), ).model_dump() return Response(data=httpResData, status=status.HTTP_200_OK) @action(detail=False, methods=["get", "post", "put", "delete"], url_path="del_list_contact_way") def 删除全部数据信息(self, request: Request): """""" self_user_unionid = request.data.get("self_user_unionid", None) self_mini_openid = request.data.get("self_mini_openid", None) data = requests.post(url="https://xiao.sixgou.com/wecom_chat/contact/list_contact_way/") apiData = BaseResponse(**data.json()) contact_way = pydash.get(obj=apiData.resData.dictData, path="contact_way") for way in contact_way: config_id = pydash.get(obj=way, path="config_id") requests.post(url="https://xiao.sixgou.com/wecom_chat/contact/del_contact_way/", data={"config_id": config_id}) wecom推广码.objects.all().delete() httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump() return Response(data=httpResData, status=status.HTTP_200_OK) @action(detail=False, methods=["get", "post", "put", "delete"], url_path="zizi") def 删除所有的配置方式(self, request: Request): """""" self_user_unionid = request.data.get("self_user_unionid", None) self_mini_openid = request.data.get("self_mini_openid", None) httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump() return Response(data=httpResData, status=status.HTTP_200_OK) @action(detail=False, methods=["get", "post", "put", "delete"], url_path="close_temp_chat") def 结束临时会话(self, request: Request): """""" self_user_unionid = request.data.get("self_user_unionid", None) self_mini_openid = request.data.get("self_mini_openid", None) httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump() return Response(data=httpResData, status=status.HTTP_200_OK) @action(detail=False, methods=["get", "post", "put", "delete"], url_path="configureTheSpecifiedContactPerson") def 配置指定的联系单人(self, request: Request): """对指定的企微员工,创建一个独立的二维码,带有分享信息""" # https://developer.work.weixin.qq.com/document/path/92228 wecom_token = sixToken.get_wecom_token() uri = f"https://qyapi.weixin.qq.com/cgi-bin/externalcontact/add_contact_way?access_token={wecom_token}" state = request.data.get("state", "oy4Jw6Lyfyj1VvgZFodzq9oqJzHE") userID = request.data.get("userID", None) remark = request.data.get("remark", None) # 备注 print(userID, type(userID)) 请求数据 = { "type": 1, # 联系方式类型,1-单人, 2-多人 "scene": 2, # 场景,1-在小程序中联系,2-通过二维码联系 # "style": 1, # 否 在小程序中联系时使用的控件样式,详见附表 "remark": remark, # 否 联系方式的备注信息,用于助记,不超过30个字符; "skip_verify": True, # 否 外部客户添加时是否无需验证,默认为true; "state": state, # 是 区分业务用的; "user": [userID], # "party": [ # 否 使用该联系方式的部门id列表,只在type为2时有效 "is_exclusive": True, # 否 是否开启同一外部企业客户只能添加同一个员工,默认为否,开启后,同一个企业的客户会优先添加到同一个跟进人 } print(请求数据) 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") wecom推广码.objects.update_or_create( codeConfigId=config_id, codeQrCode=qr_code, codeState=state, codeRemark=remark, codeUser=userID, wai外键weiuser表_id=state, code省="江苏省", code市="泰州市", code区="海陵区", ) httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump() return Response(data=httpResData, status=status.HTTP_200_OK)