第一步 直接新建 fund.ts 编写

import axios from 'axios'
import type { FUNCAPP, FUNCCTX } from '../context'
const ctx: FUNCCTX = _CTX
const app: FUNCAPP = _APP

// 基金代码
const code = '012414'
//企业微信 api
const qyApi = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx'

// http 访问时输出
export const main = async () => {
    const { name, price, ratio } = await getFundData(code)
    return { data: { fund: { name, price, ratio } } }
}

// cron 定时任务执行方法
export const cron = async () => {
    console.log('cron start')
    const { name, price, ratio } = await getFundData(code)
    if (ratio < -0.2 || ratio > 2) {
        await axios.post(qyApi, {
            "msgtype": "text",
            "text": {
                "content": `请注意${name}基金变动`
            }
        })
    }
}

const getFundData = async (code: string) => {
    const str: string = await axios.get(`http://hqf.stock.sohu.com/kfund/${code}_6.up`)
        .then(res => res.data)
    //剔除 fortune_hq( );
    const jsonStr = str.substring(11).slice(0, -3).replace(/\'/g, '"')
    const data = JSON.parse(jsonStr)
    const name = data.price[1]
    const price = parseFloat(data.price[2])
    const ratio = parseFloat(data.price[3])
    const result = { name, price, ratio }
    // 可以 db 保存起来
    // await ctx.redis.setex(dayjs().format('YYYY-MM-DD') + '-' + code, 7200, JSON.stringify(result))
    // await ctx.knex('fund').insert(result) //先得创建表
    return result
}

第二步:修改配置文件的 token 和 cron

exports.config = {
    default: {
        project: "test",
        token: "123456",
        url: "http://127.0.0.1:3000",
        secret: {},
        crons: [
            { label: 'fund', name: 'fund', cron: '0 40 14 * * *' }
        ]
    }
}

第三步:部署

yajs -n fund
yajs --cron

完成搞定,撒花
相关代码已上传 github

https://github.com/mtnbgx/yajs-example

https://github.com/mtnbgx/yajs

Yajs 是一个自运行的云函数开源框架,运行内存小于 100m ,适合各种 serverless 、云开发退坑后替代