|
|
package main
|
|
|
|
|
|
import (
|
|
|
"WechatGateWay/global"
|
|
|
"WechatGateWay/handle"
|
|
|
"WechatGateWay/third_part"
|
|
|
"fmt"
|
|
|
"github.com/robfig/cron"
|
|
|
"github.com/spf13/viper"
|
|
|
"log"
|
|
|
"math/rand"
|
|
|
"net/http"
|
|
|
"os"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
func init() {
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
// 读取配置文件
|
|
|
workDir, _ := os.Getwd()
|
|
|
viper.SetConfigName("application")
|
|
|
viper.SetConfigType("yml")
|
|
|
viper.AddConfigPath(workDir)
|
|
|
err := viper.ReadInConfig()
|
|
|
if err != nil {
|
|
|
fmt.Println("config file not found")
|
|
|
os.Exit(1)
|
|
|
}
|
|
|
global.WechatCorpId = viper.GetString("wechat.CorpId")
|
|
|
global.WechatToken = viper.GetString("wechat.AppToken")
|
|
|
global.WechatEncodingAesKey = viper.GetString("wechat.EncodingAesKey")
|
|
|
global.WechatSendSecret = viper.GetString("wechat.SendSecret")
|
|
|
global.WechatSendAid = viper.GetString("wechat.SendAid")
|
|
|
global.WechatImagePath = viper.GetString("wechat.ImagePath")
|
|
|
// receive_id 企业应用的回调,表示corpid
|
|
|
global.WxCrypt = global.NewWXBizMsgCrypt(global.WechatToken, global.WechatEncodingAesKey, global.WechatCorpId, global.XmlType)
|
|
|
third_part.GetRemoteToken()
|
|
|
//初始化数据库
|
|
|
global.DB = global.InitDB()
|
|
|
defer global.DB.Close()
|
|
|
log.Println("数据库初始化成功")
|
|
|
|
|
|
log.Println("server init success")
|
|
|
}
|
|
|
func main() {
|
|
|
c := cron.New()
|
|
|
//每30分钟执行一次
|
|
|
c.AddFunc("0 */30 * * * *", func() { go third_part.GetRemoteToken() })
|
|
|
c.Start()
|
|
|
log.Println("任务注册成功")
|
|
|
|
|
|
// 开启一个http服务器,接收来自企业微信的消息
|
|
|
http.HandleFunc("/", handle.HandleTencent)
|
|
|
port := viper.GetString("server.port")
|
|
|
if port == "" {
|
|
|
port = "8080"
|
|
|
}
|
|
|
log.Println("server start at port:", port)
|
|
|
log.Fatalln(http.ListenAndServe(":"+port, nil))
|
|
|
}
|