import { newPage, closePage, wait, installHook, readHook } from '/home/admin9webs/shein建品/sh_lib.mjs'; import { selectCategory, fillRequiredSelects } from '/home/admin9webs/shein建品/sh_build.mjs'; import { SAN as S } from '/tmp/cdp3.mjs'; import { readFileSync } from 'fs'; const P = `document.querySelector('#app').__vue_app__.config.globalProperties.$pinia`; const pid = process.argv[2] || '676236596821'; const it = JSON.parse(readFileSync('/home/admin9webs/tiktok建品/tk_input.json','utf8'))[pid]; const IMGS = (it.imgs||[]).slice(0,6); const c = await newPage('https://www.dianxiaomi.com/web/smtChoice/add'); await wait(12000); await installHook(c); // 店 const sp = JSON.parse(String(await c.evalJS(`(()=>{const f=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null).find(e=>/店铺名称/.test((e.querySelector('label')||{}).innerText||'')); const s=f.querySelector('.ant-select-selector'); s.scrollIntoView({block:'center'}); const r=s.getBoundingClientRect(); return JSON.stringify([Math.round(r.x+r.width/2),Math.round(r.y+r.height/2)]);})()`))); await c.send('Input.dispatchMouseEvent',{type:'mouseMoved',x:sp[0],y:sp[1],buttons:0}); await wait(300); for(const t of ['mousePressed','mouseReleased']) await c.send('Input.dispatchMouseEvent',{type:t,x:sp[0],y:sp[1],button:'left',clickCount:1,buttons:1}); await wait(2000); await c.evalJS(`(()=>{const dd=[...document.querySelectorAll('.ant-select-dropdown')].filter(e=>e.offsetParent!==null).pop(); const op=[...dd.querySelectorAll('.ant-select-item-option')][0]; const r=op.getBoundingClientRect(); for(const ev of ['mousedown','mouseup','click']) op.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window,clientX:r.x+r.width/2,clientY:r.y+r.height/2})); return 1;})()`); await wait(6000); // 备货类型 const bp = JSON.parse(String(await c.evalJS(`(()=>{const f=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null).find(e=>/备货类型/.test((e.querySelector('label')||{}).innerText||'')); const s=f.querySelector('.ant-select-selector'); s.scrollIntoView({block:'center'}); const r=s.getBoundingClientRect(); return JSON.stringify([Math.round(r.x+r.width/2),Math.round(r.y+r.height/2)]);})()`))); await c.send('Input.dispatchMouseEvent',{type:'mouseMoved',x:bp[0],y:bp[1],buttons:0}); await wait(300); for(const t of ['mousePressed','mouseReleased']) await c.send('Input.dispatchMouseEvent',{type:t,x:bp[0],y:bp[1],button:'left',clickCount:1,buttons:1}); await wait(2000); await c.evalJS(`(()=>{const dd=[...document.querySelectorAll('.ant-select-dropdown')].filter(e=>e.offsetParent!==null).pop(); const op=[...dd.querySelectorAll('.ant-select-item-option')][0]; const r=op.getBoundingClientRect(); for(const ev of ['mousedown','mouseup','click']) op.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window,clientX:r.x+r.width/2,clientY:r.y+r.height/2})); return 1;})()`); await wait(3000); console.log('类目:', S(String(await selectCategory(c, it.cat_kw || '发夹', it.cat_kw))).slice(0,120)); await wait(6000); // 结构 dump const shape = await c.evalJS(`(()=>{const p=${P}; const sd=p._s.get('smtChoiceSkuDataStore').$state.formState; const sku=JSON.stringify(sd.skuDataList&&sd.skuDataList[0]||{}).slice(0,700); const desc=p._s.get('smtChoiceAddStore').$state.describeInfoData; const sa=p._s.get('smtChoiceSkuAttrStore').$state.formState.skuAttrs||[]; return JSON.stringify({skuRow:sku, descKeys:Object.keys(desc||{}).slice(0,10), desc:JSON.stringify(desc||{}).slice(0,300), attrs:sa.map(a=>a.attributeId+':'+a.attributeName+' opts='+((a.options||a._options||[]).length))});})()`); console.log('结构:', S(String(shape)).slice(0,1200)); // PC端描述 DOM const dom = await c.evalJS(`(()=>{const f=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null).find(e=>/PC端描述/.test((e.querySelector('label')||{}).innerText||'')); if(!f) return 'no-item'; return JSON.stringify({tags:[...f.querySelectorAll('input,textarea,[contenteditable],.ql-editor,.w-e-text')].map(x=>x.tagName+(x.className?'.'+String(x.className).slice(0,30):'')).slice(0,6), txt:(f.innerText||'').replace(/\\s+/g,' ').trim().slice(0,60)});})()`); console.log('PC端描述:', S(String(dom)).slice(0,400)); // 填 store const fill = await c.evalJS(`(()=>{const p=${P}; const imgs=${JSON.stringify(IMGS)}; const mk=(u,i)=>({url:u,imgUrl:u,name:'img'+(i+1)+'.jpg',width:800,height:800}); const b=p._s.get('smtChoiceAddBasicStore').$state.formState; b.subject=${JSON.stringify(it.title)}; const pr=p._s.get('smtChoiceAddProductStore').$state.formState; pr.mainImage=imgs.map(mk); const sd=p._s.get('smtChoiceSkuDataStore').$state.formState; (sd.skuDataList||[]).forEach((r,i)=>{ for(const k of ['skuPrice','price','skuStock','stock','inventory','skuSupplierCode','supplierCode','skuCode','code']) { if(k in r) { if(/price/i.test(k)) r[k]=String(${JSON.stringify(String(it.price_cny||'19.90'))}); else if(/stock|inventory/i.test(k)) r[k]='999'; else r[k]='AC-'+${JSON.stringify(pid)}+'-'+(i+1); } } }); const desc=p._s.get('smtChoiceAddStore').$state.describeInfoData; if(desc) desc.detail=(desc.detail||'')+${JSON.stringify((it.bullets||[]).join('
'))}; (sd.skuDataList||[]).forEach(r=>{ r.skuPrice=String(${JSON.stringify(String(it.price_cny||'19.90'))}); r.skuCode='AC-'+${JSON.stringify(pid)}; r.packageWeight='100'; r.packageLength='20'; r.packageWidth='15'; r.packageHeight='3'; }); return JSON.stringify({subject:b.subject.slice(0,20), imgs:pr.mainImage.length, rows:(sd.skuDataList||[]).length, desc:(desc&&desc.detail||'').length});})()`); console.log('填充:', S(String(fill)).slice(0,300)); await wait(3500); console.log('必填属性:', S(String(await fillRequiredSelects(c))).slice(0,300)); await wait(2000); // 只 dump PC端描述 控件属性 const d = await c.evalJS(`(()=>{ const f=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null).find(e=>/PC端描述/.test((e.querySelector('label')||{}).innerText||'')); if(!f) return 'no-item'; const ta=f.querySelector('textarea'); const info=ta?{ro:ta.readOnly,dis:ta.disabled,cls:String(ta.className).slice(0,50),ph:ta.placeholder||'',rect:JSON.stringify(ta.getBoundingClientRect()),style:(ta.getAttribute('style')||'').slice(0,80)}:'no-ta'; const btns=[...f.querySelectorAll('button,a,span')].filter(x=>(x.innerText||'').trim()).map(x=>(x.innerText||'').trim().slice(0,14)).slice(0,8); return JSON.stringify({info, btns, html:f.innerHTML.replace(/\\s+/g,' ').slice(0,600)});})()`); console.log('PC端描述详情:', S(String(d)).slice(0,1200)); try{ await c.shot('/tmp/alc_desc.jpg'); console.log('shot ok'); }catch(e){} await closePage(c); process.exit(0);