app.py 276 KB

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