{"size": 890, "ext": "js", "max_stars_repo_path": "app/service/baidu.js", "max_stars_repo_name": "hezhongfeng/im-server", "max_stars_repo_stars_event_min_datetime": "2020-07-14T09:12:13.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-10T07:59:26.000Z", "max_issues_repo_path": "app/service/baidu.js", "max_issues_repo_name": "hezhongfeng/im-server", "max_issues_repo_issues_event_min_datetime": "2021-03-02T00:43:59.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-08T23:02:24.000Z", "max_forks_repo_path": "app/service/baidu.js", "max_forks_repo_name": "hezhongfeng/im-server", "max_forks_repo_forks_event_min_datetime": "2020-07-22T02:26:34.000Z", "max_forks_repo_forks_event_max_datetime": "2022-02-10T06:37:17.000Z"}
{"max_stars_count": 48, "max_issues_count": 6, "max_forks_count": 24, "avg_line_length": 27.8125, "max_line_length": 128, "alphanum_fraction": 0.6404494382}
const Service = require('egg').Service;

class BaiduService extends Service {
  async getToken() {
    const params = {
      grant_type: 'client_credentials',
      client_id: this.app.config.baidu.client_id,
      client_secret: this.app.config.baidu.client_secret
    };
    this.app.http.get('https://aip.baidubce.com/oauth/2.0/token', params).then(data => {
      this.app.baiduToken = data.data.access_token;
    });
  }

  async chat(msg) {
    const params = {
      session_id: '666666',
      version: '2.0',
      service_id: this.app.config.baidu.service_id,
      skill_ids: this.app.config.baidu.skill_ids,
      log_id: 'testtest',
      request: {
        query: msg,
        user_id: 'test'
      }
    };
    return this.app.http.post('https://aip.baidubce.com/rpc/2.0/unit/service/chat?access_token=' + this.app.baiduToken, params);
  }
}

module.exports = BaiduService;
