经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Go语言 » 查看文章
Golang如何实现节假日不打扰用户?
来源:cnblogs  作者:壮壮熊  时间:2023/1/18 8:43:47  对本文有异议

?

1、场景

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

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

2、思路

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

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

优势:离线简单

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

2、调用第三方接口数据

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

劣势:有次数限制

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

3、接口分析

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

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

juhe1.png

分析:

juhe2.png

请求详情:

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

返回内容:

  1. {
  2. "reason":"success",
  3. "result":{
  4. "date":"2023-01-16",
  5. "week":"星期一",
  6. "statusDesc":"工作日",
  7. "status":null
  8. },
  9. "error_code":0
  10. }

4、golang实现

4.1、json2go小工具

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

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

image.png

4.2、代码实现

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. )
  6. func main() {
  7. // 当前日期
  8. todayStr := time.Unix(time.Now().Unix(), 0).Format("2006-01-02")
  9. key := "xxxxxxx" //获取,节假日信息查询接口 https://dashboard.juhe.cn/data/index/my
  10. // 判断当前是否是节假日(周末也算节假日,除非是节假日后补班情况),节假日跳过
  11. calendarJh := new(util.CalendarJH)
  12. isHolidays := calendarJh.IsHolidays(&todayStr, &key)
  13. if isHolidays {
  14. fmt.Printf("%v 是节假日,今天跳过!\n", todayStr)
  15. return
  16. }
  17. }
  18. // IsHolidays 是否是节假日(周末也算节假日,除非是节假日后补班情况)
  19. func (calendarJH *CalendarJH) IsHolidays(date *string, key *string) bool {
  20. // 默认返回是节假日
  21. result := true
  22. // 调用juhe api 接口
  23. juheAPI := "https://apis.juhe.cn/fapig/calendar/day?date=" + *date + "&detail=1&key=" + *key
  24. body, _ := HttpGetInfo(&juheAPI)
  25. err := json.Unmarshal(*body, calendarJH)
  26. if err != nil {
  27. fmt.Printf("调用juhe接口出错,默认返回是节假日,错误原因:%v \n", err)
  28. return result
  29. }
  30. if calendarJH.Result.Status == "1" {
  31. result = true
  32. return result
  33. } else if calendarJH.Result.Status == "2" {
  34. result = false
  35. return result
  36. } else if calendarJH.Result.Status == nil {
  37. if calendarJH.Result.StatusDesc == "周末" {
  38. result = true
  39. return result
  40. } else if calendarJH.Result.StatusDesc == "工作日" {
  41. result = false
  42. return result
  43. }
  44. }
  45. return result
  46. }
  47. // CalendarJH 日历数据,来自juhe
  48. type CalendarJH struct {
  49. Reason string `json:"reason"`
  50. Result Result `json:"result"`
  51. ErrorCode int `json:"error_code"`
  52. }
  53. type Result struct {
  54. Date string `json:"date"`
  55. Week string `json:"week"`
  56. StatusDesc string `json:"statusDesc"` //状态描述,节假日/工作日/周末。1.当status为1时,表示节假日;2.当status为2时,表示工作日;3.当status为null时,如果week为周六或者周日,表示周末,否则表示工作日
  57. Status interface{} `json:"status"` //当天状态标识,1:节假日,2:工作日,null:周末或工作日(可根据week进行判断,也可以直接根据statusDesc进行判断)
  58. }

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

程序猿牧场公众号.png

原文链接:https://www.cnblogs.com/zhouqinxiong/p/17057176.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号