main.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from fastapi import FastAPI,Cookie, Depends, Query, status,File, UploadFile,Request,Response,HTTPException
  2. from fastapi.templating import Jinja2Templates
  3. from fastapi.responses import HTMLResponse, RedirectResponse, JSONResponse
  4. from typing import List, Optional
  5. from os.path import isfile, isdir, join
  6. import threading
  7. import os
  8. import requests
  9. from bs4 import BeautifulSoup
  10. import asyncio
  11. import urllib.request
  12. from fastapi.responses import FileResponse
  13. from fastapi.middleware.cors import CORSMiddleware
  14. import dataset
  15. from datetime import datetime, timedelta
  16. from fastapi.staticfiles import StaticFiles
  17. import shutil
  18. import io
  19. import pymysql
  20. pymysql.install_as_MySQLdb()
  21. app = FastAPI()
  22. app.add_middleware(
  23. CORSMiddleware,
  24. allow_origins=["*"],
  25. allow_credentials=True,
  26. allow_methods=["*"],
  27. allow_headers=["*"],
  28. )
  29. '''
  30. 資料表: agent_form
  31. 欄位:
  32. cellphone (手機)
  33. customer_note (備註)
  34. '''
  35. # 取得資料庫連線
  36. def get_db_connection():
  37. # 測試機 DB
  38. # db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/stage?charset=utf8mb4')
  39. # 正式機 DB
  40. db = dataset.connect('mysql://hhh7796hhh:lYmWsu^ujcA1@hhh-v57-cluster.cluster-cmab1ctkglka.ap-northeast-2.rds.amazonaws.com:3306/xoops?charset=utf8mb4')
  41. return db
  42. # 檢查已登錄過的客戶手機號碼
  43. @app.get("/check")
  44. async def checkCellphone(cellphone: str = ''):
  45. # 呼叫傳入參數
  46. temp_list = cellphone.split("\n")
  47. # 手機號碼清單
  48. cellphone_list = []
  49. # 避免換行空白問題
  50. for t in temp_list:
  51. if t:
  52. cellphone_list.append(t.strip().replace("-", ""))
  53. cellphone_list = "','".join(cellphone_list)
  54. # 取得資料庫連線
  55. db = get_db_connection()
  56. # 取得手機號碼資料
  57. q = "SELECT * \
  58. FROM agent_form \
  59. WHERE REPLACE(cellphone, '-', '') IN ('" + cellphone_list + "')"
  60. count = len(list(db.query(q)))
  61. if count == 0: # 無手機號碼資料
  62. print('無手機號碼資料')
  63. return {"count": 0, "msg": "無手機號碼資料"}
  64. else:
  65. # 有手機號碼資料
  66. cellphone_list = []
  67. for r1 in db.query(q):
  68. print('手機號碼: ' + r1['cellphone'] + ' 資料已存在')
  69. cellphone_list.append(r1['cellphone'])
  70. return {"count": count, "cellphone": cellphone_list}