Spaces:
Runtime error
Runtime error
远兮
commited on
Commit
·
842b5ee
1
Parent(s):
4eb1abb
add
Browse files- chatgpt-next-web/chat_proxy.py +1 -0
- chatgpt-next-web/mail.py +1 -0
- chatgpt-next-web/pay_package.py +3 -2
- chatgpt-next-web/requirements.txt +8 -0
- chatgpt-next-web/run_server.sh +2 -0
- chatgpt-next-web/service.py +56 -16
- chatgpt-next-web/utils.py +1 -0
chatgpt-next-web/chat_proxy.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import websocket
|
2 |
import json
|
3 |
import os
|
|
|
1 |
+
# coding=utf-8
|
2 |
import websocket
|
3 |
import json
|
4 |
import os
|
chatgpt-next-web/mail.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import smtplib
|
2 |
import os
|
3 |
from email.mime.text import MIMEText
|
|
|
1 |
+
# coding=utf-8
|
2 |
import smtplib
|
3 |
import os
|
4 |
from email.mime.text import MIMEText
|
chatgpt-next-web/pay_package.py
CHANGED
@@ -1,10 +1,11 @@
|
|
|
|
1 |
packages = [
|
2 |
{
|
3 |
'id': '1',
|
4 |
'title': '基础套餐(推荐)',
|
5 |
'basic_chat_limit': 10,
|
6 |
'advanced_chat_limit': 10,
|
7 |
-
'price':
|
8 |
'expiration': -1
|
9 |
},
|
10 |
{
|
@@ -12,7 +13,7 @@ packages = [
|
|
12 |
'title': '高级套餐',
|
13 |
'basic_chat_limit': -1, # -1 表示无限次
|
14 |
'advanced_chat_limit': -1,
|
15 |
-
'price': 100,
|
16 |
'expiration': -1
|
17 |
}
|
18 |
]
|
|
|
1 |
+
# coding=utf-8
|
2 |
packages = [
|
3 |
{
|
4 |
'id': '1',
|
5 |
'title': '基础套餐(推荐)',
|
6 |
'basic_chat_limit': 10,
|
7 |
'advanced_chat_limit': 10,
|
8 |
+
'price': 1, # 以分计算
|
9 |
'expiration': -1
|
10 |
},
|
11 |
{
|
|
|
13 |
'title': '高级套餐',
|
14 |
'basic_chat_limit': -1, # -1 表示无限次
|
15 |
'advanced_chat_limit': -1,
|
16 |
+
'price': 100, # 以分计算
|
17 |
'expiration': -1
|
18 |
}
|
19 |
]
|
chatgpt-next-web/requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Flask==2.3.2
|
2 |
+
Flask-Cors==4.0.0
|
3 |
+
openai==0.27.6
|
4 |
+
openapi-schema-pydantic==1.2.4
|
5 |
+
redis==4.5.5
|
6 |
+
streamlit==1.21.0
|
7 |
+
streamlit-chat==0.0.2.2
|
8 |
+
pyJWT==2.4.0
|
chatgpt-next-web/run_server.sh
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
gunicorn -b 0.0.0.0:5000 service:app
|
chatgpt-next-web/service.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import json
|
2 |
import random
|
3 |
import string
|
@@ -6,6 +7,8 @@ import jwt
|
|
6 |
import datetime
|
7 |
import requests
|
8 |
import os
|
|
|
|
|
9 |
from flask import Flask, request, jsonify, Request, Response
|
10 |
from redis import Redis
|
11 |
from utils import nowtime
|
@@ -18,10 +21,10 @@ SECERT_KEY = "8U2LL1"
|
|
18 |
MY_OPENAI_API_KEY = os.environ.get('MY_OPENAI_API_KEY')
|
19 |
|
20 |
app = Flask(__name__)
|
21 |
-
cors = CORS(app)
|
22 |
-
redis = Redis(host='192.168.3.229', port=6379, password='lizhen-redis')
|
23 |
# redis = Redis(host='10.254.13.87', port=6379)
|
24 |
-
|
25 |
|
26 |
|
27 |
# 生成验证码
|
@@ -190,27 +193,29 @@ def create_order():
|
|
190 |
if not user_id:
|
191 |
return jsonify({'code': 400, 'message': 'User not found'})
|
192 |
# 创建订单
|
193 |
-
order_id =
|
194 |
order_data = {
|
195 |
'order_id': order_id,
|
196 |
'user_id': user_id,
|
197 |
-
'package_id':
|
198 |
-
'
|
|
|
199 |
'status': 'pending', # 订单状态设置为待支付
|
200 |
'created_at': nowtime().strftime('%Y-%m-%d %H:%M:%S') # 将时间转换为字符串格式保存
|
201 |
}
|
202 |
# 将订单信息存储到Redis,并设置过期时间为2小时
|
203 |
order_key = f'order:{user_id}:{order_id}'
|
204 |
-
redis.
|
|
|
|
|
|
|
|
|
|
|
|
|
205 |
redis.expire(order_key, datetime.timedelta(hours=2))
|
206 |
-
|
207 |
# 发起POST请求到http://localhost:8080/payPre获取codeUrl
|
208 |
-
|
209 |
-
'order_id': order_id,
|
210 |
-
'amount': package['price'],
|
211 |
-
'user_id': user_id
|
212 |
-
}
|
213 |
-
response = requests.post('http://localhost:8080/payPre', json=payload)
|
214 |
|
215 |
if response.status_code == 200:
|
216 |
data = response.json()
|
@@ -261,6 +266,28 @@ def order_center():
|
|
261 |
})
|
262 |
|
263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
def get_user_orders(user_id):
|
265 |
# 从Redis中获取该用户的所有订单数据
|
266 |
user_orders = []
|
@@ -268,7 +295,8 @@ def get_user_orders(user_id):
|
|
268 |
order_data = redis.hgetall(key)
|
269 |
# 将bytes类型的键和值转换为str类型
|
270 |
order_data_str = {
|
271 |
-
k.decode('utf-8'): v
|
|
|
272 |
}
|
273 |
user_orders.append(order_data_str)
|
274 |
return user_orders
|
@@ -698,5 +726,17 @@ def get_free_count(user_id):
|
|
698 |
return int(basic_chat_count)
|
699 |
|
700 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
701 |
if __name__ == '__main__':
|
702 |
-
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
import json
|
3 |
import random
|
4 |
import string
|
|
|
7 |
import datetime
|
8 |
import requests
|
9 |
import os
|
10 |
+
import hashlib
|
11 |
+
import time
|
12 |
from flask import Flask, request, jsonify, Request, Response
|
13 |
from redis import Redis
|
14 |
from utils import nowtime
|
|
|
21 |
MY_OPENAI_API_KEY = os.environ.get('MY_OPENAI_API_KEY')
|
22 |
|
23 |
app = Flask(__name__)
|
24 |
+
cors = CORS(app, origins=["http://localhost:3000", "http://127.0.0.1:5697"])
|
25 |
+
# redis = Redis(host='192.168.3.229', port=6379, password='lizhen-redis')
|
26 |
# redis = Redis(host='10.254.13.87', port=6379)
|
27 |
+
redis = Redis(host='localhost', port=6379)
|
28 |
|
29 |
|
30 |
# 生成验证码
|
|
|
193 |
if not user_id:
|
194 |
return jsonify({'code': 400, 'message': 'User not found'})
|
195 |
# 创建订单
|
196 |
+
order_id = generate_order_id()
|
197 |
order_data = {
|
198 |
'order_id': order_id,
|
199 |
'user_id': user_id,
|
200 |
+
'package_id': package['id'],
|
201 |
+
'title': package['title'],
|
202 |
+
'amount': int(package['price']),
|
203 |
'status': 'pending', # 订单状态设置为待支付
|
204 |
'created_at': nowtime().strftime('%Y-%m-%d %H:%M:%S') # 将时间转换为字符串格式保存
|
205 |
}
|
206 |
# 将订单信息存储到Redis,并设置过期时间为2小时
|
207 |
order_key = f'order:{user_id}:{order_id}'
|
208 |
+
redis.hset(order_key, 'order_id', order_data['order_id'])
|
209 |
+
redis.hset(order_key, 'user_id', order_data['user_id'])
|
210 |
+
redis.hset(order_key, 'package_id', order_data['package_id'])
|
211 |
+
redis.hset(order_key, 'title', order_data['title'])
|
212 |
+
redis.hset(order_key, 'amount', order_data['amount'])
|
213 |
+
redis.hset(order_key, 'status', order_data['status'])
|
214 |
+
redis.hset(order_key, 'created_at', order_data['created_at'])
|
215 |
redis.expire(order_key, datetime.timedelta(hours=2))
|
216 |
+
|
217 |
# 发起POST请求到http://localhost:8080/payPre获取codeUrl
|
218 |
+
response = requests.post('http://localhost:5697/payPre', json=order_data)
|
|
|
|
|
|
|
|
|
|
|
219 |
|
220 |
if response.status_code == 200:
|
221 |
data = response.json()
|
|
|
266 |
})
|
267 |
|
268 |
|
269 |
+
@app.route('/pay/notify', methods=['POST'])
|
270 |
+
def handle_payment_notification():
|
271 |
+
# 获取POST请求中的JSON数据,这是微信支付发送的通知数据
|
272 |
+
notification_data = request.json
|
273 |
+
|
274 |
+
print("/pay/notify", notification_data)
|
275 |
+
|
276 |
+
# 假设你从通知数据中获取了订单信息,例如订单ID和用户ID
|
277 |
+
order_id = notification_data.get('order_id')
|
278 |
+
user_id = notification_data.get('user_id')
|
279 |
+
|
280 |
+
# 更新订单状态为已支付
|
281 |
+
order_key = f'order:{user_id}:{order_id}'
|
282 |
+
redis.hset(order_key, 'status', 'paid')
|
283 |
+
|
284 |
+
# 设置订单6个月后过期
|
285 |
+
redis.expire(order_key, datetime.timedelta(days=6*30).total_seconds())
|
286 |
+
|
287 |
+
# 返回一个响应,可以是一个简单的字符串或者JSON对象,告诉微信支付通知已经收到
|
288 |
+
return jsonify({'code': 0, 'message': 'Notification received'})
|
289 |
+
|
290 |
+
|
291 |
def get_user_orders(user_id):
|
292 |
# 从Redis中获取该用户的所有订单数据
|
293 |
user_orders = []
|
|
|
295 |
order_data = redis.hgetall(key)
|
296 |
# 将bytes类型的键和值转换为str类型
|
297 |
order_data_str = {
|
298 |
+
k.decode('utf-8'): int(v) if k == b'amount' else v.decode('utf-8')
|
299 |
+
for k, v in order_data.items()
|
300 |
}
|
301 |
user_orders.append(order_data_str)
|
302 |
return user_orders
|
|
|
726 |
return int(basic_chat_count)
|
727 |
|
728 |
|
729 |
+
def generate_order_id():
|
730 |
+
timestamp = int(time.time() * 1000) # 获取当前时间戳,精确到毫秒
|
731 |
+
rand_num = random.randint(0, 9999) # 生成四位随机数
|
732 |
+
raw_order_id = f"{timestamp}-{rand_num}"
|
733 |
+
|
734 |
+
# 对订单号进行哈希处理,使用SHA-256算法(32字节长度)
|
735 |
+
hashed_order_id = hashlib.sha256(raw_order_id.encode()).hexdigest()[:32]
|
736 |
+
|
737 |
+
return hashed_order_id
|
738 |
+
|
739 |
+
|
740 |
if __name__ == '__main__':
|
741 |
+
print("启动了")
|
742 |
+
app.run(host="0.0.0.0", port=5000)
|
chatgpt-next-web/utils.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import datetime
|
2 |
|
3 |
|
|
|
1 |
+
# coding=utf-8
|
2 |
import datetime
|
3 |
|
4 |
|