init
This commit is contained in:
106
app/service/captcha.go
Normal file
106
app/service/captcha.go
Normal 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
56
app/service/eventhub.go
Normal 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
30
app/service/session.go
Normal 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{}
|
||||
}
|
Reference in New Issue
Block a user