feat:完善项目

This commit is contained in:
henry
2021-09-28 11:47:19 +08:00
commit da7b3130fe
167 changed files with 456676 additions and 0 deletions

42
lib/config.go Normal file
View File

@ -0,0 +1,42 @@
package lib
import (
"encoding/json"
"io/ioutil"
"path"
"gopkg.in/yaml.v2"
)
type callback func(interface{})
var loaders = map[string]func([]byte, interface{}) error{
".json": LoadConfigFormJsonBytes,
".yaml": LoadConfigFromYamlBytes,
}
func LoadConfigFormJsonBytes(content []byte, obj interface{}) error {
return json.Unmarshal(content, obj)
}
func LoadConfigFromYamlBytes(content []byte, obj interface{}) error {
return yaml.Unmarshal(content, obj)
}
func LoadConfig(file string, v interface{}, callback ...callback) {
content, err := ioutil.ReadFile(file)
if err != nil {
panic("Load Config Error " + err.Error())
}
loader, ok := loaders[path.Ext(file)]
if !ok {
panic("Unknown File Type" + path.Ext(file))
}
if err = loader(content, v); err == nil {
for _, _callback := range callback {
_callback(v)
}
}
}

36
lib/email.go Normal file
View File

@ -0,0 +1,36 @@
package lib
import (
"gopkg.in/gomail.v2"
)
type Email struct {
*EmailOption
}
type EmailOption struct {
User, Auth string // Auth定义邮箱服务器连接信息如果是阿里邮箱 Auth填密码qq邮箱填授权码
Host string
Port int
}
type EmailSendHandle func(objects []string, subject, body string) error
func (this *Email) Init() {
}
func (this *Email) Send() EmailSendHandle {
return func(objects []string, subject, body string) error {
m := gomail.NewMessage()
m.SetHeader("From", "XD Game"+"<"+this.User+">") //这种方式可以添加别名即“XD Game” 也可以直接用<code>m.SetHeader("From",mailConn["user"])</code> 读者可以自行实验下效果
m.SetHeader("To", objects...) //发送给多个用户
m.SetHeader("Subject", subject) //设置邮件主题
m.SetBody("text/html", body) //设置邮件正文
return gomail.NewDialer(this.Host, this.Port, this.User, this.Auth).DialAndSend(m)
}
}
func NewEmail(option *EmailOption) *Email {
return &Email{option}
}

44
lib/email_test.go Normal file
View File

@ -0,0 +1,44 @@
package lib
import (
"strconv"
"testing"
"gopkg.in/gomail.v2"
)
func SendMail(mailTo []string, subject string, body string) error {
//定义邮箱服务器连接信息,如果是阿里邮箱 pass填密码qq邮箱填授权码
mailConn := map[string]string{
"user": "postmaster@ipeace.org.cn",
"pass": "Email@henry!",
"host": "smtp.qiye.aliyun.com",
"port": "465",
}
port, _ := strconv.Atoi(mailConn["port"]) //转换端口类型为int
m := gomail.NewMessage()
m.SetHeader("From", "XD Game"+"<"+mailConn["user"]+">") //这种方式可以添加别名即“XD Game” 也可以直接用<code>m.SetHeader("From",mailConn["user"])</code> 读者可以自行实验下效果
m.SetHeader("To", mailTo...) //发送给多个用户
m.SetHeader("Subject", subject) //设置邮件主题
m.SetBody("text/html", body) //设置邮件正文
d := gomail.NewDialer(mailConn["host"], port, mailConn["user"], mailConn["pass"])
err := d.DialAndSend(m)
return err
}
func TestNewEmail(t *testing.T) {
//定义收件人
mailTo := []string{
"592856124@qq.com",
}
//邮件主题为"Hello"
subject := "Hello"
// 邮件正文
body := "Good"
err := SendMail(mailTo, subject, body)
t.Log(err)
}

97
lib/upload.go Normal file
View File

@ -0,0 +1,97 @@
package lib
import (
"SciencesServer/utils"
"errors"
"fmt"
"mime/multipart"
"path/filepath"
"strings"
"time"
)
type (
UploadConfig struct {
Path string // 根目录,完整目录-更目录+时间
Ext []string // 文件类型
Size int64 // 文件大小
Rename bool // 重命名
}
UploadHandle struct {
Url string `json:"url"`
Filepath string `json:"filepath"`
Filename string `json:"filename"`
RelativePath string `json:"-"`
}
)
type (
Handle func(file *multipart.FileHeader, domain string) (*UploadHandle, error)
)
func (this *UploadConfig) ext(ext string) bool {
if !utils.InArray(strings.ToLower(ext), this.Ext) {
return false
}
return true
}
func (this *UploadConfig) size(size int64) bool {
if size > this.Size {
return false
}
return true
}
func (this *UploadConfig) Handle() Handle {
return func(file *multipart.FileHeader, domain string) (handle *UploadHandle, e error) {
ext := filepath.Ext(file.Filename)
if !this.ext(strings.Replace(ext, ".", "", 1)) {
return nil, errors.New("文件类型限制不可上传")
}
if !this.size(file.Size) {
return nil, errors.New("文件过大不可上传")
}
// 判断目录是否存在
now := time.Now()
this.Path += now.Format("20060102") + "/"
isExist, err := utils.PathExists(this.Path)
if err != nil {
return nil, err
}
if !isExist {
if err = utils.MkdirAll(this.Path); err != nil {
return nil, err
}
}
_file := file.Filename
if this.Rename {
file.Filename = utils.Md5String(file.Filename, fmt.Sprintf("%d", now.UnixNano()))
_file = file.Filename + ext
}
_filepath := "/" + this.Path + _file
return &UploadHandle{
Url: domain + _filepath,
Filepath: _filepath,
Filename: _file,
RelativePath: this.Path + _file,
}, nil
}
}
func Upload(path string, ext []string, size int64, rename bool) *UploadConfig {
return &UploadConfig{
Path: path,
Ext: ext,
Size: size,
Rename: rename,
}
}