Skip to content

Commit 2f25cd9

Browse files
committedApr 30, 2019
init the project
0 parents  commit 2f25cd9

13 files changed

+996
-0
lines changed
 

‎config/config-test.json

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"redis": {
3+
"host": "xxxx",
4+
"port": "xxxx",
5+
"db": 0,
6+
"pwd": "xxxx"
7+
},
8+
"psql": {
9+
"host": "xxxxx",
10+
"port": "xxxx",
11+
"db": "xxxx",
12+
"pwd": "xxxx",
13+
"user": "xxxx",
14+
"sslmode": "disable",
15+
"isShow": true
16+
},
17+
"env": {
18+
"env": "dev",
19+
"port": ":9099",
20+
"host": "http://localhost"
21+
},
22+
"jwt":{
23+
"pwd": ""
24+
}
25+
}

‎config/global_variable.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* @Description: System startup and parameter configuration global variables for third party services
3+
* @Author: Young (hao_youngg@163.com)
4+
* @LastEditors: Young (hao_youngg@163.com)
5+
* @Date: 2019-04-30 11:41:33
6+
* @LastEditTime: 2019-04-30 16:32:30
7+
*/
8+
package config
9+
10+
import "time"
11+
12+
var (
13+
Conf *Config // System configuration config parameter
14+
15+
SYS_TIME_LOCATION, _ = time.LoadLocation("Asia/Chongqing") // China's time zone
16+
)
17+
18+
// Time format string
19+
const (
20+
SYS_TIME_YMDHMS string = "2006-01-02 15:04:05" // Format time YYYY:MM:DD HH:mm:ss
21+
SYS_TIME_YMDHM string = "2006-01-02 15:04" // Format time YYYY:MM:DD HH:mm
22+
SYS_TIME_YMD string = "2006-01-02" // Format time YYYY:MM:DD
23+
)
24+
25+
const (
26+
SUCC_CODE = 200 + iota // Successful network request without any errors
27+
ERROR_CODE_CAN_NOT_SKIP // Successful network request with some problems
28+
BACK_TO_LOGIN // A successful network request was made, but the user information is invalid and you need to login again
29+
)

‎config/sys_config.go

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* @Description: System parameters initialize the configuration file
3+
* @Author: Young (hao_youngg@163.com)
4+
* @LastEditors: Young (hao_youngg@163.com)
5+
* @Date: 2019-04-30 11:35:20
6+
* @LastEditTime: 2019-04-30 16:43:06
7+
*/
8+
package config
9+
10+
import (
11+
"base_core/log"
12+
"encoding/json"
13+
"fmt"
14+
"io/ioutil"
15+
"os"
16+
"path/filepath"
17+
)
18+
19+
type Config struct {
20+
Redis RedisConfig // Redis配置
21+
Psql PsqlConfig // PostgreSQL配置
22+
Env EnvConfig // 系统启动环境配置
23+
Jwt JwtConfig // JWT 密码配置
24+
}
25+
26+
type PsqlConfig struct {
27+
Host string `json:"host"`
28+
Port string `json:"port"`
29+
DB string `json:"db"`
30+
User string `json:"user"`
31+
Pwd string `json:"pwd"`
32+
Sslmode string `json:"sslmode"`
33+
IsShow bool `json:"isShow"`
34+
}
35+
type RedisConfig struct {
36+
Host string `json:"host"`
37+
Port string `json:"port"`
38+
Db int32 `json:"db"`
39+
Pwd string `json:"pwd"`
40+
}
41+
42+
type EnvConfig struct {
43+
Env string `json:"env"`
44+
Port string `json:"port"`
45+
Host string `json:"host"`
46+
}
47+
48+
type JwtConfig struct {
49+
Pwd []byte `json:"pwd"`
50+
}
51+
52+
func ConfInit(path string) {
53+
Conf = NewConfig(path)
54+
}
55+
56+
func NewConfig(config string) *Config {
57+
absPath, _ := filepath.Abs(config)
58+
config_str, err := ioutil.ReadFile(absPath)
59+
if err != nil {
60+
log.DaoLogger.Error("NewConfig: ", err)
61+
}
62+
63+
config_obj := Config{}
64+
e := json.Unmarshal(config_str, &config_obj)
65+
if e != nil {
66+
fmt.Println(e.Error())
67+
fmt.Println("配置文件 json 解析失败")
68+
os.Exit(0)
69+
}
70+
71+
return &config_obj
72+
}

‎connect/db.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
建立Psql数据库连接
3+
*/
4+
package connect
5+
6+
import (
7+
"fmt"
8+
conf "mcc-core/config"
9+
"mcc-core/log"
10+
"os"
11+
"sync"
12+
13+
"github.com/go-xorm/xorm"
14+
_ "github.com/lib/pq"
15+
)
16+
17+
var (
18+
ViewPath string
19+
StaticPath string
20+
)
21+
22+
var (
23+
PsqlEngine *xorm.Engine
24+
onePsql sync.Once
25+
)
26+
27+
func PsqlInit() {
28+
NewPsql(&conf.Conf.Psql)
29+
}
30+
31+
// 数据库连接,单例
32+
func NewPsql(config *conf.PsqlConfig) {
33+
onePsql.Do(func() {
34+
log.InfoF("connect to psql %s:%s",
35+
config.Host, config.Port)
36+
37+
driveSource := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
38+
config.Host, config.Port, config.User, config.Pwd, config.DB, config.Sslmode)
39+
40+
engine, err := xorm.NewEngine("postgres", driveSource)
41+
if err != nil {
42+
log.Info("psql connect error", err)
43+
os.Exit(0)
44+
}
45+
46+
// Debug模式,打印全部的SQL语句,帮助对比,看ORM与SQL执行的对照关系
47+
engine.ShowSQL(conf.Conf.Psql.IsShowSQL)
48+
engine.SetTZLocation(conf.SysTimeChina)
49+
50+
//设置连接数
51+
engine.SetMaxIdleConns(20)
52+
engine.SetMaxOpenConns(100)
53+
54+
// 性能优化的时候才考虑,加上本机的SQL缓存
55+
// cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
56+
// engine.SetDefaultCacher(cacher)
57+
58+
err = engine.Ping()
59+
if err != nil {
60+
log.Crit(err.Error())
61+
os.Exit(0)
62+
} else {
63+
log.Info("psql had connected")
64+
}
65+
PsqlEngine = engine
66+
},
67+
)
68+
}

‎connect/redis.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* @Description:
3+
* @Author: Young (hao_youngg@163.com)
4+
* @LastEditors: Young (hao_youngg@163.com)
5+
* @Date: 2019-04-30 13:39:06
6+
* @LastEditTime: 2019-04-30 13:53:31
7+
*/
8+
/*
9+
建立reids数据库连接
10+
*/
11+
package connect
12+
13+
import (
14+
"fmt"
15+
"base_core/config"
16+
"base_core/log"
17+
18+
"sync"
19+
20+
"github.com/go-redis/redis"
21+
)
22+
23+
var (
24+
RedisEngine *redis.Client
25+
oneRedis sync.Once
26+
27+
DbRedis2 *redis.Client
28+
DbRedis5 *redis.Client
29+
DbRedis9 *redis.Client
30+
)
31+
32+
func RedisInit() {
33+
NewRedis(&config.Conf.Redis)
34+
}
35+
36+
// 单例子模式
37+
func NewRedis(conf *config.RedisConfig) {
38+
oneRedis.Do(func() {
39+
log.InfoF("connect to redis %s:%s", conf.Host, conf.Port)
40+
redisEngine := redis.NewClient(&redis.Options{Addr: fmt.Sprintf("%s:%s", conf.Host, conf.Port), Password: conf.Pwd, DB: int(conf.Db), PoolSize: 20})
41+
if redisEngine != nil {
42+
log.Info("redis had connected")
43+
} else {
44+
log.Crit("redis start fail")
45+
}
46+
RedisEngine = redisEngine
47+
},
48+
)
49+
}
50+
51+
// 连接指定的db库
52+
func NewRedisByDB(dbNum int) *redis.Client {
53+
c := &config.Conf.Redis
54+
log.InfoF("connect to redis %s:%s %d", c.Host, c.Port, dbNum)
55+
redisEngine := redis.NewClient(&redis.Options{Addr: fmt.Sprintf("%s:%s", c.Host, c.Port), Password: c.Pwd, DB: dbNum, PoolSize: 20})
56+
if redisEngine != nil {
57+
log.Info("redis had connected")
58+
} else {
59+
log.Crit("redis start fail")
60+
}
61+
return redisEngine
62+
}

‎dao/test_dao.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @Description:
3+
* @Author: Young (hao_youngg@163.com)
4+
* @LastEditors: Young (hao_youngg@163.com)
5+
* @Date: 2019-04-30 14:31:35
6+
* @LastEditTime: 2019-04-30 15:42:58
7+
*/
8+
package dao
9+
10+
import (
11+
"base_core/data"
12+
13+
"github.com/go-xorm/xorm"
14+
)
15+
16+
type TestDao struct {
17+
engine *xorm.Engine
18+
sess *xorm.Session
19+
}
20+
21+
func NewTestDao(engine *xorm.Engine) *TestDao {
22+
return &TestDao{
23+
engine: engine,
24+
sess: engine.NewSession(),
25+
}
26+
}
27+
28+
/**
29+
* @Description: This Dao For Test
30+
* @Author: Young (hao_youngg@163.com)
31+
* @LastEditors: Young (hao_youngg@163.com)
32+
* @param {
33+
data.TestDaoInData
34+
}
35+
* @return: {
36+
data.TestDaoOutData
37+
}
38+
* @Date: 2019-04-30 14:44:08
39+
*/
40+
func (d *TestDao) TestDao(inputdata *data.TestDaoInputData) *data.TestDaoOutputData {
41+
// Data processing
42+
outdata := new(data.TestDaoOutputData)
43+
outdata.TestDaoOutputStr = inputdata.TestDaoInputStr + " Dao Data Output ->"
44+
return outdata
45+
}

‎data/request.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @Description: Input data structure for controller and dao
3+
* @Author: Young (hao_youngg@163.com)
4+
* @LastEditors: Young (hao_youngg@163.com)
5+
* @Date: 2019-04-30 14:33:51
6+
* @LastEditTime: 2019-04-30 15:33:37
7+
*/
8+
package data
9+
10+
type TestControllerInputData struct {
11+
TestControllerInputstr string
12+
}
13+
14+
type TestDaoInputData struct {
15+
TestDaoInputStr string
16+
}

‎data/response.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @Description: Output data structure for dao and services
3+
* @Author: Young (hao_youngg@163.com)
4+
* @LastEditors: Young (hao_youngg@163.com)
5+
* @Date: 2019-04-30 14:33:51
6+
* @LastEditTime: 2019-04-30 15:44:36
7+
*/
8+
package data
9+
10+
type TestControllerOutputData struct {
11+
TestControllerOutputStr string
12+
}
13+
14+
type TestDaoOutputData struct {
15+
TestDaoOutputStr string
16+
}

‎log/logger.go

+424
Large diffs are not rendered by default.

‎services/test_service.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @Description:
3+
* @Author: Young (hao_youngg@163.com)
4+
* @LastEditors: Young (hao_youngg@163.com)
5+
* @Date: 2019-04-30 14:57:49
6+
* @LastEditTime: 2019-04-30 15:50:45
7+
*/
8+
package services
9+
10+
import (
11+
"base_core/connect"
12+
"base_core/dao"
13+
"base_core/data"
14+
)
15+
16+
type TestService interface {
17+
TestServices(inputSerdata *data.TestControllerInputData) *data.TestControllerOutputData
18+
}
19+
20+
type testService struct {
21+
testDao *dao.TestDao
22+
}
23+
24+
func NewTestService() TestService {
25+
return &testService{
26+
testDao: dao.NewTestDao(connect.PsqlEngine),
27+
}
28+
}
29+
30+
/**
31+
* @Description:
32+
* @Author: Young (hao_youngg@163.com)
33+
* @LastEditors: Young (hao_youngg@163.com)
34+
* @param {
35+
serdata.TestServiceInputData
36+
}
37+
* @return: {
38+
serdata.TestServiceOutputData
39+
}
40+
* @Date: 2019-04-30 15:12:14
41+
*/
42+
func (s *testService) TestServices(inputdata *data.TestControllerInputData) *data.TestControllerOutputData {
43+
// Logical processing
44+
45+
// dao data input
46+
inputDaodata := new(data.TestDaoInputData)
47+
48+
// controller data output
49+
outputCondata := new(data.TestControllerOutputData)
50+
51+
// controller data handle for dao data
52+
inputDaodata.TestDaoInputStr = inputdata.TestControllerInputstr + " Service Data Input ->"
53+
54+
// dao processing
55+
outputDaodata := s.testDao.TestDao(inputDaodata)
56+
57+
// service processing
58+
outputCondata.TestControllerOutputStr = outputDaodata.TestDaoOutputStr + " Service Data Output ->"
59+
60+
return outputCondata
61+
}

‎utils/color_fmt.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
//带颜色输出fmt方法 backColor:背景颜色 fontColor:字体颜色 fontStyle:字体样式 content:内容
8+
func ColorFmt(backColor string, fontColor string, fontStyle string, Enter bool, content ...interface{}) {
9+
var backColorInt int
10+
var fontColorInt int
11+
var fontStyleInt int
12+
13+
switch fontStyle {
14+
case "默认":
15+
backColorInt = 0
16+
case "高亮":
17+
backColorInt = 1
18+
case "下划线":
19+
backColorInt = 4
20+
case "闪烁":
21+
backColorInt = 5
22+
case "反白":
23+
backColorInt = 7
24+
case "不可见":
25+
backColorInt = 8
26+
}
27+
28+
switch backColor {
29+
case "黑色":
30+
backColorInt = 40
31+
case "红色":
32+
backColorInt = 41
33+
case "绿色":
34+
backColorInt = 42
35+
case "黄色":
36+
backColorInt = 43
37+
case "蓝色":
38+
backColorInt = 44
39+
case "紫红色":
40+
backColorInt = 45
41+
case "青蓝色":
42+
backColorInt = 46
43+
case "白色":
44+
backColorInt = 47
45+
}
46+
47+
switch fontColor {
48+
case "黑色":
49+
fontColorInt = 30
50+
case "红色":
51+
fontColorInt = 31
52+
case "绿色":
53+
fontColorInt = 32
54+
case "黄色":
55+
fontColorInt = 33
56+
case "蓝色":
57+
fontColorInt = 34
58+
case "紫红色":
59+
fontColorInt = 35
60+
case "青蓝色":
61+
fontColorInt = 36
62+
case "白色":
63+
fontColorInt = 37
64+
}
65+
if Enter == true {
66+
fmt.Printf("%c[%d;%d;%dm%s%c[0m\n", 0x1B, fontStyleInt, backColorInt, fontColorInt, content, 0x1B)
67+
} else {
68+
fmt.Printf("%c[%d;%d;%dm%s%c[0m", 0x1B, fontStyleInt, backColorInt, fontColorInt, content, 0x1B)
69+
}
70+
}

‎utils/jwt.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @Description:
3+
* @Author: Young (hao_youngg@163.com)
4+
* @LastEditors: Young (hao_youngg@163.com)
5+
* @Date: 2019-04-30 16:44:03
6+
* @LastEditTime: 2019-04-30 16:45:32
7+
*/
8+
package utils
9+
10+
import (
11+
"base_core/config"
12+
"fmt"
13+
"time"
14+
15+
jwt "github.com/dgrijalva/jwt-go"
16+
)
17+
18+
func UnixToTime(timestamp int64) time.Time {
19+
return time.Unix(timestamp, 0).In(config.SYS_TIME_LOCATION)
20+
}
21+
22+
func AuthToken(tokenString string, Secret []byte) (jwt.MapClaims, error) {
23+
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
24+
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
25+
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
26+
}
27+
return Secret, nil
28+
})
29+
30+
// TODO: TOKEN失效时间延长
31+
tokencheck := token.Claims.(jwt.MapClaims)
32+
authtime := UnixToTime(int64(tokencheck["Timeout"].(float64)))
33+
out, _ := time.ParseDuration("2h")
34+
authtime = authtime.Add(out)
35+
fmt.Println(authtime.After(time.Now()))
36+
if authtime.After(time.Now()) == false {
37+
return nil, err
38+
}
39+
return token.Claims.(jwt.MapClaims), err
40+
}

‎utils/key.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package utils
2+
3+
import "strconv"
4+
5+
var (
6+
KeyType = map[string]string{
7+
"USER_REGISTER_KEY": "user_register:",
8+
"USER_REGISTER_TIME_KEY": "user_register:",
9+
"USER_LOGIN_KEY": "login_user_",
10+
"USER_MODIFY_PAYPASSWORD_KEY": "user_modify_paypassword:",
11+
"USER_MODIFY_PAYPASSWORD_TIME_KEY": "user_modify_time_paypassword:",
12+
"USER_CHANGE_ACCOUNT_KEY": "user_change_account_auth_code:",
13+
"USER_CHANGE_ACCOUNT_TIME_KEY": "user_change_account_auth_time_code:",
14+
"USER_FOGOT_PASSWORD_KEY": "user_fogot_password_auth_code:",
15+
"USER_FOGET_PASSWORD_TIME_KEY": "user_fogot_password_auth_time_code:",
16+
"Resource_TreeGrid4_Menu": "ResourceTreeGrid4Menu",
17+
"Resource_TreeGrid4_Action": "ResourceTreeGrid4Action",
18+
"SERVER_LOGIN": "server_login_",
19+
"CEC_TRANS_PUSH_NOMAL_KEY": "",
20+
"VIP_SEARCH_KEY": "vip_parent_id:",
21+
"CEC_TRANS_NOT_PARESE_KEY": "-notparse",
22+
}
23+
)
24+
25+
type KeyData struct {
26+
PairName string
27+
Mobile string
28+
UserId int
29+
Num int
30+
}
31+
32+
func (k *KeyData) GetKey(KeyName string) string {
33+
switch KeyName {
34+
case "USER_REGISTER_KEY":
35+
return KeyType[KeyName] + k.Mobile
36+
case "USER_REGISTER_TIME_KEY":
37+
return KeyType[KeyName] + k.Mobile
38+
case "USER_FOGOT_PASSWORD_KEY":
39+
return KeyType[KeyName] + k.Mobile
40+
case "USER_FOGET_PASSWORD_TIME_KEY":
41+
return KeyType[KeyName] + k.Mobile
42+
case "USER_LOGIN_KEY":
43+
return KeyType[KeyName] + strconv.Itoa(k.UserId)
44+
case "USER_FOGET_PWD_KEY":
45+
return KeyType[KeyName] + k.Mobile
46+
case "Resource_TreeGrid4_Menu":
47+
return KeyType[KeyName] + "_" + strconv.Itoa(k.UserId) + "_" + strconv.Itoa(k.Num)
48+
case "Resource_TreeGrid4_Action":
49+
return KeyType[KeyName] + "_" + strconv.Itoa(k.UserId) + "_" + strconv.Itoa(k.Num)
50+
case "SERVER_LOGIN":
51+
return KeyType[KeyName] + strconv.Itoa(k.UserId)
52+
case "USER_CHANGE_ACCOUNT_KEY":
53+
return KeyType[KeyName] + k.Mobile
54+
case "USER_CHANGE_ACCOUNT_TIME_KEY":
55+
return KeyType[KeyName] + k.Mobile
56+
case "USER_MODIFY_PAYPASSWORD_KEY":
57+
return KeyType[KeyName] + k.Mobile
58+
case "USER_MODIFY_PAYPASSWORD_TIME_KEY":
59+
return KeyType[KeyName] + k.Mobile
60+
case "CEC_TRANS_PUSH_NOMAL_KEY":
61+
return k.PairName
62+
case "VIP_SEARCH_KEY":
63+
return KeyType[KeyName] + strconv.Itoa(k.UserId)
64+
case "CEC_TRANS_NOT_PARESE_KEY":
65+
return k.PairName + KeyType[KeyName]
66+
}
67+
return ""
68+
}

0 commit comments

Comments
 (0)
Please sign in to comment.