mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-28 15:05:01 +08:00
This commit is contained in:
@@ -3,7 +3,6 @@
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package common
|
||||
|
||||
import (
|
||||
@@ -59,9 +58,17 @@ func (s *sCommonUpload) UploadFile(ctx context.Context, file *ghttp.UploadFile)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = f.GetFileType(meta.Ext)
|
||||
if _, err = f.GetFileType(meta.Ext); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
result, err = s.HasFile(ctx, meta.Md5)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
if result != nil {
|
||||
return
|
||||
}
|
||||
|
||||
conf, err := service.SysConfig().GetUpload(ctx)
|
||||
@@ -98,11 +105,22 @@ func (s *sCommonUpload) UploadImage(ctx context.Context, file *ghttp.UploadFile)
|
||||
}
|
||||
|
||||
if !f.IsImgType(meta.Ext) {
|
||||
return nil, gerror.New("上传的文件不是图片")
|
||||
err = gerror.New("上传的文件不是图片")
|
||||
return
|
||||
}
|
||||
|
||||
if meta.Size > 2*1024*1024 {
|
||||
return nil, gerror.New("图片大小不能超过2MB")
|
||||
err = gerror.New("图片大小不能超过2MB")
|
||||
return
|
||||
}
|
||||
|
||||
result, err = s.HasFile(ctx, meta.Md5)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if result != nil {
|
||||
return
|
||||
}
|
||||
|
||||
conf, err := service.SysConfig().GetUpload(ctx)
|
||||
@@ -122,16 +140,13 @@ func (s *sCommonUpload) UploadImage(ctx context.Context, file *ghttp.UploadFile)
|
||||
case consts.UploadDriveQiNiu:
|
||||
return s.UploadQiNiu(ctx, conf, file, meta)
|
||||
default:
|
||||
return nil, gerror.Newf("暂不支持上传驱动:%v", conf.Drive)
|
||||
err = gerror.Newf("暂不支持上传驱动:%v", conf.Drive)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// UploadLocal 上传本地
|
||||
func (s *sCommonUpload) UploadLocal(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
|
||||
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
value = g.Cfg().MustGet(ctx, "server.serverRoot")
|
||||
nowDate = time.Now().Format("2006-01-02")
|
||||
@@ -158,7 +173,7 @@ func (s *sCommonUpload) UploadLocal(ctx context.Context, conf *model.UploadConfi
|
||||
|
||||
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveLocal)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
|
||||
@@ -171,10 +186,6 @@ func (s *sCommonUpload) UploadLocal(ctx context.Context, conf *model.UploadConfi
|
||||
|
||||
// UploadUCloud 上传UCloud对象存储
|
||||
func (s *sCommonUpload) UploadUCloud(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
|
||||
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if conf.UCloudPath == "" {
|
||||
err = gerror.New("UCloud存储驱动必须配置存储路径!")
|
||||
return
|
||||
@@ -194,26 +205,26 @@ func (s *sCommonUpload) UploadUCloud(ctx context.Context, conf *model.UploadConf
|
||||
Endpoint: conf.UCloudEndpoint,
|
||||
VerifyUploadMD5: false,
|
||||
}
|
||||
|
||||
req, err := ufile.NewFileRequest(config, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
// 流式上传本地小文件
|
||||
f2, err := file.Open()
|
||||
defer func() {
|
||||
_ = f2.Close()
|
||||
}()
|
||||
defer f2.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
if err = req.IOPut(f2, fullPath, ""); err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveUCloud)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
|
||||
@@ -226,10 +237,6 @@ func (s *sCommonUpload) UploadUCloud(ctx context.Context, conf *model.UploadConf
|
||||
|
||||
// UploadCOS 上传腾讯云对象存储
|
||||
func (s *sCommonUpload) UploadCOS(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
|
||||
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if conf.CosPath == "" {
|
||||
err = gerror.New("COS存储驱动必须配置存储路径!")
|
||||
return
|
||||
@@ -243,11 +250,9 @@ func (s *sCommonUpload) UploadCOS(ctx context.Context, conf *model.UploadConfig,
|
||||
|
||||
// 流式上传本地小文件
|
||||
f2, err := file.Open()
|
||||
defer func() {
|
||||
_ = f2.Close()
|
||||
}()
|
||||
defer f2.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
u, _ := url.Parse(conf.CosBucketURL)
|
||||
@@ -259,14 +264,13 @@ func (s *sCommonUpload) UploadCOS(ctx context.Context, conf *model.UploadConfig,
|
||||
},
|
||||
})
|
||||
|
||||
_, err = c.Object.Put(ctx, fullPath, f2, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if _, err = c.Object.Put(ctx, fullPath, f2, nil); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveCos)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
|
||||
@@ -279,10 +283,6 @@ func (s *sCommonUpload) UploadCOS(ctx context.Context, conf *model.UploadConfig,
|
||||
|
||||
// UploadOSS 上传阿里云云对象存储
|
||||
func (s *sCommonUpload) UploadOSS(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
|
||||
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if conf.OssPath == "" {
|
||||
err = gerror.New("OSS存储驱动必须配置存储路径!")
|
||||
return
|
||||
@@ -296,30 +296,28 @@ func (s *sCommonUpload) UploadOSS(ctx context.Context, conf *model.UploadConfig,
|
||||
|
||||
// 流式上传本地小文件
|
||||
f2, err := file.Open()
|
||||
defer func() {
|
||||
_ = f2.Close()
|
||||
}()
|
||||
defer f2.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
client, err := oss.New(conf.OssEndpoint, conf.OssSecretId, conf.OssSecretKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
bucket, err := client.Bucket(conf.OssBucket)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
if err = bucket.PutObject(fullPath, f2); err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveOss)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
|
||||
@@ -332,10 +330,6 @@ func (s *sCommonUpload) UploadOSS(ctx context.Context, conf *model.UploadConfig,
|
||||
|
||||
// UploadQiNiu 上传七牛云对象存储
|
||||
func (s *sCommonUpload) UploadQiNiu(ctx context.Context, conf *model.UploadConfig, file *ghttp.UploadFile, meta *sysin.UploadFileMeta) (result *sysin.AttachmentListModel, err error) {
|
||||
if ok, err1 := s.HasFile(ctx, meta.Md5); ok || err1 != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if conf.QiNiuPath == "" {
|
||||
err = gerror.New("七牛云存储驱动必须配置存储路径!")
|
||||
return
|
||||
@@ -349,11 +343,9 @@ func (s *sCommonUpload) UploadQiNiu(ctx context.Context, conf *model.UploadConfi
|
||||
|
||||
// 流式上传本地小文件
|
||||
f2, err := file.Open()
|
||||
defer func() {
|
||||
_ = f2.Close()
|
||||
}()
|
||||
defer f2.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
putPolicy := storage.PutPolicy{
|
||||
@@ -362,10 +354,13 @@ func (s *sCommonUpload) UploadQiNiu(ctx context.Context, conf *model.UploadConfi
|
||||
token := putPolicy.UploadToken(qbox.NewMac(conf.QiNiuAccessKey, conf.QiNiuSecretKey))
|
||||
|
||||
cfg := storage.Config{}
|
||||
|
||||
// 是否使用https域名
|
||||
cfg.UseHTTPS = true
|
||||
|
||||
// 上传是否使用CDN上传加速
|
||||
cfg.UseCdnDomains = false
|
||||
|
||||
// 空间对应的机房
|
||||
cfg.Region, err = storage.GetRegion(conf.QiNiuAccessKey, conf.QiNiuBucket)
|
||||
if err != nil {
|
||||
@@ -378,7 +373,7 @@ func (s *sCommonUpload) UploadQiNiu(ctx context.Context, conf *model.UploadConfi
|
||||
|
||||
attachment, err := service.SysAttachment().Add(ctx, meta, fullPath, consts.UploadDriveQiNiu)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return
|
||||
}
|
||||
|
||||
attachment.FileUrl = s.LastUrl(ctx, conf, attachment.FileUrl, attachment.Drive)
|
||||
@@ -412,13 +407,9 @@ func (s *sCommonUpload) LastUrl(ctx context.Context, conf *model.UploadConfig, f
|
||||
}
|
||||
|
||||
// HasFile 文件是否存在
|
||||
func (s *sCommonUpload) HasFile(ctx context.Context, md5 string) (bool, error) {
|
||||
result, err := dao.SysAttachment.GetMd5File(ctx, md5)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return result != nil, nil
|
||||
func (s *sCommonUpload) HasFile(ctx context.Context, md5 string) (res *sysin.AttachmentListModel, err error) {
|
||||
res, err = dao.SysAttachment.GetMd5File(ctx, md5)
|
||||
return
|
||||
}
|
||||
|
||||
// fileMeta 上传文件元数据
|
||||
|
156
server/internal/logic/common/wechat.go
Normal file
156
server/internal/logic/common/wechat.go
Normal file
@@ -0,0 +1,156 @@
|
||||
// Package common
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2023 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gcron"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/util/gmeta"
|
||||
"hotgo/api/admin/common"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/cache"
|
||||
"hotgo/internal/library/contexts"
|
||||
"hotgo/internal/library/jwt"
|
||||
"hotgo/internal/library/response"
|
||||
"hotgo/internal/library/wechat"
|
||||
"hotgo/internal/model/input/commonin"
|
||||
"hotgo/internal/service"
|
||||
"time"
|
||||
)
|
||||
|
||||
type sCommonWechat struct {
|
||||
temp map[string]*AuthorizeCallState
|
||||
}
|
||||
|
||||
// AuthorizeCallState 微信授权回调参数
|
||||
type AuthorizeCallState struct {
|
||||
State string `json:"state" dc:"state"`
|
||||
MemberId int64 `json:"memberId" dc:"管理员ID"`
|
||||
Type string `json:"type" dc:"授权类型"`
|
||||
SyncRedirect string `json:"syncRedirect" dc:"同步跳转地址"`
|
||||
Tick *gtime.Time `json:"tick" dc:"标记授权时间"`
|
||||
}
|
||||
|
||||
func NewCommonWechat() *sCommonWechat {
|
||||
return &sCommonWechat{
|
||||
temp: make(map[string]*AuthorizeCallState),
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
serv := NewCommonWechat()
|
||||
service.RegisterCommonWechat(serv)
|
||||
gcron.Add(gctx.New(), "@every 300s", serv.CleanTempMap, "WechatCleanTempMap")
|
||||
}
|
||||
|
||||
// Authorize 微信用户授权
|
||||
func (s *sCommonWechat) Authorize(ctx context.Context, in commonin.WechatAuthorizeInp) (res *commonin.WechatAuthorizeModel, err error) {
|
||||
basic, err := service.SysConfig().GetBasic(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
request = g.RequestFromCtx(ctx)
|
||||
prefix = g.Cfg().MustGet(ctx, "router.admin.prefix", "/admin").String()
|
||||
path = gmeta.Get(common.WechatAuthorizeCallReq{}, "path").String()
|
||||
redirectUri = basic.Domain + prefix + path
|
||||
memberId = contexts.GetUserId(ctx)
|
||||
state = s.GetCacheKey(in.Type, jwt.GenAuthKey(jwt.GetAuthorization(request)))
|
||||
scope string
|
||||
)
|
||||
|
||||
if memberId <= 0 {
|
||||
err = gerror.New("获取用户信息失败!")
|
||||
return
|
||||
}
|
||||
|
||||
switch in.Type {
|
||||
case consts.WechatAuthorizeOpenId: // 设置openid
|
||||
scope = consts.WechatScopeBase
|
||||
case consts.WechatAuthorizeBindLogin: // 绑定微信登录
|
||||
scope = consts.WechatScopeUserinfo
|
||||
default:
|
||||
err = gerror.New("无效的授权方式!")
|
||||
return
|
||||
}
|
||||
|
||||
url, err := wechat.GetOauthURL(ctx, redirectUri, scope, state)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
s.temp[state] = &AuthorizeCallState{
|
||||
State: state,
|
||||
MemberId: memberId,
|
||||
Type: in.Type,
|
||||
SyncRedirect: in.SyncRedirect,
|
||||
Tick: gtime.Now(),
|
||||
}
|
||||
response.Redirect(g.RequestFromCtx(ctx), url)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sCommonWechat) AuthorizeCall(ctx context.Context, in commonin.WechatAuthorizeCallInp) (res *commonin.WechatAuthorizeCallModel, err error) {
|
||||
data, ok := s.temp[in.State]
|
||||
if !ok || data == nil {
|
||||
err = gerror.New("授权无效或已过期,请重试")
|
||||
return
|
||||
}
|
||||
|
||||
defer delete(s.temp, in.State)
|
||||
|
||||
token, err := wechat.GetUserAccessToken(ctx, in.Code)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
switch data.Type {
|
||||
case consts.WechatAuthorizeOpenId: // 设置openid
|
||||
cache.Instance().Set(ctx, data.State, token.OpenID, time.Hour*24*7)
|
||||
case consts.WechatAuthorizeBindLogin: // 绑定微信登录
|
||||
// ...
|
||||
default:
|
||||
err = gerror.New("无效的授权方式!")
|
||||
return
|
||||
}
|
||||
|
||||
response.Redirect(g.RequestFromCtx(ctx), data.SyncRedirect)
|
||||
return
|
||||
}
|
||||
|
||||
// GetOpenId 从缓存中获取临时openid
|
||||
func (s *sCommonWechat) GetOpenId(ctx context.Context) (openId string, err error) {
|
||||
request := ghttp.RequestFromCtx(ctx)
|
||||
key := s.GetCacheKey(consts.WechatAuthorizeOpenId, jwt.GenAuthKey(jwt.GetAuthorization(request)))
|
||||
date, err := cache.Instance().Get(ctx, key)
|
||||
if err != nil {
|
||||
err = gerror.Newf("GetOpenId err:%+v", err.Error())
|
||||
return
|
||||
}
|
||||
openId = date.String()
|
||||
return
|
||||
}
|
||||
|
||||
func (s *sCommonWechat) GetCacheKey(typ, ak string) string {
|
||||
return fmt.Sprintf("%v:%v", typ, ak)
|
||||
}
|
||||
|
||||
// CleanTempMap 清理临时map
|
||||
func (s *sCommonWechat) CleanTempMap(ctx context.Context) {
|
||||
t := gtime.Now().Add(time.Second * 600)
|
||||
for _, state := range s.temp {
|
||||
if state.Tick.After(t) {
|
||||
delete(s.temp, state.State)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user