Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -55,18 +55,44 @@ except Exception as e:
|
|
| 55 |
custom_api = FastAPI()
|
| 56 |
|
| 57 |
# ----------------------------
|
| 58 |
-
# 🛒 系統資料與動態設定 (菜單、密碼、會員)
|
| 59 |
# ----------------------------
|
| 60 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 61 |
MENU_FILE = os.path.join(BASE_DIR, "menu.json")
|
| 62 |
PWD_FILE = os.path.join(BASE_DIR, "admin_pwd.json")
|
| 63 |
ORDERS_FILE = os.path.join(BASE_DIR, "orders.json")
|
| 64 |
MEMBERS_FILE = os.path.join(BASE_DIR, "members.json")
|
|
|
|
| 65 |
|
| 66 |
BACKUP_FILE = os.path.join(BASE_DIR, "orders_backup.csv")
|
| 67 |
MENU_BACKUP_FILE = os.path.join(BASE_DIR, "menu_backup.csv")
|
| 68 |
MEMBERS_BACKUP_FILE = os.path.join(BASE_DIR, "members_backup.csv")
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
def load_orders():
|
| 71 |
loaded_data = []
|
| 72 |
if os.path.exists(ORDERS_FILE):
|
|
@@ -396,6 +422,42 @@ def change_password(old_pwd, new_pwd):
|
|
| 396 |
save_pwd_to_file(new_pwd.strip())
|
| 397 |
return gr.update(value=f"✅ 密碼修改成功!下次登入請使用新密碼。({datetime.now(TAIPEI_TZ).strftime('%H:%M:%S')})")
|
| 398 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
# ----------------------------
|
| 400 |
# 🤖 LINE Bot 邏輯
|
| 401 |
# ----------------------------
|
|
@@ -981,6 +1043,7 @@ def backup_and_push_to_github():
|
|
| 981 |
log_messages.append(f"💾 已儲存密碼設定至: {PWD_FILE}")
|
| 982 |
log_messages.append(f"💾 已儲存會員資料至: {MEMBERS_FILE}")
|
| 983 |
log_messages.append(f"💾 已儲存訂單資料庫至: {ORDERS_FILE}")
|
|
|
|
| 984 |
|
| 985 |
run_git_cmd('git config user.email "bot@example.com"')
|
| 986 |
run_git_cmd('git config user.name "AutoBackupBot"')
|
|
@@ -1038,14 +1101,6 @@ def autofill_member_form(evt: gr.SelectData, df_data):
|
|
| 1038 |
# 🖥️ 網頁介面 (UI) 與 終極防護注射
|
| 1039 |
# ----------------------------
|
| 1040 |
custom_css = """
|
| 1041 |
-
/* 加入全域背景圖設定 (使用高質感咖啡廳漸層背景) */
|
| 1042 |
-
body, .gradio-container {
|
| 1043 |
-
background-image: linear-gradient(rgba(255, 255, 255, 0.85), rgba(255, 255, 255, 0.85)), url('https://images.unsplash.com/photo-1495474472207-464ba5231c2c?q=80&w=2000&auto=format&fit=crop') !important;
|
| 1044 |
-
background-size: cover !important;
|
| 1045 |
-
background-attachment: fixed !important;
|
| 1046 |
-
background-position: center !important;
|
| 1047 |
-
}
|
| 1048 |
-
|
| 1049 |
/* 強制置中對齊用的 Column */
|
| 1050 |
.center-col { display: flex !important; flex-direction: column !important; justify-content: center !important; align-items: center !important; margin: auto 0 !important; }
|
| 1051 |
|
|
@@ -1141,7 +1196,7 @@ function() {
|
|
| 1141 |
if (document.body) {
|
| 1142 |
Array.from(document.body.children).forEach(el => {
|
| 1143 |
if (el.tagName === 'SCRIPT' || el.tagName === 'STYLE' || el.tagName === 'LINK') return;
|
| 1144 |
-
if (el.classList.contains('gradio-container-wrap') || el.querySelector('.gradio-container') || el.id === 'custom-css-injector') return;
|
| 1145 |
const html = el.innerHTML || '';
|
| 1146 |
if (html.includes('huggingface.co') || html.includes('Sign in') || html.includes('Duplicate Space') || html.includes('Log in')) {
|
| 1147 |
el.style.setProperty('display', 'none', 'important');
|
|
@@ -1173,11 +1228,14 @@ function() {
|
|
| 1173 |
|
| 1174 |
with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo_ui:
|
| 1175 |
gr.HTML(f"<style>{custom_css}</style>", elem_id="custom-css-injector")
|
|
|
|
|
|
|
|
|
|
| 1176 |
|
| 1177 |
menu_trigger = gr.State(value=0)
|
| 1178 |
current_user_state = gr.State(value="")
|
| 1179 |
|
| 1180 |
-
gr.Markdown("<h1 style='text-align: center;'>🛒 簡易訂單管理系統 (LINE 整合版)</h1>")
|
| 1181 |
|
| 1182 |
with gr.Column(visible=True, elem_classes="login-box") as login_container:
|
| 1183 |
gr.Markdown("<h2 style='text-align: center;'>🔐 會員登入</h2>")
|
|
@@ -1557,6 +1615,22 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1557 |
with gr.Column(scale=1, min_width=150):
|
| 1558 |
update_pwd_btn = gr.Button("💾 確認更新密碼", variant="primary")
|
| 1559 |
pwd_update_status = gr.Markdown()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1560 |
|
| 1561 |
# --- Admin 事件綁定 ---
|
| 1562 |
# 訂單過濾
|
|
@@ -1592,6 +1666,36 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo
|
|
| 1592 |
js="(acc) => { if(!acc) return acc; return confirm('⚠️ 確定刪除會員 ' + acc + ' 嗎?') ? acc : ''; }"
|
| 1593 |
)
|
| 1594 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1595 |
# 備份與雜項
|
| 1596 |
github_btn.click(fn=backup_and_push_to_github, inputs=[], outputs=[github_log_output])
|
| 1597 |
restore_btn.click(fn=restore_from_backup, inputs=[], outputs=[global_sys_msg, members_table, menu_trigger, menu_cat_filter, menu_lvl_filter, menu_item_filter])
|
|
|
|
| 55 |
custom_api = FastAPI()
|
| 56 |
|
| 57 |
# ----------------------------
|
| 58 |
+
# 🛒 系統資料與動態設定 (菜單、密碼、會員、外觀)
|
| 59 |
# ----------------------------
|
| 60 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
| 61 |
MENU_FILE = os.path.join(BASE_DIR, "menu.json")
|
| 62 |
PWD_FILE = os.path.join(BASE_DIR, "admin_pwd.json")
|
| 63 |
ORDERS_FILE = os.path.join(BASE_DIR, "orders.json")
|
| 64 |
MEMBERS_FILE = os.path.join(BASE_DIR, "members.json")
|
| 65 |
+
APP_CONFIG_FILE = os.path.join(BASE_DIR, "app_config.json")
|
| 66 |
|
| 67 |
BACKUP_FILE = os.path.join(BASE_DIR, "orders_backup.csv")
|
| 68 |
MENU_BACKUP_FILE = os.path.join(BASE_DIR, "menu_backup.csv")
|
| 69 |
MEMBERS_BACKUP_FILE = os.path.join(BASE_DIR, "members_backup.csv")
|
| 70 |
|
| 71 |
+
def load_app_config():
|
| 72 |
+
if os.path.exists(APP_CONFIG_FILE):
|
| 73 |
+
try:
|
| 74 |
+
with open(APP_CONFIG_FILE, "r", encoding="utf-8") as f:
|
| 75 |
+
return json.load(f)
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"讀取系統設定失敗: {e}")
|
| 78 |
+
|
| 79 |
+
# 預設外觀設定
|
| 80 |
+
return {
|
| 81 |
+
"bg_source": "default", # 'default', 'url', 'upload'
|
| 82 |
+
"bg_url": "",
|
| 83 |
+
"bg_base64": "",
|
| 84 |
+
"bg_opacity": 0.85
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
APP_CONFIG = load_app_config()
|
| 88 |
+
|
| 89 |
+
def save_app_config():
|
| 90 |
+
try:
|
| 91 |
+
with open(APP_CONFIG_FILE, "w", encoding="utf-8") as f:
|
| 92 |
+
json.dump(APP_CONFIG, f, ensure_ascii=False, indent=4)
|
| 93 |
+
except Exception as e:
|
| 94 |
+
print(f"儲存系統設定失敗: {e}")
|
| 95 |
+
|
| 96 |
def load_orders():
|
| 97 |
loaded_data = []
|
| 98 |
if os.path.exists(ORDERS_FILE):
|
|
|
|
| 422 |
save_pwd_to_file(new_pwd.strip())
|
| 423 |
return gr.update(value=f"✅ 密碼修改成功!下次登入請使用新密碼。({datetime.now(TAIPEI_TZ).strftime('%H:%M:%S')})")
|
| 424 |
|
| 425 |
+
# ============================
|
| 426 |
+
# 🎨 動態背景樣式產生函數
|
| 427 |
+
# ============================
|
| 428 |
+
def generate_bg_css(config=None):
|
| 429 |
+
if config is None:
|
| 430 |
+
config = APP_CONFIG
|
| 431 |
+
|
| 432 |
+
opacity = config.get("bg_opacity", 0.85)
|
| 433 |
+
bg_source = config.get("bg_source", "default")
|
| 434 |
+
|
| 435 |
+
if bg_source == "default":
|
| 436 |
+
bg_url = "https://images.unsplash.com/photo-1495474472207-464ba5231c2c?q=80&w=2000&auto=format&fit=crop"
|
| 437 |
+
elif bg_source == "url":
|
| 438 |
+
bg_url = config.get("bg_url", "")
|
| 439 |
+
if not bg_url:
|
| 440 |
+
bg_url = "https://images.unsplash.com/photo-1495474472207-464ba5231c2c?q=80&w=2000&auto=format&fit=crop"
|
| 441 |
+
elif bg_source == "upload":
|
| 442 |
+
b64 = config.get("bg_base64", "")
|
| 443 |
+
if b64:
|
| 444 |
+
bg_url = f"data:image/jpeg;base64,{b64}"
|
| 445 |
+
else:
|
| 446 |
+
bg_url = "https://images.unsplash.com/photo-1495474472207-464ba5231c2c?q=80&w=2000&auto=format&fit=crop"
|
| 447 |
+
else:
|
| 448 |
+
bg_url = "https://images.unsplash.com/photo-1495474472207-464ba5231c2c?q=80&w=2000&auto=format&fit=crop"
|
| 449 |
+
|
| 450 |
+
return f"""
|
| 451 |
+
<style>
|
| 452 |
+
body, .gradio-container {{
|
| 453 |
+
background-image: linear-gradient(rgba(255, 255, 255, {opacity}), rgba(255, 255, 255, {opacity})), url('{bg_url}') !important;
|
| 454 |
+
background-size: cover !important;
|
| 455 |
+
background-attachment: fixed !important;
|
| 456 |
+
background-position: center !important;
|
| 457 |
+
}}
|
| 458 |
+
</style>
|
| 459 |
+
"""
|
| 460 |
+
|
| 461 |
# ----------------------------
|
| 462 |
# 🤖 LINE Bot 邏輯
|
| 463 |
# ----------------------------
|
|
|
|
| 1043 |
log_messages.append(f"💾 已儲存密碼設定至: {PWD_FILE}")
|
| 1044 |
log_messages.append(f"💾 已儲存會員資料至: {MEMBERS_FILE}")
|
| 1045 |
log_messages.append(f"💾 已儲存訂單資料庫至: {ORDERS_FILE}")
|
| 1046 |
+
log_messages.append(f"💾 已儲存外觀設定至: {APP_CONFIG_FILE}")
|
| 1047 |
|
| 1048 |
run_git_cmd('git config user.email "bot@example.com"')
|
| 1049 |
run_git_cmd('git config user.name "AutoBackupBot"')
|
|
|
|
| 1101 |
# 🖥️ 網頁介面 (UI) 與 終極防護注射
|
| 1102 |
# ----------------------------
|
| 1103 |
custom_css = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1104 |
/* 強制置中對齊用的 Column */
|
| 1105 |
.center-col { display: flex !important; flex-direction: column !important; justify-content: center !important; align-items: center !important; margin: auto 0 !important; }
|
| 1106 |
|
|
|
|
| 1196 |
if (document.body) {
|
| 1197 |
Array.from(document.body.children).forEach(el => {
|
| 1198 |
if (el.tagName === 'SCRIPT' || el.tagName === 'STYLE' || el.tagName === 'LINK') return;
|
| 1199 |
+
if (el.classList.contains('gradio-container-wrap') || el.querySelector('.gradio-container') || el.id === 'custom-css-injector' || el.id === 'dynamic-bg-css-injector') return;
|
| 1200 |
const html = el.innerHTML || '';
|
| 1201 |
if (html.includes('huggingface.co') || html.includes('Sign in') || html.includes('Duplicate Space') || html.includes('Log in')) {
|
| 1202 |
el.style.setProperty('display', 'none', 'important');
|
|
|
|
| 1228 |
|
| 1229 |
with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統") as demo_ui:
|
| 1230 |
gr.HTML(f"<style>{custom_css}</style>", elem_id="custom-css-injector")
|
| 1231 |
+
|
| 1232 |
+
# 動態注入背景 CSS
|
| 1233 |
+
dynamic_bg_html = gr.HTML(value=generate_bg_css(), elem_id="dynamic-bg-css-injector")
|
| 1234 |
|
| 1235 |
menu_trigger = gr.State(value=0)
|
| 1236 |
current_user_state = gr.State(value="")
|
| 1237 |
|
| 1238 |
+
gr.Markdown("<h1 style='text-align: center; margin-top: 20px;'>🛒 簡易訂單管理系統 (LINE 整合版)</h1>")
|
| 1239 |
|
| 1240 |
with gr.Column(visible=True, elem_classes="login-box") as login_container:
|
| 1241 |
gr.Markdown("<h2 style='text-align: center;'>🔐 會員登入</h2>")
|
|
|
|
| 1615 |
with gr.Column(scale=1, min_width=150):
|
| 1616 |
update_pwd_btn = gr.Button("💾 確認更新密碼", variant="primary")
|
| 1617 |
pwd_update_status = gr.Markdown()
|
| 1618 |
+
|
| 1619 |
+
with gr.TabItem("🎨 外觀設定"):
|
| 1620 |
+
gr.Markdown("### 🖼️ 系統背景與透明度設定\n您可以自訂網頁的背景圖片,以及白色遮罩的不透明度。若選擇上傳圖片,檔案將自動轉存並套用。")
|
| 1621 |
+
|
| 1622 |
+
init_source = APP_CONFIG.get("bg_source", "default")
|
| 1623 |
+
init_source_str = {"default": "預設圖片", "url": "自訂網址", "upload": "上傳圖片"}.get(init_source, "預設圖片")
|
| 1624 |
+
|
| 1625 |
+
with gr.Row():
|
| 1626 |
+
with gr.Column():
|
| 1627 |
+
bg_source_radio = gr.Radio(choices=["預設圖片", "自訂網址", "上傳圖片"], label="背景圖來源", value=init_source_str, interactive=True)
|
| 1628 |
+
bg_url_input = gr.Textbox(label="圖片網址", value=APP_CONFIG.get("bg_url", ""), visible=(init_source_str == "自訂網址"), interactive=True)
|
| 1629 |
+
bg_upload_input = gr.Image(type="filepath", label="上傳背景圖", visible=(init_source_str == "上傳圖片"), interactive=True)
|
| 1630 |
+
with gr.Column():
|
| 1631 |
+
bg_opacity_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, label="白色遮罩不透明度 (數值越高背景越白越不明顯)", value=APP_CONFIG.get("bg_opacity", 0.85), interactive=True)
|
| 1632 |
+
save_bg_btn = gr.Button("💾 儲存外觀設定並套用", variant="primary")
|
| 1633 |
+
bg_status_msg = gr.Markdown()
|
| 1634 |
|
| 1635 |
# --- Admin 事件綁定 ---
|
| 1636 |
# 訂單過濾
|
|
|
|
| 1666 |
js="(acc) => { if(!acc) return acc; return confirm('⚠️ 確定刪除會員 ' + acc + ' 嗎?') ? acc : ''; }"
|
| 1667 |
)
|
| 1668 |
|
| 1669 |
+
# 外觀設定操作
|
| 1670 |
+
bg_source_radio.change(
|
| 1671 |
+
fn=lambda s: (gr.update(visible=(s=="自訂網址")), gr.update(visible=(s=="上傳圖片"))),
|
| 1672 |
+
inputs=[bg_source_radio], outputs=[bg_url_input, bg_upload_input]
|
| 1673 |
+
)
|
| 1674 |
+
|
| 1675 |
+
def update_background(source, url, img_path, opacity):
|
| 1676 |
+
global APP_CONFIG
|
| 1677 |
+
src_map = {"預設圖片": "default", "自訂網址": "url", "上傳圖片": "upload"}
|
| 1678 |
+
APP_CONFIG["bg_source"] = src_map.get(source, "default")
|
| 1679 |
+
APP_CONFIG["bg_url"] = str(url).strip() if url else ""
|
| 1680 |
+
APP_CONFIG["bg_opacity"] = float(opacity)
|
| 1681 |
+
|
| 1682 |
+
if APP_CONFIG["bg_source"] == "upload" and img_path:
|
| 1683 |
+
try:
|
| 1684 |
+
with open(img_path, "rb") as f:
|
| 1685 |
+
b64 = base64.b64encode(f.read()).decode("utf-8")
|
| 1686 |
+
APP_CONFIG["bg_base64"] = b64
|
| 1687 |
+
except Exception as e:
|
| 1688 |
+
return gr.update(), f"❌ 圖片處理失敗: {str(e)}"
|
| 1689 |
+
|
| 1690 |
+
save_app_config()
|
| 1691 |
+
return generate_bg_css(APP_CONFIG), f"✅ 外觀設定已更新!({datetime.now(TAIPEI_TZ).strftime('%H:%M:%S')})"
|
| 1692 |
+
|
| 1693 |
+
save_bg_btn.click(
|
| 1694 |
+
fn=update_background,
|
| 1695 |
+
inputs=[bg_source_radio, bg_url_input, bg_upload_input, bg_opacity_slider],
|
| 1696 |
+
outputs=[dynamic_bg_html, bg_status_msg]
|
| 1697 |
+
)
|
| 1698 |
+
|
| 1699 |
# 備份與雜項
|
| 1700 |
github_btn.click(fn=backup_and_push_to_github, inputs=[], outputs=[github_log_output])
|
| 1701 |
restore_btn.click(fn=restore_from_backup, inputs=[], outputs=[global_sys_msg, members_table, menu_trigger, menu_cat_filter, menu_lvl_filter, menu_item_filter])
|