Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import sys
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
# 【終極防護解除】關閉 Gradio 跨域與 CSRF 阻擋
|
| 5 |
os.environ["GRADIO_CSRF_TRUST_ORIGINS"] = "*"
|
| 6 |
|
| 7 |
try:
|
|
@@ -10,6 +10,7 @@ try:
|
|
| 10 |
from datetime import datetime
|
| 11 |
import subprocess
|
| 12 |
from fastapi import FastAPI, Request, HTTPException
|
|
|
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware
|
| 14 |
from linebot import LineBotApi, WebhookHandler
|
| 15 |
from linebot.exceptions import InvalidSignatureError
|
|
@@ -39,7 +40,13 @@ except Exception as e:
|
|
| 39 |
|
| 40 |
custom_api = FastAPI()
|
| 41 |
|
| 42 |
-
# 【關鍵修復】
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
custom_api.add_middleware(
|
| 44 |
CORSMiddleware,
|
| 45 |
allow_origins=["*"],
|
|
@@ -117,7 +124,6 @@ def handle_message(event):
|
|
| 117 |
# 💻 網頁後台邏輯
|
| 118 |
# ----------------------------
|
| 119 |
def get_orders_data():
|
| 120 |
-
"""如果資料庫是空的,必須塞入一排空白資料,否則表格會直接讓網頁卡死!"""
|
| 121 |
if not orders_db:
|
| 122 |
return [["", "", "", "", "", "", ""]]
|
| 123 |
return [[o["時間"], o["顧客姓名"], o["商品"], o["單價"], o["數量"], o["總價"], o["備註"]] for o in orders_db]
|
|
@@ -144,9 +150,10 @@ def add_order(c_name, q0, q1, q2, q3, q4, nts):
|
|
| 144 |
has_ordered_items = False
|
| 145 |
menu_items = list(MENU.items())
|
| 146 |
|
| 147 |
-
for i,
|
|
|
|
| 148 |
try:
|
| 149 |
-
qty = int(
|
| 150 |
except:
|
| 151 |
qty = 0
|
| 152 |
|
|
@@ -239,8 +246,8 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統", css=cus
|
|
| 239 |
for item, price in MENU.items():
|
| 240 |
with gr.Row(elem_classes="menu-row"):
|
| 241 |
gr.Markdown(f"**{item}** <br><span style='color: gray;'>NT$ {price}</span>")
|
| 242 |
-
# 【
|
| 243 |
-
qty = gr.Dropdown(choices=
|
| 244 |
quantity_components.append(qty)
|
| 245 |
|
| 246 |
notes = gr.Textbox(label="備註", placeholder="例如:少冰微糖...")
|
|
@@ -269,15 +276,15 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統", css=cus
|
|
| 269 |
github_log_output = gr.Textbox(label="執行日誌", interactive=False, lines=4, elem_classes="github-log")
|
| 270 |
clear_btn = gr.Button("🗑️ 清空所有訂單", variant="stop")
|
| 271 |
|
| 272 |
-
# 事件綁定
|
| 273 |
inputs_list = [customer_name] + quantity_components + [notes]
|
| 274 |
|
| 275 |
-
submit_btn.click(fn=add_order, inputs=inputs_list, outputs=[status_msg, orders_table, revenue_display]
|
| 276 |
-
login_btn.click(fn=verify_password, inputs=admin_pwd, outputs=[login_pane, overview_pane, login_status]
|
| 277 |
-
logout_btn.click(fn=logout, inputs=[], outputs=[login_pane, overview_pane, login_status, admin_pwd]
|
| 278 |
-
clear_btn.click(fn=clear_orders, inputs=[], outputs=[status_msg, orders_table, revenue_display]
|
| 279 |
-
github_btn.click(fn=backup_and_push_to_github, inputs=[], outputs=[github_log_output]
|
| 280 |
-
refresh_btn.click(fn=refresh_data, inputs=[], outputs=[orders_table, revenue_display]
|
| 281 |
|
| 282 |
# ----------------------------
|
| 283 |
# 🚀 啟動伺服器
|
|
|
|
| 1 |
import sys
|
| 2 |
import os
|
| 3 |
|
| 4 |
+
# 【終極防護解除】關閉 Gradio 跨域與 CSRF 阻擋
|
| 5 |
os.environ["GRADIO_CSRF_TRUST_ORIGINS"] = "*"
|
| 6 |
|
| 7 |
try:
|
|
|
|
| 10 |
from datetime import datetime
|
| 11 |
import subprocess
|
| 12 |
from fastapi import FastAPI, Request, HTTPException
|
| 13 |
+
from fastapi.responses import RedirectResponse
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
from linebot import LineBotApi, WebhookHandler
|
| 16 |
from linebot.exceptions import InvalidSignatureError
|
|
|
|
| 40 |
|
| 41 |
custom_api = FastAPI()
|
| 42 |
|
| 43 |
+
# 【關鍵修復 1】攔截 LINE 的外部開啟參數,將網址洗乾淨並重新導向,防止 Gradio 前端在手機上崩潰!
|
| 44 |
+
@custom_api.middleware("http")
|
| 45 |
+
async def strip_line_query_param(request: Request, call_next):
|
| 46 |
+
if request.url.path == "/" and "openExternalBrowser" in request.query_params:
|
| 47 |
+
return RedirectResponse(url="/")
|
| 48 |
+
return await call_next(request)
|
| 49 |
+
|
| 50 |
custom_api.add_middleware(
|
| 51 |
CORSMiddleware,
|
| 52 |
allow_origins=["*"],
|
|
|
|
| 124 |
# 💻 網頁後台邏輯
|
| 125 |
# ----------------------------
|
| 126 |
def get_orders_data():
|
|
|
|
| 127 |
if not orders_db:
|
| 128 |
return [["", "", "", "", "", "", ""]]
|
| 129 |
return [[o["時間"], o["顧客姓名"], o["商品"], o["單價"], o["數量"], o["總價"], o["備註"]] for o in orders_db]
|
|
|
|
| 150 |
has_ordered_items = False
|
| 151 |
menu_items = list(MENU.items())
|
| 152 |
|
| 153 |
+
for i, qty_str in enumerate(quantities):
|
| 154 |
+
# 安全轉換文字為數字
|
| 155 |
try:
|
| 156 |
+
qty = int(qty_str) if qty_str else 0
|
| 157 |
except:
|
| 158 |
qty = 0
|
| 159 |
|
|
|
|
| 246 |
for item, price in MENU.items():
|
| 247 |
with gr.Row(elem_classes="menu-row"):
|
| 248 |
gr.Markdown(f"**{item}** <br><span style='color: gray;'>NT$ {price}</span>")
|
| 249 |
+
# 【關鍵修復 2】強制轉換選項為純字串 ("0", "1"...),避免手機瀏覽器的底層整數型別判斷當機
|
| 250 |
+
qty = gr.Dropdown(choices=[str(i) for i in range(21)], label="數量", value="0", interactive=True)
|
| 251 |
quantity_components.append(qty)
|
| 252 |
|
| 253 |
notes = gr.Textbox(label="備註", placeholder="例如:少冰微糖...")
|
|
|
|
| 276 |
github_log_output = gr.Textbox(label="執行日誌", interactive=False, lines=4, elem_classes="github-log")
|
| 277 |
clear_btn = gr.Button("🗑️ 清空所有訂單", variant="stop")
|
| 278 |
|
| 279 |
+
# 事件綁定:回歸最單純的綁定,移除干擾的 api_name
|
| 280 |
inputs_list = [customer_name] + quantity_components + [notes]
|
| 281 |
|
| 282 |
+
submit_btn.click(fn=add_order, inputs=inputs_list, outputs=[status_msg, orders_table, revenue_display])
|
| 283 |
+
login_btn.click(fn=verify_password, inputs=admin_pwd, outputs=[login_pane, overview_pane, login_status])
|
| 284 |
+
logout_btn.click(fn=logout, inputs=[], outputs=[login_pane, overview_pane, login_status, admin_pwd])
|
| 285 |
+
clear_btn.click(fn=clear_orders, inputs=[], outputs=[status_msg, orders_table, revenue_display])
|
| 286 |
+
github_btn.click(fn=backup_and_push_to_github, inputs=[], outputs=[github_log_output])
|
| 287 |
+
refresh_btn.click(fn=refresh_data, inputs=[], outputs=[orders_table, revenue_display])
|
| 288 |
|
| 289 |
# ----------------------------
|
| 290 |
# 🚀 啟動伺服器
|