6

Golang如何实现节假日不打扰用户? - 壮壮熊

 1 year ago
source link: https://www.cnblogs.com/zhouqinxiong/p/17057176.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

想象下以下场景,嘿嘿...!

一个iphone用户,闹钟是可以按节假日不响的!
每日新闻机器人,节假日是可以不打扰我的!
我的业务,节假日是可以...

要实现识别节假日,大概有两种方式:

1、自己收集国家法定节假日数据,离线存储

优势:离线简单

劣势:新一年要去更新,容易忘记,麻烦

2、调用第三方接口数据

优势:不需要我们操心数据本身

劣势:有次数限制

本次介绍调用第三方接口的方式,用golang实现整个过程。

3、接口分析

分析了网上现有接口,发现juhe的api接口会比较合适,详细:

https://www.juhe.cn/docs/api/id/606

juhe1.png

分析:

juhe2.png

请求详情:

请求地址:http://apis.juhe.cn/fapig/calendar/day
请求参数:date=2023-01-16&detail=&key=c6ff98d3**\*\***be4a35b2
请求方式:GET
Header:
   Content-Type:application/x-www-form-urlencoded

返回内容:

{
  "reason":"success",
  "result":{
    "date":"2023-01-16",
    "week":"星期一",
    "statusDesc":"工作日",
    "status":null
  },
  "error_code":0
}

4、golang实现

4.1、json2go小工具

这里我们利用下json转golang struct的小工具,把接口返回的json转成golang的代码

https://www.bejson.com/transfor/json2go/

image.png

4.2、代码实现

package main

import (
  "encoding/json"
  "fmt"
)

func main() {
  // 当前日期
  todayStr := time.Unix(time.Now().Unix(), 0).Format("2006-01-02")
  key := "xxxxxxx" //获取,节假日信息查询接口 https://dashboard.juhe.cn/data/index/my

  // 判断当前是否是节假日(周末也算节假日,除非是节假日后补班情况),节假日跳过
  calendarJh := new(util.CalendarJH)
  isHolidays := calendarJh.IsHolidays(&todayStr, &key)
  if isHolidays {
    fmt.Printf("%v 是节假日,今天跳过!\n", todayStr)
    return
  }
}

// IsHolidays 是否是节假日(周末也算节假日,除非是节假日后补班情况)
func (calendarJH *CalendarJH) IsHolidays(date *string, key *string) bool {

  // 默认返回是节假日
  result := true

  // 调用juhe api 接口
  juheAPI := "https://apis.juhe.cn/fapig/calendar/day?date=" + *date + "&detail=1&key=" + *key
  body, _ := HttpGetInfo(&juheAPI)
  err := json.Unmarshal(*body, calendarJH)
  if err != nil {
    fmt.Printf("调用juhe接口出错,默认返回是节假日,错误原因:%v \n", err)
    return result
  }

  if calendarJH.Result.Status == "1" {
    result = true
    return result
  } else if calendarJH.Result.Status == "2" {
    result = false
    return result
  } else if calendarJH.Result.Status == nil {

    if calendarJH.Result.StatusDesc == "周末" {
      result = true
      return result
    } else if calendarJH.Result.StatusDesc == "工作日" {
      result = false
      return result
    }
  }

  return result
}

// CalendarJH 日历数据,来自juhe
type CalendarJH struct {
  Reason    string `json:"reason"`
  Result    Result `json:"result"`
  ErrorCode int    `json:"error_code"`
}
type Result struct {
  Date       string      `json:"date"`
  Week       string      `json:"week"`
  StatusDesc string      `json:"statusDesc"` //状态描述,节假日/工作日/周末。1.当status为1时,表示节假日;2.当status为2时,表示工作日;3.当status为null时,如果week为周六或者周日,表示周末,否则表示工作日
  Status     interface{} `json:"status"`     //当天状态标识,1:节假日,2:工作日,null:周末或工作日(可根据week进行判断,也可以直接根据statusDesc进行判断)
}

最后,若对您有帮助,请关注我,谢谢!

程序猿牧场公众号.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK