app.py 266 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145
  1. import os
  2. import pymysql
  3. import requests
  4. import json
  5. import re
  6. import threading
  7. import urllib3
  8. import fitz # PyMuPDF
  9. import base64
  10. from flask import Flask, render_template, request, redirect, url_for, session, flash, jsonify, Response, stream_with_context
  11. from werkzeug.utils import secure_filename
  12. from oss_utils import upload_to_oss
  13. from ocr_utils import extract_page_number
  14. import time
  15. from datetime import datetime
  16. # Suppress InsecureRequestWarning
  17. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  18. app = Flask(__name__, static_folder='static', static_url_path='/manager/static')
  19. app.secret_key = 'genealogy_secret_key'
  20. app.config['UPLOAD_FOLDER'] = 'uploads'
  21. os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
  22. # 数据库配置
  23. DB_CONFIG = {
  24. "host": "rm-f8ze60yirdj8786u2wo.mysql.rds.aliyuncs.com",
  25. "port": 3306,
  26. "user": "root",
  27. "password": "csqz@20255",
  28. "db": "csqz-client",
  29. "charset": "utf8mb4",
  30. "cursorclass": pymysql.cursors.DictCursor
  31. }
  32. # 微信小程序配置
  33. WECHAT_APP_ID = "wx98f5cf1c60f793b8"
  34. WECHAT_APP_SECRET = "3d34d5be301f893fe86349122deada65"
  35. # Access Token 缓存
  36. access_token = None
  37. access_token_expire_time = 0
  38. access_token_lock = threading.Lock()
  39. # 图片扩展名列表
  40. IMAGE_EXTENSIONS = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.webp', '.tiff'}
  41. def add_oss_watermark(url, username=None):
  42. """
  43. 为图片URL添加阿里云OSS水印
  44. :param url: 原始图片URL
  45. :param username: 当前登录用户名,如果未提供则使用默认值
  46. :return: 添加水印后的URL,如果不是图片则返回原始URL
  47. """
  48. if not url:
  49. return url
  50. # 检查是否已经有水印参数
  51. if 'x-oss-process=image/watermark' in url:
  52. return url
  53. # 检查是否为图片格式
  54. lower_url = url.lower()
  55. is_image = any(lower_url.endswith(ext) for ext in IMAGE_EXTENSIONS)
  56. if not is_image:
  57. return url
  58. # 生成水印内容:用户名_时间戳
  59. if not username:
  60. username = 'genealogy'
  61. timestamp = int(time.time())
  62. watermark_text = f"{username}_{timestamp}"
  63. # 对水印文字进行base64编码(需要URL安全的base64)
  64. try:
  65. encoded_text = base64.b64encode(watermark_text.encode('utf-8')).decode('utf-8')
  66. # 替换URL不安全的字符
  67. encoded_text = encoded_text.replace('+', '-').replace('/', '_').replace('=', '')
  68. except Exception as e:
  69. print(f"[Watermark] Error encoding watermark text: {e}")
  70. return url
  71. # 构建水印参数
  72. watermark_params = f"?x-oss-process=image/watermark,text_{encoded_text},type_d3F5LXplbmhlaQ,size_30,t_30,g_nw,x_50,y_50,rotate_30"
  73. # 添加水印参数到URL
  74. if '?' in url:
  75. # 如果URL已有参数,使用&连接
  76. return f"{url}&{watermark_params[1:]}"
  77. else:
  78. return f"{url}{watermark_params}"
  79. def get_wechat_access_token():
  80. """获取微信小程序access_token,带缓存和线程安全"""
  81. global access_token, access_token_expire_time
  82. with access_token_lock:
  83. # 检查缓存是否有效(提前1小时刷新)
  84. now = time.time()
  85. if access_token and access_token_expire_time > now + 3600:
  86. return access_token
  87. # 需要获取新的access_token
  88. url = "https://api.weixin.qq.com/cgi-bin/token"
  89. params = {
  90. "grant_type": "client_credential",
  91. "appid": WECHAT_APP_ID,
  92. "secret": WECHAT_APP_SECRET
  93. }
  94. try:
  95. response = requests.get(url, params=params, timeout=30)
  96. data = response.json()
  97. if 'access_token' in data:
  98. access_token = data['access_token']
  99. expires_in = data.get('expires_in', 7200)
  100. access_token_expire_time = now + expires_in
  101. print(f"[WeChat API] Access token obtained, expires in {expires_in} seconds")
  102. return access_token
  103. else:
  104. print(f"[WeChat API] Failed to get access_token: {data}")
  105. return None
  106. except Exception as e:
  107. print(f"[WeChat API] Error getting access_token: {e}")
  108. return None
  109. def decrypt_wechat_phone(encrypted_data, iv, session_key):
  110. """解密微信手机号(需要使用官方解密库)"""
  111. try:
  112. from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
  113. from cryptography.hazmat.backends import default_backend
  114. import base64
  115. # AES解密
  116. session_key = base64.b64decode(session_key)
  117. encrypted_data = base64.b64decode(encrypted_data)
  118. iv = base64.b64decode(iv)
  119. cipher = Cipher(algorithms.AES(session_key), modes.CBC(iv), backend=default_backend())
  120. decryptor = cipher.decryptor()
  121. decrypted = decryptor.update(encrypted_data) + decryptor.finalize()
  122. # PKCS7 padding去除
  123. padding = ord(decrypted[-1:])
  124. decrypted = decrypted[:-padding]
  125. return json.loads(decrypted.decode('utf-8'))
  126. except Exception as e:
  127. print(f"[WeChat Decrypt] Error decrypting phone: {e}")
  128. return None
  129. from PIL import Image
  130. def compress_image_if_needed(file_path, max_dim=2000):
  131. """Compress, resize and normalize image to JPEG for AI processing."""
  132. try:
  133. # We always want to normalize to JPEG so AI doesn't complain about format
  134. with Image.open(file_path) as img:
  135. # Convert RGBA/P or any other mode to RGB for JPEG saving
  136. if img.mode != 'RGB':
  137. img = img.convert('RGB')
  138. width, height = img.size
  139. if max(width, height) > max_dim:
  140. ratio = max_dim / max(width, height)
  141. new_size = (int(width * ratio), int(height * ratio))
  142. img = img.resize(new_size, Image.Resampling.LANCZOS)
  143. # Always save as JPEG to normalize the format
  144. new_path = os.path.splitext(file_path)[0] + '_normalized.jpg'
  145. img.save(new_path, 'JPEG', quality=85)
  146. return new_path
  147. except Exception as e:
  148. print(f"Warning: Image compression/normalization failed for {file_path}: {e}")
  149. return file_path
  150. # 尝试使用数据库连接池,如果不可用则使用普通连接
  151. try:
  152. try:
  153. from dbutils.pooled_db import PooledDB # dbutils >= 2.0(新包名)
  154. except ImportError:
  155. from DBUtils.PooledDB import PooledDB # DBUtils <= 1.x(旧包名)
  156. # 创建连接池
  157. pool = PooledDB(
  158. creator=pymysql,
  159. maxconnections=10, # 连接池最大连接数
  160. mincached=2, # 初始化时创建的空闲连接数
  161. maxcached=5, # 最大空闲连接数
  162. maxshared=3, # 最大共享连接数
  163. blocking=True, # 连接池满时是否阻塞等待
  164. maxusage=1000, # 一个连接最多被重复使用的次数,防止连接长时间使用失效
  165. setsession=[], # 开始会话前执行的命令列表
  166. ping=1, # 每次获取连接时都检查连接是否可用
  167. **DB_CONFIG
  168. )
  169. def get_db_connection():
  170. conn = pool.connection()
  171. print(f"[Database] Got connection from pool: {id(conn)}")
  172. return conn
  173. print("[Database] Database connection pool initialized successfully")
  174. except ImportError:
  175. # 如果DBUtils不可用,使用普通连接
  176. def get_db_connection():
  177. conn = pymysql.connect(**DB_CONFIG)
  178. print(f"[Database] Created new connection: {id(conn)}")
  179. return conn
  180. print("[Database] DBUtils not available, using regular database connections")
  181. def get_mp_user_from_token(token):
  182. """通过 token 获取小程序用户信息,返回 mp_users 行或 None"""
  183. if not token:
  184. return None
  185. conn = get_db_connection()
  186. try:
  187. with conn.cursor() as cursor:
  188. cursor.execute("SELECT id, openid, phone FROM mp_users WHERE token = %s", (token,))
  189. return cursor.fetchone()
  190. except Exception:
  191. return None
  192. finally:
  193. conn.close()
  194. def verify_connection(conn):
  195. """Verify database connection is still alive"""
  196. try:
  197. cursor = conn.cursor()
  198. cursor.execute("SELECT 1")
  199. cursor.fetchone()
  200. cursor.close()
  201. return True
  202. except Exception as e:
  203. print(f"[Database] Connection verification failed: {e}")
  204. return False
  205. def safe_commit(conn):
  206. """Safely commit transaction with error handling"""
  207. try:
  208. conn.commit()
  209. print(f"[Database] Transaction committed successfully")
  210. return True
  211. except Exception as e:
  212. print(f"[Database] Commit failed: {e}")
  213. try:
  214. conn.rollback()
  215. print(f"[Database] Rollback completed")
  216. except Exception as rollback_err:
  217. print(f"[Database] Rollback also failed: {rollback_err}")
  218. return False
  219. def format_timestamp(ts):
  220. if not ts: return '未知'
  221. try:
  222. # 兼容秒和毫秒
  223. if ts > 10000000000: # 超过2286年的秒数,通常认为是毫秒
  224. ts = ts / 1000
  225. return time.strftime('%Y-%m-%d', time.localtime(ts))
  226. except:
  227. return '未知'
  228. def manual_simplify(text):
  229. """
  230. Simple fallback for common Traditional to Simplified conversion
  231. if AI fails to convert specific characters.
  232. """
  233. if not text: return text
  234. mapping = {
  235. '學': '学', '國': '国', '萬': '万', '寶': '宝', '興': '兴',
  236. '華': '华', '會': '会', '葉': '叶', '藝': '艺', '號': '号',
  237. '處': '处', '見': '见', '視': '视', '言': '言', '語': '语',
  238. '貝': '贝', '車': '车', '長': '长', '門': '门', '韋': '韦',
  239. '頁': '页', '風': '风', '飛': '飞', '食': '食', '馬': '马',
  240. '魚': '鱼', '鳥': '鸟', '麥': '麦', '黃': '黄', '齊': '齐',
  241. '齒': '齿', '龍': '龙', '龜': '龟', '壽': '寿', '榮': '荣',
  242. '愛': '爱', '慶': '庆', '衛': '卫', '賢': '贤', '義': '义',
  243. '禮': '礼', '樂': '乐', '靈': '灵', '滅': '灭', '氣': '气',
  244. '智': '智', '信': '信', '仁': '仁', '勇': '勇', '嚴': '严',
  245. '銳': '锐', '優': '优', '楊': '杨', '吳': '吴', '銀': '银'
  246. }
  247. result = ""
  248. for char in text:
  249. result += mapping.get(char, char)
  250. return result
  251. def convert_to_simplified(text):
  252. """繁体转简体,优先使用 zhconv 库,失败则降级到 manual_simplify"""
  253. if not text:
  254. return text
  255. try:
  256. import zhconv
  257. return zhconv.convert(text, 'zh-hans')
  258. except Exception:
  259. return manual_simplify(text)
  260. def _build_reverse_simplify_map():
  261. """
  262. Build a reverse map from simplified char -> list of traditional chars
  263. based on the fallback manual_simplify mapping.
  264. """
  265. mapping = {
  266. '學': '学', '國': '国', '萬': '万', '寶': '宝', '興': '兴',
  267. '華': '华', '會': '会', '葉': '叶', '藝': '艺', '號': '号',
  268. '處': '处', '見': '见', '視': '视', '言': '言', '語': '语',
  269. '貝': '贝', '車': '车', '長': '长', '門': '门', '韋': '韦',
  270. '頁': '页', '風': '风', '飛': '飞', '食': '食', '馬': '马',
  271. '魚': '鱼', '鳥': '鸟', '麥': '麦', '黃': '黄', '齊': '齐',
  272. '齒': '齿', '龍': '龙', '龜': '龟', '壽': '寿', '榮': '荣',
  273. '愛': '爱', '慶': '庆', '衛': '卫', '賢': '贤', '義': '义',
  274. '禮': '礼', '樂': '乐', '靈': '灵', '滅': '灭', '氣': '气',
  275. '智': '智', '信': '信', '仁': '仁', '勇': '勇', '嚴': '严',
  276. '銳': '锐', '優': '优', '楊': '杨', '吳': '吴', '銀': '银'
  277. }
  278. rev = {}
  279. for trad, simp in mapping.items():
  280. rev.setdefault(simp, [])
  281. if trad not in rev[simp]:
  282. rev[simp].append(trad)
  283. return rev
  284. _REVERSE_SIMPLIFY_MAP = _build_reverse_simplify_map()
  285. def expand_name_search_variants(keyword, max_variants=60):
  286. """
  287. Expand keyword into a small set of variants so Simplified/Traditional
  288. searches can match both `name` and `simplified_name`.
  289. - Always includes original keyword
  290. - Includes fallback-trad->simp conversion
  291. - Includes best-effort simp->trad expansions based on reverse map
  292. """
  293. if not keyword:
  294. return []
  295. kw = str(keyword).strip()
  296. if not kw:
  297. return []
  298. variants = set([kw])
  299. variants.add(manual_simplify(kw))
  300. # Build possible traditional variants when the input is simplified.
  301. # For each char, if we have traditional candidates, branch; otherwise keep itself.
  302. choices = []
  303. for ch in kw:
  304. cand = _REVERSE_SIMPLIFY_MAP.get(ch)
  305. if cand:
  306. # include itself too (covers already-traditional or neutral chars)
  307. choices.append([ch] + cand)
  308. else:
  309. choices.append([ch])
  310. # Cartesian product with early stop.
  311. results = ['']
  312. for opts in choices:
  313. new_results = []
  314. for prefix in results:
  315. for opt in opts:
  316. new_results.append(prefix + opt)
  317. if len(new_results) >= max_variants:
  318. break
  319. if len(new_results) >= max_variants:
  320. break
  321. results = new_results
  322. if len(results) >= max_variants:
  323. break
  324. for r in results:
  325. if r:
  326. variants.add(r)
  327. variants.add(manual_simplify(r))
  328. # Keep deterministic order for stable SQL params
  329. ordered = []
  330. for v in variants:
  331. v2 = (v or '').strip()
  332. if v2 and v2 not in ordered:
  333. ordered.append(v2)
  334. if len(ordered) >= max_variants:
  335. break
  336. return ordered
  337. def clean_name(name):
  338. """
  339. Clean name according to Liu family genealogy rules:
  340. 1. If name is '学公' or '留学公', keep 'Gong' (exception).
  341. 2. Otherwise, if name ends with '公', remove '公'.
  342. 3. If name does not start with '留', prepend '留'.
  343. """
  344. if not name: return name
  345. name = name.strip()
  346. # Pre-process: Ensure Simplified Chinese for specific chars
  347. name = manual_simplify(name)
  348. # 1. Check exceptions (names that SHOULD keep 'Gong')
  349. exceptions = ['学公', '留学公']
  350. if name in exceptions:
  351. if not name.startswith('留'):
  352. name = '留' + name
  353. return name
  354. # 2. General Rule: Remove 'Gong' suffix
  355. if name.endswith('公'):
  356. name = name[:-1]
  357. # 3. Ensure 'Liu' surname
  358. if not name.startswith('留'):
  359. name = '留' + name
  360. return name
  361. def is_female_value(sex_value):
  362. """Return True when sex value represents female."""
  363. if sex_value is None:
  364. return False
  365. s = str(sex_value).strip().lower()
  366. return s in ('女', '2', 'female', 'f')
  367. def normalize_lookup_name(name):
  368. """Normalize names for loose matching in AI parsed content."""
  369. if not name:
  370. return ''
  371. return manual_simplify(str(name)).strip()
  372. def should_skip_liu_prefix_for_person(person, spouse_name_set):
  373. """
  374. Female spouse records should not auto-prepend '留' in simplified_name.
  375. We treat a person as female spouse if:
  376. 1) sex is female, and
  377. 2) has spouse_name field OR appears in another person's spouse_name list.
  378. """
  379. if not isinstance(person, dict):
  380. return False
  381. if not is_female_value(person.get('sex')):
  382. return False
  383. own_names = set()
  384. own_names.add(normalize_lookup_name(person.get('name')))
  385. own_names.add(normalize_lookup_name(person.get('original_name')))
  386. own_names.discard('')
  387. has_spouse_name = bool(normalize_lookup_name(person.get('spouse_name')))
  388. referenced_by_other = any(n in spouse_name_set for n in own_names)
  389. return has_spouse_name or referenced_by_other
  390. def get_normalized_base64_image(image_url):
  391. """Download image, normalize to JPEG, and return base64 data URI for AI payload."""
  392. import io
  393. import base64
  394. import requests
  395. from PIL import Image
  396. try:
  397. response = requests.get(image_url, timeout=30)
  398. response.raise_for_status()
  399. with Image.open(io.BytesIO(response.content)) as img:
  400. # Convert to RGB to ensure JPEG compatibility
  401. if img.mode != 'RGB':
  402. img = img.convert('RGB')
  403. # Resize if too large
  404. max_dim = 2000
  405. if max(img.width, img.height) > max_dim:
  406. ratio = max_dim / max(img.width, img.height)
  407. new_size = (int(img.width * ratio), int(img.height * ratio))
  408. img = img.resize(new_size, Image.Resampling.LANCZOS)
  409. # Save as JPEG in memory
  410. buffer = io.BytesIO()
  411. img.save(buffer, format='JPEG', quality=85)
  412. b64_str = base64.b64encode(buffer.getvalue()).decode('utf-8')
  413. return f"data:image/jpeg;base64,{b64_str}"
  414. except Exception as e:
  415. print(f"Error normalizing image from {image_url}: {e}")
  416. return image_url # Fallback to original URL if processing fails
  417. def process_ai_task(record_id, image_url):
  418. """Background task to process image with AI and store result."""
  419. print(f"[AI Task] Starting task for record {record_id}...")
  420. conn = get_db_connection()
  421. try:
  422. with conn.cursor() as cursor:
  423. cursor.execute("UPDATE genealogy_records SET ai_status = 1 WHERE id = %s", (record_id,))
  424. conn.commit()
  425. print(f"[AI Task] Status updated to 'Processing' for record {record_id}")
  426. api_key = "a1800657-9212-4afe-9b7c-b49f015c54d3"
  427. api_url = "https://ark.cn-beijing.volces.com/api/v3/responses"
  428. prompt = """
  429. 请分析这张家谱图片,提取其中关于人物的信息。
  430. 请务必将繁体字转换为简体字(original_name 字段除外)。
  431. 特别注意:'name' 字段必须是纯简体中文,不能包含繁体字(例如:'學'应转换为'学','劉'应转换为'刘','萬'应转换为'万')。
  432. 请提取以下字段(如果存在):
  433. - original_name: 原始姓名(严格保持图片上的繁体字,不做任何修改或转换)
  434. - name: 简体姓名(必须转换为简体中文,去除不需要的敬称)
  435. - sex: 性别(男/女)
  436. - birthday: 出生日期(尝试转换为YYYY-MM-DD格式,如果无法确定年份可只填月日)
  437. - death_date: 逝世日期(如文本中出现“殁”、“葬”、“卒”等字眼及其对应的时间,请提取)
  438. - father_name: 父亲姓名
  439. - spouse_name: 配偶姓名
  440. - generation: 第几世/代数
  441. - name_word: 字辈(例如名字为“学勤公”,“学”为字辈;提取名字中的字辈信息)
  442. - education: 学历/功名
  443. - title: 官职/称号
  444. 请严格以JSON列表格式返回,不要包含Markdown代码块标记(如 ```json ... ```),直接返回JSON数组。
  445. 如果包含多个人物,请都提取出来。
  446. Do not output any reasoning or explanation, just the JSON.
  447. """
  448. ai_payload_url = get_normalized_base64_image(image_url)
  449. payload = {
  450. "model": "doubao-seed-1-8-251228",
  451. "stream": True, # Streaming for robust handling
  452. "input": [
  453. {
  454. "role": "user",
  455. "content": [
  456. {"type": "input_image", "image_url": ai_payload_url},
  457. {"type": "input_text", "text": prompt}
  458. ]
  459. }
  460. ]
  461. }
  462. headers = {
  463. "Authorization": f"Bearer {api_key}",
  464. "Content-Type": "application/json"
  465. }
  466. max_retries = 3
  467. last_exception = None
  468. for attempt in range(max_retries):
  469. try:
  470. print(f"[AI Task] Attempt {attempt+1}/{max_retries} connecting to API for record {record_id}...")
  471. response = requests.post(
  472. api_url,
  473. json=payload,
  474. headers=headers,
  475. timeout=1200,
  476. stream=True,
  477. verify=False,
  478. proxies={"http": None, "https": None}
  479. )
  480. if response.status_code == 200:
  481. print(f"[AI Task] Connection established for record {record_id}, receiving stream...")
  482. full_content = ""
  483. for line in response.iter_lines():
  484. if not line: continue
  485. line_str = line.decode('utf-8')
  486. # Debug: Print full line to understand event flow
  487. print(f"[AI Task Debug] Raw Line: {line_str[:500]}") # Truncate very long lines
  488. if line_str.startswith('data: '):
  489. json_str = line_str[6:]
  490. if json_str.strip() == '[DONE]':
  491. print("[AI Task Debug] Received [DONE]")
  492. break
  493. try:
  494. chunk = json.loads(json_str)
  495. chunk_type = chunk.get('type')
  496. # Standard OpenAI format (choices)
  497. if 'choices' in chunk and len(chunk['choices']) > 0:
  498. delta = chunk['choices'][0].get('delta', {})
  499. if 'content' in delta:
  500. full_content += delta['content']
  501. # Doubao/Volcengine specific formats (delta)
  502. elif chunk_type == 'response.text.delta':
  503. full_content += chunk.get('delta', '')
  504. # Check response.completed if empty
  505. elif chunk_type == 'response.completed' and not full_content:
  506. output = chunk.get('response', {}).get('output', [])
  507. for item in output:
  508. # Also extract from reasoning if it contains JSON-like text
  509. if item.get('type') == 'reasoning':
  510. summary = item.get('summary', [])
  511. for sum_item in summary:
  512. if sum_item.get('type') == 'summary_text':
  513. full_content += sum_item.get('text', '')
  514. elif item.get('type') == 'message':
  515. content = item.get('content')
  516. if isinstance(content, str):
  517. full_content += content
  518. elif isinstance(content, list):
  519. for part in content:
  520. if isinstance(part, dict) and part.get('type') == 'text':
  521. full_content += part.get('text', '')
  522. # Fallback: output_item.added
  523. elif chunk_type == 'response.output_item.added':
  524. item = chunk.get('item', {})
  525. if item.get('role') == 'assistant':
  526. content_field = item.get('content', [])
  527. if isinstance(content_field, str):
  528. full_content += content_field
  529. elif isinstance(content_field, list):
  530. for part in content_field:
  531. if isinstance(part, dict) and part.get('type') == 'text':
  532. full_content += part.get('text', '')
  533. except Exception as e:
  534. print(f"[AI Task] Chunk parse error: {e}")
  535. else:
  536. # Fallback for non-SSE
  537. try:
  538. chunk = json.loads(line_str)
  539. if 'choices' in chunk and len(chunk['choices']) > 0:
  540. content = chunk['choices'][0]['message']['content']
  541. full_content += content
  542. except:
  543. pass
  544. print(f"[AI Task] Stream finished. Content length: {len(full_content)}")
  545. if len(full_content) == 0:
  546. print(f"[AI Task] WARNING: No content received from AI stream.")
  547. # Continue to JSON parse to fail gracefully
  548. # Clean JSON
  549. try:
  550. # 1. Try finding [...] array
  551. start = full_content.find('[')
  552. end = full_content.rfind(']')
  553. # 2. If not found, try finding {...} object and wrap it
  554. is_single_object = False
  555. if start == -1 or end == -1 or end <= start:
  556. start = full_content.find('{')
  557. end = full_content.rfind('}')
  558. is_single_object = True
  559. if start != -1 and end != -1 and end > start:
  560. content_clean = full_content[start:end+1]
  561. else:
  562. # Fallback to regex or raw
  563. content_clean = re.sub(r'^```json\s*', '', full_content)
  564. content_clean = re.sub(r'```$', '', content_clean)
  565. parsed = json.loads(content_clean)
  566. # Normalize single object to list
  567. if is_single_object and isinstance(parsed, dict):
  568. parsed = [parsed]
  569. content_clean = json.dumps(parsed, ensure_ascii=False)
  570. elif isinstance(parsed, dict) and not isinstance(parsed, list):
  571. # Just in case json.loads parsed a dict even if we looked for []
  572. parsed = [parsed]
  573. content_clean = json.dumps(parsed, ensure_ascii=False)
  574. # Build spouse name lookup for "female spouse" detection
  575. spouse_name_set = set()
  576. if isinstance(parsed, list):
  577. for person in parsed:
  578. n = normalize_lookup_name(person.get('spouse_name'))
  579. if n:
  580. spouse_name_set.add(n)
  581. # Clean names in parsed content
  582. if isinstance(parsed, list):
  583. for person in parsed:
  584. # Process Name: 'name' is Simplified from AI, 'original_name' is Traditional/Raw from AI
  585. simplified_name = person.get('name', '') or person.get('original_name', '')
  586. original_name = person.get('original_name', '')
  587. # Female spouse: only simplify Chinese, do NOT prepend '留'
  588. if should_skip_liu_prefix_for_person(person, spouse_name_set):
  589. cleaned_simplified = manual_simplify(simplified_name)
  590. else:
  591. # Same-clan default: prepend '留' and handle trailing '公'
  592. cleaned_simplified = clean_name(simplified_name)
  593. person['simplified_name'] = cleaned_simplified
  594. # Store raw name in 'name' field (as requested)
  595. if original_name:
  596. person['name'] = original_name
  597. else:
  598. # Fallback: if no original_name returned, use the uncleaned name as 'name'
  599. # or keep existing logic. But user wants raw in 'name'.
  600. # If AI didn't return original_name, 'name' is likely simplified.
  601. pass # Keep 'name' as is (which is Simplified) if original_name missing
  602. # Father name:同族,需要按“留”姓规则清洗
  603. if 'father_name' in person and person['father_name']:
  604. person['father_name'] = clean_name(person['father_name'])
  605. # Spouse name:只做繁转简,不拼接“留”姓,也不去“公”
  606. if 'spouse_name' in person and person['spouse_name']:
  607. person['spouse_name'] = manual_simplify(person['spouse_name'])
  608. # Re-serialize
  609. content_clean = json.dumps(parsed, ensure_ascii=False)
  610. with conn.cursor() as cursor:
  611. cursor.execute("UPDATE genealogy_records SET ai_status = 2, ai_content = %s WHERE id = %s", (content_clean, record_id))
  612. conn.commit()
  613. print(f"[AI Task] SUCCESS: Record {record_id} processed and saved.")
  614. return # Success
  615. except json.JSONDecodeError as err:
  616. raise Exception(f"JSON Parse Error: {str(err)}. Raw: {full_content}")
  617. else:
  618. raise Exception(f"API Error {response.status_code}: {response.text}")
  619. except Exception as e:
  620. print(f"[AI Task] Attempt {attempt+1} failed for record {record_id}: {e}")
  621. last_exception = e
  622. if attempt < max_retries - 1:
  623. wait_time = 2 * (attempt + 1)
  624. print(f"[AI Task] Waiting {wait_time}s before retry...")
  625. time.sleep(wait_time)
  626. raise last_exception or Exception("Unknown error")
  627. except Exception as e:
  628. print(f"[AI Task] FINAL FAILURE for record {record_id}: {e}")
  629. try:
  630. with conn.cursor() as cursor:
  631. cursor.execute("UPDATE genealogy_records SET ai_status = 3, ai_content = %s WHERE id = %s", (f"Max Retries Exceeded. Error: {str(e)}", record_id))
  632. conn.commit()
  633. except:
  634. pass
  635. finally:
  636. conn.close()
  637. print(f"[AI Task] Task finished for record {record_id}")
  638. def ensure_pdf_table():
  639. conn = get_db_connection()
  640. try:
  641. with conn.cursor() as cursor:
  642. cursor.execute("""
  643. CREATE TABLE IF NOT EXISTS genealogy_pdfs (
  644. id INT AUTO_INCREMENT PRIMARY KEY,
  645. file_name VARCHAR(255) NOT NULL,
  646. oss_url TEXT NOT NULL,
  647. description VARCHAR(500) DEFAULT '',
  648. upload_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  649. uploader VARCHAR(100) DEFAULT '',
  650. version_name VARCHAR(255) DEFAULT '',
  651. version_source VARCHAR(255) DEFAULT '',
  652. file_provider VARCHAR(100) DEFAULT '',
  653. parse_status INT DEFAULT 0
  654. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  655. """)
  656. # 检查是否存在parse_status字段,如果不存在则添加
  657. cursor.execute("SHOW COLUMNS FROM genealogy_pdfs LIKE 'parse_status'")
  658. if not cursor.fetchone():
  659. cursor.execute("ALTER TABLE genealogy_pdfs ADD COLUMN parse_status INT DEFAULT 0")
  660. # 检查是否存在version_name字段,如果不存在则添加
  661. cursor.execute("SHOW COLUMNS FROM genealogy_pdfs LIKE 'version_name'")
  662. if not cursor.fetchone():
  663. cursor.execute("ALTER TABLE genealogy_pdfs ADD COLUMN version_name VARCHAR(255) DEFAULT ''")
  664. # 检查是否存在version_source字段,如果不存在则添加
  665. cursor.execute("SHOW COLUMNS FROM genealogy_pdfs LIKE 'version_source'")
  666. if not cursor.fetchone():
  667. cursor.execute("ALTER TABLE genealogy_pdfs ADD COLUMN version_source VARCHAR(255) DEFAULT ''")
  668. # 检查是否存在file_provider字段,如果不存在则添加
  669. cursor.execute("SHOW COLUMNS FROM genealogy_pdfs LIKE 'file_provider'")
  670. if not cursor.fetchone():
  671. cursor.execute("ALTER TABLE genealogy_pdfs ADD COLUMN file_provider VARCHAR(100) DEFAULT ''")
  672. conn.commit()
  673. finally:
  674. conn.close()
  675. @app.route('/manager/pdf_management')
  676. def pdf_management():
  677. if 'user_id' not in session:
  678. return redirect(url_for('login'))
  679. username = session.get('username', 'unknown')
  680. is_super_admin = session.get('is_super_admin', 'NOT_SET')
  681. print(f"[PDF Management Access] User: {username}, is_super_admin: {is_super_admin}")
  682. # Verify is_super_admin against database - always check latest status
  683. conn = get_db_connection()
  684. try:
  685. with conn.cursor() as cursor:
  686. cursor.execute("SELECT is_super_admin FROM users WHERE id = %s", (session['user_id'],))
  687. db_result = cursor.fetchone()
  688. db_is_super = db_result['is_super_admin'] if db_result else 0
  689. print(f"[PDF Management Access] DB is_super_admin: {db_is_super}")
  690. if not db_is_super:
  691. print(f"[PDF Management Access] Denied for {username} (DB check)")
  692. flash('无权限访问此页面')
  693. return redirect(url_for('home'))
  694. finally:
  695. conn.close()
  696. print(f"[PDF Management Access] Allowed for {username}")
  697. ensure_pdf_table()
  698. view_id = request.args.get('view', type=int)
  699. preview = request.args.get('preview', type=bool, default=False)
  700. selected_pdf = None
  701. conn = get_db_connection()
  702. try:
  703. with conn.cursor() as cursor:
  704. cursor.execute("SELECT * FROM genealogy_pdfs ORDER BY upload_time DESC")
  705. pdfs = cursor.fetchall()
  706. if view_id and preview:
  707. cursor.execute("SELECT * FROM genealogy_pdfs WHERE id = %s", (view_id,))
  708. selected_pdf = cursor.fetchone()
  709. finally:
  710. conn.close()
  711. return render_template('pdf_management.html', pdfs=pdfs, selected_pdf=selected_pdf)
  712. @app.route('/manager/parse_pdf/<int:pdf_id>', methods=['POST'])
  713. def parse_pdf(pdf_id):
  714. if 'user_id' not in session:
  715. return jsonify({"success": False, "message": "Unauthorized"}), 401
  716. # 标记PDF为解析中
  717. conn = get_db_connection()
  718. try:
  719. with conn.cursor() as cursor:
  720. cursor.execute("UPDATE genealogy_pdfs SET parse_status = 1 WHERE id = %s", (pdf_id,))
  721. conn.commit()
  722. finally:
  723. conn.close()
  724. # 异步执行PDF解析
  725. def parse_pdf_async():
  726. try:
  727. # 获取PDF信息
  728. conn = get_db_connection()
  729. pdf_info = None
  730. try:
  731. with conn.cursor() as cursor:
  732. cursor.execute("SELECT * FROM genealogy_pdfs WHERE id = %s", (pdf_id,))
  733. pdf_info = cursor.fetchone()
  734. finally:
  735. conn.close()
  736. if not pdf_info:
  737. return
  738. # 下载PDF并拆分
  739. pdf_url = pdf_info['oss_url']
  740. response = requests.get(pdf_url)
  741. response.raise_for_status()
  742. # 保存临时PDF文件
  743. temp_pdf_path = f"/tmp/{pdf_info['file_name']}"
  744. with open(temp_pdf_path, 'wb') as f:
  745. f.write(response.content)
  746. # 使用PyMuPDF拆分PDF
  747. doc = fitz.open(temp_pdf_path)
  748. page_count = doc.page_count
  749. # 每个PDF的页码从1开始计算
  750. max_page = 0
  751. # 逐页处理
  752. for i in range(page_count):
  753. page = doc[i]
  754. pix = page.get_pixmap()
  755. image_path = f"/tmp/{pdf_info['file_name']}_page_{i+1}.png"
  756. pix.save(image_path)
  757. # 上传图片到OSS
  758. image_oss_url = upload_to_oss(image_path, f"{pdf_info['file_name']}_page_{i+1}.png")
  759. # 检查上传是否成功
  760. if not image_oss_url:
  761. raise Exception(f"Failed to upload image to OSS: {image_path}")
  762. # 保存到genealogy_records表
  763. conn = get_db_connection()
  764. try:
  765. with conn.cursor() as cursor:
  766. cursor.execute("""
  767. INSERT INTO genealogy_records
  768. (file_name, oss_url, file_type, page_number, genealogy_version, genealogy_source, upload_person, upload_time)
  769. VALUES (%s, %s, %s, %s, %s, %s, %s, CURRENT_TIMESTAMP)
  770. """, (
  771. f"{pdf_info['file_name']}_page_{i+1}.png",
  772. image_oss_url,
  773. '图片',
  774. max_page + i + 1,
  775. pdf_info['version_name'],
  776. pdf_info['version_source'],
  777. pdf_info['file_provider']
  778. ))
  779. conn.commit()
  780. finally:
  781. conn.close()
  782. # 删除临时图片文件
  783. if os.path.exists(image_path):
  784. os.remove(image_path)
  785. # 删除临时PDF文件
  786. if os.path.exists(temp_pdf_path):
  787. os.remove(temp_pdf_path)
  788. # 更新PDF解析状态为成功
  789. conn = get_db_connection()
  790. try:
  791. with conn.cursor() as cursor:
  792. cursor.execute("UPDATE genealogy_pdfs SET parse_status = 2 WHERE id = %s", (pdf_id,))
  793. conn.commit()
  794. finally:
  795. conn.close()
  796. except Exception as e:
  797. # 更新PDF解析状态为失败
  798. conn = get_db_connection()
  799. try:
  800. with conn.cursor() as cursor:
  801. cursor.execute("UPDATE genealogy_pdfs SET parse_status = 3 WHERE id = %s", (pdf_id,))
  802. conn.commit()
  803. finally:
  804. conn.close()
  805. print(f"PDF解析失败: {e}")
  806. # 启动异步任务
  807. thread = threading.Thread(target=parse_pdf_async)
  808. thread.daemon = True
  809. thread.start()
  810. return jsonify({"success": True, "message": "PDF解析已开始,将在后台执行"})
  811. @app.route('/manager/batch_ai_parse', methods=['GET'])
  812. def batch_ai_parse():
  813. """Batch AI parse for unprocessed records."""
  814. if 'user_id' not in session:
  815. return jsonify({"success": False, "message": "Unauthorized"}), 401
  816. # Start background thread
  817. thread = threading.Thread(target=batch_ai_parse_async)
  818. thread.daemon = True
  819. thread.start()
  820. return jsonify({"success": True, "message": "批量AI解析已开始,请稍候查看结果"})
  821. def batch_ai_parse_async():
  822. """Background task to batch AI parse unprocessed records."""
  823. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  824. print(f"[{timestamp}] [Batch AI Parse] Starting batch AI parse task...")
  825. # Get unprocessed records (ai_status = 0)
  826. conn = None
  827. unprocessed_records = []
  828. try:
  829. conn = get_db_connection()
  830. with conn.cursor() as cursor:
  831. cursor.execute("SELECT id, oss_url FROM genealogy_records WHERE ai_status = 0 order by page_number")
  832. unprocessed_records = cursor.fetchall()
  833. conn.close()
  834. conn = None
  835. total_records = len(unprocessed_records)
  836. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  837. print(f"[{timestamp}] [Batch AI Parse] Found {total_records} unprocessed records")
  838. if total_records == 0:
  839. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  840. print(f"[{timestamp}] [Batch AI Parse] No unprocessed records found")
  841. return
  842. # Control concurrency to 5
  843. max_concurrency = 5
  844. semaphore = threading.Semaphore(max_concurrency)
  845. threads = []
  846. def process_record(record):
  847. """Process a single record with semaphore."""
  848. with semaphore:
  849. try:
  850. record_id = record['id']
  851. image_url = record['oss_url']
  852. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  853. print(f"[{timestamp}] [Batch AI Parse] Processing record {record_id}")
  854. process_ai_task(record_id, image_url)
  855. except Exception as e:
  856. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  857. print(f"[{timestamp}] [Batch AI Parse] Error processing record {record['id']}: {e}")
  858. # If failed, we'll handle it in the next batch
  859. # Start threads for each record
  860. for record in unprocessed_records:
  861. thread = threading.Thread(target=process_record, args=(record,))
  862. thread.daemon = True
  863. thread.start()
  864. threads.append(thread)
  865. # Wait for all threads to complete
  866. for thread in threads:
  867. thread.join()
  868. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  869. print(f"[{timestamp}] [Batch AI Parse] Batch processing completed. Processed {total_records} records")
  870. # Check for failed records and restart them
  871. check_failed_records()
  872. except Exception as e:
  873. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  874. print(f"[{timestamp}] [Batch AI Parse] Error: {e}")
  875. finally:
  876. if conn:
  877. try:
  878. conn.close()
  879. except:
  880. pass
  881. def check_failed_records():
  882. """Check for failed records and restart them."""
  883. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  884. print(f"[{timestamp}] [Batch AI Parse] Checking for failed records...")
  885. conn = None
  886. failed_records = []
  887. try:
  888. conn = get_db_connection()
  889. with conn.cursor() as cursor:
  890. cursor.execute("SELECT id, oss_url FROM genealogy_records WHERE ai_status = 3")
  891. failed_records = cursor.fetchall()
  892. conn.close()
  893. conn = None
  894. total_failed = len(failed_records)
  895. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  896. print(f"[{timestamp}] [Batch AI Parse] Found {total_failed} failed records")
  897. if total_failed == 0:
  898. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  899. print(f"[{timestamp}] [Batch AI Parse] No failed records found")
  900. return
  901. # Control concurrency to 5 for failed records
  902. max_concurrency = 5
  903. semaphore = threading.Semaphore(max_concurrency)
  904. threads = []
  905. def process_failed_record(record):
  906. """Process a failed record with semaphore."""
  907. with semaphore:
  908. retry_conn = None
  909. try:
  910. record_id = record['id']
  911. image_url = record['oss_url']
  912. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  913. print(f"[{timestamp}] [Batch AI Parse] Retrying failed record {record_id}")
  914. # Reset status to processing
  915. retry_conn = get_db_connection()
  916. with retry_conn.cursor() as cursor:
  917. cursor.execute("UPDATE genealogy_records SET ai_status = 1 WHERE id = %s", (record_id,))
  918. retry_conn.commit()
  919. retry_conn.close()
  920. retry_conn = None
  921. process_ai_task(record_id, image_url)
  922. except Exception as e:
  923. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  924. print(f"[{timestamp}] [Batch AI Parse] Error retrying record {record['id']}: {e}")
  925. finally:
  926. if retry_conn:
  927. try:
  928. retry_conn.close()
  929. except:
  930. pass
  931. # Start threads for each failed record
  932. for record in failed_records:
  933. thread = threading.Thread(target=process_failed_record, args=(record,))
  934. thread.daemon = True
  935. thread.start()
  936. threads.append(thread)
  937. # Wait for all threads to complete
  938. for thread in threads:
  939. thread.join()
  940. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  941. print(f"[{timestamp}] [Batch AI Parse] Retry processing completed. Retried {total_failed} failed records")
  942. except Exception as e:
  943. timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  944. print(f"[{timestamp}] [Batch AI Parse] Error checking failed records: {e}")
  945. finally:
  946. if conn:
  947. try:
  948. conn.close()
  949. except:
  950. pass
  951. @app.route('/manager/delete_pdf/<int:pdf_id>', methods=['POST'])
  952. def delete_pdf(pdf_id):
  953. if 'user_id' not in session:
  954. return jsonify({"success": False, "message": "Unauthorized"}), 401
  955. conn = get_db_connection()
  956. try:
  957. with conn.cursor() as cursor:
  958. cursor.execute("DELETE FROM genealogy_pdfs WHERE id = %s", (pdf_id,))
  959. conn.commit()
  960. flash('PDF文件记录已删除')
  961. except Exception as e:
  962. flash(f'删除失败: {e}')
  963. finally:
  964. conn.close()
  965. return redirect(url_for('pdf_management'))
  966. @app.route('/manager/')
  967. def index():
  968. if 'user_id' not in session:
  969. return redirect(url_for('login'))
  970. # 获取当前登录用户名
  971. username = session.get('username', 'genealogy')
  972. page = request.args.get('page', 1, type=int)
  973. version = request.args.get('version', '').strip()
  974. print(f"Received version parameter: '{version}'")
  975. source = request.args.get('source', '').strip()
  976. person = request.args.get('person', '').strip()
  977. file_type = request.args.get('file_type', '').strip()
  978. per_page = 10
  979. offset = (page - 1) * per_page
  980. conn = get_db_connection()
  981. try:
  982. with conn.cursor() as cursor:
  983. query_conditions = []
  984. params = []
  985. if version:
  986. query_conditions.append("genealogy_version LIKE %s")
  987. params.append(f"%{version}%")
  988. if source:
  989. query_conditions.append("genealogy_source LIKE %s")
  990. params.append(f"%{source}%")
  991. if person:
  992. query_conditions.append("upload_person LIKE %s")
  993. params.append(f"%{person}%")
  994. if file_type:
  995. query_conditions.append("file_type = %s")
  996. params.append(file_type)
  997. where_clause = ""
  998. if query_conditions:
  999. where_clause = "WHERE " + " AND ".join(query_conditions)
  1000. count_sql = f"SELECT COUNT(*) as count FROM genealogy_records {where_clause}"
  1001. cursor.execute(count_sql, params)
  1002. total = cursor.fetchone()['count']
  1003. sql = f"SELECT * FROM genealogy_records {where_clause} ORDER BY page_number ASC LIMIT %s OFFSET %s"
  1004. cursor.execute(sql, params + [per_page, offset])
  1005. records = cursor.fetchall()
  1006. # 为图片URL添加水印
  1007. for record in records:
  1008. if record.get('oss_url'):
  1009. record['oss_url'] = add_oss_watermark(record['oss_url'], username)
  1010. total_pages = (total + per_page - 1) // per_page
  1011. finally:
  1012. conn.close()
  1013. return render_template('index.html', records=records, page=page, total_pages=total_pages, version=version, source=source, person=person, file_type=file_type, total=total)
  1014. @app.route('/manager/members')
  1015. def members():
  1016. if 'user_id' not in session:
  1017. return redirect(url_for('login'))
  1018. search_name = request.args.get('name', '').strip()
  1019. page = request.args.get('page', 1, type=int)
  1020. per_page = 10
  1021. offset = (page - 1) * per_page
  1022. print(f"[Members List] Fetching members page: {page}, search: '{search_name}', per_page: {per_page}")
  1023. conn = get_db_connection()
  1024. try:
  1025. with conn.cursor() as cursor:
  1026. # 1. Get total count
  1027. if search_name:
  1028. variants = expand_name_search_variants(search_name)
  1029. where_parts = []
  1030. params = []
  1031. for v in variants:
  1032. where_parts.append("(name LIKE %s OR simplified_name LIKE %s)")
  1033. like = f"%{v}%"
  1034. params.extend([like, like])
  1035. where_clause = " OR ".join(where_parts) if where_parts else "name LIKE %s"
  1036. if not where_parts:
  1037. params = [f"%{search_name}%"]
  1038. count_sql = f"SELECT COUNT(*) as count FROM family_member_info WHERE {where_clause}"
  1039. print(f"[Members List] Executing count SQL: {count_sql}")
  1040. print(f"[Members List] Count SQL parameters: {params}")
  1041. cursor.execute(count_sql, tuple(params))
  1042. else:
  1043. count_sql = "SELECT COUNT(*) as count FROM family_member_info"
  1044. print(f"[Members List] Executing count SQL: {count_sql}")
  1045. cursor.execute(count_sql)
  1046. result = cursor.fetchone()
  1047. total = result['count'] if result else 0
  1048. total_pages = (total + per_page - 1) // per_page
  1049. print(f"[Members List] Total members: {total}, total pages: {total_pages}")
  1050. # 2. Get paginated results, ordered by modified_time DESC (or create_time if modified is null/same)
  1051. # Using COALESCE to ensure sort works even if modified_time is NULL
  1052. order_clause = "ORDER BY COALESCE(fmi.modified_time, fmi.create_time) DESC"
  1053. # 父亲信息 JOIN(取亲生/普通父亲,排除入继关系)
  1054. father_join = """
  1055. LEFT JOIN family_relation_info fri
  1056. ON fmi.id = fri.child_mid AND fri.relation_type = 1 AND COALESCE(fri.sub_relation_type, 0) != 3
  1057. LEFT JOIN family_member_info father ON fri.parent_mid = father.id
  1058. """
  1059. father_cols = ", father.id as father_id, father.name as father_name, father.simplified_name as father_simplified_name, fri.child_order as child_order_to_father"
  1060. if search_name:
  1061. variants = expand_name_search_variants(search_name)
  1062. where_parts = []
  1063. params = []
  1064. for v in variants:
  1065. where_parts.append("(fmi.name LIKE %s OR fmi.simplified_name LIKE %s)")
  1066. like = f"%{v}%"
  1067. params.extend([like, like])
  1068. where_clause = " OR ".join(where_parts) if where_parts else "(fmi.name LIKE %s OR fmi.simplified_name LIKE %s)"
  1069. if not where_parts:
  1070. like = f"%{search_name}%"
  1071. params = [like, like]
  1072. sql = f"SELECT fmi.id, fmi.name, fmi.simplified_name, fmi.sex, fmi.name_word_generation, fmi.birthday, fmi.occupation, fmi.family_rank, fmi.branch_family_hall, fmi.residential_address, fmi.is_pass_away, fmi.create_time, fmi.modified_time{father_cols} FROM family_member_info fmi {father_join} WHERE {where_clause} {order_clause} LIMIT %s OFFSET %s"
  1073. print(f"[Members List] Executing members SQL: {sql}")
  1074. print(f"[Members List] Members SQL parameters: {params + [per_page, offset]}")
  1075. cursor.execute(sql, tuple(params + [per_page, offset]))
  1076. else:
  1077. sql = f"SELECT fmi.id, fmi.name, fmi.simplified_name, fmi.sex, fmi.name_word_generation, fmi.birthday, fmi.occupation, fmi.family_rank, fmi.branch_family_hall, fmi.residential_address, fmi.is_pass_away, fmi.create_time, fmi.modified_time{father_cols} FROM family_member_info fmi {father_join} {order_clause} LIMIT %s OFFSET %s"
  1078. print(f"[Members List] Executing members SQL: {sql}")
  1079. print(f"[Members List] Members SQL parameters: {[per_page, offset]}")
  1080. cursor.execute(sql, (per_page, offset))
  1081. members = cursor.fetchall()
  1082. print(f"[Members List] Fetched {len(members)} members")
  1083. # 格式化日期
  1084. for m in members:
  1085. m['birthday_str'] = format_timestamp(m.get('birthday'))
  1086. # 格式化创建时间 (针对 TIMESTAMP 字段)
  1087. if m.get('create_time'):
  1088. m['create_time_str'] = m['create_time'].strftime('%Y-%m-%d')
  1089. if m.get('modified_time'):
  1090. m['modified_time_str'] = m['modified_time'].strftime('%Y-%m-%d %H:%M')
  1091. finally:
  1092. print(f"[Members List] Closing database connection")
  1093. conn.close()
  1094. return render_template('members.html', members=members, search_name=search_name, page=page, total_pages=total_pages, total=total)
  1095. @app.route('/manager/batch_genealogy')
  1096. def batch_genealogy():
  1097. if 'user_id' not in session:
  1098. return redirect(url_for('login'))
  1099. return render_template('batch_genealogy.html')
  1100. @app.route('/manager/suspected_errors')
  1101. def suspected_errors():
  1102. if 'user_id' not in session:
  1103. return redirect(url_for('login'))
  1104. search_name = request.args.get('name', '').strip()
  1105. page = request.args.get('page', 1, type=int)
  1106. per_page = 20
  1107. offset = (page - 1) * per_page
  1108. conn = get_db_connection()
  1109. try:
  1110. with conn.cursor() as cursor:
  1111. # Base query with condition for non-empty suspected_error (using TRIM to remove whitespace)
  1112. base_query = "SELECT id, name, simplified_name, sex, name_word_generation, birthday, suspected_error FROM family_member_info WHERE suspected_error IS NOT NULL AND TRIM(suspected_error) != ''"
  1113. count_query = "SELECT COUNT(*) as count FROM family_member_info WHERE suspected_error IS NOT NULL AND TRIM(suspected_error) != ''"
  1114. # Add search condition if provided
  1115. params = []
  1116. if search_name:
  1117. # Support both traditional and simplified name search
  1118. base_query += " AND (name LIKE %s OR simplified_name LIKE %s)"
  1119. count_query += " AND (name LIKE %s OR simplified_name LIKE %s)"
  1120. search_param = f"%{search_name}%"
  1121. params.extend([search_param, search_param])
  1122. # Get total count
  1123. cursor.execute(count_query, params)
  1124. result = cursor.fetchone()
  1125. total = result['count'] if result else 0
  1126. total_pages = (total + per_page - 1) // per_page
  1127. # Get members with pagination
  1128. base_query += " ORDER BY name LIMIT %s OFFSET %s"
  1129. params.extend([per_page, offset])
  1130. cursor.execute(base_query, params)
  1131. members = cursor.fetchall()
  1132. # Format birthday for display
  1133. for member in members:
  1134. if member['birthday']:
  1135. member['birthday_str'] = format_timestamp(member['birthday'])
  1136. else:
  1137. member['birthday_str'] = '未知'
  1138. finally:
  1139. conn.close()
  1140. return render_template('suspected_errors.html', members=members, search_name=search_name, page=page, total_pages=total_pages, total=total)
  1141. @app.route('/manager/tree')
  1142. def tree():
  1143. if 'user_id' not in session:
  1144. return redirect(url_for('login'))
  1145. return render_template('tree.html')
  1146. @app.route('/manager/lineage_query')
  1147. def lineage_query():
  1148. if 'user_id' not in session:
  1149. return redirect(url_for('login'))
  1150. return render_template('lineage_query.html')
  1151. @app.route('/manager/tree_classic')
  1152. def tree_classic():
  1153. if 'user_id' not in session:
  1154. return redirect(url_for('login'))
  1155. return render_template('tree_classic.html')
  1156. @app.route('/manager/tree_gen')
  1157. def tree_gen():
  1158. if 'user_id' not in session:
  1159. return redirect(url_for('login'))
  1160. return render_template('tree_gen.html')
  1161. @app.route('/manager/api/tree_data')
  1162. def tree_data():
  1163. if 'user_id' not in session:
  1164. return jsonify({"error": "Unauthorized"}), 401
  1165. conn = get_db_connection()
  1166. try:
  1167. with conn.cursor() as cursor:
  1168. # 获取所有成员
  1169. cursor.execute("SELECT id, name, simplified_name, sex, family_rank, name_word_generation FROM family_member_info")
  1170. members = cursor.fetchall()
  1171. # 获取所有关系 (1:父子 2:母子 10:夫妻 11:兄弟 12:姐妹),包括子类型
  1172. cursor.execute("SELECT parent_mid, child_mid, relation_type, sub_relation_type FROM family_relation_info")
  1173. relations = cursor.fetchall()
  1174. return jsonify({"members": members, "relations": relations})
  1175. finally:
  1176. conn.close()
  1177. @app.route('/manager/api/search_member', methods=['POST'])
  1178. def search_member():
  1179. if 'user_id' not in session:
  1180. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1181. data = request.get_json()
  1182. keyword = data.get('keyword', '').strip()
  1183. if not keyword:
  1184. return jsonify({"success": False, "message": "请输入搜索关键词"})
  1185. conn = get_db_connection()
  1186. try:
  1187. with conn.cursor() as cursor:
  1188. cursor.execute("""
  1189. SELECT id, name, simplified_name
  1190. FROM family_member_info
  1191. WHERE name LIKE %s OR simplified_name LIKE %s OR former_name LIKE %s
  1192. ORDER BY
  1193. CASE WHEN name = %s THEN 1
  1194. WHEN simplified_name = %s THEN 2
  1195. WHEN name LIKE %s THEN 3
  1196. WHEN simplified_name LIKE %s THEN 4
  1197. ELSE 5 END
  1198. """, (f'%{keyword}%', f'%{keyword}%', f'%{keyword}%', keyword, keyword, f'{keyword}%', f'{keyword}%'))
  1199. members = cursor.fetchall()
  1200. if members:
  1201. return jsonify({"success": True, "members": members})
  1202. else:
  1203. return jsonify({"success": False, "message": "未找到匹配的成员"})
  1204. finally:
  1205. conn.close()
  1206. @app.route('/manager/api/get_lineage/<int:member_id>')
  1207. def get_lineage(member_id):
  1208. if 'user_id' not in session:
  1209. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1210. import time
  1211. start_time = time.time()
  1212. print(f"[Lineage Query] Starting query for member_id: {member_id} at {time.strftime('%Y-%m-%d %H:%M:%S')}")
  1213. conn = get_db_connection()
  1214. try:
  1215. with conn.cursor() as cursor:
  1216. # Step 1: Get center person
  1217. step_start = time.time()
  1218. cursor.execute("SELECT id, name, simplified_name, name_word, name_word_generation FROM family_member_info WHERE id = %s", (member_id,))
  1219. center = cursor.fetchone()
  1220. print(f"[Lineage Query] Step 1 - Get center: {time.time() - step_start:.3f}s")
  1221. if not center:
  1222. return jsonify({"success": False, "message": "成员不存在"})
  1223. # Step 2: Get ancestors with their siblings (generations)
  1224. step_start = time.time()
  1225. generations = [] # Array of generations, each with main ancestor and siblings
  1226. current_id = member_id
  1227. max_depth = 100 # 支持最多 100 代祖先(实际家谱一般不超过 80 代)
  1228. ancestor_ids = [] # Track ancestor IDs for exclusion when expanding
  1229. displayed_ids = set() # Track IDs that are already displayed
  1230. displayed_ids.add(member_id) # Center person is displayed
  1231. visited_ancestor_ids = set([member_id]) # 循环检测:避免脏数据死循环
  1232. for depth in range(max_depth):
  1233. # 获取所有父母关系(支持出继/入继)
  1234. cursor.execute("""
  1235. SELECT p.id, p.name, p.simplified_name, p.name_word, p.name_word_generation,
  1236. EXISTS(SELECT 1 FROM family_relation_info WHERE parent_mid = p.id AND relation_type IN (1, 2)) as has_children,
  1237. r.sub_relation_type
  1238. FROM family_relation_info r
  1239. JOIN family_member_info p ON r.parent_mid = p.id
  1240. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  1241. """, (current_id,))
  1242. parents = cursor.fetchall()
  1243. if not parents:
  1244. break
  1245. # 优先选择直系父母(非出继),如果都是出继/入继,选择入继
  1246. parent = None
  1247. adoptive_parent = None
  1248. for p in parents:
  1249. if p['sub_relation_type'] == 2: # 出继(亲生父母)
  1250. parent = p
  1251. elif p['sub_relation_type'] == 3: # 入继(养父母)
  1252. adoptive_parent = p
  1253. else: # 普通关系(亲生)
  1254. parent = p
  1255. # 如果没有找到普通父母,使用入继父母
  1256. if not parent:
  1257. parent = adoptive_parent
  1258. # 循环检测:如果该祖先已在链中出现过,终止(数据异常保护)
  1259. if parent['id'] in visited_ancestor_ids:
  1260. break
  1261. visited_ancestor_ids.add(parent['id'])
  1262. ancestor_ids.append(parent['id'])
  1263. displayed_ids.add(parent['id'])
  1264. # Get siblings of this ancestor (father's brothers)
  1265. # First get grandparent (parent's father)
  1266. cursor.execute("""
  1267. SELECT gp.id
  1268. FROM family_relation_info r
  1269. JOIN family_member_info gp ON r.parent_mid = gp.id
  1270. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  1271. LIMIT 1
  1272. """, (parent['id'],))
  1273. grandparent = cursor.fetchone()
  1274. parent_siblings = []
  1275. if grandparent:
  1276. # 获取祖先自身的 child_order(在祖父下的排行)
  1277. cursor.execute("""
  1278. SELECT COALESCE(child_order, NULL) AS child_order
  1279. FROM family_relation_info
  1280. WHERE parent_mid = %s AND child_mid = %s AND relation_type IN (1, 2)
  1281. LIMIT 1
  1282. """, (grandparent['id'], parent['id']))
  1283. co_row = cursor.fetchone()
  1284. parent['child_order'] = co_row['child_order'] if co_row else None
  1285. # 获取祖先的兄弟(含 child_order,用于前端排序与徽章)
  1286. cursor.execute("""
  1287. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  1288. EXISTS(SELECT 1 FROM family_relation_info WHERE parent_mid = c.id AND relation_type IN (1, 2)) as has_children,
  1289. COALESCE(r.child_order, NULL) AS child_order
  1290. FROM family_relation_info r
  1291. JOIN family_member_info c ON r.child_mid = c.id
  1292. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2) AND c.id != %s
  1293. ORDER BY COALESCE(r.child_order, 99999), c.id
  1294. LIMIT 30
  1295. """, (grandparent['id'], parent['id']))
  1296. parent_siblings = cursor.fetchall()
  1297. # Mark sibling IDs as displayed
  1298. for sibling in parent_siblings:
  1299. displayed_ids.add(sibling['id'])
  1300. # Check if parent has any children NOT already displayed
  1301. # Only show expand button if there are undisplayed children
  1302. cursor.execute("""
  1303. SELECT COUNT(*) as count
  1304. FROM family_relation_info r
  1305. JOIN family_member_info c ON r.child_mid = c.id
  1306. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2)
  1307. """, (parent['id'],))
  1308. total_children = cursor.fetchone()['count']
  1309. # Check if current child is displayed (current_id is the child of parent)
  1310. child_displayed = current_id in displayed_ids
  1311. # Show expand if there are children not displayed
  1312. show_expand = total_children > (1 if child_displayed else 0)
  1313. parent['show_expand'] = show_expand
  1314. generations.append({
  1315. 'ancestor': parent,
  1316. 'siblings': parent_siblings,
  1317. 'depth': depth
  1318. })
  1319. current_id = parent['id']
  1320. print(f"[Lineage Query] Step 2 - Get generations ({len(generations)}): {time.time() - step_start:.3f}s")
  1321. # Step 3: Get immediate children only (limited count)
  1322. step_start = time.time()
  1323. # 获取子女:
  1324. # - 包含入继子女(sub_relation_type=3,养父母侧)
  1325. # - 包含普通子女(sub_relation_type 为空或非2/3)
  1326. # - 排除出继子女(sub_relation_type=2,生父母侧)若该子女已有养父母记录
  1327. cursor.execute("""
  1328. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  1329. EXISTS(SELECT 1 FROM family_relation_info WHERE parent_mid = c.id AND relation_type IN (1, 2)) as has_children,
  1330. r.sub_relation_type,
  1331. r.child_order
  1332. FROM family_relation_info r
  1333. JOIN family_member_info c ON r.child_mid = c.id
  1334. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2)
  1335. AND (
  1336. COALESCE(r.sub_relation_type, 0) != 2
  1337. OR NOT EXISTS (
  1338. SELECT 1 FROM family_relation_info r2
  1339. WHERE r2.child_mid = c.id AND r2.sub_relation_type = 3
  1340. )
  1341. )
  1342. ORDER BY COALESCE(r.child_order, 99999), c.id
  1343. LIMIT 30
  1344. """, (member_id,))
  1345. children = cursor.fetchall()
  1346. # 对于入继的子女,获取其生父母信息并生成"由xxx公第N子入继"说明
  1347. _order_labels_lg = {1:'长', 2:'次', 3:'三', 4:'四', 5:'五',
  1348. 6:'六', 7:'七', 8:'八', 9:'九', 10:'十'}
  1349. for child in children:
  1350. if child['sub_relation_type'] == 3: # 入继
  1351. cursor.execute("""
  1352. SELECT p.id, p.name, p.simplified_name, r.child_order
  1353. FROM family_relation_info r
  1354. JOIN family_member_info p ON r.parent_mid = p.id
  1355. WHERE r.child_mid = %s AND r.sub_relation_type = 2
  1356. LIMIT 1
  1357. """, (child['id'],))
  1358. bio_parent = cursor.fetchone()
  1359. if bio_parent:
  1360. bio_name = bio_parent['simplified_name'] or bio_parent['name']
  1361. order = bio_parent['child_order']
  1362. order_str = _order_labels_lg.get(order, f'第{order}') if order else '某'
  1363. child['adopt_info'] = f"由{bio_name}公{order_str}子入继"
  1364. # Initialize children array
  1365. for child in children:
  1366. child['children'] = []
  1367. print(f"[Lineage Query] Step 3 - Get children ({len(children)}): {time.time() - step_start:.3f}s")
  1368. # Step 4: Get siblings of center person + center's own child_order
  1369. step_start = time.time()
  1370. siblings = []
  1371. center_child_order = None
  1372. if generations:
  1373. parent_id = generations[0]['ancestor']['id'] # Father
  1374. # 中心人物自身的排行
  1375. cursor.execute("""
  1376. SELECT COALESCE(child_order, NULL) AS child_order
  1377. FROM family_relation_info
  1378. WHERE parent_mid = %s AND child_mid = %s AND relation_type IN (1, 2)
  1379. LIMIT 1
  1380. """, (parent_id, member_id))
  1381. co_row = cursor.fetchone()
  1382. center_child_order = co_row['child_order'] if co_row else None
  1383. cursor.execute("""
  1384. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  1385. EXISTS(SELECT 1 FROM family_relation_info WHERE parent_mid = c.id AND relation_type IN (1, 2)) as has_children,
  1386. r.sub_relation_type,
  1387. COALESCE(r.child_order, NULL) AS child_order
  1388. FROM family_relation_info r
  1389. JOIN family_member_info c ON r.child_mid = c.id
  1390. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2) AND c.id != %s
  1391. ORDER BY COALESCE(r.child_order, 99999), c.id
  1392. LIMIT 30
  1393. """, (parent_id, member_id))
  1394. siblings = cursor.fetchall()
  1395. print(f"[Lineage Query] Step 4 - Get siblings ({len(siblings)}): {time.time() - step_start:.3f}s")
  1396. total_time = time.time() - start_time
  1397. print(f"[Lineage Query] Total time: {total_time:.3f}s")
  1398. # 判断是否还有更高的祖先(顶端祖先是否仍有父亲)
  1399. has_more_ancestors = False
  1400. topmost_ancestor_id = None
  1401. if generations:
  1402. topmost_ancestor_id = generations[-1]['ancestor']['id']
  1403. cursor.execute("""
  1404. SELECT COUNT(*) as cnt FROM family_relation_info
  1405. WHERE child_mid = %s AND relation_type IN (1,2)
  1406. """, (topmost_ancestor_id,))
  1407. has_more_ancestors = cursor.fetchone()['cnt'] > 0
  1408. return jsonify({
  1409. "success": True,
  1410. "data": {
  1411. "center": {**center, "child_order": center_child_order},
  1412. "generations": generations,
  1413. "ancestor_ids": ancestor_ids,
  1414. "siblings": siblings,
  1415. "children": children,
  1416. "has_more_ancestors": has_more_ancestors,
  1417. "topmost_ancestor_id": topmost_ancestor_id
  1418. }
  1419. })
  1420. except Exception as e:
  1421. print(f"[Lineage Query] Error: {e}")
  1422. return jsonify({"success": False, "message": str(e)})
  1423. finally:
  1424. conn.close()
  1425. @app.route('/manager/api/get_ancestors_above/<int:ancestor_id>')
  1426. def get_ancestors_above(ancestor_id):
  1427. """从指定祖先节点继续向上追溯,用于世系查询"继续向上"按钮"""
  1428. if 'user_id' not in session:
  1429. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1430. conn = get_db_connection()
  1431. try:
  1432. with conn.cursor() as cursor:
  1433. generations = []
  1434. current_id = ancestor_id
  1435. max_depth = 100
  1436. visited_ids = set([ancestor_id])
  1437. for depth in range(max_depth):
  1438. cursor.execute("""
  1439. SELECT p.id, p.name, p.simplified_name, p.name_word, p.name_word_generation,
  1440. EXISTS(SELECT 1 FROM family_relation_info
  1441. WHERE parent_mid = p.id AND relation_type IN (1,2)) as has_children,
  1442. r.sub_relation_type
  1443. FROM family_relation_info r
  1444. JOIN family_member_info p ON r.parent_mid = p.id
  1445. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  1446. """, (current_id,))
  1447. parents = cursor.fetchall()
  1448. if not parents:
  1449. break
  1450. parent = None
  1451. adoptive_parent = None
  1452. for p in parents:
  1453. if p['sub_relation_type'] == 3:
  1454. adoptive_parent = p
  1455. else:
  1456. parent = p
  1457. if not parent:
  1458. parent = adoptive_parent
  1459. if parent['id'] in visited_ids:
  1460. break
  1461. visited_ids.add(parent['id'])
  1462. # 查祖父,用于获取该祖先的兄弟
  1463. cursor.execute("""
  1464. SELECT gp.id FROM family_relation_info r
  1465. JOIN family_member_info gp ON r.parent_mid = gp.id
  1466. WHERE r.child_mid = %s AND r.relation_type IN (1, 2) LIMIT 1
  1467. """, (parent['id'],))
  1468. grandparent = cursor.fetchone()
  1469. parent_siblings = []
  1470. if grandparent:
  1471. cursor.execute("""
  1472. SELECT COALESCE(child_order, 1) AS child_order
  1473. FROM family_relation_info
  1474. WHERE parent_mid = %s AND child_mid = %s AND relation_type IN (1,2) LIMIT 1
  1475. """, (grandparent['id'], parent['id']))
  1476. co_row = cursor.fetchone()
  1477. parent['child_order'] = co_row['child_order'] if co_row else 1
  1478. cursor.execute("""
  1479. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  1480. EXISTS(SELECT 1 FROM family_relation_info
  1481. WHERE parent_mid = c.id AND relation_type IN (1,2)) as has_children,
  1482. COALESCE(r.child_order, 1) AS child_order
  1483. FROM family_relation_info r
  1484. JOIN family_member_info c ON r.child_mid = c.id
  1485. WHERE r.parent_mid = %s AND r.relation_type IN (1,2) AND c.id != %s
  1486. ORDER BY COALESCE(r.child_order, 1), c.id
  1487. LIMIT 10
  1488. """, (grandparent['id'], parent['id']))
  1489. parent_siblings = cursor.fetchall()
  1490. for s in parent_siblings:
  1491. s['has_children'] = bool(s['has_children'])
  1492. else:
  1493. parent['child_order'] = None
  1494. parent['has_children'] = bool(parent['has_children'])
  1495. generations.append({
  1496. 'ancestor': parent,
  1497. 'siblings': list(parent_siblings),
  1498. 'depth': depth
  1499. })
  1500. current_id = parent['id']
  1501. # 是否还有更高的祖先
  1502. has_more_ancestors = False
  1503. topmost_ancestor_id = None
  1504. if generations:
  1505. topmost_ancestor_id = generations[-1]['ancestor']['id']
  1506. cursor.execute("""
  1507. SELECT COUNT(*) as cnt FROM family_relation_info
  1508. WHERE child_mid = %s AND relation_type IN (1,2)
  1509. """, (topmost_ancestor_id,))
  1510. has_more_ancestors = cursor.fetchone()['cnt'] > 0
  1511. return jsonify({
  1512. "success": True,
  1513. "data": {
  1514. "generations": generations,
  1515. "has_more_ancestors": has_more_ancestors,
  1516. "topmost_ancestor_id": topmost_ancestor_id
  1517. }
  1518. })
  1519. except Exception as e:
  1520. return jsonify({"success": False, "message": str(e)})
  1521. finally:
  1522. conn.close()
  1523. @app.route('/manager/api/get_descendants/<int:parent_id>')
  1524. def get_descendants(parent_id):
  1525. if 'user_id' not in session:
  1526. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1527. # Get excluded IDs from query parameter
  1528. excluded_ids = request.args.get('exclude', '')
  1529. excluded_list = []
  1530. if excluded_ids:
  1531. excluded_list = [int(id.strip()) for id in excluded_ids.split(',') if id.strip().isdigit()]
  1532. print(f"[get_descendants] Parent ID: {parent_id}, Excluded IDs: {excluded_list}")
  1533. conn = get_db_connection()
  1534. try:
  1535. with conn.cursor() as cursor:
  1536. if excluded_list:
  1537. # Build query with exclusion
  1538. placeholders = ', '.join(['%s'] * len(excluded_list))
  1539. cursor.execute(f"""
  1540. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  1541. EXISTS(SELECT 1 FROM family_relation_info WHERE parent_mid = c.id AND relation_type IN (1, 2)) as has_children
  1542. FROM family_relation_info r
  1543. JOIN family_member_info c ON r.child_mid = c.id
  1544. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2) AND c.id NOT IN ({placeholders})
  1545. ORDER BY COALESCE(r.child_order, 99999), c.id
  1546. LIMIT 20
  1547. """, (parent_id,) + tuple(excluded_list))
  1548. else:
  1549. cursor.execute("""
  1550. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  1551. EXISTS(SELECT 1 FROM family_relation_info WHERE parent_mid = c.id AND relation_type IN (1, 2)) as has_children
  1552. FROM family_relation_info r
  1553. JOIN family_member_info c ON r.child_mid = c.id
  1554. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2)
  1555. ORDER BY COALESCE(r.child_order, 99999), c.id
  1556. LIMIT 20
  1557. """, (parent_id,))
  1558. children = cursor.fetchall()
  1559. for child in children:
  1560. child['children'] = []
  1561. return jsonify({"success": True, "children": children})
  1562. finally:
  1563. conn.close()
  1564. @app.route('/manager/api/save_relation', methods=['POST'])
  1565. def save_relation():
  1566. if 'user_id' not in session:
  1567. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1568. data = request.json
  1569. source_mid = data.get('source_mid') # The member being dragged
  1570. target_mid = data.get('target_mid') # The member being dropped onto
  1571. rel_type = int(data.get('relation_type'))
  1572. sub_rel_type = int(data.get('sub_relation_type', 0))
  1573. if not source_mid or not target_mid or not rel_type:
  1574. return jsonify({"success": False, "message": "参数不完整"}), 400
  1575. conn = get_db_connection()
  1576. try:
  1577. with conn.cursor() as cursor:
  1578. # 简单处理:如果是父子/母子关系
  1579. # target_mid 是父辈,source_mid 是子辈
  1580. parent_mid = target_mid
  1581. child_mid = source_mid
  1582. gen_diff = 1
  1583. if rel_type == 10: # 夫妻
  1584. # 夫妻关系中,我们通常把关联人设为 parent_mid
  1585. parent_mid = target_mid
  1586. child_mid = source_mid
  1587. gen_diff = 0
  1588. elif rel_type in [11, 12]: # 兄弟姐妹
  1589. # 这里逻辑上比较复杂,通常兄弟姐妹有共同父母。
  1590. # 简化处理:暂时存为同级关系 (gen_diff=0)
  1591. parent_mid = target_mid
  1592. child_mid = source_mid
  1593. gen_diff = 0
  1594. # 删除旧关系
  1595. cursor.execute("DELETE FROM family_relation_info WHERE source_mid = %s", (source_mid,))
  1596. # 插入新关系
  1597. sql = """
  1598. INSERT INTO family_relation_info
  1599. (parent_mid, child_mid, relation_type, sub_relation_type, source_mid, generation_diff)
  1600. VALUES (%s, %s, %s, %s, %s, %s)
  1601. """
  1602. cursor.execute(sql, (parent_mid, child_mid, rel_type, sub_rel_type, source_mid, gen_diff))
  1603. conn.commit()
  1604. return jsonify({"success": True, "message": "关系已保存"})
  1605. except Exception as e:
  1606. return jsonify({"success": False, "message": str(e)}), 500
  1607. finally:
  1608. conn.close()
  1609. @app.route('/manager/api/members')
  1610. def get_members():
  1611. if 'user_id' not in session:
  1612. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1613. page = int(request.args.get('page', 1))
  1614. search = request.args.get('search', '')
  1615. per_page = 10
  1616. offset = (page - 1) * per_page
  1617. conn = get_db_connection()
  1618. try:
  1619. with conn.cursor() as cursor:
  1620. # Count total members
  1621. if search:
  1622. cursor.execute("SELECT COUNT(*) as total FROM family_member_info WHERE name LIKE %s OR simplified_name LIKE %s",
  1623. (f'%{search}%', f'%{search}%'))
  1624. else:
  1625. cursor.execute("SELECT COUNT(*) as total FROM family_member_info")
  1626. total_result = cursor.fetchone()
  1627. total = total_result['total'] if total_result else 0
  1628. # Get members for current page with father information
  1629. if search:
  1630. cursor.execute("""
  1631. SELECT
  1632. fmi.id, fmi.name, fmi.simplified_name, fmi.sex, fmi.name_word_generation,
  1633. father.name as father_name, father.simplified_name as father_simplified_name, father.name_word_generation as father_generation
  1634. FROM family_member_info fmi
  1635. LEFT JOIN family_relation_info fri ON fmi.id = fri.child_mid AND fri.relation_type IN (1, 2)
  1636. LEFT JOIN family_member_info father ON fri.parent_mid = father.id
  1637. WHERE fmi.name LIKE %s OR fmi.simplified_name LIKE %s
  1638. LIMIT %s OFFSET %s
  1639. """, (f'%{search}%', f'%{search}%', per_page, offset))
  1640. else:
  1641. cursor.execute("""
  1642. SELECT
  1643. fmi.id, fmi.name, fmi.simplified_name, fmi.sex, fmi.name_word_generation,
  1644. father.name as father_name, father.simplified_name as father_simplified_name, father.name_word_generation as father_generation
  1645. FROM family_member_info fmi
  1646. LEFT JOIN family_relation_info fri ON fmi.id = fri.child_mid AND fri.relation_type IN (1, 2)
  1647. LEFT JOIN family_member_info father ON fri.parent_mid = father.id
  1648. LIMIT %s OFFSET %s
  1649. """, (per_page, offset))
  1650. members = cursor.fetchall()
  1651. # Convert to list of dictionaries if needed
  1652. members_list = []
  1653. for member in members:
  1654. members_list.append({
  1655. 'id': member['id'],
  1656. 'name': member['name'],
  1657. 'simplified_name': member['simplified_name'],
  1658. 'sex': member['sex'],
  1659. 'name_word_generation': member.get('name_word_generation'),
  1660. 'father_name': member.get('father_name'),
  1661. 'father_simplified_name': member.get('father_simplified_name'),
  1662. 'father_generation': member.get('father_generation')
  1663. })
  1664. return jsonify({"success": True, "members": members_list, "total": total})
  1665. except Exception as e:
  1666. return jsonify({"success": False, "message": f"获取成员失败: {e}"}), 500
  1667. finally:
  1668. conn.close()
  1669. def call_doubao_api(prompt, image_url=None):
  1670. """调用豆包API处理文本"""
  1671. api_key = "a1800657-9212-4afe-9b7c-b49f015c54d3"
  1672. api_url = "https://ark.cn-beijing.volces.com/api/v3/responses"
  1673. payload = {
  1674. "model": "doubao-seed-1-8-251228",
  1675. "stream": False,
  1676. "input": [
  1677. {
  1678. "role": "user",
  1679. "content": [
  1680. {"type": "input_text", "text": prompt}
  1681. ]
  1682. }
  1683. ]
  1684. }
  1685. headers = {
  1686. "Authorization": f"Bearer {api_key}",
  1687. "Content-Type": "application/json"
  1688. }
  1689. try:
  1690. response = requests.post(
  1691. api_url,
  1692. json=payload,
  1693. headers=headers,
  1694. timeout=120,
  1695. verify=False,
  1696. proxies={"http": None, "https": None}
  1697. )
  1698. if response.status_code == 200:
  1699. result = response.json()
  1700. print(f"[AI API] Raw response: {result}")
  1701. # 解析响应 - 尝试多种格式
  1702. if 'output' in result:
  1703. for item in result['output']:
  1704. if item.get('type') == 'message':
  1705. content = item.get('content')
  1706. if isinstance(content, str):
  1707. return content
  1708. elif isinstance(content, list):
  1709. for part in content:
  1710. if isinstance(part, dict) and part.get('type') == 'text':
  1711. return part.get('text', '')
  1712. elif isinstance(content, dict) and 'text' in content:
  1713. return content.get('text', '')
  1714. # 尝试其他响应格式
  1715. if 'choices' in result and len(result['choices']) > 0:
  1716. message = result['choices'][0].get('message', {})
  1717. return message.get('content', '')
  1718. # 尝试直接获取文本内容
  1719. if 'text' in result:
  1720. return result['text']
  1721. # 尝试获取响应中的message
  1722. if 'message' in result:
  1723. msg = result['message']
  1724. if isinstance(msg, str):
  1725. return msg
  1726. elif isinstance(msg, dict) and 'content' in msg:
  1727. return msg['content']
  1728. # 返回字符串形式
  1729. return str(result)
  1730. else:
  1731. print(f"[AI API] Error: {response.status_code} - {response.text}")
  1732. return None
  1733. except Exception as e:
  1734. print(f"[AI API] Exception: {e}")
  1735. return None
  1736. def parse_ai_response(ai_response):
  1737. """解析AI响应,提取族谱原文"""
  1738. if not ai_response:
  1739. return None, None
  1740. # 尝试从响应中提取JSON
  1741. try:
  1742. # 移除可能的markdown代码块标记
  1743. text = ai_response.strip()
  1744. if text.startswith('```json'):
  1745. text = text[7:]
  1746. if text.endswith('```'):
  1747. text = text[:-3]
  1748. text = text.strip()
  1749. # 尝试解析JSON
  1750. result = json.loads(text)
  1751. traditional = result.get('genealogy_traditional', '')
  1752. simplified = result.get('genealogy_simplified', '')
  1753. if traditional or simplified:
  1754. return traditional, simplified
  1755. except json.JSONDecodeError:
  1756. print(f"[AI Parse] JSON decode error: {ai_response[:200]}")
  1757. # 如果JSON解析失败,尝试直接提取文本
  1758. # 尝试匹配模式
  1759. import re
  1760. traditional_match = re.search(r'genealogy_traditional["\']?\s*[,:]\s*["\']([^"\']+)["\']', ai_response)
  1761. simplified_match = re.search(r'genealogy_simplified["\']?\s*[,:]\s*["\']([^"\']+)["\']', ai_response)
  1762. traditional = traditional_match.group(1) if traditional_match else ''
  1763. simplified = simplified_match.group(1) if simplified_match else ''
  1764. return traditional, simplified
  1765. @app.route('/manager/api/members/empty_genealogy', methods=['GET'])
  1766. def get_members_empty_genealogy():
  1767. """获取族谱原文为空的成员列表"""
  1768. if 'user_id' not in session:
  1769. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1770. page = int(request.args.get('page', 1))
  1771. per_page = int(request.args.get('per_page', 20))
  1772. offset = (page - 1) * per_page
  1773. conn = get_db_connection()
  1774. try:
  1775. with conn.cursor() as cursor:
  1776. # Count total
  1777. cursor.execute("""
  1778. SELECT COUNT(*) as total
  1779. FROM family_member_info
  1780. WHERE (genealogy_original_traditional IS NULL OR genealogy_original_traditional = '' OR genealogy_original_traditional = 'None')
  1781. AND (genealogy_original_simplified IS NULL OR genealogy_original_simplified = '' OR genealogy_original_simplified = 'None')
  1782. """)
  1783. total_result = cursor.fetchone()
  1784. total = total_result['total'] if total_result else 0
  1785. # Get members
  1786. cursor.execute("""
  1787. SELECT id, name, simplified_name, name_word_generation, sex, occupation, notes, birth_place
  1788. FROM family_member_info
  1789. WHERE (genealogy_original_traditional IS NULL OR genealogy_original_traditional = '' OR genealogy_original_traditional = 'None')
  1790. AND (genealogy_original_simplified IS NULL OR genealogy_original_simplified = '' OR genealogy_original_simplified = 'None')
  1791. LIMIT %s OFFSET %s
  1792. """, (per_page, offset))
  1793. members = cursor.fetchall()
  1794. # 关联查询父亲信息
  1795. member_list = []
  1796. for member in members:
  1797. cursor.execute("""
  1798. SELECT p.name, p.simplified_name, p.name_word_generation
  1799. FROM family_relation_info r
  1800. JOIN family_member_info p ON r.parent_mid = p.id
  1801. WHERE r.child_mid = %s AND r.relation_type = 1
  1802. LIMIT 1
  1803. """, (member['id'],))
  1804. father = cursor.fetchone()
  1805. cursor.execute("""
  1806. SELECT p.name, p.simplified_name
  1807. FROM family_relation_info r
  1808. JOIN family_member_info p ON r.parent_mid = p.id
  1809. WHERE r.child_mid = %s AND r.relation_type = 2
  1810. LIMIT 1
  1811. """, (member['id'],))
  1812. mother = cursor.fetchone()
  1813. member_list.append({
  1814. 'id': member['id'],
  1815. 'name': member['name'],
  1816. 'simplified_name': member['simplified_name'],
  1817. 'name_word_generation': member['name_word_generation'],
  1818. 'sex': member['sex'],
  1819. 'occupation': member['occupation'],
  1820. 'notes': member['notes'],
  1821. 'birth_place': member['birth_place'],
  1822. 'father_name': father['name'] if father else None,
  1823. 'father_simplified_name': father['simplified_name'] if father else None,
  1824. 'father_generation': father['name_word_generation'] if father else None,
  1825. 'mother_name': mother['name'] if mother else None,
  1826. 'mother_simplified_name': mother['simplified_name'] if mother else None
  1827. })
  1828. return jsonify({"success": True, "members": member_list, "total": total})
  1829. except Exception as e:
  1830. return jsonify({"success": False, "message": f"获取成员失败: {e}"}), 500
  1831. finally:
  1832. conn.close()
  1833. @app.route('/manager/api/members/batch_process_genealogy', methods=['POST'])
  1834. def batch_process_genealogy():
  1835. """批量处理成员族谱原文"""
  1836. if 'user_id' not in session:
  1837. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1838. data = request.get_json()
  1839. member_ids = data.get('member_ids', [])
  1840. if not member_ids or len(member_ids) > 10:
  1841. return jsonify({"success": False, "message": "请选择1-10个成员进行处理"}), 400
  1842. conn = get_db_connection()
  1843. results = []
  1844. try:
  1845. for member_id in member_ids:
  1846. with conn.cursor() as cursor:
  1847. cursor.execute("""
  1848. SELECT id, name, simplified_name, name_word_generation,
  1849. birth_place, occupation, notes, sex
  1850. FROM family_member_info WHERE id = %s
  1851. """, (member_id,))
  1852. member = cursor.fetchone()
  1853. # 获取父亲信息
  1854. cursor.execute("""
  1855. SELECT p.name, p.simplified_name
  1856. FROM family_relation_info r
  1857. JOIN family_member_info p ON r.parent_mid = p.id
  1858. WHERE r.child_mid = %s AND r.relation_type = 1
  1859. LIMIT 1
  1860. """, (member_id,))
  1861. father = cursor.fetchone()
  1862. # 获取母亲信息
  1863. cursor.execute("""
  1864. SELECT p.name, p.simplified_name
  1865. FROM family_relation_info r
  1866. JOIN family_member_info p ON r.parent_mid = p.id
  1867. WHERE r.child_mid = %s AND r.relation_type = 2
  1868. LIMIT 1
  1869. """, (member_id,))
  1870. mother = cursor.fetchone()
  1871. member['father_name'] = father['name'] if father else None
  1872. member['father_simplified_name'] = father['simplified_name'] if father else None
  1873. member['mother_name'] = mother['name'] if mother else None
  1874. member['mother_simplified_name'] = mother['simplified_name'] if mother else None
  1875. if not member:
  1876. results.append({"member_id": member_id, "success": False, "message": "成员不存在"})
  1877. continue
  1878. # 构建AI提示词
  1879. member_info = f"""
  1880. 姓名(繁体):{member['name']}
  1881. 姓名(简体):{member['simplified_name'] or '未知'}
  1882. 世系世代:{member['name_word_generation'] or '未知'}
  1883. 父亲姓名:{member['father_name'] or '未知'}
  1884. 母亲姓名:{member['mother_name'] or '未知'}
  1885. 出生地:{member['birth_place'] or '未知'}
  1886. 职业:{member['occupation'] or '未知'}
  1887. 备注:{member['notes'] or '无'}
  1888. """
  1889. prompt = f"""
  1890. 请根据以下人员信息,模拟生成该人员的族谱原文:
  1891. {member_info}
  1892. 请输出两个字段:
  1893. 1. genealogy_traditional: 族谱原文(繁体中文,模仿传统族谱格式)
  1894. 2. genealogy_simplified: 族谱原文(简体中文,将繁体转换为简体)
  1895. 请严格按照JSON格式输出,不要包含任何额外解释:
  1896. {{
  1897. "genealogy_traditional": "繁体族谱原文内容",
  1898. "genealogy_simplified": "简体族谱原文内容"
  1899. }}
  1900. """
  1901. ai_response = call_doubao_api(prompt)
  1902. print(f"[AI Response] Member {member_id}: {ai_response}")
  1903. if ai_response:
  1904. # 使用新的解析函数
  1905. traditional, simplified = parse_ai_response(ai_response)
  1906. if traditional or simplified:
  1907. with conn.cursor() as cursor:
  1908. cursor.execute("""
  1909. UPDATE family_member_info
  1910. SET genealogy_original_traditional = %s,
  1911. genealogy_original_simplified = %s
  1912. WHERE id = %s
  1913. """, (traditional, simplified, member_id))
  1914. conn.commit()
  1915. results.append({
  1916. "member_id": member_id,
  1917. "name": member['name'],
  1918. "success": True,
  1919. "traditional": traditional[:100] + "..." if len(traditional) > 100 else traditional,
  1920. "simplified": simplified[:100] + "..." if len(simplified) > 100 else simplified
  1921. })
  1922. else:
  1923. results.append({
  1924. "member_id": member_id,
  1925. "name": member['name'],
  1926. "success": False,
  1927. "message": "AI未返回有效数据"
  1928. })
  1929. else:
  1930. results.append({
  1931. "member_id": member_id,
  1932. "name": member['name'],
  1933. "success": False,
  1934. "message": "AI调用失败"
  1935. })
  1936. return jsonify({"success": True, "results": results})
  1937. except Exception as e:
  1938. print(f"[Batch Process] Exception: {e}")
  1939. return jsonify({"success": False, "message": f"批量处理失败: {e}"}), 500
  1940. finally:
  1941. conn.close()
  1942. @app.route('/manager/api/member/<int:member_id>')
  1943. def get_member(member_id):
  1944. if 'user_id' not in session:
  1945. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1946. conn = get_db_connection()
  1947. try:
  1948. with conn.cursor() as cursor:
  1949. cursor.execute("SELECT id, name, name_word_generation, source_record_id FROM family_member_info WHERE id = %s", (member_id,))
  1950. member = cursor.fetchone()
  1951. if not member:
  1952. return jsonify({"success": False, "message": "成员不存在"}), 404
  1953. return jsonify({"member": member})
  1954. except Exception as e:
  1955. return jsonify({"success": False, "message": f"获取成员失败: {e}"}), 500
  1956. finally:
  1957. conn.close()
  1958. @app.route('/manager/api/check_relations', methods=['POST'])
  1959. def check_relations():
  1960. if 'user_id' not in session:
  1961. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1962. data = request.json
  1963. people = data.get('people', [])
  1964. if not people:
  1965. return jsonify({"success": False, "matches": {}})
  1966. conn = get_db_connection()
  1967. matches = {}
  1968. try:
  1969. with conn.cursor() as cursor:
  1970. # Collect all father names and spouse names to query
  1971. names_to_check = set()
  1972. for p in people:
  1973. if p.get('father_name'): names_to_check.add(p['father_name'])
  1974. if p.get('spouse_name'): names_to_check.add(p['spouse_name'])
  1975. if not names_to_check:
  1976. return jsonify({"success": True, "matches": {}})
  1977. # Query DB
  1978. format_strings = ','.join(['%s'] * len(names_to_check))
  1979. if names_to_check:
  1980. sql = "SELECT id, name, simplified_name, sex, birthday FROM family_member_info WHERE name IN (%s) OR simplified_name IN (%s)" % (format_strings, format_strings)
  1981. cursor.execute(sql, tuple(names_to_check) * 2)
  1982. results = cursor.fetchall()
  1983. else:
  1984. results = []
  1985. # Organize by name
  1986. db_map = {} # name -> [list of members]
  1987. for r in results:
  1988. # Add under 'name' (Traditional/Old Simplified)
  1989. if r['name'] not in db_map: db_map[r['name']] = []
  1990. db_map[r['name']].append(r)
  1991. # Add under 'simplified_name' if exists
  1992. if r.get('simplified_name'):
  1993. sname = r['simplified_name']
  1994. if sname not in db_map: db_map[sname] = []
  1995. # Avoid duplicates if simplified_name is same as name?
  1996. # The list might contain same object reference, which is fine.
  1997. if sname != r['name']:
  1998. db_map[sname].append(r)
  1999. # Build matches for each input person
  2000. for index, p in enumerate(people):
  2001. p_match = {}
  2002. # Check Father
  2003. fname = p.get('father_name')
  2004. if fname and fname in db_map:
  2005. candidates = db_map[fname]
  2006. # Filter: Father should be Male usually, and older than child (if birthday available)
  2007. valid_fathers = [c for c in candidates if c['sex'] == 1]
  2008. if valid_fathers:
  2009. p_match['father'] = valid_fathers # Return all candidates
  2010. # Check Spouse
  2011. sname = p.get('spouse_name')
  2012. if sname and sname in db_map:
  2013. candidates = db_map[sname]
  2014. # Filter: Spouse usually opposite sex
  2015. target_sex = 1 if p.get('sex') == '女' else 2
  2016. valid_spouses = [c for c in candidates if c['sex'] == target_sex]
  2017. if valid_spouses:
  2018. p_match['spouse'] = valid_spouses
  2019. if p_match:
  2020. matches[index] = p_match
  2021. return jsonify({"success": True, "matches": matches})
  2022. finally:
  2023. conn.close()
  2024. @app.route('/manager/add_member', methods=['GET', 'POST'])
  2025. def add_member():
  2026. if 'user_id' not in session:
  2027. return redirect(url_for('login'))
  2028. # 获取当前登录用户名
  2029. username = session.get('username', 'genealogy')
  2030. conn = get_db_connection()
  2031. try:
  2032. # Check for source_record_id (from GET or POST)
  2033. source_record_id = request.args.get('record_id') or request.form.get('source_record_id')
  2034. prefilled_content = None
  2035. source_oss_url = None
  2036. if source_record_id:
  2037. with conn.cursor() as cursor:
  2038. cursor.execute("SELECT oss_url, ai_content, ai_status FROM genealogy_records WHERE id = %s", (source_record_id,))
  2039. rec = cursor.fetchone()
  2040. if rec:
  2041. source_oss_url = rec['oss_url']
  2042. # Check ai_status (2 = success)
  2043. if rec['ai_status'] == 2 and rec['ai_content']:
  2044. prefilled_content = rec['ai_content']
  2045. if request.method == 'POST':
  2046. # 处理生日转换为 Unix 时间戳
  2047. birthday_str = request.form.get('birthday')
  2048. birthday_ts = 0
  2049. if birthday_str:
  2050. try:
  2051. birthday_ts = int(datetime.strptime(birthday_str, '%Y-%m-%d').timestamp())
  2052. except ValueError:
  2053. birthday_ts = 0
  2054. # 关系数据 - 支持多条关系
  2055. relations = []
  2056. # Parse relations from form data
  2057. i = 0
  2058. while True:
  2059. parent_mid = request.form.get(f'relations[{i}][parent_mid]')
  2060. rel_type = request.form.get(f'relations[{i}][relation_type]')
  2061. sub_rel_type = request.form.get(f'relations[{i}][sub_relation_type]', '0')
  2062. if not parent_mid or not rel_type:
  2063. break
  2064. relations.append({
  2065. 'parent_mid': int(parent_mid),
  2066. 'relation_type': int(rel_type),
  2067. 'sub_relation_type': int(sub_rel_type)
  2068. })
  2069. i += 1
  2070. # For backward compatibility, check old-style single relation
  2071. if not relations:
  2072. related_mid = request.form.get('related_mid')
  2073. relation_type = request.form.get('relation_type')
  2074. if related_mid and relation_type:
  2075. relations.append({
  2076. 'parent_mid': int(related_mid),
  2077. 'relation_type': int(relation_type),
  2078. 'sub_relation_type': int(request.form.get('sub_relation_type', '0'))
  2079. })
  2080. # 年龄校验逻辑
  2081. for rel in relations:
  2082. if rel['relation_type'] in [1, 2]: # 1:父子 2:母子
  2083. with conn.cursor() as cursor:
  2084. cursor.execute("SELECT name, birthday FROM family_member_info WHERE id = %s", (rel['parent_mid'],))
  2085. parent = cursor.fetchone()
  2086. if parent and parent['birthday'] > 0 and birthday_ts > 0:
  2087. if birthday_ts < parent['birthday']:
  2088. error_msg = f"数据冲突:成员年龄不能比其父亲/母亲({parent['name']})大,请检查并修正出生日期。"
  2089. flash(error_msg)
  2090. # Re-fetch data for rendering
  2091. cursor.execute("SELECT id, name FROM family_member_info ORDER BY name")
  2092. all_members = cursor.fetchall()
  2093. cursor.execute("SELECT * FROM genealogy_records ORDER BY page_number ASC")
  2094. images = cursor.fetchall()
  2095. # 为图片URL添加水印
  2096. for img in images:
  2097. if img.get('oss_url'):
  2098. img['oss_url'] = add_oss_watermark(img['oss_url'], username)
  2099. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  2100. return jsonify({
  2101. "success": False,
  2102. "message": error_msg
  2103. }), 400
  2104. selected_member_name = ''
  2105. return render_template('add_member.html', all_members=all_members, images=images,
  2106. prefilled_content=prefilled_content, source_oss_url=source_oss_url, source_record_id=source_record_id, selected_member_name=selected_member_name)
  2107. break
  2108. # 获取表单数据
  2109. data = {
  2110. 'name': request.form['name'],
  2111. 'simplified_name': request.form.get('simplified_name'),
  2112. 'genealogy_original_traditional': request.form.get('genealogy_original_traditional'),
  2113. 'genealogy_original_simplified': request.form.get('genealogy_original_simplified'),
  2114. 'former_name': request.form.get('former_name'),
  2115. 'childhood_name': request.form.get('childhood_name'),
  2116. 'name_word': request.form.get('name_word'),
  2117. 'name_word_generation': ';'.join([g.strip() for g in request.form.getlist('lineage_generations[]') if g.strip()]),
  2118. 'name_title': request.form.get('name_title'),
  2119. 'sex': request.form['sex'],
  2120. 'birthday': birthday_ts,
  2121. 'is_pass_away': request.form.get('is_pass_away', 0),
  2122. 'marital_status': request.form.get('marital_status', 0),
  2123. 'birth_place': request.form.get('birth_place'),
  2124. 'branch_family_hall': request.form.get('branch_family_hall'),
  2125. 'cluster_place': request.form.get('cluster_place'),
  2126. 'nation': request.form.get('nation'),
  2127. 'residential_address': request.form.get('residential_address'),
  2128. 'phone': request.form.get('phone'),
  2129. 'mail': request.form.get('mail'),
  2130. 'wechat_account': request.form.get('wechat_account'),
  2131. 'id_number': request.form.get('id_number'),
  2132. 'occupation': request.form.get('occupation'),
  2133. 'educational': request.form.get('educational'),
  2134. 'blood_type': request.form.get('blood_type'),
  2135. 'religion': request.form.get('religion'),
  2136. 'hobbies': request.form.get('hobbies'),
  2137. 'personal_achievements': request.form.get('personal_achievements'),
  2138. 'family_rank': request.form.get('family_rank'),
  2139. 'tags': request.form.get('tags'),
  2140. 'notes': request.form.get('notes'),
  2141. 'suspected_error': request.form.get('suspected_error').strip() if request.form.get('suspected_error') else '',
  2142. 'source_record_id': request.form.get('source_record_id') or None, # Save source record ID
  2143. 'create_uid': session['user_id'] # 记录当前操作人
  2144. }
  2145. # ... (rest of logic) ...
  2146. with conn.cursor() as cursor:
  2147. print(f"[Add Member] Inserting member data: {data}")
  2148. fields = ", ".join(data.keys())
  2149. placeholders = ", ".join(["%s"] * len(data))
  2150. sql = f"INSERT INTO family_member_info ({fields}) VALUES ({placeholders})"
  2151. print(f"[Add Member] Executing SQL: {sql}")
  2152. print(f"[Add Member] SQL parameters: {list(data.values())}")
  2153. cursor.execute(sql, list(data.values()))
  2154. member_id = cursor.lastrowid
  2155. print(f"[Add Member] Inserted member with ID: {member_id}")
  2156. # 录入关系(支持多条)
  2157. sql_relation = """
  2158. INSERT INTO family_relation_info
  2159. (parent_mid, child_mid, relation_type, sub_relation_type, source_mid, generation_diff, child_order)
  2160. VALUES (%s, %s, %s, %s, %s, %s, %s)
  2161. """
  2162. for rel in relations:
  2163. rel_type = rel['relation_type']
  2164. parent_mid = rel['parent_mid']
  2165. sub_relation_type = rel['sub_relation_type']
  2166. child_order = rel.get('child_order') if rel_type in [1, 2] else None
  2167. gen_diff = 1 if rel_type in [1, 2] else 0
  2168. print(f"[Add Member] Inserting relation: parent_mid={parent_mid}, child_mid={member_id}, relation_type={rel_type}, sub_relation_type={sub_relation_type}, child_order={child_order}")
  2169. cursor.execute(sql_relation, (parent_mid, member_id, rel_type, sub_relation_type, member_id, gen_diff, child_order))
  2170. # Update AI Record Status if applicable
  2171. source_record_id = data.get('source_record_id')
  2172. source_index = request.form.get('source_index')
  2173. if source_record_id and source_index and source_index.isdigit():
  2174. try:
  2175. idx = int(source_index)
  2176. print(f"[Add Member] Updating AI record status: record_id={source_record_id}, index={idx}")
  2177. cursor.execute("SELECT ai_content FROM genealogy_records WHERE id = %s FOR UPDATE", (source_record_id,))
  2178. rec = cursor.fetchone()
  2179. if rec and rec['ai_content']:
  2180. import json
  2181. content = json.loads(rec['ai_content'])
  2182. # Ensure content is a list (it might be a dict if single object, though we try to normalize)
  2183. if isinstance(content, dict):
  2184. content = [content]
  2185. if isinstance(content, list):
  2186. updated = False
  2187. if 0 <= idx < len(content):
  2188. # Always update the status regardless of current value
  2189. content[idx]['is_imported'] = True
  2190. content[idx]['imported_member_id'] = member_id
  2191. updated = True
  2192. if updated:
  2193. new_content = json.dumps(content, ensure_ascii=False)
  2194. cursor.execute("UPDATE genealogy_records SET ai_content = %s WHERE id = %s", (new_content, source_record_id))
  2195. print(f"[Add Member] Updated AI record status")
  2196. except Exception as e:
  2197. print(f"[Add Member] Error updating AI content status: {e}")
  2198. print(f"[Add Member] Committing transaction")
  2199. if safe_commit(conn):
  2200. print(f"[Add Member] Transaction committed successfully")
  2201. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  2202. return jsonify({"success": True, "message": "成员录入成功", "member_id": member_id})
  2203. flash('成员录入成功')
  2204. return redirect(url_for('members'))
  2205. else:
  2206. print(f"[Add Member] Transaction commit failed!")
  2207. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  2208. return jsonify({"success": False, "message": "成员录入失败,事务提交失败"}), 500
  2209. flash('成员录入失败,事务提交失败')
  2210. return redirect(url_for('add_member'))
  2211. with conn.cursor() as cursor:
  2212. cursor.execute("SELECT id, name FROM family_member_info ORDER BY name")
  2213. all_members = cursor.fetchall()
  2214. cursor.execute("SELECT * FROM genealogy_records ORDER BY page_number ASC")
  2215. images = cursor.fetchall()
  2216. # 为图片URL添加水印
  2217. for img in images:
  2218. if img.get('oss_url'):
  2219. img['oss_url'] = add_oss_watermark(img['oss_url'], username)
  2220. except Exception as e:
  2221. flash(f'发生错误: {e}')
  2222. all_members = []
  2223. images = []
  2224. finally:
  2225. conn.close()
  2226. selected_member_name = ''
  2227. return render_template('add_member.html', all_members=all_members, images=images,
  2228. prefilled_content=prefilled_content, source_oss_url=source_oss_url, source_record_id=source_record_id, selected_member_name=selected_member_name)
  2229. @app.route('/manager/edit_member/<int:member_id>', methods=['GET', 'POST'])
  2230. def edit_member(member_id):
  2231. if 'user_id' not in session:
  2232. return redirect(url_for('login'))
  2233. conn = get_db_connection()
  2234. try:
  2235. if request.method == 'POST':
  2236. birthday_str = request.form.get('birthday')
  2237. birthday_ts = 0
  2238. if birthday_str:
  2239. try:
  2240. birthday_ts = int(datetime.strptime(birthday_str, '%Y-%m-%d').timestamp())
  2241. except ValueError:
  2242. birthday_ts = 0
  2243. # 关系数据 - 支持多条关系
  2244. relations = []
  2245. i = 0
  2246. while True:
  2247. parent_mid = request.form.get(f'relations[{i}][parent_mid]')
  2248. rel_type = request.form.get(f'relations[{i}][relation_type]')
  2249. sub_rel_type = request.form.get(f'relations[{i}][sub_relation_type]', '0')
  2250. child_order_raw = request.form.get(f'relations[{i}][child_order]', '')
  2251. if not parent_mid or not rel_type:
  2252. break
  2253. child_order = int(child_order_raw) if child_order_raw.strip().isdigit() else None
  2254. relations.append({
  2255. 'parent_mid': int(parent_mid),
  2256. 'relation_type': int(rel_type),
  2257. 'sub_relation_type': int(sub_rel_type),
  2258. 'child_order': child_order,
  2259. })
  2260. i += 1
  2261. # For backward compatibility
  2262. if not relations:
  2263. related_mid = request.form.get('related_mid')
  2264. relation_type = request.form.get('relation_type')
  2265. if related_mid and relation_type:
  2266. child_order_raw = request.form.get('child_order', '')
  2267. relations.append({
  2268. 'parent_mid': int(related_mid),
  2269. 'relation_type': int(relation_type),
  2270. 'sub_relation_type': int(request.form.get('sub_relation_type', '0')),
  2271. 'child_order': int(child_order_raw) if child_order_raw.strip().isdigit() else None,
  2272. })
  2273. # 年龄校验逻辑
  2274. for rel in relations:
  2275. if rel['relation_type'] in [1, 2]:
  2276. with conn.cursor() as cursor:
  2277. cursor.execute("SELECT name, birthday FROM family_member_info WHERE id = %s", (rel['parent_mid'],))
  2278. parent = cursor.fetchone()
  2279. if parent and parent['birthday'] > 0 and birthday_ts > 0:
  2280. if birthday_ts < parent['birthday']:
  2281. flash(f"数据冲突:成员年龄不能比其父亲/母亲({parent['name']})大,请检查并修正出生日期。")
  2282. # 重新加载编辑页所需数据
  2283. cursor.execute("SELECT * FROM family_member_info WHERE id = %s", (member_id,))
  2284. member = cursor.fetchone()
  2285. member['birthday_date'] = birthday_str # 保持用户输入
  2286. cursor.execute("SELECT id, name FROM family_member_info WHERE id != %s ORDER BY name", (member_id,))
  2287. all_members = cursor.fetchall()
  2288. cursor.execute("SELECT * FROM genealogy_records ORDER BY page_number ASC")
  2289. images = cursor.fetchall()
  2290. # 为图片URL添加水印
  2291. for img in images:
  2292. if img.get('oss_url'):
  2293. img['oss_url'] = add_oss_watermark(img['oss_url'], username)
  2294. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  2295. return jsonify({
  2296. "success": False,
  2297. "message": f"数据冲突:成员年龄不能比其父亲/母亲({parent['name']})大,请检查并修正出生日期。"
  2298. }), 400
  2299. selected_member_name = ''
  2300. return render_template('add_member.html', member=member, images=images, all_members=all_members, selected_member_name=selected_member_name)
  2301. break
  2302. data = {
  2303. 'name': request.form['name'],
  2304. 'simplified_name': request.form.get('simplified_name'),
  2305. 'genealogy_original_traditional': request.form.get('genealogy_original_traditional'),
  2306. 'genealogy_original_simplified': request.form.get('genealogy_original_simplified'),
  2307. 'former_name': request.form.get('former_name'),
  2308. 'childhood_name': request.form.get('childhood_name'),
  2309. 'name_word': request.form.get('name_word'),
  2310. 'name_word_generation': ';'.join([g.strip() for g in request.form.getlist('lineage_generations[]') if g.strip()]),
  2311. 'name_title': request.form.get('name_title'),
  2312. 'sex': request.form['sex'],
  2313. 'birthday': birthday_ts,
  2314. 'is_pass_away': request.form.get('is_pass_away', 0),
  2315. 'marital_status': request.form.get('marital_status', 0),
  2316. 'birth_place': request.form.get('birth_place'),
  2317. 'branch_family_hall': request.form.get('branch_family_hall'),
  2318. 'cluster_place': request.form.get('cluster_place'),
  2319. 'nation': request.form.get('nation'),
  2320. 'residential_address': request.form.get('residential_address'),
  2321. 'phone': request.form.get('phone'),
  2322. 'mail': request.form.get('mail'),
  2323. 'wechat_account': request.form.get('wechat_account'),
  2324. 'id_number': request.form.get('id_number'),
  2325. 'occupation': request.form.get('occupation'),
  2326. 'educational': request.form.get('educational'),
  2327. 'blood_type': request.form.get('blood_type'),
  2328. 'religion': request.form.get('religion'),
  2329. 'hobbies': request.form.get('hobbies'),
  2330. 'personal_achievements': request.form.get('personal_achievements'),
  2331. 'family_rank': request.form.get('family_rank'),
  2332. 'tags': request.form.get('tags'),
  2333. 'notes': request.form.get('notes'),
  2334. 'suspected_error': request.form.get('suspected_error').strip() if request.form.get('suspected_error') else '',
  2335. 'source_record_id': request.form.get('source_record_id') or None,
  2336. 'create_uid': session['user_id'] # 记录当前操作人
  2337. }
  2338. with conn.cursor() as cursor:
  2339. print(f"[Edit Member] Updating member data: {data}")
  2340. update_parts = [f"{k} = %s" for k in data.keys()]
  2341. sql = f"UPDATE family_member_info SET {', '.join(update_parts)} WHERE id = %s"
  2342. print(f"[Edit Member] Executing SQL: {sql}")
  2343. print(f"[Edit Member] SQL parameters: {list(data.values()) + [member_id]}")
  2344. cursor.execute(sql, list(data.values()) + [member_id])
  2345. print(f"[Edit Member] Updated member with ID: {member_id}")
  2346. # 更新关系(支持多条)
  2347. print(f"[Edit Member] Deleting existing relations for member ID: {member_id}")
  2348. cursor.execute("DELETE FROM family_relation_info WHERE source_mid = %s", (member_id,))
  2349. sql_relation = """
  2350. INSERT INTO family_relation_info
  2351. (parent_mid, child_mid, relation_type, sub_relation_type, source_mid, generation_diff, child_order)
  2352. VALUES (%s, %s, %s, %s, %s, %s, %s)
  2353. """
  2354. for rel in relations:
  2355. rel_type = rel['relation_type']
  2356. parent_mid = rel['parent_mid']
  2357. sub_relation_type = rel['sub_relation_type']
  2358. child_order = rel.get('child_order') if rel_type in [1, 2] else None
  2359. gen_diff = 1 if rel_type in [1, 2] else 0
  2360. print(f"[Edit Member] Inserting relation: parent_mid={parent_mid}, child_mid={member_id}, relation_type={rel_type}, sub_relation_type={sub_relation_type}, child_order={child_order}")
  2361. cursor.execute(sql_relation, (parent_mid, member_id, rel_type, sub_relation_type, member_id, gen_diff, child_order))
  2362. # Update AI Record Status if applicable
  2363. source_record_id = data.get('source_record_id')
  2364. source_index = request.form.get('source_index')
  2365. if source_record_id and source_index and source_index.isdigit():
  2366. try:
  2367. idx = int(source_index)
  2368. print(f"[Edit Member] Updating AI record status: record_id={source_record_id}, index={idx}")
  2369. cursor.execute("SELECT ai_content FROM genealogy_records WHERE id = %s FOR UPDATE", (source_record_id,))
  2370. rec = cursor.fetchone()
  2371. if rec and rec['ai_content']:
  2372. import json
  2373. content = json.loads(rec['ai_content'])
  2374. if isinstance(content, dict):
  2375. content = [content]
  2376. if isinstance(content, list):
  2377. updated = False
  2378. if 0 <= idx < len(content):
  2379. # Always update the status regardless of current value
  2380. content[idx]['is_imported'] = True
  2381. content[idx]['imported_member_id'] = member_id
  2382. updated = True
  2383. if updated:
  2384. new_content = json.dumps(content, ensure_ascii=False)
  2385. cursor.execute("UPDATE genealogy_records SET ai_content = %s WHERE id = %s", (new_content, source_record_id))
  2386. print(f"[Edit Member] Updated AI record status")
  2387. except Exception as e:
  2388. print(f"[Edit Member] Error updating AI content status: {e}")
  2389. print(f"[Edit Member] Committing transaction")
  2390. conn.commit()
  2391. print(f"[Edit Member] Transaction committed successfully")
  2392. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  2393. return jsonify({"success": True, "message": "成员信息更新成功"})
  2394. flash('成员信息更新成功')
  2395. return redirect(url_for('members'))
  2396. with conn.cursor() as cursor:
  2397. cursor.execute("SELECT * FROM family_member_info WHERE id = %s", (member_id,))
  2398. member = cursor.fetchone()
  2399. if not member:
  2400. flash('成员不存在')
  2401. return redirect(url_for('members'))
  2402. # 格式化日期供显示
  2403. if member.get('birthday'):
  2404. member['birthday_date'] = format_timestamp(member['birthday'])
  2405. # 获取现有关系
  2406. cursor.execute("SELECT * FROM family_relation_info WHERE source_mid = %s LIMIT 1", (member_id,))
  2407. current_relation = cursor.fetchone()
  2408. cursor.execute("SELECT id, name FROM family_member_info WHERE id != %s ORDER BY name", (member_id,))
  2409. all_members = cursor.fetchall()
  2410. cursor.execute("SELECT * FROM genealogy_records ORDER BY page_number ASC")
  2411. images = cursor.fetchall()
  2412. # 为图片URL添加水印
  2413. for img in images:
  2414. if img.get('oss_url'):
  2415. img['oss_url'] = add_oss_watermark(img['oss_url'], username)
  2416. finally:
  2417. conn.close()
  2418. # Calculate selected_member_name based on current_relation
  2419. selected_member_name = ''
  2420. if current_relation and current_relation['parent_mid']:
  2421. for m in all_members:
  2422. if m['id'] == current_relation['parent_mid']:
  2423. selected_member_name = m['name']
  2424. break
  2425. # Get source_record_id from member data
  2426. source_record_id = member.get('source_record_id') if member else None
  2427. return render_template('add_member.html', member=member, images=images, all_members=all_members, current_relation=current_relation, selected_member_name=selected_member_name, source_record_id=source_record_id)
  2428. @app.route('/manager/member_detail/<int:member_id>')
  2429. def member_detail(member_id):
  2430. if 'user_id' not in session:
  2431. return redirect(url_for('login'))
  2432. # 获取当前登录用户名
  2433. username = session.get('username', 'genealogy')
  2434. conn = get_db_connection()
  2435. try:
  2436. with conn.cursor() as cursor:
  2437. # Join with genealogy_records to get source image info
  2438. sql = """
  2439. SELECT m.*, r.oss_url as source_image_url, r.page_number as source_page,
  2440. r.genealogy_version, r.genealogy_source, r.upload_person
  2441. FROM family_member_info m
  2442. LEFT JOIN genealogy_records r ON m.source_record_id = r.id
  2443. WHERE m.id = %s
  2444. """
  2445. cursor.execute(sql, (member_id,))
  2446. member = cursor.fetchone()
  2447. if not member:
  2448. flash('成员不存在')
  2449. return redirect(url_for('members'))
  2450. # 为图片URL添加水印
  2451. if member.get('source_image_url'):
  2452. member['source_image_url'] = add_oss_watermark(member['source_image_url'], username)
  2453. member['birthday_str'] = format_timestamp(member.get('birthday'))
  2454. # 获取关系(包含子类型和第几子)
  2455. cursor.execute("""
  2456. SELECT m.id, m.name, m.simplified_name, r.relation_type, r.sub_relation_type, r.child_order
  2457. FROM family_relation_info r
  2458. JOIN family_member_info m ON r.parent_mid = m.id
  2459. WHERE r.child_mid = %s
  2460. """, (member_id,))
  2461. parents = cursor.fetchall()
  2462. cursor.execute("""
  2463. SELECT m.id, m.name, m.simplified_name, r.relation_type, r.sub_relation_type, r.child_order
  2464. FROM family_relation_info r
  2465. JOIN family_member_info m ON r.child_mid = m.id
  2466. WHERE r.parent_mid = %s
  2467. ORDER BY COALESCE(r.child_order, 99999), m.id
  2468. """, (member_id,))
  2469. children = cursor.fetchall()
  2470. # 计算入继说明:若该成员有 sub_relation_type=3(养父母)记录,
  2471. # 则从 sub_relation_type=2(生父母)记录中取排行,生成"由xxx公第N子入继"
  2472. _order_labels = {1:'长', 2:'次', 3:'三', 4:'四', 5:'五',
  2473. 6:'六', 7:'七', 8:'八', 9:'九', 10:'十'}
  2474. adopt_info = None
  2475. is_adopted_in = any(p['sub_relation_type'] == 3 for p in parents)
  2476. if is_adopted_in:
  2477. bio = next((p for p in parents if p['sub_relation_type'] == 2), None)
  2478. if bio:
  2479. bio_name = bio['simplified_name'] or bio['name']
  2480. order = bio['child_order']
  2481. order_str = _order_labels.get(order, f'第{order}') if order else '某'
  2482. adopt_info = f"由{bio_name}公{order_str}子入继"
  2483. finally:
  2484. conn.close()
  2485. return render_template('member_detail.html', member=member, parents=parents,
  2486. children=children, adopt_info=adopt_info)
  2487. @app.route('/manager/delete_member/<int:member_id>', methods=['POST'])
  2488. def delete_member(member_id):
  2489. if 'user_id' not in session:
  2490. return jsonify({"success": False, "message": "Unauthorized"}), 401
  2491. conn = get_db_connection()
  2492. try:
  2493. with conn.cursor() as cursor:
  2494. # 1. 删除关系表中关联该成员的所有记录
  2495. cursor.execute("DELETE FROM family_relation_info WHERE parent_mid = %s OR child_mid = %s OR source_mid = %s",
  2496. (member_id, member_id, member_id))
  2497. # 2. 删除成员本身
  2498. cursor.execute("DELETE FROM family_member_info WHERE id = %s", (member_id,))
  2499. conn.commit()
  2500. flash('成员及其关系已成功删除')
  2501. return redirect(url_for('members'))
  2502. except Exception as e:
  2503. conn.rollback()
  2504. flash(f'删除失败: {e}')
  2505. return redirect(url_for('members'))
  2506. finally:
  2507. conn.close()
  2508. @app.route('/manager/home')
  2509. def home():
  2510. """Home page - Dashboard for the genealogy management system"""
  2511. if 'user_id' not in session:
  2512. return redirect(url_for('login'))
  2513. # Force re-login if is_super_admin not set in session (fresh login required)
  2514. if 'is_super_admin' not in session:
  2515. session.clear()
  2516. flash('请重新登录以获取最新权限')
  2517. return redirect(url_for('login'))
  2518. conn = get_db_connection()
  2519. try:
  2520. with conn.cursor() as cursor:
  2521. # Get member count
  2522. cursor.execute("SELECT COUNT(*) as count FROM family_member_info")
  2523. member_count = cursor.fetchone()['count']
  2524. # Get record count
  2525. cursor.execute("SELECT COUNT(*) as count FROM genealogy_records")
  2526. record_count = cursor.fetchone()['count']
  2527. # Get PDF count
  2528. cursor.execute("SELECT COUNT(*) as count FROM genealogy_pdfs")
  2529. pdf_count = cursor.fetchone()['count']
  2530. # Get suspected error count
  2531. cursor.execute("SELECT COUNT(*) as count FROM family_member_info WHERE suspected_error IS NOT NULL AND TRIM(suspected_error) != ''")
  2532. error_count = cursor.fetchone()['count']
  2533. finally:
  2534. conn.close()
  2535. return render_template('home.html',
  2536. member_count=member_count,
  2537. record_count=record_count,
  2538. pdf_count=pdf_count,
  2539. error_count=error_count)
  2540. @app.route('/manager/login', methods=['GET', 'POST'])
  2541. def login():
  2542. if request.method == 'POST':
  2543. username = request.form['username']
  2544. password = request.form['password']
  2545. try:
  2546. conn = get_db_connection()
  2547. try:
  2548. with conn.cursor() as cursor:
  2549. cursor.execute("SELECT * FROM users WHERE username=%s AND password=%s", (username, password))
  2550. user = cursor.fetchone()
  2551. if user:
  2552. session['user_id'] = user['id']
  2553. session['username'] = user['username']
  2554. session['is_super_admin'] = user.get('is_super_admin', 0) == 1
  2555. return redirect(url_for('home'))
  2556. else:
  2557. flash('用户名或密码错误')
  2558. finally:
  2559. conn.close()
  2560. except Exception as e:
  2561. flash(f'数据库连接错误: {str(e)}')
  2562. print(f'Login error: {str(e)}')
  2563. return render_template('login.html')
  2564. @app.route('/manager/logout')
  2565. def logout():
  2566. session.clear()
  2567. return redirect(url_for('login'))
  2568. @app.route('/manager/api/check_name')
  2569. def check_name():
  2570. if 'user_id' not in session:
  2571. return jsonify({"success": False, "message": "Unauthorized"}), 401
  2572. name = request.args.get('name', '').strip()
  2573. if not name:
  2574. return jsonify({"success": True, "exists": False})
  2575. conn = get_db_connection()
  2576. try:
  2577. with conn.cursor() as cursor:
  2578. # Check for name or simplified_name match
  2579. cursor.execute("SELECT id, name, simplified_name, sex, birthday, is_pass_away FROM family_member_info WHERE name = %s OR simplified_name = %s", (name, name))
  2580. matches = cursor.fetchall()
  2581. if matches:
  2582. # Format birthday for display
  2583. for m in matches:
  2584. if m.get('birthday'):
  2585. m['birthday_str'] = format_timestamp(m['birthday'])
  2586. else:
  2587. m['birthday_str'] = '未知'
  2588. return jsonify({"success": True, "exists": True, "matches": matches})
  2589. else:
  2590. return jsonify({"success": True, "exists": False})
  2591. except Exception as e:
  2592. return jsonify({"success": False, "error": str(e)}), 500
  2593. finally:
  2594. conn.close()
  2595. import requests
  2596. import json
  2597. import re
  2598. @app.route('/manager/api/recognize_image', methods=['POST'])
  2599. def recognize_image():
  2600. if 'user_id' not in session:
  2601. return jsonify({"success": False, "message": "Unauthorized"}), 401
  2602. data = request.json
  2603. image_url = data.get('image_url')
  2604. if not image_url:
  2605. return jsonify({"success": False, "message": "No image URL provided"}), 400
  2606. api_key = "a1800657-9212-4afe-9b7c-b49f015c54d3"
  2607. api_url = "https://ark.cn-beijing.volces.com/api/v3/responses"
  2608. prompt = """
  2609. 请分析这张家谱图片,提取其中关于人物的信息。
  2610. 请务必将繁体字转换为简体字(original_name 字段除外)。
  2611. 特别注意:'name' 字段必须是纯简体中文,不能包含繁体字(例如:'學'应转换为'学','劉'应转换为'刘','萬'应转换为'万')。
  2612. 请提取以下字段(如果存在):
  2613. - original_name: 原始姓名(严格保持图片上的繁体字,不做任何修改或转换)
  2614. - name: 简体姓名(必须转换为简体中文,去除不需要的敬称)
  2615. - sex: 性别(男/女)
  2616. - birthday: 出生日期(尝试转换为YYYY-MM-DD格式,如果无法确定年份可只填月日)
  2617. - death_date: 逝世日期(如文本中出现“殁”、“葬”、“卒”等字眼及其对应的时间,请提取)
  2618. - father_name: 父亲姓名
  2619. - spouse_name: 配偶姓名
  2620. - generation: 第几世/代数
  2621. - name_word: 字辈(例如名字为“学勤公”,“学”为字辈;提取名字中的字辈信息)
  2622. - education: 学历/功名
  2623. - title: 官职/称号
  2624. 请严格以JSON列表格式返回,不要包含Markdown代码块标记(如 ```json ... ```),直接返回JSON数组。
  2625. 如果包含多个人物,请都提取出来。
  2626. """
  2627. ai_payload_url = get_normalized_base64_image(image_url)
  2628. payload = {
  2629. "model": "doubao-seed-1-8-251228",
  2630. "stream": True,
  2631. "input": [
  2632. {
  2633. "role": "user",
  2634. "content": [
  2635. {
  2636. "type": "input_image",
  2637. "image_url": ai_payload_url
  2638. },
  2639. {
  2640. "type": "input_text",
  2641. "text": prompt
  2642. }
  2643. ]
  2644. }
  2645. ]
  2646. }
  2647. headers = {
  2648. "Authorization": f"Bearer {api_key}",
  2649. "Content-Type": "application/json"
  2650. }
  2651. def generate():
  2652. yield "正在连接 AI 服务...\n"
  2653. try:
  2654. # 使用 stream=True, timeout=120
  2655. # 增加 verify=False 以防 SSL 问题(开发环境)
  2656. # 增加 proxies=None 以防本地代理干扰
  2657. with requests.post(
  2658. api_url,
  2659. json=payload,
  2660. headers=headers,
  2661. stream=True,
  2662. timeout=1200,
  2663. verify=False,
  2664. proxies={"http": None, "https": None}
  2665. ) as r:
  2666. if r.status_code != 200:
  2667. yield f"Error: API returned status code {r.status_code}. Response: {r.text}"
  2668. return
  2669. yield "连接成功,正在等待 AI 响应...\n"
  2670. full_reasoning = ""
  2671. json_started = False
  2672. for line in r.iter_lines():
  2673. if line:
  2674. line_str = line.decode('utf-8')
  2675. if line_str.startswith('data: '):
  2676. json_str = line_str[6:]
  2677. if json_str.strip() == '[DONE]':
  2678. break
  2679. try:
  2680. chunk = json.loads(json_str)
  2681. # 处理 standard OpenAI choices format (content)
  2682. if 'choices' in chunk and len(chunk['choices']) > 0:
  2683. delta = chunk['choices'][0].get('delta', {})
  2684. if 'content' in delta:
  2685. if not json_started:
  2686. yield "|||JSON_START|||"
  2687. json_started = True
  2688. yield delta['content']
  2689. # 处理 standard OpenAI choices format (reasoning_content) if any
  2690. if 'reasoning_content' in delta:
  2691. yield f"\n[推理]: {delta['reasoning_content']}"
  2692. # 处理 Doubao/Volcano specific formats
  2693. # Type: response.reasoning_summary_text.delta
  2694. if chunk.get('type') == 'response.reasoning_summary_text.delta':
  2695. if 'delta' in chunk:
  2696. yield chunk['delta']
  2697. # Type: response.text.delta
  2698. if chunk.get('type') == 'response.text.delta':
  2699. if 'delta' in chunk:
  2700. if not json_started:
  2701. yield "|||JSON_START|||"
  2702. json_started = True
  2703. yield chunk['delta']
  2704. # Type: response.output_item.added (May contain initial content or status)
  2705. # Type: response.reasoning_summary_part.added
  2706. except Exception as e:
  2707. print(f"Chunk parse error: {e}")
  2708. else:
  2709. # 尝试直接解析非 data: 开头的行
  2710. try:
  2711. chunk = json.loads(line_str)
  2712. if 'choices' in chunk and len(chunk['choices']) > 0:
  2713. content = chunk['choices'][0]['message']['content']
  2714. yield content
  2715. except:
  2716. pass
  2717. except Exception as e:
  2718. yield f"\n[Error: {str(e)}]"
  2719. return Response(stream_with_context(generate()), mimetype='text/plain')
  2720. @app.route('/manager/api/start_analysis/<int:record_id>', methods=['POST'])
  2721. def start_analysis(record_id):
  2722. if 'user_id' not in session:
  2723. return jsonify({"success": False, "message": "Unauthorized"}), 401
  2724. conn = get_db_connection()
  2725. try:
  2726. with conn.cursor() as cursor:
  2727. # Check if record exists
  2728. cursor.execute("SELECT oss_url, ai_status FROM genealogy_records WHERE id = %s", (record_id,))
  2729. record = cursor.fetchone()
  2730. if not record:
  2731. return jsonify({"success": False, "message": "Record not found"}), 404
  2732. # Update status to processing (1)
  2733. cursor.execute("UPDATE genealogy_records SET ai_status = 1 WHERE id = %s", (record_id,))
  2734. conn.commit()
  2735. # Start background task
  2736. threading.Thread(target=process_ai_task, args=(record_id, record['oss_url'])).start()
  2737. return jsonify({"success": True, "message": "Analysis started"})
  2738. except Exception as e:
  2739. return jsonify({"success": False, "message": str(e)}), 500
  2740. finally:
  2741. conn.close()
  2742. def process_files_background(upload_folder, saved_files, manual_page, suggested_page, genealogy_version, genealogy_source, upload_person):
  2743. current_suggested_page = int(manual_page) if manual_page and str(manual_page).isdigit() else suggested_page
  2744. ensure_pdf_table()
  2745. for item in saved_files:
  2746. if len(item) >= 4:
  2747. filename, file_path, file_page, original_filename = item[0], item[1], item[2], item[3]
  2748. elif len(item) == 3:
  2749. filename, file_path, file_page = item
  2750. original_filename = filename
  2751. else:
  2752. filename, file_path = item[0], item[1]
  2753. file_page = None
  2754. original_filename = filename
  2755. try:
  2756. if filename.lower().endswith('.pdf'):
  2757. import uuid
  2758. display_pdf_name = (original_filename or filename).strip() or filename
  2759. oss_pdf_name = secure_filename(display_pdf_name)
  2760. if not oss_pdf_name or not oss_pdf_name.lower().endswith('.pdf'):
  2761. oss_pdf_name = f"genealogy_pdf_{uuid.uuid4().hex[:8]}.pdf"
  2762. pdf_oss_url = upload_to_oss(file_path, custom_filename=oss_pdf_name)
  2763. if pdf_oss_url:
  2764. desc_parts = []
  2765. if genealogy_version:
  2766. desc_parts.append(genealogy_version)
  2767. if genealogy_source:
  2768. desc_parts.append(genealogy_source)
  2769. pdf_description = ' · '.join(desc_parts) if desc_parts else ''
  2770. conn_pdf = get_db_connection()
  2771. try:
  2772. with conn_pdf.cursor() as cursor:
  2773. cursor.execute(
  2774. "INSERT INTO genealogy_pdfs (file_name, oss_url, description, uploader) VALUES (%s, %s, %s, %s)",
  2775. (display_pdf_name, pdf_oss_url, pdf_description, upload_person or '')
  2776. )
  2777. conn_pdf.commit()
  2778. except Exception as pdf_meta_e:
  2779. print(f"Error inserting genealogy_pdfs for {display_pdf_name}: {pdf_meta_e}")
  2780. finally:
  2781. conn_pdf.close()
  2782. else:
  2783. print(f"Warning: full PDF upload to OSS failed for {filename}, scan pages will still be processed.")
  2784. doc = fitz.open(file_path)
  2785. for page_index in range(len(doc)):
  2786. img_path = None
  2787. try:
  2788. page = doc.load_page(page_index)
  2789. max_dim = max(page.rect.width, page.rect.height)
  2790. zoom = 2000 / max_dim if max_dim > 0 else 2.0
  2791. if zoom > 2.5: zoom = 2.5
  2792. mat = fitz.Matrix(zoom, zoom)
  2793. # Use get_pixmap with matrix directly
  2794. pix = page.get_pixmap(matrix=mat)
  2795. final_page = current_suggested_page
  2796. if genealogy_version and genealogy_source:
  2797. if final_page is not None and str(final_page).strip() != '':
  2798. img_filename = f"{genealogy_version}_{genealogy_source}_{final_page}.jpg"
  2799. else:
  2800. img_filename = f"{genealogy_version}_{genealogy_source}.jpg"
  2801. else:
  2802. img_filename = f"{os.path.splitext(filename)[0]}_page_{page_index+1}.jpg"
  2803. img_path = os.path.join(upload_folder, img_filename)
  2804. # Save the pixmap to the image path
  2805. pix.save(img_path)
  2806. oss_url = upload_to_oss(img_path, custom_filename=img_filename)
  2807. if oss_url:
  2808. conn = get_db_connection()
  2809. try:
  2810. with conn.cursor() as cursor:
  2811. sql = """INSERT INTO genealogy_records
  2812. (file_name, oss_url, page_number, ai_status, genealogy_version, genealogy_source, upload_person, file_type)
  2813. VALUES (%s, %s, %s, 1, %s, %s, %s, %s)"""
  2814. cursor.execute(sql, (img_filename, oss_url, final_page, genealogy_version, genealogy_source, upload_person, 'PDF'))
  2815. record_id = cursor.lastrowid
  2816. conn.commit()
  2817. threading.Thread(target=process_ai_task, args=(record_id, oss_url)).start()
  2818. current_suggested_page += 1
  2819. finally:
  2820. conn.close()
  2821. except Exception as page_e:
  2822. print(f"Error processing page {page_index} of {filename}: {page_e}")
  2823. finally:
  2824. if img_path and os.path.exists(img_path):
  2825. try:
  2826. os.remove(img_path)
  2827. except:
  2828. pass
  2829. doc.close()
  2830. else:
  2831. img_path = compress_image_if_needed(file_path)
  2832. # Use explicitly set page number if provided, otherwise extract from filename or auto-increment
  2833. if file_page and str(file_page).isdigit():
  2834. final_page = int(file_page)
  2835. current_suggested_page = final_page + 1
  2836. page_num = final_page
  2837. else:
  2838. page_num = extract_page_number(img_path)
  2839. final_page = page_num if page_num else current_suggested_page
  2840. ext = os.path.splitext(img_path)[1]
  2841. if genealogy_version and genealogy_source:
  2842. if final_page is not None and str(final_page).strip() != '':
  2843. img_filename = f"{genealogy_version}_{genealogy_source}_{final_page}{ext}"
  2844. else:
  2845. img_filename = f"{genealogy_version}_{genealogy_source}{ext}"
  2846. else:
  2847. img_filename = os.path.basename(img_path)
  2848. oss_url = upload_to_oss(img_path, custom_filename=img_filename)
  2849. if oss_url:
  2850. conn = get_db_connection()
  2851. try:
  2852. with conn.cursor() as cursor:
  2853. sql = """INSERT INTO genealogy_records
  2854. (file_name, oss_url, page_number, ai_status, genealogy_version, genealogy_source, upload_person, file_type)
  2855. VALUES (%s, %s, %s, 1, %s, %s, %s, %s)"""
  2856. cursor.execute(sql, (img_filename, oss_url, final_page, genealogy_version, genealogy_source, upload_person, '图片'))
  2857. record_id = cursor.lastrowid
  2858. conn.commit()
  2859. threading.Thread(target=process_ai_task, args=(record_id, oss_url)).start()
  2860. if page_num:
  2861. current_suggested_page = page_num + 1
  2862. else:
  2863. current_suggested_page += 1
  2864. finally:
  2865. conn.close()
  2866. if img_path and img_path != file_path and os.path.exists(img_path):
  2867. try:
  2868. os.remove(img_path)
  2869. except:
  2870. pass
  2871. except Exception as e:
  2872. print(f"Error processing file {filename}: {e}")
  2873. finally:
  2874. if os.path.exists(file_path):
  2875. try:
  2876. os.remove(file_path)
  2877. except:
  2878. pass
  2879. @app.route('/manager/upload', methods=['GET', 'POST'])
  2880. def upload():
  2881. if 'user_id' not in session:
  2882. return redirect(url_for('login'))
  2883. # 获取建议页码 (当前最大页码 + 1)
  2884. conn = get_db_connection()
  2885. suggested_page = 1
  2886. try:
  2887. with conn.cursor() as cursor:
  2888. cursor.execute("SELECT MAX(page_number) as max_p FROM genealogy_records")
  2889. result = cursor.fetchone()
  2890. if result and result['max_p']:
  2891. suggested_page = result['max_p'] + 1
  2892. finally:
  2893. conn.close()
  2894. if request.method == 'POST':
  2895. if 'file' not in request.files:
  2896. flash('未选择文件')
  2897. return redirect(request.url)
  2898. files = request.files.getlist('file')
  2899. if not files or files[0].filename == '':
  2900. flash('未选择文件')
  2901. return redirect(request.url)
  2902. manual_page = request.form.get('manual_page')
  2903. genealogy_version = request.form.get('genealogy_version', '')
  2904. genealogy_source = request.form.get('genealogy_source', '')
  2905. upload_person = request.form.get('upload_person', '')
  2906. if not upload_person:
  2907. upload_person = session.get('username', '')
  2908. import uuid
  2909. saved_files = []
  2910. for i, file in enumerate(files):
  2911. if not file or not file.filename:
  2912. continue
  2913. original_filename = file.filename
  2914. ext = os.path.splitext(original_filename)[1].lower()
  2915. base_name = secure_filename(original_filename)
  2916. # If secure_filename removes all characters (e.g., pure Chinese name) or just leaves 'pdf'
  2917. if not base_name or base_name == ext.strip('.'):
  2918. filename = f"upload_{uuid.uuid4().hex[:8]}{ext}"
  2919. else:
  2920. # Ensure the extension is preserved
  2921. if not base_name.lower().endswith(ext):
  2922. filename = f"{base_name}{ext}"
  2923. else:
  2924. filename = base_name
  2925. file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
  2926. file.save(file_path)
  2927. # Fetch individual page number if it exists
  2928. file_page = request.form.get(f'page_number_{i}')
  2929. saved_files.append((filename, file_path, file_page, original_filename))
  2930. if saved_files:
  2931. threading.Thread(
  2932. target=process_files_background,
  2933. args=(app.config['UPLOAD_FOLDER'], saved_files, manual_page, suggested_page, genealogy_version, genealogy_source, upload_person)
  2934. ).start()
  2935. flash('上传完成,AI解析中,稍后查看')
  2936. time.sleep(1.5)
  2937. return redirect(url_for('index'))
  2938. return render_template('upload.html', suggested_page=suggested_page)
  2939. @app.route('/manager/save_upload', methods=['POST'])
  2940. def save_upload():
  2941. if 'user_id' not in session: return redirect(url_for('login'))
  2942. filename = request.form.get('filename')
  2943. oss_url = request.form.get('oss_url')
  2944. page_number = request.form.get('page_number')
  2945. genealogy_version = request.form.get('genealogy_version', '')
  2946. genealogy_source = request.form.get('genealogy_source', '')
  2947. upload_person = request.form.get('upload_person', session.get('username', ''))
  2948. file_type = request.form.get('file_type', '图片')
  2949. if not oss_url or not page_number:
  2950. flash('页码不能为空')
  2951. return redirect(url_for('upload'))
  2952. conn = get_db_connection()
  2953. try:
  2954. with conn.cursor() as cursor:
  2955. sql = """INSERT INTO genealogy_records
  2956. (file_name, oss_url, page_number, ai_status, genealogy_version, genealogy_source, upload_person, file_type)
  2957. VALUES (%s, %s, %s, 1, %s, %s, %s, %s)"""
  2958. cursor.execute(sql, (filename, oss_url, page_number, genealogy_version, genealogy_source, upload_person, file_type))
  2959. record_id = cursor.lastrowid
  2960. conn.commit()
  2961. # Start AI Task
  2962. threading.Thread(target=process_ai_task, args=(record_id, oss_url)).start()
  2963. flash('上传完成,AI解析中,稍后查看')
  2964. except Exception as e:
  2965. flash(f'保存失败: {e}')
  2966. finally:
  2967. conn.close()
  2968. return redirect(url_for('index'))
  2969. @app.route('/manager/delete_upload/<int:record_id>', methods=['POST'])
  2970. def delete_upload(record_id):
  2971. if 'user_id' not in session:
  2972. return jsonify({"success": False, "message": "Unauthorized"}), 401
  2973. conn = get_db_connection()
  2974. try:
  2975. with conn.cursor() as cursor:
  2976. # 删除记录
  2977. cursor.execute("DELETE FROM genealogy_records WHERE id = %s", (record_id,))
  2978. conn.commit()
  2979. flash('文件记录已成功删除')
  2980. return redirect(url_for('index'))
  2981. except Exception as e:
  2982. conn.rollback()
  2983. flash(f'删除失败: {e}')
  2984. return redirect(url_for('index'))
  2985. finally:
  2986. conn.close()
  2987. @app.route('/manager/upload_pdf', methods=['GET', 'POST'])
  2988. def upload_pdf():
  2989. if 'user_id' not in session:
  2990. return redirect(url_for('login'))
  2991. if request.method == 'GET':
  2992. return render_template('upload_pdf.html')
  2993. # POST请求处理
  2994. if 'file' not in request.files:
  2995. flash('请选择要上传的PDF文件')
  2996. return redirect(request.url)
  2997. file = request.files['file']
  2998. if file.filename == '':
  2999. flash('请选择要上传的PDF文件')
  3000. return redirect(request.url)
  3001. # 检查文件类型
  3002. if not file.filename.lower().endswith('.pdf'):
  3003. flash('只支持PDF文件上传')
  3004. return redirect(request.url)
  3005. # 获取表单数据
  3006. version_name = request.form.get('version_name', '').strip()
  3007. version_source = request.form.get('version_source', '').strip()
  3008. file_provider = request.form.get('file_provider', '').strip()
  3009. # 验证必填字段
  3010. if not version_name:
  3011. flash('版本名称为必填项')
  3012. return redirect(request.url)
  3013. if not version_source:
  3014. flash('版本来源为必填项')
  3015. return redirect(request.url)
  3016. # 如果未提供文件提供人,使用当前登录用户
  3017. if not file_provider:
  3018. file_provider = session.get('user_id', '未知')
  3019. import uuid
  3020. original_filename = file.filename
  3021. ext = os.path.splitext(original_filename)[1].lower()
  3022. base_name = secure_filename(original_filename)
  3023. if not base_name or base_name == ext.strip('.'):
  3024. filename = f"genealogy_pdf_{uuid.uuid4().hex[:8]}{ext}"
  3025. else:
  3026. if not base_name.lower().endswith(ext):
  3027. filename = f"{base_name}{ext}"
  3028. else:
  3029. filename = base_name
  3030. file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
  3031. file.save(file_path)
  3032. try:
  3033. # Upload to OSS
  3034. oss_url = upload_to_oss(file_path, custom_filename=filename)
  3035. if not oss_url:
  3036. flash('文件上传失败')
  3037. return redirect(request.url)
  3038. # Save to database
  3039. conn = get_db_connection()
  3040. try:
  3041. with conn.cursor() as cursor:
  3042. cursor.execute(
  3043. "INSERT INTO genealogy_pdfs (file_name, oss_url, version_name, version_source, file_provider, upload_time) VALUES (%s, %s, %s, %s, %s, CURRENT_TIMESTAMP)",
  3044. (original_filename, oss_url, version_name, version_source, file_provider)
  3045. )
  3046. conn.commit()
  3047. flash('PDF文件上传成功')
  3048. return redirect(url_for('pdf_management'))
  3049. except Exception as e:
  3050. flash(f'保存失败: {e}')
  3051. return redirect(request.url)
  3052. finally:
  3053. conn.close()
  3054. finally:
  3055. if os.path.exists(file_path):
  3056. try:
  3057. os.remove(file_path)
  3058. except:
  3059. pass
  3060. def process_pdf_pages(file_path, pdf_oss_url, uploader):
  3061. """Process PDF pages and add them to genealogy records"""
  3062. try:
  3063. import fitz
  3064. doc = fitz.open(file_path)
  3065. # Get current max page number
  3066. conn = get_db_connection()
  3067. suggested_page = 1
  3068. try:
  3069. with conn.cursor() as cursor:
  3070. cursor.execute("SELECT MAX(page_number) as max_p FROM genealogy_records")
  3071. result = cursor.fetchone()
  3072. if result and result['max_p']:
  3073. suggested_page = result['max_p'] + 1
  3074. finally:
  3075. conn.close()
  3076. for page_index in range(len(doc)):
  3077. try:
  3078. page = doc[page_index]
  3079. pix = page.get_pixmap(dpi=150)
  3080. # Save as image
  3081. img_filename = f"{os.path.splitext(os.path.basename(file_path))[0]}_page_{page_index+1}.jpg"
  3082. img_path = os.path.join(app.config['UPLOAD_FOLDER'], img_filename)
  3083. pix.save(img_path)
  3084. # Upload to OSS
  3085. img_oss_url = upload_to_oss(img_path, custom_filename=img_filename)
  3086. if img_oss_url:
  3087. # Save to genealogy_records
  3088. conn = get_db_connection()
  3089. try:
  3090. with conn.cursor() as cursor:
  3091. cursor.execute(
  3092. "INSERT INTO genealogy_records (file_name, oss_url, page_number, ai_status, upload_person, file_type) VALUES (%s, %s, %s, 1, %s, %s)",
  3093. (img_filename, img_oss_url, suggested_page + page_index, uploader, '图片')
  3094. )
  3095. record_id = cursor.lastrowid
  3096. conn.commit()
  3097. # Start AI processing
  3098. threading.Thread(target=process_ai_task, args=(record_id, img_oss_url)).start()
  3099. finally:
  3100. conn.close()
  3101. except Exception as e:
  3102. print(f"Error processing page {page_index+1}: {e}")
  3103. finally:
  3104. if 'img_path' in locals() and os.path.exists(img_path):
  3105. try:
  3106. os.remove(img_path)
  3107. except:
  3108. pass
  3109. except Exception as e:
  3110. print(f"Error processing PDF: {e}")
  3111. # --- Settlement Routes ---
  3112. @app.route('/manager/settlements')
  3113. def settlements():
  3114. if 'user_id' not in session:
  3115. return redirect(url_for('login'))
  3116. return render_template('settlements.html')
  3117. @app.route('/manager/api/settlements', methods=['GET'])
  3118. def get_settlements():
  3119. if 'user_id' not in session:
  3120. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3121. conn = get_db_connection()
  3122. try:
  3123. with conn.cursor() as cursor:
  3124. cursor.execute("""
  3125. SELECT s.*, m.name as representative_name, m.simplified_name as representative_simplified_name
  3126. FROM family_settlements s
  3127. LEFT JOIN family_member_info m ON s.representative_id = m.id
  3128. ORDER BY s.created_at DESC
  3129. """)
  3130. settlements = cursor.fetchall()
  3131. # Convert Decimal to float/int for JSON serialization
  3132. result = []
  3133. for s in settlements:
  3134. item = dict(s)
  3135. if item.get('latitude'):
  3136. item['latitude'] = float(item['latitude'])
  3137. if item.get('longitude'):
  3138. item['longitude'] = float(item['longitude'])
  3139. if item.get('population'):
  3140. item['population'] = int(item['population'])
  3141. result.append(item)
  3142. return jsonify({"success": True, "settlements": result})
  3143. finally:
  3144. conn.close()
  3145. @app.route('/manager/api/settlements/<int:id>', methods=['GET'])
  3146. def get_settlement(id):
  3147. if 'user_id' not in session:
  3148. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3149. conn = get_db_connection()
  3150. try:
  3151. with conn.cursor() as cursor:
  3152. cursor.execute("""
  3153. SELECT s.*, m.name as representative_name, m.simplified_name as representative_simplified_name
  3154. FROM family_settlements s
  3155. LEFT JOIN family_member_info m ON s.representative_id = m.id
  3156. WHERE s.id = %s
  3157. """, (id,))
  3158. settlement = cursor.fetchone()
  3159. if settlement:
  3160. # Convert Decimal to float/int for JSON serialization
  3161. item = dict(settlement)
  3162. if item.get('latitude'):
  3163. item['latitude'] = float(item['latitude'])
  3164. if item.get('longitude'):
  3165. item['longitude'] = float(item['longitude'])
  3166. if item.get('population'):
  3167. item['population'] = int(item['population'])
  3168. return jsonify({"success": True, "settlement": item})
  3169. else:
  3170. return jsonify({"success": False, "message": "聚落不存在"})
  3171. finally:
  3172. conn.close()
  3173. @app.route('/manager/api/settlements', methods=['POST'])
  3174. def add_settlement():
  3175. if 'user_id' not in session:
  3176. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3177. if not session.get('is_super_admin'):
  3178. return jsonify({"success": False, "message": "权限不足"}), 403
  3179. data = request.get_json()
  3180. conn = get_db_connection()
  3181. try:
  3182. with conn.cursor() as cursor:
  3183. cursor.execute("""
  3184. INSERT INTO family_settlements
  3185. (name, region, latitude, longitude, population, representative_id, description, surname_type, new_surname, enthusiastic_members)
  3186. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  3187. """, (
  3188. data.get('name'),
  3189. data.get('region'),
  3190. data.get('latitude') or None,
  3191. data.get('longitude') or None,
  3192. data.get('population') or 0,
  3193. data.get('representative_id') or None,
  3194. data.get('description'),
  3195. data.get('surname_type') or 0,
  3196. data.get('new_surname') or None,
  3197. data.get('enthusiastic_members') or None
  3198. ))
  3199. conn.commit()
  3200. return jsonify({"success": True, "message": "添加成功"})
  3201. finally:
  3202. conn.close()
  3203. @app.route('/manager/api/settlements/<int:id>', methods=['PUT'])
  3204. def update_settlement(id):
  3205. if 'user_id' not in session:
  3206. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3207. if not session.get('is_super_admin'):
  3208. return jsonify({"success": False, "message": "权限不足"}), 403
  3209. data = request.get_json()
  3210. conn = get_db_connection()
  3211. try:
  3212. with conn.cursor() as cursor:
  3213. cursor.execute("""
  3214. UPDATE family_settlements
  3215. SET name=%s, region=%s, latitude=%s, longitude=%s,
  3216. population=%s, representative_id=%s, description=%s,
  3217. surname_type=%s, new_surname=%s, enthusiastic_members=%s
  3218. WHERE id=%s
  3219. """, (
  3220. data.get('name'),
  3221. data.get('region'),
  3222. data.get('latitude') or None,
  3223. data.get('longitude') or None,
  3224. data.get('population') or 0,
  3225. data.get('representative_id') or None,
  3226. data.get('description'),
  3227. data.get('surname_type') or 0,
  3228. data.get('new_surname') or None,
  3229. data.get('enthusiastic_members') or None,
  3230. id
  3231. ))
  3232. conn.commit()
  3233. return jsonify({"success": True, "message": "更新成功"})
  3234. finally:
  3235. conn.close()
  3236. @app.route('/manager/api/settlements/<int:id>', methods=['DELETE'])
  3237. def delete_settlement(id):
  3238. if 'user_id' not in session:
  3239. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3240. if not session.get('is_super_admin'):
  3241. return jsonify({"success": False, "message": "权限不足"}), 403
  3242. conn = get_db_connection()
  3243. try:
  3244. with conn.cursor() as cursor:
  3245. cursor.execute("DELETE FROM family_settlements WHERE id=%s", (id,))
  3246. conn.commit()
  3247. return jsonify({"success": True, "message": "删除成功"})
  3248. finally:
  3249. conn.close()
  3250. # 异步批量处理族谱原文功能
  3251. import uuid
  3252. def init_batch_task_table():
  3253. """初始化批量任务表(如果不存在)"""
  3254. conn = get_db_connection()
  3255. try:
  3256. with conn.cursor() as cursor:
  3257. cursor.execute("""
  3258. CREATE TABLE IF NOT EXISTS batch_genealogy_task (
  3259. id INT AUTO_INCREMENT PRIMARY KEY,
  3260. task_id VARCHAR(64) UNIQUE NOT NULL,
  3261. user_id INT NOT NULL,
  3262. status VARCHAR(20) DEFAULT 'pending',
  3263. total_count INT DEFAULT 0,
  3264. completed_count INT DEFAULT 0,
  3265. failed_count INT DEFAULT 0,
  3266. last_processed_id INT DEFAULT 0,
  3267. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  3268. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  3269. results TEXT
  3270. );
  3271. """)
  3272. # 检查是否存在last_processed_id字段,如果不存在则添加
  3273. cursor.execute("SHOW COLUMNS FROM batch_genealogy_task LIKE 'last_processed_id'")
  3274. if not cursor.fetchone():
  3275. cursor.execute("ALTER TABLE batch_genealogy_task ADD COLUMN last_processed_id INT DEFAULT 0")
  3276. conn.commit()
  3277. print("[Database] batch_genealogy_task table initialized")
  3278. except Exception as e:
  3279. print(f"[Database] Error creating batch_genealogy_task table: {e}")
  3280. finally:
  3281. conn.close()
  3282. # 初始化表
  3283. init_batch_task_table()
  3284. def migrate_child_order_column():
  3285. """为 family_relation_info 表添加 child_order 字段(如不存在)"""
  3286. conn = get_db_connection()
  3287. try:
  3288. with conn.cursor() as cursor:
  3289. cursor.execute("SHOW COLUMNS FROM family_relation_info LIKE 'child_order'")
  3290. if not cursor.fetchone():
  3291. cursor.execute(
  3292. "ALTER TABLE family_relation_info ADD COLUMN child_order INT DEFAULT NULL COMMENT '第几子,用于兄弟排序'"
  3293. )
  3294. conn.commit()
  3295. print("[DB Migrate] Added child_order column to family_relation_info")
  3296. else:
  3297. print("[DB Migrate] child_order column already exists")
  3298. except Exception as e:
  3299. print(f"[DB Migrate] Error adding child_order: {e}")
  3300. finally:
  3301. conn.close()
  3302. migrate_child_order_column()
  3303. def migrate_enthusiastic_members_column():
  3304. """为 family_settlements 表添加 enthusiastic_members 字段(如不存在)"""
  3305. conn = get_db_connection()
  3306. try:
  3307. with conn.cursor() as cursor:
  3308. cursor.execute("SHOW COLUMNS FROM family_settlements LIKE 'enthusiastic_members'")
  3309. if not cursor.fetchone():
  3310. cursor.execute(
  3311. "ALTER TABLE family_settlements ADD COLUMN enthusiastic_members TEXT DEFAULT NULL COMMENT '热心宗亲,多人以逗号分隔'"
  3312. )
  3313. conn.commit()
  3314. print("[DB Migrate] Added enthusiastic_members column to family_settlements")
  3315. else:
  3316. print("[DB Migrate] enthusiastic_members column already exists")
  3317. except Exception as e:
  3318. print(f"[DB Migrate] Error adding enthusiastic_members: {e}")
  3319. finally:
  3320. conn.close()
  3321. migrate_enthusiastic_members_column()
  3322. def async_process_genealogy_task(task_id, member_ids, user_id):
  3323. """异步处理族谱原文任务"""
  3324. results = []
  3325. conn = get_db_connection()
  3326. try:
  3327. # 更新任务状态为处理中
  3328. with conn.cursor() as cursor:
  3329. cursor.execute("""
  3330. UPDATE batch_genealogy_task
  3331. SET status = 'processing', total_count = %s
  3332. WHERE task_id = %s
  3333. """, (len(member_ids), task_id))
  3334. conn.commit()
  3335. completed_count = 0
  3336. failed_count = 0
  3337. for member_id in member_ids:
  3338. try:
  3339. with conn.cursor() as cursor:
  3340. cursor.execute("""
  3341. SELECT id, name, simplified_name, name_word_generation,
  3342. birth_place, occupation, notes, sex
  3343. FROM family_member_info WHERE id = %s
  3344. """, (member_id,))
  3345. member = cursor.fetchone()
  3346. # 获取父亲信息
  3347. cursor.execute("""
  3348. SELECT p.name, p.simplified_name
  3349. FROM family_relation_info r
  3350. JOIN family_member_info p ON r.parent_mid = p.id
  3351. WHERE r.child_mid = %s AND r.relation_type = 1
  3352. LIMIT 1
  3353. """, (member_id,))
  3354. father = cursor.fetchone()
  3355. # 获取母亲信息
  3356. cursor.execute("""
  3357. SELECT p.name, p.simplified_name
  3358. FROM family_relation_info r
  3359. JOIN family_member_info p ON r.parent_mid = p.id
  3360. WHERE r.child_mid = %s AND r.relation_type = 2
  3361. LIMIT 1
  3362. """, (member_id,))
  3363. mother = cursor.fetchone()
  3364. member['father_name'] = father['name'] if father else None
  3365. member['father_simplified_name'] = father['simplified_name'] if father else None
  3366. member['mother_name'] = mother['name'] if mother else None
  3367. member['mother_simplified_name'] = mother['simplified_name'] if mother else None
  3368. except Exception as e:
  3369. print(f"[Async Process] Error getting member {member_id}: {e}")
  3370. results.append({
  3371. "member_id": member_id,
  3372. "name": "未知",
  3373. "success": False,
  3374. "message": f"获取成员信息失败: {e}"
  3375. })
  3376. failed_count += 1
  3377. continue
  3378. if not member:
  3379. results.append({
  3380. "member_id": member_id,
  3381. "name": "未知",
  3382. "success": False,
  3383. "message": "成员不存在"
  3384. })
  3385. failed_count += 1
  3386. continue
  3387. # 构建AI提示词
  3388. member_info = f"""
  3389. 姓名(繁体):{member['name']}
  3390. 姓名(简体):{member['simplified_name'] or '未知'}
  3391. 世系世代:{member['name_word_generation'] or '未知'}
  3392. 父亲姓名:{member['father_name'] or '未知'}
  3393. 母亲姓名:{member['mother_name'] or '未知'}
  3394. 出生地:{member['birth_place'] or '未知'}
  3395. 职业:{member['occupation'] or '未知'}
  3396. 备注:{member['notes'] or '无'}
  3397. """
  3398. prompt = f"""
  3399. 请根据以下人员信息,模拟生成该人员的族谱原文:
  3400. {member_info}
  3401. 请输出两个字段:
  3402. 1. genealogy_traditional: 族谱原文(繁体中文,模仿传统族谱格式)
  3403. 2. genealogy_simplified: 族谱原文(简体中文,将繁体转换为简体)
  3404. 请严格按照JSON格式输出,不要包含任何额外解释:
  3405. {{
  3406. "genealogy_traditional": "繁体族谱原文内容",
  3407. "genealogy_simplified": "简体族谱原文内容"
  3408. }}
  3409. """
  3410. ai_response = call_doubao_api(prompt)
  3411. if ai_response:
  3412. traditional, simplified = parse_ai_response(ai_response)
  3413. if traditional or simplified:
  3414. try:
  3415. with conn.cursor() as cursor:
  3416. cursor.execute("""
  3417. UPDATE family_member_info
  3418. SET genealogy_original_traditional = %s,
  3419. genealogy_original_simplified = %s
  3420. WHERE id = %s
  3421. """, (traditional, simplified, member_id))
  3422. conn.commit()
  3423. results.append({
  3424. "member_id": member_id,
  3425. "name": member['name'],
  3426. "success": True,
  3427. "traditional": traditional[:100] + "..." if len(traditional) > 100 else traditional,
  3428. "simplified": simplified[:100] + "..." if len(simplified) > 100 else simplified
  3429. })
  3430. completed_count += 1
  3431. except Exception as e:
  3432. print(f"[Async Process] Error updating member {member_id}: {e}")
  3433. results.append({
  3434. "member_id": member_id,
  3435. "name": member['name'],
  3436. "success": False,
  3437. "message": f"保存失败: {e}"
  3438. })
  3439. failed_count += 1
  3440. else:
  3441. results.append({
  3442. "member_id": member_id,
  3443. "name": member['name'],
  3444. "success": False,
  3445. "message": "AI未返回有效数据"
  3446. })
  3447. failed_count += 1
  3448. else:
  3449. results.append({
  3450. "member_id": member_id,
  3451. "name": member['name'],
  3452. "success": False,
  3453. "message": "AI调用失败"
  3454. })
  3455. failed_count += 1
  3456. # 更新任务状态
  3457. status = 'completed' if failed_count == 0 else 'completed_with_errors'
  3458. with conn.cursor() as cursor:
  3459. cursor.execute("""
  3460. UPDATE batch_genealogy_task
  3461. SET status = %s, completed_count = %s, failed_count = %s, results = %s
  3462. WHERE task_id = %s
  3463. """, (status, completed_count, failed_count, json.dumps(results, ensure_ascii=False), task_id))
  3464. conn.commit()
  3465. print(f"[Async Process] Task {task_id} completed: {completed_count} success, {failed_count} failed")
  3466. except Exception as e:
  3467. print(f"[Async Process] Error in task {task_id}: {e}")
  3468. with conn.cursor() as cursor:
  3469. cursor.execute("""
  3470. UPDATE batch_genealogy_task
  3471. SET status = 'failed', results = %s
  3472. WHERE task_id = %s
  3473. """, (json.dumps({"error": str(e)}, ensure_ascii=False), task_id))
  3474. conn.commit()
  3475. finally:
  3476. conn.close()
  3477. @app.route('/manager/api/members/batch_process_genealogy_async', methods=['POST'])
  3478. def batch_process_genealogy_async():
  3479. """异步批量处理族谱原文"""
  3480. if 'user_id' not in session:
  3481. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3482. data = request.get_json()
  3483. member_ids = data.get('member_ids', [])
  3484. if not member_ids:
  3485. return jsonify({"success": False, "message": "请选择成员进行处理"}), 400
  3486. # 生成任务ID
  3487. task_id = str(uuid.uuid4())
  3488. # 保存任务到数据库
  3489. conn = get_db_connection()
  3490. try:
  3491. with conn.cursor() as cursor:
  3492. cursor.execute("""
  3493. INSERT INTO batch_genealogy_task (task_id, user_id, status, total_count)
  3494. VALUES (%s, %s, 'pending', %s)
  3495. """, (task_id, session['user_id'], len(member_ids)))
  3496. conn.commit()
  3497. finally:
  3498. conn.close()
  3499. # 启动异步线程处理
  3500. threading.Thread(target=async_process_genealogy_task, args=(task_id, member_ids, session['user_id'])).start()
  3501. return jsonify({
  3502. "success": True,
  3503. "task_id": task_id,
  3504. "message": "任务已创建,正在后台处理中"
  3505. })
  3506. @app.route('/manager/api/members/batch_task_status/<task_id>', methods=['GET'])
  3507. def get_batch_task_status(task_id):
  3508. """获取批量任务状态"""
  3509. if 'user_id' not in session:
  3510. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3511. conn = get_db_connection()
  3512. try:
  3513. with conn.cursor() as cursor:
  3514. cursor.execute("""
  3515. SELECT task_id, status, total_count, completed_count, failed_count,
  3516. created_at, updated_at, results
  3517. FROM batch_genealogy_task
  3518. WHERE task_id = %s AND user_id = %s
  3519. """, (task_id, session['user_id']))
  3520. task = cursor.fetchone()
  3521. if task:
  3522. result = {
  3523. "task_id": task['task_id'],
  3524. "status": task['status'],
  3525. "total_count": task['total_count'],
  3526. "completed_count": task['completed_count'],
  3527. "failed_count": task['failed_count'],
  3528. "created_at": task['created_at'].isoformat() if task['created_at'] else None,
  3529. "updated_at": task['updated_at'].isoformat() if task['updated_at'] else None
  3530. }
  3531. if task['results']:
  3532. try:
  3533. result['results'] = json.loads(task['results'])
  3534. except:
  3535. result['results'] = task['results']
  3536. return jsonify({"success": True, "task": result})
  3537. else:
  3538. return jsonify({"success": False, "message": "任务不存在或无权访问"}), 404
  3539. finally:
  3540. conn.close()
  3541. @app.route('/manager/api/members/batch_tasks', methods=['GET'])
  3542. def get_batch_tasks():
  3543. """获取用户的批量任务列表"""
  3544. if 'user_id' not in session:
  3545. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3546. conn = get_db_connection()
  3547. try:
  3548. with conn.cursor() as cursor:
  3549. cursor.execute("""
  3550. SELECT task_id, status, total_count, completed_count, failed_count,
  3551. last_processed_id, created_at, updated_at
  3552. FROM batch_genealogy_task
  3553. WHERE user_id = %s
  3554. ORDER BY created_at DESC
  3555. LIMIT 20
  3556. """, (session['user_id'],))
  3557. tasks = cursor.fetchall()
  3558. result = []
  3559. for task in tasks:
  3560. result.append({
  3561. "task_id": task['task_id'],
  3562. "status": task['status'],
  3563. "total_count": task['total_count'],
  3564. "completed_count": task['completed_count'],
  3565. "failed_count": task['failed_count'],
  3566. "last_processed_id": task['last_processed_id'],
  3567. "created_at": task['created_at'].isoformat() if task['created_at'] else None,
  3568. "updated_at": task['updated_at'].isoformat() if task['updated_at'] else None
  3569. })
  3570. return jsonify({"success": True, "tasks": result})
  3571. finally:
  3572. conn.close()
  3573. def call_doubao_image_api(image_url, prompt):
  3574. """调用豆包API处理图片,提取文本内容"""
  3575. api_key = "a1800657-9212-4afe-9b7c-b49f015c54d3"
  3576. api_url = "https://ark.cn-beijing.volces.com/api/v3/responses"
  3577. ai_payload_url = get_normalized_base64_image(image_url)
  3578. payload = {
  3579. "model": "doubao-seed-1-8-251228",
  3580. "stream": False,
  3581. "input": [
  3582. {
  3583. "role": "user",
  3584. "content": [
  3585. {"type": "input_image", "image_url": ai_payload_url},
  3586. {"type": "input_text", "text": prompt}
  3587. ]
  3588. }
  3589. ]
  3590. }
  3591. headers = {
  3592. "Authorization": f"Bearer {api_key}",
  3593. "Content-Type": "application/json"
  3594. }
  3595. try:
  3596. response = requests.post(
  3597. api_url,
  3598. json=payload,
  3599. headers=headers,
  3600. timeout=120,
  3601. verify=False,
  3602. proxies={"http": None, "https": None}
  3603. )
  3604. if response.status_code == 200:
  3605. return response.json()
  3606. else:
  3607. print(f"[Image AI API] Error: {response.status_code} - {response.text}")
  3608. return None
  3609. except Exception as e:
  3610. print(f"[Image AI API] Exception: {e}")
  3611. return None
  3612. def extract_pure_text(response):
  3613. """从API响应中提取纯文本内容,优先返回 message 类型的最终答案"""
  3614. if not response:
  3615. return ''
  3616. # 优先从 output 列表中提取 message 类型(最终答案)
  3617. if 'output' in response:
  3618. # 第一遍:只找 message 类型
  3619. for item in response['output']:
  3620. if item.get('type') == 'message':
  3621. content = item.get('content')
  3622. if isinstance(content, str):
  3623. return content
  3624. elif isinstance(content, list):
  3625. text_parts = []
  3626. for part in content:
  3627. if isinstance(part, dict) and part.get('type') == 'text':
  3628. text_parts.append(part.get('text', ''))
  3629. elif isinstance(part, str):
  3630. text_parts.append(part)
  3631. result = ''.join(text_parts)
  3632. if result:
  3633. return result
  3634. # 第二遍:没有 message 时才使用 reasoning 内容作为兜底
  3635. for item in response['output']:
  3636. if item.get('type') == 'reasoning':
  3637. content = item.get('content')
  3638. all_text = ''
  3639. summary = item.get('summary', [])
  3640. for part in summary:
  3641. if isinstance(part, dict):
  3642. if part.get('type') in ('summary_text', 'text'):
  3643. all_text += part.get('text', '')
  3644. elif isinstance(part, str):
  3645. all_text += part
  3646. if isinstance(content, str):
  3647. all_text += content
  3648. elif isinstance(content, list):
  3649. for part in content:
  3650. if isinstance(part, dict) and part.get('type') == 'text':
  3651. all_text += part.get('text', '')
  3652. elif isinstance(part, str):
  3653. all_text += part
  3654. if all_text:
  3655. return all_text
  3656. # 第三遍:content 直接是字符串的情况
  3657. for item in response['output']:
  3658. content = item.get('content')
  3659. if isinstance(content, str) and content:
  3660. return content
  3661. # 尝试从 choices 中提取(兼容 OpenAI 格式)
  3662. if 'choices' in response and len(response['choices']) > 0:
  3663. message = response['choices'][0].get('message', {})
  3664. return message.get('content', '')
  3665. return str(response)
  3666. def build_genealogy_prompt(member_name):
  3667. """
  3668. 构建用于竖排繁体家谱图片 OCR 提取的 Prompt。
  3669. 家谱图片为竖排版式(从上到下、从右到左),每位人物记录通常包含:
  3670. 辈字+名讳、字号、行次、父子关系、配偶(配某氏)、生卒年、葬地、子嗣等。
  3671. """
  3672. return f"""这是一张竖排繁体中文家谱图片。图片文字采用竖排格式,从上到下、从右到左逐列阅读。
  3673. 每位人物的记录通常包含以下内容(不一定全有):
  3674. - 辈字加名讳(如:公諱光元)
  3675. - 字号(如:字維亮)
  3676. - 行次(如:行仁一)
  3677. - 与父亲的关系(如:某某公長子、次子、三子)
  3678. - 配偶(如:配李氏、娶王氏)
  3679. - 生卒年月(如:生於某年某月、卒於某年某月)
  3680. - 葬地(如:葬祖山某向、塟於某地)
  3681. - 子嗣(如:子二:長某某、次某某)
  3682. 任务:找到人物「{member_name}」在图片中的完整记录,将其繁体原文逐字准确复制输出。
  3683. 要求:
  3684. 1. 只输出「{member_name}」这一个人物的记录,不包含其他人的内容
  3685. 2. 保持繁体字原貌,不要转换为简体
  3686. 3. 保留原文中的标点符号
  3687. 4. 不要添加任何解释、标注、序号或额外说明
  3688. 5. 直接输出原文内容"""
  3689. def _extract_from_thinking_output(text):
  3690. """
  3691. 从推理模型的思维链输出中提取最终答案。
  3692. 推理模型(如 doubao-seed 系列)会在 message 内容里写出完整思考过程:
  3693. 反复写候选答案、说"不对"再修正,最后以"现在确认/所以输出这个内容"等结论收尾。
  3694. 本函数的策略:
  3695. 1. 找最后一个"答案引导词 + 冒号"之后的文本(如"准确的原文是:"、"准确复制:")
  3696. 2. 若无引导词,则取"现在确认"/"所以输出这个内容"之前的最后一段文本
  3697. 3. 以上均失败则原文返回
  3698. """
  3699. # 思维链特征词
  3700. THINKING_SIGNALS = ['不对,', '现在确认', '准确复制', '准确的原文是', '正确的输出是', '所以输出这个内容']
  3701. if not any(sig in text for sig in THINKING_SIGNALS):
  3702. return text # 非思维链输出,原样返回
  3703. print(f"[CleanText] Detected thinking-model output, extracting final answer")
  3704. # ---- 策略1:找最后一个答案引导词 ----
  3705. ANSWER_INTRO_PATTERNS = [
  3706. r'准确的原文是[::]\s*',
  3707. r'正确的输出是[::]\s*',
  3708. r'现在准确复制[::]\s*',
  3709. r'准确复制[::]\s*',
  3710. r'应该是[::]\s*',
  3711. r'因此输出[::]\s*',
  3712. r'所以正确.*?是[::]\s*',
  3713. r'原文是[::]\s*',
  3714. r'输出[::]\s*',
  3715. ]
  3716. last_end = -1
  3717. for pattern in ANSWER_INTRO_PATTERNS:
  3718. for m in re.finditer(pattern, text):
  3719. if m.end() > last_end:
  3720. last_end = m.end()
  3721. if last_end >= 0:
  3722. remaining = text[last_end:]
  3723. # 取到第一个"结束标志"前
  3724. END_MARKERS = ['不对', '现在确认', '但是', '然而', '\n\n']
  3725. end_pos = len(remaining)
  3726. for marker in END_MARKERS:
  3727. idx = remaining.find(marker)
  3728. if 0 < idx < end_pos:
  3729. end_pos = idx
  3730. candidate = remaining[:end_pos].strip()
  3731. if len(candidate) >= 5:
  3732. print(f"[CleanText] Extracted via answer-intro pattern: '{candidate[:80]}'")
  3733. return candidate
  3734. # ---- 策略2:取"现在确认"之前的最后一段 ----
  3735. for end_phrase in ['现在确认', '所以输出这个内容', '这就是.*?的完整记录']:
  3736. m = re.search(end_phrase, text)
  3737. if m:
  3738. before = text[:m.start()].rstrip()
  3739. # 找最后一个换行符,取之后的内容
  3740. last_nl = before.rfind('\n')
  3741. candidate = (before[last_nl + 1:] if last_nl >= 0 else before[-400:]).strip()
  3742. if len(candidate) >= 5:
  3743. print(f"[CleanText] Extracted before confirmation phrase: '{candidate[:80]}'")
  3744. return candidate
  3745. return text # 均失败则原样返回
  3746. def _apply_char_whitelist(text):
  3747. """只保留汉字(含扩展A区)和常见中文标点"""
  3748. return re.sub(
  3749. r'[^\u4e00-\u9fff\u3400-\u4dbf\u3000-\u303f\uff00-\uffef,。;:、()【】「」『』〔〕·~—…《》]',
  3750. '', text
  3751. ).strip()
  3752. def clean_genealogy_text(text):
  3753. """
  3754. 清理从 AI 响应中提取的族谱文本。
  3755. - 处理 Markdown/JSON 格式噪声
  3756. - 自动识别思维链推理模型输出,提取最终答案段落
  3757. - 保留中文字符和中文标点,去除英文说明行
  3758. """
  3759. if not text:
  3760. return ''
  3761. text = text.strip()
  3762. # 去除代码块标记
  3763. text = re.sub(r'^```[a-z]*\n?', '', text)
  3764. text = re.sub(r'\n?```$', '', text)
  3765. text = text.strip()
  3766. # 尝试解析 JSON,从已知字段提取
  3767. try:
  3768. result = json.loads(text)
  3769. if isinstance(result, dict):
  3770. for key in ['text', 'content', 'result', 'traditional', 'genealogy_traditional']:
  3771. if key in result:
  3772. text = str(result[key])
  3773. break
  3774. except (json.JSONDecodeError, ValueError):
  3775. pass
  3776. # 针对思维链推理模型输出,提取最终答案(必须在行过滤之前,因为推理文本中含有必要的换行结构)
  3777. text = _extract_from_thinking_output(text)
  3778. # 按行过滤:去除纯英文/数字行、空行及明显解释性前缀行
  3779. lines = text.splitlines()
  3780. kept_lines = []
  3781. for line in lines:
  3782. line = line.strip()
  3783. if not line:
  3784. continue
  3785. non_ascii = sum(1 for c in line if ord(c) > 127)
  3786. if non_ascii == 0:
  3787. continue
  3788. if re.match(r'^(注[::]|说明[::]|Note[::]|备注[::])', line):
  3789. continue
  3790. kept_lines.append(line)
  3791. text = ''.join(kept_lines)
  3792. # 字符白名单:只保留汉字和中文标点
  3793. text = _apply_char_whitelist(text)
  3794. return text
  3795. def async_process_all_empty_genealogy(task_id, user_id):
  3796. """
  3797. 异步批量处理族谱原文为空的成员,支持断点续跑。
  3798. 连接管理原则:DB 连接仅在快速读写期间持有,AI 调用(最长120s)期间
  3799. 不占用任何 DB 连接,避免影响其他用户的正常操作。
  3800. """
  3801. import time
  3802. # ── 1. 读取断点位置,立即释放连接 ──────────────────────────────────────
  3803. conn = get_db_connection()
  3804. try:
  3805. with conn.cursor() as cursor:
  3806. cursor.execute(
  3807. "SELECT last_processed_id FROM batch_genealogy_task WHERE task_id = %s",
  3808. (task_id,)
  3809. )
  3810. task = cursor.fetchone()
  3811. last_processed_id = task['last_processed_id'] if task else 0
  3812. finally:
  3813. conn.close()
  3814. completed_count = 0
  3815. failed_count = 0
  3816. results = []
  3817. while True:
  3818. # ── 2. 取下一条待处理成员(短暂占用连接后立即释放)────────────────
  3819. conn = get_db_connection()
  3820. try:
  3821. with conn.cursor() as cursor:
  3822. cursor.execute("""
  3823. SELECT m.id, m.name, m.name_word_generation, m.source_record_id,
  3824. r.oss_url AS image_url, r.ai_content AS record_ai_content
  3825. FROM family_member_info m
  3826. LEFT JOIN genealogy_records r ON m.source_record_id = r.id
  3827. WHERE (m.genealogy_original_traditional IS NULL
  3828. OR m.genealogy_original_traditional = ''
  3829. OR m.genealogy_original_traditional = 'None')
  3830. AND (m.genealogy_original_simplified IS NULL
  3831. OR m.genealogy_original_simplified = ''
  3832. OR m.genealogy_original_simplified = 'None')
  3833. AND m.id > %s
  3834. ORDER BY m.id ASC
  3835. LIMIT 1
  3836. """, (last_processed_id,))
  3837. member = cursor.fetchone()
  3838. finally:
  3839. conn.close()
  3840. if not member:
  3841. break
  3842. member_id = member['id']
  3843. member_name = member['name']
  3844. image_url = member['image_url']
  3845. record_ai_content = member['record_ai_content']
  3846. print(f"[Batch Process] Processing member {member_id}: {member_name}")
  3847. traditional = ""
  3848. simplified = ""
  3849. extract_source = "basic_info"
  3850. try:
  3851. # ── 3. AI 提取(此阶段不持有任何 DB 连接)────────────────────
  3852. if image_url:
  3853. print(f"[Batch Process] Extracting from image: {image_url}")
  3854. prompt = build_genealogy_prompt(member_name)
  3855. ai_response = call_doubao_image_api(image_url, prompt)
  3856. print(f"[Batch Process] AI response for {member_id}: {str(ai_response)[:300]}")
  3857. if ai_response:
  3858. raw_text = extract_pure_text(ai_response)
  3859. traditional = clean_genealogy_text(raw_text)
  3860. print(f"[Batch Process] Cleaned traditional: {traditional[:100]}")
  3861. name_chars = [c for c in member_name if '\u4e00' <= c <= '\u9fff']
  3862. name_found = any(c in traditional for c in name_chars)
  3863. if traditional and len(traditional) >= 5 and name_found:
  3864. simplified = convert_to_simplified(traditional)
  3865. extract_source = "image"
  3866. print(f"[Batch Process] Image extract OK - trad: {traditional[:80]}")
  3867. else:
  3868. traditional = ""
  3869. simplified = ""
  3870. print(f"[Batch Process] Image extract invalid "
  3871. f"(name_found={name_found}, len={len(traditional)}), resetting")
  3872. # ── 4. 回退:从 record AI content 拼装(内存操作,无需 DB)──
  3873. if not (traditional and simplified) and record_ai_content:
  3874. print(f"[Batch Process] Fallback: trying record AI content")
  3875. try:
  3876. ai_content = json.loads(record_ai_content)
  3877. if isinstance(ai_content, list):
  3878. current_person = None
  3879. for person in ai_content:
  3880. person_name = person.get('original_name', person.get('name', '')).strip()
  3881. if person_name and (
  3882. member_name in person_name or person_name in member_name
  3883. ):
  3884. current_person = person
  3885. break
  3886. if current_person:
  3887. name = current_person.get('original_name',
  3888. current_person.get('name', member_name))
  3889. father_name = current_person.get('father_name', '')
  3890. spouse_name = current_person.get('spouse_name', '')
  3891. generation = current_person.get('generation',
  3892. member['name_word_generation'])
  3893. traditional = f"{name},{father_name}之子" if father_name else name
  3894. if spouse_name:
  3895. traditional += f",配{spouse_name}"
  3896. if generation:
  3897. traditional = f"第{generation}世 " + traditional
  3898. simplified = convert_to_simplified(traditional)
  3899. extract_source = "ai_content"
  3900. print(f"[Batch Process] AI content fallback: {traditional[:80]}")
  3901. else:
  3902. print(f"[Batch Process] No matching person for '{member_name}' in AI content")
  3903. except Exception as e:
  3904. print(f"[Batch Process] Failed to parse record AI content: {e}")
  3905. # ── 5. 最终回退:从关系表查父亲和配偶,短暂占用连接后立即释放 ──
  3906. if not (traditional and simplified):
  3907. print(f"[Batch Process] Fallback: basic info from DB")
  3908. conn = get_db_connection()
  3909. try:
  3910. with conn.cursor() as cursor:
  3911. cursor.execute("""
  3912. SELECT p.name FROM family_relation_info r
  3913. JOIN family_member_info p ON r.parent_mid = p.id
  3914. WHERE r.child_mid = %s AND r.relation_type = 1 LIMIT 1
  3915. """, (member_id,))
  3916. father = cursor.fetchone()
  3917. cursor.execute("""
  3918. SELECT p.name FROM family_relation_info r
  3919. JOIN family_member_info p ON r.parent_mid = p.id
  3920. WHERE r.child_mid = %s AND r.relation_type = 2 LIMIT 1
  3921. """, (member_id,))
  3922. spouse = cursor.fetchone()
  3923. finally:
  3924. conn.close()
  3925. father_name = father['name'] if father else ''
  3926. spouse_name = spouse['name'] if spouse else ''
  3927. generation = member['name_word_generation']
  3928. traditional = f"{member_name},{father_name}之子" if father_name else member_name
  3929. if spouse_name:
  3930. traditional += f",配{spouse_name}"
  3931. if generation:
  3932. traditional = f"第{generation}世 " + traditional
  3933. simplified = convert_to_simplified(traditional)
  3934. extract_source = "basic_info"
  3935. print(f"[Batch Process] Basic info fallback: {traditional[:80]}")
  3936. except Exception as extract_err:
  3937. print(f"[Batch Process] Extraction error for member {member_id}: {extract_err}")
  3938. traditional = ""
  3939. simplified = ""
  3940. # ── 6. 保存结果(短暂占用连接后立即释放)────────────────────────
  3941. last_processed_id = member_id
  3942. conn = get_db_connection()
  3943. try:
  3944. if traditional and simplified:
  3945. with conn.cursor() as cursor:
  3946. cursor.execute("""
  3947. UPDATE family_member_info
  3948. SET genealogy_original_traditional = %s,
  3949. genealogy_original_simplified = %s
  3950. WHERE id = %s
  3951. """, (traditional, simplified, member_id))
  3952. completed_count += 1
  3953. results.append({
  3954. "member_id": member_id,
  3955. "name": member_name,
  3956. "success": True,
  3957. "source": extract_source,
  3958. "traditional_length": len(traditional),
  3959. "simplified_length": len(simplified),
  3960. })
  3961. print(f"[Batch Process] Saved member {member_id} (source={extract_source})")
  3962. else:
  3963. failed_count += 1
  3964. results.append({
  3965. "member_id": member_id,
  3966. "name": member_name,
  3967. "success": False,
  3968. "message": "无法提取或生成族谱原文",
  3969. })
  3970. print(f"[Batch Process] Skipped member {member_id}: no valid text extracted")
  3971. with conn.cursor() as cursor:
  3972. cursor.execute("""
  3973. UPDATE batch_genealogy_task
  3974. SET completed_count = %s,
  3975. failed_count = %s,
  3976. last_processed_id = %s,
  3977. status = 'processing'
  3978. WHERE task_id = %s
  3979. """, (completed_count, failed_count, last_processed_id, task_id))
  3980. conn.commit()
  3981. except Exception as db_err:
  3982. print(f"[Batch Process] DB save error for member {member_id}: {db_err}")
  3983. failed_count += 1
  3984. finally:
  3985. conn.close()
  3986. # 每条处理完后短暂暂停,降低对 AI API 和服务器资源的压力
  3987. time.sleep(0.5)
  3988. # ── 7. 任务完成,写入最终状态 ─────────────────────────────────────────
  3989. conn = get_db_connection()
  3990. try:
  3991. status = 'completed' if failed_count == 0 else 'completed_with_errors'
  3992. with conn.cursor() as cursor:
  3993. cursor.execute("""
  3994. UPDATE batch_genealogy_task
  3995. SET status = %s,
  3996. completed_count = %s,
  3997. failed_count = %s,
  3998. results = %s
  3999. WHERE task_id = %s
  4000. """, (status, completed_count, failed_count,
  4001. json.dumps(results, ensure_ascii=False), task_id))
  4002. conn.commit()
  4003. print(f"[Batch Process] Task {task_id} done: "
  4004. f"{completed_count} success, {failed_count} failed")
  4005. except Exception as e:
  4006. print(f"[Batch Process] Error writing final status for {task_id}: {e}")
  4007. finally:
  4008. conn.close()
  4009. @app.route('/manager/api/members/extract_genealogy/<int:member_id>', methods=['GET'])
  4010. def extract_single_genealogy(member_id):
  4011. """单人员提取族谱原文,核心逻辑与批量处理一致,提取后写入数据库"""
  4012. if 'user_id' not in session:
  4013. return jsonify({"success": False, "message": "Unauthorized"}), 401
  4014. conn = get_db_connection()
  4015. try:
  4016. # 查询成员信息
  4017. with conn.cursor() as cursor:
  4018. cursor.execute("""
  4019. SELECT
  4020. m.id, m.name, m.name_word_generation,
  4021. m.source_record_id, r.oss_url as image_url,
  4022. r.ai_content AS record_ai_content
  4023. FROM family_member_info m
  4024. LEFT JOIN genealogy_records r ON m.source_record_id = r.id
  4025. WHERE m.id = %s
  4026. """, (member_id,))
  4027. row = cursor.fetchone()
  4028. if not row:
  4029. return jsonify({"success": False, "message": "未找到成员"}), 404
  4030. # 处理字典或元组格式的返回
  4031. if isinstance(row, dict):
  4032. member = row
  4033. else:
  4034. member = {
  4035. 'id': row[0],
  4036. 'name': row[1],
  4037. 'name_word_generation': row[2],
  4038. 'source_record_id': row[3],
  4039. 'image_url': row[4],
  4040. 'record_ai_content': row[5]
  4041. }
  4042. # 调试:打印查询结果
  4043. print(f"[Single Extract] Query result - id: {member['id']}, name: '{member['name']}', name_word_generation: '{member['name_word_generation']}', source_record_id: {member['source_record_id']}, image_url: '{member['image_url']}', record_ai_content: '{member['record_ai_content'][:50] if member['record_ai_content'] else None}'")
  4044. traditional = ""
  4045. simplified = ""
  4046. source = "basic_info"
  4047. image_url = member['image_url']
  4048. record_ai_content = member['record_ai_content']
  4049. print(f"[Single Extract] Processing member {member_id}: {member['name']}")
  4050. # 优先从关联图片中提取族谱原文
  4051. if image_url:
  4052. print(f"[Single Extract] Extracting from image: {image_url}")
  4053. member_name = member['name']
  4054. prompt = build_genealogy_prompt(member_name)
  4055. ai_response = call_doubao_image_api(image_url, prompt)
  4056. print(f"[Single Extract] AI response: {str(ai_response)[:500]}")
  4057. if ai_response:
  4058. raw_text = extract_pure_text(ai_response)
  4059. print(f"[Single Extract] Raw text from response: '{raw_text[:300]}'")
  4060. traditional = clean_genealogy_text(raw_text)
  4061. print(f"[Single Extract] Cleaned traditional: '{traditional[:200]}', length: {len(traditional)}")
  4062. # 验证提取结果是否包含该人物的姓名(至少包含名字中的一个字)
  4063. name_chars = [c for c in member_name if '\u4e00' <= c <= '\u9fff']
  4064. name_found = any(c in traditional for c in name_chars)
  4065. if traditional and len(traditional) >= 5 and name_found:
  4066. simplified = convert_to_simplified(traditional)
  4067. source = "image"
  4068. print(f"[Single Extract] Extracted from image - traditional: {traditional[:100]}, simplified: {simplified[:100]}")
  4069. else:
  4070. traditional = ""
  4071. simplified = ""
  4072. if not name_found:
  4073. print(f"[Single Extract] Extracted text does not contain name '{member_name}', resetting")
  4074. else:
  4075. print(f"[Single Extract] Image extraction too short ({len(traditional)} chars), resetting")
  4076. else:
  4077. print(f"[Single Extract] AI response is None or empty")
  4078. else:
  4079. print(f"[Single Extract] No image URL found for member {member_id}")
  4080. # 如果从图片提取失败或没有图片,尝试从已有的AI解析内容中提取
  4081. if not (traditional and simplified) and record_ai_content:
  4082. print(f"[Single Extract] Trying to extract from record AI content")
  4083. try:
  4084. ai_content = json.loads(record_ai_content)
  4085. if isinstance(ai_content, list) and len(ai_content) > 0:
  4086. current_person = None
  4087. member_name = member['name']
  4088. for person in ai_content:
  4089. person_name = person.get('original_name', person.get('name', '')).strip()
  4090. if person_name and (member_name in person_name or person_name in member_name):
  4091. current_person = person
  4092. break
  4093. if current_person:
  4094. name = current_person.get('original_name', current_person.get('name', member['name']))
  4095. father_name = current_person.get('father_name', '')
  4096. spouse_name = current_person.get('spouse_name', '')
  4097. generation = current_person.get('generation', member['name_word_generation'])
  4098. traditional = f"{name},{father_name}之子"
  4099. if spouse_name:
  4100. traditional += f",配{spouse_name}"
  4101. if generation:
  4102. traditional = f"第{generation}世 " + traditional
  4103. simplified = convert_to_simplified(traditional)
  4104. source = "ai_content"
  4105. print(f"[Single Extract] Generated from AI content: {traditional}")
  4106. except Exception as e:
  4107. print(f"[Single Extract] Failed to parse record AI content: {e}")
  4108. # 如果还是没有内容,使用基本信息生成(标注来源为 basic_info)
  4109. if not (traditional and simplified):
  4110. print(f"[Single Extract] Generating from basic info")
  4111. with conn.cursor() as cursor:
  4112. cursor.execute("""
  4113. SELECT p.name, p.simplified_name
  4114. FROM family_relation_info r
  4115. JOIN family_member_info p ON r.parent_mid = p.id
  4116. WHERE r.child_mid = %s AND r.relation_type = 1
  4117. LIMIT 1
  4118. """, (member_id,))
  4119. father_row = cursor.fetchone()
  4120. father_name = father_row[0] if father_row else ''
  4121. cursor.execute("""
  4122. SELECT p.name, p.simplified_name
  4123. FROM family_relation_info r
  4124. JOIN family_member_info p ON r.parent_mid = p.id
  4125. WHERE r.child_mid = %s AND r.relation_type = 2
  4126. LIMIT 1
  4127. """, (member_id,))
  4128. spouse_row = cursor.fetchone()
  4129. spouse_name = spouse_row[0] if spouse_row else ''
  4130. generation = member['name_word_generation']
  4131. name = member['name']
  4132. traditional = f"{name},{father_name}之子" if father_name else name
  4133. if spouse_name:
  4134. traditional += f",配{spouse_name}"
  4135. if generation:
  4136. traditional = f"第{generation}世 " + traditional
  4137. simplified = convert_to_simplified(traditional)
  4138. source = "basic_info"
  4139. print(f"[Single Extract] Generated from basic info: {traditional}")
  4140. # 调试:打印最终结果
  4141. print(f"[Single Extract] Final result - traditional: '{traditional}', simplified: '{simplified}'")
  4142. # 写入数据库
  4143. if traditional and simplified:
  4144. with conn.cursor() as cursor:
  4145. cursor.execute("""
  4146. UPDATE family_member_info
  4147. SET genealogy_original_traditional = %s,
  4148. genealogy_original_simplified = %s
  4149. WHERE id = %s
  4150. """, (traditional, simplified, member_id))
  4151. conn.commit()
  4152. print(f"[Single Extract] Successfully saved to database")
  4153. return jsonify({
  4154. "success": True,
  4155. "member_id": member_id,
  4156. "name": member['name'],
  4157. "genealogy_traditional": traditional,
  4158. "genealogy_simplified": simplified,
  4159. "source": source
  4160. })
  4161. else:
  4162. return jsonify({
  4163. "success": False,
  4164. "member_id": member_id,
  4165. "message": "无法提取或生成族谱原文"
  4166. })
  4167. except Exception as e:
  4168. import traceback
  4169. print(f"[Single Extract] Error: {e}")
  4170. print(f"[Single Extract] Traceback: {traceback.format_exc()}")
  4171. return jsonify({
  4172. "success": False,
  4173. "member_id": member_id,
  4174. "message": str(e),
  4175. "error_type": type(e).__name__
  4176. })
  4177. finally:
  4178. conn.close()
  4179. @app.route('/manager/api/members/batch_resume_task', methods=['GET'])
  4180. def batch_resume_task():
  4181. """
  4182. 恢复因服务重启而中断的批量任务(GET,方便浏览器直接访问)。
  4183. 可选参数:?task_id=xxx 不传则自动找最近一条中断任务。
  4184. """
  4185. if 'user_id' not in session:
  4186. return jsonify({"success": False, "message": "Unauthorized"}), 401
  4187. task_id = request.args.get('task_id')
  4188. conn = get_db_connection()
  4189. try:
  4190. with conn.cursor() as cursor:
  4191. if task_id:
  4192. cursor.execute("""
  4193. SELECT task_id, status, last_processed_id, total_count, completed_count, failed_count
  4194. FROM batch_genealogy_task
  4195. WHERE task_id = %s AND user_id = %s
  4196. """, (task_id, session['user_id']))
  4197. else:
  4198. # 找最近一条中断的任务
  4199. cursor.execute("""
  4200. SELECT task_id, status, last_processed_id, total_count, completed_count, failed_count
  4201. FROM batch_genealogy_task
  4202. WHERE user_id = %s AND status IN ('pending', 'processing', 'interrupted')
  4203. ORDER BY created_at DESC
  4204. LIMIT 1
  4205. """, (session['user_id'],))
  4206. task = cursor.fetchone()
  4207. if not task:
  4208. return jsonify({"success": False, "message": "未找到可恢复的任务"}), 404
  4209. task_id = task['task_id']
  4210. # 重新标记为 processing,准备恢复线程
  4211. with conn.cursor() as cursor:
  4212. cursor.execute("""
  4213. UPDATE batch_genealogy_task
  4214. SET status = 'processing'
  4215. WHERE task_id = %s
  4216. """, (task_id,))
  4217. conn.commit()
  4218. threading.Thread(
  4219. target=async_process_all_empty_genealogy,
  4220. args=(task_id, session['user_id']),
  4221. daemon=True
  4222. ).start()
  4223. return jsonify({
  4224. "success": True,
  4225. "task_id": task_id,
  4226. "message": f"任务已从断点恢复(已完成 {task['completed_count']},从 last_processed_id={task['last_processed_id']} 继续)",
  4227. "last_processed_id": task['last_processed_id'],
  4228. "completed_count": task['completed_count'],
  4229. "total_count": task['total_count'],
  4230. })
  4231. finally:
  4232. conn.close()
  4233. @app.route('/manager/api/members/batch_process_all_empty', methods=['GET'])
  4234. def batch_process_all_empty():
  4235. """简便批量处理接口:自动处理所有族谱原文为空的成员,支持断点续跑"""
  4236. if 'user_id' not in session:
  4237. return jsonify({"success": False, "message": "Unauthorized"}), 401
  4238. conn = get_db_connection()
  4239. try:
  4240. with conn.cursor() as cursor:
  4241. cursor.execute("""
  4242. SELECT COUNT(*) as count
  4243. FROM family_member_info
  4244. WHERE (genealogy_original_traditional IS NULL OR genealogy_original_traditional = '' OR genealogy_original_traditional = 'None')
  4245. AND (genealogy_original_simplified IS NULL OR genealogy_original_simplified = '' OR genealogy_original_simplified = 'None')
  4246. """)
  4247. result = cursor.fetchone()
  4248. total_empty = result['count'] if result else 0
  4249. cursor.execute("""
  4250. SELECT task_id, status, last_processed_id, total_count, completed_count, failed_count
  4251. FROM batch_genealogy_task
  4252. WHERE user_id = %s AND status IN ('pending', 'processing')
  4253. ORDER BY created_at DESC
  4254. LIMIT 1
  4255. """, (session['user_id'],))
  4256. running_task = cursor.fetchone()
  4257. if running_task:
  4258. return jsonify({
  4259. "success": False,
  4260. "message": "存在正在进行的任务,若服务已重启可调用 POST /manager/api/members/batch_resume_task 恢复",
  4261. "task_id": running_task['task_id'],
  4262. "status": running_task['status'],
  4263. "last_processed_id": running_task['last_processed_id'],
  4264. "completed_count": running_task['completed_count'],
  4265. "total_count": running_task['total_count'],
  4266. "resume_tip": "POST /manager/api/members/batch_resume_task body: {\"task_id\": \"" + running_task['task_id'] + "\"}"
  4267. })
  4268. task_id = str(uuid.uuid4())
  4269. with conn.cursor() as cursor:
  4270. cursor.execute("""
  4271. INSERT INTO batch_genealogy_task (task_id, user_id, status, total_count, last_processed_id)
  4272. VALUES (%s, %s, 'processing', %s, 0)
  4273. """, (task_id, session['user_id'], total_empty))
  4274. conn.commit()
  4275. threading.Thread(
  4276. target=async_process_all_empty_genealogy,
  4277. args=(task_id, session['user_id']),
  4278. daemon=True
  4279. ).start()
  4280. return jsonify({
  4281. "success": True,
  4282. "task_id": task_id,
  4283. "message": f"任务已创建,将处理 {total_empty} 个族谱原文为空的成员",
  4284. "total_count": total_empty
  4285. })
  4286. finally:
  4287. conn.close()
  4288. # ==================== 微信小程序 API 接口 ====================
  4289. @app.route('/manager/api/wechat/login', methods=['POST'])
  4290. def api_wechat_login():
  4291. """微信小程序登录接口(正式流程)"""
  4292. import time
  4293. start_time = time.time()
  4294. try:
  4295. data = request.get_json()
  4296. if not data:
  4297. print(f"[API Wechat Login] Error: No request data")
  4298. return jsonify({"success": False, "message": "请求数据为空"}), 400
  4299. code = data.get('code', '')
  4300. encrypted_data = data.get('encryptedData', '')
  4301. iv = data.get('iv', '')
  4302. phone_code = data.get('phoneCode', '')
  4303. if not code:
  4304. print(f"[API Wechat Login] Error: Missing code parameter")
  4305. return jsonify({"success": False, "message": "缺少code参数"}), 400
  4306. print(f"[API Wechat Login] Received login request, code: {code[:10]}..., phoneCode: {phone_code[:10]}...")
  4307. # 1. 使用code获取session_key和openid
  4308. session_url = "https://api.weixin.qq.com/sns/jscode2session"
  4309. session_params = {
  4310. "appid": WECHAT_APP_ID,
  4311. "secret": WECHAT_APP_SECRET,
  4312. "js_code": code,
  4313. "grant_type": "authorization_code"
  4314. }
  4315. try:
  4316. session_response = requests.get(session_url, params=session_params, timeout=15)
  4317. session_response.raise_for_status()
  4318. except requests.exceptions.RequestException as e:
  4319. print(f"[WeChat Login] Session request failed: {e}")
  4320. return jsonify({"success": False, "message": f"网络请求失败: {str(e)}"}), 500
  4321. session_data = session_response.json()
  4322. print(f"[WeChat Login] Session response: {session_data}")
  4323. if 'errcode' in session_data and session_data['errcode'] != 0:
  4324. print(f"[WeChat Login] Session error: {session_data}")
  4325. return jsonify({"success": False, "message": session_data.get('errmsg', '登录失败')}), 400
  4326. openid = session_data.get('openid')
  4327. session_key = session_data.get('session_key')
  4328. if not openid:
  4329. print(f"[WeChat Login] Error: openid is empty")
  4330. return jsonify({"success": False, "message": "获取openid失败"}), 400
  4331. # 2. 获取手机号(支持两种方式)
  4332. phone = None
  4333. # 方式一:使用phoneCode调用官方接口(推荐)
  4334. if phone_code:
  4335. print(f"[WeChat Phone] Trying to get phone via phoneCode")
  4336. try:
  4337. access_token = get_wechat_access_token()
  4338. if access_token:
  4339. phone_url = f"https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token={access_token}"
  4340. phone_response = requests.post(phone_url, json={"code": phone_code}, timeout=15)
  4341. phone_response.raise_for_status()
  4342. phone_result = phone_response.json()
  4343. print(f"[WeChat Phone] Phone API response: {phone_result}")
  4344. if phone_result.get('errcode') == 0 and phone_result.get('phone_info'):
  4345. phone = phone_result['phone_info'].get('phoneNumber')
  4346. print(f"[WeChat Phone] Phone obtained via phoneCode: {phone}")
  4347. else:
  4348. print(f"[WeChat Phone] Failed to get phone via phoneCode: {phone_result}")
  4349. else:
  4350. print(f"[WeChat Phone] Failed to get access_token")
  4351. except requests.exceptions.RequestException as e:
  4352. print(f"[WeChat Phone] Phone request failed: {e}")
  4353. # 方式二:使用encryptedData解密(兼容旧方式)
  4354. if not phone and encrypted_data and iv and session_key:
  4355. print(f"[WeChat Phone] Trying to decrypt phone via encryptedData")
  4356. phone_data = decrypt_wechat_phone(encrypted_data, iv, session_key)
  4357. if phone_data and 'phoneNumber' in phone_data:
  4358. phone = phone_data['phoneNumber']
  4359. print(f"[WeChat Phone] Phone obtained via decryption: {phone}")
  4360. # 3. 创建或获取小程序用户(使用mp_users表)
  4361. conn = get_db_connection()
  4362. try:
  4363. with conn.cursor() as cursor:
  4364. cursor.execute("SELECT id, phone FROM mp_users WHERE openid = %s", (openid,))
  4365. mp_user = cursor.fetchone()
  4366. if mp_user:
  4367. update_fields = []
  4368. update_params = []
  4369. if phone and phone != mp_user.get('phone'):
  4370. update_fields.append("phone = %s")
  4371. update_params.append(phone)
  4372. update_fields.append("last_login_at = CURRENT_TIMESTAMP")
  4373. update_params.append(openid)
  4374. if update_fields:
  4375. sql = f"UPDATE mp_users SET {', '.join(update_fields)} WHERE openid = %s"
  4376. cursor.execute(sql, update_params)
  4377. conn.commit()
  4378. user_id = mp_user['id']
  4379. else:
  4380. cursor.execute("""
  4381. INSERT INTO mp_users (openid, phone, created_at, updated_at, last_login_at)
  4382. VALUES (%s, %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
  4383. """, (openid, phone))
  4384. conn.commit()
  4385. user_id = cursor.lastrowid
  4386. print(f"[WeChat Login] Created new user: {user_id}, openid: {openid[:10]}...")
  4387. finally:
  4388. conn.close()
  4389. import uuid
  4390. token = str(uuid.uuid4())
  4391. # 持久化 token,用于后续接口识别用户身份
  4392. conn2 = get_db_connection()
  4393. try:
  4394. with conn2.cursor() as cursor2:
  4395. cursor2.execute("UPDATE mp_users SET token = %s WHERE id = %s", (token, user_id))
  4396. conn2.commit()
  4397. finally:
  4398. conn2.close()
  4399. elapsed = time.time() - start_time
  4400. print(f"[API Wechat Login] Success, elapsed: {elapsed:.2f}s, user_id: {user_id}, phone: {phone}")
  4401. return jsonify({
  4402. "success": True,
  4403. "token": token,
  4404. "user": {
  4405. "id": user_id,
  4406. "openid": openid,
  4407. "phone": phone,
  4408. "login_type": "wechat_mp"
  4409. }
  4410. })
  4411. except Exception as e:
  4412. elapsed = time.time() - start_time
  4413. print(f"[API Wechat Login] Error: {e}, elapsed: {elapsed:.2f}s")
  4414. return jsonify({"success": False, "message": str(e)}), 500
  4415. @app.route('/manager/api/members/search', methods=['GET'])
  4416. def api_search_members():
  4417. """搜索成员(小程序用)"""
  4418. keyword = request.args.get('keyword', '')
  4419. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  4420. if not token:
  4421. return jsonify({"success": False, "message": "未登录"}), 401
  4422. conn = get_db_connection()
  4423. try:
  4424. with conn.cursor() as cursor:
  4425. base_sql = """
  4426. SELECT
  4427. m.id, m.name, m.simplified_name, m.name_word_generation,
  4428. m.sex, m.birthday, m.family_rank, m.is_pass_away, m.marital_status,
  4429. p.name AS father_name,
  4430. p.simplified_name AS father_simplified_name,
  4431. p.name_word_generation AS father_generation,
  4432. r.relation_type AS father_relation_type
  4433. FROM family_member_info m
  4434. LEFT JOIN family_relation_info r
  4435. ON r.child_mid = m.id AND r.relation_type IN (1, 2)
  4436. LEFT JOIN family_member_info p ON p.id = r.parent_mid
  4437. {where}
  4438. ORDER BY m.name_word_generation ASC, m.id ASC
  4439. LIMIT 30
  4440. """
  4441. if keyword:
  4442. cursor.execute(
  4443. base_sql.format(where="WHERE m.name LIKE %s OR m.simplified_name LIKE %s"),
  4444. (f"%{keyword}%", f"%{keyword}%")
  4445. )
  4446. else:
  4447. cursor.execute(base_sql.format(where=""))
  4448. members = cursor.fetchall()
  4449. for m in members:
  4450. m['birthday_date'] = format_timestamp(m.get('birthday'))
  4451. return jsonify({"success": True, "data": members})
  4452. finally:
  4453. conn.close()
  4454. @app.route('/manager/api/members/check_duplicate', methods=['GET'])
  4455. def api_check_duplicate():
  4456. """检查同名成员"""
  4457. name = request.args.get('name', '')
  4458. conn = get_db_connection()
  4459. try:
  4460. with conn.cursor() as cursor:
  4461. cursor.execute("""
  4462. SELECT id, name, simplified_name, name_word_generation
  4463. FROM family_member_info
  4464. WHERE name = %s OR simplified_name = %s
  4465. LIMIT 10
  4466. """, (name, name))
  4467. members = cursor.fetchall()
  4468. return jsonify({
  4469. "success": True,
  4470. "data": members
  4471. })
  4472. finally:
  4473. conn.close()
  4474. @app.route('/manager/api/members/<int:member_id>', methods=['GET'])
  4475. def api_get_member(member_id):
  4476. """获取单个成员信息(含关系)"""
  4477. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  4478. if not token:
  4479. return jsonify({"success": False, "message": "未登录"}), 401
  4480. conn = get_db_connection()
  4481. try:
  4482. with conn.cursor() as cursor:
  4483. cursor.execute("SELECT * FROM family_member_info WHERE id = %s", (member_id,))
  4484. member = cursor.fetchone()
  4485. if not member:
  4486. return jsonify({"success": False, "message": "成员不存在"}), 404
  4487. member['birthday_date'] = format_timestamp(member.get('birthday'))
  4488. if member.get('create_time'):
  4489. member['create_time'] = member['create_time'].strftime('%Y-%m-%d %H:%M')
  4490. if member.get('modified_time'):
  4491. member['modified_time'] = member['modified_time'].strftime('%Y-%m-%d %H:%M')
  4492. # 父母
  4493. cursor.execute("""
  4494. SELECT m.id, m.name, m.simplified_name, m.name_word_generation,
  4495. r.relation_type, r.sub_relation_type, r.child_order
  4496. FROM family_relation_info r
  4497. JOIN family_member_info m ON m.id = r.parent_mid
  4498. WHERE r.child_mid = %s
  4499. ORDER BY r.relation_type ASC
  4500. """, (member_id,))
  4501. parents = cursor.fetchall()
  4502. # 子女
  4503. cursor.execute("""
  4504. SELECT m.id, m.name, m.simplified_name, m.name_word_generation,
  4505. r.relation_type, r.sub_relation_type, r.child_order
  4506. FROM family_relation_info r
  4507. JOIN family_member_info m ON m.id = r.child_mid
  4508. WHERE r.parent_mid = %s
  4509. ORDER BY COALESCE(r.child_order, 9999), m.id ASC
  4510. """, (member_id,))
  4511. children = cursor.fetchall()
  4512. # relation_type: 1=父, 2=母
  4513. relation_labels = {1: '父', 2: '母', 3: '祖父', 4: '祖母'}
  4514. for p in parents:
  4515. p['relation_label'] = relation_labels.get(p.get('relation_type'), '亲属')
  4516. # 计算入继说明
  4517. _order_labels = {1:'长', 2:'次', 3:'三', 4:'四', 5:'五',
  4518. 6:'六', 7:'七', 8:'八', 9:'九', 10:'十'}
  4519. adopt_info = None
  4520. is_adopted_in = any(p.get('sub_relation_type') == 3 for p in parents)
  4521. if is_adopted_in:
  4522. bio = next((p for p in parents if p.get('sub_relation_type') == 2), None)
  4523. if bio:
  4524. bio_name = bio.get('simplified_name') or bio.get('name', '')
  4525. order = bio.get('child_order')
  4526. order_str = _order_labels.get(order, f'第{order}') if order else '某'
  4527. adopt_info = f"由{bio_name}公{order_str}子入继"
  4528. return jsonify({
  4529. "success": True,
  4530. "data": {**member, "parents": parents, "children": children,
  4531. "adopt_info": adopt_info}
  4532. })
  4533. finally:
  4534. conn.close()
  4535. @app.route('/manager/api/members/add', methods=['POST'])
  4536. def api_add_member():
  4537. """添加成员(小程序用)"""
  4538. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  4539. if not token:
  4540. return jsonify({"success": False, "message": "未登录"}), 401
  4541. mp_user = get_mp_user_from_token(token)
  4542. mp_user_id = mp_user['id'] if mp_user else None
  4543. try:
  4544. data = request.get_json()
  4545. name = data.get('name', '')
  4546. simplified_name = data.get('simplified_name', '')
  4547. sex = data.get('sex', 1)
  4548. birthday_str = data.get('birthday', '')
  4549. family_rank = data.get('family_rank', '')
  4550. name_word_generation = data.get('name_word_generation', '')
  4551. is_pass_away = data.get('is_pass_away', 0)
  4552. marital_status = data.get('marital_status', 0)
  4553. former_name = data.get('former_name', '')
  4554. phone = data.get('phone', '')
  4555. notes = data.get('notes', '')
  4556. relations = data.get('relations', [])
  4557. if not name:
  4558. return jsonify({"success": False, "message": "姓名不能为空"}), 400
  4559. # 将日期字符串 "YYYY-MM-DD" 转为 Unix 时间戳(与后台一致)
  4560. birthday_ts = 0
  4561. if birthday_str:
  4562. try:
  4563. birthday_ts = int(datetime.strptime(birthday_str, '%Y-%m-%d').timestamp())
  4564. except Exception:
  4565. birthday_ts = 0
  4566. conn = get_db_connection()
  4567. try:
  4568. with conn.cursor() as cursor:
  4569. cursor.execute("""
  4570. INSERT INTO family_member_info
  4571. (name, simplified_name, sex, birthday, family_rank,
  4572. name_word_generation, is_pass_away, marital_status, former_name, phone, notes,
  4573. data_source, create_uid, create_time, modified_time)
  4574. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 'miniprogram', %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
  4575. """, (name, simplified_name, sex, birthday_ts, family_rank,
  4576. name_word_generation, is_pass_away, marital_status, former_name, phone, notes,
  4577. mp_user_id))
  4578. conn.commit()
  4579. new_member_id = cursor.lastrowid
  4580. # 添加关系
  4581. for rel in relations:
  4582. parent_mid = rel.get('parent_mid')
  4583. relation_type = rel.get('relation_type', 1)
  4584. sub_relation_type = rel.get('sub_relation_type', 0)
  4585. if parent_mid:
  4586. cursor.execute("""
  4587. INSERT INTO family_relation_info
  4588. (parent_mid, child_mid, relation_type, sub_relation_type)
  4589. VALUES (%s, %s, %s, %s)
  4590. """, (parent_mid, new_member_id, relation_type, sub_relation_type))
  4591. conn.commit()
  4592. return jsonify({
  4593. "success": True,
  4594. "message": "添加成功",
  4595. "memberId": new_member_id
  4596. })
  4597. finally:
  4598. conn.close()
  4599. except Exception as e:
  4600. print(f"[API Add Member] Error: {e}")
  4601. return jsonify({"success": False, "message": str(e)}), 500
  4602. @app.route('/manager/api/members/my', methods=['GET'])
  4603. def api_my_members():
  4604. """获取当前小程序用户录入的所有成员"""
  4605. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  4606. mp_user = get_mp_user_from_token(token)
  4607. if not mp_user:
  4608. return jsonify({"success": False, "message": "未登录或登录已过期"}), 401
  4609. conn = get_db_connection()
  4610. try:
  4611. with conn.cursor() as cursor:
  4612. cursor.execute("""
  4613. SELECT id, name, simplified_name, sex, birthday, name_word_generation,
  4614. family_rank, is_pass_away, marital_status, create_time
  4615. FROM family_member_info
  4616. WHERE create_uid = %s AND data_source = 'miniprogram'
  4617. ORDER BY create_time DESC
  4618. """, (mp_user['id'],))
  4619. members = cursor.fetchall()
  4620. for m in members:
  4621. m['birthday_date'] = format_timestamp(m.get('birthday'))
  4622. if m.get('create_time'):
  4623. m['create_time'] = m['create_time'].strftime('%Y-%m-%d %H:%M')
  4624. return jsonify({"success": True, "data": members})
  4625. except Exception as e:
  4626. print(f"[API My Members] Error: {e}")
  4627. return jsonify({"success": False, "message": str(e)}), 500
  4628. finally:
  4629. conn.close()
  4630. @app.route('/manager/api/member/<int:member_id>', methods=['PUT'])
  4631. def api_update_member(member_id):
  4632. """更新成员信息(小程序用,只能修改自己录入的)"""
  4633. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  4634. mp_user = get_mp_user_from_token(token)
  4635. if not mp_user:
  4636. return jsonify({"success": False, "message": "未登录或登录已过期"}), 401
  4637. conn = get_db_connection()
  4638. try:
  4639. with conn.cursor() as cursor:
  4640. cursor.execute(
  4641. "SELECT id, create_uid, data_source FROM family_member_info WHERE id = %s",
  4642. (member_id,)
  4643. )
  4644. member = cursor.fetchone()
  4645. if not member:
  4646. return jsonify({"success": False, "message": "成员不存在"}), 404
  4647. if member['data_source'] != 'miniprogram' or member['create_uid'] != mp_user['id']:
  4648. return jsonify({"success": False, "message": "无权限修改此成员"}), 403
  4649. data = request.get_json() or {}
  4650. name = data.get('name', '').strip()
  4651. if not name:
  4652. return jsonify({"success": False, "message": "姓名不能为空"}), 400
  4653. birthday_str = data.get('birthday', '')
  4654. birthday_ts = 0
  4655. if birthday_str:
  4656. try:
  4657. from datetime import datetime as _dt
  4658. birthday_ts = int(_dt.strptime(birthday_str, '%Y-%m-%d').timestamp())
  4659. except Exception:
  4660. birthday_ts = 0
  4661. cursor.execute("""
  4662. UPDATE family_member_info
  4663. SET name=%s, simplified_name=%s, sex=%s, birthday=%s,
  4664. family_rank=%s, name_word_generation=%s, is_pass_away=%s,
  4665. marital_status=%s, phone=%s, notes=%s, modified_time=CURRENT_TIMESTAMP
  4666. WHERE id=%s
  4667. """, (
  4668. name,
  4669. data.get('simplified_name', ''),
  4670. int(data.get('sex', 1)),
  4671. birthday_ts,
  4672. data.get('family_rank') or None,
  4673. data.get('name_word_generation', ''),
  4674. int(data.get('is_pass_away', 0)),
  4675. int(data.get('marital_status', 0)),
  4676. data.get('phone', ''),
  4677. data.get('notes', ''),
  4678. member_id
  4679. ))
  4680. conn.commit()
  4681. return jsonify({"success": True, "message": "修改成功"})
  4682. except Exception as e:
  4683. conn.rollback()
  4684. print(f"[API Update Member] Error: {e}")
  4685. return jsonify({"success": False, "message": str(e)}), 500
  4686. finally:
  4687. conn.close()
  4688. @app.route('/manager/api/members/<int:member_id>', methods=['DELETE'])
  4689. def api_delete_member(member_id):
  4690. """删除成员(小程序用,只能删除自己录入的)"""
  4691. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  4692. mp_user = get_mp_user_from_token(token)
  4693. if not mp_user:
  4694. return jsonify({"success": False, "message": "未登录或登录已过期"}), 401
  4695. conn = get_db_connection()
  4696. try:
  4697. with conn.cursor() as cursor:
  4698. cursor.execute("""
  4699. SELECT id, create_uid, data_source FROM family_member_info WHERE id = %s
  4700. """, (member_id,))
  4701. member = cursor.fetchone()
  4702. if not member:
  4703. return jsonify({"success": False, "message": "成员不存在"}), 404
  4704. if member['data_source'] != 'miniprogram' or member['create_uid'] != mp_user['id']:
  4705. return jsonify({"success": False, "message": "无权限删除此成员"}), 403
  4706. cursor.execute("DELETE FROM family_relation_info WHERE parent_mid = %s OR child_mid = %s",
  4707. (member_id, member_id))
  4708. cursor.execute("DELETE FROM family_member_info WHERE id = %s", (member_id,))
  4709. conn.commit()
  4710. return jsonify({"success": True, "message": "删除成功"})
  4711. except Exception as e:
  4712. conn.rollback()
  4713. print(f"[API Delete Member] Error: {e}")
  4714. return jsonify({"success": False, "message": str(e)}), 500
  4715. finally:
  4716. conn.close()
  4717. @app.route('/manager/api/lineage/<int:member_id>', methods=['GET'])
  4718. def api_get_lineage(member_id):
  4719. """获取世系信息(小程序用)- 完整多代版"""
  4720. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  4721. if not token:
  4722. return jsonify({"success": False, "message": "未登录"}), 401
  4723. conn = get_db_connection()
  4724. try:
  4725. with conn.cursor() as cursor:
  4726. # Step 1: 获取查询人物
  4727. cursor.execute("""
  4728. SELECT id, name, simplified_name, name_word, name_word_generation
  4729. FROM family_member_info WHERE id = %s
  4730. """, (member_id,))
  4731. center = cursor.fetchone()
  4732. if not center:
  4733. return jsonify({"success": False, "message": "成员不存在"}), 404
  4734. # Step 2: 向上追溯祖先链(最多100代),每代带同辈兄弟
  4735. generations = []
  4736. current_id = member_id
  4737. max_depth = 100
  4738. visited_ancestor_ids = set([member_id]) # 循环检测
  4739. for depth in range(max_depth):
  4740. cursor.execute("""
  4741. SELECT p.id, p.name, p.simplified_name, p.name_word, p.name_word_generation,
  4742. EXISTS(SELECT 1 FROM family_relation_info
  4743. WHERE parent_mid = p.id AND relation_type IN (1,2)) as has_children,
  4744. r.sub_relation_type
  4745. FROM family_relation_info r
  4746. JOIN family_member_info p ON r.parent_mid = p.id
  4747. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  4748. """, (current_id,))
  4749. parents = cursor.fetchall()
  4750. if not parents:
  4751. break
  4752. # 优先取非养父母关系
  4753. parent = None
  4754. for p in parents:
  4755. if p['sub_relation_type'] != 3:
  4756. parent = p
  4757. break
  4758. if not parent:
  4759. parent = parents[0]
  4760. # 循环检测
  4761. if parent['id'] in visited_ancestor_ids:
  4762. break
  4763. visited_ancestor_ids.add(parent['id'])
  4764. # 查祖父以获取该祖先的兄弟
  4765. cursor.execute("""
  4766. SELECT gp.id FROM family_relation_info r
  4767. JOIN family_member_info gp ON r.parent_mid = gp.id
  4768. WHERE r.child_mid = %s AND r.relation_type IN (1, 2) LIMIT 1
  4769. """, (parent['id'],))
  4770. grandparent = cursor.fetchone()
  4771. parent_siblings = []
  4772. if grandparent:
  4773. # 获取祖先自身的 child_order
  4774. cursor.execute("""
  4775. SELECT COALESCE(child_order, 1) AS child_order
  4776. FROM family_relation_info
  4777. WHERE parent_mid = %s AND child_mid = %s AND relation_type IN (1,2)
  4778. LIMIT 1
  4779. """, (grandparent['id'], parent['id']))
  4780. co_row = cursor.fetchone()
  4781. parent['child_order'] = co_row['child_order'] if co_row else 1
  4782. cursor.execute("""
  4783. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  4784. EXISTS(SELECT 1 FROM family_relation_info
  4785. WHERE parent_mid = c.id AND relation_type IN (1,2)) as has_children,
  4786. COALESCE(r.child_order, 1) AS child_order
  4787. FROM family_relation_info r
  4788. JOIN family_member_info c ON r.child_mid = c.id
  4789. WHERE r.parent_mid = %s AND r.relation_type IN (1,2) AND c.id != %s
  4790. ORDER BY COALESCE(r.child_order, 1), c.id
  4791. LIMIT 10
  4792. """, (grandparent['id'], parent['id']))
  4793. parent_siblings = cursor.fetchall()
  4794. for s in parent_siblings:
  4795. s['has_children'] = bool(s['has_children'])
  4796. else:
  4797. parent['child_order'] = None
  4798. parent['has_children'] = bool(parent['has_children'])
  4799. generations.append({
  4800. 'ancestor': parent,
  4801. 'siblings': list(parent_siblings),
  4802. 'depth': depth
  4803. })
  4804. current_id = parent['id']
  4805. # Step 3: 获取子女(排除出继、保留入继,带排行)
  4806. cursor.execute("""
  4807. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  4808. EXISTS(SELECT 1 FROM family_relation_info
  4809. WHERE parent_mid = c.id AND relation_type IN (1,2)) as has_children,
  4810. COALESCE(r.child_order, 1) AS child_order, r.sub_relation_type
  4811. FROM family_relation_info r
  4812. JOIN family_member_info c ON r.child_mid = c.id
  4813. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2)
  4814. AND (
  4815. COALESCE(r.sub_relation_type, 0) != 2
  4816. OR NOT EXISTS (
  4817. SELECT 1 FROM family_relation_info r2
  4818. WHERE r2.child_mid = c.id AND r2.sub_relation_type = 3
  4819. )
  4820. )
  4821. ORDER BY COALESCE(r.child_order, 1), c.id
  4822. LIMIT 20
  4823. """, (member_id,))
  4824. children = cursor.fetchall()
  4825. _order_labels_alg = {1:'长', 2:'次', 3:'三', 4:'四', 5:'五',
  4826. 6:'六', 7:'七', 8:'八', 9:'九', 10:'十'}
  4827. for c in children:
  4828. c['has_children'] = bool(c['has_children'])
  4829. # 入继子女:附加生父母信息,生成"由xxx公第N子入继"说明
  4830. if c['sub_relation_type'] == 3:
  4831. cursor.execute("""
  4832. SELECT p.name, p.simplified_name, r.child_order
  4833. FROM family_relation_info r
  4834. JOIN family_member_info p ON r.parent_mid = p.id
  4835. WHERE r.child_mid = %s AND r.sub_relation_type = 2 LIMIT 1
  4836. """, (c['id'],))
  4837. bp = cursor.fetchone()
  4838. if bp:
  4839. bio_name = bp['simplified_name'] or bp['name']
  4840. order = bp['child_order']
  4841. order_str = _order_labels_alg.get(order, f'第{order}') if order else '某'
  4842. c['adopt_info'] = f"由{bio_name}公{order_str}子入继"
  4843. # Step 4: 获取查询人物的同辈兄弟(含center自己的child_order)
  4844. siblings = []
  4845. center_child_order = None
  4846. if generations:
  4847. parent_id = generations[0]['ancestor']['id']
  4848. # 先获取 center 自身的 child_order
  4849. cursor.execute("""
  4850. SELECT child_order FROM family_relation_info
  4851. WHERE parent_mid = %s AND child_mid = %s AND relation_type IN (1,2)
  4852. LIMIT 1
  4853. """, (parent_id, member_id))
  4854. co_row = cursor.fetchone()
  4855. center_child_order = (co_row['child_order'] if co_row and co_row['child_order'] else 1)
  4856. cursor.execute("""
  4857. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  4858. EXISTS(SELECT 1 FROM family_relation_info
  4859. WHERE parent_mid = c.id AND relation_type IN (1,2)) as has_children,
  4860. r.sub_relation_type, COALESCE(r.child_order, 1) AS child_order
  4861. FROM family_relation_info r
  4862. JOIN family_member_info c ON r.child_mid = c.id
  4863. WHERE r.parent_mid = %s AND r.relation_type IN (1,2) AND c.id != %s
  4864. ORDER BY COALESCE(r.child_order, 1), c.id
  4865. LIMIT 10
  4866. """, (parent_id, member_id))
  4867. siblings = cursor.fetchall()
  4868. for s in siblings:
  4869. s['has_children'] = bool(s['has_children'])
  4870. # 判断是否还有更高的祖先
  4871. has_more_ancestors = False
  4872. topmost_ancestor_id = None
  4873. if generations:
  4874. topmost_ancestor_id = generations[-1]['ancestor']['id']
  4875. cursor.execute("""
  4876. SELECT COUNT(*) as cnt FROM family_relation_info
  4877. WHERE child_mid = %s AND relation_type IN (1,2)
  4878. """, (topmost_ancestor_id,))
  4879. has_more_ancestors = cursor.fetchone()['cnt'] > 0
  4880. return jsonify({
  4881. "success": True,
  4882. "data": {
  4883. "center": {**center, "child_order": center_child_order or 1},
  4884. "generations": generations,
  4885. "siblings": list(siblings),
  4886. "children": list(children),
  4887. "has_more_ancestors": has_more_ancestors,
  4888. "topmost_ancestor_id": topmost_ancestor_id
  4889. }
  4890. })
  4891. except Exception as e:
  4892. print(f"[API Lineage] Error: {e}")
  4893. return jsonify({"success": False, "message": str(e)}), 500
  4894. finally:
  4895. conn.close()
  4896. @app.route('/manager/api/lineage/<int:ancestor_id>/ancestors_above', methods=['GET'])
  4897. def api_get_ancestors_above(ancestor_id):
  4898. """小程序世系查询:从指定祖先节点继续向上追溯(分批加载更多祖先)"""
  4899. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  4900. if not token:
  4901. return jsonify({"success": False, "message": "未登录"}), 401
  4902. conn = get_db_connection()
  4903. try:
  4904. with conn.cursor() as cursor:
  4905. generations = []
  4906. current_id = ancestor_id
  4907. max_depth = 100
  4908. visited_ids = set([ancestor_id])
  4909. for depth in range(max_depth):
  4910. cursor.execute("""
  4911. SELECT p.id, p.name, p.simplified_name, p.name_word, p.name_word_generation,
  4912. EXISTS(SELECT 1 FROM family_relation_info
  4913. WHERE parent_mid = p.id AND relation_type IN (1,2)) as has_children,
  4914. r.sub_relation_type
  4915. FROM family_relation_info r
  4916. JOIN family_member_info p ON r.parent_mid = p.id
  4917. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  4918. """, (current_id,))
  4919. parents = cursor.fetchall()
  4920. if not parents:
  4921. break
  4922. parent = None
  4923. for p in parents:
  4924. if p['sub_relation_type'] != 3:
  4925. parent = p
  4926. break
  4927. if not parent:
  4928. parent = parents[0]
  4929. if parent['id'] in visited_ids:
  4930. break
  4931. visited_ids.add(parent['id'])
  4932. cursor.execute("""
  4933. SELECT gp.id FROM family_relation_info r
  4934. JOIN family_member_info gp ON r.parent_mid = gp.id
  4935. WHERE r.child_mid = %s AND r.relation_type IN (1, 2) LIMIT 1
  4936. """, (parent['id'],))
  4937. grandparent = cursor.fetchone()
  4938. parent_siblings = []
  4939. if grandparent:
  4940. cursor.execute("""
  4941. SELECT COALESCE(child_order, 1) AS child_order
  4942. FROM family_relation_info
  4943. WHERE parent_mid = %s AND child_mid = %s AND relation_type IN (1,2) LIMIT 1
  4944. """, (grandparent['id'], parent['id']))
  4945. co_row = cursor.fetchone()
  4946. parent['child_order'] = co_row['child_order'] if co_row else 1
  4947. cursor.execute("""
  4948. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  4949. EXISTS(SELECT 1 FROM family_relation_info
  4950. WHERE parent_mid = c.id AND relation_type IN (1,2)) as has_children,
  4951. COALESCE(r.child_order, 1) AS child_order
  4952. FROM family_relation_info r
  4953. JOIN family_member_info c ON r.child_mid = c.id
  4954. WHERE r.parent_mid = %s AND r.relation_type IN (1,2) AND c.id != %s
  4955. ORDER BY COALESCE(r.child_order, 1), c.id
  4956. LIMIT 10
  4957. """, (grandparent['id'], parent['id']))
  4958. parent_siblings = cursor.fetchall()
  4959. for s in parent_siblings:
  4960. s['has_children'] = bool(s['has_children'])
  4961. else:
  4962. parent['child_order'] = None
  4963. parent['has_children'] = bool(parent['has_children'])
  4964. generations.append({
  4965. 'ancestor': parent,
  4966. 'siblings': list(parent_siblings),
  4967. 'depth': depth
  4968. })
  4969. current_id = parent['id']
  4970. has_more_ancestors = False
  4971. topmost_ancestor_id = None
  4972. if generations:
  4973. topmost_ancestor_id = generations[-1]['ancestor']['id']
  4974. cursor.execute("""
  4975. SELECT COUNT(*) as cnt FROM family_relation_info
  4976. WHERE child_mid = %s AND relation_type IN (1,2)
  4977. """, (topmost_ancestor_id,))
  4978. has_more_ancestors = cursor.fetchone()['cnt'] > 0
  4979. return jsonify({
  4980. "success": True,
  4981. "data": {
  4982. "generations": generations,
  4983. "has_more_ancestors": has_more_ancestors,
  4984. "topmost_ancestor_id": topmost_ancestor_id
  4985. }
  4986. })
  4987. except Exception as e:
  4988. print(f"[API Ancestors Above] Error: {e}")
  4989. return jsonify({"success": False, "message": str(e)}), 500
  4990. finally:
  4991. conn.close()
  4992. @app.route('/manager/api/mp/wx/auth/login', methods=['POST'])
  4993. def mp_wx_login():
  4994. """微信小程序登录接口"""
  4995. try:
  4996. data = request.get_json()
  4997. code = data.get('code', '')
  4998. userInfo = data.get('userInfo', {})
  4999. if not code:
  5000. return jsonify({"success": False, "message": "缺少code参数"}), 400
  5001. openid = f"mock_openid_{code[:8]}"
  5002. conn = get_db_connection()
  5003. try:
  5004. with conn.cursor() as cursor:
  5005. cursor.execute("SELECT id, openid, member_id, is_bound FROM family_member_bind WHERE openid = %s", (openid,))
  5006. bind_info = cursor.fetchone()
  5007. if not bind_info:
  5008. cursor.execute("INSERT INTO family_member_bind (openid, created_at) VALUES (%s, CURRENT_TIMESTAMP)", (openid,))
  5009. conn.commit()
  5010. bind_info = {
  5011. 'id': cursor.lastrowid,
  5012. 'openid': openid,
  5013. 'member_id': None,
  5014. 'is_bound': 0
  5015. }
  5016. finally:
  5017. conn.close()
  5018. return jsonify({
  5019. "success": True,
  5020. "data": {
  5021. "openid": openid,
  5022. "token": f"mock_token_{openid}",
  5023. "isBound": bool(bind_info['is_bound']),
  5024. "memberId": bind_info['member_id']
  5025. }
  5026. })
  5027. except Exception as e:
  5028. print(f"[MP Login] Error: {e}")
  5029. return jsonify({"success": False, "message": str(e)}), 500
  5030. @app.route('/manager/api/mp/wx/config/getConfig', methods=['GET'])
  5031. def mp_wx_get_config():
  5032. """获取配置信息"""
  5033. config_key = request.args.get('configKey', '')
  5034. config_data = {
  5035. "CAROUSEL": {
  5036. "success": True,
  5037. "images": [
  5038. {
  5039. "image": "",
  5040. "title": "留家族旅",
  5041. "subtitle": "传承家族文化"
  5042. }
  5043. ]
  5044. },
  5045. "HONOR": {
  5046. "success": True,
  5047. "data": {
  5048. "name": "留越",
  5049. "role": "族谱发起人",
  5050. "desc": "2025年发起族谱建设,统筹信息收集"
  5051. }
  5052. }
  5053. }
  5054. result = config_data.get(config_key, {"success": False, "message": "配置不存在"})
  5055. return jsonify(result)
  5056. @app.route('/manager/api/mp/wx/family/member/selfCard', methods=['GET'])
  5057. def mp_wx_self_card():
  5058. """获取用户自己的卡片信息"""
  5059. openid = request.headers.get('X-MP-Openid', '')
  5060. if not openid:
  5061. return jsonify({"success": False, "message": "未登录"}), 401
  5062. conn = get_db_connection()
  5063. try:
  5064. with conn.cursor() as cursor:
  5065. cursor.execute("SELECT member_id FROM family_member_bind WHERE openid = %s", (openid,))
  5066. bind_info = cursor.fetchone()
  5067. if not bind_info or not bind_info['member_id']:
  5068. return jsonify({"success": False, "message": "未绑定成员"})
  5069. cursor.execute("SELECT id, name, simplified_name, sex, birthday, occupation, family_rank, branch_family_hall, residential_address FROM family_member_info WHERE id = %s", (bind_info['member_id'],))
  5070. member = cursor.fetchone()
  5071. if not member:
  5072. return jsonify({"success": False, "message": "成员不存在"})
  5073. return jsonify({
  5074. "success": True,
  5075. "data": member
  5076. })
  5077. finally:
  5078. conn.close()
  5079. @app.route('/manager/api/mp/wx/family/member/search', methods=['GET'])
  5080. def mp_wx_search_members():
  5081. """搜索家族成员"""
  5082. keyword = request.args.get('keyword', '')
  5083. conn = get_db_connection()
  5084. try:
  5085. with conn.cursor() as cursor:
  5086. if keyword:
  5087. cursor.execute("""
  5088. SELECT id, name, simplified_name, sex, birthday, family_rank
  5089. FROM family_member_info
  5090. WHERE name LIKE %s OR simplified_name LIKE %s
  5091. ORDER BY name_word_generation ASC, id ASC
  5092. LIMIT 20
  5093. """, (f"%{keyword}%", f"%{keyword}%"))
  5094. else:
  5095. cursor.execute("""
  5096. SELECT id, name, simplified_name, sex, birthday, family_rank
  5097. FROM family_member_info
  5098. ORDER BY name_word_generation ASC, id ASC
  5099. LIMIT 20
  5100. """)
  5101. members = cursor.fetchall()
  5102. return jsonify({
  5103. "success": True,
  5104. "data": members
  5105. })
  5106. finally:
  5107. conn.close()
  5108. @app.route('/manager/api/mp/wx/family/member/bind', methods=['POST'])
  5109. def mp_wx_bind_member():
  5110. """绑定用户到家族成员"""
  5111. try:
  5112. data = request.get_json()
  5113. openid = data.get('openid', '')
  5114. member_id = data.get('memberId', '')
  5115. if not openid or not member_id:
  5116. return jsonify({"success": False, "message": "参数错误"}), 400
  5117. conn = get_db_connection()
  5118. try:
  5119. with conn.cursor() as cursor:
  5120. cursor.execute("SELECT id FROM family_member_info WHERE id = %s", (member_id,))
  5121. member = cursor.fetchone()
  5122. if not member:
  5123. return jsonify({"success": False, "message": "成员不存在"})
  5124. cursor.execute("UPDATE family_member_bind SET member_id = %s, is_bound = 1, updated_at = CURRENT_TIMESTAMP WHERE openid = %s", (member_id, openid))
  5125. conn.commit()
  5126. return jsonify({"success": True, "message": "绑定成功"})
  5127. finally:
  5128. conn.close()
  5129. except Exception as e:
  5130. print(f"[MP Bind] Error: {e}")
  5131. return jsonify({"success": False, "message": str(e)}), 500
  5132. @app.route('/manager/api/mp/wx/family/lineage', methods=['GET'])
  5133. def mp_wx_get_lineage():
  5134. """获取世系信息"""
  5135. openid = request.headers.get('X-MP-Openid', '')
  5136. member_id = request.args.get('memberId', '')
  5137. if not openid:
  5138. return jsonify({"success": False, "message": "未登录"}), 401
  5139. conn = get_db_connection()
  5140. try:
  5141. if not member_id:
  5142. with conn.cursor() as cursor:
  5143. cursor.execute("SELECT member_id FROM family_member_bind WHERE openid = %s", (openid,))
  5144. bind_info = cursor.fetchone()
  5145. if bind_info and bind_info['member_id']:
  5146. member_id = bind_info['member_id']
  5147. else:
  5148. return jsonify({"success": False, "message": "未绑定成员"})
  5149. with conn.cursor() as cursor:
  5150. cursor.execute("""
  5151. SELECT id, name, simplified_name, sex, name_word_generation, birthday, occupation, family_rank, branch_family_hall, residential_address
  5152. FROM family_member_info
  5153. WHERE id = %s
  5154. """, (member_id,))
  5155. member = cursor.fetchone()
  5156. if not member:
  5157. return jsonify({"success": False, "message": "成员不存在"})
  5158. current_member = {
  5159. "id": member['id'],
  5160. "name": member['name'],
  5161. "simplified_name": member['simplified_name'],
  5162. "sex": member['sex'],
  5163. "name_word_generation": member['name_word_generation'],
  5164. "birthday": member['birthday'],
  5165. "occupation": member['occupation'],
  5166. "family_rank": member['family_rank'],
  5167. "branch_family_hall": member['branch_family_hall'],
  5168. "residential_address": member['residential_address']
  5169. }
  5170. cursor.execute("""
  5171. SELECT p.id, p.name, p.simplified_name, p.sex, p.name_word_generation, p.birthday
  5172. FROM family_relation_info r
  5173. JOIN family_member_info p ON r.parent_mid = p.id
  5174. WHERE r.child_mid = %s AND r.relation_type = 1
  5175. """, (member_id,))
  5176. father = cursor.fetchone()
  5177. cursor.execute("""
  5178. SELECT p.id, p.name, p.simplified_name, p.sex, p.name_word_generation, p.birthday
  5179. FROM family_relation_info r
  5180. JOIN family_member_info p ON r.parent_mid = p.id
  5181. WHERE r.child_mid = %s AND r.relation_type = 2
  5182. """, (member_id,))
  5183. mother = cursor.fetchone()
  5184. ancestors = []
  5185. if father or mother:
  5186. ancestors.append({
  5187. "father": father,
  5188. "mother": mother
  5189. })
  5190. cursor.execute("""
  5191. SELECT c.id, c.name, c.simplified_name, c.sex, c.name_word_generation, c.birthday, r.child_order
  5192. FROM family_relation_info r
  5193. JOIN family_member_info c ON r.child_mid = c.id
  5194. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2)
  5195. ORDER BY COALESCE(r.child_order, 999) ASC
  5196. """, (member_id,))
  5197. children = cursor.fetchall()
  5198. cursor.execute("""
  5199. SELECT DISTINCT s.id, s.name, s.simplified_name, s.sex
  5200. FROM family_relation_info r1
  5201. JOIN family_relation_info r2 ON r1.parent_mid = r2.parent_mid
  5202. JOIN family_member_info s ON r2.child_mid = s.id
  5203. WHERE r1.child_mid = %s AND r2.child_mid != %s
  5204. ORDER BY COALESCE(r2.child_order, 999) ASC
  5205. """, (member_id, member_id))
  5206. siblings = cursor.fetchall()
  5207. return jsonify({
  5208. "success": True,
  5209. "data": {
  5210. "member": current_member,
  5211. "ancestors": ancestors,
  5212. "children": children,
  5213. "siblings": siblings
  5214. }
  5215. })
  5216. finally:
  5217. conn.close()
  5218. @app.route('/manager/api/mp/wx/family/member/add', methods=['POST'])
  5219. def mp_wx_add_member():
  5220. """添加家族成员"""
  5221. try:
  5222. data = request.get_json()
  5223. openid = data.get('openid', '')
  5224. member_data = data.get('memberData', {})
  5225. relation_data = data.get('relationData', {})
  5226. if not openid:
  5227. return jsonify({"success": False, "message": "未登录"}), 401
  5228. conn = get_db_connection()
  5229. try:
  5230. with conn.cursor() as cursor:
  5231. member_info = {
  5232. 'name': member_data.get('name', ''),
  5233. 'simplified_name': member_data.get('simplified_name', '') or member_data.get('name', ''),
  5234. 'sex': member_data.get('sex', 1),
  5235. 'birthday': member_data.get('birthday', ''),
  5236. 'occupation': member_data.get('occupation', ''),
  5237. 'family_rank': member_data.get('family_rank', ''),
  5238. 'branch_family_hall': member_data.get('branch_family_hall', ''),
  5239. 'residential_address': member_data.get('residential_address', ''),
  5240. 'genealogy_text': member_data.get('genealogy_text', ''),
  5241. 'create_time': datetime.now(),
  5242. 'modified_time': datetime.now()
  5243. }
  5244. fields = ", ".join(member_info.keys())
  5245. placeholders = ", ".join(["%s"] * len(member_info))
  5246. sql = f"INSERT INTO family_member_info ({fields}) VALUES ({placeholders})"
  5247. cursor.execute(sql, list(member_info.values()))
  5248. new_member_id = cursor.lastrowid
  5249. relation_type = relation_data.get('relationType', '')
  5250. if relation_type:
  5251. parent_id = relation_data.get('parentId')
  5252. child_order = relation_data.get('childOrder', 1)
  5253. if parent_id and relation_type in ['father', 'mother', 'child']:
  5254. rel_type = 1 if relation_type == 'father' else 2 if relation_type == 'mother' else 1
  5255. cursor.execute("""
  5256. INSERT INTO family_relation_info
  5257. (parent_mid, child_mid, relation_type, child_order, source_mid, generation_diff)
  5258. VALUES (%s, %s, %s, %s, %s, 1)
  5259. """, (parent_id, new_member_id, rel_type, child_order, new_member_id))
  5260. conn.commit()
  5261. return jsonify({
  5262. "success": True,
  5263. "message": "添加成功",
  5264. "memberId": new_member_id
  5265. })
  5266. except Exception as e:
  5267. conn.rollback()
  5268. raise e
  5269. finally:
  5270. conn.close()
  5271. except Exception as e:
  5272. print(f"[MP Add Member] Error: {e}")
  5273. return jsonify({"success": False, "message": str(e)}), 500
  5274. @app.route('/manager/api/mp/wx/family/member/update', methods=['POST'])
  5275. def mp_wx_update_member():
  5276. """更新家族成员信息"""
  5277. try:
  5278. data = request.get_json()
  5279. openid = data.get('openid', '')
  5280. member_id = data.get('memberId', '')
  5281. update_data = data.get('updateData', {})
  5282. if not openid or not member_id:
  5283. return jsonify({"success": False, "message": "参数错误"}), 400
  5284. conn = get_db_connection()
  5285. try:
  5286. update_parts = []
  5287. params = []
  5288. if 'name' in update_data:
  5289. update_parts.append("name = %s")
  5290. params.append(update_data['name'])
  5291. if 'simplified_name' in update_data:
  5292. update_parts.append("simplified_name = %s")
  5293. params.append(update_data['simplified_name'])
  5294. if 'sex' in update_data:
  5295. update_parts.append("sex = %s")
  5296. params.append(update_data['sex'])
  5297. if 'birthday' in update_data:
  5298. update_parts.append("birthday = %s")
  5299. params.append(update_data['birthday'])
  5300. if 'occupation' in update_data:
  5301. update_parts.append("occupation = %s")
  5302. params.append(update_data['occupation'])
  5303. if 'family_rank' in update_data:
  5304. update_parts.append("family_rank = %s")
  5305. params.append(update_data['family_rank'])
  5306. if 'branch_family_hall' in update_data:
  5307. update_parts.append("branch_family_hall = %s")
  5308. params.append(update_data['branch_family_hall'])
  5309. if 'residential_address' in update_data:
  5310. update_parts.append("residential_address = %s")
  5311. params.append(update_data['residential_address'])
  5312. update_parts.append("modified_time = CURRENT_TIMESTAMP")
  5313. params.append(member_id)
  5314. sql = f"UPDATE family_member_info SET {', '.join(update_parts)} WHERE id = %s"
  5315. with conn.cursor() as cursor:
  5316. cursor.execute(sql, params)
  5317. conn.commit()
  5318. return jsonify({"success": True, "message": "更新成功"})
  5319. except Exception as e:
  5320. conn.rollback()
  5321. raise e
  5322. finally:
  5323. conn.close()
  5324. except Exception as e:
  5325. print(f"[MP Update Member] Error: {e}")
  5326. return jsonify({"success": False, "message": str(e)}), 500
  5327. # ==================== End 微信小程序 API 接口 ====================
  5328. if __name__ == '__main__':
  5329. app.run(debug=False, host='0.0.0.0', port=5001)