开始业务编写

This commit is contained in:
suguo.yao 2020-03-24 20:33:14 +08:00
parent 045319fca6
commit 4ffee0e6db
7 changed files with 161 additions and 183 deletions

View File

@ -1,6 +1,18 @@
package admin
import "github.com/gin-gonic/gin"
import (
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/prometheus/common/log"
"yyjishu.com/rubbish-class/video"
)
func VideoStatHandler(c *gin.Context) {
@ -13,3 +25,92 @@ func VideoListHandler(c *gin.Context) {
func VideoDownloadHandler(c *gin.Context) {
}
//HistoryVideoHandle 视频(文件)下载
func HistoryVideoHandler(c *gin.Context) {
openid := c.Param("openid")
p := "./video/" + openid
files, err := ioutil.ReadDir(p)
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error())
return
}
var videos []string
for _, f := range files {
if f.IsDir() {
continue
}
if !strings.HasSuffix(f.Name(), ".mp4") {
continue
}
videos = append(videos, f.Name())
}
c.JSON(http.StatusOK, gin.H{
"videos": videos,
})
}
type newForm struct {
File *multipart.FileHeader `form:"file"`
Openid string `form:"openid"`
}
//UploadfileAndFormHandle 上传带form
func UploadfileAndFormHandler(c *gin.Context) {
var data newForm
if err := c.ShouldBind(&data); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
video := &video.Video{}
filepath, err := video.Upload(data.File)
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": gin.H{
"filepath": filepath,
"openid": data.Openid,
},
})
}
//UploadHandle 小程序中文件上传,正式环境中使用云存储
func UploadHandler(c *gin.Context) {
header, err := c.FormFile("file")
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
dst := header.Filename
src, err := header.Open()
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
defer src.Close()
out, err := os.Create(`video/` + dst)
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
defer out.Close()
n, err := io.Copy(out, src)
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": n,
})
}

View File

@ -1,20 +1,26 @@
package app
import (
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/prometheus/common/log"
"github.com/silenceper/wechat"
"github.com/silenceper/wechat/cache"
"github.com/silenceper/wechat/miniprogram"
)
//Indexhandle index
func Indexhandle(c *gin.Context) {
var wxa *miniprogram.MiniProgram
func init() {
mem := cache.NewMemory()
appConfig := &wechat.Config{
AppID: "wx49cced01eec31847",
AppSecret: "0090134e4a137554a271133d7f73e633",
Cache: mem,
}
wx := wechat.NewWechat(appConfig)
wxa = wx.GetMiniProgram()
}
//Code2SessionHandle 登录凭证校验,通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。
@ -29,91 +35,3 @@ func Code2SessionHandler(c *gin.Context) {
}
c.JSON(http.StatusOK, result)
}
//HistoryVideoHandle 视频(文件)下载
func HistoryVideoHandler(c *gin.Context) {
openid := c.Param("openid")
p := "./video/" + openid
files, err := ioutil.ReadDir(p)
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error())
return
}
var videos []string
for _, f := range files {
if f.IsDir() {
continue
}
if !strings.HasSuffix(f.Name(), ".mp4") {
continue
}
videos = append(videos, f.Name())
}
c.JSON(http.StatusOK, gin.H{
"videos": videos,
})
}
type newForm struct {
File *multipart.FileHeader `form:"file"`
Openid string `form:"openid"`
}
//UploadfileAndFormHandle 上传带form
func UploadfileAndFormHandler(c *gin.Context) {
var data newForm
if err := c.ShouldBind(&data); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
filepath, err := SaveVideoFileService(data.File, &data.Openid)
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": gin.H{
"filepath": filepath,
"openid": data.Openid,
},
})
}
//UploadHandle 小程序中文件上传,正式环境中使用云存储
func UploadHandler(c *gin.Context) {
header, err := c.FormFile("file")
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
dst := header.Filename
src, err := header.Open()
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
defer src.Close()
out, err := os.Create(`video/` + dst)
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
defer out.Close()
n, err := io.Copy(out, src)
if err != nil {
log.Warn(err)
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": n,
})
}

View File

@ -1,65 +0,0 @@
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
}

View File

@ -1,7 +0,0 @@
package user
import "github.com/gin-gonic/gin"
func StatHandler(c *gin.Context) {
}

36
user/user-service.go Normal file
View File

@ -0,0 +1,36 @@
package user
import (
"io"
"mime/multipart"
"os"
"strings"
)
//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
}

View File

@ -1,15 +0,0 @@
package video
import "github.com/gin-gonic/gin"
func StatHandler(c *gin.Context) {
}
func ListHandler(c *gin.Context) {
}
func DownloadHandler(c *gin.Context) {
}

10
video/video-service.go Normal file
View File

@ -0,0 +1,10 @@
package video
import "mime/multipart"
type Video struct {
}
func (v *Video) Upload(file *multipart.FileHeader) (*string, error) {
return nil, nil
}