Files
jx-callback/controllers/dingding_api.go
2019-04-15 14:33:22 +08:00

58 lines
1.5 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 controllers
import (
"crypto/sha1"
"fmt"
"time"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"github.com/astaxie/beego"
)
type DDAPIController struct {
beego.Controller
}
type DDAPI struct {
Ticket string
ExpiresIn time.Time
}
var (
ddapi *DDAPI
)
func init() {
ddapi = &DDAPI{}
}
// @Title 得到门店用户信息
// @Description 得到门店用户信息
// @Param token header string true "认证token"
// @Param url query string true "当前网页的URL不包含#及其后面部分"
// @Param nonceStr query string true "随机串,自己定义"
// @Param timeStamp query string true "时间戳"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /Sign [get]
func (c *DDAPIController) Sign() {
c.callSign(func(params *tDdapiSignParams) (retVal interface{}, errCode string, err error) {
if ddapi.Ticket == "" || time.Now().Sub(ddapi.ExpiresIn) >= -5*time.Minute {
expiresIn, ticket, err2 := api.DingDingAPI.GetJSAPITicket("")
if err = err2; err == nil {
ddapi.Ticket = ticket
ddapi.ExpiresIn = time.Now().Add(time.Duration(expiresIn) * time.Second)
}
}
if err == nil {
str := "jsapi_ticket=" + ddapi.Ticket +
"&noncestr=" + params.NonceStr + "&timestamp=" + params.TimeStamp + "&url=" + params.Url
retVal = fmt.Sprintf("%x", sha1.Sum([]byte(str)))
globals.SugarLogger.Debugf("dingapi str:%s sign:%v", str, retVal)
}
return retVal, "", err
})
}