Commit 6142204c by zhengqiuyun86

初始化

parent dc7cf41f
package conf
type Config struct {
ENV string
ServerName string
ShowSql bool
LogLevel string
LogColorful bool
LogResponseLength int
}
func BaseConfig(conf Config) {
ENV = conf.ENV
ServerName = conf.ServerName
ShowSql = conf.ShowSql
LogLevel = conf.LogLevel
LogColorful = conf.LogColorful
LogResponseLength = conf.LogResponseLength
}
var ENV = "test"
var ServerName = "git.168cad.top/zhengqiuyun/rym-util"
//ShowSql 是否打印SQL
var ShowSql = true //是否打印SQL
var LogLevel = "Debug"
//LogColorful 日志是否开启彩色打印
var LogColorful = true
var LogResponseLength = 1000
...@@ -2,14 +2,14 @@ package exception ...@@ -2,14 +2,14 @@ package exception
import ( import (
"errors" "errors"
"git.168cad.top/zhengqiuyun/rym-util" "git.168cad.top/zhengqiuyun/rym-util/a/log"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"reflect" "reflect"
) )
func ThrowsErr(err error) { func ThrowsErr(err error) {
if err != nil { if err != nil {
rym_util.Error(err.Error()) log.Error(err.Error())
panic(err) panic(err)
} }
} }
...@@ -57,7 +57,7 @@ func (e *DError) Error() string { ...@@ -57,7 +57,7 @@ func (e *DError) Error() string {
} }
func ThrowsErrS(err string) { func ThrowsErrS(err string) {
if &err != nil && err != "" { if &err != nil && err != "" {
rym_util.Error(err) log.Error(err)
panic(DError{Err: err}) panic(DError{Err: err})
} }
} }
...@@ -2,7 +2,7 @@ package generate ...@@ -2,7 +2,7 @@ package generate
import ( import (
"fmt" "fmt"
"git.168cad.top/zhengqiuyun/rym-util" "git.168cad.top/zhengqiuyun/rym-util/a/conf"
"github.com/deckarep/golang-set" "github.com/deckarep/golang-set"
"github.com/iancoleman/strcase" "github.com/iancoleman/strcase"
"io" "io"
...@@ -49,7 +49,7 @@ func BuildPgModels() { ...@@ -49,7 +49,7 @@ func BuildPgModels() {
doTable, ok := DoTables[table.Name] doTable, ok := DoTables[table.Name]
if ok { if ok {
var tableFields []TableField var tableFields []TableField
if rym_util.DBType == "PostgreSql" { if conf.DBType == "PostgreSql" {
tableFields = GetTableFiledByTableName(table.Name) tableFields = GetTableFiledByTableName(table.Name)
} else { } else {
tableFields = MysqlGetTableFiledByTableName(table.Name) tableFields = MysqlGetTableFiledByTableName(table.Name)
...@@ -83,7 +83,7 @@ func getModelImport(field TableField) string { ...@@ -83,7 +83,7 @@ func getModelImport(field TableField) string {
} }
func getModelFiledType(field TableField) string { func getModelFiledType(field TableField) string {
if rym_util.DBType == "PostgreSql" { if conf.DBType == "PostgreSql" {
return DbToGoPG[field.FieldType] return DbToGoPG[field.FieldType]
} else { } else {
if strings.Index(field.ColumnType, "unsigned") > 0 { if strings.Index(field.ColumnType, "unsigned") > 0 {
...@@ -294,10 +294,10 @@ func generateDao(table *Table, fields []TableField, doTable TableInfo) { ...@@ -294,10 +294,10 @@ func generateDao(table *Table, fields []TableField, doTable TableInfo) {
pageQuery += "}\n" pageQuery += "}\n"
importStr := "import (\n" importStr := "import (\n"
importStr += "\t\"" + projectName + "/a/PagePlus\"\n" importStr += "\t\"" + projectName + "/a/PagePlus\"\n"
if rym_util.DBType == "" { if conf.DBType == "" {
importStr += "\t\"" + projectName + "/a/db\"\n" importStr += "\t\"" + projectName + "/a/db\"\n"
} else { } else {
importStr += "\t\"" + projectName + "/a/db/" + rym_util.DBType + "\"\n" importStr += "\t\"" + projectName + "/a/db/" + conf.DBType + "\"\n"
} }
importStr += "\t\"" + projectName + "/a/exception\"\n" importStr += "\t\"" + projectName + "/a/exception\"\n"
importStr += "\t\"" + projectName + "/a/page\"\n" importStr += "\t\"" + projectName + "/a/page\"\n"
......
...@@ -3,9 +3,9 @@ package getWay ...@@ -3,9 +3,9 @@ package getWay
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"git.168cad.top/zhengqiuyun/rym-util"
"git.168cad.top/zhengqiuyun/rym-util/a/conf" "git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util/a/db/redis" "git.168cad.top/zhengqiuyun/rym-util/a/db/redis"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"io/ioutil" "io/ioutil"
"net" "net"
...@@ -45,18 +45,18 @@ func GetAdminInfo(token string) Admin { ...@@ -45,18 +45,18 @@ func GetAdminInfo(token string) Admin {
var adminResponse = AdminResponse{} var adminResponse = AdminResponse{}
resp, err := http.Get(conf.SystemHost + "/admin/current?token=" + token) resp, err := http.Get(conf.SystemHost + "/admin/current?token=" + token)
if err != nil { if err != nil {
rym_util.Error(err.Error()) log.Error(err.Error())
return Admin{} return Admin{}
} }
defer resp.Body.Close() defer resp.Body.Close()
adminResponseBytes, err := ioutil.ReadAll(resp.Body) adminResponseBytes, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
rym_util.Error(err.Error()) log.Error(err.Error())
return Admin{} return Admin{}
} }
err = json.Unmarshal(adminResponseBytes, &adminResponse) err = json.Unmarshal(adminResponseBytes, &adminResponse)
if err != nil { if err != nil {
rym_util.Error(err.Error()) log.Error(err.Error())
} }
return adminResponse.Admin return adminResponse.Admin
} }
...@@ -145,13 +145,13 @@ func ValidRequest(c *gin.Context) { ...@@ -145,13 +145,13 @@ func ValidRequest(c *gin.Context) {
token := GetToken(c) token := GetToken(c)
if token == "" { if token == "" {
if checkIp(ip) { //如果没有token,看是否是ip白名单 if checkIp(ip) { //如果没有token,看是否是ip白名单
rym_util.Debug(fmt.Sprintf("IP白名单校验通过:%s", ip)) log.Debug(fmt.Sprintf("IP白名单校验通过:%s", ip))
} else { } else {
rym_util.Debug(fmt.Sprintf("IP白名单校验不通过:%s", ip)) log.Debug(fmt.Sprintf("IP白名单校验不通过:%s", ip))
if checkUri(url) { //如果不是ip白名单,看是否是url白名单,例如一些第三方的回调 if checkUri(url) { //如果不是ip白名单,看是否是url白名单,例如一些第三方的回调
rym_util.Debug(fmt.Sprintf("URL白名单校验通过:%s", url)) log.Debug(fmt.Sprintf("URL白名单校验通过:%s", url))
} else { } else {
rym_util.Debug(fmt.Sprintf("URL白名单校验不通过:%s", url)) log.Debug(fmt.Sprintf("URL白名单校验不通过:%s", url))
ThrowsGetWayErr(Illegal9) ThrowsGetWayErr(Illegal9)
} }
} }
...@@ -159,9 +159,9 @@ func ValidRequest(c *gin.Context) { ...@@ -159,9 +159,9 @@ func ValidRequest(c *gin.Context) {
if redis.Exists(token) { //校验token是否有效 if redis.Exists(token) { //校验token是否有效
currentUser := GetAdminInfo(token) currentUser := GetAdminInfo(token)
c.Set(CurrentUserKey, currentUser) c.Set(CurrentUserKey, currentUser)
rym_util.Debug(fmt.Sprintf("登陆校验通过:%s,对应用户id: %d", token, currentUser.Id)) log.Debug(fmt.Sprintf("登陆校验通过:%s,对应用户id: %d", token, currentUser.Id))
} else { } else {
rym_util.Debug(fmt.Sprintf("登陆校验不通过:%s", token)) log.Debug(fmt.Sprintf("登陆校验不通过:%s", token))
ThrowsGetWayErr(Expire) ThrowsGetWayErr(Expire)
} }
} }
...@@ -180,7 +180,7 @@ func exceptionGetWayCatch(c *gin.Context) { ...@@ -180,7 +180,7 @@ func exceptionGetWayCatch(c *gin.Context) {
} }
func checkUri(uri string) bool { func checkUri(uri string) bool {
for _, v := range rym_util.WhiteListUri { for _, v := range conf.WhiteListUri {
if strings.Index(uri, v) == 0 { if strings.Index(uri, v) == 0 {
return true return true
} }
...@@ -189,7 +189,7 @@ func checkUri(uri string) bool { ...@@ -189,7 +189,7 @@ func checkUri(uri string) bool {
} }
func checkIp(ip string) bool { func checkIp(ip string) bool {
for _, v := range rym_util.WhiteListIp { for _, v := range conf.WhiteListIp {
if strings.Index(ip, v) == 0 { if strings.Index(ip, v) == 0 {
return true return true
} }
...@@ -198,5 +198,5 @@ func checkIp(ip string) bool { ...@@ -198,5 +198,5 @@ func checkIp(ip string) bool {
} }
func logStart(c *gin.Context) { func logStart(c *gin.Context) {
rym_util.Info(fmt.Sprintf("开始 %s %s %s %s", GetMethod(c), GetURL(c), GetToken(c), GetClientIP(c))) log.Info(fmt.Sprintf("开始 %s %s %s %s", GetMethod(c), GetURL(c), GetToken(c), GetClientIP(c)))
} }
...@@ -3,11 +3,11 @@ package getWay ...@@ -3,11 +3,11 @@ package getWay
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"git.168cad.top/zhengqiuyun/rym-util" "git.168cad.top/zhengqiuyun/rym-util/a/conf"
db "git.168cad.top/zhengqiuyun/rym-util/a/db/mysql" db "git.168cad.top/zhengqiuyun/rym-util/a/db/mysql"
"git.168cad.top/zhengqiuyun/rym-util/a/db/redis" "git.168cad.top/zhengqiuyun/rym-util/a/db/redis"
"git.168cad.top/zhengqiuyun/rym-util/a/log"
"git.168cad.top/zhengqiuyun/rym-util/a/log/es" "git.168cad.top/zhengqiuyun/rym-util/a/log/es"
"git.168cad.top/zhengqiuyun/rym-util/a/util"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
"time" "time"
...@@ -76,7 +76,7 @@ func AliSuccess(c *gin.Context) { ...@@ -76,7 +76,7 @@ func AliSuccess(c *gin.Context) {
} }
func logEnd(c *gin.Context, data interface{}) { func logEnd(c *gin.Context, data interface{}) {
esSwitch := redis.SwitchOpen(redis.ES日志开关 + rym_util.ServerName) esSwitch := redis.SwitchOpen(redis.ES日志开关 + conf.ServerName)
startTime := GetStartTime(c) startTime := GetStartTime(c)
endTime := time.Now().UnixNano() endTime := time.Now().UnixNano()
var responseStr string var responseStr string
...@@ -85,14 +85,14 @@ func logEnd(c *gin.Context, data interface{}) { ...@@ -85,14 +85,14 @@ func logEnd(c *gin.Context, data interface{}) {
rs := string(dataJson) rs := string(dataJson)
excludeUrl := []string{""} excludeUrl := []string{""}
if esSwitch { if esSwitch {
if util.ArrayContains(excludeUrl, GetURL(c)) && len(rs) > rym_util.LogResponseLength { if util.ArrayContains(excludeUrl, GetURL(c)) && len(rs) > conf.LogResponseLength {
responseStr = rs[0:rym_util.LogResponseLength] + "......" responseStr = rs[0:conf.LogResponseLength] + "......"
} else { } else {
responseStr = rs responseStr = rs
} }
} else { } else {
if len(rs) > rym_util.LogResponseLength { if len(rs) > conf.LogResponseLength {
responseStr = rs[0:rym_util.LogResponseLength] + "......" responseStr = rs[0:conf.LogResponseLength] + "......"
} else { } else {
responseStr = rs responseStr = rs
} }
...@@ -101,18 +101,18 @@ func logEnd(c *gin.Context, data interface{}) { ...@@ -101,18 +101,18 @@ func logEnd(c *gin.Context, data interface{}) {
token := GetToken(c) token := GetToken(c)
if esSwitch && token != "" { if esSwitch && token != "" {
esLog(c, responseStr) esLog(c, responseStr)
rym_util.Info(fmt.Sprintf("结束,耗时%d毫秒", (endTime-startTime)/1e6)) log.Info(fmt.Sprintf("结束,耗时%d毫秒", (endTime-startTime)/1e6))
} else { } else {
rym_util.Info(fmt.Sprintf("返回数据:%s", responseStr)) log.Info(fmt.Sprintf("返回数据:%s", responseStr))
rym_util.Info(fmt.Sprintf("结束,耗时%d毫秒 %s %s %s %s %s", (endTime-startTime)/1e6, GetMethod(c), GetURL(c), token, GetClientIP(c), GetParams(c))) log.Info(fmt.Sprintf("结束,耗时%d毫秒 %s %s %s %s %s", (endTime-startTime)/1e6, GetMethod(c), GetURL(c), token, GetClientIP(c), GetParams(c)))
} }
} }
func esLog(c *gin.Context, response string) { func esLog(c *gin.Context, response string) {
log := es.ServerLogModel{ log := es.ServerLogModel{
ServerName: rym_util.ServerName, ServerName: conf.ServerName,
Env: rym_util.ENV, Env: conf.ENV,
TimeStamp: util.MilSecond(time.Now()), TimeStamp: util.MilSecond(time.Now()),
ThreadNo: fmt.Sprintf("%d", util.GetGID()), ThreadNo: fmt.Sprintf("%d", util.GetGID()),
ServerIp: GetServerIP(), ServerIp: GetServerIP(),
......
...@@ -7,10 +7,9 @@ import ( ...@@ -7,10 +7,9 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"git.168cad.top/zhengqiuyun/rym-util"
"git.168cad.top/zhengqiuyun/rym-util/a/conf" "git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util/a/exception" "git.168cad.top/zhengqiuyun/rym-util/a/exception"
"git.168cad.top/zhengqiuyun/rym-util/a/util" "git.168cad.top/zhengqiuyun/rym-util/a/log"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
...@@ -53,7 +52,7 @@ func (c *Client) doRequest(method string, param Param, result interface{}) (err ...@@ -53,7 +52,7 @@ func (c *Client) doRequest(method string, param Param, result interface{}) (err
requestStr = p.Encode() requestStr = p.Encode()
buf = strings.NewReader(requestStr) buf = strings.NewReader(requestStr)
} }
rym_util.Debug(fmt.Sprintf("请求参数:%s", requestStr)) log.Debug(fmt.Sprintf("请求参数:%s", requestStr))
req, err := http.NewRequest(method, conf.AllinpayUrl, buf) req, err := http.NewRequest(method, conf.AllinpayUrl, buf)
if err != nil { if err != nil {
return err return err
...@@ -70,7 +69,7 @@ func (c *Client) doRequest(method string, param Param, result interface{}) (err ...@@ -70,7 +69,7 @@ func (c *Client) doRequest(method string, param Param, result interface{}) (err
if err != nil { if err != nil {
return err return err
} }
rym_util.Debug(fmt.Sprintf("请求结果:%s", string(data))) log.Debug(fmt.Sprintf("请求结果:%s", string(data)))
ok, err := verifyData(data, c.tlPublicKey) ok, err := verifyData(data, c.tlPublicKey)
if err != nil { if err != nil {
return err return err
...@@ -145,7 +144,7 @@ func signWithPKCS1v15(param url.Values, privateKey *rsa.PrivateKey, hash crypto. ...@@ -145,7 +144,7 @@ func signWithPKCS1v15(param url.Values, privateKey *rsa.PrivateKey, hash crypto.
} }
sort.Strings(pList) sort.Strings(pList)
var src = strings.Join(pList, "&") var src = strings.Join(pList, "&")
rym_util.Debug(fmt.Sprintf("签名数据:%s", src)) log.Debug(fmt.Sprintf("签名数据:%s", src))
md5Str := md5S(src) md5Str := md5S(src)
sig, err := RSASignWithKey([]byte(md5Str), privateKey, hash) sig, err := RSASignWithKey([]byte(md5Str), privateKey, hash)
if err != nil { if err != nil {
...@@ -179,7 +178,7 @@ func (c *Client) getDefaultNotification(data map[string]string, sign string, res ...@@ -179,7 +178,7 @@ func (c *Client) getDefaultNotification(data map[string]string, sign string, res
} }
sort.Strings(pList) sort.Strings(pList)
var src = strings.Join(pList, "&") var src = strings.Join(pList, "&")
rym_util.Debug(fmt.Sprintf("签名数据:%s", src)) log.Debug(fmt.Sprintf("签名数据:%s", src))
md5Str := md5B([]byte(src)) md5Str := md5B([]byte(src))
signBytes, err := base64.StdEncoding.DecodeString(sign) signBytes, err := base64.StdEncoding.DecodeString(sign)
if err != nil { if err != nil {
......
...@@ -2,11 +2,10 @@ package main ...@@ -2,11 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"git.168cad.top/zhengqiuyun/rym-util"
"git.168cad.top/zhengqiuyun/rym-util/a/conf" "git.168cad.top/zhengqiuyun/rym-util/a/conf"
"git.168cad.top/zhengqiuyun/rym-util/a/exception" "git.168cad.top/zhengqiuyun/rym-util/a/exception"
lib_allinpay "git.168cad.top/zhengqiuyun/rym-util/a/lib/allinpay" lib_allinpay "git.168cad.top/zhengqiuyun/rym-util/a/lib/allinpay"
"git.168cad.top/zhengqiuyun/rym-util/a/util" "git.168cad.top/zhengqiuyun/rym-util/a/log"
"time" "time"
) )
...@@ -46,9 +45,9 @@ func testGetPayeeFundsInTransitDetail() { ...@@ -46,9 +45,9 @@ func testGetPayeeFundsInTransitDetail() {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess { if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%v", response.Data)) log.Info(fmt.Sprintf("%v", response.Data))
} else { } else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg)) log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
} }
} }
...@@ -69,9 +68,9 @@ func testQueryInExpDetail() { ...@@ -69,9 +68,9 @@ func testQueryInExpDetail() {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess { if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%v", response.Data)) log.Info(fmt.Sprintf("%v", response.Data))
} else { } else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg)) log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
} }
} }
...@@ -106,7 +105,7 @@ func testCreateMember() { ...@@ -106,7 +105,7 @@ func testCreateMember() {
if err != nil { if err != nil {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg)) log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
} }
func testBindWxOpenId() { func testBindWxOpenId() {
...@@ -123,7 +122,7 @@ func testBindWxOpenId() { ...@@ -123,7 +122,7 @@ func testBindWxOpenId() {
if err != nil { if err != nil {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg)) log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
} }
func testGetMemberInfo() { func testGetMemberInfo() {
...@@ -137,7 +136,7 @@ func testGetMemberInfo() { ...@@ -137,7 +136,7 @@ func testGetMemberInfo() {
if err != nil { if err != nil {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg)) log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
} }
func testConsumeApply() { func testConsumeApply() {
...@@ -176,9 +175,9 @@ func testConsumeApply() { ...@@ -176,9 +175,9 @@ func testConsumeApply() {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess { if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%s", response.Data.PayInfo)) log.Info(fmt.Sprintf("%s", response.Data.PayInfo))
} else { } else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg)) log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
} }
} }
...@@ -196,9 +195,9 @@ func testGetOrderDetail() { ...@@ -196,9 +195,9 @@ func testGetOrderDetail() {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess { if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%s", response.Data)) log.Info(fmt.Sprintf("%s", response.Data))
} else { } else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg)) log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
} }
} }
...@@ -214,9 +213,9 @@ func testQueryReserveFundBalance() { ...@@ -214,9 +213,9 @@ func testQueryReserveFundBalance() {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess { if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%s", response.Data)) log.Info(fmt.Sprintf("%s", response.Data))
} else { } else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg)) log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
} }
} }
...@@ -233,9 +232,9 @@ func testQueryBalance() { ...@@ -233,9 +232,9 @@ func testQueryBalance() {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess { if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%s", response.Data)) log.Info(fmt.Sprintf("%s", response.Data))
} else { } else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg)) log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
} }
} }
...@@ -251,9 +250,9 @@ func testQueryBankCard() { ...@@ -251,9 +250,9 @@ func testQueryBankCard() {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess { if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%s", response.Data)) log.Info(fmt.Sprintf("%s", response.Data))
} else { } else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg)) log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
} }
} }
...@@ -275,7 +274,7 @@ func testWithDrawApply() { ...@@ -275,7 +274,7 @@ func testWithDrawApply() {
p.BackUrl = conf.MainHost + "/pay/allin/pay/draw/notify" p.BackUrl = conf.MainHost + "/pay/allin/pay/draw/notify"
bankCardNo := "324348812323" bankCardNo := "324348812323"
encryptBankCardNo, err := lib_allinpay.AesEncryptECB(bankCardNo, conf.AllinpaySecretKey) encryptBankCardNo, err := lib_allinpay.AesEncryptECB(bankCardNo, conf.AllinpaySecretKey)
rym_util.Info(fmt.Sprintf("银行卡加密数据:%s", encryptBankCardNo)) log.Info(fmt.Sprintf("银行卡加密数据:%s", encryptBankCardNo))
if err != nil { if err != nil {
exception.ThrowsErrS("通联密钥错误,请检查配置") exception.ThrowsErrS("通联密钥错误,请检查配置")
} }
...@@ -291,9 +290,9 @@ func testWithDrawApply() { ...@@ -291,9 +290,9 @@ func testWithDrawApply() {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess { if response.Code == lib_allinpay.CodeSuccess && response.SubCode == lib_allinpay.SubCodeSuccess {
rym_util.Info(fmt.Sprintf("%s", response.Data)) log.Info(fmt.Sprintf("%s", response.Data))
} else { } else {
rym_util.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg)) log.Info(fmt.Sprintf("%s-%s", response.Msg, response.SubMsg))
} }
} }
...@@ -315,5 +314,5 @@ func testGetTradeNotification() { ...@@ -315,5 +314,5 @@ func testGetTradeNotification() {
if err != nil { if err != nil {
exception.ThrowsErr(err) exception.ThrowsErr(err)
} }
rym_util.Info(fmt.Sprintf("%v", rd)) log.Info(fmt.Sprintf("%v", rd))
} }
package log
import (
"bytes"
"fmt"
"git.168cad.top/zhengqiuyun/rym-util/a/conf"
"runtime"
"strconv"
"strings"
"time"
)
const (
DebugL = "Debug"
InfoL = "Info"
ErrorL = "Error"
)
// Colors
const (
Reset = "\033[0m"
Red = "\033[31m"
Green = "\033[32m"
Yellow = "\033[33m"
Blue = "\033[34m"
Magenta = "\033[35m"
Cyan = "\033[36m"
White = "\033[37m"
BlueBold = "\033[34;1m"
MagentaBold = "\033[35;1m"
RedBold = "\033[31;1m"
YellowBold = "\033[33;1m"
)
func Print(tag, msg string, file string, line int) {
str := "%s %s [%d] %s %d %s"
if conf.LogColorful {
if tag == "Error" {
str = "%s %s [%d] %s %d " + Red + "%s" + Reset
} else if tag == "Info" {
str = "%s %s [%d] %s %d " + Yellow + "%s" + Reset
} else {
str = "%s %s [%d] %s %d " + Green + "%s" + Reset
}
}
file = file[strings.LastIndex(file, "/")+1:]
fmt.Println(fmt.Sprintf(str, FormatDateMillTime(time.Now()), tag, GetGID(), file, line, msg))
}
func FormatDateMillTime(dataTime time.Time) string {
return dataTime.Format("2006-01-02 15:04:05.000000")
}
func GetGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}
package util package rym_util
func ArrayContains(array []string, e string) bool { func ArrayContains(array []string, e string) bool {
for _, v := range array { for _, v := range array {
......
package util package rym_util
import ( import (
"time" "time"
......
package util package rym_util
func IfInt(isTrue bool, a, b int) int { func IfInt(isTrue bool, a, b int) int {
if isTrue { if isTrue {
......
package util package rym_util
import ( import (
"encoding/base64" "encoding/base64"
......
package util package rym_util
import ( import (
"bytes" "bytes"
......
package util package rym_util
import ( import (
"encoding/json" "encoding/json"
......
package util package rym_util
import ( import (
"crypto/md5" "crypto/md5"
......
package util package rym_util
import ( import (
"os" "os"
......
package util package rym_util
func EncodePwd(loginPwd, loginPwdSalt string) string { func EncodePwd(loginPwd, loginPwdSalt string) string {
pingPwd := loginPwdSalt + loginPwd pingPwd := loginPwdSalt + loginPwd
......
package util package rym_util
import ( import (
"bytes" "bytes"
......
package util package rym_util
import ( import (
"encoding/json" "encoding/json"
......
package rym_util
type Config struct {
ENV string
}
func BaseConfig(conf Config) {
ENV = conf.ENV
}
var ENV = "test"
var ServerName = "git.168cad.top/zhengqiuyun/rym-util"
var DBType = "mysql"
var EsHttpHost = "http://192.168.1.124:9200"
//mysql
var DbDSN = "root:dcrym29210924..@tcp(192.168.1.37:3306)/duocaihui_dev?charset=utf8mb4&parseTime=True&loc=Local"
//ShowSql 是否打印SQL
var ShowSql = true //是否打印SQL
var LogLevel = "Debug"
var HttpServerPort = "8080"
var WhiteListIp = [...]string{"::1", "192.168.71.127", "171.88.67.133"}
var WhiteListUri = [...]string{"/swagger", "/api/app/login/get/captcha",
"/api/app/login/register/send/sms", "/api/app/login/register",
"/api/app/login/forget/pwd/send/sms", "/api/app/login/forget/pwd",
"/api/app/login/login",
"/api/app/wx/login/bind/check", "/api/app/wx/login/bind/send/sms",
"/api/app/wx/login/bind", "/api/app/wx/login", "/pay/wx/pay/notify"}
var CheckSms = false
var SmsSendUrl = "http://192.168.1.43:9001"
var SmsVerifyUrl = "http://192.168.1.43:9001"
//LogColorful 日志是否开启彩色打印
var LogColorful = true
var LogResponseLength = 1000
var CaptchaMax = 5
...@@ -3,17 +3,6 @@ module git.168cad.top/zhengqiuyun/rym-util ...@@ -3,17 +3,6 @@ module git.168cad.top/zhengqiuyun/rym-util
go 1.16 go 1.16
require ( require (
github.com/deckarep/golang-set v1.7.1
github.com/garyburd/redigo v1.6.3
github.com/gin-gonic/gin v1.7.4
github.com/go-playground/validator/v10 v10.4.1
github.com/iancoleman/strcase v0.2.0
github.com/shopspring/decimal v1.2.0
github.com/wechatpay-apiv3/wechatpay-go v0.2.9
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
gorm.io/driver/mysql v1.2.2
gorm.io/driver/postgres v1.2.2
gorm.io/gorm v1.22.4
) )
require ( require (
...@@ -22,13 +11,8 @@ require ( ...@@ -22,13 +11,8 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/olivere/elastic/v7 v7.0.32
github.com/robfig/cron v1.2.0
github.com/smartwalle/crypto4go v1.0.3 github.com/smartwalle/crypto4go v1.0.3
github.com/wenlng/go-captcha v1.2.5
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
) )
replace git.168cad.top/zhengqiuyun/rym-util => ../rym-util
package rym_util package rym_util
import ( import (
"bytes" "git.168cad.top/zhengqiuyun/rym-util/a/conf"
"fmt" "git.168cad.top/zhengqiuyun/rym-util/a/log"
"gorm.io/gorm/logger"
"runtime" "runtime"
"strconv"
"strings" "strings"
"time"
)
const (
DebugL = "Debug"
InfoL = "Info"
ErrorL = "Error"
) )
func DebugDo(f func()) { func DebugDo(f func()) {
...@@ -24,8 +15,8 @@ func DebugDo(f func()) { ...@@ -24,8 +15,8 @@ func DebugDo(f func()) {
func IsDebug() bool { func IsDebug() bool {
ok := false ok := false
switch strings.ToLower(LogLevel) { switch strings.ToLower(conf.LogLevel) {
case strings.ToLower(DebugL): case strings.ToLower(log.DebugL):
ok = true ok = true
break break
} }
...@@ -34,76 +25,48 @@ func IsDebug() bool { ...@@ -34,76 +25,48 @@ func IsDebug() bool {
func Debug(msg string) { func Debug(msg string) {
ok := false ok := false
switch strings.ToLower(LogLevel) { switch strings.ToLower(conf.LogLevel) {
case strings.ToLower(DebugL): case strings.ToLower(log.DebugL):
ok = true ok = true
break break
} }
if ok { if ok {
_, file, line, _ := runtime.Caller(1) _, file, line, _ := runtime.Caller(1)
mark("Debug", msg, file, line) log.Print("Debug", msg, file, line)
} }
} }
func Info(msg string) { func Info(msg string) {
ok := false ok := false
switch strings.ToLower(LogLevel) { switch strings.ToLower(conf.LogLevel) {
case strings.ToLower(DebugL): case strings.ToLower(log.DebugL):
ok = true ok = true
break break
case strings.ToLower(InfoL): case strings.ToLower(log.InfoL):
ok = true ok = true
break break
} }
if ok { if ok {
_, file, line, _ := runtime.Caller(1) _, file, line, _ := runtime.Caller(1)
mark("Info", msg, file, line) log.Print("Info", msg, file, line)
} }
} }
func Error(msg string) { func Error(msg string) {
ok := false ok := false
switch strings.ToLower(LogLevel) { switch strings.ToLower(conf.LogLevel) {
case strings.ToLower(DebugL): case strings.ToLower(log.DebugL):
ok = true ok = true
break break
case strings.ToLower(InfoL): case strings.ToLower(log.InfoL):
ok = true ok = true
break break
case strings.ToLower(ErrorL): case strings.ToLower(log.ErrorL):
ok = true ok = true
break break
} }
if ok { if ok {
_, file, line, _ := runtime.Caller(1) _, file, line, _ := runtime.Caller(1)
mark("Error", msg, file, line) log.Print("Error", msg, file, line)
}
}
func mark(tag, msg string, file string, line int) {
str := "%s %s [%d] %s %d %s"
if LogColorful {
if tag == "Error" {
str = "%s %s [%d] %s %d " + logger.Red + "%s" + logger.Reset
} else if tag == "Info" {
str = "%s %s [%d] %s %d " + logger.Yellow + "%s" + logger.Reset
} else {
str = "%s %s [%d] %s %d " + logger.Green + "%s" + logger.Reset
}
} }
file = file[strings.LastIndex(file, "/")+1:]
fmt.Println(fmt.Sprintf(str, FormatDateMillTime(time.Now()), tag, GetGID(), file, line, msg))
}
func FormatDateMillTime(dataTime time.Time) string {
return dataTime.Format("2006-01-02 15:04:05.000000")
}
func GetGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment