Files

98 lines
8.0 KiB
JavaScript

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('<br/>'))};
(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);
// 先触发一次保存校验,让「请选择物流属性」的报错块出现
await c.evalJS(`window.scrollTo(0,0)`); await wait(600);
await c.evalJS(`(()=>{const xs=[...document.querySelectorAll('button')].filter(e=>e.offsetParent!==null);
const b=xs.find(e=>/^\\s*保存\\s*$/.test((e.innerText||'').trim()))||xs.find(e=>/保存/.test(e.innerText||''));
if(b) for(const ev of ['mousedown','mouseup','click']) b.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window})); return 1;})()`);
await wait(12000);
// 找报错块里的编辑入口
const info = await c.evalJS(`(()=>{
const bad=[...document.querySelectorAll('.ant-form-item-has-error')];
const out=bad.map(b=>({txt:(b.innerText||'').replace(/\\s+/g,' ').trim().slice(0,40), icons:[...b.querySelectorAll('i,a,.link')].map(x=>String(x.className).slice(0,40))}));
const link=document.querySelector('.ant-form-item-has-error .icon_edit2')||document.querySelector('.icon_edit2.link');
let pos='[]';
if(link){ link.scrollIntoView({block:'center'}); const r=link.getBoundingClientRect(); pos=JSON.stringify([Math.round(r.x+r.width/2),Math.round(r.y+r.height/2)]); }
return JSON.stringify({out, pos});})()`);
console.log('报错块:', S(String(info)).slice(0,500));
const pos = JSON.parse(String(JSON.parse(String(info)).pos || '[]'));
if(pos.length){
await c.send('Input.dispatchMouseEvent',{type:'mouseMoved',x:pos[0],y:pos[1],buttons:0}); await wait(300);
for(const t of ['mousePressed','mouseReleased']) await c.send('Input.dispatchMouseEvent',{type:t,x:pos[0],y:pos[1],button:'left',clickCount:1,buttons:1});
await wait(4000);
}
const dlg = await c.evalJS(`(()=>{
const ds=[...document.querySelectorAll('.ant-modal,.ant-drawer')].filter(e=>e.offsetParent!==null)
.map(e=>({cls:String(e.className).slice(0,40), txt:(e.innerText||'').replace(/\\s+/g,' ').trim().slice(0,320),
checks:e.querySelectorAll('.ant-checkbox-wrapper,.ant-radio-wrapper').length, selects:e.querySelectorAll('.ant-select').length,
btns:[...e.querySelectorAll('button')].map(b=>(b.innerText||'').trim().slice(0,12)).filter(Boolean).slice(0,6)}));
return JSON.stringify({n:ds.length, ds:ds.slice(0,2)});})()`);
console.log('弹层:', S(String(dlg)).slice(0,1200));
try{ await c.shot('/tmp/alc_logi.jpg'); console.log('shot ok'); }catch(e){}
await closePage(c); process.exit(0);