setting.go 5.47 KB
Newer Older
wangp's avatar
wangp committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
package setting

import (
	"fmt"
	"strconv"
	"time"

	"github.com/fsnotify/fsnotify"
	"github.com/spf13/viper"
)

var Conf = new(Config)

type Config struct {
	ServerSetting   *Server                 `mapstructure:"server"`
	AppSetting      *App                    `mapstructure:"app"`
	DatabaseSetting *Database               `mapstructure:"database"`
	LogSetting      *Log                    `mapstructure:"log"`
wangp's avatar
wangp committed
19 20
	PayUrl          *PayUrlDetail           `mapstructure:"payurl"`
	Lakala          *Lakala                 `mapstructure:"lakala"`
wangp's avatar
wangp committed
21 22 23 24 25 26 27 28 29 30
}

// Server 服务
type Server struct {
	RunMode      string        `mapstructure:"runmode"`
	HttpPort     int           `mapstructure:"httpport"`
	ReadTimeout  time.Duration `mapstructure:"readtimeout"`
	WriteTimeout time.Duration `mapstructure:"writetimeout"`
}

wangp's avatar
wangp committed
31 32 33 34 35 36
type App struct {
	Name       string `mapstructure:"name"`
	Version    string `mapstructure:"version"`
	TimeFormat string `mapstructure:"timeformat""`
}

wangp's avatar
wangp committed
37 38 39
// Database DB
type Database struct {
	Type      string `mapstructure:"type""`
wangp's avatar
wangp committed
40
	SystemDB  string `mapstructure:"systemdb"`
wangp's avatar
wangp committed
41 42
}

wangp's avatar
wangp committed
43 44 45 46 47 48 49 50
type Log struct {
	LogSavePath string `mapstructure:"logsavepath"`
	LogSaveName string `mapstructure:"logsavename"`
	LogFileExt  string `mapstructure:"logfileext"`
	Level       string `mapstructure:"level"`
	MaxSize     int    `mapstructure:"max_size"`
	MaxAge      int    `mapstructure:"max_age"`
	MaxBackups  int    `mapstructure:"max_backups"`
wangp's avatar
wangp committed
51 52
}

wangp's avatar
wangp committed
53
// PayUrlDetail 网关
wangp's avatar
wangp committed
54 55 56
type PayUrlDetail struct {
	DomainName string `mapstructure:"domainname"`
}
wangp's avatar
wangp committed
57

wangp's avatar
wangp committed
58 59
// 拉卡拉支付
type Lakala struct {
wangp's avatar
wangp committed
60 61 62 63
	// 谛宝多多自营 - start
	DbcVersion string `mapstructure:"dbc_version"`
	DbcAppid string `mapstructure:"dbc_appid"`
	DbcSerialNo string `mapstructure:"dbc_serial_no"`
wangp's avatar
wangp committed
64
	// 1.聚合收银台(微信H5、支付宝H5)
wangp's avatar
wangp committed
65
	DbcMerchantNo1 string `mapstructure:"dbc_merchant_no1"`
wangp's avatar
wangp committed
66
	// 2.聚合收银台(微信扫码、支付宝扫码)
wangp's avatar
wangp committed
67
	DbcMerchantNo2 string `mapstructure:"dbc_merchant_no2"`
wangp's avatar
wangp committed
68
	// 3.聚合主扫(微信JSAPI、微信小程序)
wangp's avatar
wangp committed
69 70
	DbcMerchantNo3 string `mapstructure:"dbc_merchant_no3"`
	DbcTermNo3 string `mapstructure:"dbc_term_no3"`
wangp's avatar
wangp committed
71
	// 4.扫码枪
wangp's avatar
wangp committed
72 73
	DbcMerchantNo4 string `mapstructure:"dbc_merchant_no4"`
	DbcTermNo4 string `mapstructure:"dbc_term_no4"`
wangp's avatar
wangp committed
74 75 76
	// 配置项
	DbcPathPrivateKey string `mapstructure:"dbc_path_private_key"` //私钥
	DbcPathCert string `mapstructure:"dbc_path_cert"` //证书
wangp's avatar
wangp committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	// 谛宝多多自营 - end

	// 必康自营 - start
	BkVersion string `mapstructure:"bk_version"`
	BkAppid string `mapstructure:"bk_appid"`
	BkSerialNo string `mapstructure:"bk_serial_no"`
	// 1.聚合收银台(微信H5、支付宝H5)
	BkMerchantNo1 string `mapstructure:"bk_merchant_no1"`
	// 2.聚合收银台(微信扫码、支付宝扫码)
	BkMerchantNo2 string `mapstructure:"bk_merchant_no2"`
	// 3.聚合主扫(微信JSAPI、微信小程序)
	BkMerchantNo3 string `mapstructure:"bk_merchant_no3"`
	BkTermNo3 string `mapstructure:"bk_term_no3"`
	// 4.扫码枪
	BkMerchantNo4 string `mapstructure:"bk_merchant_no4"`
	BkTermNo4 string `mapstructure:"bk_term_no4"`
	// 配置项
wangp's avatar
wangp committed
94 95
	BkPathPrivateKey string `mapstructure:"bk_path_private_key"` //私钥
	BkPathCert string `mapstructure:"bk_path_cert"` //证书
wangp's avatar
wangp committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
	// 必康自营 - end

	// 必康医生 - start
	SaasVersion string `mapstructure:"saas_version"`
	SaasAppid string `mapstructure:"saas_appid"`
	SaasSerialNo string `mapstructure:"saas_serial_no"`
	// 1.聚合收银台(微信H5、支付宝H5)
	SaasMerchantNo1 string `mapstructure:"saas_merchant_no1"`
	// 2.聚合收银台(微信扫码、支付宝扫码)
	SaasMerchantNo2 string `mapstructure:"saas_merchant_no2"`
	// 3.聚合主扫(微信JSAPI、微信小程序)
	SaasMerchantNo3 string `mapstructure:"saas_merchant_no3"`
	SaasTermNo3 string `mapstructure:"saas_term_no3"`
	// 4.扫码枪
	SaasMerchantNo4 string `mapstructure:"saas_merchant_no4"`
	SaasTermNo4 string `mapstructure:"saas_term_no4"`
	// 配置项
wangp's avatar
wangp committed
113 114
	SaasPathPrivateKey string `mapstructure:"saas_path_private_key"` //私钥
	SaasPathCert string `mapstructure:"saas_path_cert"` //证书
wangp's avatar
wangp committed
115 116
	// 必康医生 - end

wangp's avatar
wangp committed
117 118 119 120 121
	// 拉卡拉接口地址
	UrlCreate string `mapstructure:"url_create"` //聚合收银台(微信H5、支付宝H5、微信扫码、支付宝扫码)
	UrlPreorder string `mapstructure:"url_preorder"` //聚合主扫(微信JSAPI、微信小程序)
	UrlMicropay string `mapstructure:"url_micropay"` //聚合被扫(扫码枪)
	UrlRefund string `mapstructure:"url_refund"` //聚合扫码-退款交易
wangp's avatar
wangp committed
122
	UrlTradeRefund string `mapstructure:"url_trade_refund"` //统一退货接口
123
	UrlOrderState string `mapstructure:"url_order_state"` //聚合扫码-交易查询
wangp's avatar
wangp committed
124 125
}

wangp's avatar
wangp committed
126 127
// Init 支持热修改的viper设置
func Init() error {
wangp's avatar
wangp committed
128
	viper.SetConfigFile("conf/config.yaml") // 指定配置文件路径
wangp's avatar
wangp committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	err := viper.ReadInConfig()             // 读取配置信息
	if err != nil {                         // 读取配置信息失败
		fmt.Printf("viper.ReadInConfig failed, new_error:%v\n", err)
		return err
	}
	// 将读取的配置信息保存至全局变量Conf
	if err = viper.Unmarshal(Conf); err != nil {
		fmt.Printf("viper.Unmarshal failed, new_error:%v\n", err)
	}
	// 监控配置文件变化
	viper.WatchConfig()
	// 注意!!!配置文件发生变化后要同步到全局变量Conf
	viper.OnConfigChange(func(in fsnotify.Event) {
		fmt.Println("配置文件被修改啦...")
		if err = viper.Unmarshal(Conf); err != nil {
			fmt.Printf("viper.Unmarshal failed, new_error:%v\n", err)
		}
		//fmt.Println(Conf)
	})

	return err
}

func GetPort() string {
	s := strconv.Itoa(Conf.ServerSetting.HttpPort)
	return ":" + s
}