Files
jx-callback/globals/globals2/globals2.go

75 lines
2.5 KiB
Go

package globals2
import (
"git.rosy.net.cn/baseapi/platformapi/dadaapi"
"git.rosy.net.cn/baseapi/platformapi/elmapi"
"git.rosy.net.cn/baseapi/platformapi/jdapi"
"git.rosy.net.cn/baseapi/platformapi/mtpsapi"
"git.rosy.net.cn/baseapi/platformapi/weixinapi"
"git.rosy.net.cn/baseapi/utils"
elmmodels "git.rosy.net.cn/jx-callback/business/elm/models"
"git.rosy.net.cn/jx-callback/business/freshfood"
jdmodels "git.rosy.net.cn/jx-callback/business/jd/models"
"git.rosy.net.cn/jx-callback/business/models"
"github.com/astaxie/beego"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql" // import your used driver
)
const (
ErrStrAccessDB = "Error when access DB"
)
var (
FreshFoodAPI *freshfood.FreshFoodAPI
Jdapi *jdapi.API
ElmAPI *elmapi.API
MtpsAPI *mtpsapi.API
DadaAPI *dadaapi.API
WeixinAPI *weixinapi.API
)
func init() {
initDB()
FreshFoodAPI = freshfood.NewFreshFoodAPI(beego.AppConfig.String("freshFoodServerURL"))
Jdapi = jdapi.New(beego.AppConfig.String("jdToken"), beego.AppConfig.String("jdAppKey"), beego.AppConfig.String("jdSecret"))
initElm()
MtpsAPI = mtpsapi.New(beego.AppConfig.String("mtpsAppKey"), beego.AppConfig.String("mtpsSecret"))
DadaAPI = dadaapi.New(beego.AppConfig.String("dadaAppKey"), beego.AppConfig.String("dadaAppSecret"), beego.AppConfig.String("dadaSourceID"), beego.AppConfig.String("dadaCallbackURL"), beego.AppConfig.DefaultBool("dadaIsProd", false))
WeixinAPI = weixinapi.New(beego.AppConfig.String("weixinAppID"), beego.AppConfig.String("weixinSecret"))
}
func initElm() {
token := beego.AppConfig.String("elmToken")
if token == "" {
db := orm.NewOrm()
var tokenInfo []orm.Params
num, err := db.Raw("SELECT * FROM config WHERE thirdparty='eleme'").Values(&tokenInfo)
if err != nil || num != 1 {
panic(err.Error())
}
var tokenInfo2 map[string]interface{}
if err := utils.UnmarshalUseNumber([]byte(tokenInfo[0]["token"].(string)), &tokenInfo2); err != nil {
panic(err.Error())
}
token = tokenInfo2["accessToken"].(string)
}
ElmAPI = elmapi.New(token, beego.AppConfig.String("elmAppKey"), beego.AppConfig.String("elmSecret"), beego.AppConfig.DefaultBool("elmIsProd", false))
}
func initDB() {
// set default database
orm.RegisterDataBase("default", "mysql", beego.AppConfig.String("dbConnectStr"), 30)
// register model
orm.RegisterModel(new(jdmodels.Jdorder))
orm.RegisterModel(new(elmmodels.ELMOrder))
orm.RegisterModel(new(models.Config))
// create table
orm.RunSyncdb("default", false, true)
}