cool 中的node.js 实现一个处理第三方Api 数据的处理

1.需求

    你现在是一个后端 前端跟你要一个接口 ,但是你发现 你这个接口 除了调用第三方Api的接口 有的数据还是没有 你直接返回 前端使用不了 因为都没有关于那样的数据 但是三方Api 中有其他的接口和现在的接口 经过处理 是可以实现这个前端需要的数据。遇到这样的情况 cool Node.js框架,其实是和Java 中的sping boot是相似的。都是由controller 和 service

    我认为所有的后端框架 目前都类似 只要不是写基础的代码 基本的逻辑都一样

java php cool go  在现在现代框架的基础上 框架的基本逻辑都一样 写法不一样而已

2.实现 

controller代码

现在你看到的是一个接口 我的post 是封装了一个 方法 所以调用直接就相当于发起了post请求 向三方Api 经过处理这样可以获取到数据

肯定我的代码你们不能使用 但是我是想告诉你们基本的逻辑 在三方API 没有对应的接口的情况下 我们只能暂时这样处理 

  @Get('/FilmDate', { summary: '获取这个影片在城市下的所有有排期的日期数组' })
  async FilmDateList(@Query() query) {
    const url = '/api/xxx/xxx/cinemaShowList';
    const cinemaListUrl = '/api/xxx/xxx/cinemaList';
    try {
      let cinemaInfo = {
        currentPage: 1,
        pageSize: 99,
        filmId: query.filmId,
        areaId: query.areaCode,
      };
      const allCinemaList = await this.mangoRequestService.post(
        cinemaListUrl,
        cinemaInfo
      );
      if (!allCinemaList.list.length) {
        return this.ok([]);
      }
      const aggrementList = await this.mangoRequestService.post(url, {
        filmId: query.filmId,
        cinemaId: allCinemaList.list[0].id,
      });
      let date = [];
      aggrementList?.forEach(item => {
        if (item.showDate) {
          date.push(item.showDate);
        }
      });

      return this.ok([...new Set(date)]);

      // const MovieList = await this.getList(query);
      // console.log(MovieList, 'MovieList');
    } catch (error) {
      throw new CoolCommException(error.message);
    }
  }

你可能感兴趣的:(node.js)