six_001
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
import jmespath
|
||||
import requests
|
||||
from django.core.files.uploadedfile import InMemoryUploadedFile
|
||||
from django.forms import model_to_dict
|
||||
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.routers import DefaultRouter
|
||||
from rest_framework.viewsets import ViewSet
|
||||
|
||||
from sixGuoDjango import BaseResponse, ResData
|
||||
from wechat_token.token工具包 import sixToken
|
||||
from wechat_数据库.用户数据库.小程序登陆用户 import weiuser
|
||||
|
||||
|
||||
class 小程序用户头像昵称接口ViewSets(ViewSet):
|
||||
@action(detail=False, methods=["post"], url_path="chooseAvatar")
|
||||
def 用户头像接口(self, request: Request):
|
||||
""""""
|
||||
chooseAvatar: InMemoryUploadedFile | None = request.FILES.get("chooseAvatar", None)
|
||||
print(chooseAvatar)
|
||||
self_user_unionid = request.data.get("self_user_unionid", None)
|
||||
self_mini_openid = request.data.get("self_mini_openid", None)
|
||||
print(self_user_unionid)
|
||||
httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump()
|
||||
return Response(data=httpResData, status=status.HTTP_200_OK)
|
||||
|
||||
@action(detail=False, methods=["post"], url_path="nickName")
|
||||
def 更新用户昵称(self, request: Request):
|
||||
""""""
|
||||
self_user_unionid = request.data.get("self_user_unionid", None)
|
||||
self_mini_openid = request.data.get("self_mini_openid", None)
|
||||
nickName = request.data.get("nickName", None)
|
||||
print("获取到的用户昵称", nickName)
|
||||
httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump()
|
||||
return Response(data=httpResData, status=status.HTTP_200_OK)
|
||||
|
||||
@action(detail=False, methods=["post"], url_path="getPhoneNumber")
|
||||
def 获取用户手机号码(self, request: Request):
|
||||
""""""
|
||||
self_user_unionid = request.data.get("self_user_unionid", None)
|
||||
self_mini_openid = request.data.get("self_mini_openid", None)
|
||||
code = request.data.get("code", None)
|
||||
mini_token = sixToken.get_mini_token()
|
||||
url = f"https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token={mini_token}"
|
||||
jsonData = {"code": code}
|
||||
resData = requests.post(url=url, json=jsonData).json()
|
||||
print(resData, "❌❌❌❌❌❌❌❌❌❌❌❌")
|
||||
phoneNumber = jmespath.search(data=resData, expression="phone_info.phoneNumber")
|
||||
print(phoneNumber)
|
||||
httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"phoneNumber": phoneNumber}), ).model_dump()
|
||||
return Response(data=httpResData, status=status.HTTP_200_OK)
|
||||
|
||||
@action(detail=False, methods=["post"], url_path="search")
|
||||
def 查询用户头像和昵称(self, request: Request):
|
||||
""""""
|
||||
self_mini_openid = request.data.get("self_mini_openid", None)
|
||||
self_mini_unionid = request.data.get("self_mini_unionid", None)
|
||||
用户头像信息 = weiuser.objects.get(unionid=self_mini_unionid)
|
||||
resData = model_to_dict(instance=用户头像信息, fields=["avatar", "nickName"])
|
||||
print(self_mini_unionid)
|
||||
httpResData = BaseResponse(resData=ResData(listData=[""], dictData=resData), ).model_dump()
|
||||
return Response(data=httpResData, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
小程序用户头像昵称接口ViewSetsRouter = DefaultRouter()
|
||||
小程序用户头像昵称接口ViewSetsRouter.register(prefix="", viewset=小程序用户头像昵称接口ViewSets, basename="")
|
||||
@@ -0,0 +1,119 @@
|
||||
"""
|
||||
Author: xiao 792282@qq.com
|
||||
Date: 2025-08-10 11:36:05
|
||||
LastEditors: xiao 792282@qq.com
|
||||
LastEditTime: 2025-08-17 13:38:40
|
||||
FilePath: django_小程序/小程序服务端服务接口/小程序用户登陆接口.py
|
||||
Description: 这是默认设置,可以在设置》工具》File Description中进行配置
|
||||
"""
|
||||
|
||||
import jmespath
|
||||
from django.forms import model_to_dict
|
||||
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.routers import SimpleRouter
|
||||
from rest_framework.viewsets import ViewSet
|
||||
|
||||
from sixGuoDjango import BaseResponse, ResData
|
||||
from wechat_数据库.用户数据库.小程序登陆用户 import weiuser
|
||||
from wechat_项目工具包.小程序工具包.小程序用户授权数据 import wei小程序服务端工具
|
||||
from wechat_项目工具包.微信支付工具包.微信支付类 import paymentCategory, wechatPay
|
||||
|
||||
上级分享者的默认值 = "六果"
|
||||
|
||||
|
||||
class 六果科技小程序用户登陆(ViewSet):
|
||||
|
||||
def res_obj_data_func(self, self_user: weiuser) -> dict[str, str]:
|
||||
""""""
|
||||
res_obj_data = model_to_dict(instance=self_user, fields=None, exclude=None)
|
||||
return res_obj_data
|
||||
|
||||
@action(detail=False, methods=["post", ], url_path="mini_login")
|
||||
def 小程序用户(self, request: Request, *args, **kwargs):
|
||||
""""""
|
||||
encryptedData = request.data.get("encryptedData", None) # 解密群号所需要的参数
|
||||
iv = request.data.get("iv", None) # 解密群号所需要的参数 iv
|
||||
code: str = request.data.get("code", "")
|
||||
# superior_openid = request.data.get( "superior_openid", ) # 如果上级是默认的值 则说明是没有上级的,
|
||||
superior_unionid = request.data.get("superior_unionid", 上级分享者的默认值) # 如果上级是默认的值 则说明是没有上级的,
|
||||
scene = request.data.get("scene", None) # 微信用户进入小程的场景值
|
||||
shareTicket = request.data.get("shareTicket", None) # 微信用户的shareTicket值
|
||||
res = wei小程序服务端工具.获取OPENID(js_code=code, appid=paymentCategory.appid,
|
||||
secret=paymentCategory.appsecret, )
|
||||
# 代码能往下执行:则说明,已经正确拿到结果;
|
||||
self_openid = jmespath.search(data=res, expression="openid")
|
||||
session_key = jmespath.search(data=res, expression="session_key")
|
||||
self_unionid = jmespath.search(data=res, expression="unionid")
|
||||
# 准备好,需要的数据
|
||||
|
||||
share分享者对象, share分享者对象是新创建 = weiuser.objects.update_or_create(unionid=superior_unionid, )
|
||||
# 首次进来的用户要全部的添加上默认的 用户 角色
|
||||
click点击者对象, self_bool = weiuser.objects.update_or_create(unionid=self_unionid,
|
||||
defaults={"mini_openid": self_openid})
|
||||
|
||||
six六果对象, sixBool = weiuser.objects.get_or_create(unionid="六果")
|
||||
if superior_unionid == 上级分享者的默认值:
|
||||
share分享者对象 = six六果对象
|
||||
print("登陆成功了,同时打印一下上级的用户是谁", superior_unionid)
|
||||
if click点击者对象.parent is not None:
|
||||
# 不为空说明已经有上级存在了
|
||||
res_obj_data = self.res_obj_data_func(self_user=click点击者对象) # 函数调用返回数据
|
||||
httpResData = BaseResponse(resData=ResData(listData=[""], dictData=res_obj_data), ).model_dump()
|
||||
return Response(data=httpResData, status=status.HTTP_200_OK)
|
||||
|
||||
if superior_unionid == self_unionid and share分享者对象是新创建 == True:
|
||||
# 说明是自己点了自己
|
||||
print("说明是自己点了自己说明是自己点了自己说明是自己点了自己")
|
||||
click点击者对象.parent = six六果对象
|
||||
click点击者对象.codeConfigId = six六果对象.codeConfigId
|
||||
click点击者对象.codeQrCode = six六果对象.codeQrCode
|
||||
click点击者对象.codeRemark = six六果对象.codeRemark
|
||||
click点击者对象.user_用户当前的等级 = six六果对象.user_用户当前的等级 + 1
|
||||
click点击者对象.user_绑定企微UNIONID = six六果对象.user_绑定企微UNIONID
|
||||
click点击者对象.save()
|
||||
res_obj_data = self.res_obj_data_func(self_user=click点击者对象) # 函数调用返回数据
|
||||
httpResData = BaseResponse(resData=ResData(listData=[""], dictData=res_obj_data), ).model_dump()
|
||||
return Response(data=httpResData, status=status.HTTP_200_OK)
|
||||
if click点击者对象.parent is None and scene == 1044:
|
||||
openGId, timestamp, appid = wechatPay(payment=paymentCategory).解密群号(iv=iv, encryptedData=encryptedData,
|
||||
sessionKey=session_key, )
|
||||
# TODO 在这里需要完成,对群号数据的添加
|
||||
click点击者对象.parent = share分享者对象
|
||||
click点击者对象.codeConfigId = share分享者对象.codeConfigId
|
||||
click点击者对象.codeQrCode = share分享者对象.codeQrCode
|
||||
click点击者对象.codeRemark = share分享者对象.codeRemark
|
||||
click点击者对象.user_用户当前的等级 = share分享者对象.user_用户当前的等级 + 1
|
||||
click点击者对象.user_绑定企微UNIONID = share分享者对象.user_绑定企微UNIONID
|
||||
click点击者对象.save()
|
||||
res_obj_data = self.res_obj_data_func(self_user=click点击者对象) # 函数调用返回数据
|
||||
httpResData = BaseResponse(resData=ResData(listData=[""], dictData=res_obj_data), ).model_dump()
|
||||
return Response(data=httpResData, status=status.HTTP_200_OK)
|
||||
if click点击者对象.parent is None:
|
||||
# 说明没有上级存在,更新数据
|
||||
print("说明没有上级存在,更新数据")
|
||||
# use, use_bool = weiuser.objects.update_or_create(
|
||||
# unionid=self_unionid,
|
||||
# defaults={ "mini_openid": self_openid, "user_用户当前的等级": share分享者对象.user_用户当前的等级 + 1, "parent": share分享者对象,
|
||||
# "codeConfigId": share分享者对象.codeConfigId, "codeQrCode": share分享者对象.codeQrCode,
|
||||
# "codeRemark": share分享者对象.codeRemark,
|
||||
# }, )
|
||||
click点击者对象.parent = share分享者对象
|
||||
click点击者对象.codeConfigId = share分享者对象.codeConfigId
|
||||
click点击者对象.codeQrCode = share分享者对象.codeQrCode
|
||||
click点击者对象.codeRemark = share分享者对象.codeRemark
|
||||
click点击者对象.user_用户当前的等级 = share分享者对象.user_用户当前的等级 + 1
|
||||
click点击者对象.user_绑定企微UNIONID = share分享者对象.user_绑定企微UNIONID
|
||||
click点击者对象.save()
|
||||
print("自己点了自已,同时还没有创建数据时的情况", share分享者对象.user_角色权限)
|
||||
res_obj_data = self.res_obj_data_func(self_user=click点击者对象) # 函数调用返回数据
|
||||
httpResData = BaseResponse(resData=ResData(listData=[""], dictData=res_obj_data), ).model_dump()
|
||||
return Response(data=httpResData, status=status.HTTP_200_OK)
|
||||
# 在所有的业务判断都不成功时,直接返回
|
||||
return Response(data="", status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
六果科技小程序用户登陆Router = SimpleRouter()
|
||||
六果科技小程序用户登陆Router.register(prefix="", viewset=六果科技小程序用户登陆, basename="六果科技小程序用户登陆")
|
||||
@@ -0,0 +1,106 @@
|
||||
import datetime
|
||||
import secrets
|
||||
|
||||
import orjson
|
||||
from django.core.handlers.wsgi import WSGIRequest
|
||||
from requests import get, post
|
||||
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.routers import DefaultRouter
|
||||
from rest_framework.viewsets import ViewSet
|
||||
|
||||
from sixGuoDjango import BaseResponse, ResData
|
||||
from wechat_token.token工具包 import sixToken
|
||||
from wechat_数据库.小程序后端数据库.订阅消息 import miniSub订阅消息模板
|
||||
from wechat_数据库.雪花算法.雪花算法工具 import generator
|
||||
|
||||
|
||||
class miniMsg订阅消息(ViewSet):
|
||||
"""miniMsg订阅消息工具"""
|
||||
|
||||
@action(detail=False, methods=["post"], url_path="查询服务卡片状态")
|
||||
def 查询服务卡片状态(self, request: WSGIRequest | Request):
|
||||
"""
|
||||
查询服务卡片状态
|
||||
:return:
|
||||
:rtype:
|
||||
"""
|
||||
uri = f"https://api.weixin.qq.com/wxa/get_user_notify"
|
||||
notify_code = secrets.token_hex(10)
|
||||
post_data = {"openid": "o6oV610oYhpoJYaCJ9tDJKehsJTc", # string 是 用户身份标识符
|
||||
"notify_type": "vknIxXIC1O0IMhHI0yiS-zPUt3UvPYO-ihDM_mcHRJw", #
|
||||
"notify_code": notify_code,
|
||||
}
|
||||
mini_token = sixToken.get_mini_token()
|
||||
result_data = post(url=uri, params={"access_token": mini_token}, json=post_data)
|
||||
print(result_data.json())
|
||||
return Response()
|
||||
|
||||
@action(detail=False, methods=["post"], url_path="gettemplate")
|
||||
def 获取消息列表(self, request: WSGIRequest | Request):
|
||||
""""""
|
||||
"""
|
||||
获取个人模板列表
|
||||
:return:
|
||||
:rtype:
|
||||
"""
|
||||
print("----------------------------------------")
|
||||
uri = f"https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate"
|
||||
mini_token = sixToken.get_mini_token()
|
||||
print(mini_token)
|
||||
post_data = {}
|
||||
result_data = get(url=uri, params={"access_token": mini_token}, )
|
||||
print(orjson.loads(result_data.content.decode(encoding="utf-8")))
|
||||
httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump()
|
||||
return Response(data=httpResData, status=status.HTTP_200_OK)
|
||||
|
||||
@action(detail=False, methods=["post"], url_path="sendmsg")
|
||||
def 发送模板消息(self, request: WSGIRequest | Request):
|
||||
"""
|
||||
如果不知道模板格式,就需要调用上面的接口
|
||||
{'priTmplId': 'xCfF4yeTlKaEfZ4sh3z9C6r-wG845W4Bst2JO9hWR6A',
|
||||
'title': '卡券到期提醒',
|
||||
'content':
|
||||
'卡券名称:{{thing1.DATA}}\n
|
||||
到期时间:{{time2.DATA}}\n
|
||||
使用规则:{{thing3.DATA}}\n
|
||||
状态:{{phrase4.DATA}}\n', '
|
||||
example': '卡券名称:软件维护服务1次\n到期时间:2020年10月14日 23:59\n使用规则:将有专业服务人员上门服务并核销卡券\n状态:未使用\n', 'type': 2, 'keywordEnumValueList': []},
|
||||
content内的内容就是模板的信息 格式为 thing1:{value:"内容,"},time2:{"value":"时间字符串"}
|
||||
"""
|
||||
pass
|
||||
uri = f"https://api.weixin.qq.com/cgi-bin/message/subscribe/send"
|
||||
datetimd_str = datetime.datetime.now().strftime(format="%Y-%m-%d %H:%M:%S")
|
||||
random_str = generator.generate_str()
|
||||
post_data = {"touser": "ohQkD7A-N93kljcFmkCyG6e_D9vg",
|
||||
"template_id": "RvGf7wyALX4djhBgeIp6jqlfAKNzd_weXRFEd1P54fk",
|
||||
"page": "tabPage/Home/Home",
|
||||
"data": {
|
||||
"thing1": {"value": "100优惠券"},
|
||||
"thing2": {"value": "random_str"},
|
||||
},
|
||||
}
|
||||
mini_token = sixToken.get_mini_token()
|
||||
result_data = post(url=uri, params={"access_token": mini_token}, json=post_data)
|
||||
print(result_data.json(), "订阅消息发送成功了啦")
|
||||
return Response(data={"msg": "订阅消息"})
|
||||
|
||||
@action(detail=False, methods=["post"], url_path="add_messages")
|
||||
def 计数订阅成功的次数(self, request: Request):
|
||||
""""""
|
||||
self_mini_openid = request.data.get("self_mini_openid", None)
|
||||
self_mini_unionid = request.data.get("self_mini_unionid", None)
|
||||
|
||||
sub对象, sub对象是否存在 = miniSub订阅消息模板.objects.update_or_create(sub订阅用户ID=self_mini_openid,
|
||||
sub订阅模板ID="RvGf7wyALX4djhBgeIp6jqlfAKNzd_weXRFEd1P54fk",
|
||||
)
|
||||
sub对象.sub订阅次数 = sub对象.sub订阅次数 + 1
|
||||
sub对象.save()
|
||||
httpResData = BaseResponse(resData=ResData(listData=[""], dictData={"demo": "xiaozizi"}), ).model_dump()
|
||||
return Response(data=httpResData, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
miniMsg订阅消息Router = DefaultRouter()
|
||||
miniMsg订阅消息Router.register(prefix="", viewset=miniMsg订阅消息, basename="")
|
||||
Reference in New Issue
Block a user