@@ -2,9 +2,26 @@ import {
22 ChannelPlugin , ChannelPluginContext , IChannelService , ConfigFieldType ,
33 type MessageContent ,
44} from 'channel.base' ;
5- import { XiaoaiService } from './XiaoaiService' ;
5+ import { XiaoaiService , type XiaoaiAuthMode } from './XiaoaiService' ;
66import type { XiaoaiMessageArgs } from './XiaoaiService' ;
77
8+ function readString ( value : unknown ) : string {
9+ return typeof value === 'string' ? value . trim ( ) : '' ;
10+ }
11+
12+ function resolveAuthMode ( config : Record < string , any > ) : XiaoaiAuthMode {
13+ if ( config . mode === 'password' || config . mode === 'passToken' ) return config . mode ;
14+ if ( readString ( config . passToken ) ) return 'passToken' ;
15+ if ( readString ( config . password ) ) return 'password' ;
16+ return 'passToken' ;
17+ }
18+
19+ function resolveCredential ( config : Record < string , any > , mode : XiaoaiAuthMode ) : string {
20+ const credential = readString ( config . credential ) ;
21+ if ( credential ) return credential ;
22+ return mode === 'passToken' ? readString ( config . passToken ) : readString ( config . password ) ;
23+ }
24+
825export const xiaoaiPlugin : ChannelPlugin = {
926 type : 'xiaoai' ,
1027 label : '小爱同学' ,
@@ -16,26 +33,33 @@ export const xiaoaiPlugin: ChannelPlugin = {
1633 required : true ,
1734 description : '小米账号 ID' ,
1835 } ,
19- password : {
20- label : '密码' ,
21- type : ConfigFieldType . Password ,
22- description : '小米账号密码(推荐用辅助工具登录,免填密码)' ,
36+ mode : {
37+ label : '登录方式' ,
38+ type : ConfigFieldType . Select ,
39+ required : true ,
40+ default : 'passToken' ,
41+ description : '推荐使用辅助工具导出的 passToken' ,
42+ options : [
43+ { label : 'passToken(推荐)' , value : 'passToken' } ,
44+ { label : '密码' , value : 'password' } ,
45+ ] ,
2346 } ,
24- passToken : {
25- label : 'passToken' ,
47+ credential : {
48+ label : '密码 / passToken' ,
2649 type : ConfigFieldType . Password ,
27- description : '小米 passToken,由辅助工具登录后导出' ,
50+ required : true ,
51+ description : '按登录方式填写小米账号密码或辅助工具导出的 passToken' ,
2852 } ,
29- deviceId : {
30- label : 'deviceId ' ,
53+ loginDeviceId : {
54+ label : '登录设备 ID ' ,
3155 type : ConfigFieldType . String ,
32- description : 'PassportSDK 设备 ID ,由辅助工具一并导出' ,
56+ description : 'PassportSDK deviceId ,由辅助工具一并导出;留空则随机生成 ' ,
3357 } ,
34- device : {
35- label : '设备名称 ' ,
58+ deviceName : {
59+ label : '音箱名称 ' ,
3660 type : ConfigFieldType . String ,
3761 required : true ,
38- description : '音箱设备名称(需与米家中一致) ' ,
62+ description : '米家中的小爱音箱名称 ' ,
3963 } ,
4064 heartbeat : {
4165 label : '轮询间隔 (ms)' ,
@@ -59,19 +83,19 @@ export const xiaoaiPlugin: ChannelPlugin = {
5983 async init ( ctx : ChannelPluginContext ) : Promise < IChannelService | undefined > {
6084 const { config, logger, filterEvent, initSession, onReceiveMessage } = ctx ;
6185
62- const userId = config . userId ?. trim ( ) ;
63- const password = config . password ?. trim ( ) ;
64- const passToken = config . passToken ?. trim ( ) ;
65- const deviceId = config . deviceId ?. trim ( ) ;
66- const device = config . device ?. trim ( ) ;
67- if ( ! userId || ! device || ( ! password && ! passToken ) ) return undefined ;
86+ const userId = readString ( config . userId ) ;
87+ const mode = resolveAuthMode ( config ) ;
88+ const credential = resolveCredential ( config , mode ) ;
89+ const loginDeviceId = readString ( config . loginDeviceId ) || readString ( config . deviceId ) ;
90+ const deviceName = readString ( config . deviceName ) || readString ( config . device ) ;
91+ if ( ! userId || ! deviceName || ! credential ) return undefined ;
6892
6993 const service = new XiaoaiService ( {
7094 userId,
71- password ,
72- passToken ,
73- deviceId ,
74- device ,
95+ mode ,
96+ credential ,
97+ loginDeviceId ,
98+ deviceName ,
7599 heartbeat : Number ( config . heartbeat ) || 1000 ,
76100 textChunkLimit : Number ( config . textChunkLimit ) || 200 ,
77101 volume : config . volume ? Number ( config . volume ) : undefined ,
0 commit comments