120 lines
2.6 KiB
Go
120 lines
2.6 KiB
Go
package app
|
|
|
|
import (
|
|
"io"
|
|
"io/ioutil"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/prometheus/common/log"
|
|
)
|
|
|
|
//Indexhandle index
|
|
func Indexhandle(c *gin.Context) {
|
|
|
|
}
|
|
|
|
//Code2SessionHandle 登录凭证校验,通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。
|
|
//GET https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
|
|
func Code2SessionHandler(c *gin.Context) {
|
|
jscode := c.Param("jscode")
|
|
result, err := wxa.Code2Session(jscode)
|
|
if err != nil {
|
|
log.Warn(err)
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
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,
|
|
})
|
|
}
|