add mysql record

master
dustoair 3 years ago
parent 97bffe8290
commit 1834fe628e

@ -5,4 +5,14 @@ wechat:
AppToken: xPh343434343434343434
EncodingAesKey: SJmT3434343434343434343434
SendSecret: Jk343434444444444444444343434
SendAid: 1000002
SendAid: 1000002
ImagePath: /app/data
db:
MySQLHOST: mysql
MySQLPORT: 3306
MySQLDB: ttt
MySQLUSER: root
MySQLPASSWORD: ttttttttttttt
MySQLCharset: utf8mb4
MySQLLoc: Asia/Shanghai
GORMLog: true

@ -0,0 +1,73 @@
package global
import (
"WechatGateWay/handle"
"fmt"
"github.com/jinzhu/gorm"
"github.com/spf13/viper"
"log"
"net/url"
)
func InitDB() *gorm.DB {
driverName := "mysql"
host := viper.GetString("db.MySQLHOST")
port := viper.GetInt("db.MySQLPORT")
database := viper.GetString("db.MySQLDB")
username := viper.GetString("db.MySQLUSER")
password := viper.GetString("db.MySQLPASSWORD")
charset := viper.GetString("db.MySQLCharset")
loc := viper.GetString("db.MySQLLoc")
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=true&loc=%s",
username,
password,
host,
port,
database,
charset,
url.QueryEscape(loc))
//dsn := "user:pass@tcp(127.0.0.1:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(driverName, dsn)
//db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err == nil {
log.Println("Connected to database")
//db.DB()实现一个连接池
//设置最大连接数和最大闲置数
//默认0表示不限制
db.DB().SetMaxIdleConns(0)
db.DB().SetMaxOpenConns(0)
//给默认表名加前缀
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
//return "admin_" + defaultTableName + "_test_env"
return "" + defaultTableName + ""
}
// 全局禁用表名复数
// 如果设置为true,`User`的默认表名为`user`,使用`TableName`设置的表名不受影响
// 如果设置为false,`User`的默认表名为`users`
db.SingularTable(false)
//打开sql日志
db.LogMode(viper.GetBool("db.GORMLog"))
//创建表时,添加后缀
db.Set("grom:table_options", "ENGINE=InnoDB")
//自动建表
db.AutoMigrate(&handle.WechatLog{})
DB = db
return db
} else {
log.Println("Connected to database failed")
//log.Println("failed to connect database, err: " + err.Error())
panic("failed to connect database, err: " + err.Error())
return nil
}
}

@ -3,11 +3,13 @@ package global
import (
"context"
"github.com/go-redis/redis/v8"
"github.com/jinzhu/gorm"
)
var (
CTX = context.Background()
RedisDb *redis.Client
DB *gorm.DB
HistoryCmds []string
EnableReply = true
EnableAiReply = true

@ -8,4 +8,5 @@ var (
WechatSendSecret string
WechatSendAid string
WechatAccessToken string //主动发消息的token
WechatImagePath string
)

@ -8,6 +8,8 @@ require (
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/gorm v1.9.16 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect

@ -0,0 +1,12 @@
package handle
import "github.com/jinzhu/gorm"
type WechatLog struct {
gorm.Model
ToUsername string `xml:"ToUserName"`
FromUsername string `xml:"FromUserName"`
CreateTime uint32 `xml:"CreateTime"`
MsgType string `xml:"MsgType"`
Content string `xml:"Content"`
}

@ -0,0 +1,23 @@
package handle
import (
"WechatGateWay/global"
"fmt"
"log"
)
func RecordWechatLog(ToUsername, FromUsername string, CreateTime uint32, MsgType, Content string) {
newRecord := WechatLog{
ToUsername: ToUsername,
FromUsername: FromUsername,
CreateTime: CreateTime,
MsgType: MsgType,
Content: Content,
}
result := global.DB.Create(&newRecord) // 通过数据的指针来创建
if result.RowsAffected != 1 {
log.Println(fmt.Sprintf("记录失败ToUsername: %s MsgType: %s Content: %s", ToUsername, MsgType, Content))
}
}

@ -1,6 +1,7 @@
package handle
import (
"WechatGateWay/global"
"WechatGateWay/third_part"
"WechatGateWay/utils"
"net/http"
@ -8,5 +9,7 @@ import (
func replyImage(msgContent MsgContent, timestamp, nonce string, w http.ResponseWriter) {
//third_part.SendPicMid(msgContent.FromUsername, utils.RandMapValue(global.ImageMap))
third_part.SendPicFile(msgContent.FromUsername, "/app/data/"+utils.RandomFile("/app/data"))
filename := global.WechatImagePath + "/" + utils.RandomFile(global.WechatImagePath)
go RecordWechatLog(msgContent.ToUsername, msgContent.FromUsername, msgContent.CreateTime, msgContent.MsgType, filename)
third_part.SendPicFile(msgContent.FromUsername, filename)
}

@ -31,9 +31,15 @@ func init() {
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() {

Loading…
Cancel
Save