rubbishclass-srv/app/app-service.go

66 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package app
import (
"io"
"mime/multipart"
"os"
"strings"
"github.com/silenceper/wechat"
"github.com/silenceper/wechat/cache"
"github.com/silenceper/wechat/miniprogram"
)
var (
wxa *miniprogram.MiniProgram
//RedisHost redis地址用于小程序缓存DB号10
RedisHost *string
)
//init先于主程序运行如何使配置生效
func init() {
// memCache := cache.NewRedis(&cache.RedisOpts{
// Host: *RedisHost,
// Database: 10,
// MaxIdle: 2,
// MaxActive: 50,
// IdleTimeout: 60,
// })
mem := cache.NewMemory()
appConfig := &wechat.Config{
AppID: "wx49cced01eec31847",
AppSecret: "0090134e4a137554a271133d7f73e633",
Cache: mem,
}
wx := wechat.NewWechat(appConfig)
wxa = wx.GetMiniProgram()
}
//SaveVideoFileService 文件上传服务
func SaveVideoFileService(file *multipart.FileHeader, openid *string) (*string, error) {
p := "./video/" + *openid
_, err := os.Stat(p)
if err != nil {
if os.IsNotExist(err) {
os.Mkdir(p, os.ModePerm)
os.Chmod(p, 0755)
}
}
src, err := file.Open()
if err != nil {
return nil, err
}
defer src.Close()
//创建 dst 文件
fn := strings.TrimLeft(file.Filename, "tmp_")
out, err := os.Create(p + `/` + fn)
if err != nil {
return nil, err
}
defer out.Close()
// 拷贝文件
_, err = io.Copy(out, src)
filename := out.Name()
return &filename, err
}