add astro api

master
jager 3 years ago
parent f8f203851a
commit 49037549cf

@ -13,24 +13,42 @@
package main package main
import ( import (
"idata/internal/api" "github.com/jageros/hawox/astro"
"idata/internal/service/soup"
"idata/internal/service/user"
"github.com/jageros/hawox/flags" "github.com/jageros/hawox/flags"
"github.com/jageros/hawox/httpx" "github.com/jageros/hawox/httpx"
"github.com/jageros/hawox/logx" "github.com/jageros/hawox/logx"
"github.com/jageros/hawox/mysql" "github.com/jageros/hawox/mysql"
"github.com/jageros/hawox/redis" "github.com/jageros/hawox/redis"
"idata/internal/api"
"idata/internal/service/soup"
"idata/internal/service/user"
"math/rand"
"time"
) )
const appName = "idata" const appName = "idata"
func main() { func main() {
rand.Seed(time.Now().Unix())
ctx, wait := flags.Parse(appName) ctx, wait := flags.Parse(appName)
defer wait() defer wait()
err := soup.InitFromFile("config/soup.txt") soupPath := flags.GetValStr("soup.path")
if soupPath == "" {
soupPath = "soup.txt"
}
datePath := flags.GetValStr("date.path")
if datePath == "" {
datePath = "date.json"
}
err := astro.InitFromJsonFile(datePath)
if err != nil {
logx.Fatalf("astro init err: %v", err)
}
err = soup.InitFromFile(soupPath)
if err != nil { if err != nil {
logx.Fatalf("soup init err%v", err) logx.Fatalf("soup init err%v", err)
} }

@ -30,3 +30,9 @@ redis:
db: 0 db: 0
# user: "redis-user" # user: "redis-user"
# password: "redis-passwd" # password: "redis-passwd"
soup:
path: "conf.d/soup.txt"
date:
path: "conf.d/date.json"

File diff suppressed because it is too large Load Diff

@ -1,51 +0,0 @@
server:
id: 1
mode: "release"
frontend:
addr: "ws://testapi.gzjy-game.xyz/ws"
log:
dir: "logs"
caller: true
request: true
stat: false
http:
ip: "0.0.0.0"
port: 8001
rpc:
ip: "127.0.0.1"
port: 8081
mysql:
addr: "127.0.0.1:3306"
user: "root"
password: "QianYin@66"
database: "hawox"
mongo:
addr: "127.0.0.1:27017"
database: "attribute"
# user: "mongo-user"
# password: "mongo-password"
etcd:
addrs: "127.0.0.1:2379"
# user: "etcd-user"
# password: "etcd-password"
redis:
addrs: "127.0.0.1:7001;127.0.0.1:7002;127.0.0.1:7003;127.0.0.1:7004;127.0.0.1:7005;127.0.0.1:7006;127.0.0.1:7007"
db: 0
# user: "redis-user"
# password: "redis-passwd"
queue:
type: "kafka"
addrs: "127.0.0.1:9092"
#queue:
# type: "nsq"
# addrs: "127.0.0.1:4161"

@ -4,7 +4,7 @@ go 1.17
require ( require (
github.com/gin-gonic/gin v1.7.7 github.com/gin-gonic/gin v1.7.7
github.com/jageros/hawox v0.1.5 github.com/jageros/hawox v0.1.8
github.com/tal-tech/go-zero v1.2.1 github.com/tal-tech/go-zero v1.2.1
) )

@ -322,8 +322,8 @@ github.com/iancoleman/strcase v0.1.2/go.mod h1:SK73tn/9oHe+/Y0h39VT4UCxmurVJkR5N
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/jageros/hawox v0.1.5 h1:awUzBuOf4SbT/4TZ098MRXFKsFQ8LGDuONd3U4NfiDU= github.com/jageros/hawox v0.1.8 h1:4LpBuKEeYs4q3KstPJrOUWuiLT9X1e3HTApE0S3+/fg=
github.com/jageros/hawox v0.1.5/go.mod h1:SmP4sI64uIP4Cg8CwU1///5Gii2z0jcn+BHi9ajnyyg= github.com/jageros/hawox v0.1.8/go.mod h1:SmP4sI64uIP4Cg8CwU1///5Gii2z0jcn+BHi9ajnyyg=
github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs= github.com/jcmturner/aescts/v2 v2.0.0/go.mod h1:AiaICIRyfYg35RUkr8yESTqvSy7csK90qZ5xfvvsoNs=
github.com/jcmturner/dnsutils/v2 v2.0.0/go.mod h1:b0TnjGOvI/n42bZa+hmXL+kFJZsFT7G4t3HTlQ184QM= github.com/jcmturner/dnsutils/v2 v2.0.0/go.mod h1:b0TnjGOvI/n42bZa+hmXL+kFJZsFT7G4t3HTlQ184QM=
github.com/jcmturner/gofork v1.0.0/go.mod h1:MK8+TM0La+2rjBD4jE12Kj1pCCxK7d2LK/UM3ncEo0o= github.com/jcmturner/gofork v1.0.0/go.mod h1:MK8+TM0La+2rjBD4jE12Kj1pCCxK7d2LK/UM3ncEo0o=

@ -13,6 +13,8 @@
package api package api
import ( import (
"github.com/jageros/hawox/jwt"
"idata/internal/service/almanac"
"idata/internal/service/soup" "idata/internal/service/soup"
"idata/internal/service/user" "idata/internal/service/user"
@ -26,10 +28,13 @@ func Registry(engine *gin.Engine) {
ur := r.Group("/user") ur := r.Group("/user")
ur.POST("/register", user.Register) ur.POST("/register", user.Register)
ur.POST("/login", user.Login) ur.POST("/login", user.Login)
ur.GET("/info", user.Info) ur.GET("/info", jwt.CheckToken, user.Info)
ur.GET("/secret", jwt.CheckToken, user.Secret)
ur.GET("/open", jwt.CheckToken, user.OpenApi)
apiR := r.Group("/api", auth) apiR := r.Group("/api", auth)
apiR.GET("/soup", soup.GetRandSoup) apiR.GET("/soup", soup.GetRandSoup)
apiR.GET("/almanac", almanac.Almanac)
} }
func auth(ctx *gin.Context) { func auth(ctx *gin.Context) {

@ -0,0 +1,42 @@
/**
* @Author: jager
* @Email: lhj168os@gmail.com
* @File: consts
* @Date: 2021/12/31 5:20
* @package: consts
* @Version: v1.0.0
*
* @Description:
*
*/
package apity
import "strconv"
type ApiType int64
const (
Soup ApiType = 1 << iota
Weather
Obscenity
Astro
)
func Type(ty string) ApiType {
tyN, err := strconv.Atoi(ty)
if err == nil {
return ApiType(1 << (tyN - 1))
}
switch ty {
case "soup":
return Soup
case "weather":
return Weather
case "obscenity":
return Obscenity
case "astro":
return Astro
}
return ApiType(0)
}

@ -0,0 +1,29 @@
/**
* @Author: jager
* @Email: lhj168os@gmail.com
* @File: consts_test
* @Date: 2022/1/6 2:36
* @package: consts
* @Version: v1.0.0
*
* @Description:
*
*/
package apity
import (
"fmt"
"testing"
)
func TestApiType(t *testing.T) {
var opened int64 = 7
opened = opened & int64(Astro)
//opened = opened | int64(Obscenity)
//opened = opened | int64(Weather)
fmt.Printf("opened = %b\n", opened)
//fmt.Printf("soup type = %d\n", int64(Soup)&opened)
//fmt.Printf("weather type = %b\n", Weather)
//fmt.Printf("obscenity type = %b\n", Obscenity)
}

@ -2,7 +2,7 @@
* @Author: jager * @Author: jager
* @Email: lhj168os@gmail.com * @Email: lhj168os@gmail.com
* @File: consts * @File: consts
* @Date: 2021/12/31 5:20 * @Date: 2022/1/6 6:16
* @package: consts * @package: consts
* @Version: v1.0.0 * @Version: v1.0.0
* *
@ -12,9 +12,7 @@
package consts package consts
type ApiType int64
const ( const (
Soup ApiType = 1 DateFormat = "2006-01-02"
Weather ApiType = 2 TimeFormat = "2006-01-02 15:04:05"
) )

@ -0,0 +1,69 @@
/**
* @Author: jager
* @Email: lhj168os@gmail.com
* @File: almanac
* @Date: 2022/1/6 6:13
* @package: almanac
* @Version: v1.0.0
*
* @Description:
*
*/
package almanac
import (
"github.com/gin-gonic/gin"
"github.com/jageros/hawox/astro"
"github.com/jageros/hawox/errcode"
"github.com/jageros/hawox/httpx"
"idata/internal/apity"
"idata/internal/consts"
"idata/internal/service/user"
"idata/internal/types"
"strconv"
"strings"
"time"
)
func Almanac(ctx *gin.Context) {
uid, ok := httpx.DecodeUrlVal(ctx, "uid")
if !ok {
return
}
var opened bool
err := user.OperateSync(uid, func(u types.RWUser) {
opened = u.ApiCanReq(apity.Astro)
if opened && !u.IsVip() {
u.SetScore(u.Score() - 1)
}
})
if err != nil {
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
return
}
if !opened {
httpx.ErrInterrupt(ctx, errcode.New(1, "未获得此Api权限"))
return
}
date, ok := httpx.DecodeUrlVal(ctx, "date")
if !ok || date == "" {
date = time.Now().Format(consts.DateFormat)
}
strs := strings.Split(date, "_")
var hour = -1
if len(strs) >= 2 {
h, err := strconv.Atoi(strs[1])
if err == nil {
hour = h
}
}
dd := astro.GetDate(strs[0])
data, err := dd.Map(hour)
if err != nil {
httpx.ErrInterrupt(ctx, errcode.WithErrcode(2, err))
return
}
httpx.PkgMsgWrite(ctx, data)
}

@ -14,10 +14,33 @@ package soup
import ( import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jageros/hawox/errcode"
"github.com/jageros/hawox/httpx" "github.com/jageros/hawox/httpx"
"idata/internal/apity"
"idata/internal/service/user"
"idata/internal/types"
) )
func GetRandSoup(ctx *gin.Context) { func GetRandSoup(ctx *gin.Context) {
uid, ok := httpx.DecodeUrlVal(ctx, "uid")
if !ok {
return
}
var opened bool
err := user.OperateSync(uid, func(u types.RWUser) {
opened = u.ApiCanReq(apity.Soup)
if opened && !u.IsVip() {
u.SetScore(u.Score() - 1)
}
})
if err != nil {
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
return
}
if !opened {
httpx.ErrInterrupt(ctx, errcode.New(1, "未获得此Api权限"))
return
}
text := getContent() text := getContent()
httpx.PkgMsgWrite(ctx, map[string]interface{}{"context": text}) httpx.PkgMsgWrite(ctx, map[string]interface{}{"context": text})
} }

@ -42,18 +42,21 @@ type (
} }
Users struct { Users struct {
Id int64 `db:"id"` Id int64 `db:"id"`
Uid string `db:"uid"` // 用户ID Uid string `db:"uid"` // 用户ID
Openid string `db:"openid"` // 微信openid Openid string `db:"openid"` // 微信openid
Secret string `db:"secret"` // secret Secret string `db:"secret"` // secret
Phone string `db:"phone"` // 手机号 Phone string `db:"phone"` // 手机号
Password string `db:"password"` // 密码 Password string `db:"password"` // 密码
Avatar string `db:"avatar"` // 头像url Avatar string `db:"avatar"` // 头像url
Name string `db:"name"` // 昵称 Name string `db:"name"` // 昵称
Gender int64 `db:"gender"` // 性别 Gender int64 `db:"gender"` // 性别
Score int64 `db:"score"` // 金币数量 Score int64 `db:"score"` // 金币数量
CreateTime time.Time `db:"create_time"` Vip int64 `db:"vip"` // VIP等级
UpdateTime time.Time `db:"update_time"` VipExpireTime int64 `db:"vip_expire_time"` // VIP到期时间戳
ApiOpened int64 `db:"api_opened"` // Api开通情况
CreateTime time.Time `db:"create_time"`
UpdateTime time.Time `db:"update_time"`
} }
) )
@ -65,12 +68,12 @@ func NewUsersModel(conn sqlx.SqlConn, c cache.CacheConf) UsersModel {
} }
func (m *defaultUsersModel) Insert(data Users) (sql.Result, error) { func (m *defaultUsersModel) Insert(data Users) (sql.Result, error) {
usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, data.Uid)
usersOpenidKey := fmt.Sprintf("%s%v", cacheUsersOpenidPrefix, data.Openid) usersOpenidKey := fmt.Sprintf("%s%v", cacheUsersOpenidPrefix, data.Openid)
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone) usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, data.Uid)
ret, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) { ret, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, usersRowsExpectAutoSet) query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, usersRowsExpectAutoSet)
return conn.Exec(query, data.Uid, data.Openid, data.Secret, data.Phone, data.Password, data.Avatar, data.Name, data.Gender, data.Score) return conn.Exec(query, data.Uid, data.Openid, data.Secret, data.Phone, data.Password, data.Avatar, data.Name, data.Gender, data.Score, data.Vip, data.VipExpireTime, data.ApiOpened)
}, usersOpenidKey, usersPhoneKey, usersUidKey) }, usersOpenidKey, usersPhoneKey, usersUidKey)
return ret, err return ret, err
} }
@ -153,14 +156,14 @@ func (m *defaultUsersModel) FindOneByUid(uid string) (*Users, error) {
} }
func (m *defaultUsersModel) Update(data Users) error { func (m *defaultUsersModel) Update(data Users) error {
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, data.Id)
usersOpenidKey := fmt.Sprintf("%s%v", cacheUsersOpenidPrefix, data.Openid) usersOpenidKey := fmt.Sprintf("%s%v", cacheUsersOpenidPrefix, data.Openid)
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone) usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, data.Uid) usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, data.Uid)
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, data.Id)
_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) { _, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, usersRowsWithPlaceHolder) query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, usersRowsWithPlaceHolder)
return conn.Exec(query, data.Uid, data.Openid, data.Secret, data.Phone, data.Password, data.Avatar, data.Name, data.Gender, data.Score, data.Id) return conn.Exec(query, data.Uid, data.Openid, data.Secret, data.Phone, data.Password, data.Avatar, data.Name, data.Gender, data.Score, data.Vip, data.VipExpireTime, data.ApiOpened, data.Id)
}, usersUidKey, usersIdKey, usersOpenidKey, usersPhoneKey) }, usersPhoneKey, usersUidKey, usersIdKey, usersOpenidKey)
return err return err
} }
@ -170,14 +173,14 @@ func (m *defaultUsersModel) Delete(id int64) error {
return err return err
} }
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, id)
usersOpenidKey := fmt.Sprintf("%s%v", cacheUsersOpenidPrefix, data.Openid) usersOpenidKey := fmt.Sprintf("%s%v", cacheUsersOpenidPrefix, data.Openid)
usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone) usersPhoneKey := fmt.Sprintf("%s%v", cacheUsersPhonePrefix, data.Phone)
usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, data.Uid) usersUidKey := fmt.Sprintf("%s%v", cacheUsersUidPrefix, data.Uid)
usersIdKey := fmt.Sprintf("%s%v", cacheUsersIdPrefix, id)
_, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) { _, err = m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("delete from %s where `id` = ?", m.table) query := fmt.Sprintf("delete from %s where `id` = ?", m.table)
return conn.Exec(query, id) return conn.Exec(query, id)
}, usersIdKey, usersOpenidKey, usersPhoneKey, usersUidKey) }, usersUidKey, usersIdKey, usersOpenidKey, usersPhoneKey)
return err return err
} }

@ -13,12 +13,13 @@
package user package user
import ( import (
"errors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/jageros/hawox/errcode" "github.com/jageros/hawox/errcode"
"github.com/jageros/hawox/httpx" "github.com/jageros/hawox/httpx"
"github.com/jageros/hawox/jwt" "github.com/jageros/hawox/jwt"
"github.com/jageros/hawox/logx"
"github.com/jageros/hawox/uuid" "github.com/jageros/hawox/uuid"
"idata/internal/apity"
"idata/internal/service/user/dao" "idata/internal/service/user/dao"
"idata/internal/types" "idata/internal/types"
"idata/internal/utils" "idata/internal/utils"
@ -39,8 +40,6 @@ func Register(ctx *gin.Context) {
phone := ph.(string) phone := ph.(string)
password := pwd.(string) password := pwd.(string)
logx.Infof("phone=%s pwd=%s", phone, password)
_, err := userModel.FindOneByPhone(phone) _, err := userModel.FindOneByPhone(phone)
if err == nil { if err == nil {
httpx.ErrInterrupt(ctx, errcode.New(1, "phone has registry")) httpx.ErrInterrupt(ctx, errcode.New(1, "phone has registry"))
@ -75,11 +74,94 @@ func Register(ctx *gin.Context) {
} }
func Login(ctx *gin.Context) { func Login(ctx *gin.Context) {
arg, ok := httpx.BindJsonArgs(ctx)
if !ok {
return
}
ph, ok1 := arg["phone"]
pwd, ok2 := arg["password"]
if !ok1 || !ok2 {
httpx.ErrInterrupt(ctx, errcode.InvalidParam)
return
}
phone := ph.(string)
password := pwd.(string)
var verify bool
var uid string
err := OperateByPhone(phone, func(u types.RUser) {
verify = u.VerifyPwd(password)
uid = u.Uid()
})
if err != nil {
if err == dao.ErrNotFound {
err = errors.New("用户未注册")
}
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
return
}
if !verify {
httpx.ErrInterrupt(ctx, errcode.VerifyErr)
return
}
token, err := jwt.GenerateToken(uid)
if err != nil {
httpx.ErrInterrupt(ctx, errcode.WithErrcode(2, err))
return
}
httpx.PkgMsgWrite(ctx, map[string]interface{}{"uid": uid, "token": token})
} }
func Info(ctx *gin.Context) { func Info(ctx *gin.Context) {
uid, ok := jwt.Uid(ctx)
if !ok {
return
}
var data interface{}
err := Operate(uid, func(u types.RUser) {
data = u.Info()
})
if err != nil {
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
return
}
httpx.PkgMsgWrite(ctx, data)
}
func Secret(ctx *gin.Context) {
uid, ok := jwt.Uid(ctx)
if !ok {
return
}
var secret string
err := Operate(uid, func(u types.RUser) {
secret = u.Secret()
})
if err != nil {
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
return
}
httpx.PkgMsgWrite(ctx, map[string]interface{}{"secret": secret})
}
func OpenApi(ctx *gin.Context) {
uid, ok1 := jwt.Uid(ctx)
apiType, ok2 := httpx.DecodeUrlVal(ctx, "api_type")
if !ok1 || !ok2 {
return
}
err := OperateSync(uid, func(u types.RWUser) {
u.OpenApi(apity.Type(apiType))
})
if err != nil {
httpx.ErrInterrupt(ctx, errcode.WithErrcode(1, err))
return
}
httpx.PkgMsgWrite(ctx, nil)
} }
func Auth(uid, secret string) bool { func Auth(uid, secret string) bool {

@ -19,9 +19,11 @@ import (
"github.com/jageros/hawox/redis" "github.com/jageros/hawox/redis"
"github.com/tal-tech/go-zero/core/stores/cache" "github.com/tal-tech/go-zero/core/stores/cache"
"github.com/tal-tech/go-zero/core/stores/sqlx" "github.com/tal-tech/go-zero/core/stores/sqlx"
"idata/internal/apity"
"idata/internal/service/user/dao" "idata/internal/service/user/dao"
"idata/internal/types" "idata/internal/types"
"sync" "sync"
"time"
) )
var ( var (
@ -59,6 +61,7 @@ func newUser(uid string) (*user, error) {
Phone: uid, Phone: uid,
Openid: uid, Openid: uid,
Secret: uid, Secret: uid,
Score: 100,
Name: fmt.Sprintf("U_%s", uid), Name: fmt.Sprintf("U_%s", uid),
} }
usr := userPool.Get().(user) usr := userPool.Get().(user)
@ -73,6 +76,13 @@ func getRUser(uid string) (user, error) {
return usr, err return usr, err
} }
func getRUserByPhone(phone string) (user, error) {
usr := userPool.Get().(user)
uData, err := userModel.FindOneByPhone(phone)
usr.data = uData
return usr, err
}
func getRWUser(uid string) (*user, error) { func getRWUser(uid string) (*user, error) {
uData, err := userModel.FindOneByUid(uid) uData, err := userModel.FindOneByUid(uid)
if err != nil { if err != nil {
@ -83,7 +93,17 @@ func getRWUser(uid string) (*user, error) {
return &usr, nil return &usr, nil
} }
func OperateSync(uid string, f func(types.RWUser)) error { func getRWUserByPhone(phone string) (*user, error) {
uData, err := userModel.FindOneByPhone(phone)
if err != nil {
return nil, err
}
usr := userPool.Get().(user)
usr.data = uData
return &usr, nil
}
func OperateSync(uid string, f func(u types.RWUser)) error {
var err error var err error
err2 := redis.LockExec(uid, func(key string) { err2 := redis.LockExec(uid, func(key string) {
var u *user var u *user
@ -100,6 +120,23 @@ func OperateSync(uid string, f func(types.RWUser)) error {
return err return err
} }
func OperateSyncByPhone(phone string, f func(u types.RWUser)) error {
u, err := getRWUserByPhone(phone)
defer userPool.Put(*u)
if err != nil {
return err
}
err2 := redis.LockExec(u.data.Uid, func(key string) {
f(u)
err = userModel.Update(*u.data)
})
if err2 != nil {
return err2
}
return err
}
func Operate(uid string, f func(u types.RUser)) error { func Operate(uid string, f func(u types.RUser)) error {
u, err := getRUser(uid) u, err := getRUser(uid)
if err == nil { if err == nil {
@ -109,6 +146,15 @@ func Operate(uid string, f func(u types.RUser)) error {
return err return err
} }
func OperateByPhone(phone string, f func(u types.RUser)) error {
u, err := getRUserByPhone(phone)
if err == nil {
f(u)
}
userPool.Put(u)
return err
}
func RegistryUser(uid string, f func(u types.RWUser)) error { func RegistryUser(uid string, f func(u types.RWUser)) error {
var err error var err error
err2 := redis.LockExec(uid, func(key string) { err2 := redis.LockExec(uid, func(key string) {
@ -166,6 +212,42 @@ func (u user) Avatar() string {
return u.data.Avatar return u.data.Avatar
} }
func (u user) VerifyPwd(pwd string) bool {
return u.data.Password == encrypt.Md5(pwd)
}
func (u user) ApiOpened(ty apity.ApiType) bool {
return int64(ty)&u.data.ApiOpened > 0
}
func (u user) ApiCanReq(ty apity.ApiType) bool {
opened := u.ApiOpened(ty)
if !opened {
return false
}
if u.IsVip() {
return true
}
return u.data.Score > 0
}
func (u user) IsVip() bool {
return u.data.Vip > 0 && u.data.VipExpireTime > time.Now().Unix()
}
func (u user) Info() map[string]interface{} {
return map[string]interface{}{
"uid": u.Uid(),
"name": u.Name(),
"gender": u.Gender(),
"avatar": u.Avatar(),
"phone": u.Phone(),
"score": u.Score(),
"vip": u.data.Vip,
"vip_expire_time": u.data.VipExpireTime,
}
}
// ========== end ========== // ========== end ==========
// ====== Types.RWUser 实现 ====== // ====== Types.RWUser 实现 ======
@ -202,4 +284,8 @@ func (u *user) SetPassword(pwd string) {
u.data.Password = encrypt.Md5(pwd) u.data.Password = encrypt.Md5(pwd)
} }
func (u *user) OpenApi(ty apity.ApiType) {
u.data.ApiOpened = u.data.ApiOpened | int64(ty)
}
// ========== end ========== // ========== end ==========

@ -7,18 +7,21 @@ SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `users`; DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` CREATE TABLE `users`
( (
`id` bigint unsigned NOT NULL AUTO_INCREMENT, `id` bigint unsigned NOT NULL AUTO_INCREMENT,
`uid` varchar(127) NOT NULL COMMENT '用户ID', `uid` varchar(127) NOT NULL COMMENT '用户ID',
`openid` varchar(255) NOT NULL COMMENT '微信openid', `openid` varchar(255) NOT NULL COMMENT '微信openid',
`secret` varchar(255) NOT NULL COMMENT 'secret', `secret` varchar(255) NOT NULL COMMENT 'secret',
`phone` varchar(63) NOT NULL COMMENT '手机号', `phone` varchar(63) NOT NULL COMMENT '手机号',
`password` varchar(255) NOT NULL DEFAULT '' COMMENT '密码', `password` varchar(255) NOT NULL DEFAULT '' COMMENT '密码',
`avatar` varchar(255) NOT NULL DEFAULT '' COMMENT '头像url', `avatar` varchar(255) NOT NULL DEFAULT '' COMMENT '头像url',
`name` varchar(255) NOT NULL DEFAULT '' COMMENT '昵称', `name` varchar(255) NOT NULL DEFAULT '' COMMENT '昵称',
`gender` int NOT NULL DEFAULT 0 COMMENT '性别', `gender` int NOT NULL DEFAULT 0 COMMENT '性别',
`score` int NOT NULL DEFAULT 0 COMMENT '金币数量', `score` int NOT NULL DEFAULT 0 COMMENT '金币数量',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP, `vip` int NOT NULL DEFAULT 0 COMMENT 'VIP等级',
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `vip_expire_time` bigint NOT NULL DEFAULT 0 COMMENT 'VIP到期时间戳',
`api_opened` bigint NOT NULL DEFAULT 0 COMMENT 'Api开通情况',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`) USING BTREE, PRIMARY KEY (`id`) USING BTREE,
UNIQUE KEY `uid_unique` (`uid`) USING BTREE, UNIQUE KEY `uid_unique` (`uid`) USING BTREE,
UNIQUE KEY `phone_unique` (`phone`) USING BTREE, UNIQUE KEY `phone_unique` (`phone`) USING BTREE,
@ -32,4 +35,6 @@ CREATE TABLE `users`
BEGIN; BEGIN;
COMMIT; COMMIT;
SET FOREIGN_KEY_CHECKS = 1; SET FOREIGN_KEY_CHECKS = 1;
-- goctl model mysql datasource -url="root:Hawtech^666@tcp(gz-cynosdbmysql-grp-niaolcgt.sql.tencentcdb.com:26529)/idata" -table="*" -dir="./dao" -c

@ -12,6 +12,8 @@
package types package types
import "idata/internal/apity"
type RUser interface { type RUser interface {
Uid() string Uid() string
Openid() string Openid() string
@ -21,6 +23,11 @@ type RUser interface {
Gender() int64 Gender() int64
Avatar() string Avatar() string
Score() int64 Score() int64
IsVip() bool
VerifyPwd(pwd string) bool
ApiOpened(ty apity.ApiType) bool
ApiCanReq(ty apity.ApiType) bool
Info() map[string]interface{}
} }
type RWUser interface { type RWUser interface {
@ -33,4 +40,5 @@ type RWUser interface {
SetGender(gender int64) SetGender(gender int64)
SetScore(score int64) SetScore(score int64)
SetPassword(pwd string) SetPassword(pwd string)
OpenApi(ty apity.ApiType)
} }

@ -15,9 +15,14 @@ package utils
import ( import (
"fmt" "fmt"
"github.com/jageros/hawox/encrypt" "github.com/jageros/hawox/encrypt"
"math/rand"
"time" "time"
) )
func GenSecret(uid string) string { func GenSecret(uid string) string {
return encrypt.Md5(fmt.Sprintf("%s%d", uid, time.Now().Unix())) return encrypt.Sha1(fmt.Sprintf("%s%d%d", uid, time.Now().Unix(), rand.Intn(99999999)+100))
}
func GenSecretMd5(uid string) string {
return encrypt.Md5(fmt.Sprintf("%s%d%d", uid, time.Now().Unix(), rand.Intn(99999999)+100))
} }

@ -0,0 +1,23 @@
/**
* @Author: jager
* @Email: lhj168os@gmail.com
* @File: utils_test
* @Date: 2022/1/6 5:00
* @package: utils
* @Version: v1.0.0
*
* @Description:
*
*/
package utils
import (
"fmt"
"testing"
)
func TestGenSecret(t *testing.T) {
fmt.Println("Sha1: " + GenSecret("1000"))
fmt.Println("MD5: " + GenSecretMd5("1000"))
}
Loading…
Cancel
Save