This commit is contained in:
henry
2021-11-02 09:43:19 +08:00
parent 570bb3c772
commit 4734344985
78 changed files with 4798 additions and 0 deletions

56
app/event/redis.go Normal file
View File

@ -0,0 +1,56 @@
package event
import (
"ArmedPolice/serve/cache"
"fmt"
)
type (
RedisHashProduce struct{}
RedisHashDestroy struct{}
)
type (
RedisListProduce struct{}
RedisListDestroy struct{}
)
func (this *RedisHashProduce) Handle(args ...interface{}) {
_ = cache.Cache.HSet(args[0].(string), args[1].(string), args[2])
}
func NewRedisHashProduce() *RedisHashProduce {
return &RedisHashProduce{}
}
func (this *RedisHashDestroy) Handle(args ...interface{}) {
fields := make([]string, 0)
for i := 1; i < len(args); i++ {
fields = append(fields, args[i].(string))
}
_ = cache.Cache.HDel(args[0].(string), fields...)
}
func NewRedisHashDestroy() *RedisHashDestroy {
return &RedisHashDestroy{}
}
func (this *RedisListProduce) Handle(args ...interface{}) {
_ = cache.Cache.SAdd(args[0].(string), args[1:]...)
}
func NewRedisListProduce() *RedisListProduce {
return &RedisListProduce{}
}
func (this *RedisListDestroy) Handle(args ...interface{}) {
err := cache.Cache.SRem(args[0].(string), args[1:]...)
fmt.Println(err)
}
func NewRedisListDestroy() *RedisListDestroy {
return &RedisListDestroy{}
}

16
app/init.go Normal file
View File

@ -0,0 +1,16 @@
package app
import (
"Edu/app/event"
"Edu/app/service"
"Edu/config"
)
func Init() {
// RedisHash存储/移除监听
service.Subscribe(config.EventForRedisHashProduce, event.NewRedisHashProduce())
service.Subscribe(config.EventForRedisHashDestroy, event.NewRedisHashDestroy())
// RedisList存储/移除监听
service.Subscribe(config.EventForRedisListProduce, event.NewRedisListProduce())
service.Subscribe(config.EventForRedisListDestroy, event.NewRedisListDestroy())
}

106
app/service/captcha.go Normal file
View File

@ -0,0 +1,106 @@
package service
import (
"errors"
"github.com/mojocn/base64Captcha"
)
type Param struct {
obj interface{}
}
type CaptchaInfo struct {
Key string `json:"key"`
Captcha string `json:"captcha"`
}
type CaptchaType string
const (
CaptchaTypeForDefault CaptchaType = "default"
CaptchaTypeForAudio CaptchaType = "audio"
CaptchaTypeForString CaptchaType = "string"
CaptchaTypeForMath CaptchaType = "math"
CaptchaTypeForChinese CaptchaType = "chinese"
)
var drivers = map[CaptchaType]func() base64Captcha.Driver{
CaptchaTypeForDefault: captchaForDigit,
CaptchaTypeForAudio: captchaForAudio,
CaptchaTypeForString: captchaForString,
CaptchaTypeForMath: captchaForMath,
CaptchaTypeForChinese: captchaForChinese,
}
var store = base64Captcha.DefaultMemStore
func captchaForDigit() base64Captcha.Driver {
drive := new(base64Captcha.DriverDigit)
drive.Width = 100
drive.Height = 40
drive.DotCount = 80
drive.Length = 6
drive.MaxSkew = 0.7
return drive
}
func captchaForAudio() base64Captcha.Driver {
drive := new(base64Captcha.DriverAudio)
drive.Language = "zh"
drive.Length = 4
return drive
}
func captchaForString() base64Captcha.Driver {
drive := new(base64Captcha.DriverString)
drive.Width = 100
drive.Width = 40
drive.Source = "设想,你在,处理,消费者,的音,频输,出音,频可,能无,论什,么都,没有,任何,输出,或者,它可,能是,单声道,立体声,或是,环绕立,体声的,不想要,的值"
drive.Length = 2
return drive.ConvertFonts()
}
func captchaForMath() base64Captcha.Driver {
drive := new(base64Captcha.DriverMath)
drive.Width = 100
drive.Height = 40
drive.NoiseCount = 1
return drive.ConvertFonts()
}
func captchaForChinese() base64Captcha.Driver {
drive := new(base64Captcha.DriverChinese)
drive.Width = 100
drive.Width = 40
drive.Source = "1234567890qwertyuioplkjhgfdsazxcvbnm"
drive.Length = 4
return drive.ConvertFonts()
}
func (this *Param) Create() (*CaptchaInfo, error) {
obj := this.obj.(CaptchaType)
drive, has := drivers[obj]
if !has {
return nil, errors.New("参数异常")
}
c := base64Captcha.NewCaptcha(drive(), store)
key, captcha, err := c.Generate()
if err != nil {
return nil, err
}
return &CaptchaInfo{Key: key, Captcha: captcha}, nil
}
func (this *Param) Validate() bool {
obj := this.obj.(*CaptchaInfo)
return store.Verify(obj.Key, obj.Captcha, true)
}
func Captcha(obj interface{}) *Param {
return &Param{obj: obj}
}

56
app/service/eventhub.go Normal file
View File

@ -0,0 +1,56 @@
package service
import (
"sync"
)
// listener 监听
type listener struct {
handler ListenerHandle
}
// listeners 监听器
type listeners struct {
listener map[interface{}]*listener
mutex *sync.RWMutex
}
// ListenerHandle 监听事件
type ListenerHandle interface {
Handle(args ...interface{})
}
// listenerObject 监听器
var listenerObject = &listeners{
listener: make(map[interface{}]*listener, 0),
mutex: new(sync.RWMutex),
}
// Subscribe 注册事件
func Subscribe(prefix interface{}, handler ListenerHandle) {
listenerObject.mutex.Lock()
defer listenerObject.mutex.Unlock()
listenerObject.listener[prefix] = &listener{handler: handler}
}
// Publish 推送事件
func Publish(prefix interface{}, args ...interface{}) {
listenerObject.mutex.RLock()
defer listenerObject.mutex.RUnlock()
listener, has := listenerObject.listener[prefix]
if !has {
return
}
listener.handler.Handle(args...)
}
// Unsubscribe 取消监听
func Unsubscribe(prefix interface{}) {
listenerObject.mutex.Lock()
defer listenerObject.mutex.Unlock()
_, has := listenerObject.listener[prefix]
if !has {
return
}
delete(listenerObject.listener, prefix)
}

30
app/service/session.go Normal file
View File

@ -0,0 +1,30 @@
package service
import (
"Edu/utils"
"encoding/json"
)
type Session struct {
ID uint64 `json:"id"` // 用户ID
OpenID string `json:"open_id"`
Token string `json:"token"` // token
Name string `json:"name"` // 名称
SessionKey string `json:"session_key"` // 用户信息
}
func (this *Session) MarshalBinary() ([]byte, error) {
return json.Marshal(this)
}
func (this *Session) UnmarshalBinary(data []byte) error {
return utils.FromJSONBytes(data, this)
}
func (this *Session) IDToString() string {
return utils.UintToString(this.ID)
}
func NewSession() *Session {
return &Session{}
}