';
document.body.appendChild(loadingOverlay);
generatePosterForSelectedWithCallback(selected, function(blob){
var loading=document.getElementById('posterLoading');
if(loading)loading.remove();
var url=URL.createObjectURL(blob);
showPosterPreview(url, blob);
}, function(){
var loading=document.getElementById('posterLoading');
if(loading)loading.remove();
showToast('生成失败,请重试');
});
}
}
function doCopySelected(selectedIds){
var items=allNews[currentDate]||[];
var d=fmtDate(currentDate);
var text='📰 单词妈妈·每日教育资讯 '+d.month+d.day+' '+d.weekday+'\n\n';
var catGroups={};
selectedIds.forEach(function(id){
var item=items.find(function(i){return i.id==id});
if(item){
var cat=CAT[item.category]?CAT[item.category].name:item.category;
if(!catGroups[cat])catGroups[cat]=[];
catGroups[cat].push(item);
}
});
for(var cat in catGroups){
text+='【'+cat+'】\n';
catGroups[cat].forEach(function(i,idx){text+=(idx+1)+'. '+i.summary+'\n\n'}); // 每条新闻之间空一行
text+='\n';
}
text+='💡 AI重塑学习力,让科学用脑流行起来!\n详见:https://wordma.ai8.news';
navigator.clipboard.writeText(text).then(function(){
var shares=getShares();shares.unshift({date:currentDate});if(shares.length>100)shares=shares.slice(0,100);saveShares(shares);
trackStats('copy',0);
showToast('已复制到剪贴板');
}).catch(function(){showToast('复制失败')});
}
// ========== 建议和测验 ==========
function renderSuggestions(){
var sc=document.getElementById('sugContainer');
var items=allNews[currentDate]||[];
var aiC=items.filter(function(i){return i.category==='ai'}).length;
var poC=items.filter(function(i){return i.category==='policy'}).length;
var adC=items.filter(function(i){return i.category==='admission'}).length;
var suC=items.filter(function(i){return i.category==='subject'}).length;
var nsC=items.filter(function(i){return i.category==='non-subject'}).length;
var sugs=[];
if(aiC)sugs.push({t:'家长',c:'建议尝试AI学习工具,如豆包学伴等,可提升孩子学习效率30%以上',p:true},{t:'从业者',c:'关注AI教育赛道,布局智能学习系统开发',p:true});
if(poC)sugs.push({t:'家长',c:'关注课程改革动态,提前规划孩子综合素养培养方向',p:false},{t:'从业者',c:'研读最新政策,调整教学内容以适应新课标要求',p:false});
if(adC)sugs.push({t:'家长',c:'及时了解升学政策变化,提早准备报名材料',p:false});
if(suC)sugs.push({t:'家长',c:'关注英语等学科学习趋势,善用数字化学习资源',p:false});
if(nsC)sugs.push({t:'家长',c:'重视素质教育,培养孩子艺术体育等综合能力',p:false});
if(sugs.length){sc.innerHTML='
💡 单词妈妈今日教育行动建议 '+sugs.map(function(s){return'
'+s.t+' '+s.c+'
'}).join('')+'
'}else{sc.innerHTML=''}
}
function renderQuiz(){
var section=document.getElementById('quizSection');
var daily=allNews[currentDate+'_meta'];
if(!daily||!daily.quiz){section.style.display='none';return}
section.style.display='block';currentQuiz=daily.quiz;quizAnswered=false;
document.getElementById('quizQuestion').textContent=currentQuiz.question;
document.getElementById('quizOptions').innerHTML=currentQuiz.options.map(function(opt,idx){return''+opt+' '}).join('');
document.getElementById('quizResult').classList.remove('show');
}
function selectQuizOption(el,idx){
if(quizAnswered)return;quizAnswered=true;
document.querySelectorAll('.quiz-option').forEach(function(o){o.classList.remove('selected')});
el.classList.add('selected');
// 更新答题记录
var quiz=getQuizRecord();
quiz.total++;
if(idx===currentQuiz.answer){
el.classList.add('correct');
quiz.correct++;
}else{
el.classList.add('wrong');
document.querySelectorAll('.quiz-option')[currentQuiz.answer].classList.add('correct');
}
saveQuizRecord(quiz);
var res=document.getElementById('quizResult');
document.getElementById('quizResultTitle').textContent=idx===currentQuiz.answer?'✅ 回答正确!':'❌ 回答错误';
document.getElementById('quizResultText').textContent=currentQuiz.explanation||'';
res.classList.add('show');
}
// ========== 日期和加载 ==========
function updateDateDisplay(){
var d=fmtDate(currentDate);
var el=document.getElementById('dateMainTop');
if(el) el.textContent=d.month+d.day;
el=document.getElementById('dateWeekdayTop');
if(el) el.textContent=d.weekday;
el=document.getElementById('dateMainTop');
if(el) el.textContent=d.month+d.day;
el=document.getElementById('dateWeekdayTop');
if(el) el.textContent=d.weekday;
}
function loadAllNews(){console.log("loadAllNews called");
return fetch('/api/news/all').then(function(res){return res.json()}).then(function(json){
if(json.success&&json.data){
allNewsFlat=[];
for(var date in json.data){
if(date.endsWith('_meta'))continue;
allNews[date]=json.data[date];
if(json.data[date+'_meta'])allNews[date+'_meta']=json.data[date+'_meta'];
if(Array.isArray(json.data[date])){
json.data[date].forEach(function(item){item.date=date;allNewsFlat.push(item)});
}
}
dates=Object.keys(allNews).filter(function(d){return !d.endsWith('_meta')&&d.includes('-')}).sort().reverse();
currentDate=dates[0]||new Date().toISOString().slice(0,10);
}
}).catch(function(e){showToast('加载失败')});
}
function loadData(){console.log("loadData called");
// 先清除"正在加载..."状态
var list=document.getElementById('newsList');
list.innerHTML='';
loadAllNews().then(function(){
updateDateDisplay();
renderNews();
updateStreak();
trackStats('view',0);
}).catch(function(e){
console.error('Load error:',e);
// 即使加载失败也尝试渲染(可能有缓存数据)
if(typeof renderNews === 'function'){
updateDateDisplay();
renderNews();
}
});
}
// ========== 搜索 ==========
function doSearch(kw){
kw=kw.toLowerCase().trim();
if(!kw)return;
var results=allNewsFlat.filter(function(i){return i.summary.toLowerCase().indexOf(kw)>=0}).slice(0,10);
var drop=document.getElementById('searchDropdown');
if(!results.length){drop.innerHTML='未找到相关资讯
';drop.classList.add('show');return}
drop.innerHTML=results.map(function(r){return''+CAT[r.category].name+' '+r.summary.substring(0,40)+'
'}).join('');
drop.classList.add('show');
}
document.getElementById('tabs').addEventListener('click',function(e){
if(!e.target.classList.contains('tab'))return;
aiExpanded=false;
document.querySelectorAll('.tab').forEach(function(t){t.classList.remove('active')});
e.target.classList.add('active');currentCat=e.target.dataset.cat;currentExpandedId=null;renderNews();
});
function changeDate(delta){
var idx=dates.indexOf(currentDate),newIdx=idx+delta;
if(newIdx>=0&&newIdx'+m+' ';grouped[m].sort().reverse().forEach(function(d){var dt=fmtDate(d);html+=''+dt.month+dt.day+' ('+dt.weekday+')
'});html+=''}
body.innerHTML=html;openM('archiveModal');
});
document.getElementById('archiveBody').addEventListener('click',function(e){
var item=e.target.closest('.archive-item');
if(item){currentDate=item.dataset.date;aiExpanded=false;currentExpandedId=null;updateDateDisplay();renderNews();closeM('archiveModal');window.scrollTo({top:0,behavior:'smooth'})}
});
document.getElementById('modalOverlay').addEventListener('click',function(){
['cityModal','archiveModal','myModal'].forEach(function(id){document.getElementById(id).classList.remove('show')});
closeSelectModal();
this.classList.remove('show');
});
// ========== 底部按钮 ==========
document.getElementById('posterBtn').addEventListener('click',function(){openSelectModal('poster')});
document.getElementById('copyBtn').addEventListener('click',function(){openSelectModal('copy')});
// ========== 分享按钮 - 优先Web Share API ==========
document.getElementById('shareBtn').addEventListener('click',function(){
trackStats('share',0);
var url='https://wordma.ai8.news';
var title='单词妈妈·每日教育资讯';
var text='AI重塑学习力,让科学用脑流行起来!';
// 优先尝试 Web Share API (微信中可拉起分享面板)
if(navigator.share){
navigator.share({title:title,text:text,url:url}).then(function(){
showToast('分享成功');
}).catch(function(err){
// 用户取消或不支持,静默降级
copyToClipboard(url);
});
}else{
// 降级为复制链接
copyToClipboard(url);
}
});
function copyToClipboard(text){
navigator.clipboard.writeText(text).then(function(){
showToast('链接已复制,快去分享吧!');
}).catch(function(){
showToast('复制失败,请长按复制');
});
}
// ========== QR码生成 ==========
function generateQRCode(text,size){
var qr=[];
var typeNumber=4;
var moduleCount=17+typeNumber*4;
qr=Array(moduleCount).fill(null).map(function(){return Array(moduleCount).fill(false)});
addFinderPattern(qr,0,0);
addFinderPattern(qr,moduleCount-7,0);
addFinderPattern(qr,0,moduleCount-7);
addAlignmentPattern(qr,moduleCount);
for(var i=8;i0)row--;else{dir=false;row++;col--}
}else{
if(row=2&&r<=4&&c>=2&&c<=4)){
qr[x+r]&&(qr[x+r][y+c]=true);
}
}
}
for(var i=0;i<8;i++){
if(qr[x+7])qr[x+7][y+i]=false;
if(qr[x+i])qr[x+i][y+7]=false;
if(qr[x+7])qr[x+7][y+moduleCount-1-i]=false;
if(qr[x+i])qr[moduleCount-1-i]&&(qr[moduleCount-1-i][y+7]=false);
}
}
function addAlignmentPattern(qr,count){
var pos=[count-9,count-7,count-5,count-3];
pos.forEach(function(p){
for(var r=p-2;r<=p+2;r++){
for(var c=p-2;c<=p+2;c++){
if(!qr[r])qr[r]=[];
if(r===p-2||r===p+2||c===p-2||c===p+2||(r===p&&c===p)){
qr[r][c]=true;
}
}
}
});
}
function encodeData(text,size){
var data=[];
for(var i=0;i=0;b--){
data.push((code>>b)&1===1);
}
}
return data;
}
// ========== 海报生成(带Logo和二维码)==========
function generatePosterForSelected(selectedIds){
var items=allNews[currentDate]||[];
var selected=items.filter(function(i){return selectedIds.indexOf(i.id)>=0});
if(selected.length===0){showToast('请选择内容');return}
var d=fmtDate(currentDate);
var city=currentCity||currentProvince||'全国';
var daily=allNews[currentDate+'_meta'];
var W=540;
var LOGO_H=50;
var HEADER_H=60;
var ITEM_H=45;
var QR_H=100;
var FOOTER_H=80;
var QUOTE_H=50;
var totalItems=selected.length;
var H=LOGO_H+20+50+totalItems*ITEM_H+QUOTE_H+QR_H+FOOTER_H;
if(H>3600)H=3600;
var canvas=document.createElement('canvas');
canvas.width=W;canvas.height=H;
var ctx=canvas.getContext('2d');
// 预加载Logo图片
var logoImg=new Image();
logoImg.src='/logo.jpg';
logoImg.onload=function(){
// 顶部Logo
var logoW=LOGO_H*(logoImg.width/logoImg.height);
var logoX=(W-logoW)/2;
ctx.drawImage(logoImg,logoX,5,logoW,LOGO_H);
// 顶部渐变
var grad=ctx.createLinearGradient(0,LOGO_H+10,W,LOGO_H+HEADER_H);
grad.addColorStop(0,'#FF6B2B');grad.addColorStop(1,'#FF8F5E');
ctx.fillStyle=grad;ctx.fillRect(0,LOGO_H+10,W,HEADER_H);
ctx.fillStyle='#fff';ctx.font='bold 16px sans-serif';
ctx.fillText('单词妈妈',15,LOGO_H+38);
ctx.font='11px sans-serif';
ctx.fillText(city+' | '+d.month+d.day+' '+d.weekday,W-160,LOGO_H+38);
// 内容区
var y=LOGO_H+HEADER_H+20;
ctx.fillStyle='#f5f5f5';ctx.fillRect(0,y,W,H-y-QUOTE_H-QR_H-FOOTER_H);
// 按分类分组显示
var catGroups={};
selected.forEach(function(item){
var cat=item.category;
if(!catGroups[cat])catGroups[cat]=[];
catGroups[cat].push(item);
});
CAT_ORDER.forEach(function(cat){
var catItems=catGroups[cat];
if(!catItems||catItems.length===0)return;
var catInfo=CAT[cat];
ctx.fillStyle='#fff';ctx.font='bold 12px sans-serif';
ctx.fillText(catInfo.emoji+' '+catInfo.name+' ('+catItems.length+'条)',15,y+12);
y+=28;
catItems.forEach(function(item,idx){
ctx.fillStyle=catInfo.color;
ctx.beginPath();ctx.arc(18,y+7,5,0,Math.PI*2);ctx.fill();
ctx.fillStyle='#fff';ctx.font='9px sans-serif';ctx.textAlign='center';
ctx.fillText(idx+1,18,y+10);ctx.textAlign='left';
ctx.fillStyle='#333';ctx.font='12px sans-serif';
var title=item.summary.substring(0,28)+(item.summary.length>28?'...':'');
ctx.fillText(title,30,y+11);
y+=ITEM_H;
});
y+=4;
});
// 今日一句
var quote=daily&&daily.dailyQuote?'"'+daily.dailyQuote+'"':'"最好的教育,是和孩子一起成长"';
y+=5;
ctx.fillStyle='#666';ctx.font='italic 12px sans-serif';ctx.textAlign='center';
ctx.fillText(quote,W/2,y+12);ctx.textAlign='left';
// 二维码
y+=35;
try{
var qrCanvas=generateQRCode('https://wordma.ai8.news',80);
ctx.drawImage(qrCanvas,W/2-40,y);
ctx.fillStyle='#666';ctx.font='10px sans-serif';ctx.textAlign='center';
ctx.fillText('扫码查看更多教育新闻',W/2,y+90);
}catch(e){}
ctx.textAlign='left';
// 底部
y=H-FOOTER_H;
var grad2=ctx.createLinearGradient(0,y,W,y+FOOTER_H);
grad2.addColorStop(0,'#FF6B2B');grad2.addColorStop(1,'#FF8F5E');
ctx.fillStyle=grad2;ctx.fillRect(0,y,W,FOOTER_H);
ctx.fillStyle='#fff';ctx.font='bold 14px sans-serif';ctx.textAlign='center';
ctx.fillText('AI重塑学习力,让科学用脑流行起来!',W/2,y+30);
ctx.fillStyle='rgba(255,255,255,0.8)';ctx.font='12px sans-serif';
ctx.fillText('wordma.ai8.news',W/2,y+52);ctx.textAlign='left';
savePoster(canvas);
};
logoImg.onerror=function(){
// Logo加载失败,使用纯文字版本
generatePosterWithoutLogo(selected,d,city,daily,W,H);
};
}
function generatePosterWithoutLogo(selected,d,city,daily,W,H){
var canvas=document.createElement('canvas');
canvas.width=W;canvas.height=H;
var ctx=canvas.getContext('2d');
var LOGO_H=0;
var HEADER_H=60;
var ITEM_H=45;
var QR_H=100;
var FOOTER_H=80;
var QUOTE_H=50;
var grad=ctx.createLinearGradient(0,0,W,HEADER_H);
grad.addColorStop(0,'#FF6B2B');grad.addColorStop(1,'#FF8F5E');
ctx.fillStyle=grad;ctx.fillRect(0,0,W,HEADER_H);
ctx.fillStyle='#fff';ctx.font='bold 18px sans-serif';
ctx.fillText('单词妈妈·每日教育资讯',15,38);
ctx.font='12px sans-serif';
ctx.fillText(city+' | '+d.month+d.day+' '+d.weekday,W-180,38);
var y=HEADER_H+20;
ctx.fillStyle='#f5f5f5';ctx.fillRect(0,y,W,H-HEADER_H-QUOTE_H-QR_H-FOOTER_H);
var catGroups={};
selected.forEach(function(item){
var cat=item.category;
if(!catGroups[cat])catGroups[cat]=[];
catGroups[cat].push(item);
});
CAT_ORDER.forEach(function(cat){
var catItems=catGroups[cat];
if(!catItems||catItems.length===0)return;
var catInfo=CAT[cat];
ctx.fillStyle='#fff';ctx.font='bold 13px sans-serif';
ctx.fillText(catInfo.emoji+' '+catInfo.name+' ('+catItems.length+'条)',15,y+15);
y+=32;
catItems.forEach(function(item,idx){
ctx.fillStyle=catInfo.color;
ctx.beginPath();ctx.arc(20,y+8,6,0,Math.PI*2);ctx.fill();
ctx.fillStyle='#fff';ctx.font='9px sans-serif';ctx.textAlign='center';
ctx.fillText(idx+1,20,y+11);ctx.textAlign='left';
ctx.fillStyle='#333';ctx.font='12px sans-serif';
var title=item.summary.substring(0,30)+(item.summary.length>30?'...':'');
ctx.fillText(title,35,y+12);
y+=ITEM_H;
});
y+=5;
});
var quote=daily&&daily.dailyQuote?'"'+daily.dailyQuote+'"':'"最好的教育,是和孩子一起成长"';
y+=5;
ctx.fillStyle='#666';ctx.font='italic 13px sans-serif';ctx.textAlign='center';
ctx.fillText(quote,W/2,y+15);ctx.textAlign='left';
y+=40;
try{
var qrCanvas=generateQRCode('https://wordma.ai8.news',80);
ctx.drawImage(qrCanvas,W/2-40,y);
ctx.fillStyle='#666';ctx.font='10px sans-serif';ctx.textAlign='center';
ctx.fillText('扫码查看更多教育新闻',W/2,y+90);
}catch(e){}
ctx.textAlign='left';
y=H-FOOTER_H;
var grad2=ctx.createLinearGradient(0,y,W,y+FOOTER_H);
grad2.addColorStop(0,'#FF6B2B');grad2.addColorStop(1,'#FF8F5E');
ctx.fillStyle=grad2;ctx.fillRect(0,y,W,FOOTER_H);
ctx.fillStyle='#fff';ctx.font='bold 14px sans-serif';ctx.textAlign='center';
ctx.fillText('AI重塑学习力,让科学用脑流行起来!',W/2,y+30);
ctx.fillStyle='rgba(255,255,255,0.8)';ctx.font='12px sans-serif';
ctx.fillText('wordma.ai8.news',W/2,y+52);ctx.textAlign='left';
savePoster(canvas);
}
function savePoster(canvas){
showToast('正在生成海报...');
canvas.toBlob(function(blob){
var url=URL.createObjectURL(blob);
var a=document.createElement('a');a.href=url;a.download='单词妈妈教育早报_'+currentDate+'.png';a.click();
URL.revokeObjectURL(url);showToast('海报已保存');
trackStats('poster',0);
});
}
// 默认海报(全部内容)
function generatePoster(){
var items=allNews[currentDate]||[];
var selectedIds=items.map(function(i){return i.id});
generatePosterForSelected(selectedIds);
}
// 初始化
// ========== 海报预览弹窗 ==========
function showPosterPreview(url, blob){
var modal=document.createElement('div');
modal.id='posterPreviewModal';
modal.style='position:fixed;top:0;left:0;right:0;bottom:0;background:rgba(0,0,0,0.85);z-index:10001;display:flex;flex-direction:column;align-items:center;padding:20px;';
var maxW=Math.min(window.innerWidth-40, 360);
var scale=maxW/540;
var maxH=window.innerHeight-160;
var imgH=Math.min(540*scale, maxH);
modal.innerHTML='海报预览
'+
' '+
''+
'保存图片 '+
'分享给朋友 '+
'
'+
'关闭 ';
document.body.appendChild(modal);
document.getElementById('closePreviewBtn').onclick=function(){
modal.remove();
URL.revokeObjectURL(url);
};
document.getElementById('savePosterBtn').onclick=function(){
var a=document.createElement('a');
a.href=url;
a.download='单词妈妈教育早报_'+currentDate+'.png';
a.click();
showToast('海报已保存');
trackStats('poster',0);
};
document.getElementById('sharePosterBtn').onclick=function(){
if(navigator.share && navigator.canShare){
var file=new File([blob],'单词妈妈教育早报.png',{type:'image/png'});
if(navigator.canShare({files:[file]})){
navigator.share({files:[file],title:'单词妈妈·每日教育资讯',text:'AI重塑学习力,让科学用脑流行起来!'}).then(function(){
showToast('分享成功');
}).catch(function(){
var a=document.createElement('a');
a.href=url;
a.download='单词妈妈教育早报_'+currentDate+'.png';
a.click();
showToast('已保存图片,请手动分享');
});
}else{
copyToClipboard('https://wordma.ai8.news');
}
}else{
copyToClipboard('https://wordma.ai8.news');
showToast('已复制链接,请手动分享');
}
};
}
// ========== 海报生成(带回调版本)==========
function generatePosterForSelectedWithCallback(selectedIds, onSuccess, onError){
var items=allNews[currentDate]||[];
var selected=items.filter(function(i){return selectedIds.indexOf(i.id)>=0});
if(selected.length===0){showToast('请选择内容');if(onError)onError();return}
var d=fmtDate(currentDate);
var city=currentCity||currentProvince||'全国';
var daily=allNews[currentDate+'_meta'];
var W=540;
var LOGO_H=50;
var HEADER_H=60;
var ITEM_H=45;
var QR_H=100;
var FOOTER_H=80;
var QUOTE_H=50;
var totalItems=selected.length;
var H=LOGO_H+20+50+totalItems*ITEM_H+QUOTE_H+QR_H+FOOTER_H;
if(H>3600)H=3600;
var canvas=document.createElement('canvas');
canvas.width=W;canvas.height=H;
var ctx=canvas.getContext('2d');
var logoImg=new Image();
logoImg.src='/logo.jpg';
logoImg.onload=function(){
var logoW=LOGO_H*(logoImg.width/logoImg.height);
var logoX=(W-logoW)/2;
ctx.drawImage(logoImg,logoX,5,logoW,LOGO_H);
var grad=ctx.createLinearGradient(0,LOGO_H+10,W,LOGO_H+HEADER_H);
grad.addColorStop(0,'#FF6B2B');grad.addColorStop(1,'#FF8F5E');
ctx.fillStyle=grad;ctx.fillRect(0,LOGO_H+10,W,HEADER_H);
ctx.fillStyle='#fff';ctx.font='bold 16px sans-serif';
ctx.fillText('单词妈妈',15,LOGO_H+38);
ctx.font='11px sans-serif';
ctx.fillText(city+' | '+d.month+d.day+' '+d.weekday,W-160,LOGO_H+38);
var y=LOGO_H+HEADER_H+20;
ctx.fillStyle='#f5f5f5';ctx.fillRect(0,y,W,H-y-QUOTE_H-QR_H-FOOTER_H);
var catGroups={};
selected.forEach(function(item){
var cat=item.category;
if(!catGroups[cat])catGroups[cat]=[];
catGroups[cat].push(item);
});
CAT_ORDER.forEach(function(cat){
var catItems=catGroups[cat];
if(!catItems||catItems.length===0)return;
var catInfo=CAT[cat];
ctx.fillStyle='#fff';ctx.font='bold 12px sans-serif';
ctx.fillText(catInfo.emoji+' '+catInfo.name+' ('+catItems.length+'条)',15,y+12);
y+=28;
catItems.forEach(function(item,idx){
ctx.fillStyle=catInfo.color;
ctx.beginPath();ctx.arc(18,y+7,5,0,Math.PI*2);ctx.fill();
ctx.fillStyle='#fff';ctx.font='9px sans-serif';ctx.textAlign='center';
ctx.fillText(idx+1,18,y+10);ctx.textAlign='left';
ctx.fillStyle='#333';ctx.font='12px sans-serif';
var title=item.summary.substring(0,28)+(item.summary.length>28?'...':'');
ctx.fillText(title,30,y+11);
y+=ITEM_H;
});
y+=4;
});
var quote=daily&&daily.dailyQuote?'"'+daily.dailyQuote+'"':'"最好的教育,是和孩子一起成长"';
y+=5;
ctx.fillStyle='#666';ctx.font='italic 12px sans-serif';ctx.textAlign='center';
ctx.fillText(quote,W/2,y+12);ctx.textAlign='left';
y+=35;
try{
var qrCanvas=generateQRCode('https://wordma.ai8.news',80);
ctx.drawImage(qrCanvas,W/2-40,y);
ctx.fillStyle='#666';ctx.font='10px sans-serif';ctx.textAlign='center';
ctx.fillText('扫码查看更多教育新闻',W/2,y+90);
}catch(e){}
ctx.textAlign='left';
y=H-FOOTER_H;
var grad2=ctx.createLinearGradient(0,y,W,y+FOOTER_H);
grad2.addColorStop(0,'#FF6B2B');grad2.addColorStop(1,'#FF8F5E');
ctx.fillStyle=grad2;ctx.fillRect(0,y,W,FOOTER_H);
ctx.fillStyle='#fff';ctx.font='bold 14px sans-serif';ctx.textAlign='center';
ctx.fillText('AI重塑学习力,让科学用脑流行起来!',W/2,y+30);
ctx.fillStyle='rgba(255,255,255,0.8)';ctx.font='12px sans-serif';
ctx.fillText('wordma.ai8.news',W/2,y+52);ctx.textAlign='left';
savePosterWithCallback(canvas, onSuccess, onError);
};
logoImg.onerror=function(){
generatePosterWithoutLogoWithCallback(selected,d,city,daily,W,H,onSuccess,onError);
};
}
function savePosterWithCallback(canvas, onSuccess, onError){
canvas.toBlob(function(blob){
if(blob && onSuccess)onSuccess(blob);
},'image/png');
}
function generatePosterWithoutLogoWithCallback(selected,d,city,daily,W,H,onSuccess,onError){
var canvas=document.createElement('canvas');
canvas.width=W;canvas.height=H;
var ctx=canvas.getContext('2d');
var LOGO_H=0;
var HEADER_H=60;
var ITEM_H=45;
var QR_H=100;
var FOOTER_H=80;
var QUOTE_H=50;
var totalItems=selected.length;
var H=LOGO_H+HEADER_H+30+totalItems*ITEM_H+QUOTE_H+QR_H+FOOTER_H;
if(H>3600)H=3600;
canvas.height=H;
var grad=ctx.createLinearGradient(0,0,W,HEADER_H);
grad.addColorStop(0,'#FF6B2B');grad.addColorStop(1,'#FF8F5E');
ctx.fillStyle=grad;ctx.fillRect(0,0,W,HEADER_H);
ctx.fillStyle='#fff';ctx.font='bold 20px sans-serif';
ctx.fillText('单词妈妈·每日教育资讯',15,40);
ctx.font='12px sans-serif';
ctx.fillText(city+' | '+d.month+d.day+' '+d.weekday,15,58);
var y=HEADER_H+15;
ctx.fillStyle='#f5f5f5';ctx.fillRect(0,y,W,H-y-QUOTE_H-QR_H-FOOTER_H);
var catGroups={};
selected.forEach(function(item){
var cat=item.category;
if(!catGroups[cat])catGroups[cat]=[];
catGroups[cat].push(item);
});
CAT_ORDER.forEach(function(cat){
var catItems=catGroups[cat];
if(!catItems||catItems.length===0)return;
var catInfo=CAT[cat];
ctx.fillStyle='#fff';ctx.font='bold 13px sans-serif';
ctx.fillText(catInfo.emoji+' '+catInfo.name+' ('+catItems.length+'条)',15,y+12);
y+=28;
catItems.forEach(function(item,idx){
ctx.fillStyle=catInfo.color;
ctx.beginPath();ctx.arc(18,y+7,5,0,Math.PI*2);ctx.fill();
ctx.fillStyle='#fff';ctx.font='9px sans-serif';ctx.textAlign='center';
ctx.fillText(idx+1,18,y+10);ctx.textAlign='left';
ctx.fillStyle='#333';ctx.font='12px sans-serif';
var title=item.summary.substring(0,30)+(item.summary.length>30?'...':'');
ctx.fillText(title,30,y+11);
y+=ITEM_H;
});
y+=4;
});
var quote=daily&&daily.dailyQuote?'"'+daily.dailyQuote+'"':'"最好的教育,是和孩子一起成长"';
y+=5;
ctx.fillStyle='#666';ctx.font='italic 12px sans-serif';ctx.textAlign='center';
ctx.fillText(quote,W/2,y+12);ctx.textAlign='left';
y+=35;
try{
var qrCanvas=generateQRCode('https://wordma.ai8.news',80);
ctx.drawImage(qrCanvas,W/2-40,y);
ctx.fillStyle='#666';ctx.font='10px sans-serif';ctx.textAlign='center';
ctx.fillText('扫码查看更多教育新闻',W/2,y+90);
}catch(e){}
ctx.textAlign='left';
y=H-FOOTER_H;
var grad2=ctx.createLinearGradient(0,y,W,y+FOOTER_H);
grad2.addColorStop(0,'#FF6B2B');grad2.addColorStop(1,'#FF8F5E');
ctx.fillStyle=grad2;ctx.fillRect(0,y,W,FOOTER_H);
ctx.fillStyle='#fff';ctx.font='bold 14px sans-serif';ctx.textAlign='center';
ctx.fillText('AI重塑学习力,让科学用脑流行起来!',W/2,y+30);
ctx.fillStyle='rgba(255,255,255,0.8)';ctx.font='12px sans-serif';
ctx.fillText('wordma.ai8.news',W/2,y+52);ctx.textAlign='left';
savePosterWithCallback(canvas, onSuccess, onError);
}