From 76d644c447c73dee78ee3302c761c8b3018d80a1 Mon Sep 17 00:00:00 2001 From: hackrobot Date: Sat, 10 Aug 2024 18:24:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E4=BB=98=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/main.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index df27c97..bc03fda 100644 --- a/app/main.py +++ b/app/main.py @@ -3,6 +3,7 @@ from fastapi import BackgroundTasks, FastAPI, HTTPException, Depends, Query from fastapi.responses import JSONResponse, PlainTextResponse from sqlalchemy.orm import Session from typing import List, Optional, Any +from pydantic import BaseModel from app import models, schemas, crud from app.database import SessionLocal, engine @@ -22,11 +23,53 @@ from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentClo from tencentcloud.sms.v20210111 import sms_client, models as smsmodels from tencentcloud.common.profile.client_profile import ClientProfile from tencentcloud.common.profile.http_profile import HttpProfile - +import hashlib +from urllib.parse import urlencode, unquote +import time # 初始化数据库 models.Base.metadata.create_all(bind=engine) + +""" + JSAPI 支付 +""" +key = "zA2hBU6pv6CIBzkW" # 填写通信密钥 +mchid = "1561724891" # 特写商户号 + +# 构造签名函数 +def sign(attributes): + attributes_new = {k: attributes[k] for k in sorted(attributes.keys())} + return ( + hashlib.md5( + (unquote(urlencode(attributes_new)) + "&key=" + key).encode( + encoding="utf-8" + ) + ) + .hexdigest() + .upper() + ) + + +class Item(BaseModel): + event: str + EventGroupMsg: None = None + prirobot_wxidce: None = None + type: None = None +# event +# "":"EventGroupMsg",//事件标示(当前值为群消息事件) +# "robot_wxid":"wxid_5hxa04j4z6pg22",//机器人wxid +# "robot_name":"",//机器人昵称,一般为空 +# "type":1,//1/文本消息 3/图片消息 34/语音消息 42/名片消息 43/视频 47/动态表情 48/地理位置 49/分享链接 2000/转账 2001/红包 2002/小程序 2003/群邀请 +# "from_wxid":"18900134932@chatroom",//群id,群消息事件才有 +# "from_name":"微群测",//群名字 +# "final_from_wxid":"sundreamer",//发该消息的用户微信id +# "final_from_name":"遗忘悠剑o",//微信昵称 +# "to_wxid":"wxid_5hxa04j4z6pg22",//接收消息的人id,(一般是机器人收到了,也有可能是机器人发出的消息,别人收到了,那就是别人) +# "msg":"图片https://b3logfile.com/bing/20201024.jpg",//消息内容(string/array) 使用时候根据不同的事件标示来定义这个值,字符串类型或者数据类型 +# "money":0.01 //金额,只有"EventReceivedTransfer"事件才有该参数 + + app = FastAPI() # 挂载 images 文件夹 @@ -112,6 +155,34 @@ def hello(): return PlainTextResponse(content=response) +@app.post("/payh5") +async def payh5(item: dict): + print('item', item) + # 时间戳 + timenew = str(int(time.time())) + order = { + "mchid": mchid, + "body": timenew, # 订单标题 + "out_trade_no": timenew, # 订单号 + "total_fee": 100, # 金额,单位:分 + # "openid": "o7LFAwWu3OhSrs49-1x5cSlFm0D8", # 通过openid接口获取到的openid + # "notify_url": "" + # "callback_url":'https://aiimg.hackrobot.cn/api' + } + + order["callback_url"] =item['callback_url'] + order["openid"] =item['openid'] + order["sign"] = sign(order) + + request_url = "https://payjs.cn/api/jsapi" + # 返回结果中包含`jsapi`字段,该字段的值即是前端发起时所需的6个支付参数 + headers = {"content-type": "application/x-www-form-urlencoded"} + response = requests.post(request_url, data=order, headers=headers) + if response: + print(response.json()) + return response.json() + + @app.get("/tgMessage") def tgMessagefun(openid: Optional[str] = None, db: Session = Depends(get_db)):