2023-09-21 17:33:59 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2023-11-30 11:37:02 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-10-11 09:21:07 +08:00
|
|
|
|
"go-wechat/config"
|
2023-10-26 10:07:08 +08:00
|
|
|
|
"go-wechat/initialization"
|
2024-02-19 14:17:26 +08:00
|
|
|
|
"go-wechat/mq"
|
2023-11-30 11:37:02 +08:00
|
|
|
|
"go-wechat/router"
|
2023-09-21 17:33:59 +08:00
|
|
|
|
"go-wechat/tasks"
|
2023-11-30 11:37:02 +08:00
|
|
|
|
"go-wechat/tcpserver"
|
2023-10-26 10:07:08 +08:00
|
|
|
|
"go-wechat/utils"
|
2023-11-30 17:31:50 +08:00
|
|
|
|
"html/template"
|
2023-09-21 17:33:59 +08:00
|
|
|
|
"log"
|
2023-11-30 17:31:50 +08:00
|
|
|
|
"net/http"
|
2024-08-21 10:11:29 +08:00
|
|
|
|
"os"
|
|
|
|
|
"strconv"
|
2023-11-30 17:31:50 +08:00
|
|
|
|
"strings"
|
2023-10-26 10:07:08 +08:00
|
|
|
|
"time"
|
2023-09-21 17:33:59 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
2023-12-07 15:58:55 +08:00
|
|
|
|
initialization.InitConfig() // 初始化配置
|
|
|
|
|
initialization.InitWechatRobotInfo() // 初始化机器人信息
|
2023-12-11 10:44:23 +08:00
|
|
|
|
initialization.Plugin() // 注册插件
|
2023-12-07 15:58:55 +08:00
|
|
|
|
tasks.InitTasks() // 初始化定时任务
|
2024-02-19 14:17:26 +08:00
|
|
|
|
mq.Init() // 初始化MQ
|
2024-08-21 10:11:29 +08:00
|
|
|
|
|
|
|
|
|
if flag, _ := strconv.ParseBool(os.Getenv("DONT_SEND")); flag {
|
|
|
|
|
log.Printf("已设置环境变量DONT_SEND,不发送消息")
|
|
|
|
|
}
|
|
|
|
|
if flag, _ := strconv.ParseBool(os.Getenv("DONT_SAVE")); flag {
|
|
|
|
|
log.Printf("已设置环境变量DONT_SAVE,不保存消息")
|
|
|
|
|
}
|
2023-09-21 17:33:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
2023-10-26 10:07:08 +08:00
|
|
|
|
// 如果启用了自动配置回调,就设置一下
|
|
|
|
|
if config.Conf.Wechat.AutoSetCallback {
|
|
|
|
|
utils.ClearCallback()
|
|
|
|
|
time.Sleep(500 * time.Millisecond) // 休眠五百毫秒再设置
|
|
|
|
|
utils.SetCallback(config.Conf.Wechat.Callback)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-30 11:37:02 +08:00
|
|
|
|
// 启动TCP服务
|
|
|
|
|
go tcpserver.Start()
|
2023-09-21 17:33:59 +08:00
|
|
|
|
|
2023-11-30 11:37:02 +08:00
|
|
|
|
// 启动HTTP服务
|
|
|
|
|
app := gin.Default()
|
2023-11-30 17:31:50 +08:00
|
|
|
|
|
|
|
|
|
// 自定义模板引擎函数
|
|
|
|
|
app.SetFuncMap(template.FuncMap{
|
2024-04-23 10:42:08 +08:00
|
|
|
|
"codeToChinese": func(code string) string {
|
|
|
|
|
switch code {
|
|
|
|
|
case "friend":
|
|
|
|
|
return "好友列表"
|
|
|
|
|
case "group":
|
|
|
|
|
return "群组列表"
|
|
|
|
|
case "index":
|
|
|
|
|
return "首页"
|
2024-06-18 17:02:37 +08:00
|
|
|
|
case "assistant":
|
|
|
|
|
return "AI角色"
|
2024-04-23 10:42:08 +08:00
|
|
|
|
default:
|
|
|
|
|
return "其他页面"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"boolToChinese": func(flag bool) string {
|
2023-11-30 17:31:50 +08:00
|
|
|
|
if flag {
|
2024-04-23 10:42:08 +08:00
|
|
|
|
return "是"
|
2023-11-30 17:31:50 +08:00
|
|
|
|
}
|
2024-04-23 10:42:08 +08:00
|
|
|
|
return "否"
|
2023-11-30 17:31:50 +08:00
|
|
|
|
},
|
2024-07-16 11:36:10 +08:00
|
|
|
|
"checkIsGroup": func(str string) bool {
|
|
|
|
|
// 如果id包含@chatroom就是群组
|
|
|
|
|
return strings.HasSuffix(str, "@chatroom")
|
|
|
|
|
},
|
2023-11-30 17:31:50 +08:00
|
|
|
|
})
|
|
|
|
|
|
2023-11-30 11:37:02 +08:00
|
|
|
|
app.LoadHTMLGlob("views/*.html")
|
2023-11-30 14:20:48 +08:00
|
|
|
|
app.Static("/assets", "./views/static")
|
2023-11-30 11:37:02 +08:00
|
|
|
|
app.StaticFile("/favicon.ico", "./views/wechat.ico")
|
|
|
|
|
// 404返回数据
|
|
|
|
|
app.NoRoute(func(ctx *gin.Context) {
|
2023-11-30 17:31:50 +08:00
|
|
|
|
if strings.HasPrefix(ctx.Request.URL.Path, "/api") {
|
|
|
|
|
ctx.String(404, "接口不存在")
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-11-30 11:37:02 +08:00
|
|
|
|
// 404直接跳转到首页
|
2024-04-23 10:42:08 +08:00
|
|
|
|
ctx.Redirect(302, "/404.html")
|
2023-11-30 11:37:02 +08:00
|
|
|
|
})
|
2023-11-30 17:31:50 +08:00
|
|
|
|
app.NoMethod(func(ctx *gin.Context) {
|
|
|
|
|
ctx.String(http.StatusMethodNotAllowed, "不支持的请求方式")
|
|
|
|
|
})
|
2023-11-30 11:37:02 +08:00
|
|
|
|
// 初始化路由
|
|
|
|
|
router.Init(app)
|
2024-07-11 15:47:35 +08:00
|
|
|
|
if err := app.Run(":8080"); err != nil {
|
2023-11-30 11:37:02 +08:00
|
|
|
|
log.Panicf("服务启动失败:%v", err)
|
2023-09-21 17:33:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|