@@ -123,68 +123,20 @@ async function sendMsg2RecvdApi(msg) {
123123
124124 switch ( msg . type ( ) ) {
125125 case MSG_TYPE_ENUM . ATTACHMENT : {
126+ // hack, 如果附件类型消息
126127 try {
127- /**@type {import('file-box').FileBox } */
128- //@ts -expect-errors 这里msg一定是wechaty的msg
129- const steamFile = msg . toFileBox ? await msg . toFileBox ( ) : msg . content ( )
130- formData . append ( 'type' , 'file' )
131- let fileInfo = {
132- // @ts -ignore
133- ext : steamFile . _name . split ( '.' ) . pop ( ) ?? '' ,
134- // @ts -ignore
135- mime : steamFile . _mediaType ?? 'Unknown' ,
136- // @ts -ignore
137- filename : steamFile . _name ?? 'UnknownFile'
138- }
139-
140- formData . append (
141- 'content' ,
142- //@ts -expect-errors 需要用到私有属性
143- steamFile . buffer /** 发送一个文件 */ ??
144- //@ts -expect-errors 需要用到私有属性
145- steamFile . stream /** 同一个文件转发 */ ,
146- {
147- filename : fileInfo . filename ,
148- contentType : fileInfo . mime
149- }
150- )
128+ dealWithFileMsg ( formData , msg )
151129 } catch ( e ) {
152- /** 如果 const steamFile = msg.toFileBox ? await msg.toFileBox() : msg.content() 这里执行抛出错误 */
153- /** 则很有可能是合并消息转发 */
154- formData . append ( 'type' , 'combineforward' )
155- formData . append ( 'content' , msg . text ( ) )
130+ /** 已知场景转发会进入这个逻辑,但好像还是没法显示转发内容,当unknown处理 */
131+ formData . append ( 'type' , 'unknown' )
132+ formData . append ( 'content' , msg . text ( ) ) /** 当前不支持该消息展示 */
156133 }
157134 break
158135 }
159136 case MSG_TYPE_ENUM . VOICE :
160137 case MSG_TYPE_ENUM . PIC :
161138 case MSG_TYPE_ENUM . VIDEO : {
162- // 视频
163- formData . append ( 'type' , 'file' )
164- /**@type {import('file-box').FileBox } */
165- //@ts -expect-errors 这里msg一定是wechaty的msg
166- const steamFile = msg . toFileBox ? await msg . toFileBox ( ) : msg . content ( )
167-
168- let fileInfo = {
169- // @ts -ignore
170- ext : steamFile . _name . split ( '.' ) . pop ( ) ?? '' ,
171- // @ts -ignore
172- mime : steamFile . _mediaType ?? 'Unknown' ,
173- // @ts -ignore
174- filename : steamFile . _name ?? 'UnknownFile'
175- }
176-
177- formData . append (
178- 'content' ,
179- //@ts -expect-errors 需要用到私有属性
180- steamFile . buffer /** 发送一个文件 */ ??
181- //@ts -expect-errors 需要用到私有属性
182- steamFile . stream /** 同一个文件转发 */ ,
183- {
184- filename : fileInfo . filename ,
185- contentType : fileInfo . mime
186- }
187- )
139+ dealWithFileMsg ( formData , msg )
188140 break
189141 }
190142 // 分享的Url
@@ -253,6 +205,40 @@ async function sendMsg2RecvdApi(msg) {
253205 return response
254206}
255207
208+ /**
209+ * 抽离附件上传公共逻辑
210+ * @param {import('form-data') } formData
211+ * @param {extendedMsg } msg
212+ */
213+ async function dealWithFileMsg ( formData , msg ) {
214+ // 视频
215+ formData . append ( 'type' , 'file' )
216+ /**@type {import('file-box').FileBox } */
217+ //@ts -expect-errors 这里msg一定是wechaty的msg
218+ const steamFile = msg . toFileBox ? await msg . toFileBox ( ) : msg . content ( )
219+
220+ let fileInfo = {
221+ // @ts -ignore
222+ ext : steamFile . _name . split ( '.' ) . pop ( ) ?? '' ,
223+ // @ts -ignore
224+ mime : steamFile . _mediaType ?? 'Unknown' ,
225+ // @ts -ignore
226+ filename : steamFile . _name ?? 'UnknownFile'
227+ }
228+
229+ formData . append (
230+ 'content' ,
231+ //@ts -expect-errors 需要用到私有属性
232+ steamFile . buffer /** 发送一个文件 */ ??
233+ //@ts -expect-errors 需要用到私有属性
234+ steamFile . stream /** 同一个文件转发 */ ,
235+ {
236+ filename : fileInfo . filename ,
237+ contentType : fileInfo . mime
238+ }
239+ )
240+ }
241+
256242module . exports = {
257243 sendMsg2RecvdApi
258244}
0 commit comments