66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
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
|
||
}
|