123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- package util
- import (
- "fmt"
- "github.com/leeqvip/gophp/serialize"
- "gopkg.in/mgo.v2"
- "gopkg.in/mgo.v2/bson"
- "log"
- "net/url"
- "time"
- "xiaoniaokuaiyan.com/xiaoniao/config"
- )
- /**
- * @Author: qz
- * @Date: 2021/3/11 18:05
- * @Description:
- */
- var (
- mgoSess *mgo.Session
- )
- func GetMgoDB() *mgo.Database {
- var (
- mgoURI = config.IniConf.Section("database").Key("mgo.uri").Value()
- mgoDatabase = config.IniConf.Section("database").Key("mgo.database").Value()
- mgoUser = config.IniConf.Section("database").Key("mgo.user").Value()
- mgoPassword = config.IniConf.Section("database").Key("mgo.password").Value()
- )
- mgoURI = fmt.Sprintf("%s:%s@%s/%s", mgoUser, url.QueryEscape(mgoPassword), mgoURI, mgoDatabase)
- var err error
- if mgoSess == nil {
- mgoSess, err = mgo.Dial(mgoURI)
- if err != nil {
- log.Println(err)
- return nil
- }
- }
- return mgoSess.Copy().DB(mgoDatabase)
- }
- func InsertMongo(name, source string, dbtype int, data map[string]interface{}) {
- jsonbyte, err := serialize.Marshal(data)
- if err != nil {
- fmt.Println(err)
- return
- }
- db := GetMgoDB()
- if db == nil {
- return
- }
- defer db.Session.Close()
- err = db.C("t_log").Insert(bson.M{
- "name": name,
- "type": dbtype,
- "data": string(jsonbyte),
- "createby": source,
- "create": time.Now().Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- fmt.Println(err)
- return
- }
- }
|