new
This commit is contained in:
52
service/framework/config/config.go
Normal file
52
service/framework/config/config.go
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Config
|
||||
|
||||
import "time"
|
||||
|
||||
var Get = &config{}
|
||||
|
||||
type config struct {
|
||||
Service service `json:"service"`
|
||||
Database database `json:"database"`
|
||||
Hash hash `json:"hash"`
|
||||
}
|
||||
|
||||
type service struct {
|
||||
Mode string `json:"mode"`
|
||||
HttpPort int `json:"http_port"`
|
||||
ReadTimeout time.Duration `json:"read_timeout"`
|
||||
WriteTimeout time.Duration `json:"write_timeout"`
|
||||
}
|
||||
|
||||
type database struct {
|
||||
Type string `json:"type"`
|
||||
User string `json:"user"`
|
||||
Password string `json:"password"`
|
||||
Host string `json:"host"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type hash struct {
|
||||
Salt string `json:"salt"`
|
||||
}
|
||||
|
||||
func Setup() {
|
||||
Get.Service.Mode = "debug"
|
||||
Get.Service.HttpPort = 7100
|
||||
Get.Service.ReadTimeout = 60 * time.Second
|
||||
Get.Service.WriteTimeout = 60 * time.Second
|
||||
|
||||
Get.Database.Name = "mir2"
|
||||
Get.Database.Type = "mysql"
|
||||
Get.Database.Host = "127.0.0.1"
|
||||
Get.Database.User = "root"
|
||||
Get.Database.Password = "88888888"
|
||||
|
||||
Get.Hash.Salt = "godot_$@#game_@$salt_$@$server%#^#%@%#"
|
||||
}
|
||||
37
service/framework/controller/disktop/check.go
Normal file
37
service/framework/controller/disktop/check.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package DisktopController
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
"Service/framework/database/game_account_data"
|
||||
"Service/framework/utils"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type responseCheckIndex struct {
|
||||
Account GameAccountData.Return `json:"account"`
|
||||
}
|
||||
|
||||
func CheckIndex(c *gin.Context) {
|
||||
|
||||
returnData := responseCheckIndex{}
|
||||
|
||||
referer, uid := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
if uid > 0 {
|
||||
accountDatabase := Database.New(GameAccountData.TableName)
|
||||
accountData := GameAccountData.Data{}
|
||||
accountWhere := fmt.Sprintf("account_id = %d", uid)
|
||||
err := accountDatabase.GetData(&accountData, accountWhere, "account_id DESC")
|
||||
if err == nil {
|
||||
returnData.Account = GameAccountData.ReturnData(&accountData)
|
||||
}
|
||||
}
|
||||
|
||||
Utils.Success(c, returnData)
|
||||
return
|
||||
}
|
||||
188
service/framework/controller/disktop/index.go
Normal file
188
service/framework/controller/disktop/index.go
Normal file
@@ -0,0 +1,188 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package DisktopController
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
"Service/framework/database/game_account_data"
|
||||
"Service/framework/database/game_code_data"
|
||||
"Service/framework/utils"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func MailCode(c *gin.Context) {
|
||||
|
||||
referer, _ := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
mail := c.DefaultQuery("mail", "")
|
||||
if mail == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
randNumber := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
code := fmt.Sprintf("%06v", randNumber.Int31n(1000000))
|
||||
|
||||
codeDatabase := Database.New(GameCodeData.TableName)
|
||||
codeData := &GameCodeData.Data{}
|
||||
codeData.CodeMail = mail
|
||||
codeData.CodePhone = ""
|
||||
codeData.CodeContent = code
|
||||
err := codeDatabase.CreateData(codeData)
|
||||
if err != nil {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
send := Utils.SendMail(mail, "注册/登录验证码:"+codeData.CodeContent, "你的账号注册/登录验证码为:"+codeData.CodeContent)
|
||||
if !send {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
Utils.Success(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
type responseMailLogin struct {
|
||||
Account GameAccountData.Return `json:"account"`
|
||||
LoginToken string `json:"token"`
|
||||
}
|
||||
|
||||
func MailLogin(c *gin.Context) {
|
||||
|
||||
returnData := responseMailLogin{}
|
||||
|
||||
referer, _ := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
email := c.DefaultQuery("email", "")
|
||||
if email == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
password := c.DefaultQuery("password", "")
|
||||
if password == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
accountDatabase := Database.New(GameAccountData.TableName)
|
||||
accountData := GameAccountData.Data{}
|
||||
accountWhere := fmt.Sprintf("account_mail = %q AND account_password = %q AND account_group = %d", email, Utils.EncryptMD5(password), 10)
|
||||
err := accountDatabase.GetData(&accountData, accountWhere, "account_id DESC")
|
||||
if err != nil {
|
||||
Utils.Warning(c, 10000, "20006", Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
returnData.Account = GameAccountData.ReturnData(&accountData)
|
||||
returnData.LoginToken = Utils.EncodeId(128, accountData.AccountId)
|
||||
|
||||
Utils.Success(c, returnData)
|
||||
return
|
||||
}
|
||||
|
||||
type responseMailRegister struct {
|
||||
Account GameAccountData.Return `json:"account"`
|
||||
LoginToken string `json:"token"`
|
||||
}
|
||||
|
||||
func MailRegister(c *gin.Context) {
|
||||
|
||||
returnData := responseMailRegister{}
|
||||
|
||||
referer, _ := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
email := c.DefaultQuery("email", "")
|
||||
if email == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
code := c.DefaultQuery("code", "")
|
||||
if code == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
password := c.DefaultQuery("password", "")
|
||||
if password == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
codeDatabase := Database.New(GameCodeData.TableName)
|
||||
codeData := GameCodeData.Data{}
|
||||
codeWhere := fmt.Sprintf("code_mail = %q AND code_content = %q", email, code)
|
||||
err := codeDatabase.GetData(&codeData, codeWhere, "code_id DESC")
|
||||
if err != nil {
|
||||
Utils.Warning(c, 10000, "20007", Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
accountDatabase := Database.New(GameAccountData.TableName)
|
||||
accountData := GameAccountData.Data{}
|
||||
accountWhere := fmt.Sprintf("account_mail = %q", email)
|
||||
err = accountDatabase.GetData(&accountData, accountWhere, "account_id DESC")
|
||||
if err == nil {
|
||||
Utils.Warning(c, 10000, "20008", Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
AccountGroup := 1
|
||||
countWhere := fmt.Sprintf("account_status > %d", 0)
|
||||
count, _ := accountDatabase.CountData(countWhere)
|
||||
if count == 0 {
|
||||
AccountGroup = 10
|
||||
}
|
||||
|
||||
setData := &GameAccountData.Data{}
|
||||
setData.AccountMail = email
|
||||
setData.AccountPassword = Utils.EncryptMD5(password)
|
||||
setData.AccountName = ""
|
||||
setData.AccountNumber = ""
|
||||
setData.AccountQuestionA = ""
|
||||
setData.AccountAnswerA = ""
|
||||
setData.AccountQuestionB = ""
|
||||
setData.AccountAnswerB = ""
|
||||
setData.AccountGroup = AccountGroup
|
||||
setData.AccountStatus = 2
|
||||
err = accountDatabase.CreateData(setData)
|
||||
if err != nil {
|
||||
Utils.Error(c, accountData)
|
||||
return
|
||||
}
|
||||
if setData.AccountId == 0 {
|
||||
Utils.Error(c, accountData)
|
||||
return
|
||||
}
|
||||
|
||||
returnData.LoginToken = Utils.EncodeId(128, setData.AccountId)
|
||||
returnData.Account = GameAccountData.ReturnData(setData)
|
||||
|
||||
_ = codeDatabase.DeleteData(&GameCodeData.Data{}, fmt.Sprintf("code_mail = %q", email))
|
||||
|
||||
Utils.Success(c, returnData)
|
||||
return
|
||||
}
|
||||
225
service/framework/controller/game/account.go
Normal file
225
service/framework/controller/game/account.go
Normal file
@@ -0,0 +1,225 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package GameController
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
"Service/framework/database/game_account_data"
|
||||
"Service/framework/database/game_code_data"
|
||||
"Service/framework/utils"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func MailCode(c *gin.Context) {
|
||||
|
||||
referer, _ := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
mail := c.DefaultQuery("mail", "")
|
||||
if mail == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
randNumber := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
code := fmt.Sprintf("%06v", randNumber.Int31n(1000000))
|
||||
|
||||
codeDatabase := Database.New(GameCodeData.TableName)
|
||||
codeData := &GameCodeData.Data{}
|
||||
codeData.CodeMail = mail
|
||||
codeData.CodePhone = ""
|
||||
codeData.CodeContent = code
|
||||
err := codeDatabase.CreateData(codeData)
|
||||
if err != nil {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
send := Utils.SendMail(mail, "注册/登录验证码:"+codeData.CodeContent, "你的账号注册/登录验证码为:"+codeData.CodeContent)
|
||||
if !send {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
Utils.Success(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
type responseMailLogin struct {
|
||||
Account GameAccountData.Return `json:"account"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
func MailLogin(c *gin.Context) {
|
||||
|
||||
returnData := responseMailLogin{}
|
||||
|
||||
referer, _ := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
email := c.DefaultQuery("email", "")
|
||||
if email == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
password := c.DefaultQuery("password", "")
|
||||
if password == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
accountDatabase := Database.New(GameAccountData.TableName)
|
||||
accountData := GameAccountData.Data{}
|
||||
accountWhere := fmt.Sprintf("account_mail = %q AND account_password = %q", email, Utils.EncryptMD5(password))
|
||||
err := accountDatabase.GetData(&accountData, accountWhere, "account_id DESC")
|
||||
if err != nil {
|
||||
Utils.Warning(c, 10000, "账号密码错误", Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
returnData.Account = GameAccountData.ReturnData(&accountData)
|
||||
returnData.Token = Utils.EncodeId(128, accountData.AccountId)
|
||||
|
||||
Utils.Success(c, returnData)
|
||||
return
|
||||
}
|
||||
|
||||
type requestMailRegister struct {
|
||||
Mail string `json:"mail"`
|
||||
Code string `json:"code"`
|
||||
Password string `json:"password"`
|
||||
Name string `json:"name"`
|
||||
Number string `json:"number"`
|
||||
QuestionA string `json:"question_a"`
|
||||
AnswerA string `json:"answer_a"`
|
||||
QuestionB string `json:"question_b"`
|
||||
AnswerB string `json:"answer_b"`
|
||||
}
|
||||
|
||||
func MailRegister(c *gin.Context) {
|
||||
|
||||
referer, _ := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
jsonData := requestMailRegister{}
|
||||
requestJson, _ := ioutil.ReadAll(c.Request.Body)
|
||||
err := json.Unmarshal(requestJson, &jsonData)
|
||||
if err != nil {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
codeDatabase := Database.New(GameCodeData.TableName)
|
||||
codeData := GameCodeData.Data{}
|
||||
codeWhere := fmt.Sprintf("code_mail = %q AND code_content = %q", jsonData.Mail, jsonData.Code)
|
||||
err = codeDatabase.GetData(&codeData, codeWhere, "code_id DESC")
|
||||
if err != nil {
|
||||
Utils.Warning(c, 10000, "验证码错误", Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
accountDatabase := Database.New(GameAccountData.TableName)
|
||||
accountData := GameAccountData.Data{}
|
||||
accountWhere := fmt.Sprintf("account_mail = %q", jsonData.Mail)
|
||||
err = accountDatabase.GetData(&accountData, accountWhere, "account_id DESC")
|
||||
if err == nil {
|
||||
Utils.Warning(c, 10000, "邮箱已经被占用,请换一个", Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
AccountGroup := 1
|
||||
countWhere := fmt.Sprintf("account_status > %d", 0)
|
||||
count, _ := accountDatabase.CountData(countWhere)
|
||||
if count == 0 {
|
||||
AccountGroup = 10
|
||||
}
|
||||
|
||||
setData := &GameAccountData.Data{}
|
||||
setData.AccountMail = jsonData.Mail
|
||||
setData.AccountPassword = Utils.EncryptMD5(jsonData.Password)
|
||||
setData.AccountName = jsonData.Name
|
||||
setData.AccountNumber = jsonData.Number
|
||||
setData.AccountQuestionA = jsonData.QuestionA
|
||||
setData.AccountAnswerA = jsonData.AnswerA
|
||||
setData.AccountQuestionB = jsonData.QuestionB
|
||||
setData.AccountAnswerB = jsonData.AnswerB
|
||||
setData.AccountGroup = AccountGroup
|
||||
setData.AccountStatus = 2
|
||||
err = accountDatabase.CreateData(setData)
|
||||
if err != nil {
|
||||
Utils.Error(c, accountData)
|
||||
return
|
||||
}
|
||||
if setData.AccountId == 0 {
|
||||
Utils.Error(c, accountData)
|
||||
return
|
||||
}
|
||||
|
||||
_ = codeDatabase.DeleteData(&GameCodeData.Data{}, fmt.Sprintf("code_mail = %q", jsonData.Mail))
|
||||
|
||||
Utils.Success(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
type requestMailChangePassword struct {
|
||||
Mail string `json:"mail"`
|
||||
Password string `json:"password"`
|
||||
NewPassword string `json:"new_password"`
|
||||
}
|
||||
|
||||
func MailChangePassword(c *gin.Context) {
|
||||
|
||||
referer, _ := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
jsonData := requestMailChangePassword{}
|
||||
requestJson, _ := ioutil.ReadAll(c.Request.Body)
|
||||
err := json.Unmarshal(requestJson, &jsonData)
|
||||
if err != nil {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
accountDatabase := Database.New(GameAccountData.TableName)
|
||||
accountData := GameAccountData.Data{}
|
||||
accountWhere := fmt.Sprintf("account_mail = %q AND account_password = %q", jsonData.Mail, Utils.EncryptMD5(jsonData.Password))
|
||||
err = accountDatabase.GetData(&accountData, accountWhere, "account_id DESC")
|
||||
if err != nil {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
where := fmt.Sprintf("account_id = %d", accountData.AccountId)
|
||||
update := map[string]interface{}{"account_password": Utils.EncryptMD5(jsonData.NewPassword)}
|
||||
err = accountDatabase.UpdateData(where, update)
|
||||
if err != nil {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
Utils.Success(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
304
service/framework/controller/game/player.go
Normal file
304
service/framework/controller/game/player.go
Normal file
@@ -0,0 +1,304 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package GameController
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
"Service/framework/database/game_level_data"
|
||||
"Service/framework/database/game_map_data"
|
||||
"Service/framework/database/game_player_data"
|
||||
"Service/framework/utils"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io/ioutil"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type responsePlayerIndex struct {
|
||||
Player []GamePlayerData.Return `json:"player"`
|
||||
}
|
||||
|
||||
func PlayerIndex(c *gin.Context) {
|
||||
|
||||
returnData := responsePlayerIndex{}
|
||||
returnData.Player = make([]GamePlayerData.Return, 0)
|
||||
|
||||
referer, uid := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
if uid == 0 {
|
||||
Utils.Warning(c, -1, "登录失效,请重新登录", Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
token := c.DefaultQuery("token", "")
|
||||
if token == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
serverId, _ := Utils.DecodeId(32, token)
|
||||
if len(serverId) != 1 {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
roleDatabase := Database.New(GamePlayerData.TableName)
|
||||
roleList := make([]GamePlayerData.Data, 0)
|
||||
roleWhere := fmt.Sprintf("player_status = %d AND player_account_id = %d AND player_server_id = %d", 2, uid, serverId[0])
|
||||
err := roleDatabase.ListData(&roleList, roleWhere, "player_id DESC", 6)
|
||||
if err == nil {
|
||||
for _, v := range roleList {
|
||||
item := GamePlayerData.ReturnData(&v)
|
||||
// 地图数据查询
|
||||
mapDatabase := Database.New(GameMapData.TableName)
|
||||
mapData := GameMapData.Data{}
|
||||
mapWhere := fmt.Sprintf("map_number = %q", item.PlayerMap)
|
||||
err := mapDatabase.GetData(&mapData, mapWhere, "")
|
||||
if err == nil {
|
||||
if item.PlayerMapX == 0 || item.PlayerMapY == 0 {
|
||||
item.PlayerMapName = mapData.MapName
|
||||
item.PlayerMapX = mapData.MapDefaultX
|
||||
item.PlayerMapY = mapData.MapDefaultY
|
||||
}
|
||||
}
|
||||
// 等级数据查询
|
||||
levelDatabase := Database.New(GameLevelData.TableName)
|
||||
levelData := GameLevelData.Data{}
|
||||
levelWhere := fmt.Sprintf("level_career = %q AND (level_min >= %d AND level_max > %d)", item.PlayerCareer, item.PlayerAssetExperience, item.PlayerAssetExperience)
|
||||
err = levelDatabase.GetData(&levelData, levelWhere, "")
|
||||
if err == nil {
|
||||
item.PlayerAssetLevel = levelData.LevelName
|
||||
item.PlayerAssetLifeMax = levelData.LevelLifeValue
|
||||
item.PlayerAssetMagicMax = levelData.LevelMagicValue
|
||||
item.PlayerAssetExperienceMax = levelData.LevelMax
|
||||
|
||||
}
|
||||
returnData.Player = append(returnData.Player, item)
|
||||
}
|
||||
}
|
||||
|
||||
Utils.Success(c, returnData)
|
||||
return
|
||||
}
|
||||
|
||||
type requestPlayerCreate struct {
|
||||
Token string `json:"token"`
|
||||
Nickname string `json:"nickname"`
|
||||
Gender string `json:"gender"`
|
||||
Career string `json:"career"`
|
||||
}
|
||||
|
||||
type responsePlayerCreate struct {
|
||||
Player GamePlayerData.Return `json:"player"`
|
||||
}
|
||||
|
||||
func PlayerCreate(c *gin.Context) {
|
||||
|
||||
returnData := responsePlayerCreate{}
|
||||
|
||||
referer, uid := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
if uid == 0 {
|
||||
Utils.Warning(c, -1, "登录失效,请重新登录", Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
jsonData := requestPlayerCreate{}
|
||||
requestJson, _ := ioutil.ReadAll(c.Request.Body)
|
||||
fmt.Println(string(requestJson))
|
||||
err := json.Unmarshal(requestJson, &jsonData)
|
||||
if err != nil {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
serverId, _ := Utils.DecodeId(32, jsonData.Token)
|
||||
if len(serverId) != 1 {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
playerDatabase := Database.New(GamePlayerData.TableName)
|
||||
playerData := GamePlayerData.Data{}
|
||||
playerWhere := fmt.Sprintf("player_account_id = %d AND player_server_id = %d AND player_nickname = %q", uid, serverId[0], jsonData.Nickname)
|
||||
err = playerDatabase.GetData(&playerData, playerWhere, "")
|
||||
if err == nil {
|
||||
Utils.Warning(c, 10000, "昵称已经被占用,请换一个", Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
clothe := "000"
|
||||
weapon := "000"
|
||||
wing := "000"
|
||||
|
||||
if jsonData.Career == "warrior" {
|
||||
clothe = "009"
|
||||
weapon = "034"
|
||||
wing = "010"
|
||||
}
|
||||
|
||||
if jsonData.Career == "mage" {
|
||||
clothe = "009"
|
||||
weapon = "035"
|
||||
wing = "010"
|
||||
}
|
||||
|
||||
if jsonData.Career == "taoist" {
|
||||
clothe = "009"
|
||||
weapon = "036"
|
||||
wing = "010"
|
||||
}
|
||||
|
||||
setData := &GamePlayerData.Data{}
|
||||
setData.PlayerAccountId = uid
|
||||
setData.PlayerServerId = serverId[0]
|
||||
setData.PlayerNickname = jsonData.Nickname
|
||||
setData.PlayerCareer = jsonData.Career
|
||||
setData.PlayerGender = jsonData.Gender
|
||||
setData.PlayerBalance = 100
|
||||
setData.PlayerIntegral = 10
|
||||
setData.PlayerAngle = 2
|
||||
setData.PlayerMap = "001"
|
||||
setData.PlayerMapX = 0
|
||||
setData.PlayerMapY = 0
|
||||
setData.PlayerAssetLife = 1
|
||||
setData.PlayerAssetMagic = 1
|
||||
setData.PlayerAssetExperience = 1
|
||||
setData.PlayerBodyClothe = clothe
|
||||
setData.PlayerBodyWeapon = weapon
|
||||
setData.PlayerBodyWing = wing
|
||||
setData.PlayerGroupId = 1
|
||||
setData.PlayerStatus = 2
|
||||
err = playerDatabase.CreateData(setData)
|
||||
if err != nil {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
returnData.Player = GamePlayerData.ReturnData(setData)
|
||||
|
||||
// 地图数据查询
|
||||
mapDatabase := Database.New(GameMapData.TableName)
|
||||
mapData := GameMapData.Data{}
|
||||
mapWhere := fmt.Sprintf("map_number = %q", returnData.Player.PlayerMap)
|
||||
err = mapDatabase.GetData(&mapData, mapWhere, "")
|
||||
if err == nil {
|
||||
if returnData.Player.PlayerMapX == 0 || returnData.Player.PlayerMapY == 0 {
|
||||
returnData.Player.PlayerMapName = mapData.MapName
|
||||
returnData.Player.PlayerMapX = mapData.MapDefaultX
|
||||
returnData.Player.PlayerMapY = mapData.MapDefaultY
|
||||
}
|
||||
}
|
||||
|
||||
// 等级数据查询
|
||||
levelDatabase := Database.New(GameLevelData.TableName)
|
||||
levelData := GameLevelData.Data{}
|
||||
levelWhere := fmt.Sprintf("level_career = %q AND (level_min >= %d AND level_max > %d)", returnData.Player.PlayerCareer, returnData.Player.PlayerAssetExperience, returnData.Player.PlayerAssetExperience)
|
||||
err = levelDatabase.GetData(&levelData, levelWhere, "")
|
||||
if err == nil {
|
||||
returnData.Player.PlayerAssetLevel = levelData.LevelName
|
||||
returnData.Player.PlayerAssetLifeMax = levelData.LevelLifeValue
|
||||
returnData.Player.PlayerAssetMagicMax = levelData.LevelMagicValue
|
||||
returnData.Player.PlayerAssetExperienceMax = levelData.LevelMax
|
||||
}
|
||||
|
||||
Utils.Success(c, returnData)
|
||||
return
|
||||
}
|
||||
|
||||
func PlayerUpdateClientId(c *gin.Context) {
|
||||
|
||||
referer, uid := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
clientId := c.DefaultQuery("client_id", "")
|
||||
if clientId == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
token := c.DefaultQuery("token", "")
|
||||
if token == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
playerId, _ := Utils.DecodeId(32, token)
|
||||
if len(playerId) != 3 {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
if uid == 0 {
|
||||
Utils.Warning(c, -1, "登录失效,请重新登录", Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
playerDatabase := Database.New(GamePlayerData.TableName)
|
||||
playerWhere := fmt.Sprintf("player_account_id = %d AND player_server_id = %d AND player_id = %d", uid, playerId[2], playerId[0])
|
||||
clientIdInt, _ := strconv.Atoi(clientId)
|
||||
update := map[string]interface{}{"player_client_id": clientIdInt}
|
||||
err := playerDatabase.UpdateData(playerWhere, update)
|
||||
if err != nil {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
Utils.Success(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
func PlayerDelete(c *gin.Context) {
|
||||
|
||||
referer, uid := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
if uid == 0 {
|
||||
Utils.Warning(c, -1, "登录失效,请重新登录", Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
token := c.DefaultQuery("token", "")
|
||||
if token == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
playerId, _ := Utils.DecodeId(32, token)
|
||||
if len(playerId) != 3 {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
playerDatabase := Database.New(GamePlayerData.TableName)
|
||||
playerWhere := fmt.Sprintf("player_account_id = %d AND player_server_id = %d AND player_id = %d", uid, playerId[2], playerId[0])
|
||||
update := map[string]interface{}{"player_status": 1}
|
||||
err := playerDatabase.UpdateData(playerWhere, update)
|
||||
if err != nil {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
Utils.Success(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
51
service/framework/controller/game/server.go
Normal file
51
service/framework/controller/game/server.go
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package GameController
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
"Service/framework/database/game_server_data"
|
||||
"Service/framework/utils"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type responseServerIndex struct {
|
||||
Server []GameServerData.Return `json:"server"`
|
||||
}
|
||||
|
||||
func ServerIndex(c *gin.Context) {
|
||||
|
||||
returnData := responseServerIndex{}
|
||||
returnData.Server = make([]GameServerData.Return, 0)
|
||||
|
||||
referer, uid := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
if uid == 0 {
|
||||
Utils.Warning(c, -1, "登录失效,请重新登录", Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
serverDatabase := Database.New(GameServerData.TableName)
|
||||
serverList := make([]GameServerData.Data, 0)
|
||||
serverWhere := fmt.Sprintf("server_status = %d", 2)
|
||||
err := serverDatabase.ListData(&serverList, serverWhere, "server_id", 10)
|
||||
if err == nil {
|
||||
for _, v := range serverList {
|
||||
item := GameServerData.ReturnData(&v)
|
||||
returnData.Server = append(returnData.Server, item)
|
||||
}
|
||||
}
|
||||
|
||||
Utils.Success(c, returnData)
|
||||
return
|
||||
}
|
||||
40
service/framework/controller/intranet/map.go
Normal file
40
service/framework/controller/intranet/map.go
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package IntranetController
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
"Service/framework/database/game_map_data"
|
||||
"Service/framework/utils"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type responseMapList struct {
|
||||
Map []GameMapData.Return `json:"map"`
|
||||
}
|
||||
|
||||
func MapList(c *gin.Context) {
|
||||
|
||||
returnData := responseMapList{}
|
||||
returnData.Map = make([]GameMapData.Return, 0)
|
||||
|
||||
mapDatabase := Database.New(GameMapData.TableName)
|
||||
mapList := make([]GameMapData.Data, 0)
|
||||
mapWhere := fmt.Sprintf("map_status = %d", 2)
|
||||
err := mapDatabase.ListData(&mapList, mapWhere, "map_id", 1000)
|
||||
if err == nil {
|
||||
for _, v := range mapList {
|
||||
item := GameMapData.ReturnData(&v)
|
||||
returnData.Map = append(returnData.Map, item)
|
||||
}
|
||||
}
|
||||
|
||||
Utils.Success(c, returnData)
|
||||
return
|
||||
}
|
||||
61
service/framework/controller/intranet/message.go
Normal file
61
service/framework/controller/intranet/message.go
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package IntranetController
|
||||
|
||||
import (
|
||||
"Service/framework/package/socket"
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gookit/color"
|
||||
"github.com/gorilla/websocket"
|
||||
"log"
|
||||
)
|
||||
|
||||
func MessageIndex(c *gin.Context) {
|
||||
|
||||
conn, err := SocketPackage.SocketGrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
log.Println("[controller:intranet:message:index:error]", color.Yellow.Text("websocket upgrade error:"+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
if SocketPackage.Message.Status == false {
|
||||
SocketPackage.Message.User = make(map[*websocket.Conn]bool)
|
||||
SocketPackage.Message.Status = true
|
||||
}
|
||||
|
||||
SocketPackage.Message.User[conn] = true
|
||||
|
||||
log.Println("[controller:intranet:message:index]", color.Green.Text("game server connection socket..."))
|
||||
|
||||
for {
|
||||
_, data, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
_ = conn.Close()
|
||||
delete(SocketPackage.Message.User, conn)
|
||||
return
|
||||
}
|
||||
|
||||
SocketPackage.Callback(data)
|
||||
|
||||
jsonFormat := SocketPackage.MessageFormat{}
|
||||
err = json.Unmarshal(data, &jsonFormat)
|
||||
if err == nil {
|
||||
if jsonFormat.Command != "" {
|
||||
for user := range SocketPackage.Message.User {
|
||||
err := user.WriteMessage(websocket.TextMessage, data)
|
||||
if err != nil {
|
||||
_ = user.Close()
|
||||
delete(SocketPackage.Message.User, user)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
51
service/framework/controller/intranet/player.go
Normal file
51
service/framework/controller/intranet/player.go
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package IntranetController
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
"Service/framework/database/game_player_data"
|
||||
"Service/framework/utils"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func PlayerUpdateClientId(c *gin.Context) {
|
||||
|
||||
clientId := c.DefaultQuery("client_id", "")
|
||||
if clientId == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
token := c.DefaultQuery("token", "")
|
||||
if token == "" {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
playerId, _ := Utils.DecodeId(32, token)
|
||||
if len(playerId) != 3 {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
playerDatabase := Database.New(GamePlayerData.TableName)
|
||||
playerWhere := fmt.Sprintf("player_server_id = %d AND player_id = %d", playerId[2], playerId[0])
|
||||
clientIdInt, _ := strconv.Atoi(clientId)
|
||||
update := map[string]interface{}{"player_client_id": clientIdInt}
|
||||
err := playerDatabase.UpdateData(playerWhere, update)
|
||||
if err != nil {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
Utils.Success(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
25
service/framework/controller/ping/ping.go
Normal file
25
service/framework/controller/ping/ping.go
Normal file
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package PingController
|
||||
|
||||
import (
|
||||
"Service/framework/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Ping(c *gin.Context) {
|
||||
|
||||
referer, _ := Utils.CheckHeader(c)
|
||||
if !referer {
|
||||
Utils.Error(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
|
||||
Utils.Success(c, Utils.EmptyData{})
|
||||
return
|
||||
}
|
||||
80
service/framework/database/database.go
Normal file
80
service/framework/database/database.go
Normal file
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Database
|
||||
|
||||
import (
|
||||
"Service/framework/config"
|
||||
"fmt"
|
||||
"github.com/gookit/color"
|
||||
"github.com/jinzhu/gorm"
|
||||
_ "github.com/jinzhu/gorm/dialects/mysql"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
var Get *gorm.DB
|
||||
|
||||
type DefaultField struct {
|
||||
CreateAt int `gorm:"Column:create_at" json:"create_at"`
|
||||
UpdateAt int `gorm:"Column:update_at" json:"update_at"`
|
||||
DeleteAt int `gorm:"Column:delete_at" json:"delete_at"`
|
||||
}
|
||||
|
||||
func Init() {
|
||||
|
||||
var err error
|
||||
|
||||
Get, err = gorm.Open(Config.Get.Database.Type, fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", Config.Get.Database.User, Config.Get.Database.Password, Config.Get.Database.Host, Config.Get.Database.Name))
|
||||
if err != nil {
|
||||
log.Println("[database]", color.Red.Text(err.Error()))
|
||||
}
|
||||
|
||||
if Config.Get.Service.Mode == "release" {
|
||||
Get.LogMode(false)
|
||||
} else {
|
||||
Get.LogMode(true)
|
||||
}
|
||||
|
||||
gorm.DefaultTableNameHandler = func(db *gorm.DB, defaultTableName string) string {
|
||||
return defaultTableName
|
||||
}
|
||||
|
||||
Get.SingularTable(true)
|
||||
|
||||
Get.Callback().Create().Replace("gorm:update_time_stamp", func(scope *gorm.Scope) {
|
||||
if !scope.HasError() {
|
||||
nowTime := time.Now().Unix()
|
||||
if createTimeField, ok := scope.FieldByName("CreateAt"); ok {
|
||||
if createTimeField.IsBlank {
|
||||
err := createTimeField.Set(nowTime)
|
||||
if err != nil {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if modifyTimeField, ok := scope.FieldByName("UpdateAt"); ok {
|
||||
if modifyTimeField.IsBlank {
|
||||
err := modifyTimeField.Set(nowTime)
|
||||
if err != nil {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Get.Callback().Update().Replace("gorm:update_time_stamp", func(scope *gorm.Scope) {
|
||||
if _, ok := scope.Get("gorm:update_column"); !ok {
|
||||
_ = scope.SetColumn("UpdateAt", time.Now().Unix())
|
||||
}
|
||||
})
|
||||
|
||||
Get.DB().SetMaxIdleConns(1000)
|
||||
Get.DB().SetMaxOpenConns(10000)
|
||||
|
||||
Get.DB().SetConnMaxLifetime(time.Second * 45)
|
||||
}
|
||||
45
service/framework/database/game_account_data/table.go
Normal file
45
service/framework/database/game_account_data/table.go
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package GameAccountData
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
"Service/framework/utils"
|
||||
)
|
||||
|
||||
var TableName = "game_account_data"
|
||||
|
||||
type Data struct {
|
||||
AccountId int `gorm:"primary_key;AUTO_INCREMENT;unique_index;not null;column:account_id"`
|
||||
AccountMail string `gorm:"column:account_mail"`
|
||||
AccountPassword string `gorm:"column:account_password"`
|
||||
AccountName string `gorm:"column:account_name"`
|
||||
AccountNumber string `gorm:"column:account_number"`
|
||||
AccountQuestionA string `gorm:"column:account_question_a"`
|
||||
AccountQuestionB string `gorm:"column:account_question_b"`
|
||||
AccountAnswerA string `gorm:"column:account_answer_a"`
|
||||
AccountAnswerB string `gorm:"column:account_answer_b"`
|
||||
AccountGroup int `gorm:"column:account_group"`
|
||||
AccountStatus int `gorm:"column:account_status"`
|
||||
Database.DefaultField
|
||||
}
|
||||
|
||||
type Return struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
func ReturnData(dataStruct *Data) Return {
|
||||
|
||||
data := Return{}
|
||||
|
||||
if dataStruct.AccountId > 0 {
|
||||
data.Token = Utils.EncodeId(32, dataStruct.AccountId)
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
36
service/framework/database/game_code_data/table.go
Normal file
36
service/framework/database/game_code_data/table.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package GameCodeData
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
"Service/framework/utils"
|
||||
)
|
||||
|
||||
var TableName = "game_code_data"
|
||||
|
||||
type Data struct {
|
||||
CodeId int `gorm:"primary_key;AUTO_INCREMENT;unique_index;not null;column:code_id" json:"code_id"`
|
||||
CodePhone string `gorm:"column:code_phone" json:"code_phone"`
|
||||
CodeMail string `gorm:"column:code_mail" json:"code_mail"`
|
||||
CodeContent string `gorm:"column:code_content" json:"code_content"`
|
||||
Database.DefaultField
|
||||
}
|
||||
|
||||
type ReturnData struct {
|
||||
Token string `json:"token"`
|
||||
CodeId int `json:"code_id"`
|
||||
CodePhone string `json:"code_phone"`
|
||||
CodeMail string `json:"code_mail"`
|
||||
CodeContent string `json:"code_content"`
|
||||
}
|
||||
|
||||
func FormatData(dataStruct *Data) ReturnData {
|
||||
|
||||
data := ReturnData{}
|
||||
|
||||
if dataStruct.CodeId > 0 {
|
||||
data.Token = Utils.EncodeId(32, dataStruct.CodeId, 1)
|
||||
data.CodeContent = dataStruct.CodeContent
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
52
service/framework/database/game_level_data/table.go
Normal file
52
service/framework/database/game_level_data/table.go
Normal file
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package GameLevelData
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
)
|
||||
|
||||
var TableName = "game_level_data"
|
||||
|
||||
type Data struct {
|
||||
LevelId int `gorm:"primary_key;AUTO_INCREMENT;unique_index;not null;column:level_id"`
|
||||
LevelServerId int `gorm:"column:level_server_id"`
|
||||
LevelCareer string `gorm:"column:level_career"`
|
||||
LevelName int `gorm:"column:level_name"`
|
||||
LevelMin int `gorm:"column:level_min"`
|
||||
LevelMax int `gorm:"column:level_max"`
|
||||
LevelLifeValue int `gorm:"column:level_life_value"`
|
||||
LevelMagicValue int `gorm:"column:level_magic_value"`
|
||||
LevelStatus int `gorm:"column:level_status"`
|
||||
Database.DefaultField
|
||||
}
|
||||
|
||||
type Return struct {
|
||||
LevelCareer string `json:"level_career"`
|
||||
LevelName int `json:"level_name"`
|
||||
LevelMin int `json:"level_min"`
|
||||
LevelMax int `json:"level_max"`
|
||||
LevelLifeValue int `json:"level_life_value"`
|
||||
LevelMagicValue int `json:"level_magic_value"`
|
||||
}
|
||||
|
||||
func ReturnData(dataStruct *Data) Return {
|
||||
|
||||
data := Return{}
|
||||
|
||||
if dataStruct.LevelId > 0 {
|
||||
data.LevelCareer = dataStruct.LevelCareer
|
||||
data.LevelName = dataStruct.LevelName
|
||||
data.LevelMin = dataStruct.LevelMin
|
||||
data.LevelMax = dataStruct.LevelMax
|
||||
data.LevelLifeValue = dataStruct.LevelLifeValue
|
||||
data.LevelMagicValue = dataStruct.LevelMagicValue
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
46
service/framework/database/game_map_data/table.go
Normal file
46
service/framework/database/game_map_data/table.go
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package GameMapData
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
)
|
||||
|
||||
var TableName = "game_map_data"
|
||||
|
||||
type Data struct {
|
||||
MapId int `gorm:"primary_key;AUTO_INCREMENT;unique_index;not null;column:map_id"`
|
||||
MapServerId int `gorm:"column:map_server_id"`
|
||||
MapNumber string `gorm:"column:map_number"`
|
||||
MapName string `gorm:"column:map_name"`
|
||||
MapDefaultX int `gorm:"column:map_default_x"`
|
||||
MapDefaultY int `gorm:"column:map_default_y"`
|
||||
MapStatus int `gorm:"column:map_status"`
|
||||
Database.DefaultField
|
||||
}
|
||||
|
||||
type Return struct {
|
||||
MapNumber string `json:"map_number"`
|
||||
MapName string `json:"map_name"`
|
||||
MapDefaultX int `json:"map_default_x"`
|
||||
MapDefaultY int `json:"map_default_y"`
|
||||
}
|
||||
|
||||
func ReturnData(dataStruct *Data) Return {
|
||||
|
||||
data := Return{}
|
||||
|
||||
if dataStruct.MapId > 0 {
|
||||
data.MapNumber = dataStruct.MapNumber
|
||||
data.MapName = dataStruct.MapName
|
||||
data.MapDefaultX = dataStruct.MapDefaultX
|
||||
data.MapDefaultY = dataStruct.MapDefaultY
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
102
service/framework/database/game_player_data/table.go
Normal file
102
service/framework/database/game_player_data/table.go
Normal file
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package GamePlayerData
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
"Service/framework/utils"
|
||||
)
|
||||
|
||||
var TableName = "game_player_data"
|
||||
|
||||
type Data struct {
|
||||
PlayerId int `gorm:"primary_key;AUTO_INCREMENT;unique_index;not null;column:player_id"`
|
||||
PlayerAccountId int `gorm:"column:player_account_id"`
|
||||
PlayerServerId int `gorm:"column:player_server_id"`
|
||||
PlayerNickname string `gorm:"column:player_nickname"`
|
||||
PlayerCareer string `gorm:"column:player_career"`
|
||||
PlayerGender string `gorm:"column:player_gender"`
|
||||
PlayerBalance int `gorm:"column:player_balance"`
|
||||
PlayerIntegral int `gorm:"column:player_integral"`
|
||||
PlayerAngle int `gorm:"column:player_angle"`
|
||||
PlayerMap string `gorm:"column:player_map"`
|
||||
PlayerMapX int `gorm:"column:player_map_x"`
|
||||
PlayerMapY int `gorm:"column:player_map_y"`
|
||||
PlayerAssetLife int `gorm:"column:player_asset_life"`
|
||||
PlayerAssetMagic int `gorm:"column:player_asset_magic"`
|
||||
PlayerAssetExperience int `gorm:"column:player_asset_experience"`
|
||||
PlayerBodyClothe string `gorm:"column:player_body_clothe"`
|
||||
PlayerBodyWeapon string `gorm:"column:player_body_weapon"`
|
||||
PlayerBodyWing string `gorm:"column:player_body_wing"`
|
||||
PlayerClientId int `gorm:"column:player_client_id"`
|
||||
PlayerGroupId int `gorm:"column:player_group_id"`
|
||||
PlayerStatus int `gorm:"column:player_status"`
|
||||
Database.DefaultField
|
||||
}
|
||||
|
||||
type Return struct {
|
||||
Token string `json:"token"`
|
||||
PlayerNickname string `json:"player_nickname"`
|
||||
PlayerCareer string `json:"player_career"`
|
||||
PlayerGender string `json:"player_gender"`
|
||||
PlayerBalance string `json:"player_balance"`
|
||||
PlayerIntegral string `json:"player_integral"`
|
||||
PlayerAngle int `json:"player_angle"`
|
||||
PlayerMap string `json:"player_map"`
|
||||
PlayerMapName string `json:"player_map_name"`
|
||||
PlayerMapX int `json:"player_map_x"`
|
||||
PlayerMapY int `json:"player_map_y"`
|
||||
PlayerAssetLevel int `json:"player_asset_level"`
|
||||
PlayerAssetLife int `json:"player_asset_life"`
|
||||
PlayerAssetLifeMax int `json:"player_asset_life_max"`
|
||||
PlayerAssetMagic int `json:"player_asset_magic"`
|
||||
PlayerAssetMagicMax int `json:"player_asset_magic_max"`
|
||||
PlayerAssetWeight int `json:"player_asset_weight"`
|
||||
PlayerAssetWeightMax int `json:"player_asset_weight_max"`
|
||||
PlayerAssetExperience int `json:"player_asset_experience"`
|
||||
PlayerAssetExperienceMax int `json:"player_asset_experience_max"`
|
||||
PlayerBodyClothe string `json:"player_body_clothe"`
|
||||
PlayerBodyWeapon string `json:"player_body_weapon"`
|
||||
PlayerBodyWing string `json:"player_body_wing"`
|
||||
PlayerClientId int `json:"player_client_id"`
|
||||
PlayerGroupId int `json:"player_group_id"`
|
||||
}
|
||||
|
||||
func ReturnData(dataStruct *Data) Return {
|
||||
|
||||
data := Return{}
|
||||
|
||||
if dataStruct.PlayerId > 0 {
|
||||
data.Token = Utils.EncodeId(32, dataStruct.PlayerId, dataStruct.PlayerAccountId, dataStruct.PlayerServerId)
|
||||
data.PlayerNickname = dataStruct.PlayerNickname
|
||||
data.PlayerCareer = dataStruct.PlayerCareer
|
||||
data.PlayerGender = dataStruct.PlayerGender
|
||||
data.PlayerBalance = Utils.FormatCurrency(dataStruct.PlayerBalance)
|
||||
data.PlayerIntegral = Utils.FormatCurrency(dataStruct.PlayerIntegral)
|
||||
data.PlayerAngle = dataStruct.PlayerAngle
|
||||
data.PlayerMap = dataStruct.PlayerMap
|
||||
data.PlayerMapX = dataStruct.PlayerMapX
|
||||
data.PlayerMapY = dataStruct.PlayerMapY
|
||||
data.PlayerAssetLevel = 0
|
||||
data.PlayerAssetLife = dataStruct.PlayerAssetLife
|
||||
data.PlayerAssetLifeMax = 0
|
||||
data.PlayerAssetMagic = dataStruct.PlayerAssetMagic
|
||||
data.PlayerAssetMagicMax = 0
|
||||
data.PlayerAssetWeight = 5
|
||||
data.PlayerAssetWeightMax = 50
|
||||
data.PlayerAssetExperience = dataStruct.PlayerAssetExperience
|
||||
data.PlayerAssetExperienceMax = 0
|
||||
data.PlayerBodyClothe = dataStruct.PlayerBodyClothe
|
||||
data.PlayerBodyWeapon = dataStruct.PlayerBodyWeapon
|
||||
data.PlayerBodyWing = dataStruct.PlayerBodyWing
|
||||
data.PlayerClientId = dataStruct.PlayerClientId
|
||||
data.PlayerGroupId = dataStruct.PlayerGroupId
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
39
service/framework/database/game_server_data/table.go
Normal file
39
service/framework/database/game_server_data/table.go
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package GameServerData
|
||||
|
||||
import (
|
||||
"Service/framework/database"
|
||||
"Service/framework/utils"
|
||||
)
|
||||
|
||||
var TableName = "game_server_data"
|
||||
|
||||
type Data struct {
|
||||
ServerId int `gorm:"primary_key;AUTO_INCREMENT;unique_index;not null;column:server_id"`
|
||||
ServerName string `gorm:"column:server_name"`
|
||||
ServerStatus int `gorm:"column:server_status"`
|
||||
Database.DefaultField
|
||||
}
|
||||
|
||||
type Return struct {
|
||||
Token string `json:"token"`
|
||||
ServerName string `json:"server_name"`
|
||||
}
|
||||
|
||||
func ReturnData(dataStruct *Data) Return {
|
||||
|
||||
data := Return{}
|
||||
|
||||
if dataStruct.ServerId > 0 {
|
||||
data.Token = Utils.EncodeId(32, dataStruct.ServerId)
|
||||
data.ServerName = dataStruct.ServerName
|
||||
}
|
||||
|
||||
return data
|
||||
}
|
||||
65
service/framework/database/interface.go
Normal file
65
service/framework/database/interface.go
Normal file
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Database
|
||||
|
||||
import (
|
||||
"github.com/jinzhu/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Base struct {
|
||||
TableName string
|
||||
}
|
||||
|
||||
func New(table string) *Base {
|
||||
return &Base{
|
||||
TableName: table,
|
||||
}
|
||||
}
|
||||
|
||||
func (base *Base) CreateData(data interface{}) error {
|
||||
err := Get.Table(base.TableName).Create(data).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (base *Base) UpdateData(query interface{}, data map[string]interface{}) error {
|
||||
data["update_at"] = time.Now().Unix()
|
||||
err := Get.Table(base.TableName).Where(query).Updates(data).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (base *Base) ExprData(query interface{}, field string, operation string, data int) error {
|
||||
err := Get.Table(base.TableName).Where(query).Update(field, gorm.Expr(field+" "+operation+" ?", data)).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (base *Base) GetData(dataStruct interface{}, query interface{}, order string) error {
|
||||
err := Get.Table(base.TableName).Where(query).Order(order).First(dataStruct).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (base *Base) ListData(dataStruct interface{}, query interface{}, order string, limit int) error {
|
||||
err := Get.Table(base.TableName).Where(query).Order(order).Limit(limit).Find(dataStruct).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (base *Base) PageData(dataStruct interface{}, query interface{}, order string, limit int, page int) error {
|
||||
err := Get.Table(base.TableName).Where(query).Order(order).Limit(limit).Offset(page * limit).Find(dataStruct).Error
|
||||
return err
|
||||
}
|
||||
|
||||
func (base *Base) CountData(query interface{}) (int, error) {
|
||||
count := 0
|
||||
err := Get.Table(base.TableName).Where(query).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
func (base *Base) DeleteData(dataStruct interface{}, query interface{}) error {
|
||||
err := Get.Table(base.TableName).Where(query).Delete(dataStruct).Error
|
||||
return err
|
||||
}
|
||||
20
service/framework/framework.go
Normal file
20
service/framework/framework.go
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Framework
|
||||
|
||||
import (
|
||||
"Service/framework/config"
|
||||
"Service/framework/database"
|
||||
"Service/framework/package/crontab"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
Config.Setup()
|
||||
Database.Init()
|
||||
CrontabPackage.Start()
|
||||
}
|
||||
23
service/framework/package/crontab/contab.go
Normal file
23
service/framework/package/crontab/contab.go
Normal file
@@ -0,0 +1,23 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package CrontabPackage
|
||||
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
)
|
||||
|
||||
func Start() {
|
||||
cronTab := cron.New(cron.WithSeconds())
|
||||
_, _ = cronTab.AddFunc("*/10 * * * * *", func() {
|
||||
// every 10 seconds
|
||||
})
|
||||
_, _ = cronTab.AddFunc("0 */1 * * * *", func() {
|
||||
// every 10 minute
|
||||
})
|
||||
cronTab.Start()
|
||||
}
|
||||
39
service/framework/package/socket/socket.go
Normal file
39
service/framework/package/socket/socket.go
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package SocketPackage
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gorilla/websocket"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
var Message = &message{}
|
||||
|
||||
type message struct {
|
||||
User map[*websocket.Conn]bool
|
||||
Status bool
|
||||
}
|
||||
|
||||
var SocketGrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool {
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
type MessageFormat struct {
|
||||
Command string `json:"command"`
|
||||
}
|
||||
|
||||
func Callback(message []byte) {
|
||||
jsonFormat := MessageFormat{}
|
||||
err := json.Unmarshal(message, &jsonFormat)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
72
service/framework/router/router.go
Normal file
72
service/framework/router/router.go
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Router
|
||||
|
||||
import (
|
||||
"Service/framework/config"
|
||||
"Service/framework/controller/disktop"
|
||||
"Service/framework/controller/game"
|
||||
"Service/framework/controller/intranet"
|
||||
"Service/framework/controller/ping"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Init() *gin.Engine {
|
||||
|
||||
router := gin.New()
|
||||
|
||||
gin.SetMode(Config.Get.Service.Mode)
|
||||
|
||||
ping := router.Group("ping")
|
||||
{
|
||||
ping.GET("/index", PingController.Ping)
|
||||
}
|
||||
|
||||
internal := router.Group("internal")
|
||||
{
|
||||
internal.GET("/message/index", IntranetController.MessageIndex)
|
||||
|
||||
internal.GET("/map/list", IntranetController.MapList)
|
||||
|
||||
internal.GET("/player/update/client/id", IntranetController.PlayerUpdateClientId)
|
||||
}
|
||||
|
||||
desktop := router.Group("desktop")
|
||||
{
|
||||
desktop.GET("/check/index", DisktopController.CheckIndex)
|
||||
|
||||
desktop.GET("/index/mail/code", DisktopController.MailCode)
|
||||
|
||||
desktop.GET("/index/mail/register", DisktopController.MailRegister)
|
||||
|
||||
desktop.GET("/index/mail/login", DisktopController.MailLogin)
|
||||
}
|
||||
|
||||
game := router.Group("game")
|
||||
{
|
||||
game.GET("/account/mail/code", GameController.MailCode)
|
||||
|
||||
game.GET("/account/mail/login", GameController.MailLogin)
|
||||
|
||||
game.POST("/account/mail/register", GameController.MailRegister)
|
||||
|
||||
game.POST("/account/mail/change/password", GameController.MailChangePassword)
|
||||
|
||||
game.GET("/server/index", GameController.ServerIndex)
|
||||
|
||||
game.GET("/player/index", GameController.PlayerIndex)
|
||||
|
||||
game.POST("/player/create", GameController.PlayerCreate)
|
||||
|
||||
game.GET("/player/update/client/id", GameController.PlayerUpdateClientId)
|
||||
|
||||
game.GET("/player/delete", GameController.PlayerDelete)
|
||||
}
|
||||
|
||||
return router
|
||||
}
|
||||
10
service/framework/utils/empty_data.go
Normal file
10
service/framework/utils/empty_data.go
Normal file
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
type EmptyData struct{}
|
||||
46
service/framework/utils/hashids.go
Normal file
46
service/framework/utils/hashids.go
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import (
|
||||
"Service/framework/config"
|
||||
"github.com/speps/go-hashids"
|
||||
)
|
||||
|
||||
func EncodeId(len int, id ...int) string {
|
||||
|
||||
hd := hashids.NewData()
|
||||
|
||||
hd.Salt = Config.Get.Hash.Salt
|
||||
|
||||
hd.MinLength = len
|
||||
|
||||
h := hashids.NewWithData(hd)
|
||||
|
||||
e, _ := h.Encode(id)
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func DecodeId(len int, encodedId string) ([]int, error) {
|
||||
|
||||
hd := hashids.NewData()
|
||||
|
||||
hd.Salt = Config.Get.Hash.Salt
|
||||
|
||||
hd.MinLength = len
|
||||
|
||||
h := hashids.NewWithData(hd)
|
||||
|
||||
d, err := h.DecodeWithError(encodedId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return d, nil
|
||||
}
|
||||
41
service/framework/utils/header.go
Normal file
41
service/framework/utils/header.go
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CheckHeader(c *gin.Context) (bool, int) {
|
||||
|
||||
status := false
|
||||
accountUID := 0
|
||||
|
||||
referer := c.Request.Header.Get("Accept-Fetch-Referer")
|
||||
if referer != "" {
|
||||
allowedReferer := []string{
|
||||
"makeryang.com",
|
||||
}
|
||||
for _, allowedReferer := range allowedReferer {
|
||||
if strings.HasPrefix(referer, allowedReferer) {
|
||||
status = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auth := c.Request.Header.Get("Accept-Fetch-Auth")
|
||||
if auth != "" {
|
||||
uidMap, _ := DecodeId(128, auth)
|
||||
if len(uidMap) > 0 {
|
||||
accountUID = uidMap[0]
|
||||
}
|
||||
}
|
||||
|
||||
return status, accountUID
|
||||
}
|
||||
38
service/framework/utils/html.go
Normal file
38
service/framework/utils/html.go
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import (
|
||||
"github.com/russross/blackfriday"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func TrimHtml(html string) string {
|
||||
//将HTML标签全转换成小写
|
||||
re, _ := regexp.Compile("<[\\S\\s]+?>")
|
||||
html = re.ReplaceAllStringFunc(html, strings.ToLower)
|
||||
//去除STYLE
|
||||
re, _ = regexp.Compile("<style[\\S\\s]+?</style>")
|
||||
html = re.ReplaceAllString(html, "")
|
||||
//去除SCRIPT
|
||||
re, _ = regexp.Compile("<script[\\S\\s]+?</script>")
|
||||
html = re.ReplaceAllString(html, "")
|
||||
//去除所有尖括号内的HTML代码,并换成换行符
|
||||
re, _ = regexp.Compile("<[\\S\\s]+?>")
|
||||
html = re.ReplaceAllString(html, "\n")
|
||||
//去除连续的换行符
|
||||
re, _ = regexp.Compile("\\s{2,}")
|
||||
html = re.ReplaceAllString(html, "\n")
|
||||
return strings.TrimSpace(html)
|
||||
}
|
||||
|
||||
func Markdown2Html(markdown string) string {
|
||||
html := blackfriday.MarkdownCommon([]byte(markdown))
|
||||
return string(html)
|
||||
}
|
||||
51
service/framework/utils/mailbox.go
Normal file
51
service/framework/utils/mailbox.go
Normal file
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import (
|
||||
"gopkg.in/gomail.v2"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func MailFormat(email string) string {
|
||||
atIndex := strings.Index(email, "@")
|
||||
if atIndex == -1 {
|
||||
return email
|
||||
}
|
||||
|
||||
prefixLength := 2
|
||||
if atIndex < prefixLength {
|
||||
prefixLength = atIndex
|
||||
}
|
||||
prefix := email[:prefixLength]
|
||||
starsCount := atIndex - prefixLength
|
||||
if starsCount < 0 {
|
||||
starsCount = 0
|
||||
}
|
||||
hiddenPart := strings.Repeat("*", starsCount)
|
||||
domain := email[atIndex:]
|
||||
return prefix + hiddenPart + domain
|
||||
}
|
||||
|
||||
func SendMail(to string, subject string, content string) bool {
|
||||
|
||||
status := true
|
||||
|
||||
mail := gomail.NewMessage()
|
||||
mail.SetHeader("From", mail.FormatAddress("makeryangcom@foxmail.com", "MakerYang"))
|
||||
mail.SetHeader("To", to)
|
||||
mail.SetHeader("Subject", subject)
|
||||
mail.SetBody("text/html", content)
|
||||
|
||||
send := gomail.NewDialer("smtp.qq.com", 587, "makeryangcom@foxmail.com", "xwaacdrftozsbjgc")
|
||||
if err := send.DialAndSend(mail); err != nil {
|
||||
status = false
|
||||
}
|
||||
|
||||
return status
|
||||
}
|
||||
34
service/framework/utils/markdown.go
Normal file
34
service/framework/utils/markdown.go
Normal file
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func FilterMarkdown(input string) string {
|
||||
quoteBlockRegex := regexp.MustCompile(`^\s*>[ \t]*(.*)$`)
|
||||
lines := strings.Split(input, "\n")
|
||||
var quoteLines []string
|
||||
for _, line := range lines {
|
||||
if quoteBlockRegex.MatchString(line) {
|
||||
match := quoteBlockRegex.FindStringSubmatch(line)
|
||||
quoteLines = append(quoteLines, match[1])
|
||||
}
|
||||
}
|
||||
return strings.Join(quoteLines, "")
|
||||
}
|
||||
|
||||
func FilterSummary(input string, maxLength int) string {
|
||||
text := strings.TrimSpace(input)
|
||||
if len(text) <= maxLength {
|
||||
return text
|
||||
}
|
||||
return text[:maxLength]
|
||||
}
|
||||
45
service/framework/utils/number.go
Normal file
45
service/framework/utils/number.go
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func FormatKUnit(number int) string {
|
||||
if number < 1000 {
|
||||
return fmt.Sprintf("%d", number)
|
||||
}
|
||||
kValue := float64(number) / 1000.0
|
||||
return fmt.Sprintf("%.1fK", kValue)
|
||||
}
|
||||
|
||||
func FormatCurrency(amount int) string {
|
||||
amountStr := fmt.Sprintf("%d", amount)
|
||||
|
||||
runes := []rune(amountStr)
|
||||
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
|
||||
runes[i], runes[j] = runes[j], runes[i]
|
||||
}
|
||||
revStr := string(runes)
|
||||
|
||||
var result strings.Builder
|
||||
for i, r := range revStr {
|
||||
if i > 0 && i%3 == 0 {
|
||||
result.WriteRune(',')
|
||||
}
|
||||
result.WriteRune(r)
|
||||
}
|
||||
|
||||
runes = []rune(result.String())
|
||||
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
|
||||
runes[i], runes[j] = runes[j], runes[i]
|
||||
}
|
||||
return string(runes)
|
||||
}
|
||||
31
service/framework/utils/order.go
Normal file
31
service/framework/utils/order.go
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func CreateOrderNum() string {
|
||||
|
||||
str := "0123456789"
|
||||
|
||||
bytes := []byte(str)
|
||||
result := make([]byte, 0)
|
||||
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
for i := 0; i < 8; i++ {
|
||||
result = append(result, bytes[r.Intn(len(bytes))])
|
||||
}
|
||||
|
||||
order := time.Now().Format("20060102150405") + string(result)
|
||||
|
||||
return order
|
||||
}
|
||||
20
service/framework/utils/password.go
Normal file
20
service/framework/utils/password.go
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func EncryptMD5(input string) string {
|
||||
hash := md5.New()
|
||||
hash.Write([]byte(input))
|
||||
hashBytes := hash.Sum(nil)
|
||||
return hex.EncodeToString(hashBytes)
|
||||
}
|
||||
119
service/framework/utils/phone.go
Normal file
119
service/framework/utils/phone.go
Normal file
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"time"
|
||||
)
|
||||
|
||||
func MobileFormat(str string) string {
|
||||
re, _ := regexp.Compile("(\\d{3})(\\d{6})(\\d{2})")
|
||||
return re.ReplaceAllString(str, "$1******$3")
|
||||
}
|
||||
|
||||
type SendSmsRequest struct {
|
||||
PhoneNumberSet []string `json:"PhoneNumberSet,omitempty"`
|
||||
SmsSdkAppId string `json:"SmsSdkAppId,omitempty"`
|
||||
TemplateId string `json:"TemplateId,omitempty"`
|
||||
SignName string `json:"SignName,omitempty"`
|
||||
TemplateParamSet []string `json:"TemplateParamSet,omitempty"`
|
||||
ExtendCode string `json:"ExtendCode,omitempty"`
|
||||
SessionContext string `json:"SessionContext,omitempty"`
|
||||
SenderId string `json:"SenderId,omitempty"`
|
||||
}
|
||||
|
||||
func sha256hex(s string) string {
|
||||
b := sha256.Sum256([]byte(s))
|
||||
return hex.EncodeToString(b[:])
|
||||
}
|
||||
|
||||
func hmacsha256(s, key string) string {
|
||||
hashed := hmac.New(sha256.New, []byte(key))
|
||||
hashed.Write([]byte(s))
|
||||
return string(hashed.Sum(nil))
|
||||
}
|
||||
|
||||
func SendMessage(form string, phone string, info string) bool {
|
||||
|
||||
status := true
|
||||
|
||||
secretId := ""
|
||||
secretKey := ""
|
||||
host := "sms.tencentcloudapi.com"
|
||||
algorithm := "TC3-HMAC-SHA256"
|
||||
service := "sms"
|
||||
version := "2021-01-11"
|
||||
action := "SendSms"
|
||||
region := "ap-guangzhou"
|
||||
timestamp := time.Now().Unix()
|
||||
|
||||
httpRequestMethod := "POST"
|
||||
canonicalURI := "/"
|
||||
canonicalQueryString := ""
|
||||
canonicalHeaders := "content-type:application/json; charset=utf-8\n" + "host:" + host + "\n"
|
||||
signedHeaders := "content-type;host"
|
||||
|
||||
request := SendSmsRequest{
|
||||
SmsSdkAppId: "",
|
||||
SignName: "MakerYang",
|
||||
TemplateId: "665293",
|
||||
TemplateParamSet: []string{info, "5"},
|
||||
PhoneNumberSet: []string{"+86" + phone},
|
||||
SessionContext: "{1}为您的验证码,请于{2}分钟内填写。如非本人操作,请忽略本短信。",
|
||||
}
|
||||
|
||||
payload, _ := json.Marshal(request)
|
||||
hashedRequestPayload := sha256hex(string(payload))
|
||||
canonicalRequest := fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s", httpRequestMethod, canonicalURI, canonicalQueryString, canonicalHeaders, signedHeaders, hashedRequestPayload)
|
||||
|
||||
date := time.Unix(timestamp, 0).UTC().Format("2006-01-02")
|
||||
credentialScope := fmt.Sprintf("%s/%s/tc3_request", date, service)
|
||||
hashedCanonicalRequest := sha256hex(canonicalRequest)
|
||||
string2sign := fmt.Sprintf("%s\n%d\n%s\n%s", algorithm, timestamp, credentialScope, hashedCanonicalRequest)
|
||||
|
||||
secretDate := hmacsha256(date, "TC3"+secretKey)
|
||||
secretService := hmacsha256(service, secretDate)
|
||||
secretSigning := hmacsha256("tc3_request", secretService)
|
||||
signature := hex.EncodeToString([]byte(hmacsha256(string2sign, secretSigning)))
|
||||
|
||||
authorization := fmt.Sprintf("%s Credential=%s/%s, SignedHeaders=%s, Signature=%s", algorithm, secretId, credentialScope, signedHeaders, signature)
|
||||
|
||||
url := fmt.Sprintf("https://%s", host)
|
||||
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBufferString(string(payload)))
|
||||
if err != nil {
|
||||
status = false
|
||||
return status
|
||||
}
|
||||
|
||||
req.Header.Set("Authorization", authorization)
|
||||
req.Header.Set("Content-Type", "application/json; charset=utf-8")
|
||||
req.Header.Set("Host", host)
|
||||
req.Header.Set("X-TC-Action", action)
|
||||
req.Header.Set("X-TC-Timestamp", fmt.Sprintf("%d", timestamp))
|
||||
req.Header.Set("X-TC-Version", version)
|
||||
req.Header.Set("X-TC-Region", region)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
status = false
|
||||
return status
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return true
|
||||
}
|
||||
14
service/framework/utils/price.go
Normal file
14
service/framework/utils/price.go
Normal file
@@ -0,0 +1,14 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import "fmt"
|
||||
|
||||
func PriceConvert(num int) string {
|
||||
return fmt.Sprintf("%.2f", float64(num)/100)
|
||||
}
|
||||
27
service/framework/utils/rand.go
Normal file
27
service/framework/utils/rand.go
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
func RandInt(min, max int) int {
|
||||
if min >= max || min == 0 || max == 0 {
|
||||
return max
|
||||
}
|
||||
return rand.Intn(max-min) + min
|
||||
}
|
||||
|
||||
func RandCode() string {
|
||||
randNumber := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
code := fmt.Sprintf("%06v", randNumber.Int31n(1000000))
|
||||
return code
|
||||
}
|
||||
137
service/framework/utils/return.go
Normal file
137
service/framework/utils/return.go
Normal file
@@ -0,0 +1,137 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
type logData struct {
|
||||
ClientIp string `json:"client_ip"`
|
||||
ClientMethod string `json:"client_method"`
|
||||
ClientUrl string `json:"client_url"`
|
||||
AcceptFetchId string `json:"accept_fetch_id"`
|
||||
AcceptFetchReferer string `json:"accept_fetch_referer"`
|
||||
AcceptFetchVisitor string `json:"accept_fetch_visitor"`
|
||||
AcceptFetchAuth string `json:"accept_fetch_auth"`
|
||||
ClientParameter clientParameter `json:"client_parameter"`
|
||||
ServerParameter string `json:"server_parameter"`
|
||||
Date string `json:"date"`
|
||||
Time int64 `json:"time"`
|
||||
Server string `json:"server"`
|
||||
Length string `json:"length"`
|
||||
}
|
||||
|
||||
type clientParameter struct {
|
||||
Get interface{} `json:"get"`
|
||||
Post interface{} `json:"post"`
|
||||
}
|
||||
|
||||
func requestLog(c *gin.Context, serverParameter string) {
|
||||
|
||||
data := &logData{}
|
||||
data.Time = time.Now().Unix()
|
||||
data.Date = time.Now().Format("2006-01-02 15:04:05")
|
||||
data.ClientMethod = c.Request.Method
|
||||
data.ClientIp = c.ClientIP()
|
||||
data.AcceptFetchId = c.Request.Header.Get("Accept-Fetch-Id")
|
||||
data.AcceptFetchReferer = c.Request.Header.Get("Accept-Fetch-Referer")
|
||||
data.AcceptFetchVisitor = c.Request.Header.Get("Accept-Fetch-Visitor")
|
||||
data.AcceptFetchAuth = c.Request.Header.Get("Accept-Fetch-Auth")
|
||||
if data.ClientMethod == "GET" {
|
||||
data.ClientParameter.Get = c.Request.URL.Query()
|
||||
}
|
||||
if data.ClientMethod == "POST" {
|
||||
data.ClientParameter.Post = c.Request.PostForm
|
||||
}
|
||||
scheme := "http://"
|
||||
if c.Request.TLS != nil {
|
||||
scheme = "https://"
|
||||
}
|
||||
serverUrl := scheme + c.Request.Host + c.Request.URL.Path
|
||||
data.ClientUrl = serverUrl
|
||||
serverName, _ := os.Hostname()
|
||||
data.Server = serverName
|
||||
data.ServerParameter = serverParameter
|
||||
|
||||
clientTimeStr := c.Request.Header.Get("Client-Time")
|
||||
if clientTimeStr != "" {
|
||||
clientTimeMs, _ := strconv.ParseInt(clientTimeStr, 10, 64)
|
||||
serverTimeMs := time.Now().UnixNano() / 1e6
|
||||
processingTimeMs := serverTimeMs - clientTimeMs
|
||||
data.Length = fmt.Sprintf("%.3f", math.Abs(float64(processingTimeMs)/1000.0))
|
||||
}
|
||||
|
||||
dataString, _ := json.Marshal(data)
|
||||
|
||||
log.Println("[request]", string(dataString))
|
||||
}
|
||||
|
||||
func Success(c *gin.Context, data interface{}) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 0,
|
||||
"msg": "success",
|
||||
"message": "success",
|
||||
"data": data,
|
||||
})
|
||||
|
||||
logJson, _ := json.Marshal(gin.H{"code": 0, "msg": "success", "message": "success", "data": data})
|
||||
|
||||
requestLog(c, string(logJson))
|
||||
}
|
||||
|
||||
func Error(c *gin.Context, data interface{}) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 10000,
|
||||
"msg": "error",
|
||||
"message": "error",
|
||||
"data": data,
|
||||
})
|
||||
|
||||
logJson, _ := json.Marshal(gin.H{"code": 10000, "msg": "error", "message": "error", "data": data})
|
||||
|
||||
requestLog(c, string(logJson))
|
||||
}
|
||||
|
||||
func Warning(c *gin.Context, code int, msg string, data interface{}) {
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": code,
|
||||
"msg": msg,
|
||||
"message": msg,
|
||||
"data": data,
|
||||
})
|
||||
|
||||
logJson, _ := json.Marshal(gin.H{"code": code, "msg": msg, "message": msg, "data": data})
|
||||
|
||||
requestLog(c, string(logJson))
|
||||
}
|
||||
|
||||
func AuthError(c *gin.Context, code int, msg string, data interface{}) {
|
||||
|
||||
c.JSON(http.StatusUnauthorized, gin.H{
|
||||
"code": code,
|
||||
"msg": msg,
|
||||
"message": msg,
|
||||
"data": data,
|
||||
})
|
||||
|
||||
logJson, _ := json.Marshal(gin.H{"code": code, "msg": msg, "message": msg, "data": data})
|
||||
|
||||
requestLog(c, string(logJson))
|
||||
}
|
||||
80
service/framework/utils/time.go
Normal file
80
service/framework/utils/time.go
Normal file
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
#*****************************************************************************
|
||||
# @author MakerYang
|
||||
# @site mir2.makeryang.com
|
||||
#*****************************************************************************
|
||||
*/
|
||||
|
||||
package Utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TimeFormat(unix int) (string, string) {
|
||||
|
||||
timeInt := time.Unix(int64(unix), 0)
|
||||
|
||||
return timeInt.Format("2006年01月02日 15:04:05"), timeInt.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
func TimeMinFormat(unix int) (string, string) {
|
||||
|
||||
timeInt := time.Unix(int64(unix), 0)
|
||||
|
||||
return timeInt.Format("2006年01月02日"), timeInt.Format("2006-01-02")
|
||||
}
|
||||
|
||||
func DateFormat(times int) string {
|
||||
|
||||
createTime := time.Unix(int64(times), 0)
|
||||
now := time.Now().Unix()
|
||||
|
||||
difTime := now - int64(times)
|
||||
|
||||
str := ""
|
||||
if difTime < 60 {
|
||||
str = "刚刚"
|
||||
} else if difTime < 3600 {
|
||||
M := difTime / 60
|
||||
str = strconv.Itoa(int(M)) + "分钟前"
|
||||
} else if difTime < 3600*24 {
|
||||
H := difTime / 3600
|
||||
str = strconv.Itoa(int(H)) + "小时前"
|
||||
} else {
|
||||
str = createTime.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func DateFormatFine(timestamp int64) string {
|
||||
now := time.Now()
|
||||
past := time.Unix(timestamp, 0) // Convert Unix timestamp to time.Time
|
||||
duration := now.Sub(past)
|
||||
|
||||
// Calculate the differences
|
||||
seconds := int(duration.Seconds())
|
||||
minutes := int(duration.Minutes())
|
||||
hours := int(duration.Hours())
|
||||
days := hours / 24
|
||||
weeks := days / 7
|
||||
// months := days / 30
|
||||
|
||||
switch {
|
||||
case seconds < 60:
|
||||
return fmt.Sprintf("%d 秒前", seconds)
|
||||
case minutes < 60:
|
||||
return fmt.Sprintf("%d 分钟前", minutes)
|
||||
case hours < 24:
|
||||
return fmt.Sprintf("%d 小时前", hours)
|
||||
case days < 7:
|
||||
return fmt.Sprintf("%d 天前", days)
|
||||
case days < 30:
|
||||
return fmt.Sprintf("%d 周前", weeks)
|
||||
default:
|
||||
return past.Format("2006年01月02日")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user