config.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. package config
  2. import (
  3. "github.com/Unknwon/goconfig"
  4. "github.com/druidcaesa/gotool"
  5. "log"
  6. "runtime"
  7. "strconv"
  8. "time"
  9. )
  10. // DEV 开发环境
  11. const DEV = "dev"
  12. // PROD 生产环境
  13. const PROD = "prod"
  14. const Version = "1.0.9"
  15. var (
  16. Cfg *goconfig.ConfigFile
  17. WechatCfg *WechatConfig
  18. )
  19. func init() {
  20. var err error
  21. var build string
  22. if IsDev() {
  23. build = DEV
  24. } else {
  25. build = PROD
  26. }
  27. Cfg, err = goconfig.LoadConfigFile("./config/config-" + build + ".ini")
  28. if err != nil {
  29. log.Fatal(err)
  30. }
  31. return
  32. }
  33. type JwtConfig struct {
  34. TimeOut time.Duration //超时时间
  35. Issuer string //签证签发人
  36. Cache string
  37. }
  38. type SmsConfig struct {
  39. AccessKeyID string //签证签发人
  40. AccessKeySecret string
  41. }
  42. // FilePath 文件存储配置获取
  43. type FilePath struct {
  44. Path string
  45. }
  46. // CaptchaConfig 文件存储配置获取
  47. type CaptchaConfig struct {
  48. Type string
  49. }
  50. type AuthConfig struct {
  51. IsOpen string
  52. }
  53. // DbCfg Mysql数据库配置
  54. type DbCfg struct {
  55. Username string
  56. Password string
  57. Database string
  58. Host string
  59. Port string
  60. MaxIdleConnection int
  61. MaxOpenConnection int
  62. ShowType string
  63. }
  64. // AppServer 应用程序配置
  65. type AppServer struct {
  66. Port string //App运行端口
  67. Lock string //是否开启多终端登录 0开启 1不开启
  68. DemoEnabled bool //是否是演示模式
  69. Redis bool //是否开启redis
  70. }
  71. // LoggerCfg 日志配置结构体
  72. type LoggerCfg struct {
  73. LogPath string
  74. LogName string
  75. }
  76. // RedisCfg redis配置结构体
  77. type RedisCfg struct {
  78. RedisHost string //地址
  79. Port int64 //端口
  80. RedisPwd string //密码
  81. RedisDB int64 //数据库
  82. Timeout int64 //超时时间
  83. }
  84. // MongoDb mongo配置结构体
  85. type MongoDb struct {
  86. Url string
  87. Port string
  88. DB string
  89. User string
  90. Password string
  91. }
  92. // WechatConfig 微信配置结构体
  93. type WechatConfig struct {
  94. MiniProgramAppId string //小程序appId
  95. MiniProgramSecret string //小程序app secret
  96. MchID string //微信支付商户号ID
  97. MchApiV2Key string //微信商户里面设置的API V2密钥
  98. MchApiV3Key string //微信商户里面设置的API V3密钥
  99. CertPath string //微信商户里面设置的API V3证书
  100. KeyPath string //API V3私钥。
  101. SerialNo string //微信支付V3证书的序列号
  102. NotifyHost string //微信支付完成后的通知回调地址域名
  103. NotifyURL string //微信支付完成后的通知回调地址
  104. NotifyRefundURL string //微信退款完成后的通知回调地址
  105. }
  106. type SysCfg struct {
  107. Version string //版本号
  108. InstallPath string //安装包路径
  109. IntegralSn string //积分服务Sn号
  110. CouponSn string //优惠券服务Sn号
  111. }
  112. type OmsApiCfg struct {
  113. CommonSetting string //总台获取配置接口
  114. MarketAppId string //总台appId
  115. ServiceAppId string //服务市场appId
  116. ServiceAppSecret string //服务市场密钥
  117. AccountLogin string //登录token接口
  118. LoginRedirection string //重定向登录
  119. DeliverDetail string //POST设置服务,GET获取服务详情
  120. DeliverPostTrack string //获取骑手位置
  121. DeliverOrderLogs string //配送记录(订单配送详情)
  122. DeliverOrderStart string //配送发起
  123. DeliverOrderPrice string //配送价格获取【多平台】
  124. DeliverOrder string //GET订单详情,POST创建订单
  125. DeliverOrderCancel string //取消订单
  126. ServiceList string //服务列表
  127. ServiceDetail string //服务详情
  128. ServiceCategories string //服务分类列表
  129. ServiceMemberLists string //已购服务列表
  130. UploadConfig string //上传配置
  131. ServiceBuy string //服务购买
  132. ServiceUse string //数量类型服务消耗
  133. ServiceUseCheck string //验证服务useSN
  134. ServiceLogs string //服务消耗记录
  135. ServiceInfo string //服务市场信息
  136. }
  137. type PrinterCfg struct {
  138. Appid string //商鹏云打印机用户唯一凭证
  139. AppSecret string //商鹏云打印机API密钥
  140. PrinterAdd string //添加打印机
  141. PrinterInfo string //获取打印机信息
  142. PrinterUpdate string //修改打印机信息
  143. PrinterSetting string //修改打印机配置参数
  144. PrinterDelete string //删除打印机
  145. PrinterPrint string //打印订单
  146. PrinterClear string //清空待打印订单
  147. PrinterOrderStatus string //查询打印订单状态
  148. PrinterOrderNumber string //查询打印机历史打印订单数
  149. }
  150. // GetMysqlCfg 读取mysql配置
  151. func GetMysqlCfg() (mysql DbCfg) {
  152. mysql.Username, _ = Cfg.GetValue("mysql", "username")
  153. mysql.Password, _ = Cfg.GetValue("mysql", "password")
  154. mysql.Database, _ = Cfg.GetValue("mysql", "database")
  155. mysql.Host, _ = Cfg.GetValue("mysql", "host")
  156. mysql.Port, _ = Cfg.GetValue("mysql", "port")
  157. mysql.ShowType, _ = Cfg.GetValue("mysql", "sqlType")
  158. value, _ := Cfg.GetValue("mysql", "MaxIdleConnection")
  159. mysql.MaxIdleConnection, _ = strconv.Atoi(value)
  160. v, _ := Cfg.GetValue("mysql", "MaxOpenConnection")
  161. mysql.MaxOpenConnection, _ = strconv.Atoi(v)
  162. return mysql
  163. }
  164. // GetServerCfg 读取server配置
  165. func GetServerCfg() (server AppServer) {
  166. server.Port, _ = Cfg.GetValue("app", "server")
  167. server.Lock, _ = Cfg.GetValue("app", "lock")
  168. demoEnabled, _ := Cfg.GetValue("app", "demoEnabled")
  169. redis, _ := Cfg.GetValue("app", "redis")
  170. server.Redis = redis == "0"
  171. server.DemoEnabled = demoEnabled == "0"
  172. return server
  173. }
  174. // GetLoggerCfg 获取Logger配置
  175. func GetLoggerCfg() (logger LoggerCfg) {
  176. logger.LogPath, _ = Cfg.GetValue("logger", "logPath")
  177. if IsDev() {
  178. logger.LogPath = "c:/" + logger.LogPath
  179. }
  180. logger.LogName, _ = Cfg.GetValue("logger", "logName")
  181. return logger
  182. }
  183. // GetMongoCfg 获取mongo配置
  184. func GetMongoCfg() (mongo MongoDb) {
  185. mongo.Url, _ = Cfg.GetValue("mongodb", "url")
  186. mongo.Port, _ = Cfg.GetValue("mongodb", "port")
  187. mongo.DB, _ = Cfg.GetValue("mongodb", "db")
  188. mongo.User, _ = Cfg.GetValue("mongodb", "user")
  189. mongo.Password, _ = Cfg.GetValue("mongodb", "password")
  190. return mongo
  191. }
  192. // GetRedisCfg 获取redis配置
  193. func GetRedisCfg() (r RedisCfg) {
  194. r.RedisHost, _ = Cfg.GetValue("redis", "host")
  195. getValue, _ := Cfg.GetValue("redis", "port")
  196. r.Port, _ = strconv.ParseInt(getValue, 10, 32)
  197. r.RedisPwd, _ = Cfg.GetValue("redis", "password")
  198. db, _ := Cfg.GetValue("redis", "db")
  199. r.RedisDB, _ = strconv.ParseInt(db, 10, 32)
  200. value, _ := Cfg.GetValue("redis", "timeout")
  201. r.Timeout, _ = strconv.ParseInt(value, 10, 64)
  202. return r
  203. }
  204. // GetFilePath 获取文件存储位置
  205. func GetFilePath() (g FilePath) {
  206. g.Path, _ = Cfg.GetValue("filePath", "path")
  207. if IsDev() {
  208. g.Path = "c:/" + g.Path
  209. }
  210. return g
  211. }
  212. // GetCaptchaConfig 获取文件存储位置
  213. func GetCaptchaConfig() (g CaptchaConfig) {
  214. g.Type, _ = Cfg.GetValue("captchaConfig", "type")
  215. return g
  216. }
  217. // GetAuthConfig 开启后端认证
  218. func GetAuthConfig() (g AuthConfig) {
  219. g.IsOpen, _ = Cfg.GetValue("auth", "isOpen")
  220. return g
  221. }
  222. func GetJwtConfig() (j JwtConfig) {
  223. value, _ := Cfg.GetValue("jwt", "timeOut")
  224. issuer, _ := Cfg.GetValue("jwt", "issuer")
  225. cache, _ := Cfg.GetValue("jwt", "cache")
  226. if gotool.StrUtils.HasEmpty(value) {
  227. value = "60"
  228. }
  229. if gotool.StrUtils.HasEmpty(issuer) {
  230. value = "monkey-cool"
  231. }
  232. if gotool.StrUtils.HasEmpty(cache) {
  233. value = "memory"
  234. }
  235. timeOut, _ := strconv.ParseInt(value, 10, 64)
  236. j.TimeOut = time.Duration(timeOut)
  237. j.Issuer = issuer
  238. j.Cache = cache
  239. return j
  240. }
  241. func GetWechatConfig() (g WechatConfig) {
  242. g.MiniProgramAppId, _ = Cfg.GetValue("wechat", "miniProgramAppId")
  243. g.MiniProgramSecret, _ = Cfg.GetValue("wechat", "miniProgramSecret")
  244. g.MchID, _ = Cfg.GetValue("wechat", "mchID")
  245. g.MchApiV2Key, _ = Cfg.GetValue("wechat", "mchApiV2Key")
  246. g.MchApiV3Key, _ = Cfg.GetValue("wechat", "mchApiV3Key")
  247. g.CertPath, _ = Cfg.GetValue("wechat", "certPath")
  248. g.KeyPath, _ = Cfg.GetValue("wechat", "keyPath")
  249. if IsDev() {
  250. g.CertPath = "c:/" + g.CertPath
  251. g.KeyPath = "c:/" + g.KeyPath
  252. }
  253. g.SerialNo, _ = Cfg.GetValue("wechat", "serialNo")
  254. g.NotifyHost, _ = Cfg.GetValue("wechat", "notifyHost")
  255. g.NotifyURL, _ = Cfg.GetValue("wechat", "notifyURL")
  256. g.NotifyRefundURL, _ = Cfg.GetValue("wechat", "notifyRefundURL")
  257. return g
  258. }
  259. func GetOmsApiCfg() (g OmsApiCfg) {
  260. g.CommonSetting, _ = Cfg.GetValue("oms", "commonSetting")
  261. g.MarketAppId, _ = Cfg.GetValue("oms", "marketAppId")
  262. g.ServiceAppId, _ = Cfg.GetValue("oms", "serviceAppId")
  263. g.ServiceAppSecret, _ = Cfg.GetValue("oms", "serviceAppSecret")
  264. g.AccountLogin = "/api/account/v1/login"
  265. g.LoginRedirection = "/api/account/v1/login/redirection"
  266. g.DeliverDetail = "/api/deliver/detail"
  267. g.DeliverPostTrack = "/api/deliver/order/post-track"
  268. g.DeliverOrderLogs = "/api/deliver/order/logs"
  269. g.DeliverOrderStart = "/api/deliver/order/start"
  270. g.DeliverOrderPrice = "/api/deliver/order/price"
  271. g.DeliverOrder = "/api/deliver/order"
  272. g.DeliverOrderCancel = "/api/deliver/order/cancel"
  273. g.ServiceList = "/api/service/v1/services"
  274. g.ServiceDetail = "/api/service/v1/service"
  275. g.ServiceCategories = "/api/service/v1/categories"
  276. g.ServiceMemberLists = "/api/service/v1/member/lists"
  277. g.ServiceBuy = "/api/service/v1/buy"
  278. g.ServiceUse = "/api/service/v1/service/use"
  279. g.ServiceUseCheck = "/api/service/v1/service/use/check"
  280. g.ServiceLogs = "/api/service/v1/logs"
  281. g.ServiceInfo = "/api/account/v1/info"
  282. g.UploadConfig = "/api/account/v1/config"
  283. return g
  284. }
  285. func GetSysCfg() (g SysCfg) {
  286. g.Version = Version
  287. g.InstallPath = "./look_update.zip"
  288. g.IntegralSn = "480ff456"
  289. g.CouponSn = "65af5f28"
  290. return g
  291. }
  292. func GetSmsCfg() (g SmsConfig) {
  293. g.AccessKeyID, _ = Cfg.GetValue("sms", "accessKeyID")
  294. g.AccessKeySecret, _ = Cfg.GetValue("sms", "accessKeySecret")
  295. return g
  296. }
  297. func GetPrinterCfg() (g PrinterCfg) {
  298. g.Appid = "sp63ae90383c07d"
  299. g.AppSecret = "6064c1365df51dafae0e83544159caf1"
  300. g.PrinterAdd = "https://open.spyun.net/v1/printer/add"
  301. g.PrinterInfo = "https://open.spyun.net/v1/printer/info"
  302. g.PrinterUpdate = "https://open.spyun.net/v1/printer/update"
  303. g.PrinterSetting = "https://open.spyun.net/v1/printer/setting"
  304. g.PrinterDelete = "https://open.spyun.net/v1/printer/delete"
  305. g.PrinterPrint = "https://open.spyun.net/v1/printer/print"
  306. g.PrinterClear = "https://open.spyun.net/v1/printer/cleansqs"
  307. g.PrinterOrderStatus = "https://open.spyun.net/v1/printer/order/status"
  308. g.PrinterOrderNumber = "https://open.spyun.net/v1/printer/order/number"
  309. return g
  310. }
  311. func IsDev() bool {
  312. /* for _, v := range os.Args {
  313. if v == "dev" {
  314. return true
  315. }
  316. }
  317. return false*/
  318. return runtime.GOOS == "windows"
  319. //return true
  320. }