Browse Source

fix:常量名称改成配置化

Roc 1 year ago
parent
commit
ef477ea8b3
6 changed files with 139 additions and 96 deletions
  1. 30 3
      go.mod
  2. 0 1
      go.sum
  3. 9 10
      services/alarm_msg/alarm_msg.go
  4. 7 22
      services/oss.go
  5. 88 36
      utils/config.go
  6. 5 24
      utils/constants.go

+ 30 - 3
go.mod

@@ -1,15 +1,42 @@
 module hongze/hongze_public_api
 
-go 1.16
+go 1.19
 
 require (
 	github.com/aliyun/aliyun-oss-go-sdk v2.2.4+incompatible
-	github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
 	github.com/beego/bee/v2 v2.0.2
 	github.com/beego/beego/v2 v2.0.1
 	github.com/go-sql-driver/mysql v1.6.0
 	github.com/kardianos/service v1.2.1
 	github.com/rdlucklib/rdluck_tools v1.0.3
-	github.com/satori/go.uuid v1.2.0 // indirect
 	gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
 )
+
+require (
+	github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
+	github.com/beorn7/perks v1.0.1 // indirect
+	github.com/cespare/xxhash/v2 v2.1.1 // indirect
+	github.com/garyburd/redigo v1.6.3 // indirect
+	github.com/golang/protobuf v1.4.2 // indirect
+	github.com/hashicorp/golang-lru v0.5.4 // indirect
+	github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
+	github.com/mitchellh/mapstructure v1.3.3 // indirect
+	github.com/pkg/errors v0.9.1 // indirect
+	github.com/prometheus/client_golang v1.7.0 // indirect
+	github.com/prometheus/client_model v0.2.0 // indirect
+	github.com/prometheus/common v0.10.0 // indirect
+	github.com/prometheus/procfs v0.1.3 // indirect
+	github.com/satori/go.uuid v1.2.0 // indirect
+	github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
+	golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
+	golang.org/x/mod v0.3.0 // indirect
+	golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
+	golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211 // indirect
+	golang.org/x/text v0.3.3 // indirect
+	golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
+	golang.org/x/tools v0.0.0-20201211185031-d93e913c1a58 // indirect
+	golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
+	google.golang.org/protobuf v1.23.0 // indirect
+	gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
+	gopkg.in/yaml.v2 v2.3.0 // indirect
+)

+ 0 - 1
go.sum

@@ -44,7 +44,6 @@ github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJm
 github.com/bradfitz/gomemcache v0.0.0-20180710155616-bc664df96737/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60=
 github.com/casbin/casbin v1.7.0/go.mod h1:c67qKN6Oum3UF5Q1+BByfFxkwKvhwW57ITjqwtzR1KE=
 github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
-github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
 github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
 github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY=
 github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=

+ 9 - 10
services/alarm_msg/alarm_msg.go

@@ -6,15 +6,14 @@ import (
 	"hongze/hongze_public_api/utils"
 )
 
-var (
-	AlarmMsgUrl = "http://127.0.0.1:8606/api/alarm/send"
-)
-
-//projectName-项目名称
-//runMode-运行模式
-//msgBody-消息内容
-//level:消息基本,1:提示消息,2:警告消息,3:严重错误信息,默认为1 提示消息
+// projectName-项目名称
+// runMode-运行模式
+// msgBody-消息内容
+// level:消息基本,1:提示消息,2:警告消息,3:严重错误信息,默认为1 提示消息
 func SendAlarmMsg(msgBody string, level int) {
+	if utils.AlarmMsgUrl == "" {
+		return
+	}
 	params := make(map[string]interface{})
 	params["ProjectName"] = utils.APPNAME
 	params["RunMode"] = utils.RunMode
@@ -25,5 +24,5 @@ func SendAlarmMsg(msgBody string, level int) {
 		utils.FileLog.Info("SendAlarmMsg json.Marshal Err:" + err.Error())
 		return
 	}
-	http.Post(AlarmMsgUrl, string(param))
-}
+	http.Post(utils.AlarmMsgUrl, string(param))
+}

+ 7 - 22
services/oss.go

@@ -8,31 +8,16 @@ import (
 	"hongze/hongze_public_api/utils"
 )
 
-const (
-	HzEndpoint          = "oss-cn-shanghai.aliyuncs.com"
-	HzBucketName string = "hzchart"
-)
-
-// 后台用于传研报小程序二维码
-var (
-	Bucketname      string = "hzchart"
-	Endpoint        string = "oss-cn-shanghai.aliyuncs.com"
-	Imghost         string = "https://hzstatic.hzinsights.com/"
-	UploadDir       string = "static/images/"
-	AccessKeyId     string = "LTAIFMZYQhS2BTvW"
-	AccessKeySecret string = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq"
-)
-
-// 上传文件到阿里云
+// UploadFileToAliyun 上传文件到阿里云
 func UploadFileToAliyun(filename, filepath, savePath string) error {
 	defer func() {
 		os.Remove(filepath)
 	}()
-	client, err := oss.New(HzEndpoint, utils.AccessKeyId, utils.AccessKeySecret)
+	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
 	if err != nil {
 		return err
 	}
-	bucket, err := client.Bucket(HzBucketName)
+	bucket, err := client.Bucket(utils.Bucketname)
 	if err != nil {
 		return err
 	}
@@ -49,20 +34,20 @@ func UploadFileToAliyun(filename, filepath, savePath string) error {
 
 // UploadAliYun 图片上传到阿里云
 func UploadAliYun(filename, filepath string) (string, error) {
-	client, err := oss.New(Endpoint, AccessKeyId, AccessKeySecret)
+	client, err := oss.New(utils.Endpoint, utils.AccessKeyId, utils.AccessKeySecret)
 	if err != nil {
 		return "1", err
 	}
-	bucket, err := client.Bucket(Bucketname)
+	bucket, err := client.Bucket(utils.Bucketname)
 	if err != nil {
 		return "2", err
 	}
-	path := UploadDir + time.Now().Format("200601/20060102/")
+	path := utils.UploadDir + time.Now().Format("200601/20060102/")
 	path += filename
 	err = bucket.PutObjectFromFile(path, filepath)
 	if err != nil {
 		return "3", err
 	}
-	path = Imghost + path
+	path = utils.Imghost + path
 	return path, err
 }

+ 88 - 36
utils/config.go

@@ -27,16 +27,48 @@ var (
 	AdminWxAppId     string
 	AdminWxAppSecret string
 
-	//微信列表
+	// WxAppList微信列表
 	WxAppList map[string]string
 
 	WxAppIdCygx     string
 	WxAppSecretCygx string
+
+	// 小程序相关
+
+	// WxCrmAppId 随手办公小程序
+	WxCrmAppId string
+	// WxCygxAppId 弘则研报小程序
+	WxYbAppId string
+	// WxCygxAppId 查研观向小程序
+	WxCygxAppId string
 )
 
 // LibreOfficePath LibreOfficePath的地址
 var LibreOfficePath string
 
+// 基础配置
+var (
+	SystemType       string // 系统类型; hz:弘则;trial:试用平台;custom:客户
+	APPNAME          string //项目中文名称
+	EmailSendToUsers string // 邮件提醒人员
+
+	// Authorization  签名秘钥
+	Authorization string
+
+	// AlarmMsgUrl 报警服务地址
+	AlarmMsgUrl string
+)
+
+// 后台用于传研报小程序二维码
+var (
+	Bucketname      string
+	Endpoint        string
+	Imghost         string
+	UploadDir       string
+	AccessKeyId     string
+	AccessKeySecret string
+)
+
 func init() {
 	tmpRunMode, err := web.AppConfig.String("run_mode")
 	if err != nil {
@@ -59,9 +91,6 @@ func init() {
 		if err != nil {
 			fmt.Println("web.LoadAppConfig Err:" + err.Error())
 		}
-
-		WxRelease()
-		WxReleaseCygx() // 查研观向小助手
 	}
 
 	config, err := web.AppConfig.GetSection(RunMode)
@@ -88,45 +117,68 @@ func init() {
 	LibreOfficePath = tmpLibreOfficePath
 
 	beeLogger.Log.Info(RunMode + " 模式")
-	if RunMode == "release" {
 
-	} else {
-		WxDebug()
+	// 系统类型
+	systemType, err := web.AppConfig.String("system_type")
+	if err != nil {
+		panic(any("配置文件读取system_type错误 " + err.Error()))
 	}
-}
+	SystemType = systemType
 
-// 测试环境模板消息
-func WxDebug() {
-	WxAppId = "wx9b5d7291e581233a"
-	WxAppSecret = "f4d52e34021eee262dce9682b31f8861"
-	WxId = "gh_5dc508325c6f"
+	// 项目中文名称
+	appNameCn, err := web.AppConfig.String("app_name_cn")
+	if err != nil {
+		panic(any("配置文件读取app_name_cn错误 " + err.Error()))
+	}
+	APPNAME = appNameCn
 
-	//内部员工公众号(弘则部门)
-	AdminWxAppId = "wx1392111da5426e9e"
-	AdminWxAppSecret = "30eceb7cf29bf2f046031155ab55d7b4"
+	// 初始化内部服务配置
 
-	WxAppList = make(map[string]string)
-	WxAppList[AdminWxAppId] = AdminWxAppSecret
-	WxAppList[WxAppId] = WxAppSecret
-}
+	// 报警服务地址
+	AlarmMsgUrl = config["alarm_msg_url"]
 
-// 生产环境模板消息
-func WxRelease() {
-	WxAppId = "wx4a844c734d8c8e56"
-	WxAppSecret = "26c586e7ccb3c575433f0f37797b3eeb"
-	WxId = "gh_b67e0049fb8c"
+	// 签名秘钥
+	Authorization = config["authorization"]
 
-	//内部员工公众号(弘则部门)
-	AdminWxAppId = "wx1392111da5426e9e"
-	AdminWxAppSecret = "30eceb7cf29bf2f046031155ab55d7b4"
+	// 邮件提醒人员
+	EmailSendToUsers = config["email_send_to_users"]
 
-	WxAppList = make(map[string]string)
-	WxAppList[AdminWxAppId] = AdminWxAppSecret
-	WxAppList[WxAppId] = WxAppSecret
-}
+	// 微信消息
+	{
+		WxAppId = config["wx_appid"]
+		WxAppSecret = config["wx_app_secret"]
+		WxId = config["wx_id"]
+
+		//内部员工公众号(弘则部门)
+		AdminWxAppId = config["admin_wx_appid"]
+		AdminWxAppSecret = config["admin_wx_app_secret"]
 
-// 查研观向生产环境模板消息
-func WxReleaseCygx() {
-	WxAppIdCygx = "wxb7cb8a15abad5b8e"
-	WxAppSecretCygx = "f425ba2863084249722af1e2a5cfffd3"
+		WxAppList = make(map[string]string)
+		if AdminWxAppId != "" && AdminWxAppSecret != "" {
+			WxAppList[AdminWxAppId] = AdminWxAppSecret
+		}
+		if WxAppId != "" && WxAppSecret != "" {
+			WxAppList[WxAppId] = WxAppSecret
+		}
+
+		WxAppIdCygx = config["wx_cygx_appid"]
+		WxAppSecretCygx = config["wx_cygx_app_secret"]
+
+		// 小程序相关
+		WxCrmAppId = config["wx_crm_miniprogram_appid"]
+		// WxCygxAppId 弘则研报小程序
+		WxYbAppId = config["wx_yb_miniprogram_appid"]
+		// WxCygxAppId 查研观向小程序
+		WxCygxAppId = config["wx_cygx_miniprogram_appid"]
+	}
+
+	// OSS相关
+	{
+		Endpoint = config["endpoint"]
+		Bucketname = config["bucket_name"]
+		Imghost = config["img_host"]
+		UploadDir = config["upload_dir"]
+		AccessKeyId = config["access_key_id"]
+		AccessKeySecret = config["access_key_secret"]
+	}
 }

+ 5 - 24
utils/constants.go

@@ -4,7 +4,7 @@ const (
 	Md5Key = "Ks@h64WJ#tcVgG8$&WlNfqvLAtMgpxWN"
 )
 
-//常量定义
+// 常量定义
 const (
 	FormatTime            = "15:04:05"                //时间格式
 	FormatDate            = "2006-01-02"              //日期格式
@@ -17,34 +17,15 @@ const (
 	PageSize10            = 10
 )
 
-const (
-	APPNAME          = "弘则-公共接口库"
-	EmailSendToUsers = "glji@hzinsights.com;pyan@hzinsights.com;cxzhang@hzinsights.com"
-)
-
-//手机号,电子邮箱正则
+// 手机号,电子邮箱正则
 const (
 	RegularMobile = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0-9])|(17[0-9])|(16[0-9])|(19[0-9]))\\d{8}$" //手机号码
 	RegularEmail  = `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*`                                             //匹配电子邮箱
 )
 
-var (
-	AccessKeyId     string = "LTAIFMZYQhS2BTvW"
-	AccessKeySecret string = "12kk1ptCHoGWedhBnKRVW5hRJzq9Fq"
-)
-
-const (
-	Authorization = "dc855fce962a639faa779cbdd4cd332f"
-)
-
-const (
-	WxCrmAppId = `wx67b68e39913e511e` //随手办公
-	WxYbAppId  = `wxb059c872d79b9967` //弘则研报小程序
-	WxCygxAppId  = `wxcc32b61f96720d2f` //查研观向小程序
-)
 // 缓存key
 const (
-	CACHE_WX_ACCESS_TOKEN_HZ         = "wx:accesstoken:hzyj"                  //弘则研究公众号 微信accessToken
-	HZ_ADMIN_WX_ACCESS_TOEKN         = "hz_admin:wx:access_token:"
-	CACHE_WX_ACCESS_TOKEN_CYGX         = "xygxxzs_wxtoken"                  //查研观向小助手公众号 微信accessToken
+	CACHE_WX_ACCESS_TOKEN_HZ   = "wx:accesstoken:hzyj" //弘则研究公众号 微信accessToken
+	HZ_ADMIN_WX_ACCESS_TOEKN   = "hz_admin:wx:access_token:"
+	CACHE_WX_ACCESS_TOKEN_CYGX = "xygxxzs_wxtoken" //查研观向小助手公众号 微信accessToken
 )