app.py 290 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695
  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. # 格式化创建时间 (针对 TIMESTAMP 字段)
  1304. if m.get('create_time'):
  1305. m['create_time_str'] = m['create_time'].strftime('%Y-%m-%d')
  1306. if m.get('modified_time'):
  1307. m['modified_time_str'] = m['modified_time'].strftime('%Y-%m-%d %H:%M')
  1308. finally:
  1309. print(f"[Members List] Closing database connection")
  1310. conn.close()
  1311. return render_template('members.html', members=members, search_name=search_name, page=page, total_pages=total_pages, total=total)
  1312. @app.route('/manager/batch_genealogy')
  1313. def batch_genealogy():
  1314. if 'user_id' not in session:
  1315. return redirect(url_for('login'))
  1316. return render_template('batch_genealogy.html')
  1317. @app.route('/manager/suspected_errors')
  1318. def suspected_errors():
  1319. if 'user_id' not in session:
  1320. return redirect(url_for('login'))
  1321. search_name = request.args.get('name', '').strip()
  1322. page = request.args.get('page', 1, type=int)
  1323. per_page = 20
  1324. offset = (page - 1) * per_page
  1325. conn = get_db_connection()
  1326. try:
  1327. with conn.cursor() as cursor:
  1328. # Base query with condition for non-empty suspected_error (using TRIM to remove whitespace)
  1329. 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) != ''"
  1330. count_query = "SELECT COUNT(*) as count FROM family_member_info WHERE suspected_error IS NOT NULL AND TRIM(suspected_error) != ''"
  1331. # Add search condition if provided
  1332. params = []
  1333. if search_name:
  1334. # Support both traditional and simplified name search
  1335. base_query += " AND (name LIKE %s OR simplified_name LIKE %s)"
  1336. count_query += " AND (name LIKE %s OR simplified_name LIKE %s)"
  1337. search_param = f"%{search_name}%"
  1338. params.extend([search_param, search_param])
  1339. # Get total count
  1340. cursor.execute(count_query, params)
  1341. result = cursor.fetchone()
  1342. total = result['count'] if result else 0
  1343. total_pages = (total + per_page - 1) // per_page
  1344. # Get members with pagination
  1345. base_query += " ORDER BY name LIMIT %s OFFSET %s"
  1346. params.extend([per_page, offset])
  1347. cursor.execute(base_query, params)
  1348. members = cursor.fetchall()
  1349. # Format birthday for display
  1350. for member in members:
  1351. if member['birthday']:
  1352. member['birthday_str'] = format_timestamp(member['birthday'])
  1353. else:
  1354. member['birthday_str'] = '未知'
  1355. finally:
  1356. conn.close()
  1357. return render_template('suspected_errors.html', members=members, search_name=search_name, page=page, total_pages=total_pages, total=total)
  1358. @app.route('/manager/tree')
  1359. def tree():
  1360. if 'user_id' not in session:
  1361. return redirect(url_for('login'))
  1362. return render_template('tree.html')
  1363. @app.route('/manager/lineage_query')
  1364. def lineage_query():
  1365. if 'user_id' not in session:
  1366. return redirect(url_for('login'))
  1367. return render_template('lineage_query.html')
  1368. @app.route('/manager/tree_classic')
  1369. def tree_classic():
  1370. if 'user_id' not in session:
  1371. return redirect(url_for('login'))
  1372. return render_template('tree_classic.html')
  1373. @app.route('/manager/tree_gen')
  1374. def tree_gen():
  1375. if 'user_id' not in session:
  1376. return redirect(url_for('login'))
  1377. return render_template('tree_gen.html')
  1378. @app.route('/manager/api/tree_data')
  1379. def tree_data():
  1380. if 'user_id' not in session:
  1381. return jsonify({"error": "Unauthorized"}), 401
  1382. conn = get_db_connection()
  1383. try:
  1384. with conn.cursor() as cursor:
  1385. # 获取所有成员
  1386. cursor.execute("SELECT id, name, simplified_name, sex, family_rank, name_word_generation FROM family_member_info")
  1387. members = cursor.fetchall()
  1388. # 获取所有关系 (1:父子 2:母子 10:夫妻 11:兄弟 12:姐妹),包括子类型
  1389. cursor.execute("SELECT parent_mid, child_mid, relation_type, sub_relation_type FROM family_relation_info")
  1390. relations = cursor.fetchall()
  1391. return jsonify({"members": members, "relations": relations})
  1392. finally:
  1393. conn.close()
  1394. @app.route('/manager/api/search_member', methods=['POST'])
  1395. def search_member():
  1396. if 'user_id' not in session:
  1397. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1398. data = request.get_json()
  1399. keyword = data.get('keyword', '').strip()
  1400. if not keyword:
  1401. return jsonify({"success": False, "message": "请输入搜索关键词"})
  1402. conn = get_db_connection()
  1403. try:
  1404. with conn.cursor() as cursor:
  1405. cursor.execute("""
  1406. SELECT fmi.id, fmi.name, fmi.simplified_name, fmi.name_word_generation,
  1407. p.name AS father_name, p.simplified_name AS father_simplified_name
  1408. FROM family_member_info fmi
  1409. LEFT JOIN family_relation_info r ON r.child_mid = fmi.id AND r.relation_type = 1
  1410. LEFT JOIN family_member_info p ON r.parent_mid = p.id
  1411. WHERE fmi.name LIKE %s OR fmi.simplified_name LIKE %s OR fmi.former_name LIKE %s
  1412. ORDER BY
  1413. CASE WHEN fmi.name = %s THEN 1
  1414. WHEN fmi.simplified_name = %s THEN 2
  1415. WHEN fmi.name LIKE %s THEN 3
  1416. WHEN fmi.simplified_name LIKE %s THEN 4
  1417. ELSE 5 END
  1418. """, (f'%{keyword}%', f'%{keyword}%', f'%{keyword}%', keyword, keyword, f'{keyword}%', f'{keyword}%'))
  1419. members = cursor.fetchall()
  1420. if members:
  1421. return jsonify({"success": True, "members": members})
  1422. else:
  1423. return jsonify({"success": False, "message": "未找到匹配的成员"})
  1424. finally:
  1425. conn.close()
  1426. @app.route('/manager/api/get_lineage/<int:member_id>')
  1427. def get_lineage(member_id):
  1428. if 'user_id' not in session:
  1429. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1430. # 追溯模式:incense(香火传承,入继→养父为上辈) | blood(血脉追溯,亲生父为上辈)
  1431. mode = request.args.get('mode', 'incense')
  1432. import time
  1433. start_time = time.time()
  1434. print(f"[Lineage Query] Starting query for member_id: {member_id} mode={mode} at {time.strftime('%Y-%m-%d %H:%M:%S')}")
  1435. conn = get_db_connection()
  1436. try:
  1437. with conn.cursor() as cursor:
  1438. # Step 1: Get center person
  1439. step_start = time.time()
  1440. cursor.execute("SELECT id, name, simplified_name, name_word, name_word_generation FROM family_member_info WHERE id = %s", (member_id,))
  1441. center = cursor.fetchone()
  1442. print(f"[Lineage Query] Step 1 - Get center: {time.time() - step_start:.3f}s")
  1443. if not center:
  1444. return jsonify({"success": False, "message": "成员不存在"})
  1445. # Step 2: Get ancestors with their siblings (generations)
  1446. step_start = time.time()
  1447. generations = [] # Array of generations, each with main ancestor and siblings
  1448. current_id = member_id
  1449. max_depth = 100 # 支持最多 100 代祖先(实际家谱一般不超过 80 代)
  1450. ancestor_ids = [] # Track ancestor IDs for exclusion when expanding
  1451. displayed_ids = set() # Track IDs that are already displayed
  1452. displayed_ids.add(member_id) # Center person is displayed
  1453. visited_ancestor_ids = set([member_id]) # 循环检测:避免脏数据死循环
  1454. for depth in range(max_depth):
  1455. # 获取所有父母关系(支持出继/入继)
  1456. cursor.execute("""
  1457. SELECT p.id, p.name, p.simplified_name, p.name_word, p.name_word_generation,
  1458. EXISTS(SELECT 1 FROM family_relation_info WHERE parent_mid = p.id AND relation_type IN (1, 2)) as has_children,
  1459. r.sub_relation_type
  1460. FROM family_relation_info r
  1461. JOIN family_member_info p ON r.parent_mid = p.id
  1462. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  1463. """, (current_id,))
  1464. parents = cursor.fetchall()
  1465. if not parents:
  1466. break
  1467. # 分拣各类父母关系
  1468. normal_parent = None
  1469. adoptive_parent = None # sub_type=3:入继养父
  1470. bio_parent = None # sub_type=2:出继亲生父
  1471. for p in parents:
  1472. if p['sub_relation_type'] == 3:
  1473. adoptive_parent = p
  1474. elif p['sub_relation_type'] == 2:
  1475. bio_parent = p
  1476. else:
  1477. normal_parent = p
  1478. if mode == 'blood':
  1479. # 血脉追溯:亲生父优先(出继亦沿亲生路径)
  1480. parent = normal_parent or bio_parent or adoptive_parent
  1481. else:
  1482. # 香火传承(默认):入继养父优先
  1483. parent = adoptive_parent or normal_parent or bio_parent
  1484. # 若走入继路径,在当事人卡片标注"从xx出继"
  1485. if parent is adoptive_parent and adoptive_parent is not None:
  1486. bio_name = (bio_parent.get('simplified_name') or bio_parent.get('name')) if bio_parent else None
  1487. adopt_label = f"从{bio_name}出继" if bio_name else "出继"
  1488. if depth == 0:
  1489. center['adoption_label'] = adopt_label
  1490. elif generations:
  1491. generations[-1]['ancestor']['adoption_label'] = adopt_label
  1492. # 祖先卡片不携带子辈关系类型(避免把子的出继/入继标在父身上)
  1493. parent['sub_relation_type'] = None
  1494. # 循环检测:如果该祖先已在链中出现过,终止(数据异常保护)
  1495. if parent['id'] in visited_ancestor_ids:
  1496. break
  1497. visited_ancestor_ids.add(parent['id'])
  1498. ancestor_ids.append(parent['id'])
  1499. displayed_ids.add(parent['id'])
  1500. # Get siblings of this ancestor (father's brothers)
  1501. # First get grandparent (parent's father)
  1502. cursor.execute("""
  1503. SELECT gp.id
  1504. FROM family_relation_info r
  1505. JOIN family_member_info gp ON r.parent_mid = gp.id
  1506. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  1507. ORDER BY CASE WHEN COALESCE(r.sub_relation_type, 0) = 3 THEN 1 ELSE 0 END, r.id
  1508. LIMIT 1
  1509. """, (parent['id'],))
  1510. grandparent = cursor.fetchone()
  1511. parent_siblings = []
  1512. if grandparent:
  1513. # 获取祖先自身的 child_order(在祖父下的排行)
  1514. cursor.execute("""
  1515. SELECT COALESCE(child_order, NULL) AS child_order
  1516. FROM family_relation_info
  1517. WHERE parent_mid = %s AND child_mid = %s AND relation_type IN (1, 2)
  1518. LIMIT 1
  1519. """, (grandparent['id'], parent['id']))
  1520. co_row = cursor.fetchone()
  1521. parent['child_order'] = co_row['child_order'] if co_row else None
  1522. # 获取祖先的兄弟(含 child_order 和 sub_relation_type,用于出继/入继标注)
  1523. cursor.execute("""
  1524. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  1525. EXISTS(SELECT 1 FROM family_relation_info WHERE parent_mid = c.id AND relation_type IN (1, 2)) as has_children,
  1526. COALESCE(r.child_order, NULL) AS child_order,
  1527. r.sub_relation_type
  1528. FROM family_relation_info r
  1529. JOIN family_member_info c ON r.child_mid = c.id
  1530. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2) AND c.id != %s
  1531. ORDER BY COALESCE(r.child_order, 99999), c.id
  1532. LIMIT 30
  1533. """, (grandparent['id'], parent['id']))
  1534. parent_siblings = cursor.fetchall()
  1535. # 为入继兄弟补充"从xx出继"标注
  1536. for sib in parent_siblings:
  1537. if sib.get('sub_relation_type') == 3:
  1538. cursor.execute("""
  1539. SELECT p.simplified_name, p.name
  1540. FROM family_relation_info r
  1541. JOIN family_member_info p ON r.parent_mid = p.id
  1542. WHERE r.child_mid = %s AND r.sub_relation_type = 2 LIMIT 1
  1543. """, (sib['id'],))
  1544. sbp = cursor.fetchone()
  1545. sib['adoption_label'] = (
  1546. f"从{sbp['simplified_name'] or sbp['name']}出继" if sbp else "出继"
  1547. )
  1548. # Mark sibling IDs as displayed
  1549. for sibling in parent_siblings:
  1550. displayed_ids.add(sibling['id'])
  1551. # Check if parent has any children NOT already displayed
  1552. # Only show expand button if there are undisplayed children
  1553. cursor.execute("""
  1554. SELECT COUNT(*) as count
  1555. FROM family_relation_info r
  1556. JOIN family_member_info c ON r.child_mid = c.id
  1557. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2)
  1558. """, (parent['id'],))
  1559. total_children = cursor.fetchone()['count']
  1560. # Check if current child is displayed (current_id is the child of parent)
  1561. child_displayed = current_id in displayed_ids
  1562. # Show expand if there are children not displayed
  1563. show_expand = total_children > (1 if child_displayed else 0)
  1564. parent['show_expand'] = show_expand
  1565. generations.append({
  1566. 'ancestor': parent,
  1567. 'siblings': parent_siblings,
  1568. 'depth': depth
  1569. })
  1570. current_id = parent['id']
  1571. print(f"[Lineage Query] Step 2 - Get generations ({len(generations)}): {time.time() - step_start:.3f}s")
  1572. # Step 3: Get immediate children only (limited count)
  1573. step_start = time.time()
  1574. # 获取子女:根据模式选择不同过滤策略
  1575. # 香火传承:包含入继子女、普通子女,排除已被人收继的出继子女
  1576. # 血脉追溯:包含亲生子女(含出继走的),排除从别处入继的子女
  1577. if mode == 'blood':
  1578. children_filter = "AND COALESCE(r.sub_relation_type, 0) != 3"
  1579. else:
  1580. children_filter = """AND (
  1581. COALESCE(r.sub_relation_type, 0) != 2
  1582. OR NOT EXISTS (
  1583. SELECT 1 FROM family_relation_info r2
  1584. WHERE r2.child_mid = c.id AND r2.sub_relation_type = 3
  1585. )
  1586. )"""
  1587. cursor.execute(f"""
  1588. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  1589. EXISTS(SELECT 1 FROM family_relation_info WHERE parent_mid = c.id AND relation_type IN (1, 2)) as has_children,
  1590. r.sub_relation_type,
  1591. r.child_order
  1592. FROM family_relation_info r
  1593. JOIN family_member_info c ON r.child_mid = c.id
  1594. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2)
  1595. {children_filter}
  1596. ORDER BY COALESCE(r.child_order, 99999), c.id
  1597. LIMIT 30
  1598. """, (member_id,))
  1599. children = cursor.fetchall()
  1600. # 香火模式:对入继子女标注"从xx出继";血脉模式:对出继子女标注"出继至xx"
  1601. for child in children:
  1602. if mode == 'incense' and child['sub_relation_type'] == 3:
  1603. cursor.execute("""
  1604. SELECT p.simplified_name, p.name
  1605. FROM family_relation_info r
  1606. JOIN family_member_info p ON r.parent_mid = p.id
  1607. WHERE r.child_mid = %s AND r.sub_relation_type = 2 LIMIT 1
  1608. """, (child['id'],))
  1609. bio_p = cursor.fetchone()
  1610. child['adoption_label'] = (
  1611. f"从{bio_p['simplified_name'] or bio_p['name']}出继" if bio_p else "出继"
  1612. )
  1613. elif mode == 'blood' and child['sub_relation_type'] == 2:
  1614. # 血脉模式下标注出继子女的去向(养父)
  1615. cursor.execute("""
  1616. SELECT p.simplified_name, p.name
  1617. FROM family_relation_info r
  1618. JOIN family_member_info p ON r.parent_mid = p.id
  1619. WHERE r.child_mid = %s AND r.sub_relation_type = 3 LIMIT 1
  1620. """, (child['id'],))
  1621. adop_p = cursor.fetchone()
  1622. if adop_p:
  1623. child['adoption_label'] = f"出继至{adop_p['simplified_name'] or adop_p['name']}"
  1624. child['sub_relation_type'] = 2 # 保持供前端显示 adopted-out 样式
  1625. # Initialize children array
  1626. for child in children:
  1627. child['children'] = []
  1628. print(f"[Lineage Query] Step 3 - Get children ({len(children)}): {time.time() - step_start:.3f}s")
  1629. # Step 4: Get siblings of center person + center's own child_order
  1630. step_start = time.time()
  1631. siblings = []
  1632. center_child_order = None
  1633. if generations:
  1634. parent_id = generations[0]['ancestor']['id'] # Father
  1635. # 中心人物自身的排行
  1636. cursor.execute("""
  1637. SELECT COALESCE(child_order, NULL) AS child_order
  1638. FROM family_relation_info
  1639. WHERE parent_mid = %s AND child_mid = %s AND relation_type IN (1, 2)
  1640. LIMIT 1
  1641. """, (parent_id, member_id))
  1642. co_row = cursor.fetchone()
  1643. center_child_order = co_row['child_order'] if co_row else None
  1644. cursor.execute("""
  1645. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  1646. EXISTS(SELECT 1 FROM family_relation_info WHERE parent_mid = c.id AND relation_type IN (1, 2)) as has_children,
  1647. r.sub_relation_type,
  1648. COALESCE(r.child_order, NULL) AS child_order
  1649. FROM family_relation_info r
  1650. JOIN family_member_info c ON r.child_mid = c.id
  1651. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2) AND c.id != %s
  1652. ORDER BY COALESCE(r.child_order, 99999), c.id
  1653. LIMIT 30
  1654. """, (parent_id, member_id))
  1655. siblings = cursor.fetchall()
  1656. # 为入继兄弟补充"从xx出继"标注
  1657. for sib in siblings:
  1658. if sib.get('sub_relation_type') == 3:
  1659. cursor.execute("""
  1660. SELECT p.simplified_name, p.name
  1661. FROM family_relation_info r
  1662. JOIN family_member_info p ON r.parent_mid = p.id
  1663. WHERE r.child_mid = %s AND r.sub_relation_type = 2 LIMIT 1
  1664. """, (sib['id'],))
  1665. sbp = cursor.fetchone()
  1666. if sbp:
  1667. sib['adoption_label'] = f"从{sbp['simplified_name'] or sbp['name']}出继"
  1668. else:
  1669. sib['adoption_label'] = "出继"
  1670. print(f"[Lineage Query] Step 4 - Get siblings ({len(siblings)}): {time.time() - step_start:.3f}s")
  1671. total_time = time.time() - start_time
  1672. print(f"[Lineage Query] Total time: {total_time:.3f}s")
  1673. # 判断是否还有更高的祖先(顶端祖先是否仍有父亲)
  1674. has_more_ancestors = False
  1675. topmost_ancestor_id = None
  1676. if generations:
  1677. topmost_ancestor_id = generations[-1]['ancestor']['id']
  1678. cursor.execute("""
  1679. SELECT COUNT(*) as cnt FROM family_relation_info
  1680. WHERE child_mid = %s AND relation_type IN (1,2)
  1681. """, (topmost_ancestor_id,))
  1682. has_more_ancestors = cursor.fetchone()['cnt'] > 0
  1683. return jsonify({
  1684. "success": True,
  1685. "data": {
  1686. "center": {**center, "child_order": center_child_order},
  1687. "generations": generations,
  1688. "ancestor_ids": ancestor_ids,
  1689. "siblings": siblings,
  1690. "children": children,
  1691. "has_more_ancestors": has_more_ancestors,
  1692. "topmost_ancestor_id": topmost_ancestor_id
  1693. }
  1694. })
  1695. except Exception as e:
  1696. print(f"[Lineage Query] Error: {e}")
  1697. return jsonify({"success": False, "message": str(e)})
  1698. finally:
  1699. conn.close()
  1700. @app.route('/manager/api/get_ancestors_above/<int:ancestor_id>')
  1701. def get_ancestors_above(ancestor_id):
  1702. """从指定祖先节点继续向上追溯,用于世系查询"继续向上"按钮"""
  1703. if 'user_id' not in session:
  1704. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1705. mode = request.args.get('mode', 'incense')
  1706. conn = get_db_connection()
  1707. try:
  1708. with conn.cursor() as cursor:
  1709. generations = []
  1710. current_id = ancestor_id
  1711. max_depth = 100
  1712. visited_ids = set([ancestor_id])
  1713. # 计算 anchor 节点(ancestor_id)自身的 adoption_label(已在上层渲染,此处只补充标签)
  1714. anchor_adoption_label = None
  1715. cursor.execute("""
  1716. SELECT p.id, p.name, p.simplified_name, r.sub_relation_type
  1717. FROM family_relation_info r
  1718. JOIN family_member_info p ON r.parent_mid = p.id
  1719. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  1720. """, (ancestor_id,))
  1721. anchor_parents = cursor.fetchall()
  1722. anchor_bio = None
  1723. has_adoptive = False
  1724. for ap in anchor_parents:
  1725. if ap['sub_relation_type'] == 3:
  1726. has_adoptive = True
  1727. elif ap['sub_relation_type'] == 2:
  1728. anchor_bio = ap
  1729. if has_adoptive and anchor_bio:
  1730. bio_name = anchor_bio.get('simplified_name') or anchor_bio.get('name')
  1731. anchor_adoption_label = f"从{bio_name}出继" if bio_name else "出继"
  1732. for depth in range(max_depth):
  1733. cursor.execute("""
  1734. SELECT p.id, p.name, p.simplified_name, p.name_word, p.name_word_generation,
  1735. EXISTS(SELECT 1 FROM family_relation_info
  1736. WHERE parent_mid = p.id AND relation_type IN (1,2)) as has_children,
  1737. r.sub_relation_type
  1738. FROM family_relation_info r
  1739. JOIN family_member_info p ON r.parent_mid = p.id
  1740. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  1741. """, (current_id,))
  1742. parents = cursor.fetchall()
  1743. if not parents:
  1744. break
  1745. # 分拣各类父母关系
  1746. normal_parent = None
  1747. adoptive_parent = None
  1748. bio_parent = None
  1749. for p in parents:
  1750. if p['sub_relation_type'] == 3:
  1751. adoptive_parent = p
  1752. elif p['sub_relation_type'] == 2:
  1753. bio_parent = p
  1754. else:
  1755. normal_parent = p
  1756. if mode == 'blood':
  1757. parent = normal_parent or bio_parent or adoptive_parent
  1758. else:
  1759. parent = adoptive_parent or normal_parent or bio_parent
  1760. # 若走入继路径,在 current_id 对应的人物上标注"从xx出继"
  1761. if parent is adoptive_parent and adoptive_parent is not None:
  1762. bio_name = (bio_parent.get('simplified_name') or bio_parent.get('name')) if bio_parent else None
  1763. adopt_label = f"从{bio_name}出继" if bio_name else "出继"
  1764. if depth == 0:
  1765. anchor_adoption_label = adopt_label
  1766. elif generations:
  1767. generations[-1]['ancestor']['adoption_label'] = adopt_label
  1768. # 祖先卡片不携带子辈关系类型
  1769. parent['sub_relation_type'] = None
  1770. if parent['id'] in visited_ids:
  1771. break
  1772. visited_ids.add(parent['id'])
  1773. # 查祖父,用于获取该祖先的兄弟(优先亲生父母,排除养父)
  1774. cursor.execute("""
  1775. SELECT gp.id FROM family_relation_info r
  1776. JOIN family_member_info gp ON r.parent_mid = gp.id
  1777. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  1778. ORDER BY CASE WHEN COALESCE(r.sub_relation_type, 0) = 3 THEN 1 ELSE 0 END, r.id
  1779. LIMIT 1
  1780. """, (parent['id'],))
  1781. grandparent = cursor.fetchone()
  1782. parent_siblings = []
  1783. if grandparent:
  1784. cursor.execute("""
  1785. SELECT COALESCE(child_order, 1) AS child_order
  1786. FROM family_relation_info
  1787. WHERE parent_mid = %s AND child_mid = %s AND relation_type IN (1,2) LIMIT 1
  1788. """, (grandparent['id'], parent['id']))
  1789. co_row = cursor.fetchone()
  1790. parent['child_order'] = co_row['child_order'] if co_row else 1
  1791. cursor.execute("""
  1792. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  1793. EXISTS(SELECT 1 FROM family_relation_info
  1794. WHERE parent_mid = c.id AND relation_type IN (1,2)) as has_children,
  1795. COALESCE(r.child_order, 1) AS child_order
  1796. FROM family_relation_info r
  1797. JOIN family_member_info c ON r.child_mid = c.id
  1798. WHERE r.parent_mid = %s AND r.relation_type IN (1,2) AND c.id != %s
  1799. ORDER BY COALESCE(r.child_order, 1), c.id
  1800. LIMIT 10
  1801. """, (grandparent['id'], parent['id']))
  1802. parent_siblings = cursor.fetchall()
  1803. for s in parent_siblings:
  1804. s['has_children'] = bool(s['has_children'])
  1805. else:
  1806. parent['child_order'] = None
  1807. parent['has_children'] = bool(parent['has_children'])
  1808. generations.append({
  1809. 'ancestor': parent,
  1810. 'siblings': list(parent_siblings),
  1811. 'depth': depth
  1812. })
  1813. current_id = parent['id']
  1814. # 是否还有更高的祖先
  1815. has_more_ancestors = False
  1816. topmost_ancestor_id = None
  1817. if generations:
  1818. topmost_ancestor_id = generations[-1]['ancestor']['id']
  1819. cursor.execute("""
  1820. SELECT COUNT(*) as cnt FROM family_relation_info
  1821. WHERE child_mid = %s AND relation_type IN (1,2)
  1822. """, (topmost_ancestor_id,))
  1823. has_more_ancestors = cursor.fetchone()['cnt'] > 0
  1824. return jsonify({
  1825. "success": True,
  1826. "data": {
  1827. "generations": generations,
  1828. "has_more_ancestors": has_more_ancestors,
  1829. "topmost_ancestor_id": topmost_ancestor_id,
  1830. "anchor_adoption_label": anchor_adoption_label
  1831. }
  1832. })
  1833. except Exception as e:
  1834. return jsonify({"success": False, "message": str(e)})
  1835. finally:
  1836. conn.close()
  1837. @app.route('/manager/api/get_descendants/<int:parent_id>')
  1838. def get_descendants(parent_id):
  1839. if 'user_id' not in session:
  1840. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1841. # Get excluded IDs from query parameter
  1842. excluded_ids = request.args.get('exclude', '')
  1843. excluded_list = []
  1844. if excluded_ids:
  1845. excluded_list = [int(id.strip()) for id in excluded_ids.split(',') if id.strip().isdigit()]
  1846. print(f"[get_descendants] Parent ID: {parent_id}, Excluded IDs: {excluded_list}")
  1847. conn = get_db_connection()
  1848. try:
  1849. with conn.cursor() as cursor:
  1850. if excluded_list:
  1851. # Build query with exclusion
  1852. placeholders = ', '.join(['%s'] * len(excluded_list))
  1853. cursor.execute(f"""
  1854. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  1855. EXISTS(SELECT 1 FROM family_relation_info WHERE parent_mid = c.id AND relation_type IN (1, 2)) as has_children
  1856. FROM family_relation_info r
  1857. JOIN family_member_info c ON r.child_mid = c.id
  1858. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2) AND c.id NOT IN ({placeholders})
  1859. ORDER BY COALESCE(r.child_order, 99999), c.id
  1860. LIMIT 20
  1861. """, (parent_id,) + tuple(excluded_list))
  1862. else:
  1863. cursor.execute("""
  1864. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  1865. EXISTS(SELECT 1 FROM family_relation_info WHERE parent_mid = c.id AND relation_type IN (1, 2)) as has_children
  1866. FROM family_relation_info r
  1867. JOIN family_member_info c ON r.child_mid = c.id
  1868. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2)
  1869. ORDER BY COALESCE(r.child_order, 99999), c.id
  1870. LIMIT 20
  1871. """, (parent_id,))
  1872. children = cursor.fetchall()
  1873. for child in children:
  1874. child['children'] = []
  1875. return jsonify({"success": True, "children": children})
  1876. finally:
  1877. conn.close()
  1878. @app.route('/manager/api/save_relation', methods=['POST'])
  1879. def save_relation():
  1880. if 'user_id' not in session:
  1881. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1882. data = request.json
  1883. source_mid = data.get('source_mid') # The member being dragged
  1884. target_mid = data.get('target_mid') # The member being dropped onto
  1885. rel_type = int(data.get('relation_type'))
  1886. sub_rel_type = int(data.get('sub_relation_type', 0))
  1887. if not source_mid or not target_mid or not rel_type:
  1888. return jsonify({"success": False, "message": "参数不完整"}), 400
  1889. conn = get_db_connection()
  1890. try:
  1891. with conn.cursor() as cursor:
  1892. # 简单处理:如果是父子/母子关系
  1893. # target_mid 是父辈,source_mid 是子辈
  1894. parent_mid = target_mid
  1895. child_mid = source_mid
  1896. gen_diff = 1
  1897. if rel_type == 10: # 夫妻
  1898. # 夫妻关系中,我们通常把关联人设为 parent_mid
  1899. parent_mid = target_mid
  1900. child_mid = source_mid
  1901. gen_diff = 0
  1902. elif rel_type in [11, 12]: # 兄弟姐妹
  1903. # 这里逻辑上比较复杂,通常兄弟姐妹有共同父母。
  1904. # 简化处理:暂时存为同级关系 (gen_diff=0)
  1905. parent_mid = target_mid
  1906. child_mid = source_mid
  1907. gen_diff = 0
  1908. # 删除旧关系
  1909. cursor.execute("DELETE FROM family_relation_info WHERE source_mid = %s", (source_mid,))
  1910. # 插入新关系
  1911. sql = """
  1912. INSERT INTO family_relation_info
  1913. (parent_mid, child_mid, relation_type, sub_relation_type, source_mid, generation_diff)
  1914. VALUES (%s, %s, %s, %s, %s, %s)
  1915. """
  1916. cursor.execute(sql, (parent_mid, child_mid, rel_type, sub_rel_type, source_mid, gen_diff))
  1917. conn.commit()
  1918. return jsonify({"success": True, "message": "关系已保存"})
  1919. except Exception as e:
  1920. return jsonify({"success": False, "message": str(e)}), 500
  1921. finally:
  1922. conn.close()
  1923. @app.route('/manager/api/members')
  1924. def get_members():
  1925. if 'user_id' not in session:
  1926. return jsonify({"success": False, "message": "Unauthorized"}), 401
  1927. page = int(request.args.get('page', 1))
  1928. search = request.args.get('search', '')
  1929. per_page = 10
  1930. offset = (page - 1) * per_page
  1931. conn = get_db_connection()
  1932. try:
  1933. with conn.cursor() as cursor:
  1934. # Count total members
  1935. if search:
  1936. cursor.execute("SELECT COUNT(*) as total FROM family_member_info WHERE name LIKE %s OR simplified_name LIKE %s",
  1937. (f'%{search}%', f'%{search}%'))
  1938. else:
  1939. cursor.execute("SELECT COUNT(*) as total FROM family_member_info")
  1940. total_result = cursor.fetchone()
  1941. total = total_result['total'] if total_result else 0
  1942. # Get members for current page with father information
  1943. if search:
  1944. cursor.execute("""
  1945. SELECT
  1946. fmi.id, fmi.name, fmi.simplified_name, fmi.sex, fmi.name_word_generation,
  1947. father.name as father_name, father.simplified_name as father_simplified_name, father.name_word_generation as father_generation
  1948. FROM family_member_info fmi
  1949. LEFT JOIN family_relation_info fri ON fmi.id = fri.child_mid AND fri.relation_type IN (1, 2)
  1950. LEFT JOIN family_member_info father ON fri.parent_mid = father.id
  1951. WHERE fmi.name LIKE %s OR fmi.simplified_name LIKE %s
  1952. LIMIT %s OFFSET %s
  1953. """, (f'%{search}%', f'%{search}%', per_page, offset))
  1954. else:
  1955. cursor.execute("""
  1956. SELECT
  1957. fmi.id, fmi.name, fmi.simplified_name, fmi.sex, fmi.name_word_generation,
  1958. father.name as father_name, father.simplified_name as father_simplified_name, father.name_word_generation as father_generation
  1959. FROM family_member_info fmi
  1960. LEFT JOIN family_relation_info fri ON fmi.id = fri.child_mid AND fri.relation_type IN (1, 2)
  1961. LEFT JOIN family_member_info father ON fri.parent_mid = father.id
  1962. LIMIT %s OFFSET %s
  1963. """, (per_page, offset))
  1964. members = cursor.fetchall()
  1965. # Convert to list of dictionaries if needed
  1966. members_list = []
  1967. for member in members:
  1968. members_list.append({
  1969. 'id': member['id'],
  1970. 'name': member['name'],
  1971. 'simplified_name': member['simplified_name'],
  1972. 'sex': member['sex'],
  1973. 'name_word_generation': member.get('name_word_generation'),
  1974. 'father_name': member.get('father_name'),
  1975. 'father_simplified_name': member.get('father_simplified_name'),
  1976. 'father_generation': member.get('father_generation')
  1977. })
  1978. return jsonify({"success": True, "members": members_list, "total": total})
  1979. except Exception as e:
  1980. return jsonify({"success": False, "message": f"获取成员失败: {e}"}), 500
  1981. finally:
  1982. conn.close()
  1983. def call_doubao_api(prompt, image_url=None):
  1984. """调用豆包API处理文本"""
  1985. api_key = "a1800657-9212-4afe-9b7c-b49f015c54d3"
  1986. api_url = "https://ark.cn-beijing.volces.com/api/v3/responses"
  1987. payload = {
  1988. "model": "doubao-seed-1-8-251228",
  1989. "stream": False,
  1990. "input": [
  1991. {
  1992. "role": "user",
  1993. "content": [
  1994. {"type": "input_text", "text": prompt}
  1995. ]
  1996. }
  1997. ]
  1998. }
  1999. headers = {
  2000. "Authorization": f"Bearer {api_key}",
  2001. "Content-Type": "application/json"
  2002. }
  2003. try:
  2004. response = requests.post(
  2005. api_url,
  2006. json=payload,
  2007. headers=headers,
  2008. timeout=120,
  2009. verify=False,
  2010. proxies={"http": None, "https": None}
  2011. )
  2012. if response.status_code == 200:
  2013. result = response.json()
  2014. print(f"[AI API] Raw response: {result}")
  2015. # 解析响应 - 尝试多种格式
  2016. if 'output' in result:
  2017. for item in result['output']:
  2018. if item.get('type') == 'message':
  2019. content = item.get('content')
  2020. if isinstance(content, str):
  2021. return content
  2022. elif isinstance(content, list):
  2023. for part in content:
  2024. if isinstance(part, dict) and part.get('type') == 'text':
  2025. return part.get('text', '')
  2026. elif isinstance(content, dict) and 'text' in content:
  2027. return content.get('text', '')
  2028. # 尝试其他响应格式
  2029. if 'choices' in result and len(result['choices']) > 0:
  2030. message = result['choices'][0].get('message', {})
  2031. return message.get('content', '')
  2032. # 尝试直接获取文本内容
  2033. if 'text' in result:
  2034. return result['text']
  2035. # 尝试获取响应中的message
  2036. if 'message' in result:
  2037. msg = result['message']
  2038. if isinstance(msg, str):
  2039. return msg
  2040. elif isinstance(msg, dict) and 'content' in msg:
  2041. return msg['content']
  2042. # 返回字符串形式
  2043. return str(result)
  2044. else:
  2045. print(f"[AI API] Error: {response.status_code} - {response.text}")
  2046. return None
  2047. except Exception as e:
  2048. print(f"[AI API] Exception: {e}")
  2049. return None
  2050. def parse_ai_response(ai_response):
  2051. """解析AI响应,提取族谱原文"""
  2052. if not ai_response:
  2053. return None, None
  2054. # 尝试从响应中提取JSON
  2055. try:
  2056. # 移除可能的markdown代码块标记
  2057. text = ai_response.strip()
  2058. if text.startswith('```json'):
  2059. text = text[7:]
  2060. if text.endswith('```'):
  2061. text = text[:-3]
  2062. text = text.strip()
  2063. # 尝试解析JSON
  2064. result = json.loads(text)
  2065. traditional = result.get('genealogy_traditional', '')
  2066. simplified = result.get('genealogy_simplified', '')
  2067. if traditional or simplified:
  2068. return traditional, simplified
  2069. except json.JSONDecodeError:
  2070. print(f"[AI Parse] JSON decode error: {ai_response[:200]}")
  2071. # 如果JSON解析失败,尝试直接提取文本
  2072. # 尝试匹配模式
  2073. import re
  2074. traditional_match = re.search(r'genealogy_traditional["\']?\s*[,:]\s*["\']([^"\']+)["\']', ai_response)
  2075. simplified_match = re.search(r'genealogy_simplified["\']?\s*[,:]\s*["\']([^"\']+)["\']', ai_response)
  2076. traditional = traditional_match.group(1) if traditional_match else ''
  2077. simplified = simplified_match.group(1) if simplified_match else ''
  2078. return traditional, simplified
  2079. @app.route('/manager/api/members/empty_genealogy', methods=['GET'])
  2080. def get_members_empty_genealogy():
  2081. """获取族谱原文为空的成员列表"""
  2082. if 'user_id' not in session:
  2083. return jsonify({"success": False, "message": "Unauthorized"}), 401
  2084. page = int(request.args.get('page', 1))
  2085. per_page = int(request.args.get('per_page', 20))
  2086. offset = (page - 1) * per_page
  2087. conn = get_db_connection()
  2088. try:
  2089. with conn.cursor() as cursor:
  2090. # Count total
  2091. cursor.execute("""
  2092. SELECT COUNT(*) as total
  2093. FROM family_member_info
  2094. WHERE (genealogy_original_traditional IS NULL OR genealogy_original_traditional = '' OR genealogy_original_traditional = 'None')
  2095. AND (genealogy_original_simplified IS NULL OR genealogy_original_simplified = '' OR genealogy_original_simplified = 'None')
  2096. """)
  2097. total_result = cursor.fetchone()
  2098. total = total_result['total'] if total_result else 0
  2099. # Get members
  2100. cursor.execute("""
  2101. SELECT id, name, simplified_name, name_word_generation, sex, occupation, notes, birth_place
  2102. FROM family_member_info
  2103. WHERE (genealogy_original_traditional IS NULL OR genealogy_original_traditional = '' OR genealogy_original_traditional = 'None')
  2104. AND (genealogy_original_simplified IS NULL OR genealogy_original_simplified = '' OR genealogy_original_simplified = 'None')
  2105. LIMIT %s OFFSET %s
  2106. """, (per_page, offset))
  2107. members = cursor.fetchall()
  2108. # 关联查询父亲信息
  2109. member_list = []
  2110. for member in members:
  2111. cursor.execute("""
  2112. SELECT p.name, p.simplified_name, p.name_word_generation
  2113. FROM family_relation_info r
  2114. JOIN family_member_info p ON r.parent_mid = p.id
  2115. WHERE r.child_mid = %s AND r.relation_type = 1
  2116. LIMIT 1
  2117. """, (member['id'],))
  2118. father = cursor.fetchone()
  2119. cursor.execute("""
  2120. SELECT p.name, p.simplified_name
  2121. FROM family_relation_info r
  2122. JOIN family_member_info p ON r.parent_mid = p.id
  2123. WHERE r.child_mid = %s AND r.relation_type = 2
  2124. LIMIT 1
  2125. """, (member['id'],))
  2126. mother = cursor.fetchone()
  2127. member_list.append({
  2128. 'id': member['id'],
  2129. 'name': member['name'],
  2130. 'simplified_name': member['simplified_name'],
  2131. 'name_word_generation': member['name_word_generation'],
  2132. 'sex': member['sex'],
  2133. 'occupation': member['occupation'],
  2134. 'notes': member['notes'],
  2135. 'birth_place': member['birth_place'],
  2136. 'father_name': father['name'] if father else None,
  2137. 'father_simplified_name': father['simplified_name'] if father else None,
  2138. 'father_generation': father['name_word_generation'] if father else None,
  2139. 'mother_name': mother['name'] if mother else None,
  2140. 'mother_simplified_name': mother['simplified_name'] if mother else None
  2141. })
  2142. return jsonify({"success": True, "members": member_list, "total": total})
  2143. except Exception as e:
  2144. return jsonify({"success": False, "message": f"获取成员失败: {e}"}), 500
  2145. finally:
  2146. conn.close()
  2147. @app.route('/manager/api/members/batch_process_genealogy', methods=['POST'])
  2148. def batch_process_genealogy():
  2149. """批量处理成员族谱原文"""
  2150. if 'user_id' not in session:
  2151. return jsonify({"success": False, "message": "Unauthorized"}), 401
  2152. data = request.get_json()
  2153. member_ids = data.get('member_ids', [])
  2154. if not member_ids or len(member_ids) > 10:
  2155. return jsonify({"success": False, "message": "请选择1-10个成员进行处理"}), 400
  2156. conn = get_db_connection()
  2157. results = []
  2158. try:
  2159. for member_id in member_ids:
  2160. with conn.cursor() as cursor:
  2161. cursor.execute("""
  2162. SELECT id, name, simplified_name, name_word_generation,
  2163. birth_place, occupation, notes, sex
  2164. FROM family_member_info WHERE id = %s
  2165. """, (member_id,))
  2166. member = cursor.fetchone()
  2167. # 获取父亲信息
  2168. cursor.execute("""
  2169. SELECT p.name, p.simplified_name
  2170. FROM family_relation_info r
  2171. JOIN family_member_info p ON r.parent_mid = p.id
  2172. WHERE r.child_mid = %s AND r.relation_type = 1
  2173. LIMIT 1
  2174. """, (member_id,))
  2175. father = cursor.fetchone()
  2176. # 获取母亲信息
  2177. cursor.execute("""
  2178. SELECT p.name, p.simplified_name
  2179. FROM family_relation_info r
  2180. JOIN family_member_info p ON r.parent_mid = p.id
  2181. WHERE r.child_mid = %s AND r.relation_type = 2
  2182. LIMIT 1
  2183. """, (member_id,))
  2184. mother = cursor.fetchone()
  2185. member['father_name'] = father['name'] if father else None
  2186. member['father_simplified_name'] = father['simplified_name'] if father else None
  2187. member['mother_name'] = mother['name'] if mother else None
  2188. member['mother_simplified_name'] = mother['simplified_name'] if mother else None
  2189. if not member:
  2190. results.append({"member_id": member_id, "success": False, "message": "成员不存在"})
  2191. continue
  2192. # 构建AI提示词
  2193. member_info = f"""
  2194. 姓名(繁体):{member['name']}
  2195. 姓名(简体):{member['simplified_name'] or '未知'}
  2196. 世系世代:{member['name_word_generation'] or '未知'}
  2197. 父亲姓名:{member['father_name'] or '未知'}
  2198. 母亲姓名:{member['mother_name'] or '未知'}
  2199. 出生地:{member['birth_place'] or '未知'}
  2200. 职业:{member['occupation'] or '未知'}
  2201. 备注:{member['notes'] or '无'}
  2202. """
  2203. prompt = f"""
  2204. 请根据以下人员信息,模拟生成该人员的族谱原文:
  2205. {member_info}
  2206. 请输出两个字段:
  2207. 1. genealogy_traditional: 族谱原文(繁体中文,模仿传统族谱格式)
  2208. 2. genealogy_simplified: 族谱原文(简体中文,将繁体转换为简体)
  2209. 请严格按照JSON格式输出,不要包含任何额外解释:
  2210. {{
  2211. "genealogy_traditional": "繁体族谱原文内容",
  2212. "genealogy_simplified": "简体族谱原文内容"
  2213. }}
  2214. """
  2215. ai_response = call_doubao_api(prompt)
  2216. print(f"[AI Response] Member {member_id}: {ai_response}")
  2217. if ai_response:
  2218. # 使用新的解析函数
  2219. traditional, simplified = parse_ai_response(ai_response)
  2220. if traditional or simplified:
  2221. with conn.cursor() as cursor:
  2222. cursor.execute("""
  2223. UPDATE family_member_info
  2224. SET genealogy_original_traditional = %s,
  2225. genealogy_original_simplified = %s
  2226. WHERE id = %s
  2227. """, (traditional, simplified, member_id))
  2228. conn.commit()
  2229. results.append({
  2230. "member_id": member_id,
  2231. "name": member['name'],
  2232. "success": True,
  2233. "traditional": traditional[:100] + "..." if len(traditional) > 100 else traditional,
  2234. "simplified": simplified[:100] + "..." if len(simplified) > 100 else simplified
  2235. })
  2236. else:
  2237. results.append({
  2238. "member_id": member_id,
  2239. "name": member['name'],
  2240. "success": False,
  2241. "message": "AI未返回有效数据"
  2242. })
  2243. else:
  2244. results.append({
  2245. "member_id": member_id,
  2246. "name": member['name'],
  2247. "success": False,
  2248. "message": "AI调用失败"
  2249. })
  2250. return jsonify({"success": True, "results": results})
  2251. except Exception as e:
  2252. print(f"[Batch Process] Exception: {e}")
  2253. return jsonify({"success": False, "message": f"批量处理失败: {e}"}), 500
  2254. finally:
  2255. conn.close()
  2256. @app.route('/manager/api/member/<int:member_id>')
  2257. def get_member(member_id):
  2258. if 'user_id' not in session:
  2259. return jsonify({"success": False, "message": "Unauthorized"}), 401
  2260. conn = get_db_connection()
  2261. try:
  2262. with conn.cursor() as cursor:
  2263. cursor.execute("SELECT id, name, name_word_generation, source_record_id FROM family_member_info WHERE id = %s", (member_id,))
  2264. member = cursor.fetchone()
  2265. if not member:
  2266. return jsonify({"success": False, "message": "成员不存在"}), 404
  2267. return jsonify({"member": member})
  2268. except Exception as e:
  2269. return jsonify({"success": False, "message": f"获取成员失败: {e}"}), 500
  2270. finally:
  2271. conn.close()
  2272. @app.route('/manager/api/check_relations', methods=['POST'])
  2273. def check_relations():
  2274. if 'user_id' not in session:
  2275. return jsonify({"success": False, "message": "Unauthorized"}), 401
  2276. data = request.json
  2277. people = data.get('people', [])
  2278. if not people:
  2279. return jsonify({"success": False, "matches": {}})
  2280. conn = get_db_connection()
  2281. matches = {}
  2282. try:
  2283. with conn.cursor() as cursor:
  2284. # Collect all father names and spouse names to query
  2285. names_to_check = set()
  2286. for p in people:
  2287. if p.get('father_name'): names_to_check.add(p['father_name'])
  2288. if p.get('spouse_name'): names_to_check.add(p['spouse_name'])
  2289. if not names_to_check:
  2290. return jsonify({"success": True, "matches": {}})
  2291. # Query DB
  2292. format_strings = ','.join(['%s'] * len(names_to_check))
  2293. if names_to_check:
  2294. 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)
  2295. cursor.execute(sql, tuple(names_to_check) * 2)
  2296. results = cursor.fetchall()
  2297. else:
  2298. results = []
  2299. # Organize by name
  2300. db_map = {} # name -> [list of members]
  2301. for r in results:
  2302. # Add under 'name' (Traditional/Old Simplified)
  2303. if r['name'] not in db_map: db_map[r['name']] = []
  2304. db_map[r['name']].append(r)
  2305. # Add under 'simplified_name' if exists
  2306. if r.get('simplified_name'):
  2307. sname = r['simplified_name']
  2308. if sname not in db_map: db_map[sname] = []
  2309. # Avoid duplicates if simplified_name is same as name?
  2310. # The list might contain same object reference, which is fine.
  2311. if sname != r['name']:
  2312. db_map[sname].append(r)
  2313. # Build matches for each input person
  2314. for index, p in enumerate(people):
  2315. p_match = {}
  2316. # Check Father
  2317. fname = p.get('father_name')
  2318. if fname and fname in db_map:
  2319. candidates = db_map[fname]
  2320. # Filter: Father should be Male usually, and older than child (if birthday available)
  2321. valid_fathers = [c for c in candidates if c['sex'] == 1]
  2322. if valid_fathers:
  2323. p_match['father'] = valid_fathers # Return all candidates
  2324. # Check Spouse
  2325. sname = p.get('spouse_name')
  2326. if sname and sname in db_map:
  2327. candidates = db_map[sname]
  2328. # Filter: Spouse usually opposite sex
  2329. target_sex = 1 if p.get('sex') == '女' else 2
  2330. valid_spouses = [c for c in candidates if c['sex'] == target_sex]
  2331. if valid_spouses:
  2332. p_match['spouse'] = valid_spouses
  2333. if p_match:
  2334. matches[index] = p_match
  2335. return jsonify({"success": True, "matches": matches})
  2336. finally:
  2337. conn.close()
  2338. @app.route('/manager/api/upload_reference', methods=['POST'])
  2339. def api_upload_reference():
  2340. """新增成员时上传参考件(无需 member_id)"""
  2341. if 'user_id' not in session:
  2342. return jsonify({"success": False, "message": "未登录"}), 401
  2343. file = request.files.get('file')
  2344. try:
  2345. oss_url, file_name = save_reference_image_to_oss(file)
  2346. username = session.get('username', 'genealogy')
  2347. return jsonify({
  2348. "success": True,
  2349. "oss_url": add_oss_watermark(oss_url, username),
  2350. "oss_url_raw": oss_url,
  2351. "file_name": file_name,
  2352. })
  2353. except ValueError as e:
  2354. return jsonify({"success": False, "message": str(e)}), 400
  2355. except Exception as e:
  2356. print(f"[Upload Reference] Error: {e}")
  2357. return jsonify({"success": False, "message": str(e)}), 500
  2358. @app.route('/manager/api/member/<int:member_id>/reference', methods=['POST', 'DELETE'])
  2359. def api_member_reference(member_id):
  2360. """编辑成员时上传或删除参考件"""
  2361. if 'user_id' not in session:
  2362. return jsonify({"success": False, "message": "未登录"}), 401
  2363. username = session.get('username', 'genealogy')
  2364. conn = get_db_connection()
  2365. try:
  2366. with conn.cursor() as cursor:
  2367. cursor.execute("SELECT id FROM family_member_info WHERE id = %s", (member_id,))
  2368. if not cursor.fetchone():
  2369. return jsonify({"success": False, "message": "成员不存在"}), 404
  2370. if request.method == 'DELETE':
  2371. cursor.execute("""
  2372. UPDATE family_member_info
  2373. SET reference_oss_url = NULL, reference_file_name = NULL,
  2374. reference_upload_time = NULL, reference_upload_uid = NULL
  2375. WHERE id = %s
  2376. """, (member_id,))
  2377. conn.commit()
  2378. return jsonify({"success": True, "message": "参考件已删除"})
  2379. file = request.files.get('file')
  2380. oss_url, file_name = save_reference_image_to_oss(file, member_id=member_id)
  2381. cursor.execute("""
  2382. UPDATE family_member_info
  2383. SET reference_oss_url = %s, reference_file_name = %s,
  2384. reference_upload_time = %s, reference_upload_uid = %s
  2385. WHERE id = %s
  2386. """, (oss_url, file_name, datetime.now(), session['user_id'], member_id))
  2387. conn.commit()
  2388. return jsonify({
  2389. "success": True,
  2390. "message": "参考件上传成功",
  2391. "oss_url": add_oss_watermark(oss_url, username),
  2392. "oss_url_raw": oss_url,
  2393. "file_name": file_name,
  2394. })
  2395. except ValueError as e:
  2396. return jsonify({"success": False, "message": str(e)}), 400
  2397. except Exception as e:
  2398. conn.rollback()
  2399. print(f"[Member Reference] Error: {e}")
  2400. return jsonify({"success": False, "message": str(e)}), 500
  2401. finally:
  2402. conn.close()
  2403. @app.route('/manager/add_member', methods=['GET', 'POST'])
  2404. def add_member():
  2405. if 'user_id' not in session:
  2406. return redirect(url_for('login'))
  2407. # 获取当前登录用户名
  2408. username = session.get('username', 'genealogy')
  2409. conn = get_db_connection()
  2410. try:
  2411. # Check for source_record_id (from GET or POST)
  2412. source_record_id = normalize_source_record_id(
  2413. request.args.get('record_id') or request.form.get('source_record_id')
  2414. )
  2415. prefilled_content = None
  2416. source_oss_url = None
  2417. if source_record_id:
  2418. with conn.cursor() as cursor:
  2419. cursor.execute("SELECT oss_url, ai_content, ai_status FROM genealogy_records WHERE id = %s", (source_record_id,))
  2420. rec = cursor.fetchone()
  2421. if rec:
  2422. source_oss_url = rec['oss_url']
  2423. # Check ai_status (2 = success)
  2424. if rec['ai_status'] == 2 and rec['ai_content']:
  2425. prefilled_content = rec['ai_content']
  2426. if request.method == 'POST':
  2427. # 处理生日转换为 Unix 时间戳
  2428. birthday_str = request.form.get('birthday')
  2429. birthday_ts = 0
  2430. if birthday_str:
  2431. try:
  2432. birthday_ts = int(datetime.strptime(birthday_str, '%Y-%m-%d').timestamp())
  2433. except ValueError:
  2434. birthday_ts = 0
  2435. # 关系数据 - 支持多条关系
  2436. relations = []
  2437. # Parse relations from form data
  2438. i = 0
  2439. while True:
  2440. parent_mid = request.form.get(f'relations[{i}][parent_mid]')
  2441. rel_type = request.form.get(f'relations[{i}][relation_type]')
  2442. sub_rel_type = request.form.get(f'relations[{i}][sub_relation_type]', '0')
  2443. child_order_raw = request.form.get(f'relations[{i}][child_order]', '')
  2444. if not parent_mid or not rel_type:
  2445. break
  2446. child_order = int(child_order_raw) if child_order_raw.strip().isdigit() else None
  2447. relations.append({
  2448. 'parent_mid': int(parent_mid),
  2449. 'relation_type': int(rel_type),
  2450. 'sub_relation_type': int(sub_rel_type),
  2451. 'child_order': child_order
  2452. })
  2453. i += 1
  2454. # For backward compatibility, check old-style single relation
  2455. if not relations:
  2456. related_mid = request.form.get('related_mid')
  2457. relation_type = request.form.get('relation_type')
  2458. if related_mid and relation_type:
  2459. child_order_raw = request.form.get('child_order', '')
  2460. child_order = int(child_order_raw) if child_order_raw.strip().isdigit() else None
  2461. relations.append({
  2462. 'parent_mid': int(related_mid),
  2463. 'relation_type': int(relation_type),
  2464. 'sub_relation_type': int(request.form.get('sub_relation_type', '0')),
  2465. 'child_order': child_order
  2466. })
  2467. # 年龄校验逻辑
  2468. for rel in relations:
  2469. if rel['relation_type'] in [1, 2]: # 1:父子 2:母子
  2470. with conn.cursor() as cursor:
  2471. cursor.execute("SELECT name, birthday FROM family_member_info WHERE id = %s", (rel['parent_mid'],))
  2472. parent = cursor.fetchone()
  2473. if parent and parent['birthday'] > 0 and birthday_ts > 0:
  2474. if birthday_ts < parent['birthday']:
  2475. error_msg = f"数据冲突:成员年龄不能比其父亲/母亲({parent['name']})大,请检查并修正出生日期。"
  2476. flash(error_msg)
  2477. # Re-fetch data for rendering
  2478. cursor.execute("SELECT id, name FROM family_member_info ORDER BY name")
  2479. all_members = cursor.fetchall()
  2480. cursor.execute("SELECT * FROM genealogy_records ORDER BY page_number ASC")
  2481. images = cursor.fetchall()
  2482. # 为图片URL添加水印
  2483. for img in images:
  2484. if img.get('oss_url'):
  2485. img['oss_url'] = add_oss_watermark(img['oss_url'], username)
  2486. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  2487. return jsonify({
  2488. "success": False,
  2489. "message": error_msg
  2490. }), 400
  2491. selected_member_name = ''
  2492. return render_template('add_member.html', all_members=all_members, images=images,
  2493. prefilled_content=prefilled_content, source_oss_url=source_oss_url, source_record_id=source_record_id, selected_member_name=selected_member_name)
  2494. break
  2495. # 获取表单数据
  2496. data = {
  2497. 'name': request.form['name'],
  2498. 'simplified_name': request.form.get('simplified_name'),
  2499. 'genealogy_original_traditional': request.form.get('genealogy_original_traditional'),
  2500. 'genealogy_original_simplified': request.form.get('genealogy_original_simplified'),
  2501. 'former_name': request.form.get('former_name'),
  2502. 'childhood_name': request.form.get('childhood_name'),
  2503. 'name_word': request.form.get('name_word'),
  2504. 'name_word_generation': ';'.join([g.strip() for g in request.form.getlist('lineage_generations[]') if g.strip()]),
  2505. 'name_title': request.form.get('name_title'),
  2506. 'sex': request.form['sex'],
  2507. 'birthday': birthday_ts,
  2508. 'is_pass_away': request.form.get('is_pass_away', 0),
  2509. 'marital_status': request.form.get('marital_status', 0),
  2510. 'birth_place': request.form.get('birth_place'),
  2511. 'branch_family_hall': request.form.get('branch_family_hall'),
  2512. 'cluster_place': request.form.get('cluster_place'),
  2513. 'nation': request.form.get('nation'),
  2514. 'residential_address': request.form.get('residential_address'),
  2515. 'phone': request.form.get('phone'),
  2516. 'mail': request.form.get('mail'),
  2517. 'wechat_account': request.form.get('wechat_account'),
  2518. 'id_number': request.form.get('id_number'),
  2519. 'occupation': request.form.get('occupation'),
  2520. 'educational': request.form.get('educational'),
  2521. 'blood_type': request.form.get('blood_type'),
  2522. 'religion': request.form.get('religion'),
  2523. 'hobbies': request.form.get('hobbies'),
  2524. 'personal_achievements': request.form.get('personal_achievements'),
  2525. 'family_rank': request.form.get('family_rank'),
  2526. 'tags': request.form.get('tags'),
  2527. 'notes': request.form.get('notes'),
  2528. 'suspected_error': request.form.get('suspected_error').strip() if request.form.get('suspected_error') else '',
  2529. 'source_record_id': normalize_source_record_id(request.form.get('source_record_id') or None),
  2530. 'create_uid': session['user_id'] # 记录当前操作人
  2531. }
  2532. apply_reference_from_form(data, request.form, session, is_update=False)
  2533. # ... (rest of logic) ...
  2534. with conn.cursor() as cursor:
  2535. print(f"[Add Member] Inserting member data: {data}")
  2536. fields = ", ".join(data.keys())
  2537. placeholders = ", ".join(["%s"] * len(data))
  2538. sql = f"INSERT INTO family_member_info ({fields}) VALUES ({placeholders})"
  2539. print(f"[Add Member] Executing SQL: {sql}")
  2540. print(f"[Add Member] SQL parameters: {list(data.values())}")
  2541. cursor.execute(sql, list(data.values()))
  2542. member_id = cursor.lastrowid
  2543. print(f"[Add Member] Inserted member with ID: {member_id}")
  2544. # 录入关系(支持多条)
  2545. sql_relation = """
  2546. INSERT INTO family_relation_info
  2547. (parent_mid, child_mid, relation_type, sub_relation_type, source_mid, generation_diff, child_order)
  2548. VALUES (%s, %s, %s, %s, %s, %s, %s)
  2549. """
  2550. for rel in relations:
  2551. rel_type = rel['relation_type']
  2552. parent_mid = rel['parent_mid']
  2553. sub_relation_type = rel['sub_relation_type']
  2554. child_order = rel.get('child_order') if rel_type in [1, 2] else None
  2555. gen_diff = 1 if rel_type in [1, 2] else 0
  2556. 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}")
  2557. cursor.execute(sql_relation, (parent_mid, member_id, rel_type, sub_relation_type, member_id, gen_diff, child_order))
  2558. # Update AI Record Status if applicable
  2559. source_record_id = data.get('source_record_id')
  2560. source_index = request.form.get('source_index')
  2561. if source_record_id and source_index and source_index.isdigit():
  2562. try:
  2563. idx = int(source_index)
  2564. print(f"[Add Member] Updating AI record status: record_id={source_record_id}, index={idx}")
  2565. cursor.execute("SELECT ai_content FROM genealogy_records WHERE id = %s FOR UPDATE", (source_record_id,))
  2566. rec = cursor.fetchone()
  2567. if rec and rec['ai_content']:
  2568. import json
  2569. content = json.loads(rec['ai_content'])
  2570. # Ensure content is a list (it might be a dict if single object, though we try to normalize)
  2571. if isinstance(content, dict):
  2572. content = [content]
  2573. if isinstance(content, list):
  2574. updated = False
  2575. if 0 <= idx < len(content):
  2576. # Always update the status regardless of current value
  2577. content[idx]['is_imported'] = True
  2578. content[idx]['imported_member_id'] = member_id
  2579. updated = True
  2580. if updated:
  2581. new_content = json.dumps(content, ensure_ascii=False)
  2582. cursor.execute("UPDATE genealogy_records SET ai_content = %s WHERE id = %s", (new_content, source_record_id))
  2583. print(f"[Add Member] Updated AI record status")
  2584. except Exception as e:
  2585. print(f"[Add Member] Error updating AI content status: {e}")
  2586. print(f"[Add Member] Committing transaction")
  2587. if safe_commit(conn):
  2588. print(f"[Add Member] Transaction committed successfully")
  2589. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  2590. return jsonify({"success": True, "message": "成员录入成功", "member_id": member_id})
  2591. flash('成员录入成功')
  2592. return redirect(url_for('members'))
  2593. else:
  2594. print(f"[Add Member] Transaction commit failed!")
  2595. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  2596. return jsonify({"success": False, "message": "成员录入失败,事务提交失败"}), 500
  2597. flash('成员录入失败,事务提交失败')
  2598. return redirect(url_for('add_member'))
  2599. with conn.cursor() as cursor:
  2600. cursor.execute("SELECT id, name FROM family_member_info ORDER BY name")
  2601. all_members = cursor.fetchall()
  2602. cursor.execute("SELECT * FROM genealogy_records ORDER BY page_number ASC")
  2603. images = cursor.fetchall()
  2604. # 为图片URL添加水印
  2605. for img in images:
  2606. if img.get('oss_url'):
  2607. img['oss_url'] = add_oss_watermark(img['oss_url'], username)
  2608. except Exception as e:
  2609. flash(f'发生错误: {e}')
  2610. all_members = []
  2611. images = []
  2612. finally:
  2613. conn.close()
  2614. selected_member_name = ''
  2615. return render_template('add_member.html', all_members=all_members, images=images,
  2616. prefilled_content=prefilled_content, source_oss_url=source_oss_url, source_record_id=source_record_id, selected_member_name=selected_member_name)
  2617. @app.route('/manager/edit_member/<int:member_id>', methods=['GET', 'POST'])
  2618. def edit_member(member_id):
  2619. if 'user_id' not in session:
  2620. return redirect(url_for('login'))
  2621. # 获取当前登录用户名
  2622. username = session.get('username', 'genealogy')
  2623. conn = get_db_connection()
  2624. try:
  2625. if request.method == 'POST':
  2626. birthday_str = request.form.get('birthday')
  2627. birthday_ts = 0
  2628. if birthday_str:
  2629. try:
  2630. birthday_ts = int(datetime.strptime(birthday_str, '%Y-%m-%d').timestamp())
  2631. except ValueError:
  2632. birthday_ts = 0
  2633. # 关系数据 - 支持多条关系
  2634. relations = []
  2635. i = 0
  2636. while True:
  2637. parent_mid = request.form.get(f'relations[{i}][parent_mid]')
  2638. rel_type = request.form.get(f'relations[{i}][relation_type]')
  2639. sub_rel_type = request.form.get(f'relations[{i}][sub_relation_type]', '0')
  2640. child_order_raw = request.form.get(f'relations[{i}][child_order]', '')
  2641. if not parent_mid or not rel_type:
  2642. break
  2643. child_order = int(child_order_raw) if child_order_raw.strip().isdigit() else None
  2644. relations.append({
  2645. 'parent_mid': int(parent_mid),
  2646. 'relation_type': int(rel_type),
  2647. 'sub_relation_type': int(sub_rel_type),
  2648. 'child_order': child_order,
  2649. })
  2650. i += 1
  2651. # For backward compatibility
  2652. if not relations:
  2653. related_mid = request.form.get('related_mid')
  2654. relation_type = request.form.get('relation_type')
  2655. if related_mid and relation_type:
  2656. child_order_raw = request.form.get('child_order', '')
  2657. relations.append({
  2658. 'parent_mid': int(related_mid),
  2659. 'relation_type': int(relation_type),
  2660. 'sub_relation_type': int(request.form.get('sub_relation_type', '0')),
  2661. 'child_order': int(child_order_raw) if child_order_raw.strip().isdigit() else None,
  2662. })
  2663. # 年龄校验逻辑
  2664. for rel in relations:
  2665. if rel['relation_type'] in [1, 2]:
  2666. with conn.cursor() as cursor:
  2667. cursor.execute("SELECT name, birthday FROM family_member_info WHERE id = %s", (rel['parent_mid'],))
  2668. parent = cursor.fetchone()
  2669. if parent and parent['birthday'] > 0 and birthday_ts > 0:
  2670. if birthday_ts < parent['birthday']:
  2671. flash(f"数据冲突:成员年龄不能比其父亲/母亲({parent['name']})大,请检查并修正出生日期。")
  2672. # 重新加载编辑页所需数据
  2673. cursor.execute("SELECT * FROM family_member_info WHERE id = %s", (member_id,))
  2674. member = cursor.fetchone()
  2675. member['birthday_date'] = birthday_str # 保持用户输入
  2676. cursor.execute("SELECT id, name FROM family_member_info WHERE id != %s ORDER BY name", (member_id,))
  2677. all_members = cursor.fetchall()
  2678. cursor.execute("SELECT * FROM genealogy_records ORDER BY page_number ASC")
  2679. images = cursor.fetchall()
  2680. # 为图片URL添加水印
  2681. for img in images:
  2682. if img.get('oss_url'):
  2683. img['oss_url'] = add_oss_watermark(img['oss_url'], username)
  2684. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  2685. return jsonify({
  2686. "success": False,
  2687. "message": f"数据冲突:成员年龄不能比其父亲/母亲({parent['name']})大,请检查并修正出生日期。"
  2688. }), 400
  2689. selected_member_name = ''
  2690. if member:
  2691. clear_invalid_member_scan_fields(member)
  2692. 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))
  2693. break
  2694. data = {
  2695. 'name': request.form['name'],
  2696. 'simplified_name': request.form.get('simplified_name'),
  2697. 'genealogy_original_traditional': request.form.get('genealogy_original_traditional'),
  2698. 'genealogy_original_simplified': request.form.get('genealogy_original_simplified'),
  2699. 'former_name': request.form.get('former_name'),
  2700. 'childhood_name': request.form.get('childhood_name'),
  2701. 'name_word': request.form.get('name_word'),
  2702. 'name_word_generation': ';'.join([g.strip() for g in request.form.getlist('lineage_generations[]') if g.strip()]),
  2703. 'name_title': request.form.get('name_title'),
  2704. 'sex': request.form['sex'],
  2705. 'birthday': birthday_ts,
  2706. 'is_pass_away': request.form.get('is_pass_away', 0),
  2707. 'marital_status': request.form.get('marital_status', 0),
  2708. 'birth_place': request.form.get('birth_place'),
  2709. 'branch_family_hall': request.form.get('branch_family_hall'),
  2710. 'cluster_place': request.form.get('cluster_place'),
  2711. 'nation': request.form.get('nation'),
  2712. 'residential_address': request.form.get('residential_address'),
  2713. 'phone': request.form.get('phone'),
  2714. 'mail': request.form.get('mail'),
  2715. 'wechat_account': request.form.get('wechat_account'),
  2716. 'id_number': request.form.get('id_number'),
  2717. 'occupation': request.form.get('occupation'),
  2718. 'educational': request.form.get('educational'),
  2719. 'blood_type': request.form.get('blood_type'),
  2720. 'religion': request.form.get('religion'),
  2721. 'hobbies': request.form.get('hobbies'),
  2722. 'personal_achievements': request.form.get('personal_achievements'),
  2723. 'family_rank': request.form.get('family_rank'),
  2724. 'tags': request.form.get('tags'),
  2725. 'notes': request.form.get('notes'),
  2726. 'suspected_error': request.form.get('suspected_error').strip() if request.form.get('suspected_error') else '',
  2727. 'source_record_id': normalize_source_record_id(request.form.get('source_record_id') or None),
  2728. 'create_uid': session['user_id'] # 记录当前操作人
  2729. }
  2730. apply_reference_from_form(data, request.form, session, is_update=True)
  2731. with conn.cursor() as cursor:
  2732. print(f"[Edit Member] Updating member data: {data}")
  2733. update_parts = [f"{k} = %s" for k in data.keys()]
  2734. sql = f"UPDATE family_member_info SET {', '.join(update_parts)} WHERE id = %s"
  2735. print(f"[Edit Member] Executing SQL: {sql}")
  2736. print(f"[Edit Member] SQL parameters: {list(data.values()) + [member_id]}")
  2737. cursor.execute(sql, list(data.values()) + [member_id])
  2738. print(f"[Edit Member] Updated member with ID: {member_id}")
  2739. # 更新关系(支持多条)
  2740. print(f"[Edit Member] Deleting existing relations for member ID: {member_id}")
  2741. cursor.execute("DELETE FROM family_relation_info WHERE source_mid = %s", (member_id,))
  2742. sql_relation = """
  2743. INSERT INTO family_relation_info
  2744. (parent_mid, child_mid, relation_type, sub_relation_type, source_mid, generation_diff, child_order)
  2745. VALUES (%s, %s, %s, %s, %s, %s, %s)
  2746. """
  2747. for rel in relations:
  2748. rel_type = rel['relation_type']
  2749. parent_mid = rel['parent_mid']
  2750. sub_relation_type = rel['sub_relation_type']
  2751. child_order = rel.get('child_order') if rel_type in [1, 2] else None
  2752. gen_diff = 1 if rel_type in [1, 2] else 0
  2753. 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}")
  2754. cursor.execute(sql_relation, (parent_mid, member_id, rel_type, sub_relation_type, member_id, gen_diff, child_order))
  2755. # Update AI Record Status if applicable
  2756. source_record_id = data.get('source_record_id')
  2757. source_index = request.form.get('source_index')
  2758. if source_record_id and source_index and source_index.isdigit():
  2759. try:
  2760. idx = int(source_index)
  2761. print(f"[Edit Member] Updating AI record status: record_id={source_record_id}, index={idx}")
  2762. cursor.execute("SELECT ai_content FROM genealogy_records WHERE id = %s FOR UPDATE", (source_record_id,))
  2763. rec = cursor.fetchone()
  2764. if rec and rec['ai_content']:
  2765. import json
  2766. content = json.loads(rec['ai_content'])
  2767. if isinstance(content, dict):
  2768. content = [content]
  2769. if isinstance(content, list):
  2770. updated = False
  2771. if 0 <= idx < len(content):
  2772. # Always update the status regardless of current value
  2773. content[idx]['is_imported'] = True
  2774. content[idx]['imported_member_id'] = member_id
  2775. updated = True
  2776. if updated:
  2777. new_content = json.dumps(content, ensure_ascii=False)
  2778. cursor.execute("UPDATE genealogy_records SET ai_content = %s WHERE id = %s", (new_content, source_record_id))
  2779. print(f"[Edit Member] Updated AI record status")
  2780. except Exception as e:
  2781. print(f"[Edit Member] Error updating AI content status: {e}")
  2782. print(f"[Edit Member] Committing transaction")
  2783. conn.commit()
  2784. print(f"[Edit Member] Transaction committed successfully")
  2785. if request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.is_json:
  2786. return jsonify({"success": True, "message": "成员信息更新成功"})
  2787. flash('成员信息更新成功')
  2788. return redirect(url_for('members'))
  2789. with conn.cursor() as cursor:
  2790. cursor.execute("SELECT * FROM family_member_info WHERE id = %s", (member_id,))
  2791. member = cursor.fetchone()
  2792. if not member:
  2793. flash('成员不存在')
  2794. return redirect(url_for('members'))
  2795. # 格式化日期供显示
  2796. if member.get('birthday'):
  2797. member['birthday_date'] = format_timestamp(member['birthday'])
  2798. # 获取现有关系(支持多条)
  2799. cursor.execute("SELECT * FROM family_relation_info WHERE source_mid = %s ORDER BY id", (member_id,))
  2800. relations = cursor.fetchall()
  2801. cursor.execute("SELECT id, name FROM family_member_info WHERE id != %s ORDER BY name", (member_id,))
  2802. all_members = cursor.fetchall()
  2803. cursor.execute("SELECT * FROM genealogy_records ORDER BY page_number ASC")
  2804. images = cursor.fetchall()
  2805. # 为图片URL添加水印
  2806. for img in images:
  2807. if img.get('oss_url'):
  2808. img['oss_url'] = add_oss_watermark(img['oss_url'], username)
  2809. if member.get('reference_oss_url'):
  2810. member['reference_image_url'] = add_oss_watermark(member['reference_oss_url'], username)
  2811. finally:
  2812. conn.close()
  2813. # Calculate selected_member_names based on relations
  2814. selected_member_names = []
  2815. if relations:
  2816. for rel in relations:
  2817. if rel.get('parent_mid'):
  2818. for m in all_members:
  2819. if m['id'] == rel['parent_mid']:
  2820. selected_member_names.append(m['name'])
  2821. break
  2822. else:
  2823. selected_member_names.append('')
  2824. else:
  2825. selected_member_names.append('')
  2826. # Get source_record_id from member data
  2827. if member:
  2828. clear_invalid_member_scan_fields(member)
  2829. source_record_id = normalize_source_record_id(member.get('source_record_id') if member else None)
  2830. 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)
  2831. @app.route('/manager/member_detail/<int:member_id>')
  2832. def member_detail(member_id):
  2833. if 'user_id' not in session:
  2834. return redirect(url_for('login'))
  2835. # 获取当前登录用户名
  2836. username = session.get('username', 'genealogy')
  2837. conn = get_db_connection()
  2838. try:
  2839. with conn.cursor() as cursor:
  2840. # Join with genealogy_records to get source image info
  2841. sql = """
  2842. SELECT m.*, r.oss_url as source_image_url, r.page_number as source_page,
  2843. r.genealogy_version, r.genealogy_source, r.upload_person
  2844. FROM family_member_info m
  2845. LEFT JOIN genealogy_records r ON m.source_record_id = r.id AND m.source_record_id != %s
  2846. WHERE m.id = %s
  2847. """
  2848. cursor.execute(sql, (INVALID_SOURCE_RECORD_ID, member_id))
  2849. member = cursor.fetchone()
  2850. if not member:
  2851. flash('成员不存在')
  2852. return redirect(url_for('members'))
  2853. clear_invalid_member_scan_fields(member)
  2854. # 为图片URL添加水印
  2855. if member.get('source_image_url'):
  2856. member['source_image_url'] = add_oss_watermark(member['source_image_url'], username)
  2857. if member.get('reference_oss_url'):
  2858. member['reference_image_url'] = add_oss_watermark(member['reference_oss_url'], username)
  2859. member['birthday_str'] = format_timestamp(member.get('birthday'))
  2860. # 获取关系(包含子类型和第几子)
  2861. cursor.execute("""
  2862. SELECT m.id, m.name, m.simplified_name, r.relation_type, r.sub_relation_type, r.child_order
  2863. FROM family_relation_info r
  2864. JOIN family_member_info m ON r.parent_mid = m.id
  2865. WHERE r.child_mid = %s
  2866. """, (member_id,))
  2867. parents = cursor.fetchall()
  2868. cursor.execute("""
  2869. SELECT m.id, m.name, m.simplified_name, r.relation_type, r.sub_relation_type, r.child_order
  2870. FROM family_relation_info r
  2871. JOIN family_member_info m ON r.child_mid = m.id
  2872. WHERE r.parent_mid = %s
  2873. ORDER BY COALESCE(r.child_order, 99999), m.id
  2874. """, (member_id,))
  2875. children = cursor.fetchall()
  2876. # 计算入继说明:若该成员有 sub_relation_type=3(养父母)记录,
  2877. # 则从 sub_relation_type=2(生父母)记录中取排行,生成"由xxx公第N子入继"
  2878. _order_labels = {1:'长', 2:'次', 3:'三', 4:'四', 5:'五',
  2879. 6:'六', 7:'七', 8:'八', 9:'九', 10:'十'}
  2880. adopt_info = None
  2881. is_adopted_in = any(p['sub_relation_type'] == 3 for p in parents)
  2882. if is_adopted_in:
  2883. bio = next((p for p in parents if p['sub_relation_type'] == 2), None)
  2884. if bio:
  2885. bio_name = bio['simplified_name'] or bio['name']
  2886. order = bio['child_order']
  2887. order_str = _order_labels.get(order, f'第{order}') if order else '某'
  2888. adopt_info = f"由{bio_name}公{order_str}子入继"
  2889. finally:
  2890. conn.close()
  2891. return render_template('member_detail.html', member=member, parents=parents,
  2892. children=children, adopt_info=adopt_info)
  2893. @app.route('/manager/delete_member/<int:member_id>', methods=['POST'])
  2894. def delete_member(member_id):
  2895. if 'user_id' not in session:
  2896. return jsonify({"success": False, "message": "Unauthorized"}), 401
  2897. conn = get_db_connection()
  2898. try:
  2899. with conn.cursor() as cursor:
  2900. # 1. 删除关系表中关联该成员的所有记录
  2901. cursor.execute("DELETE FROM family_relation_info WHERE parent_mid = %s OR child_mid = %s OR source_mid = %s",
  2902. (member_id, member_id, member_id))
  2903. # 2. 删除成员本身
  2904. cursor.execute("DELETE FROM family_member_info WHERE id = %s", (member_id,))
  2905. conn.commit()
  2906. flash('成员及其关系已成功删除')
  2907. return redirect(url_for('members'))
  2908. except Exception as e:
  2909. conn.rollback()
  2910. flash(f'删除失败: {e}')
  2911. return redirect(url_for('members'))
  2912. finally:
  2913. conn.close()
  2914. @app.route('/manager/home')
  2915. def home():
  2916. """Home page - Dashboard for the genealogy management system"""
  2917. if 'user_id' not in session:
  2918. return redirect(url_for('login'))
  2919. # Force re-login if is_super_admin not set in session (fresh login required)
  2920. if 'is_super_admin' not in session:
  2921. session.clear()
  2922. flash('请重新登录以获取最新权限')
  2923. return redirect(url_for('login'))
  2924. conn = get_db_connection()
  2925. try:
  2926. with conn.cursor() as cursor:
  2927. # Get member count
  2928. cursor.execute("SELECT COUNT(*) as count FROM family_member_info")
  2929. member_count = cursor.fetchone()['count']
  2930. # Get record count
  2931. cursor.execute("SELECT COUNT(*) as count FROM genealogy_records")
  2932. record_count = cursor.fetchone()['count']
  2933. # Get PDF count
  2934. cursor.execute("SELECT COUNT(*) as count FROM genealogy_pdfs")
  2935. pdf_count = cursor.fetchone()['count']
  2936. # Get suspected error count
  2937. cursor.execute("SELECT COUNT(*) as count FROM family_member_info WHERE suspected_error IS NOT NULL AND TRIM(suspected_error) != ''")
  2938. error_count = cursor.fetchone()['count']
  2939. finally:
  2940. conn.close()
  2941. return render_template('home.html',
  2942. member_count=member_count,
  2943. record_count=record_count,
  2944. pdf_count=pdf_count,
  2945. error_count=error_count)
  2946. @app.route('/manager/login', methods=['GET', 'POST'])
  2947. def login():
  2948. if request.method == 'POST':
  2949. username = request.form['username']
  2950. password = request.form['password']
  2951. try:
  2952. conn = get_db_connection()
  2953. try:
  2954. with conn.cursor() as cursor:
  2955. cursor.execute("SELECT * FROM users WHERE username=%s AND password=%s", (username, password))
  2956. user = cursor.fetchone()
  2957. if user:
  2958. session['user_id'] = user['id']
  2959. session['username'] = user['username']
  2960. session['is_super_admin'] = user.get('is_super_admin', 0) == 1
  2961. return redirect(url_for('home'))
  2962. else:
  2963. flash('用户名或密码错误')
  2964. finally:
  2965. conn.close()
  2966. except Exception as e:
  2967. flash(f'数据库连接错误: {str(e)}')
  2968. print(f'Login error: {str(e)}')
  2969. return render_template('login.html')
  2970. @app.route('/manager/logout')
  2971. def logout():
  2972. session.clear()
  2973. return redirect(url_for('login'))
  2974. @app.route('/manager/api/check_name')
  2975. def check_name():
  2976. if 'user_id' not in session:
  2977. return jsonify({"success": False, "message": "Unauthorized"}), 401
  2978. name = request.args.get('name', '').strip()
  2979. if not name:
  2980. return jsonify({"success": True, "exists": False})
  2981. conn = get_db_connection()
  2982. try:
  2983. with conn.cursor() as cursor:
  2984. # Check for name or simplified_name match
  2985. 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))
  2986. matches = cursor.fetchall()
  2987. if matches:
  2988. # Format birthday for display
  2989. for m in matches:
  2990. if m.get('birthday'):
  2991. m['birthday_str'] = format_timestamp(m['birthday'])
  2992. else:
  2993. m['birthday_str'] = '未知'
  2994. return jsonify({"success": True, "exists": True, "matches": matches})
  2995. else:
  2996. return jsonify({"success": True, "exists": False})
  2997. except Exception as e:
  2998. return jsonify({"success": False, "error": str(e)}), 500
  2999. finally:
  3000. conn.close()
  3001. import requests
  3002. import json
  3003. import re
  3004. @app.route('/manager/api/recognize_image', methods=['POST'])
  3005. def recognize_image():
  3006. if 'user_id' not in session:
  3007. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3008. data = request.json
  3009. image_url = data.get('image_url')
  3010. if not image_url:
  3011. return jsonify({"success": False, "message": "No image URL provided"}), 400
  3012. api_key = "a1800657-9212-4afe-9b7c-b49f015c54d3"
  3013. api_url = "https://ark.cn-beijing.volces.com/api/v3/responses"
  3014. prompt = """
  3015. 请分析这张家谱图片,提取其中关于人物的信息。
  3016. 请务必将繁体字转换为简体字(original_name 字段除外)。
  3017. 特别注意:'name' 字段必须是纯简体中文,不能包含繁体字(例如:'學'应转换为'学','劉'应转换为'刘','萬'应转换为'万')。
  3018. 请提取以下字段(如果存在):
  3019. - original_name: 原始姓名(严格保持图片上的繁体字,不做任何修改或转换)
  3020. - name: 简体姓名(必须转换为简体中文,去除不需要的敬称)
  3021. - sex: 性别(男/女)
  3022. - birthday: 出生日期(尝试转换为YYYY-MM-DD格式,如果无法确定年份可只填月日)
  3023. - death_date: 逝世日期(如文本中出现“殁”、“葬”、“卒”等字眼及其对应的时间,请提取)
  3024. - father_name: 父亲姓名
  3025. - spouse_name: 配偶姓名
  3026. - generation: 第几世/代数
  3027. - name_word: 字辈(例如名字为“学勤公”,“学”为字辈;提取名字中的字辈信息)
  3028. - education: 学历/功名
  3029. - title: 官职/称号
  3030. 请严格以JSON列表格式返回,不要包含Markdown代码块标记(如 ```json ... ```),直接返回JSON数组。
  3031. 如果包含多个人物,请都提取出来。
  3032. """
  3033. ai_payload_url = get_normalized_base64_image(image_url)
  3034. payload = {
  3035. "model": "doubao-seed-1-8-251228",
  3036. "stream": True,
  3037. "input": [
  3038. {
  3039. "role": "user",
  3040. "content": [
  3041. {
  3042. "type": "input_image",
  3043. "image_url": ai_payload_url
  3044. },
  3045. {
  3046. "type": "input_text",
  3047. "text": prompt
  3048. }
  3049. ]
  3050. }
  3051. ]
  3052. }
  3053. headers = {
  3054. "Authorization": f"Bearer {api_key}",
  3055. "Content-Type": "application/json"
  3056. }
  3057. def generate():
  3058. yield "正在连接 AI 服务...\n"
  3059. try:
  3060. # 使用 stream=True, timeout=120
  3061. # 增加 verify=False 以防 SSL 问题(开发环境)
  3062. # 增加 proxies=None 以防本地代理干扰
  3063. with requests.post(
  3064. api_url,
  3065. json=payload,
  3066. headers=headers,
  3067. stream=True,
  3068. timeout=1200,
  3069. verify=False,
  3070. proxies={"http": None, "https": None}
  3071. ) as r:
  3072. if r.status_code != 200:
  3073. yield f"Error: API returned status code {r.status_code}. Response: {r.text}"
  3074. return
  3075. yield "连接成功,正在等待 AI 响应...\n"
  3076. full_reasoning = ""
  3077. json_started = False
  3078. for line in r.iter_lines():
  3079. if line:
  3080. line_str = line.decode('utf-8')
  3081. if line_str.startswith('data: '):
  3082. json_str = line_str[6:]
  3083. if json_str.strip() == '[DONE]':
  3084. break
  3085. try:
  3086. chunk = json.loads(json_str)
  3087. # 处理 standard OpenAI choices format (content)
  3088. if 'choices' in chunk and len(chunk['choices']) > 0:
  3089. delta = chunk['choices'][0].get('delta', {})
  3090. if 'content' in delta:
  3091. if not json_started:
  3092. yield "|||JSON_START|||"
  3093. json_started = True
  3094. yield delta['content']
  3095. # 处理 standard OpenAI choices format (reasoning_content) if any
  3096. if 'reasoning_content' in delta:
  3097. yield f"\n[推理]: {delta['reasoning_content']}"
  3098. # 处理 Doubao/Volcano specific formats
  3099. # Type: response.reasoning_summary_text.delta
  3100. if chunk.get('type') == 'response.reasoning_summary_text.delta':
  3101. if 'delta' in chunk:
  3102. yield chunk['delta']
  3103. # Type: response.text.delta
  3104. if chunk.get('type') == 'response.text.delta':
  3105. if 'delta' in chunk:
  3106. if not json_started:
  3107. yield "|||JSON_START|||"
  3108. json_started = True
  3109. yield chunk['delta']
  3110. # Type: response.output_item.added (May contain initial content or status)
  3111. # Type: response.reasoning_summary_part.added
  3112. except Exception as e:
  3113. print(f"Chunk parse error: {e}")
  3114. else:
  3115. # 尝试直接解析非 data: 开头的行
  3116. try:
  3117. chunk = json.loads(line_str)
  3118. if 'choices' in chunk and len(chunk['choices']) > 0:
  3119. content = chunk['choices'][0]['message']['content']
  3120. yield content
  3121. except:
  3122. pass
  3123. except Exception as e:
  3124. yield f"\n[Error: {str(e)}]"
  3125. return Response(stream_with_context(generate()), mimetype='text/plain')
  3126. @app.route('/manager/api/start_analysis/<int:record_id>', methods=['POST'])
  3127. def start_analysis(record_id):
  3128. if 'user_id' not in session:
  3129. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3130. conn = get_db_connection()
  3131. try:
  3132. with conn.cursor() as cursor:
  3133. # Check if record exists
  3134. cursor.execute("SELECT oss_url, ai_status FROM genealogy_records WHERE id = %s", (record_id,))
  3135. record = cursor.fetchone()
  3136. if not record:
  3137. return jsonify({"success": False, "message": "Record not found"}), 404
  3138. # Update status to processing (1)
  3139. cursor.execute("UPDATE genealogy_records SET ai_status = 1 WHERE id = %s", (record_id,))
  3140. conn.commit()
  3141. # Start background task
  3142. threading.Thread(target=process_ai_task, args=(record_id, record['oss_url'])).start()
  3143. return jsonify({"success": True, "message": "Analysis started"})
  3144. except Exception as e:
  3145. return jsonify({"success": False, "message": str(e)}), 500
  3146. finally:
  3147. conn.close()
  3148. def process_files_background(upload_folder, saved_files, manual_page, suggested_page, genealogy_version, genealogy_source, upload_person):
  3149. current_suggested_page = int(manual_page) if manual_page and str(manual_page).isdigit() else suggested_page
  3150. ensure_pdf_table()
  3151. for item in saved_files:
  3152. if len(item) >= 4:
  3153. filename, file_path, file_page, original_filename = item[0], item[1], item[2], item[3]
  3154. elif len(item) == 3:
  3155. filename, file_path, file_page = item
  3156. original_filename = filename
  3157. else:
  3158. filename, file_path = item[0], item[1]
  3159. file_page = None
  3160. original_filename = filename
  3161. try:
  3162. if filename.lower().endswith('.pdf'):
  3163. import uuid
  3164. display_pdf_name = (original_filename or filename).strip() or filename
  3165. oss_pdf_name = secure_filename(display_pdf_name)
  3166. if not oss_pdf_name or not oss_pdf_name.lower().endswith('.pdf'):
  3167. oss_pdf_name = f"genealogy_pdf_{uuid.uuid4().hex[:8]}.pdf"
  3168. pdf_oss_url = upload_to_oss(file_path, custom_filename=oss_pdf_name)
  3169. if pdf_oss_url:
  3170. desc_parts = []
  3171. if genealogy_version:
  3172. desc_parts.append(genealogy_version)
  3173. if genealogy_source:
  3174. desc_parts.append(genealogy_source)
  3175. pdf_description = ' · '.join(desc_parts) if desc_parts else ''
  3176. conn_pdf = get_db_connection()
  3177. try:
  3178. with conn_pdf.cursor() as cursor:
  3179. cursor.execute(
  3180. "INSERT INTO genealogy_pdfs (file_name, oss_url, description, uploader) VALUES (%s, %s, %s, %s)",
  3181. (display_pdf_name, pdf_oss_url, pdf_description, upload_person or '')
  3182. )
  3183. conn_pdf.commit()
  3184. except Exception as pdf_meta_e:
  3185. print(f"Error inserting genealogy_pdfs for {display_pdf_name}: {pdf_meta_e}")
  3186. finally:
  3187. conn_pdf.close()
  3188. else:
  3189. print(f"Warning: full PDF upload to OSS failed for {filename}, scan pages will still be processed.")
  3190. doc = fitz.open(file_path)
  3191. for page_index in range(len(doc)):
  3192. img_path = None
  3193. try:
  3194. page = doc.load_page(page_index)
  3195. max_dim = max(page.rect.width, page.rect.height)
  3196. zoom = 2000 / max_dim if max_dim > 0 else 2.0
  3197. if zoom > 2.5: zoom = 2.5
  3198. mat = fitz.Matrix(zoom, zoom)
  3199. # Use get_pixmap with matrix directly
  3200. pix = page.get_pixmap(matrix=mat)
  3201. final_page = current_suggested_page
  3202. if genealogy_version and genealogy_source:
  3203. if final_page is not None and str(final_page).strip() != '':
  3204. img_filename = f"{genealogy_version}_{genealogy_source}_{final_page}.jpg"
  3205. else:
  3206. img_filename = f"{genealogy_version}_{genealogy_source}.jpg"
  3207. else:
  3208. img_filename = f"{os.path.splitext(filename)[0]}_page_{page_index+1}.jpg"
  3209. img_path = os.path.join(upload_folder, img_filename)
  3210. # Save the pixmap to the image path
  3211. pix.save(img_path)
  3212. oss_url = upload_to_oss(img_path, custom_filename=img_filename)
  3213. if oss_url:
  3214. conn = get_db_connection()
  3215. try:
  3216. with conn.cursor() as cursor:
  3217. sql = """INSERT INTO genealogy_records
  3218. (file_name, oss_url, page_number, ai_status, genealogy_version, genealogy_source, upload_person, file_type)
  3219. VALUES (%s, %s, %s, 1, %s, %s, %s, %s)"""
  3220. cursor.execute(sql, (img_filename, oss_url, final_page, genealogy_version, genealogy_source, upload_person, 'PDF'))
  3221. record_id = cursor.lastrowid
  3222. conn.commit()
  3223. threading.Thread(target=process_ai_task, args=(record_id, oss_url)).start()
  3224. current_suggested_page += 1
  3225. finally:
  3226. conn.close()
  3227. except Exception as page_e:
  3228. print(f"Error processing page {page_index} of {filename}: {page_e}")
  3229. finally:
  3230. if img_path and os.path.exists(img_path):
  3231. try:
  3232. os.remove(img_path)
  3233. except:
  3234. pass
  3235. doc.close()
  3236. else:
  3237. img_path = compress_image_if_needed(file_path)
  3238. # Use explicitly set page number if provided, otherwise extract from filename or auto-increment
  3239. if file_page and str(file_page).isdigit():
  3240. final_page = int(file_page)
  3241. current_suggested_page = final_page + 1
  3242. page_num = final_page
  3243. else:
  3244. page_num = extract_page_number(img_path)
  3245. final_page = page_num if page_num else current_suggested_page
  3246. ext = os.path.splitext(img_path)[1]
  3247. if genealogy_version and genealogy_source:
  3248. if final_page is not None and str(final_page).strip() != '':
  3249. img_filename = f"{genealogy_version}_{genealogy_source}_{final_page}{ext}"
  3250. else:
  3251. img_filename = f"{genealogy_version}_{genealogy_source}{ext}"
  3252. else:
  3253. img_filename = os.path.basename(img_path)
  3254. oss_url = upload_to_oss(img_path, custom_filename=img_filename)
  3255. if oss_url:
  3256. conn = get_db_connection()
  3257. try:
  3258. with conn.cursor() as cursor:
  3259. sql = """INSERT INTO genealogy_records
  3260. (file_name, oss_url, page_number, ai_status, genealogy_version, genealogy_source, upload_person, file_type)
  3261. VALUES (%s, %s, %s, 1, %s, %s, %s, %s)"""
  3262. cursor.execute(sql, (img_filename, oss_url, final_page, genealogy_version, genealogy_source, upload_person, '图片'))
  3263. record_id = cursor.lastrowid
  3264. conn.commit()
  3265. threading.Thread(target=process_ai_task, args=(record_id, oss_url)).start()
  3266. if page_num:
  3267. current_suggested_page = page_num + 1
  3268. else:
  3269. current_suggested_page += 1
  3270. finally:
  3271. conn.close()
  3272. if img_path and img_path != file_path and os.path.exists(img_path):
  3273. try:
  3274. os.remove(img_path)
  3275. except:
  3276. pass
  3277. except Exception as e:
  3278. print(f"Error processing file {filename}: {e}")
  3279. finally:
  3280. if os.path.exists(file_path):
  3281. try:
  3282. os.remove(file_path)
  3283. except:
  3284. pass
  3285. @app.route('/manager/upload', methods=['GET', 'POST'])
  3286. def upload():
  3287. if 'user_id' not in session:
  3288. return redirect(url_for('login'))
  3289. # 获取建议页码 (当前最大页码 + 1)
  3290. conn = get_db_connection()
  3291. suggested_page = 1
  3292. try:
  3293. with conn.cursor() as cursor:
  3294. cursor.execute("SELECT MAX(page_number) as max_p FROM genealogy_records")
  3295. result = cursor.fetchone()
  3296. if result and result['max_p']:
  3297. suggested_page = result['max_p'] + 1
  3298. finally:
  3299. conn.close()
  3300. if request.method == 'POST':
  3301. if 'file' not in request.files:
  3302. flash('未选择文件')
  3303. return redirect(request.url)
  3304. files = request.files.getlist('file')
  3305. if not files or files[0].filename == '':
  3306. flash('未选择文件')
  3307. return redirect(request.url)
  3308. manual_page = request.form.get('manual_page')
  3309. genealogy_version = request.form.get('genealogy_version', '')
  3310. genealogy_source = request.form.get('genealogy_source', '')
  3311. upload_person = request.form.get('upload_person', '')
  3312. if not upload_person:
  3313. upload_person = session.get('username', '')
  3314. import uuid
  3315. saved_files = []
  3316. for i, file in enumerate(files):
  3317. if not file or not file.filename:
  3318. continue
  3319. original_filename = file.filename
  3320. ext = os.path.splitext(original_filename)[1].lower()
  3321. base_name = secure_filename(original_filename)
  3322. # If secure_filename removes all characters (e.g., pure Chinese name) or just leaves 'pdf'
  3323. if not base_name or base_name == ext.strip('.'):
  3324. filename = f"upload_{uuid.uuid4().hex[:8]}{ext}"
  3325. else:
  3326. # Ensure the extension is preserved
  3327. if not base_name.lower().endswith(ext):
  3328. filename = f"{base_name}{ext}"
  3329. else:
  3330. filename = base_name
  3331. file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
  3332. file.save(file_path)
  3333. # Fetch individual page number if it exists
  3334. file_page = request.form.get(f'page_number_{i}')
  3335. saved_files.append((filename, file_path, file_page, original_filename))
  3336. if saved_files:
  3337. threading.Thread(
  3338. target=process_files_background,
  3339. args=(app.config['UPLOAD_FOLDER'], saved_files, manual_page, suggested_page, genealogy_version, genealogy_source, upload_person)
  3340. ).start()
  3341. flash('上传完成,AI解析中,稍后查看')
  3342. time.sleep(1.5)
  3343. return redirect(url_for('index'))
  3344. return render_template('upload.html', suggested_page=suggested_page)
  3345. @app.route('/manager/save_upload', methods=['POST'])
  3346. def save_upload():
  3347. if 'user_id' not in session: return redirect(url_for('login'))
  3348. filename = request.form.get('filename')
  3349. oss_url = request.form.get('oss_url')
  3350. page_number = request.form.get('page_number')
  3351. genealogy_version = request.form.get('genealogy_version', '')
  3352. genealogy_source = request.form.get('genealogy_source', '')
  3353. upload_person = request.form.get('upload_person', session.get('username', ''))
  3354. file_type = request.form.get('file_type', '图片')
  3355. if not oss_url or not page_number:
  3356. flash('页码不能为空')
  3357. return redirect(url_for('upload'))
  3358. conn = get_db_connection()
  3359. try:
  3360. with conn.cursor() as cursor:
  3361. sql = """INSERT INTO genealogy_records
  3362. (file_name, oss_url, page_number, ai_status, genealogy_version, genealogy_source, upload_person, file_type)
  3363. VALUES (%s, %s, %s, 1, %s, %s, %s, %s)"""
  3364. cursor.execute(sql, (filename, oss_url, page_number, genealogy_version, genealogy_source, upload_person, file_type))
  3365. record_id = cursor.lastrowid
  3366. conn.commit()
  3367. # Start AI Task
  3368. threading.Thread(target=process_ai_task, args=(record_id, oss_url)).start()
  3369. flash('上传完成,AI解析中,稍后查看')
  3370. except Exception as e:
  3371. flash(f'保存失败: {e}')
  3372. finally:
  3373. conn.close()
  3374. return redirect(url_for('index'))
  3375. @app.route('/manager/delete_upload/<int:record_id>', methods=['POST'])
  3376. def delete_upload(record_id):
  3377. if 'user_id' not in session:
  3378. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3379. conn = get_db_connection()
  3380. try:
  3381. with conn.cursor() as cursor:
  3382. # 删除记录
  3383. cursor.execute("DELETE FROM genealogy_records WHERE id = %s", (record_id,))
  3384. conn.commit()
  3385. flash('文件记录已成功删除')
  3386. return redirect(url_for('index'))
  3387. except Exception as e:
  3388. conn.rollback()
  3389. flash(f'删除失败: {e}')
  3390. return redirect(url_for('index'))
  3391. finally:
  3392. conn.close()
  3393. @app.route('/manager/upload_pdf', methods=['GET', 'POST'])
  3394. def upload_pdf():
  3395. if 'user_id' not in session:
  3396. return redirect(url_for('login'))
  3397. if request.method == 'GET':
  3398. return render_template('upload_pdf.html')
  3399. # POST请求处理
  3400. if 'file' not in request.files:
  3401. flash('请选择要上传的PDF文件')
  3402. return redirect(request.url)
  3403. file = request.files['file']
  3404. if file.filename == '':
  3405. flash('请选择要上传的PDF文件')
  3406. return redirect(request.url)
  3407. # 检查文件类型
  3408. if not file.filename.lower().endswith('.pdf'):
  3409. flash('只支持PDF文件上传')
  3410. return redirect(request.url)
  3411. # 获取表单数据
  3412. version_name = request.form.get('version_name', '').strip()
  3413. version_source = request.form.get('version_source', '').strip()
  3414. file_provider = request.form.get('file_provider', '').strip()
  3415. # 验证必填字段
  3416. if not version_name:
  3417. flash('版本名称为必填项')
  3418. return redirect(request.url)
  3419. if not version_source:
  3420. flash('版本来源为必填项')
  3421. return redirect(request.url)
  3422. # 如果未提供文件提供人,使用当前登录用户
  3423. if not file_provider:
  3424. file_provider = session.get('user_id', '未知')
  3425. import uuid
  3426. original_filename = file.filename
  3427. ext = os.path.splitext(original_filename)[1].lower()
  3428. base_name = secure_filename(original_filename)
  3429. if not base_name or base_name == ext.strip('.'):
  3430. filename = f"genealogy_pdf_{uuid.uuid4().hex[:8]}{ext}"
  3431. else:
  3432. if not base_name.lower().endswith(ext):
  3433. filename = f"{base_name}{ext}"
  3434. else:
  3435. filename = base_name
  3436. file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
  3437. file.save(file_path)
  3438. try:
  3439. # Upload to OSS
  3440. oss_url = upload_to_oss(file_path, custom_filename=filename)
  3441. if not oss_url:
  3442. flash('文件上传失败')
  3443. return redirect(request.url)
  3444. # Save to database
  3445. conn = get_db_connection()
  3446. try:
  3447. with conn.cursor() as cursor:
  3448. cursor.execute(
  3449. "INSERT INTO genealogy_pdfs (file_name, oss_url, version_name, version_source, file_provider, upload_time) VALUES (%s, %s, %s, %s, %s, CURRENT_TIMESTAMP)",
  3450. (original_filename, oss_url, version_name, version_source, file_provider)
  3451. )
  3452. conn.commit()
  3453. flash('PDF文件上传成功')
  3454. return redirect(url_for('pdf_management'))
  3455. except Exception as e:
  3456. flash(f'保存失败: {e}')
  3457. return redirect(request.url)
  3458. finally:
  3459. conn.close()
  3460. finally:
  3461. if os.path.exists(file_path):
  3462. try:
  3463. os.remove(file_path)
  3464. except:
  3465. pass
  3466. def process_pdf_pages(file_path, pdf_oss_url, uploader):
  3467. """Process PDF pages and add them to genealogy records"""
  3468. try:
  3469. import fitz
  3470. doc = fitz.open(file_path)
  3471. # Get current max page number
  3472. conn = get_db_connection()
  3473. suggested_page = 1
  3474. try:
  3475. with conn.cursor() as cursor:
  3476. cursor.execute("SELECT MAX(page_number) as max_p FROM genealogy_records")
  3477. result = cursor.fetchone()
  3478. if result and result['max_p']:
  3479. suggested_page = result['max_p'] + 1
  3480. finally:
  3481. conn.close()
  3482. for page_index in range(len(doc)):
  3483. try:
  3484. page = doc[page_index]
  3485. pix = page.get_pixmap(dpi=150)
  3486. # Save as image
  3487. img_filename = f"{os.path.splitext(os.path.basename(file_path))[0]}_page_{page_index+1}.jpg"
  3488. img_path = os.path.join(app.config['UPLOAD_FOLDER'], img_filename)
  3489. pix.save(img_path)
  3490. # Upload to OSS
  3491. img_oss_url = upload_to_oss(img_path, custom_filename=img_filename)
  3492. if img_oss_url:
  3493. # Save to genealogy_records
  3494. conn = get_db_connection()
  3495. try:
  3496. with conn.cursor() as cursor:
  3497. cursor.execute(
  3498. "INSERT INTO genealogy_records (file_name, oss_url, page_number, ai_status, upload_person, file_type) VALUES (%s, %s, %s, 1, %s, %s)",
  3499. (img_filename, img_oss_url, suggested_page + page_index, uploader, '图片')
  3500. )
  3501. record_id = cursor.lastrowid
  3502. conn.commit()
  3503. # Start AI processing
  3504. threading.Thread(target=process_ai_task, args=(record_id, img_oss_url)).start()
  3505. finally:
  3506. conn.close()
  3507. except Exception as e:
  3508. print(f"Error processing page {page_index+1}: {e}")
  3509. finally:
  3510. if 'img_path' in locals() and os.path.exists(img_path):
  3511. try:
  3512. os.remove(img_path)
  3513. except:
  3514. pass
  3515. except Exception as e:
  3516. print(f"Error processing PDF: {e}")
  3517. # --- Settlement Routes ---
  3518. @app.route('/manager/settlements')
  3519. def settlements():
  3520. if 'user_id' not in session:
  3521. return redirect(url_for('login'))
  3522. return render_template('settlements.html')
  3523. @app.route('/manager/api/settlements', methods=['GET'])
  3524. def get_settlements():
  3525. if 'user_id' not in session:
  3526. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3527. conn = get_db_connection()
  3528. try:
  3529. with conn.cursor() as cursor:
  3530. cursor.execute("""
  3531. SELECT s.*, m.name as representative_name, m.simplified_name as representative_simplified_name
  3532. FROM family_settlements s
  3533. LEFT JOIN family_member_info m ON s.representative_id = m.id
  3534. ORDER BY s.created_at DESC
  3535. """)
  3536. settlements = cursor.fetchall()
  3537. # Convert Decimal to float/int for JSON serialization
  3538. result = []
  3539. for s in settlements:
  3540. item = dict(s)
  3541. if item.get('latitude'):
  3542. item['latitude'] = float(item['latitude'])
  3543. if item.get('longitude'):
  3544. item['longitude'] = float(item['longitude'])
  3545. if item.get('population'):
  3546. item['population'] = int(item['population'])
  3547. result.append(item)
  3548. return jsonify({"success": True, "settlements": result})
  3549. finally:
  3550. conn.close()
  3551. @app.route('/manager/api/settlements/<int:id>', methods=['GET'])
  3552. def get_settlement(id):
  3553. if 'user_id' not in session:
  3554. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3555. conn = get_db_connection()
  3556. try:
  3557. with conn.cursor() as cursor:
  3558. cursor.execute("""
  3559. SELECT s.*, m.name as representative_name, m.simplified_name as representative_simplified_name
  3560. FROM family_settlements s
  3561. LEFT JOIN family_member_info m ON s.representative_id = m.id
  3562. WHERE s.id = %s
  3563. """, (id,))
  3564. settlement = cursor.fetchone()
  3565. if settlement:
  3566. # Convert Decimal to float/int for JSON serialization
  3567. item = dict(settlement)
  3568. if item.get('latitude'):
  3569. item['latitude'] = float(item['latitude'])
  3570. if item.get('longitude'):
  3571. item['longitude'] = float(item['longitude'])
  3572. if item.get('population'):
  3573. item['population'] = int(item['population'])
  3574. return jsonify({"success": True, "settlement": item})
  3575. else:
  3576. return jsonify({"success": False, "message": "聚落不存在"})
  3577. finally:
  3578. conn.close()
  3579. @app.route('/manager/api/settlements', methods=['POST'])
  3580. def add_settlement():
  3581. if 'user_id' not in session:
  3582. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3583. if not session.get('is_super_admin'):
  3584. return jsonify({"success": False, "message": "权限不足"}), 403
  3585. data = request.get_json()
  3586. conn = get_db_connection()
  3587. try:
  3588. with conn.cursor() as cursor:
  3589. cursor.execute("""
  3590. INSERT INTO family_settlements
  3591. (name, region, latitude, longitude, population, representative_id, description, surname_type, new_surname, enthusiastic_members)
  3592. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
  3593. """, (
  3594. data.get('name'),
  3595. data.get('region'),
  3596. data.get('latitude') or None,
  3597. data.get('longitude') or None,
  3598. data.get('population') or 0,
  3599. data.get('representative_id') or None,
  3600. data.get('description'),
  3601. data.get('surname_type') or 0,
  3602. data.get('new_surname') or None,
  3603. data.get('enthusiastic_members') or None
  3604. ))
  3605. conn.commit()
  3606. return jsonify({"success": True, "message": "添加成功"})
  3607. finally:
  3608. conn.close()
  3609. @app.route('/manager/api/settlements/<int:id>', methods=['PUT'])
  3610. def update_settlement(id):
  3611. if 'user_id' not in session:
  3612. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3613. if not session.get('is_super_admin'):
  3614. return jsonify({"success": False, "message": "权限不足"}), 403
  3615. data = request.get_json()
  3616. conn = get_db_connection()
  3617. try:
  3618. with conn.cursor() as cursor:
  3619. cursor.execute("""
  3620. UPDATE family_settlements
  3621. SET name=%s, region=%s, latitude=%s, longitude=%s,
  3622. population=%s, representative_id=%s, description=%s,
  3623. surname_type=%s, new_surname=%s, enthusiastic_members=%s
  3624. WHERE id=%s
  3625. """, (
  3626. data.get('name'),
  3627. data.get('region'),
  3628. data.get('latitude') or None,
  3629. data.get('longitude') or None,
  3630. data.get('population') or 0,
  3631. data.get('representative_id') or None,
  3632. data.get('description'),
  3633. data.get('surname_type') or 0,
  3634. data.get('new_surname') or None,
  3635. data.get('enthusiastic_members') or None,
  3636. id
  3637. ))
  3638. conn.commit()
  3639. return jsonify({"success": True, "message": "更新成功"})
  3640. finally:
  3641. conn.close()
  3642. @app.route('/manager/api/settlements/<int:id>', methods=['DELETE'])
  3643. def delete_settlement(id):
  3644. if 'user_id' not in session:
  3645. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3646. if not session.get('is_super_admin'):
  3647. return jsonify({"success": False, "message": "权限不足"}), 403
  3648. conn = get_db_connection()
  3649. try:
  3650. with conn.cursor() as cursor:
  3651. cursor.execute("DELETE FROM family_settlements WHERE id=%s", (id,))
  3652. conn.commit()
  3653. return jsonify({"success": True, "message": "删除成功"})
  3654. finally:
  3655. conn.close()
  3656. # 异步批量处理族谱原文功能
  3657. import uuid
  3658. def init_batch_task_table():
  3659. """初始化批量任务表(如果不存在)"""
  3660. conn = get_db_connection()
  3661. try:
  3662. with conn.cursor() as cursor:
  3663. cursor.execute("""
  3664. CREATE TABLE IF NOT EXISTS batch_genealogy_task (
  3665. id INT AUTO_INCREMENT PRIMARY KEY,
  3666. task_id VARCHAR(64) UNIQUE NOT NULL,
  3667. user_id INT NOT NULL,
  3668. status VARCHAR(20) DEFAULT 'pending',
  3669. total_count INT DEFAULT 0,
  3670. completed_count INT DEFAULT 0,
  3671. failed_count INT DEFAULT 0,
  3672. last_processed_id INT DEFAULT 0,
  3673. created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  3674. updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  3675. results TEXT
  3676. );
  3677. """)
  3678. # 检查是否存在last_processed_id字段,如果不存在则添加
  3679. cursor.execute("SHOW COLUMNS FROM batch_genealogy_task LIKE 'last_processed_id'")
  3680. if not cursor.fetchone():
  3681. cursor.execute("ALTER TABLE batch_genealogy_task ADD COLUMN last_processed_id INT DEFAULT 0")
  3682. conn.commit()
  3683. print("[Database] batch_genealogy_task table initialized")
  3684. except Exception as e:
  3685. print(f"[Database] Error creating batch_genealogy_task table: {e}")
  3686. finally:
  3687. conn.close()
  3688. # 初始化表
  3689. init_batch_task_table()
  3690. def migrate_child_order_column():
  3691. """为 family_relation_info 表添加 child_order 字段(如不存在)"""
  3692. conn = get_db_connection()
  3693. try:
  3694. with conn.cursor() as cursor:
  3695. cursor.execute("SHOW COLUMNS FROM family_relation_info LIKE 'child_order'")
  3696. if not cursor.fetchone():
  3697. cursor.execute(
  3698. "ALTER TABLE family_relation_info ADD COLUMN child_order INT DEFAULT NULL COMMENT '第几子,用于兄弟排序'"
  3699. )
  3700. conn.commit()
  3701. print("[DB Migrate] Added child_order column to family_relation_info")
  3702. else:
  3703. print("[DB Migrate] child_order column already exists")
  3704. except Exception as e:
  3705. print(f"[DB Migrate] Error adding child_order: {e}")
  3706. finally:
  3707. conn.close()
  3708. migrate_child_order_column()
  3709. def migrate_enthusiastic_members_column():
  3710. """为 family_settlements 表添加 enthusiastic_members 字段(如不存在)"""
  3711. conn = get_db_connection()
  3712. try:
  3713. with conn.cursor() as cursor:
  3714. cursor.execute("SHOW COLUMNS FROM family_settlements LIKE 'enthusiastic_members'")
  3715. if not cursor.fetchone():
  3716. cursor.execute(
  3717. "ALTER TABLE family_settlements ADD COLUMN enthusiastic_members TEXT DEFAULT NULL COMMENT '热心宗亲,多人以逗号分隔'"
  3718. )
  3719. conn.commit()
  3720. print("[DB Migrate] Added enthusiastic_members column to family_settlements")
  3721. else:
  3722. print("[DB Migrate] enthusiastic_members column already exists")
  3723. except Exception as e:
  3724. print(f"[DB Migrate] Error adding enthusiastic_members: {e}")
  3725. finally:
  3726. conn.close()
  3727. migrate_enthusiastic_members_column()
  3728. def migrate_reference_document_columns():
  3729. """为 family_member_info 表添加参考件字段(如不存在)"""
  3730. columns = [
  3731. ("reference_oss_url", "TEXT NULL COMMENT '参考件OSS地址'"),
  3732. ("reference_file_name", "VARCHAR(255) NULL COMMENT '参考件文件名'"),
  3733. ("reference_upload_time", "TIMESTAMP NULL COMMENT '参考件上传时间'"),
  3734. ("reference_upload_uid", "INT NULL COMMENT '参考件上传人ID'"),
  3735. ]
  3736. conn = get_db_connection()
  3737. try:
  3738. with conn.cursor() as cursor:
  3739. for col_name, col_def in columns:
  3740. cursor.execute(f"SHOW COLUMNS FROM family_member_info LIKE '{col_name}'")
  3741. if not cursor.fetchone():
  3742. cursor.execute(f"ALTER TABLE family_member_info ADD COLUMN {col_name} {col_def}")
  3743. print(f"[DB Migrate] Added {col_name} column to family_member_info")
  3744. else:
  3745. print(f"[DB Migrate] {col_name} column already exists")
  3746. conn.commit()
  3747. except Exception as e:
  3748. print(f"[DB Migrate] Error adding reference document columns: {e}")
  3749. finally:
  3750. conn.close()
  3751. migrate_reference_document_columns()
  3752. def async_process_genealogy_task(task_id, member_ids, user_id):
  3753. """异步处理族谱原文任务"""
  3754. results = []
  3755. conn = get_db_connection()
  3756. try:
  3757. # 更新任务状态为处理中
  3758. with conn.cursor() as cursor:
  3759. cursor.execute("""
  3760. UPDATE batch_genealogy_task
  3761. SET status = 'processing', total_count = %s
  3762. WHERE task_id = %s
  3763. """, (len(member_ids), task_id))
  3764. conn.commit()
  3765. completed_count = 0
  3766. failed_count = 0
  3767. for member_id in member_ids:
  3768. try:
  3769. with conn.cursor() as cursor:
  3770. cursor.execute("""
  3771. SELECT id, name, simplified_name, name_word_generation,
  3772. birth_place, occupation, notes, sex
  3773. FROM family_member_info WHERE id = %s
  3774. """, (member_id,))
  3775. member = cursor.fetchone()
  3776. # 获取父亲信息
  3777. cursor.execute("""
  3778. SELECT p.name, p.simplified_name
  3779. FROM family_relation_info r
  3780. JOIN family_member_info p ON r.parent_mid = p.id
  3781. WHERE r.child_mid = %s AND r.relation_type = 1
  3782. LIMIT 1
  3783. """, (member_id,))
  3784. father = cursor.fetchone()
  3785. # 获取母亲信息
  3786. cursor.execute("""
  3787. SELECT p.name, p.simplified_name
  3788. FROM family_relation_info r
  3789. JOIN family_member_info p ON r.parent_mid = p.id
  3790. WHERE r.child_mid = %s AND r.relation_type = 2
  3791. LIMIT 1
  3792. """, (member_id,))
  3793. mother = cursor.fetchone()
  3794. member['father_name'] = father['name'] if father else None
  3795. member['father_simplified_name'] = father['simplified_name'] if father else None
  3796. member['mother_name'] = mother['name'] if mother else None
  3797. member['mother_simplified_name'] = mother['simplified_name'] if mother else None
  3798. except Exception as e:
  3799. print(f"[Async Process] Error getting member {member_id}: {e}")
  3800. results.append({
  3801. "member_id": member_id,
  3802. "name": "未知",
  3803. "success": False,
  3804. "message": f"获取成员信息失败: {e}"
  3805. })
  3806. failed_count += 1
  3807. continue
  3808. if not member:
  3809. results.append({
  3810. "member_id": member_id,
  3811. "name": "未知",
  3812. "success": False,
  3813. "message": "成员不存在"
  3814. })
  3815. failed_count += 1
  3816. continue
  3817. # 构建AI提示词
  3818. member_info = f"""
  3819. 姓名(繁体):{member['name']}
  3820. 姓名(简体):{member['simplified_name'] or '未知'}
  3821. 世系世代:{member['name_word_generation'] or '未知'}
  3822. 父亲姓名:{member['father_name'] or '未知'}
  3823. 母亲姓名:{member['mother_name'] or '未知'}
  3824. 出生地:{member['birth_place'] or '未知'}
  3825. 职业:{member['occupation'] or '未知'}
  3826. 备注:{member['notes'] or '无'}
  3827. """
  3828. prompt = f"""
  3829. 请根据以下人员信息,模拟生成该人员的族谱原文:
  3830. {member_info}
  3831. 请输出两个字段:
  3832. 1. genealogy_traditional: 族谱原文(繁体中文,模仿传统族谱格式)
  3833. 2. genealogy_simplified: 族谱原文(简体中文,将繁体转换为简体)
  3834. 请严格按照JSON格式输出,不要包含任何额外解释:
  3835. {{
  3836. "genealogy_traditional": "繁体族谱原文内容",
  3837. "genealogy_simplified": "简体族谱原文内容"
  3838. }}
  3839. """
  3840. ai_response = call_doubao_api(prompt)
  3841. if ai_response:
  3842. traditional, simplified = parse_ai_response(ai_response)
  3843. if traditional or simplified:
  3844. try:
  3845. with conn.cursor() as cursor:
  3846. cursor.execute("""
  3847. UPDATE family_member_info
  3848. SET genealogy_original_traditional = %s,
  3849. genealogy_original_simplified = %s
  3850. WHERE id = %s
  3851. """, (traditional, simplified, member_id))
  3852. conn.commit()
  3853. results.append({
  3854. "member_id": member_id,
  3855. "name": member['name'],
  3856. "success": True,
  3857. "traditional": traditional[:100] + "..." if len(traditional) > 100 else traditional,
  3858. "simplified": simplified[:100] + "..." if len(simplified) > 100 else simplified
  3859. })
  3860. completed_count += 1
  3861. except Exception as e:
  3862. print(f"[Async Process] Error updating member {member_id}: {e}")
  3863. results.append({
  3864. "member_id": member_id,
  3865. "name": member['name'],
  3866. "success": False,
  3867. "message": f"保存失败: {e}"
  3868. })
  3869. failed_count += 1
  3870. else:
  3871. results.append({
  3872. "member_id": member_id,
  3873. "name": member['name'],
  3874. "success": False,
  3875. "message": "AI未返回有效数据"
  3876. })
  3877. failed_count += 1
  3878. else:
  3879. results.append({
  3880. "member_id": member_id,
  3881. "name": member['name'],
  3882. "success": False,
  3883. "message": "AI调用失败"
  3884. })
  3885. failed_count += 1
  3886. # 更新任务状态
  3887. status = 'completed' if failed_count == 0 else 'completed_with_errors'
  3888. with conn.cursor() as cursor:
  3889. cursor.execute("""
  3890. UPDATE batch_genealogy_task
  3891. SET status = %s, completed_count = %s, failed_count = %s, results = %s
  3892. WHERE task_id = %s
  3893. """, (status, completed_count, failed_count, json.dumps(results, ensure_ascii=False), task_id))
  3894. conn.commit()
  3895. print(f"[Async Process] Task {task_id} completed: {completed_count} success, {failed_count} failed")
  3896. except Exception as e:
  3897. print(f"[Async Process] Error in task {task_id}: {e}")
  3898. with conn.cursor() as cursor:
  3899. cursor.execute("""
  3900. UPDATE batch_genealogy_task
  3901. SET status = 'failed', results = %s
  3902. WHERE task_id = %s
  3903. """, (json.dumps({"error": str(e)}, ensure_ascii=False), task_id))
  3904. conn.commit()
  3905. finally:
  3906. conn.close()
  3907. @app.route('/manager/api/members/batch_process_genealogy_async', methods=['POST'])
  3908. def batch_process_genealogy_async():
  3909. """异步批量处理族谱原文"""
  3910. if 'user_id' not in session:
  3911. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3912. data = request.get_json()
  3913. member_ids = data.get('member_ids', [])
  3914. if not member_ids:
  3915. return jsonify({"success": False, "message": "请选择成员进行处理"}), 400
  3916. # 生成任务ID
  3917. task_id = str(uuid.uuid4())
  3918. # 保存任务到数据库
  3919. conn = get_db_connection()
  3920. try:
  3921. with conn.cursor() as cursor:
  3922. cursor.execute("""
  3923. INSERT INTO batch_genealogy_task (task_id, user_id, status, total_count)
  3924. VALUES (%s, %s, 'pending', %s)
  3925. """, (task_id, session['user_id'], len(member_ids)))
  3926. conn.commit()
  3927. finally:
  3928. conn.close()
  3929. # 启动异步线程处理
  3930. threading.Thread(target=async_process_genealogy_task, args=(task_id, member_ids, session['user_id'])).start()
  3931. return jsonify({
  3932. "success": True,
  3933. "task_id": task_id,
  3934. "message": "任务已创建,正在后台处理中"
  3935. })
  3936. @app.route('/manager/api/members/batch_task_status/<task_id>', methods=['GET'])
  3937. def get_batch_task_status(task_id):
  3938. """获取批量任务状态"""
  3939. if 'user_id' not in session:
  3940. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3941. conn = get_db_connection()
  3942. try:
  3943. with conn.cursor() as cursor:
  3944. cursor.execute("""
  3945. SELECT task_id, status, total_count, completed_count, failed_count,
  3946. created_at, updated_at, results
  3947. FROM batch_genealogy_task
  3948. WHERE task_id = %s AND user_id = %s
  3949. """, (task_id, session['user_id']))
  3950. task = cursor.fetchone()
  3951. if task:
  3952. result = {
  3953. "task_id": task['task_id'],
  3954. "status": task['status'],
  3955. "total_count": task['total_count'],
  3956. "completed_count": task['completed_count'],
  3957. "failed_count": task['failed_count'],
  3958. "created_at": task['created_at'].isoformat() if task['created_at'] else None,
  3959. "updated_at": task['updated_at'].isoformat() if task['updated_at'] else None
  3960. }
  3961. if task['results']:
  3962. try:
  3963. result['results'] = json.loads(task['results'])
  3964. except:
  3965. result['results'] = task['results']
  3966. return jsonify({"success": True, "task": result})
  3967. else:
  3968. return jsonify({"success": False, "message": "任务不存在或无权访问"}), 404
  3969. finally:
  3970. conn.close()
  3971. @app.route('/manager/api/members/batch_tasks', methods=['GET'])
  3972. def get_batch_tasks():
  3973. """获取用户的批量任务列表"""
  3974. if 'user_id' not in session:
  3975. return jsonify({"success": False, "message": "Unauthorized"}), 401
  3976. conn = get_db_connection()
  3977. try:
  3978. with conn.cursor() as cursor:
  3979. cursor.execute("""
  3980. SELECT task_id, status, total_count, completed_count, failed_count,
  3981. last_processed_id, created_at, updated_at
  3982. FROM batch_genealogy_task
  3983. WHERE user_id = %s
  3984. ORDER BY created_at DESC
  3985. LIMIT 20
  3986. """, (session['user_id'],))
  3987. tasks = cursor.fetchall()
  3988. result = []
  3989. for task in tasks:
  3990. result.append({
  3991. "task_id": task['task_id'],
  3992. "status": task['status'],
  3993. "total_count": task['total_count'],
  3994. "completed_count": task['completed_count'],
  3995. "failed_count": task['failed_count'],
  3996. "last_processed_id": task['last_processed_id'],
  3997. "created_at": task['created_at'].isoformat() if task['created_at'] else None,
  3998. "updated_at": task['updated_at'].isoformat() if task['updated_at'] else None
  3999. })
  4000. return jsonify({"success": True, "tasks": result})
  4001. finally:
  4002. conn.close()
  4003. def call_doubao_image_api(image_url, prompt):
  4004. """调用豆包API处理图片,提取文本内容"""
  4005. api_key = "a1800657-9212-4afe-9b7c-b49f015c54d3"
  4006. api_url = "https://ark.cn-beijing.volces.com/api/v3/responses"
  4007. ai_payload_url = get_normalized_base64_image(image_url)
  4008. payload = {
  4009. "model": "doubao-seed-1-8-251228",
  4010. "stream": False,
  4011. "input": [
  4012. {
  4013. "role": "user",
  4014. "content": [
  4015. {"type": "input_image", "image_url": ai_payload_url},
  4016. {"type": "input_text", "text": prompt}
  4017. ]
  4018. }
  4019. ]
  4020. }
  4021. headers = {
  4022. "Authorization": f"Bearer {api_key}",
  4023. "Content-Type": "application/json"
  4024. }
  4025. try:
  4026. response = requests.post(
  4027. api_url,
  4028. json=payload,
  4029. headers=headers,
  4030. timeout=120,
  4031. verify=False,
  4032. proxies={"http": None, "https": None}
  4033. )
  4034. if response.status_code == 200:
  4035. return response.json()
  4036. else:
  4037. print(f"[Image AI API] Error: {response.status_code} - {response.text}")
  4038. return None
  4039. except Exception as e:
  4040. print(f"[Image AI API] Exception: {e}")
  4041. return None
  4042. def extract_pure_text(response):
  4043. """从API响应中提取纯文本内容,优先返回 message 类型的最终答案"""
  4044. if not response:
  4045. return ''
  4046. # 优先从 output 列表中提取 message 类型(最终答案)
  4047. if 'output' in response:
  4048. # 第一遍:只找 message 类型
  4049. for item in response['output']:
  4050. if item.get('type') == 'message':
  4051. content = item.get('content')
  4052. if isinstance(content, str):
  4053. return content
  4054. elif isinstance(content, list):
  4055. text_parts = []
  4056. for part in content:
  4057. if isinstance(part, dict) and part.get('type') == 'text':
  4058. text_parts.append(part.get('text', ''))
  4059. elif isinstance(part, str):
  4060. text_parts.append(part)
  4061. result = ''.join(text_parts)
  4062. if result:
  4063. return result
  4064. # 第二遍:没有 message 时才使用 reasoning 内容作为兜底
  4065. for item in response['output']:
  4066. if item.get('type') == 'reasoning':
  4067. content = item.get('content')
  4068. all_text = ''
  4069. summary = item.get('summary', [])
  4070. for part in summary:
  4071. if isinstance(part, dict):
  4072. if part.get('type') in ('summary_text', 'text'):
  4073. all_text += part.get('text', '')
  4074. elif isinstance(part, str):
  4075. all_text += part
  4076. if isinstance(content, str):
  4077. all_text += content
  4078. elif isinstance(content, list):
  4079. for part in content:
  4080. if isinstance(part, dict) and part.get('type') == 'text':
  4081. all_text += part.get('text', '')
  4082. elif isinstance(part, str):
  4083. all_text += part
  4084. if all_text:
  4085. return all_text
  4086. # 第三遍:content 直接是字符串的情况
  4087. for item in response['output']:
  4088. content = item.get('content')
  4089. if isinstance(content, str) and content:
  4090. return content
  4091. # 尝试从 choices 中提取(兼容 OpenAI 格式)
  4092. if 'choices' in response and len(response['choices']) > 0:
  4093. message = response['choices'][0].get('message', {})
  4094. return message.get('content', '')
  4095. return str(response)
  4096. def build_genealogy_prompt(member_name):
  4097. """
  4098. 构建用于竖排繁体家谱图片 OCR 提取的 Prompt。
  4099. 家谱图片为竖排版式(从上到下、从右到左),每位人物记录通常包含:
  4100. 辈字+名讳、字号、行次、父子关系、配偶(配某氏)、生卒年、葬地、子嗣等。
  4101. """
  4102. return f"""这是一张竖排繁体中文家谱图片。图片文字采用竖排格式,从上到下、从右到左逐列阅读。
  4103. 每位人物的记录通常包含以下内容(不一定全有):
  4104. - 辈字加名讳(如:公諱光元)
  4105. - 字号(如:字維亮)
  4106. - 行次(如:行仁一)
  4107. - 与父亲的关系(如:某某公長子、次子、三子)
  4108. - 配偶(如:配李氏、娶王氏)
  4109. - 生卒年月(如:生於某年某月、卒於某年某月)
  4110. - 葬地(如:葬祖山某向、塟於某地)
  4111. - 子嗣(如:子二:長某某、次某某)
  4112. 任务:找到人物「{member_name}」在图片中的完整记录,将其繁体原文逐字准确复制输出。
  4113. 要求:
  4114. 1. 只输出「{member_name}」这一个人物的记录,不包含其他人的内容
  4115. 2. 保持繁体字原貌,不要转换为简体
  4116. 3. 保留原文中的标点符号
  4117. 4. 不要添加任何解释、标注、序号或额外说明
  4118. 5. 直接输出原文内容"""
  4119. def _extract_from_thinking_output(text):
  4120. """
  4121. 从推理模型的思维链输出中提取最终答案。
  4122. 推理模型(如 doubao-seed 系列)会在 message 内容里写出完整思考过程:
  4123. 反复写候选答案、说"不对"再修正,最后以"现在确认/所以输出这个内容"等结论收尾。
  4124. 本函数的策略:
  4125. 1. 找最后一个"答案引导词 + 冒号"之后的文本(如"准确的原文是:"、"准确复制:")
  4126. 2. 若无引导词,则取"现在确认"/"所以输出这个内容"之前的最后一段文本
  4127. 3. 以上均失败则原文返回
  4128. """
  4129. # 思维链特征词
  4130. THINKING_SIGNALS = ['不对,', '现在确认', '准确复制', '准确的原文是', '正确的输出是', '所以输出这个内容']
  4131. if not any(sig in text for sig in THINKING_SIGNALS):
  4132. return text # 非思维链输出,原样返回
  4133. print(f"[CleanText] Detected thinking-model output, extracting final answer")
  4134. # ---- 策略1:找最后一个答案引导词 ----
  4135. ANSWER_INTRO_PATTERNS = [
  4136. r'准确的原文是[::]\s*',
  4137. r'正确的输出是[::]\s*',
  4138. r'现在准确复制[::]\s*',
  4139. r'准确复制[::]\s*',
  4140. r'应该是[::]\s*',
  4141. r'因此输出[::]\s*',
  4142. r'所以正确.*?是[::]\s*',
  4143. r'原文是[::]\s*',
  4144. r'输出[::]\s*',
  4145. ]
  4146. last_end = -1
  4147. for pattern in ANSWER_INTRO_PATTERNS:
  4148. for m in re.finditer(pattern, text):
  4149. if m.end() > last_end:
  4150. last_end = m.end()
  4151. if last_end >= 0:
  4152. remaining = text[last_end:]
  4153. # 取到第一个"结束标志"前
  4154. END_MARKERS = ['不对', '现在确认', '但是', '然而', '\n\n']
  4155. end_pos = len(remaining)
  4156. for marker in END_MARKERS:
  4157. idx = remaining.find(marker)
  4158. if 0 < idx < end_pos:
  4159. end_pos = idx
  4160. candidate = remaining[:end_pos].strip()
  4161. if len(candidate) >= 5:
  4162. print(f"[CleanText] Extracted via answer-intro pattern: '{candidate[:80]}'")
  4163. return candidate
  4164. # ---- 策略2:取"现在确认"之前的最后一段 ----
  4165. for end_phrase in ['现在确认', '所以输出这个内容', '这就是.*?的完整记录']:
  4166. m = re.search(end_phrase, text)
  4167. if m:
  4168. before = text[:m.start()].rstrip()
  4169. # 找最后一个换行符,取之后的内容
  4170. last_nl = before.rfind('\n')
  4171. candidate = (before[last_nl + 1:] if last_nl >= 0 else before[-400:]).strip()
  4172. if len(candidate) >= 5:
  4173. print(f"[CleanText] Extracted before confirmation phrase: '{candidate[:80]}'")
  4174. return candidate
  4175. return text # 均失败则原样返回
  4176. def _apply_char_whitelist(text):
  4177. """只保留汉字(含扩展A区)和常见中文标点"""
  4178. return re.sub(
  4179. r'[^\u4e00-\u9fff\u3400-\u4dbf\u3000-\u303f\uff00-\uffef,。;:、()【】「」『』〔〕·~—…《》]',
  4180. '', text
  4181. ).strip()
  4182. def clean_genealogy_text(text):
  4183. """
  4184. 清理从 AI 响应中提取的族谱文本。
  4185. - 处理 Markdown/JSON 格式噪声
  4186. - 自动识别思维链推理模型输出,提取最终答案段落
  4187. - 保留中文字符和中文标点,去除英文说明行
  4188. """
  4189. if not text:
  4190. return ''
  4191. text = text.strip()
  4192. # 去除代码块标记
  4193. text = re.sub(r'^```[a-z]*\n?', '', text)
  4194. text = re.sub(r'\n?```$', '', text)
  4195. text = text.strip()
  4196. # 尝试解析 JSON,从已知字段提取
  4197. try:
  4198. result = json.loads(text)
  4199. if isinstance(result, dict):
  4200. for key in ['text', 'content', 'result', 'traditional', 'genealogy_traditional']:
  4201. if key in result:
  4202. text = str(result[key])
  4203. break
  4204. except (json.JSONDecodeError, ValueError):
  4205. pass
  4206. # 针对思维链推理模型输出,提取最终答案(必须在行过滤之前,因为推理文本中含有必要的换行结构)
  4207. text = _extract_from_thinking_output(text)
  4208. # 按行过滤:去除纯英文/数字行、空行及明显解释性前缀行
  4209. lines = text.splitlines()
  4210. kept_lines = []
  4211. for line in lines:
  4212. line = line.strip()
  4213. if not line:
  4214. continue
  4215. non_ascii = sum(1 for c in line if ord(c) > 127)
  4216. if non_ascii == 0:
  4217. continue
  4218. if re.match(r'^(注[::]|说明[::]|Note[::]|备注[::])', line):
  4219. continue
  4220. kept_lines.append(line)
  4221. text = ''.join(kept_lines)
  4222. # 字符白名单:只保留汉字和中文标点
  4223. text = _apply_char_whitelist(text)
  4224. return text
  4225. def async_process_all_empty_genealogy(task_id, user_id):
  4226. """
  4227. 异步批量处理族谱原文为空的成员,支持断点续跑。
  4228. 连接管理原则:DB 连接仅在快速读写期间持有,AI 调用(最长120s)期间
  4229. 不占用任何 DB 连接,避免影响其他用户的正常操作。
  4230. """
  4231. import time
  4232. # ── 1. 读取断点位置,立即释放连接 ──────────────────────────────────────
  4233. conn = get_db_connection()
  4234. try:
  4235. with conn.cursor() as cursor:
  4236. cursor.execute(
  4237. "SELECT last_processed_id FROM batch_genealogy_task WHERE task_id = %s",
  4238. (task_id,)
  4239. )
  4240. task = cursor.fetchone()
  4241. last_processed_id = task['last_processed_id'] if task else 0
  4242. finally:
  4243. conn.close()
  4244. completed_count = 0
  4245. failed_count = 0
  4246. results = []
  4247. while True:
  4248. # ── 2. 取下一条待处理成员(短暂占用连接后立即释放)────────────────
  4249. conn = get_db_connection()
  4250. try:
  4251. with conn.cursor() as cursor:
  4252. cursor.execute("""
  4253. SELECT m.id, m.name, m.name_word_generation, m.source_record_id,
  4254. r.oss_url AS image_url, r.ai_content AS record_ai_content
  4255. FROM family_member_info m
  4256. LEFT JOIN genealogy_records r ON m.source_record_id = r.id
  4257. WHERE (m.genealogy_original_traditional IS NULL
  4258. OR m.genealogy_original_traditional = ''
  4259. OR m.genealogy_original_traditional = 'None')
  4260. AND (m.genealogy_original_simplified IS NULL
  4261. OR m.genealogy_original_simplified = ''
  4262. OR m.genealogy_original_simplified = 'None')
  4263. AND m.id > %s
  4264. ORDER BY m.id ASC
  4265. LIMIT 1
  4266. """, (last_processed_id,))
  4267. member = cursor.fetchone()
  4268. finally:
  4269. conn.close()
  4270. if not member:
  4271. break
  4272. member_id = member['id']
  4273. member_name = member['name']
  4274. image_url = member['image_url']
  4275. record_ai_content = member['record_ai_content']
  4276. print(f"[Batch Process] Processing member {member_id}: {member_name}")
  4277. traditional = ""
  4278. simplified = ""
  4279. extract_source = "basic_info"
  4280. try:
  4281. # ── 3. AI 提取(此阶段不持有任何 DB 连接)────────────────────
  4282. if image_url:
  4283. print(f"[Batch Process] Extracting from image: {image_url}")
  4284. prompt = build_genealogy_prompt(member_name)
  4285. ai_response = call_doubao_image_api(image_url, prompt)
  4286. print(f"[Batch Process] AI response for {member_id}: {str(ai_response)[:300]}")
  4287. if ai_response:
  4288. raw_text = extract_pure_text(ai_response)
  4289. traditional = clean_genealogy_text(raw_text)
  4290. print(f"[Batch Process] Cleaned traditional: {traditional[:100]}")
  4291. name_chars = [c for c in member_name if '\u4e00' <= c <= '\u9fff']
  4292. name_found = any(c in traditional for c in name_chars)
  4293. if traditional and len(traditional) >= 5 and name_found:
  4294. simplified = convert_to_simplified(traditional)
  4295. extract_source = "image"
  4296. print(f"[Batch Process] Image extract OK - trad: {traditional[:80]}")
  4297. else:
  4298. traditional = ""
  4299. simplified = ""
  4300. print(f"[Batch Process] Image extract invalid "
  4301. f"(name_found={name_found}, len={len(traditional)}), resetting")
  4302. # ── 4. 回退:从 record AI content 拼装(内存操作,无需 DB)──
  4303. if not (traditional and simplified) and record_ai_content:
  4304. print(f"[Batch Process] Fallback: trying record AI content")
  4305. try:
  4306. ai_content = json.loads(record_ai_content)
  4307. if isinstance(ai_content, list):
  4308. current_person = None
  4309. for person in ai_content:
  4310. person_name = person.get('original_name', person.get('name', '')).strip()
  4311. if person_name and (
  4312. member_name in person_name or person_name in member_name
  4313. ):
  4314. current_person = person
  4315. break
  4316. if current_person:
  4317. name = current_person.get('original_name',
  4318. current_person.get('name', member_name))
  4319. father_name = current_person.get('father_name', '')
  4320. spouse_name = current_person.get('spouse_name', '')
  4321. generation = current_person.get('generation',
  4322. member['name_word_generation'])
  4323. traditional = f"{name},{father_name}之子" if father_name else name
  4324. if spouse_name:
  4325. traditional += f",配{spouse_name}"
  4326. if generation:
  4327. traditional = f"第{generation}世 " + traditional
  4328. simplified = convert_to_simplified(traditional)
  4329. extract_source = "ai_content"
  4330. print(f"[Batch Process] AI content fallback: {traditional[:80]}")
  4331. else:
  4332. print(f"[Batch Process] No matching person for '{member_name}' in AI content")
  4333. except Exception as e:
  4334. print(f"[Batch Process] Failed to parse record AI content: {e}")
  4335. # ── 5. 最终回退:从关系表查父亲和配偶,短暂占用连接后立即释放 ──
  4336. if not (traditional and simplified):
  4337. print(f"[Batch Process] Fallback: basic info from DB")
  4338. conn = get_db_connection()
  4339. try:
  4340. with conn.cursor() as cursor:
  4341. cursor.execute("""
  4342. SELECT p.name FROM family_relation_info r
  4343. JOIN family_member_info p ON r.parent_mid = p.id
  4344. WHERE r.child_mid = %s AND r.relation_type = 1 LIMIT 1
  4345. """, (member_id,))
  4346. father = cursor.fetchone()
  4347. cursor.execute("""
  4348. SELECT p.name FROM family_relation_info r
  4349. JOIN family_member_info p ON r.parent_mid = p.id
  4350. WHERE r.child_mid = %s AND r.relation_type = 2 LIMIT 1
  4351. """, (member_id,))
  4352. spouse = cursor.fetchone()
  4353. finally:
  4354. conn.close()
  4355. father_name = father['name'] if father else ''
  4356. spouse_name = spouse['name'] if spouse else ''
  4357. generation = member['name_word_generation']
  4358. traditional = f"{member_name},{father_name}之子" if father_name else member_name
  4359. if spouse_name:
  4360. traditional += f",配{spouse_name}"
  4361. if generation:
  4362. traditional = f"第{generation}世 " + traditional
  4363. simplified = convert_to_simplified(traditional)
  4364. extract_source = "basic_info"
  4365. print(f"[Batch Process] Basic info fallback: {traditional[:80]}")
  4366. except Exception as extract_err:
  4367. print(f"[Batch Process] Extraction error for member {member_id}: {extract_err}")
  4368. traditional = ""
  4369. simplified = ""
  4370. # ── 6. 保存结果(短暂占用连接后立即释放)────────────────────────
  4371. last_processed_id = member_id
  4372. conn = get_db_connection()
  4373. try:
  4374. if traditional and simplified:
  4375. with conn.cursor() as cursor:
  4376. cursor.execute("""
  4377. UPDATE family_member_info
  4378. SET genealogy_original_traditional = %s,
  4379. genealogy_original_simplified = %s
  4380. WHERE id = %s
  4381. """, (traditional, simplified, member_id))
  4382. completed_count += 1
  4383. results.append({
  4384. "member_id": member_id,
  4385. "name": member_name,
  4386. "success": True,
  4387. "source": extract_source,
  4388. "traditional_length": len(traditional),
  4389. "simplified_length": len(simplified),
  4390. })
  4391. print(f"[Batch Process] Saved member {member_id} (source={extract_source})")
  4392. else:
  4393. failed_count += 1
  4394. results.append({
  4395. "member_id": member_id,
  4396. "name": member_name,
  4397. "success": False,
  4398. "message": "无法提取或生成族谱原文",
  4399. })
  4400. print(f"[Batch Process] Skipped member {member_id}: no valid text extracted")
  4401. with conn.cursor() as cursor:
  4402. cursor.execute("""
  4403. UPDATE batch_genealogy_task
  4404. SET completed_count = %s,
  4405. failed_count = %s,
  4406. last_processed_id = %s,
  4407. status = 'processing'
  4408. WHERE task_id = %s
  4409. """, (completed_count, failed_count, last_processed_id, task_id))
  4410. conn.commit()
  4411. except Exception as db_err:
  4412. print(f"[Batch Process] DB save error for member {member_id}: {db_err}")
  4413. failed_count += 1
  4414. finally:
  4415. conn.close()
  4416. # 每条处理完后短暂暂停,降低对 AI API 和服务器资源的压力
  4417. time.sleep(0.5)
  4418. # ── 7. 任务完成,写入最终状态 ─────────────────────────────────────────
  4419. conn = get_db_connection()
  4420. try:
  4421. status = 'completed' if failed_count == 0 else 'completed_with_errors'
  4422. with conn.cursor() as cursor:
  4423. cursor.execute("""
  4424. UPDATE batch_genealogy_task
  4425. SET status = %s,
  4426. completed_count = %s,
  4427. failed_count = %s,
  4428. results = %s
  4429. WHERE task_id = %s
  4430. """, (status, completed_count, failed_count,
  4431. json.dumps(results, ensure_ascii=False), task_id))
  4432. conn.commit()
  4433. print(f"[Batch Process] Task {task_id} done: "
  4434. f"{completed_count} success, {failed_count} failed")
  4435. except Exception as e:
  4436. print(f"[Batch Process] Error writing final status for {task_id}: {e}")
  4437. finally:
  4438. conn.close()
  4439. @app.route('/manager/api/members/extract_genealogy/<int:member_id>', methods=['GET'])
  4440. def extract_single_genealogy(member_id):
  4441. """单人员提取族谱原文,核心逻辑与批量处理一致,提取后写入数据库"""
  4442. if 'user_id' not in session:
  4443. return jsonify({"success": False, "message": "Unauthorized"}), 401
  4444. conn = get_db_connection()
  4445. try:
  4446. # 查询成员信息
  4447. with conn.cursor() as cursor:
  4448. cursor.execute("""
  4449. SELECT
  4450. m.id, m.name, m.name_word_generation,
  4451. m.source_record_id, r.oss_url as image_url,
  4452. r.ai_content AS record_ai_content
  4453. FROM family_member_info m
  4454. LEFT JOIN genealogy_records r ON m.source_record_id = r.id
  4455. WHERE m.id = %s
  4456. """, (member_id,))
  4457. row = cursor.fetchone()
  4458. if not row:
  4459. return jsonify({"success": False, "message": "未找到成员"}), 404
  4460. # 处理字典或元组格式的返回
  4461. if isinstance(row, dict):
  4462. member = row
  4463. else:
  4464. member = {
  4465. 'id': row[0],
  4466. 'name': row[1],
  4467. 'name_word_generation': row[2],
  4468. 'source_record_id': row[3],
  4469. 'image_url': row[4],
  4470. 'record_ai_content': row[5]
  4471. }
  4472. # 调试:打印查询结果
  4473. 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}'")
  4474. traditional = ""
  4475. simplified = ""
  4476. source = "basic_info"
  4477. image_url = member['image_url']
  4478. record_ai_content = member['record_ai_content']
  4479. print(f"[Single Extract] Processing member {member_id}: {member['name']}")
  4480. # 优先从关联图片中提取族谱原文
  4481. if image_url:
  4482. print(f"[Single Extract] Extracting from image: {image_url}")
  4483. member_name = member['name']
  4484. prompt = build_genealogy_prompt(member_name)
  4485. ai_response = call_doubao_image_api(image_url, prompt)
  4486. print(f"[Single Extract] AI response: {str(ai_response)[:500]}")
  4487. if ai_response:
  4488. raw_text = extract_pure_text(ai_response)
  4489. print(f"[Single Extract] Raw text from response: '{raw_text[:300]}'")
  4490. traditional = clean_genealogy_text(raw_text)
  4491. print(f"[Single Extract] Cleaned traditional: '{traditional[:200]}', length: {len(traditional)}")
  4492. # 验证提取结果是否包含该人物的姓名(至少包含名字中的一个字)
  4493. name_chars = [c for c in member_name if '\u4e00' <= c <= '\u9fff']
  4494. name_found = any(c in traditional for c in name_chars)
  4495. if traditional and len(traditional) >= 5 and name_found:
  4496. simplified = convert_to_simplified(traditional)
  4497. source = "image"
  4498. print(f"[Single Extract] Extracted from image - traditional: {traditional[:100]}, simplified: {simplified[:100]}")
  4499. else:
  4500. traditional = ""
  4501. simplified = ""
  4502. if not name_found:
  4503. print(f"[Single Extract] Extracted text does not contain name '{member_name}', resetting")
  4504. else:
  4505. print(f"[Single Extract] Image extraction too short ({len(traditional)} chars), resetting")
  4506. else:
  4507. print(f"[Single Extract] AI response is None or empty")
  4508. else:
  4509. print(f"[Single Extract] No image URL found for member {member_id}")
  4510. # 如果从图片提取失败或没有图片,尝试从已有的AI解析内容中提取
  4511. if not (traditional and simplified) and record_ai_content:
  4512. print(f"[Single Extract] Trying to extract from record AI content")
  4513. try:
  4514. ai_content = json.loads(record_ai_content)
  4515. if isinstance(ai_content, list) and len(ai_content) > 0:
  4516. current_person = None
  4517. member_name = member['name']
  4518. for person in ai_content:
  4519. person_name = person.get('original_name', person.get('name', '')).strip()
  4520. if person_name and (member_name in person_name or person_name in member_name):
  4521. current_person = person
  4522. break
  4523. if current_person:
  4524. name = current_person.get('original_name', current_person.get('name', member['name']))
  4525. father_name = current_person.get('father_name', '')
  4526. spouse_name = current_person.get('spouse_name', '')
  4527. generation = current_person.get('generation', member['name_word_generation'])
  4528. traditional = f"{name},{father_name}之子"
  4529. if spouse_name:
  4530. traditional += f",配{spouse_name}"
  4531. if generation:
  4532. traditional = f"第{generation}世 " + traditional
  4533. simplified = convert_to_simplified(traditional)
  4534. source = "ai_content"
  4535. print(f"[Single Extract] Generated from AI content: {traditional}")
  4536. except Exception as e:
  4537. print(f"[Single Extract] Failed to parse record AI content: {e}")
  4538. # 如果还是没有内容,使用基本信息生成(标注来源为 basic_info)
  4539. if not (traditional and simplified):
  4540. print(f"[Single Extract] Generating from basic info")
  4541. with conn.cursor() as cursor:
  4542. cursor.execute("""
  4543. SELECT p.name, p.simplified_name
  4544. FROM family_relation_info r
  4545. JOIN family_member_info p ON r.parent_mid = p.id
  4546. WHERE r.child_mid = %s AND r.relation_type = 1
  4547. LIMIT 1
  4548. """, (member_id,))
  4549. father_row = cursor.fetchone()
  4550. father_name = father_row[0] if father_row else ''
  4551. cursor.execute("""
  4552. SELECT p.name, p.simplified_name
  4553. FROM family_relation_info r
  4554. JOIN family_member_info p ON r.parent_mid = p.id
  4555. WHERE r.child_mid = %s AND r.relation_type = 2
  4556. LIMIT 1
  4557. """, (member_id,))
  4558. spouse_row = cursor.fetchone()
  4559. spouse_name = spouse_row[0] if spouse_row else ''
  4560. generation = member['name_word_generation']
  4561. name = member['name']
  4562. traditional = f"{name},{father_name}之子" if father_name else name
  4563. if spouse_name:
  4564. traditional += f",配{spouse_name}"
  4565. if generation:
  4566. traditional = f"第{generation}世 " + traditional
  4567. simplified = convert_to_simplified(traditional)
  4568. source = "basic_info"
  4569. print(f"[Single Extract] Generated from basic info: {traditional}")
  4570. # 调试:打印最终结果
  4571. print(f"[Single Extract] Final result - traditional: '{traditional}', simplified: '{simplified}'")
  4572. # 写入数据库
  4573. if traditional and simplified:
  4574. with conn.cursor() as cursor:
  4575. cursor.execute("""
  4576. UPDATE family_member_info
  4577. SET genealogy_original_traditional = %s,
  4578. genealogy_original_simplified = %s
  4579. WHERE id = %s
  4580. """, (traditional, simplified, member_id))
  4581. conn.commit()
  4582. print(f"[Single Extract] Successfully saved to database")
  4583. return jsonify({
  4584. "success": True,
  4585. "member_id": member_id,
  4586. "name": member['name'],
  4587. "genealogy_traditional": traditional,
  4588. "genealogy_simplified": simplified,
  4589. "source": source
  4590. })
  4591. else:
  4592. return jsonify({
  4593. "success": False,
  4594. "member_id": member_id,
  4595. "message": "无法提取或生成族谱原文"
  4596. })
  4597. except Exception as e:
  4598. import traceback
  4599. print(f"[Single Extract] Error: {e}")
  4600. print(f"[Single Extract] Traceback: {traceback.format_exc()}")
  4601. return jsonify({
  4602. "success": False,
  4603. "member_id": member_id,
  4604. "message": str(e),
  4605. "error_type": type(e).__name__
  4606. })
  4607. finally:
  4608. conn.close()
  4609. @app.route('/manager/api/members/batch_resume_task', methods=['GET'])
  4610. def batch_resume_task():
  4611. """
  4612. 恢复因服务重启而中断的批量任务(GET,方便浏览器直接访问)。
  4613. 可选参数:?task_id=xxx 不传则自动找最近一条中断任务。
  4614. """
  4615. if 'user_id' not in session:
  4616. return jsonify({"success": False, "message": "Unauthorized"}), 401
  4617. task_id = request.args.get('task_id')
  4618. conn = get_db_connection()
  4619. try:
  4620. with conn.cursor() as cursor:
  4621. if task_id:
  4622. cursor.execute("""
  4623. SELECT task_id, status, last_processed_id, total_count, completed_count, failed_count
  4624. FROM batch_genealogy_task
  4625. WHERE task_id = %s AND user_id = %s
  4626. """, (task_id, session['user_id']))
  4627. else:
  4628. # 找最近一条中断的任务
  4629. cursor.execute("""
  4630. SELECT task_id, status, last_processed_id, total_count, completed_count, failed_count
  4631. FROM batch_genealogy_task
  4632. WHERE user_id = %s AND status IN ('pending', 'processing', 'interrupted')
  4633. ORDER BY created_at DESC
  4634. LIMIT 1
  4635. """, (session['user_id'],))
  4636. task = cursor.fetchone()
  4637. if not task:
  4638. return jsonify({"success": False, "message": "未找到可恢复的任务"}), 404
  4639. task_id = task['task_id']
  4640. # 重新标记为 processing,准备恢复线程
  4641. with conn.cursor() as cursor:
  4642. cursor.execute("""
  4643. UPDATE batch_genealogy_task
  4644. SET status = 'processing'
  4645. WHERE task_id = %s
  4646. """, (task_id,))
  4647. conn.commit()
  4648. threading.Thread(
  4649. target=async_process_all_empty_genealogy,
  4650. args=(task_id, session['user_id']),
  4651. daemon=True
  4652. ).start()
  4653. return jsonify({
  4654. "success": True,
  4655. "task_id": task_id,
  4656. "message": f"任务已从断点恢复(已完成 {task['completed_count']},从 last_processed_id={task['last_processed_id']} 继续)",
  4657. "last_processed_id": task['last_processed_id'],
  4658. "completed_count": task['completed_count'],
  4659. "total_count": task['total_count'],
  4660. })
  4661. finally:
  4662. conn.close()
  4663. @app.route('/manager/api/members/batch_process_all_empty', methods=['GET'])
  4664. def batch_process_all_empty():
  4665. """简便批量处理接口:自动处理所有族谱原文为空的成员,支持断点续跑"""
  4666. if 'user_id' not in session:
  4667. return jsonify({"success": False, "message": "Unauthorized"}), 401
  4668. conn = get_db_connection()
  4669. try:
  4670. with conn.cursor() as cursor:
  4671. cursor.execute("""
  4672. SELECT COUNT(*) as count
  4673. FROM family_member_info
  4674. WHERE (genealogy_original_traditional IS NULL OR genealogy_original_traditional = '' OR genealogy_original_traditional = 'None')
  4675. AND (genealogy_original_simplified IS NULL OR genealogy_original_simplified = '' OR genealogy_original_simplified = 'None')
  4676. """)
  4677. result = cursor.fetchone()
  4678. total_empty = result['count'] if result else 0
  4679. cursor.execute("""
  4680. SELECT task_id, status, last_processed_id, total_count, completed_count, failed_count
  4681. FROM batch_genealogy_task
  4682. WHERE user_id = %s AND status IN ('pending', 'processing')
  4683. ORDER BY created_at DESC
  4684. LIMIT 1
  4685. """, (session['user_id'],))
  4686. running_task = cursor.fetchone()
  4687. if running_task:
  4688. return jsonify({
  4689. "success": False,
  4690. "message": "存在正在进行的任务,若服务已重启可调用 POST /manager/api/members/batch_resume_task 恢复",
  4691. "task_id": running_task['task_id'],
  4692. "status": running_task['status'],
  4693. "last_processed_id": running_task['last_processed_id'],
  4694. "completed_count": running_task['completed_count'],
  4695. "total_count": running_task['total_count'],
  4696. "resume_tip": "POST /manager/api/members/batch_resume_task body: {\"task_id\": \"" + running_task['task_id'] + "\"}"
  4697. })
  4698. task_id = str(uuid.uuid4())
  4699. with conn.cursor() as cursor:
  4700. cursor.execute("""
  4701. INSERT INTO batch_genealogy_task (task_id, user_id, status, total_count, last_processed_id)
  4702. VALUES (%s, %s, 'processing', %s, 0)
  4703. """, (task_id, session['user_id'], total_empty))
  4704. conn.commit()
  4705. threading.Thread(
  4706. target=async_process_all_empty_genealogy,
  4707. args=(task_id, session['user_id']),
  4708. daemon=True
  4709. ).start()
  4710. return jsonify({
  4711. "success": True,
  4712. "task_id": task_id,
  4713. "message": f"任务已创建,将处理 {total_empty} 个族谱原文为空的成员",
  4714. "total_count": total_empty
  4715. })
  4716. finally:
  4717. conn.close()
  4718. # ==================== 微信小程序 API 接口 ====================
  4719. @app.route('/manager/api/wechat/login', methods=['POST'])
  4720. def api_wechat_login():
  4721. """微信小程序登录接口(正式流程)"""
  4722. import time
  4723. start_time = time.time()
  4724. try:
  4725. data = request.get_json()
  4726. if not data:
  4727. print(f"[API Wechat Login] Error: No request data")
  4728. return jsonify({"success": False, "message": "请求数据为空"}), 400
  4729. code = data.get('code', '')
  4730. encrypted_data = data.get('encryptedData', '')
  4731. iv = data.get('iv', '')
  4732. phone_code = data.get('phoneCode', '')
  4733. if not code:
  4734. print(f"[API Wechat Login] Error: Missing code parameter")
  4735. return jsonify({"success": False, "message": "缺少code参数"}), 400
  4736. print(f"[API Wechat Login] Received login request, code: {code[:10]}..., phoneCode: {phone_code[:10]}...")
  4737. # 1. 使用code获取session_key和openid
  4738. session_url = "https://api.weixin.qq.com/sns/jscode2session"
  4739. session_params = {
  4740. "appid": WECHAT_APP_ID,
  4741. "secret": WECHAT_APP_SECRET,
  4742. "js_code": code,
  4743. "grant_type": "authorization_code"
  4744. }
  4745. try:
  4746. session_response = requests.get(session_url, params=session_params, timeout=15)
  4747. session_response.raise_for_status()
  4748. except requests.exceptions.RequestException as e:
  4749. print(f"[WeChat Login] Session request failed: {e}")
  4750. return jsonify({"success": False, "message": f"网络请求失败: {str(e)}"}), 500
  4751. session_data = session_response.json()
  4752. print(f"[WeChat Login] Session response: {session_data}")
  4753. if 'errcode' in session_data and session_data['errcode'] != 0:
  4754. print(f"[WeChat Login] Session error: {session_data}")
  4755. return jsonify({"success": False, "message": session_data.get('errmsg', '登录失败')}), 400
  4756. openid = session_data.get('openid')
  4757. session_key = session_data.get('session_key')
  4758. if not openid:
  4759. print(f"[WeChat Login] Error: openid is empty")
  4760. return jsonify({"success": False, "message": "获取openid失败"}), 400
  4761. # 2. 获取手机号(支持两种方式)
  4762. phone = None
  4763. # 方式一:使用phoneCode调用官方接口(推荐)
  4764. if phone_code:
  4765. print(f"[WeChat Phone] Trying to get phone via phoneCode")
  4766. try:
  4767. access_token = get_wechat_access_token()
  4768. if access_token:
  4769. phone_url = f"https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token={access_token}"
  4770. phone_response = requests.post(phone_url, json={"code": phone_code}, timeout=15)
  4771. phone_response.raise_for_status()
  4772. phone_result = phone_response.json()
  4773. print(f"[WeChat Phone] Phone API response: {phone_result}")
  4774. if phone_result.get('errcode') == 0 and phone_result.get('phone_info'):
  4775. phone = phone_result['phone_info'].get('phoneNumber')
  4776. print(f"[WeChat Phone] Phone obtained via phoneCode: {phone}")
  4777. else:
  4778. print(f"[WeChat Phone] Failed to get phone via phoneCode: {phone_result}")
  4779. else:
  4780. print(f"[WeChat Phone] Failed to get access_token")
  4781. except requests.exceptions.RequestException as e:
  4782. print(f"[WeChat Phone] Phone request failed: {e}")
  4783. # 方式二:使用encryptedData解密(兼容旧方式)
  4784. if not phone and encrypted_data and iv and session_key:
  4785. print(f"[WeChat Phone] Trying to decrypt phone via encryptedData")
  4786. phone_data = decrypt_wechat_phone(encrypted_data, iv, session_key)
  4787. if phone_data and 'phoneNumber' in phone_data:
  4788. phone = phone_data['phoneNumber']
  4789. print(f"[WeChat Phone] Phone obtained via decryption: {phone}")
  4790. # 3. 创建或获取小程序用户(使用mp_users表)
  4791. conn = get_db_connection()
  4792. try:
  4793. with conn.cursor() as cursor:
  4794. cursor.execute("SELECT id, phone FROM mp_users WHERE openid = %s", (openid,))
  4795. mp_user = cursor.fetchone()
  4796. if mp_user:
  4797. update_fields = []
  4798. update_params = []
  4799. if phone and phone != mp_user.get('phone'):
  4800. update_fields.append("phone = %s")
  4801. update_params.append(phone)
  4802. update_fields.append("last_login_at = CURRENT_TIMESTAMP")
  4803. update_params.append(openid)
  4804. if update_fields:
  4805. sql = f"UPDATE mp_users SET {', '.join(update_fields)} WHERE openid = %s"
  4806. cursor.execute(sql, update_params)
  4807. conn.commit()
  4808. user_id = mp_user['id']
  4809. else:
  4810. cursor.execute("""
  4811. INSERT INTO mp_users (openid, phone, created_at, updated_at, last_login_at)
  4812. VALUES (%s, %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
  4813. """, (openid, phone))
  4814. conn.commit()
  4815. user_id = cursor.lastrowid
  4816. print(f"[WeChat Login] Created new user: {user_id}, openid: {openid[:10]}...")
  4817. finally:
  4818. conn.close()
  4819. import uuid
  4820. token = str(uuid.uuid4())
  4821. # 持久化 token,用于后续接口识别用户身份
  4822. conn2 = get_db_connection()
  4823. try:
  4824. with conn2.cursor() as cursor2:
  4825. cursor2.execute("UPDATE mp_users SET token = %s WHERE id = %s", (token, user_id))
  4826. conn2.commit()
  4827. finally:
  4828. conn2.close()
  4829. elapsed = time.time() - start_time
  4830. print(f"[API Wechat Login] Success, elapsed: {elapsed:.2f}s, user_id: {user_id}, phone: {phone}")
  4831. return jsonify({
  4832. "success": True,
  4833. "token": token,
  4834. "user": {
  4835. "id": user_id,
  4836. "openid": openid,
  4837. "phone": phone,
  4838. "login_type": "wechat_mp"
  4839. }
  4840. })
  4841. except Exception as e:
  4842. elapsed = time.time() - start_time
  4843. print(f"[API Wechat Login] Error: {e}, elapsed: {elapsed:.2f}s")
  4844. return jsonify({"success": False, "message": str(e)}), 500
  4845. @app.route('/manager/api/members/search', methods=['GET'])
  4846. def api_search_members():
  4847. """搜索成员(小程序用)"""
  4848. keyword = request.args.get('keyword', '')
  4849. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  4850. if not token:
  4851. return jsonify({"success": False, "message": "未登录"}), 401
  4852. conn = get_db_connection()
  4853. try:
  4854. with conn.cursor() as cursor:
  4855. base_sql = """
  4856. SELECT
  4857. m.id, m.name, m.simplified_name, m.name_word_generation,
  4858. m.sex, m.birthday, m.family_rank, m.is_pass_away, m.marital_status,
  4859. p.name AS father_name,
  4860. p.simplified_name AS father_simplified_name,
  4861. p.name_word_generation AS father_generation,
  4862. r.relation_type AS father_relation_type
  4863. FROM family_member_info m
  4864. LEFT JOIN family_relation_info r
  4865. ON r.child_mid = m.id AND r.relation_type IN (1, 2)
  4866. LEFT JOIN family_member_info p ON p.id = r.parent_mid
  4867. {where}
  4868. ORDER BY m.name_word_generation ASC, m.id ASC
  4869. LIMIT 30
  4870. """
  4871. if keyword:
  4872. cursor.execute(
  4873. base_sql.format(where="WHERE m.name LIKE %s OR m.simplified_name LIKE %s"),
  4874. (f"%{keyword}%", f"%{keyword}%")
  4875. )
  4876. else:
  4877. cursor.execute(base_sql.format(where=""))
  4878. members = cursor.fetchall()
  4879. for m in members:
  4880. m['birthday_date'] = format_timestamp(m.get('birthday'))
  4881. return jsonify({"success": True, "data": members})
  4882. finally:
  4883. conn.close()
  4884. @app.route('/manager/api/members/check_duplicate', methods=['GET'])
  4885. def api_check_duplicate():
  4886. """检查同名成员"""
  4887. name = request.args.get('name', '')
  4888. conn = get_db_connection()
  4889. try:
  4890. with conn.cursor() as cursor:
  4891. cursor.execute("""
  4892. SELECT id, name, simplified_name, name_word_generation
  4893. FROM family_member_info
  4894. WHERE name = %s OR simplified_name = %s
  4895. LIMIT 10
  4896. """, (name, name))
  4897. members = cursor.fetchall()
  4898. return jsonify({
  4899. "success": True,
  4900. "data": members
  4901. })
  4902. finally:
  4903. conn.close()
  4904. @app.route('/manager/api/members/<int:member_id>', methods=['GET'])
  4905. def api_get_member(member_id):
  4906. """获取单个成员信息(含关系)"""
  4907. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  4908. if not token:
  4909. return jsonify({"success": False, "message": "未登录"}), 401
  4910. conn = get_db_connection()
  4911. try:
  4912. with conn.cursor() as cursor:
  4913. cursor.execute("SELECT * FROM family_member_info WHERE id = %s", (member_id,))
  4914. member = cursor.fetchone()
  4915. if not member:
  4916. return jsonify({"success": False, "message": "成员不存在"}), 404
  4917. member['birthday_date'] = format_timestamp(member.get('birthday'))
  4918. if member.get('create_time'):
  4919. member['create_time'] = member['create_time'].strftime('%Y-%m-%d %H:%M')
  4920. if member.get('modified_time'):
  4921. member['modified_time'] = member['modified_time'].strftime('%Y-%m-%d %H:%M')
  4922. # 父母
  4923. cursor.execute("""
  4924. SELECT m.id, m.name, m.simplified_name, m.name_word_generation,
  4925. r.relation_type, r.sub_relation_type, r.child_order
  4926. FROM family_relation_info r
  4927. JOIN family_member_info m ON m.id = r.parent_mid
  4928. WHERE r.child_mid = %s
  4929. ORDER BY r.relation_type ASC
  4930. """, (member_id,))
  4931. parents = cursor.fetchall()
  4932. # 子女
  4933. cursor.execute("""
  4934. SELECT m.id, m.name, m.simplified_name, m.name_word_generation,
  4935. r.relation_type, r.sub_relation_type, r.child_order
  4936. FROM family_relation_info r
  4937. JOIN family_member_info m ON m.id = r.child_mid
  4938. WHERE r.parent_mid = %s
  4939. ORDER BY COALESCE(r.child_order, 9999), m.id ASC
  4940. """, (member_id,))
  4941. children = cursor.fetchall()
  4942. # relation_type: 1=父, 2=母
  4943. relation_labels = {1: '父', 2: '母', 3: '祖父', 4: '祖母'}
  4944. for p in parents:
  4945. p['relation_label'] = relation_labels.get(p.get('relation_type'), '亲属')
  4946. # 计算入继说明
  4947. _order_labels = {1:'长', 2:'次', 3:'三', 4:'四', 5:'五',
  4948. 6:'六', 7:'七', 8:'八', 9:'九', 10:'十'}
  4949. adopt_info = None
  4950. is_adopted_in = any(p.get('sub_relation_type') == 3 for p in parents)
  4951. if is_adopted_in:
  4952. bio = next((p for p in parents if p.get('sub_relation_type') == 2), None)
  4953. if bio:
  4954. bio_name = bio.get('simplified_name') or bio.get('name', '')
  4955. order = bio.get('child_order')
  4956. order_str = _order_labels.get(order, f'第{order}') if order else '某'
  4957. adopt_info = f"由{bio_name}公{order_str}子入继"
  4958. return jsonify({
  4959. "success": True,
  4960. "data": {**member, "parents": parents, "children": children,
  4961. "adopt_info": adopt_info}
  4962. })
  4963. finally:
  4964. conn.close()
  4965. @app.route('/manager/api/members/add', methods=['POST'])
  4966. def api_add_member():
  4967. """添加成员(小程序用)"""
  4968. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  4969. if not token:
  4970. return jsonify({"success": False, "message": "未登录"}), 401
  4971. mp_user = get_mp_user_from_token(token)
  4972. mp_user_id = mp_user['id'] if mp_user else None
  4973. try:
  4974. data = request.get_json()
  4975. name = data.get('name', '')
  4976. simplified_name = data.get('simplified_name', '')
  4977. sex = data.get('sex', 1)
  4978. birthday_str = data.get('birthday', '')
  4979. family_rank = data.get('family_rank', '')
  4980. name_word_generation = data.get('name_word_generation', '')
  4981. is_pass_away = data.get('is_pass_away', 0)
  4982. marital_status = data.get('marital_status', 0)
  4983. former_name = data.get('former_name', '')
  4984. phone = data.get('phone', '')
  4985. notes = data.get('notes', '')
  4986. relations = data.get('relations', [])
  4987. if not name:
  4988. return jsonify({"success": False, "message": "姓名不能为空"}), 400
  4989. # 将日期字符串 "YYYY-MM-DD" 转为 Unix 时间戳(与后台一致)
  4990. birthday_ts = 0
  4991. if birthday_str:
  4992. try:
  4993. birthday_ts = int(datetime.strptime(birthday_str, '%Y-%m-%d').timestamp())
  4994. except Exception:
  4995. birthday_ts = 0
  4996. conn = get_db_connection()
  4997. try:
  4998. with conn.cursor() as cursor:
  4999. cursor.execute("""
  5000. INSERT INTO family_member_info
  5001. (name, simplified_name, sex, birthday, family_rank,
  5002. name_word_generation, is_pass_away, marital_status, former_name, phone, notes,
  5003. data_source, create_uid, create_time, modified_time)
  5004. VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, 'miniprogram', %s, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
  5005. """, (name, simplified_name, sex, birthday_ts, family_rank,
  5006. name_word_generation, is_pass_away, marital_status, former_name, phone, notes,
  5007. mp_user_id))
  5008. conn.commit()
  5009. new_member_id = cursor.lastrowid
  5010. # 添加关系
  5011. for rel in relations:
  5012. parent_mid = rel.get('parent_mid')
  5013. relation_type = rel.get('relation_type', 1)
  5014. sub_relation_type = rel.get('sub_relation_type', 0)
  5015. if parent_mid:
  5016. cursor.execute("""
  5017. INSERT INTO family_relation_info
  5018. (parent_mid, child_mid, relation_type, sub_relation_type)
  5019. VALUES (%s, %s, %s, %s)
  5020. """, (parent_mid, new_member_id, relation_type, sub_relation_type))
  5021. conn.commit()
  5022. return jsonify({
  5023. "success": True,
  5024. "message": "添加成功",
  5025. "memberId": new_member_id
  5026. })
  5027. finally:
  5028. conn.close()
  5029. except Exception as e:
  5030. print(f"[API Add Member] Error: {e}")
  5031. return jsonify({"success": False, "message": str(e)}), 500
  5032. @app.route('/manager/api/members/my', methods=['GET'])
  5033. def api_my_members():
  5034. """获取当前小程序用户录入的所有成员"""
  5035. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  5036. mp_user = get_mp_user_from_token(token)
  5037. if not mp_user:
  5038. return jsonify({"success": False, "message": "未登录或登录已过期"}), 401
  5039. conn = get_db_connection()
  5040. try:
  5041. with conn.cursor() as cursor:
  5042. cursor.execute("""
  5043. SELECT id, name, simplified_name, sex, birthday, name_word_generation,
  5044. family_rank, is_pass_away, marital_status, create_time
  5045. FROM family_member_info
  5046. WHERE create_uid = %s AND data_source = 'miniprogram'
  5047. ORDER BY create_time DESC
  5048. """, (mp_user['id'],))
  5049. members = cursor.fetchall()
  5050. for m in members:
  5051. m['birthday_date'] = format_timestamp(m.get('birthday'))
  5052. if m.get('create_time'):
  5053. m['create_time'] = m['create_time'].strftime('%Y-%m-%d %H:%M')
  5054. return jsonify({"success": True, "data": members})
  5055. except Exception as e:
  5056. print(f"[API My Members] Error: {e}")
  5057. return jsonify({"success": False, "message": str(e)}), 500
  5058. finally:
  5059. conn.close()
  5060. @app.route('/manager/api/member/<int:member_id>', methods=['PUT'])
  5061. def api_update_member(member_id):
  5062. """更新成员信息(小程序用,只能修改自己录入的)"""
  5063. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  5064. mp_user = get_mp_user_from_token(token)
  5065. if not mp_user:
  5066. return jsonify({"success": False, "message": "未登录或登录已过期"}), 401
  5067. conn = get_db_connection()
  5068. try:
  5069. with conn.cursor() as cursor:
  5070. cursor.execute(
  5071. "SELECT id, create_uid, data_source FROM family_member_info WHERE id = %s",
  5072. (member_id,)
  5073. )
  5074. member = cursor.fetchone()
  5075. if not member:
  5076. return jsonify({"success": False, "message": "成员不存在"}), 404
  5077. if member['data_source'] != 'miniprogram' or member['create_uid'] != mp_user['id']:
  5078. return jsonify({"success": False, "message": "无权限修改此成员"}), 403
  5079. data = request.get_json() or {}
  5080. name = data.get('name', '').strip()
  5081. if not name:
  5082. return jsonify({"success": False, "message": "姓名不能为空"}), 400
  5083. birthday_str = data.get('birthday', '')
  5084. birthday_ts = 0
  5085. if birthday_str:
  5086. try:
  5087. from datetime import datetime as _dt
  5088. birthday_ts = int(_dt.strptime(birthday_str, '%Y-%m-%d').timestamp())
  5089. except Exception:
  5090. birthday_ts = 0
  5091. cursor.execute("""
  5092. UPDATE family_member_info
  5093. SET name=%s, simplified_name=%s, sex=%s, birthday=%s,
  5094. family_rank=%s, name_word_generation=%s, is_pass_away=%s,
  5095. marital_status=%s, phone=%s, notes=%s, modified_time=CURRENT_TIMESTAMP
  5096. WHERE id=%s
  5097. """, (
  5098. name,
  5099. data.get('simplified_name', ''),
  5100. int(data.get('sex', 1)),
  5101. birthday_ts,
  5102. data.get('family_rank') or None,
  5103. data.get('name_word_generation', ''),
  5104. int(data.get('is_pass_away', 0)),
  5105. int(data.get('marital_status', 0)),
  5106. data.get('phone', ''),
  5107. data.get('notes', ''),
  5108. member_id
  5109. ))
  5110. conn.commit()
  5111. return jsonify({"success": True, "message": "修改成功"})
  5112. except Exception as e:
  5113. conn.rollback()
  5114. print(f"[API Update Member] Error: {e}")
  5115. return jsonify({"success": False, "message": str(e)}), 500
  5116. finally:
  5117. conn.close()
  5118. @app.route('/manager/api/members/<int:member_id>', methods=['DELETE'])
  5119. def api_delete_member(member_id):
  5120. """删除成员(小程序用,只能删除自己录入的)"""
  5121. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  5122. mp_user = get_mp_user_from_token(token)
  5123. if not mp_user:
  5124. return jsonify({"success": False, "message": "未登录或登录已过期"}), 401
  5125. conn = get_db_connection()
  5126. try:
  5127. with conn.cursor() as cursor:
  5128. cursor.execute("""
  5129. SELECT id, create_uid, data_source FROM family_member_info WHERE id = %s
  5130. """, (member_id,))
  5131. member = cursor.fetchone()
  5132. if not member:
  5133. return jsonify({"success": False, "message": "成员不存在"}), 404
  5134. if member['data_source'] != 'miniprogram' or member['create_uid'] != mp_user['id']:
  5135. return jsonify({"success": False, "message": "无权限删除此成员"}), 403
  5136. cursor.execute("DELETE FROM family_relation_info WHERE parent_mid = %s OR child_mid = %s",
  5137. (member_id, member_id))
  5138. cursor.execute("DELETE FROM family_member_info WHERE id = %s", (member_id,))
  5139. conn.commit()
  5140. return jsonify({"success": True, "message": "删除成功"})
  5141. except Exception as e:
  5142. conn.rollback()
  5143. print(f"[API Delete Member] Error: {e}")
  5144. return jsonify({"success": False, "message": str(e)}), 500
  5145. finally:
  5146. conn.close()
  5147. @app.route('/manager/api/lineage/<int:member_id>', methods=['GET'])
  5148. def api_get_lineage(member_id):
  5149. """获取世系信息(小程序用)- 完整多代版"""
  5150. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  5151. if not token:
  5152. return jsonify({"success": False, "message": "未登录"}), 401
  5153. mode = request.args.get('mode', 'incense')
  5154. conn = get_db_connection()
  5155. try:
  5156. with conn.cursor() as cursor:
  5157. # Step 1: 获取查询人物
  5158. cursor.execute("""
  5159. SELECT id, name, simplified_name, name_word, name_word_generation
  5160. FROM family_member_info WHERE id = %s
  5161. """, (member_id,))
  5162. center = cursor.fetchone()
  5163. if not center:
  5164. return jsonify({"success": False, "message": "成员不存在"}), 404
  5165. # Step 2: 向上追溯祖先链(最多100代),每代带同辈兄弟
  5166. generations = []
  5167. current_id = member_id
  5168. max_depth = 100
  5169. visited_ancestor_ids = set([member_id]) # 循环检测
  5170. for depth in range(max_depth):
  5171. cursor.execute("""
  5172. SELECT p.id, p.name, p.simplified_name, p.name_word, p.name_word_generation,
  5173. EXISTS(SELECT 1 FROM family_relation_info
  5174. WHERE parent_mid = p.id AND relation_type IN (1,2)) as has_children,
  5175. r.sub_relation_type
  5176. FROM family_relation_info r
  5177. JOIN family_member_info p ON r.parent_mid = p.id
  5178. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  5179. """, (current_id,))
  5180. parents = cursor.fetchall()
  5181. if not parents:
  5182. break
  5183. # 分拣各类父母关系
  5184. normal_parent = None
  5185. adoptive_parent = None
  5186. bio_parent = None
  5187. for p in parents:
  5188. if p['sub_relation_type'] == 3:
  5189. adoptive_parent = p
  5190. elif p['sub_relation_type'] == 2:
  5191. bio_parent = p
  5192. else:
  5193. normal_parent = p
  5194. if mode == 'blood':
  5195. parent = normal_parent or bio_parent or adoptive_parent
  5196. else:
  5197. parent = adoptive_parent or normal_parent or bio_parent
  5198. if parent is adoptive_parent and adoptive_parent is not None:
  5199. bio_name = (bio_parent.get('simplified_name') or bio_parent.get('name')) if bio_parent else None
  5200. adopt_label = f"从{bio_name}出继" if bio_name else "出继"
  5201. if depth == 0:
  5202. center['adoption_label'] = adopt_label
  5203. elif generations:
  5204. generations[-1]['ancestor']['adoption_label'] = adopt_label
  5205. # 祖先卡片不携带子辈关系类型
  5206. parent['sub_relation_type'] = None
  5207. # 循环检测
  5208. if parent['id'] in visited_ancestor_ids:
  5209. break
  5210. visited_ancestor_ids.add(parent['id'])
  5211. # 查祖父以获取该祖先的兄弟(优先亲生父母,排除养父)
  5212. cursor.execute("""
  5213. SELECT gp.id FROM family_relation_info r
  5214. JOIN family_member_info gp ON r.parent_mid = gp.id
  5215. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  5216. ORDER BY CASE WHEN COALESCE(r.sub_relation_type, 0) = 3 THEN 1 ELSE 0 END, r.id
  5217. LIMIT 1
  5218. """, (parent['id'],))
  5219. grandparent = cursor.fetchone()
  5220. parent_siblings = []
  5221. if grandparent:
  5222. # 获取祖先自身的 child_order
  5223. cursor.execute("""
  5224. SELECT COALESCE(child_order, 1) AS child_order
  5225. FROM family_relation_info
  5226. WHERE parent_mid = %s AND child_mid = %s AND relation_type IN (1,2)
  5227. LIMIT 1
  5228. """, (grandparent['id'], parent['id']))
  5229. co_row = cursor.fetchone()
  5230. parent['child_order'] = co_row['child_order'] if co_row else 1
  5231. cursor.execute("""
  5232. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  5233. EXISTS(SELECT 1 FROM family_relation_info
  5234. WHERE parent_mid = c.id AND relation_type IN (1,2)) as has_children,
  5235. COALESCE(r.child_order, 1) AS child_order
  5236. FROM family_relation_info r
  5237. JOIN family_member_info c ON r.child_mid = c.id
  5238. WHERE r.parent_mid = %s AND r.relation_type IN (1,2) AND c.id != %s
  5239. ORDER BY COALESCE(r.child_order, 1), c.id
  5240. LIMIT 10
  5241. """, (grandparent['id'], parent['id']))
  5242. parent_siblings = cursor.fetchall()
  5243. for s in parent_siblings:
  5244. s['has_children'] = bool(s['has_children'])
  5245. else:
  5246. parent['child_order'] = None
  5247. parent['has_children'] = bool(parent['has_children'])
  5248. generations.append({
  5249. 'ancestor': parent,
  5250. 'siblings': list(parent_siblings),
  5251. 'depth': depth
  5252. })
  5253. current_id = parent['id']
  5254. # Step 3: 获取子女(排除出继、保留入继,带排行)
  5255. cursor.execute("""
  5256. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  5257. EXISTS(SELECT 1 FROM family_relation_info
  5258. WHERE parent_mid = c.id AND relation_type IN (1,2)) as has_children,
  5259. COALESCE(r.child_order, 1) AS child_order, r.sub_relation_type
  5260. FROM family_relation_info r
  5261. JOIN family_member_info c ON r.child_mid = c.id
  5262. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2)
  5263. AND (
  5264. COALESCE(r.sub_relation_type, 0) != 2
  5265. OR NOT EXISTS (
  5266. SELECT 1 FROM family_relation_info r2
  5267. WHERE r2.child_mid = c.id AND r2.sub_relation_type = 3
  5268. )
  5269. )
  5270. ORDER BY COALESCE(r.child_order, 1), c.id
  5271. LIMIT 20
  5272. """, (member_id,))
  5273. children = cursor.fetchall()
  5274. for c in children:
  5275. c['has_children'] = bool(c['has_children'])
  5276. # 入继子女:附加生父母信息,生成"从xx出继"标注
  5277. if c['sub_relation_type'] == 3:
  5278. cursor.execute("""
  5279. SELECT p.name, p.simplified_name
  5280. FROM family_relation_info r
  5281. JOIN family_member_info p ON r.parent_mid = p.id
  5282. WHERE r.child_mid = %s AND r.sub_relation_type = 2 LIMIT 1
  5283. """, (c['id'],))
  5284. bp = cursor.fetchone()
  5285. if bp:
  5286. bio_name = bp['simplified_name'] or bp['name']
  5287. c['adoption_label'] = f"从{bio_name}出继"
  5288. else:
  5289. c['adoption_label'] = "出继"
  5290. # Step 4: 获取查询人物的同辈兄弟(含center自己的child_order)
  5291. siblings = []
  5292. center_child_order = None
  5293. if generations:
  5294. parent_id = generations[0]['ancestor']['id']
  5295. # 先获取 center 自身的 child_order
  5296. cursor.execute("""
  5297. SELECT child_order FROM family_relation_info
  5298. WHERE parent_mid = %s AND child_mid = %s AND relation_type IN (1,2)
  5299. LIMIT 1
  5300. """, (parent_id, member_id))
  5301. co_row = cursor.fetchone()
  5302. center_child_order = (co_row['child_order'] if co_row and co_row['child_order'] else 1)
  5303. cursor.execute("""
  5304. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  5305. EXISTS(SELECT 1 FROM family_relation_info
  5306. WHERE parent_mid = c.id AND relation_type IN (1,2)) as has_children,
  5307. r.sub_relation_type, COALESCE(r.child_order, 1) AS child_order
  5308. FROM family_relation_info r
  5309. JOIN family_member_info c ON r.child_mid = c.id
  5310. WHERE r.parent_mid = %s AND r.relation_type IN (1,2) AND c.id != %s
  5311. ORDER BY COALESCE(r.child_order, 1), c.id
  5312. LIMIT 10
  5313. """, (parent_id, member_id))
  5314. siblings = cursor.fetchall()
  5315. for s in siblings:
  5316. s['has_children'] = bool(s['has_children'])
  5317. # 判断是否还有更高的祖先
  5318. has_more_ancestors = False
  5319. topmost_ancestor_id = None
  5320. if generations:
  5321. topmost_ancestor_id = generations[-1]['ancestor']['id']
  5322. cursor.execute("""
  5323. SELECT COUNT(*) as cnt FROM family_relation_info
  5324. WHERE child_mid = %s AND relation_type IN (1,2)
  5325. """, (topmost_ancestor_id,))
  5326. has_more_ancestors = cursor.fetchone()['cnt'] > 0
  5327. return jsonify({
  5328. "success": True,
  5329. "data": {
  5330. "center": {**center, "child_order": center_child_order or 1},
  5331. "generations": generations,
  5332. "siblings": list(siblings),
  5333. "children": list(children),
  5334. "has_more_ancestors": has_more_ancestors,
  5335. "topmost_ancestor_id": topmost_ancestor_id
  5336. }
  5337. })
  5338. except Exception as e:
  5339. print(f"[API Lineage] Error: {e}")
  5340. return jsonify({"success": False, "message": str(e)}), 500
  5341. finally:
  5342. conn.close()
  5343. @app.route('/manager/api/lineage/<int:ancestor_id>/ancestors_above', methods=['GET'])
  5344. def api_get_ancestors_above(ancestor_id):
  5345. """小程序世系查询:从指定祖先节点继续向上追溯(分批加载更多祖先)"""
  5346. token = request.headers.get('Authorization', '').replace('Bearer ', '')
  5347. if not token:
  5348. return jsonify({"success": False, "message": "未登录"}), 401
  5349. mode = request.args.get('mode', 'incense')
  5350. conn = get_db_connection()
  5351. try:
  5352. with conn.cursor() as cursor:
  5353. generations = []
  5354. current_id = ancestor_id
  5355. max_depth = 100
  5356. visited_ids = set([ancestor_id])
  5357. # 计算 anchor 节点(ancestor_id)自身的 adoption_label
  5358. anchor_adoption_label_wx = None
  5359. cursor.execute("""
  5360. SELECT p.id, p.name, p.simplified_name, r.sub_relation_type
  5361. FROM family_relation_info r
  5362. JOIN family_member_info p ON r.parent_mid = p.id
  5363. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  5364. """, (ancestor_id,))
  5365. anchor_parents_wx = cursor.fetchall()
  5366. anchor_bio_wx = None
  5367. has_adoptive_wx = False
  5368. for ap in anchor_parents_wx:
  5369. if ap['sub_relation_type'] == 3:
  5370. has_adoptive_wx = True
  5371. elif ap['sub_relation_type'] == 2:
  5372. anchor_bio_wx = ap
  5373. if has_adoptive_wx and anchor_bio_wx:
  5374. bio_name_wx = anchor_bio_wx.get('simplified_name') or anchor_bio_wx.get('name')
  5375. anchor_adoption_label_wx = f"从{bio_name_wx}出继" if bio_name_wx else "出继"
  5376. for depth in range(max_depth):
  5377. cursor.execute("""
  5378. SELECT p.id, p.name, p.simplified_name, p.name_word, p.name_word_generation,
  5379. EXISTS(SELECT 1 FROM family_relation_info
  5380. WHERE parent_mid = p.id AND relation_type IN (1,2)) as has_children,
  5381. r.sub_relation_type
  5382. FROM family_relation_info r
  5383. JOIN family_member_info p ON r.parent_mid = p.id
  5384. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  5385. """, (current_id,))
  5386. parents = cursor.fetchall()
  5387. if not parents:
  5388. break
  5389. # 分拣各类父母关系
  5390. normal_parent = None
  5391. adoptive_parent = None
  5392. bio_parent = None
  5393. for p in parents:
  5394. if p['sub_relation_type'] == 3:
  5395. adoptive_parent = p
  5396. elif p['sub_relation_type'] == 2:
  5397. bio_parent = p
  5398. else:
  5399. normal_parent = p
  5400. if mode == 'blood':
  5401. parent = normal_parent or bio_parent or adoptive_parent
  5402. else:
  5403. parent = adoptive_parent or normal_parent or bio_parent
  5404. if parent is adoptive_parent and adoptive_parent is not None:
  5405. bio_name = (bio_parent.get('simplified_name') or bio_parent.get('name')) if bio_parent else None
  5406. adopt_label = f"从{bio_name}出继" if bio_name else "出继"
  5407. if depth == 0:
  5408. anchor_adoption_label_wx = adopt_label
  5409. elif generations:
  5410. generations[-1]['ancestor']['adoption_label'] = adopt_label
  5411. # 祖先卡片不携带子辈关系类型
  5412. parent['sub_relation_type'] = None
  5413. if parent['id'] in visited_ids:
  5414. break
  5415. visited_ids.add(parent['id'])
  5416. cursor.execute("""
  5417. SELECT gp.id FROM family_relation_info r
  5418. JOIN family_member_info gp ON r.parent_mid = gp.id
  5419. WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
  5420. ORDER BY CASE WHEN COALESCE(r.sub_relation_type, 0) = 3 THEN 1 ELSE 0 END, r.id
  5421. LIMIT 1
  5422. """, (parent['id'],))
  5423. grandparent = cursor.fetchone()
  5424. parent_siblings = []
  5425. if grandparent:
  5426. cursor.execute("""
  5427. SELECT COALESCE(child_order, 1) AS child_order
  5428. FROM family_relation_info
  5429. WHERE parent_mid = %s AND child_mid = %s AND relation_type IN (1,2) LIMIT 1
  5430. """, (grandparent['id'], parent['id']))
  5431. co_row = cursor.fetchone()
  5432. parent['child_order'] = co_row['child_order'] if co_row else 1
  5433. cursor.execute("""
  5434. SELECT c.id, c.name, c.simplified_name, c.name_word, c.name_word_generation,
  5435. EXISTS(SELECT 1 FROM family_relation_info
  5436. WHERE parent_mid = c.id AND relation_type IN (1,2)) as has_children,
  5437. COALESCE(r.child_order, 1) AS child_order
  5438. FROM family_relation_info r
  5439. JOIN family_member_info c ON r.child_mid = c.id
  5440. WHERE r.parent_mid = %s AND r.relation_type IN (1,2) AND c.id != %s
  5441. ORDER BY COALESCE(r.child_order, 1), c.id
  5442. LIMIT 10
  5443. """, (grandparent['id'], parent['id']))
  5444. parent_siblings = cursor.fetchall()
  5445. for s in parent_siblings:
  5446. s['has_children'] = bool(s['has_children'])
  5447. else:
  5448. parent['child_order'] = None
  5449. parent['has_children'] = bool(parent['has_children'])
  5450. generations.append({
  5451. 'ancestor': parent,
  5452. 'siblings': list(parent_siblings),
  5453. 'depth': depth
  5454. })
  5455. current_id = parent['id']
  5456. has_more_ancestors = False
  5457. topmost_ancestor_id = None
  5458. if generations:
  5459. topmost_ancestor_id = generations[-1]['ancestor']['id']
  5460. cursor.execute("""
  5461. SELECT COUNT(*) as cnt FROM family_relation_info
  5462. WHERE child_mid = %s AND relation_type IN (1,2)
  5463. """, (topmost_ancestor_id,))
  5464. has_more_ancestors = cursor.fetchone()['cnt'] > 0
  5465. return jsonify({
  5466. "success": True,
  5467. "data": {
  5468. "generations": generations,
  5469. "has_more_ancestors": has_more_ancestors,
  5470. "topmost_ancestor_id": topmost_ancestor_id,
  5471. "anchor_adoption_label": anchor_adoption_label_wx
  5472. }
  5473. })
  5474. except Exception as e:
  5475. print(f"[API Ancestors Above] Error: {e}")
  5476. return jsonify({"success": False, "message": str(e)}), 500
  5477. finally:
  5478. conn.close()
  5479. @app.route('/manager/api/mp/wx/auth/login', methods=['POST'])
  5480. def mp_wx_login():
  5481. """微信小程序登录接口"""
  5482. try:
  5483. data = request.get_json()
  5484. code = data.get('code', '')
  5485. userInfo = data.get('userInfo', {})
  5486. if not code:
  5487. return jsonify({"success": False, "message": "缺少code参数"}), 400
  5488. openid = f"mock_openid_{code[:8]}"
  5489. conn = get_db_connection()
  5490. try:
  5491. with conn.cursor() as cursor:
  5492. cursor.execute("SELECT id, openid, member_id, is_bound FROM family_member_bind WHERE openid = %s", (openid,))
  5493. bind_info = cursor.fetchone()
  5494. if not bind_info:
  5495. cursor.execute("INSERT INTO family_member_bind (openid, created_at) VALUES (%s, CURRENT_TIMESTAMP)", (openid,))
  5496. conn.commit()
  5497. bind_info = {
  5498. 'id': cursor.lastrowid,
  5499. 'openid': openid,
  5500. 'member_id': None,
  5501. 'is_bound': 0
  5502. }
  5503. finally:
  5504. conn.close()
  5505. return jsonify({
  5506. "success": True,
  5507. "data": {
  5508. "openid": openid,
  5509. "token": f"mock_token_{openid}",
  5510. "isBound": bool(bind_info['is_bound']),
  5511. "memberId": bind_info['member_id']
  5512. }
  5513. })
  5514. except Exception as e:
  5515. print(f"[MP Login] Error: {e}")
  5516. return jsonify({"success": False, "message": str(e)}), 500
  5517. @app.route('/manager/api/mp/wx/config/getConfig', methods=['GET'])
  5518. def mp_wx_get_config():
  5519. """获取配置信息"""
  5520. config_key = request.args.get('configKey', '')
  5521. config_data = {
  5522. "CAROUSEL": {
  5523. "success": True,
  5524. "images": [
  5525. {
  5526. "image": "",
  5527. "title": "留家族旅",
  5528. "subtitle": "传承家族文化"
  5529. }
  5530. ]
  5531. },
  5532. "HONOR": {
  5533. "success": True,
  5534. "data": {
  5535. "name": "留越",
  5536. "role": "族谱发起人",
  5537. "desc": "2025年发起族谱建设,统筹信息收集"
  5538. }
  5539. }
  5540. }
  5541. result = config_data.get(config_key, {"success": False, "message": "配置不存在"})
  5542. return jsonify(result)
  5543. @app.route('/manager/api/mp/wx/family/member/selfCard', methods=['GET'])
  5544. def mp_wx_self_card():
  5545. """获取用户自己的卡片信息"""
  5546. openid = request.headers.get('X-MP-Openid', '')
  5547. if not openid:
  5548. return jsonify({"success": False, "message": "未登录"}), 401
  5549. conn = get_db_connection()
  5550. try:
  5551. with conn.cursor() as cursor:
  5552. cursor.execute("SELECT member_id FROM family_member_bind WHERE openid = %s", (openid,))
  5553. bind_info = cursor.fetchone()
  5554. if not bind_info or not bind_info['member_id']:
  5555. return jsonify({"success": False, "message": "未绑定成员"})
  5556. 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'],))
  5557. member = cursor.fetchone()
  5558. if not member:
  5559. return jsonify({"success": False, "message": "成员不存在"})
  5560. return jsonify({
  5561. "success": True,
  5562. "data": member
  5563. })
  5564. finally:
  5565. conn.close()
  5566. @app.route('/manager/api/mp/wx/family/member/search', methods=['GET'])
  5567. def mp_wx_search_members():
  5568. """搜索家族成员"""
  5569. keyword = request.args.get('keyword', '')
  5570. conn = get_db_connection()
  5571. try:
  5572. with conn.cursor() as cursor:
  5573. if keyword:
  5574. cursor.execute("""
  5575. SELECT id, name, simplified_name, sex, birthday, family_rank
  5576. FROM family_member_info
  5577. WHERE name LIKE %s OR simplified_name LIKE %s
  5578. ORDER BY name_word_generation ASC, id ASC
  5579. LIMIT 20
  5580. """, (f"%{keyword}%", f"%{keyword}%"))
  5581. else:
  5582. cursor.execute("""
  5583. SELECT id, name, simplified_name, sex, birthday, family_rank
  5584. FROM family_member_info
  5585. ORDER BY name_word_generation ASC, id ASC
  5586. LIMIT 20
  5587. """)
  5588. members = cursor.fetchall()
  5589. return jsonify({
  5590. "success": True,
  5591. "data": members
  5592. })
  5593. finally:
  5594. conn.close()
  5595. @app.route('/manager/api/mp/wx/family/member/bind', methods=['POST'])
  5596. def mp_wx_bind_member():
  5597. """绑定用户到家族成员"""
  5598. try:
  5599. data = request.get_json()
  5600. openid = data.get('openid', '')
  5601. member_id = data.get('memberId', '')
  5602. if not openid or not member_id:
  5603. return jsonify({"success": False, "message": "参数错误"}), 400
  5604. conn = get_db_connection()
  5605. try:
  5606. with conn.cursor() as cursor:
  5607. cursor.execute("SELECT id FROM family_member_info WHERE id = %s", (member_id,))
  5608. member = cursor.fetchone()
  5609. if not member:
  5610. return jsonify({"success": False, "message": "成员不存在"})
  5611. cursor.execute("UPDATE family_member_bind SET member_id = %s, is_bound = 1, updated_at = CURRENT_TIMESTAMP WHERE openid = %s", (member_id, openid))
  5612. conn.commit()
  5613. return jsonify({"success": True, "message": "绑定成功"})
  5614. finally:
  5615. conn.close()
  5616. except Exception as e:
  5617. print(f"[MP Bind] Error: {e}")
  5618. return jsonify({"success": False, "message": str(e)}), 500
  5619. @app.route('/manager/api/mp/wx/family/lineage', methods=['GET'])
  5620. def mp_wx_get_lineage():
  5621. """获取世系信息"""
  5622. openid = request.headers.get('X-MP-Openid', '')
  5623. member_id = request.args.get('memberId', '')
  5624. if not openid:
  5625. return jsonify({"success": False, "message": "未登录"}), 401
  5626. conn = get_db_connection()
  5627. try:
  5628. if not member_id:
  5629. with conn.cursor() as cursor:
  5630. cursor.execute("SELECT member_id FROM family_member_bind WHERE openid = %s", (openid,))
  5631. bind_info = cursor.fetchone()
  5632. if bind_info and bind_info['member_id']:
  5633. member_id = bind_info['member_id']
  5634. else:
  5635. return jsonify({"success": False, "message": "未绑定成员"})
  5636. with conn.cursor() as cursor:
  5637. cursor.execute("""
  5638. SELECT id, name, simplified_name, sex, name_word_generation, birthday, occupation, family_rank, branch_family_hall, residential_address
  5639. FROM family_member_info
  5640. WHERE id = %s
  5641. """, (member_id,))
  5642. member = cursor.fetchone()
  5643. if not member:
  5644. return jsonify({"success": False, "message": "成员不存在"})
  5645. current_member = {
  5646. "id": member['id'],
  5647. "name": member['name'],
  5648. "simplified_name": member['simplified_name'],
  5649. "sex": member['sex'],
  5650. "name_word_generation": member['name_word_generation'],
  5651. "birthday": member['birthday'],
  5652. "occupation": member['occupation'],
  5653. "family_rank": member['family_rank'],
  5654. "branch_family_hall": member['branch_family_hall'],
  5655. "residential_address": member['residential_address']
  5656. }
  5657. cursor.execute("""
  5658. SELECT p.id, p.name, p.simplified_name, p.sex, p.name_word_generation, p.birthday
  5659. FROM family_relation_info r
  5660. JOIN family_member_info p ON r.parent_mid = p.id
  5661. WHERE r.child_mid = %s AND r.relation_type = 1
  5662. """, (member_id,))
  5663. father = cursor.fetchone()
  5664. cursor.execute("""
  5665. SELECT p.id, p.name, p.simplified_name, p.sex, p.name_word_generation, p.birthday
  5666. FROM family_relation_info r
  5667. JOIN family_member_info p ON r.parent_mid = p.id
  5668. WHERE r.child_mid = %s AND r.relation_type = 2
  5669. """, (member_id,))
  5670. mother = cursor.fetchone()
  5671. ancestors = []
  5672. if father or mother:
  5673. ancestors.append({
  5674. "father": father,
  5675. "mother": mother
  5676. })
  5677. cursor.execute("""
  5678. SELECT c.id, c.name, c.simplified_name, c.sex, c.name_word_generation, c.birthday, r.child_order
  5679. FROM family_relation_info r
  5680. JOIN family_member_info c ON r.child_mid = c.id
  5681. WHERE r.parent_mid = %s AND r.relation_type IN (1, 2)
  5682. ORDER BY COALESCE(r.child_order, 999) ASC
  5683. """, (member_id,))
  5684. children = cursor.fetchall()
  5685. cursor.execute("""
  5686. SELECT DISTINCT s.id, s.name, s.simplified_name, s.sex
  5687. FROM family_relation_info r1
  5688. JOIN family_relation_info r2 ON r1.parent_mid = r2.parent_mid
  5689. JOIN family_member_info s ON r2.child_mid = s.id
  5690. WHERE r1.child_mid = %s AND r2.child_mid != %s
  5691. ORDER BY COALESCE(r2.child_order, 999) ASC
  5692. """, (member_id, member_id))
  5693. siblings = cursor.fetchall()
  5694. return jsonify({
  5695. "success": True,
  5696. "data": {
  5697. "member": current_member,
  5698. "ancestors": ancestors,
  5699. "children": children,
  5700. "siblings": siblings
  5701. }
  5702. })
  5703. finally:
  5704. conn.close()
  5705. @app.route('/manager/api/mp/wx/family/member/add', methods=['POST'])
  5706. def mp_wx_add_member():
  5707. """添加家族成员"""
  5708. try:
  5709. data = request.get_json()
  5710. openid = data.get('openid', '')
  5711. member_data = data.get('memberData', {})
  5712. relation_data = data.get('relationData', {})
  5713. if not openid:
  5714. return jsonify({"success": False, "message": "未登录"}), 401
  5715. conn = get_db_connection()
  5716. try:
  5717. with conn.cursor() as cursor:
  5718. member_info = {
  5719. 'name': member_data.get('name', ''),
  5720. 'simplified_name': member_data.get('simplified_name', '') or member_data.get('name', ''),
  5721. 'sex': member_data.get('sex', 1),
  5722. 'birthday': member_data.get('birthday', ''),
  5723. 'occupation': member_data.get('occupation', ''),
  5724. 'family_rank': member_data.get('family_rank', ''),
  5725. 'branch_family_hall': member_data.get('branch_family_hall', ''),
  5726. 'residential_address': member_data.get('residential_address', ''),
  5727. 'genealogy_text': member_data.get('genealogy_text', ''),
  5728. 'create_time': datetime.now(),
  5729. 'modified_time': datetime.now()
  5730. }
  5731. fields = ", ".join(member_info.keys())
  5732. placeholders = ", ".join(["%s"] * len(member_info))
  5733. sql = f"INSERT INTO family_member_info ({fields}) VALUES ({placeholders})"
  5734. cursor.execute(sql, list(member_info.values()))
  5735. new_member_id = cursor.lastrowid
  5736. relation_type = relation_data.get('relationType', '')
  5737. if relation_type:
  5738. parent_id = relation_data.get('parentId')
  5739. child_order = relation_data.get('childOrder', 1)
  5740. if parent_id and relation_type in ['father', 'mother', 'child']:
  5741. rel_type = 1 if relation_type == 'father' else 2 if relation_type == 'mother' else 1
  5742. cursor.execute("""
  5743. INSERT INTO family_relation_info
  5744. (parent_mid, child_mid, relation_type, child_order, source_mid, generation_diff)
  5745. VALUES (%s, %s, %s, %s, %s, 1)
  5746. """, (parent_id, new_member_id, rel_type, child_order, new_member_id))
  5747. conn.commit()
  5748. return jsonify({
  5749. "success": True,
  5750. "message": "添加成功",
  5751. "memberId": new_member_id
  5752. })
  5753. except Exception as e:
  5754. conn.rollback()
  5755. raise e
  5756. finally:
  5757. conn.close()
  5758. except Exception as e:
  5759. print(f"[MP Add Member] Error: {e}")
  5760. return jsonify({"success": False, "message": str(e)}), 500
  5761. @app.route('/manager/api/mp/wx/family/member/update', methods=['POST'])
  5762. def mp_wx_update_member():
  5763. """更新家族成员信息"""
  5764. try:
  5765. data = request.get_json()
  5766. openid = data.get('openid', '')
  5767. member_id = data.get('memberId', '')
  5768. update_data = data.get('updateData', {})
  5769. if not openid or not member_id:
  5770. return jsonify({"success": False, "message": "参数错误"}), 400
  5771. conn = get_db_connection()
  5772. try:
  5773. update_parts = []
  5774. params = []
  5775. if 'name' in update_data:
  5776. update_parts.append("name = %s")
  5777. params.append(update_data['name'])
  5778. if 'simplified_name' in update_data:
  5779. update_parts.append("simplified_name = %s")
  5780. params.append(update_data['simplified_name'])
  5781. if 'sex' in update_data:
  5782. update_parts.append("sex = %s")
  5783. params.append(update_data['sex'])
  5784. if 'birthday' in update_data:
  5785. update_parts.append("birthday = %s")
  5786. params.append(update_data['birthday'])
  5787. if 'occupation' in update_data:
  5788. update_parts.append("occupation = %s")
  5789. params.append(update_data['occupation'])
  5790. if 'family_rank' in update_data:
  5791. update_parts.append("family_rank = %s")
  5792. params.append(update_data['family_rank'])
  5793. if 'branch_family_hall' in update_data:
  5794. update_parts.append("branch_family_hall = %s")
  5795. params.append(update_data['branch_family_hall'])
  5796. if 'residential_address' in update_data:
  5797. update_parts.append("residential_address = %s")
  5798. params.append(update_data['residential_address'])
  5799. update_parts.append("modified_time = CURRENT_TIMESTAMP")
  5800. params.append(member_id)
  5801. sql = f"UPDATE family_member_info SET {', '.join(update_parts)} WHERE id = %s"
  5802. with conn.cursor() as cursor:
  5803. cursor.execute(sql, params)
  5804. conn.commit()
  5805. return jsonify({"success": True, "message": "更新成功"})
  5806. except Exception as e:
  5807. conn.rollback()
  5808. raise e
  5809. finally:
  5810. conn.close()
  5811. except Exception as e:
  5812. print(f"[MP Update Member] Error: {e}")
  5813. return jsonify({"success": False, "message": str(e)}), 500
  5814. # ==================== End 微信小程序 API 接口 ====================
  5815. if __name__ == '__main__':
  5816. app.run(debug=False, host='0.0.0.0', port=5001)