如何在应用内调用现有应用

应用内调用其他应用

有时候我们需要开发的功能,另外一个Fxd App已经开发完成了。这种时候完全没有必要重复开发,直接把它用起来就好了。

假设我们正在开发一个定时检测RSS并发布将最新的一个文章发布到微博的应用。当我们开发完成RSS监测功能后,就需要发布微博。

而发布微博这个功能 fxd-app-weibo-publish 已经做过了。所以我们可以直接使用它。具体方式如下:

import FxdWeiboPublish from 'fxd-app-weibo-publish'; // 首先 import进来

async main(args, opts, command) {
    this.setDeaultOpts(opts);
    this.setDeaultCommand(command);
    this.format = this.get('format');
    // ...
    // 这里要发布微博了
    const weibo_publish = new FxdWeiboPublish();// 创建对象
    result = await weibo_publish.publish( // 调用 publish 方法
        null, // 第一个参数留空,第二个参数参入调用参数 
        { 
            content: 微博内容,
            headless: 'false',
            user: this.get('user'),
        }, 
        'publish' // 第三个参数固定为被调用的方法名,也就是 publish
    );
}

那么如何知道 fxd-app-weibo-publish 有哪些方法和参数可以用呢?输入

npm view fxd-app-weibo-publish meta

可以看到相关设置:

{
  args: {
    'main|publish': {
      content: {
        name: 'content',
        cn_name: '微博正文',
        type: 'string',
        required: true,
        description: '微博内容'
      },
      headless: {
        name: 'headless',
        cn_name: '后台模式',
        description: '是否使用后台模式',
        type: 'boolean',
        default: true,
        example: 'true'
      },
      user: {
        name: 'user',
        description: '浏览器使用的用户目录',
        type: 'string',
        default: 'default',
        example: 'admin'
      },
      format: {
        name: 'format',
        description: '返回的数据格式',
        type: 'string',
        default: 'text',
        example: 'json',
        enum: [ 'json', 'text' ]
      },
      timeout: {
        name: 'timeout',
        description: ' Playwright 操作超时时间,单位毫秒',
        type: 'number',
        default: 60000,
        example: 30000
      },
      wait_type: {
        name: 'wait_type',
        description: '等待元素出现的方式',
        type: 'string',
        default: 'domcontentloaded',
        example: 'networkidle',
        enum: [ 'domcontentloaded', 'load', 'networkidle' ]
      },
      images: {
        name: 'images',
        cn_name: '微博配图URL',
        description: '图片地址,用逗号分隔',
        type: 'string',
        default: '',
        example: 'https://www.baidu.com/1.jpg,https://www.baidu.com/2.jpg'
      },
      self_only: {
        name: 'self_only',
        cn_name: '仅自己可见',
        description: '是否仅对自己可见',
        type: 'boolean',
        default: false,
        example: 'true'
      }
    }
  }
}