app.py 295 KB

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