nestjs实现掘金签到

453 阅读2分钟

因上班第一件事打开掘金就是签到,偶尔漏签还得补卡,故想到了这个需求

项目分析

  • 需要axios发送请求
  • 需要获取coolie
  • 需要任务模块
  • 没了
    项目使用直接携带cookie请求,如果加上登录的话会很麻烦,有滑块验证。

新建项目

nest new juejin

安装axios

npm install axios

src目录下新建config文件夹

//config/api.ts
// 查询是否签到
export const isSignedIn = 'https://api.juejin.cn/growth_api/v1/get_today_status'
// 签到接口
export const signIn = 'https://api.juejin.cn/growth_api/v1/check_in'
// 免费抽奖次数查询
export const freeCheck = 'https://api.juejin.cn/growth_api/v1/lottery_config/get'
// 抽奖接口
export const drawAPI = 'https://api.juejin.cn/growth_api/v1/lottery/draw'
// 获得矿石数量
export const getPointCount = 'https://api.juejin.cn/growth_api/v1/get_cur_point'
// 获得掘金活动
export const getActivityAPI = 'https://api.juejin.cn/study_api/v1/competition/competition_list'
//config/header.ts
export const HEADERS:HEADERS = {
    'Referer': 'https://juejin.cn/',
    'Upgrade-Insecure-Requests': 1,
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
    'cookie':'你自己的cookie'
}

关于这里的coolie怎么获取:登录掘金官网,打开我的主页,找个有cookie的请求复制下来就行

控制器中创建 签到、抽奖方法

//app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get('point')
  getPointCount() {
    return this.appService.getPointCount();
  }
  @Get('toDraw')
  toDraw() {
    return this.appService.toDraw();
  }
  @Get('signIn')
  signIn() {
    return this.appService.signIn();
  }
}

提供者编写具体方法

//app.service.ts
import { Injectable } from '@nestjs/common';
import axios from 'axios';
import { HEADERS } from './config/header';
import { isSignedIn, signIn, getPointCount, freeCheck, drawAPI, getActivityAPI } from './config/api';
@Injectable()
export class AppService {
  //获取矿石数量
  async getPointCount() {
    const res = await axios({
        url: getPointCount,
        method: 'get',
        headers: {
            ...HEADERS
        }
    })
    return res && res.data && res.data.data
  } 
  //签到一次
  async signIn(){
    const res = await axios({
        url: signIn,
        method: 'post',
        headers: {
            ...HEADERS
        }
    })
    return res && res.data
  }
  //抽奖一次
  async toDraw() {
    const res = await axios({
        url: drawAPI,
        method: 'post',
        headers: {
            ...HEADERS
        }
    })
    return res && res.data && res.data.data && res.data.data.lottery_name
  } 
}

抽奖前 可以判断下是不是有免费次数,接口都提供了,小伙伴可以自己写一下

创建定时任务 每天凌晨1点更新

安装任务模块npm install @nestjs/schedule

module中导入ScheduleModule

//app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ScheduleModule } from '@nestjs/schedule';
@Module({
  imports: [ScheduleModule.forRoot()],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

修改app.service中 添加定时任务

...
  //每天一点30分签到 抽奖
  @Cron('0 30 1 * * *')
  async handleCron() {
    let res=await this.signIn()
    console.log('签到结果',res);
     this.toDraw()
  }
...

至此 功能已完成 可以部署到服务器上就行了仓库地址

项目还少个日志模块,小伙伴自己加下