Files

89 lines
7.6 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(14000);
const detail = await c.evalJS(`(()=>{const bad=[...document.querySelectorAll('.ant-form-item-has-error')].map(x=>({
label:(x.querySelector('label')||{}).innerText||'(无)',
cls:String(x.className).slice(0,50),
html:x.outerHTML.replace(/\s+/g,' ').slice(0,420)}));
const sa=(()=>{const p=${P};return (p._s.get('smtChoiceSkuAttrStore').$state.formState.skuAttrs||[]).map(a=>({id:a.attributeId,name:a.attributeName,opts:(a.options||[]).length,sel:(a.options||[]).filter(o=>o.selected||o.checked).length}));})();
const sd=(()=>{const p=${P};const r=(p._s.get('smtChoiceSkuDataStore').$state.formState.skuDataList||[])[0]||{};return {keys:Object.keys(r),skuPrice:r.skuPrice,skuCode:r.skuCode,w:r.packageWeight};})();
return JSON.stringify({bad, sa, sd});})()`);
console.log('错误详情:', S(String(detail)).slice(0,1100));
const res = await c.evalJS(`JSON.stringify({
mods:[...document.querySelectorAll('.ant-modal')].filter(e=>e.offsetParent!==null).map(e=>(e.innerText||'').replace(/\\s+/g,' ').trim().slice(0,120)),
errItems:[...document.querySelectorAll('.ant-form-item-has-error')].map(x=>{const l=x.querySelector('label'); return l?(l.innerText||'').replace(/\\s+/g,' ').trim().slice(0,26):'?';}).slice(0,8)})`);
console.log('结果:', S(String(res)).slice(0,600));
console.log('hook:', String(await readHook(c)).slice(-320));
try{ await c.shot('/tmp/alc_try1.jpg'); console.log('shot ok'); }catch(e){}
await closePage(c); process.exit(0);