publishport-opencli 1.0.59 → 1.0.60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/cli-manifest.json CHANGED
@@ -49293,6 +49293,12 @@
49293
49293
  "required": false,
49294
49294
  "help": "仅保存草稿(微信公众号目前只支持草稿,此参数保留兼容性)"
49295
49295
  },
49296
+ {
49297
+ "name": "cover",
49298
+ "type": "str",
49299
+ "required": false,
49300
+ "help": "封面图:本机图片路径(相对路径先按正文文件所在目录解析)或图片 URL。先转存到微信图床,再按 2.35:1 与 1:1 居中裁切填入封面;转存或裁切失败则报错且不生成草稿。不传则草稿没有封面,草稿箱会提示「图文内容不完整」"
49301
+ },
49296
49302
  {
49297
49303
  "name": "execute",
49298
49304
  "type": "boolean",
@@ -1 +1 @@
1
- import{CliError as l,CommandExecutionError as x}from"@jackwener/opencli/errors";import{cli as k,Strategy as b}from"@jackwener/opencli/registry";import{formatImageReport as N,publishArticle as q}from"../_shared/article/publish.js";import{readFile as M,stat as T}from"node:fs/promises";import{rememberArticleFile as v}from"../_shared/article/images.js";function P(t){if(!t.execute)throw new l("INVALID_INPUT","微信公众号写操作需要 --execute 确认,避免误发。注意:本报错仅表示缺少 --execute,并未校验其余参数,不能当 dry-run 预览用")}async function A(t){const i=typeof t.text==="string"?t.text:void 0,e=typeof t.file==="string"?t.file:void 0;if(i&&e)throw new l("INVALID_INPUT","请只提供 <text> 或 --file 中的一种,不能同时使用。");let n=i??"";if(e){v(e);let r;try{r=await T(e)}catch{throw new l("INVALID_INPUT",`文件不存在:${e}`)}if(!r.isFile())throw new l("INVALID_INPUT",`路径不是可读文本文件:${e}`);let o;try{o=await M(e)}catch{throw new l("INVALID_INPUT",`文件无法读取:${e}`)}try{n=new TextDecoder("utf-8",{fatal:!0}).decode(o)}catch{throw new l("INVALID_INPUT",`文件无法解码为 UTF-8:${e}`)}}if(!n.trim())throw new l("INVALID_INPUT","正文不能为空。");return n}function U(t,i,e,n,r={}){return[{status:"success",outcome:n,message:t,target_type:i,target:e,...r}]}export const weixinProfile={home:"https://mp.weixin.qq.com",outputFormat:"html",preprocessConfig:{removeLinks:!0,keepLinkDomains:["mp.weixin.qq.com","weixin.qq.com"],compactHtml:!0},image:{skip:["mmbiz.qpic.cn","mmbiz.qlogo.cn"],uploadFn:async(t,i)=>{const e=document.documentElement.innerHTML,n=e.match(/data:\s*\{[\s\S]*?t:\s*["']([^"']+)["']/),r=e.match(/ticket:\s*["']([^"']+)["']/),o=e.match(/user_name:\s*["']([^"']+)["']/),c=e.match(/time:\s*["'](\d+)["']/);if(!n)throw Error("微信图片上传:页面中未找到 token,请确认已登录 mp.weixin.qq.com。");const a=n[1],m=r?r[1]:"",s=o?o[1]:"",p=c?Number(c[1]):Math.floor(Date.now()/1000),u=await fetch(t,{credentials:"omit"});if(!u.ok)throw Error("图片下载失败:"+t+"(HTTP "+u.status+")");const h=await u.blob(),_=Date.now(),w=_+".jpg",d=new FormData;d.append("type",h.type||"image/jpeg");d.append("id",String(_));d.append("name",w);d.append("lastModifiedDate",new Date().toString());d.append("size",String(h.size));d.append("file",h,w);const y=Date.now(),I="https://mp.weixin.qq.com/cgi-bin/filetransfer?action=upload_material&f=json&scene=8&writetype=doublewrite&groupid=1&ticket_id="+encodeURIComponent(s)+"&ticket="+encodeURIComponent(m)+"&svr_time="+p+"&token="+encodeURIComponent(a)+"&lang=zh_CN&seq="+y+"&t="+Math.random(),g=await fetch(I,{method:"POST",credentials:"include",body:d}),f=await g.json().catch(()=>({}));if(!g.ok||f.base_resp?.err_msg!=="ok"||!f.cdn_url)throw Error("微信图片上传失败:"+t+"("+(f.base_resp?.err_msg||"HTTP "+g.status)+")");return{url:f.cdn_url}}},checkAuth:async(t)=>{const e=await(await fetch("https://mp.weixin.qq.com/",{method:"GET",credentials:"include"})).text(),n=e.match(/user_name:\s*["']([^"']+)["']/),r=e.match(/nick_name:\s*["']([^"']+)["']/);if(!n&&!r)return{isAuthenticated:!1};const o=e.match(/class="weui-desktop-account__thumb"[^>]*src="([^"]+)"/),c=e.match(/head_img:\s*['"]([^'"]+)['"]/);let a=o?o[1]:c?c[1]:"";if(a.startsWith("http://"))a=a.replace("http://","https://");return{isAuthenticated:!0,userId:n?n[1]:"",username:r?r[1]:"",avatar:a}},publish:async(t,i)=>{const n=document.documentElement.innerHTML.match(/data:\s*\{[\s\S]*?t:\s*["']([^"']+)["']/);if(!n)return{ok:!1,stage:"auth",message:"页面中未找到 token,请确认已登录 mp.weixin.qq.com。"};const r=n[1],o=t.content,c=new URLSearchParams({token:r,lang:"zh_CN",f:"json",ajax:"1",random:String(Math.random()),AppMsgId:"",count:"1",data_seq:"0",operate_from:"Chrome",isnew:"0",ad_video_transition0:"",can_reward0:"0",related_video0:"",is_video_recommend0:"-1",title0:t.title,author0:"",writerid0:"0",fileid0:"",digest0:"",auto_gen_digest0:"1",content0:o,sourceurl0:"",need_open_comment0:"1",only_fans_can_comment0:"0",cdn_url0:"",cdn_235_1_url0:"",cdn_1_1_url0:"",cdn_url_back0:"",crop_list0:"",music_id0:"",video_id0:"",voteid0:"",voteismlt0:"",supervoteid0:"",cardid0:"",cardquantity0:"",cardlimit0:"",vid_type0:"",show_cover_pic0:"0",shortvideofileid0:"",copyright_type0:"0",releasefirst0:"",platform0:"",reprint_permit_type0:"",allow_reprint0:"",allow_reprint_modify0:"",original_article_type0:"",ori_white_list0:"",free_content0:"",fee0:"0",ad_id0:"",guide_words0:"",is_share_copyright0:"0",share_copyright_url0:"",source_article_type0:"",reprint_recommend_title0:"",reprint_recommend_content0:"",share_page_type0:"0",share_imageinfo0:'{"list":[]}',share_video_id0:"",dot0:"{}",share_voice_id0:"",insert_ad_mode0:"",categories_list0:"[]"}),a=await fetch("https://mp.weixin.qq.com/cgi-bin/operate_appmsg?t=ajax-response&sub=create&type=77&token="+encodeURIComponent(r)+"&lang=zh_CN",{method:"POST",credentials:"include",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:c});var m=await a.text(),s=null;try{s=JSON.parse(m)}catch(w){}if(!a.ok||!s||!s.appMsgId){var p=s&&(s.ret!=null?s.ret:s.base_resp&&s.base_resp.ret),u={"-6":"请输入验证码","-8":"请输入验证码","-1":"系统错误,请注意备份内容后重试","-2":"参数错误,请注意备份内容后重试","-99":"内容超出字数,请调整","200003":"登录态超时,请重新登录","64507":"内容不能包含外部链接","64702":"标题超出 64 字长度限制","64705":"内容超出字数,请调整"},h=p!=null&&u[String(p)]||"发布失败(错误码:"+p+"):"+m.slice(0,200);return{ok:!1,stage:"publish",status:a.status,message:h}}const _="https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit&action=edit&type=77&appmsgid="+s.appMsgId+"&token="+encodeURIComponent(r)+"&lang=zh_CN";return{ok:!0,id:String(s.appMsgId),url:_,draft:!0}}},weixinAuthProfile={home:weixinProfile.home,checkAuth:weixinProfile.checkAuth};k({site:"weixin",name:"article",access:"write",description:"通过 API 把文章发布到微信公众号草稿箱。正文默认 Markdown,外链图片自动转存到微信 CDN。",domain:"mp.weixin.qq.com",strategy:b.COOKIE,browser:!0,args:[{name:"title",positional:!0,required:!0,help:"文章标题(最长 64 字)"},{name:"text",positional:!0,help:"文章正文(默认 Markdown;加 --html 则当 HTML 处理)"},{name:"file",help:"正文文件路径(UTF-8,默认 Markdown)"},{name:"html",type:"boolean",help:"把正文当 HTML 处理而不是 Markdown"},{name:"draft",type:"boolean",help:"仅保存草稿(微信公众号目前只支持草稿,此参数保留兼容性)"},{name:"execute",type:"boolean",help:"实际执行发布。不加此参数时命令拒绝写入。"}],columns:["status","outcome","message","target_type","target","created_target","created_url"],func:async(t,i)=>{if(!t)throw new x("微信公众号 article 命令需要浏览器会话。");P(i);const e=String(i.title??"").trim();if(!e)throw new l("INVALID_INPUT","文章标题不能为空。");const n=await A(i),r=!0,o=await q(t,{title:e,body:n,format:i.html?"html":"markdown",draftOnly:r,profile:weixinProfile}),c=o.images.uploaded.length|0,a=o.images.failed.length|0;let m="已保存微信公众号草稿";if(c||a)m+="·图片:"+c+" 张已转存"+(a?","+a+" 张失败":"");m+=N(o.images);return U(m,"article","","draft",{created_target:"article:"+o.id,created_url:o.url})}});
1
+ import{CliError as y,CommandExecutionError as A}from"@jackwener/opencli/errors";import{cli as U,Strategy as C}from"@jackwener/opencli/registry";import{formatImageReport as D,publishArticle as z}from"../_shared/article/publish.js";import{readFile as E,stat as R}from"node:fs/promises";import{rememberArticleFile as j}from"../_shared/article/images.js";function F(r){if(!r.execute)throw new y("INVALID_INPUT","微信公众号写操作需要 --execute 确认,避免误发。注意:本报错仅表示缺少 --execute,并未校验其余参数,不能当 dry-run 预览用")}async function O(r){const c=typeof r.text==="string"?r.text:void 0,e=typeof r.file==="string"?r.file:void 0;if(c&&e)throw new y("INVALID_INPUT","请只提供 <text> 或 --file 中的一种,不能同时使用。");let s=c??"";if(e){j(e);let n;try{n=await R(e)}catch{throw new y("INVALID_INPUT",`文件不存在:${e}`)}if(!n.isFile())throw new y("INVALID_INPUT",`路径不是可读文本文件:${e}`);let o;try{o=await E(e)}catch{throw new y("INVALID_INPUT",`文件无法读取:${e}`)}try{s=new TextDecoder("utf-8",{fatal:!0}).decode(o)}catch{throw new y("INVALID_INPUT",`文件无法解码为 UTF-8:${e}`)}}if(!s.trim())throw new y("INVALID_INPUT","正文不能为空。");return s}function H(r,c,e,s,n={}){return[{status:"success",outcome:s,message:r,target_type:c,target:e,...n}]}export const weixinProfile={home:"https://mp.weixin.qq.com",outputFormat:"html",preprocessConfig:{removeLinks:!0,keepLinkDomains:["mp.weixin.qq.com","weixin.qq.com"],compactHtml:!0},image:{skip:["mmbiz.qpic.cn","mmbiz.qlogo.cn"],uploadFn:async(r,c)=>{const e=document.documentElement.innerHTML,s=e.match(/data:\s*\{[\s\S]*?t:\s*["']([^"']+)["']/),n=e.match(/ticket:\s*["']([^"']+)["']/),o=e.match(/user_name:\s*["']([^"']+)["']/),i=e.match(/time:\s*["'](\d+)["']/);if(!s)throw Error("微信图片上传:页面中未找到 token,请确认已登录 mp.weixin.qq.com。");const u=s[1],x=n?n[1]:"",h=o?o[1]:"",I=i?Number(i[1]):Math.floor(Date.now()/1000),l=await fetch(r,{credentials:"omit"});if(!l.ok)throw Error("图片下载失败:"+r+"(HTTP "+l.status+")");const v=await l.blob(),N=Date.now(),q=N+".jpg",_=new FormData;_.append("type",v.type||"image/jpeg");_.append("id",String(N));_.append("name",q);_.append("lastModifiedDate",new Date().toString());_.append("size",String(v.size));_.append("file",v,q);const a=Date.now(),P="https://mp.weixin.qq.com/cgi-bin/filetransfer?action=upload_material&f=json&scene=8&writetype=doublewrite&groupid=1&ticket_id="+encodeURIComponent(h)+"&ticket="+encodeURIComponent(x)+"&svr_time="+I+"&token="+encodeURIComponent(u)+"&lang=zh_CN&seq="+a+"&t="+Math.random(),f=await fetch(P,{method:"POST",credentials:"include",body:_}),m=await f.json().catch(()=>({}));if(!f.ok||m.base_resp?.err_msg!=="ok"||!m.cdn_url)throw Error("微信图片上传失败:"+r+"("+(m.base_resp?.err_msg||"HTTP "+f.status)+")");return{url:m.cdn_url}}},checkAuth:async(r)=>{const e=await(await fetch("https://mp.weixin.qq.com/",{method:"GET",credentials:"include"})).text(),s=e.match(/user_name:\s*["']([^"']+)["']/),n=e.match(/nick_name:\s*["']([^"']+)["']/);if(!s&&!n)return{isAuthenticated:!1};const o=e.match(/class="weui-desktop-account__thumb"[^>]*src="([^"]+)"/),i=e.match(/head_img:\s*['"]([^'"]+)['"]/);let u=o?o[1]:i?i[1]:"";if(u.startsWith("http://"))u=u.replace("http://","https://");return{isAuthenticated:!0,userId:s?s[1]:"",username:n?n[1]:"",avatar:u}},publish:async(r,c)=>{const s=document.documentElement.innerHTML.match(/data:\s*\{[\s\S]*?t:\s*["']([^"']+)["']/);if(!s)return{ok:!1,stage:"auth",message:"页面中未找到 token,请确认已登录 mp.weixin.qq.com。"};const n=s[1],o={url:"",u235:"",u11:"",cropList:""},i=r.params&&typeof r.params.cover==="string"?r.params.cover:"";if(i){let a;try{a=await new Promise(function(t,d){const p=new Image;p.onload=function(){t({w:p.naturalWidth,h:p.naturalHeight})};p.onerror=function(){d(Error("封面图加载失败"))};p.src=i})}catch(t){return{ok:!1,stage:"cover",message:"封面图已转存但无法读取尺寸:"+i.slice(0,160)}}if(!(a.w>0&&a.h>0))return{ok:!1,stage:"cover",message:"封面图尺寸无效("+a.w+"×"+a.h+"):"+i.slice(0,160)};const f=[["2.35_1",2.35],["1_1",1]].map(function(t){const d=t[1],p=a.w/a.h;if(p>d){const g=(a.w-a.h*d)/a.w/2;return{ratio:t[0],x1:g,y1:0,x2:1-g,y2:1}}if(p<d){const g=(a.h-a.w/d)/a.h/2;return{ratio:t[0],x1:0,y1:g,x2:1,y2:1-g}}return{ratio:t[0],x1:0,y1:0,x2:1,y2:1}}),m=new URLSearchParams({imgurl:i,size_count:String(f.length)});f.forEach(function(t,d){m.set("size"+d+"_x1",String(t.x1));m.set("size"+d+"_y1",String(t.y1));m.set("size"+d+"_x2",String(t.x2));m.set("size"+d+"_y2",String(t.y2))});m.set("token",n);m.set("lang","zh_CN");m.set("f","json");m.set("ajax","1");const M=await fetch("https://mp.weixin.qq.com/cgi-bin/cropimage?action=crop_multi",{method:"POST",credentials:"include",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:m}),S=await M.text();let w=null;try{w=JSON.parse(S)}catch(t){w=null}const k=w&&Array.isArray(w.result)?w.result:[];if(!M.ok||!w||!w.base_resp||w.base_resp.ret!==0||k.length!==f.length||k.some(function(t){return!t||!t.cdnurl}))return{ok:!1,stage:"cover",status:M.status,message:"封面裁切失败(cropimage crop_multi),未生成草稿",detail:S.slice(0,500)};const T={crop_list:[],crop_list_percent:[]};f.forEach(function(t,d){const p=k[d],g=p.width>0?p.width:a.w,b=p.height>0?p.height:a.h,L=p.file_id||0;T.crop_list.push({ratio:t.ratio,x1:Math.floor(g*t.x1),y1:Math.floor(b*t.y1),x2:Math.floor(g*t.x2),y2:Math.floor(b*t.y2),file_id:L});T.crop_list_percent.push({ratio:t.ratio,x1:t.x1,y1:t.y1,x2:t.x2,y2:t.y2,file_id:L})});o.url=i;o.u235=k[0].cdnurl;o.u11=k[1].cdnurl;o.cropList=JSON.stringify(T)}const u=r.content,x=new URLSearchParams({token:n,lang:"zh_CN",f:"json",ajax:"1",random:String(Math.random()),AppMsgId:"",count:"1",data_seq:"0",operate_from:"Chrome",isnew:"0",ad_video_transition0:"",can_reward0:"0",related_video0:"",is_video_recommend0:"-1",title0:r.title,author0:"",writerid0:"0",fileid0:"",digest0:"",auto_gen_digest0:"1",content0:u,sourceurl0:"",need_open_comment0:"1",only_fans_can_comment0:"0",cdn_url0:o.url,cdn_235_1_url0:o.u235,cdn_16_9_url0:o.url,cdn_1_1_url0:o.u11,cdn_url_back0:o.url,crop_list0:o.cropList,music_id0:"",video_id0:"",voteid0:"",voteismlt0:"",supervoteid0:"",cardid0:"",cardquantity0:"",cardlimit0:"",vid_type0:"",show_cover_pic0:"0",shortvideofileid0:"",copyright_type0:"0",releasefirst0:"",platform0:"",reprint_permit_type0:"",allow_reprint0:"",allow_reprint_modify0:"",original_article_type0:"",ori_white_list0:"",free_content0:"",fee0:"0",ad_id0:"",guide_words0:"",is_share_copyright0:"0",share_copyright_url0:"",source_article_type0:"",reprint_recommend_title0:"",reprint_recommend_content0:"",share_page_type0:"0",share_imageinfo0:'{"list":[]}',share_video_id0:"",dot0:"{}",share_voice_id0:"",insert_ad_mode0:"",categories_list0:"[]"}),h=await fetch("https://mp.weixin.qq.com/cgi-bin/operate_appmsg?t=ajax-response&sub=create&type=77&token="+encodeURIComponent(n)+"&lang=zh_CN",{method:"POST",credentials:"include",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:x});var I=await h.text(),l=null;try{l=JSON.parse(I)}catch(a){}if(!h.ok||!l||!l.appMsgId){var v=l&&(l.ret!=null?l.ret:l.base_resp&&l.base_resp.ret),N={"-6":"请输入验证码","-8":"请输入验证码","-1":"系统错误,请注意备份内容后重试","-2":"参数错误,请注意备份内容后重试","-99":"内容超出字数,请调整","200003":"登录态超时,请重新登录","64507":"内容不能包含外部链接","64702":"标题超出 64 字长度限制","64705":"内容超出字数,请调整"},q=v!=null&&N[String(v)]||"发布失败(错误码:"+v+"):"+I.slice(0,200);return{ok:!1,stage:"publish",status:h.status,message:q}}const _="https://mp.weixin.qq.com/cgi-bin/appmsg?t=media/appmsg_edit&action=edit&type=77&appmsgid="+l.appMsgId+"&token="+encodeURIComponent(n)+"&lang=zh_CN";return{ok:!0,id:String(l.appMsgId),url:_,draft:!0}}},weixinAuthProfile={home:weixinProfile.home,checkAuth:weixinProfile.checkAuth};U({site:"weixin",name:"article",access:"write",description:"通过 API 把文章发布到微信公众号草稿箱。正文默认 Markdown,外链图片自动转存到微信 CDN。",domain:"mp.weixin.qq.com",strategy:C.COOKIE,browser:!0,args:[{name:"title",positional:!0,required:!0,help:"文章标题(最长 64 字)"},{name:"text",positional:!0,help:"文章正文(默认 Markdown;加 --html 则当 HTML 处理)"},{name:"file",help:"正文文件路径(UTF-8,默认 Markdown)"},{name:"html",type:"boolean",help:"把正文当 HTML 处理而不是 Markdown"},{name:"draft",type:"boolean",help:"仅保存草稿(微信公众号目前只支持草稿,此参数保留兼容性)"},{name:"cover",help:"封面图:本机图片路径(相对路径先按正文文件所在目录解析)或图片 URL。先转存到微信图床,再按 2.35:1 与 1:1 居中裁切填入封面;转存或裁切失败则报错且不生成草稿。不传则草稿没有封面,草稿箱会提示「图文内容不完整」"},{name:"execute",type:"boolean",help:"实际执行发布。不加此参数时命令拒绝写入。"}],columns:["status","outcome","message","target_type","target","created_target","created_url"],func:async(r,c)=>{if(!r)throw new A("微信公众号 article 命令需要浏览器会话。");F(c);const e=String(c.title??"").trim();if(!e)throw new y("INVALID_INPUT","文章标题不能为空。");const s=await O(c),n=typeof c.cover==="string"?c.cover.trim():"",o=!0,i=await z(r,{title:e,body:s,format:c.html?"html":"markdown",draftOnly:o,profile:weixinProfile,publishParams:n?{cover:n}:null}),u=i.images.uploaded.length|0,x=i.images.failed.length|0;let h=n?"已保存微信公众号草稿(含封面)":"已保存微信公众号草稿(未设封面,草稿箱会提示内容不完整,可加 --cover 重新生成)";if(u||x)h+="·图片:"+u+" 张已转存"+(x?","+x+" 张失败":"");h+=D(i.images);return H(h,"article","","draft",{created_target:"article:"+i.id,created_url:i.url})}});
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "publishport-opencli",
3
- "version": "1.0.59",
3
+ "version": "1.0.60",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": false