231 lines
6.2 KiB
Go
231 lines
6.2 KiB
Go
|
package crypt
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"context"
|
||
|
"crypto/md5"
|
||
|
"encoding/hex"
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"github.com/qiniu/api.v7/auth/qbox"
|
||
|
"github.com/qiniu/api.v7/storage"
|
||
|
"mh-server/common"
|
||
|
"mh-server/lib/requests"
|
||
|
"mh-server/model"
|
||
|
"sort"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func GetWxAccessToken(appId, secret string) string {
|
||
|
//https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
|
||
|
response := struct {
|
||
|
AccessToken string `json:"access_token"`
|
||
|
ExpiresIn uint32 `json:"expires_in"`
|
||
|
Errcode uint32 `json:"errcode"`
|
||
|
Errmsg string `json:"errmsg"`
|
||
|
}{}
|
||
|
if err := requests.GetAndParseJson("https://api.weixin.qq.com/cgi-bin/token", map[string]string{
|
||
|
"grant_type": "client_credential",
|
||
|
"appId": appId,
|
||
|
"secret": secret,
|
||
|
}, &response); err != nil || response.Errcode != 0 {
|
||
|
return ""
|
||
|
}
|
||
|
return response.AccessToken
|
||
|
}
|
||
|
|
||
|
func GetHFDSign(params map[string]string, secret string) string {
|
||
|
var array []string
|
||
|
for k, v := range params {
|
||
|
array = append(array, fmt.Sprintf("%s%s", k, v))
|
||
|
}
|
||
|
sort.Strings(array)
|
||
|
three := strings.Join(array, "") + secret
|
||
|
md := md5.New()
|
||
|
md.Write([]byte(three))
|
||
|
return hex.EncodeToString(md.Sum(nil))
|
||
|
}
|
||
|
|
||
|
func GenPrizeShareCode(uid uint32, appId, secret string, prizeId ...uint32) (string, error) {
|
||
|
//aT := GetWxAccessToken(appId, secret)
|
||
|
aT, err := model.GetAccessToken()
|
||
|
if aT == "" {
|
||
|
return "", errors.New("get AccessToken Err")
|
||
|
}
|
||
|
var scene string
|
||
|
if len(prizeId) > 0 {
|
||
|
scene = fmt.Sprintf("%d/%d", uid, prizeId[0])
|
||
|
} else {
|
||
|
scene = strconv.Itoa(int(uid))
|
||
|
}
|
||
|
resp, err := requests.Post(fmt.Sprintf("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s",
|
||
|
aT), map[string]interface{}{
|
||
|
"scene": scene,
|
||
|
"width": 330,
|
||
|
"auto_color": false,
|
||
|
"is_hyaline": true,
|
||
|
}, requests.JSON)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
res := struct {
|
||
|
Errcode uint32 `json:"errcode"`
|
||
|
Errmsg string `json:"errmsg"`
|
||
|
}{}
|
||
|
err = json.Unmarshal(resp, &res)
|
||
|
if err != nil {
|
||
|
// 证明是文件
|
||
|
putPolicy := storage.PutPolicy{
|
||
|
Scope: common.Bucket,
|
||
|
}
|
||
|
mac := qbox.NewMac(common.AccessKey, common.SecretKey)
|
||
|
upToken := putPolicy.UploadToken(mac)
|
||
|
cfg := storage.Config{
|
||
|
Zone: &storage.ZoneHuanan,
|
||
|
UseHTTPS: true,
|
||
|
UseCdnDomains: true,
|
||
|
}
|
||
|
formUploader := storage.NewFormUploader(&cfg)
|
||
|
ret := storage.PutRet{}
|
||
|
dataLen := int64(len(resp))
|
||
|
putExtra := storage.PutExtra{}
|
||
|
var key string
|
||
|
if len(prizeId) > 0 {
|
||
|
key = fmt.Sprintf("zouzou-server/qrcode/prize/%d_%d.png", uid, prizeId[0])
|
||
|
} else {
|
||
|
key = fmt.Sprintf("zouzou-server/qrcode/%d.png", uid)
|
||
|
}
|
||
|
err := formUploader.Put(context.Background(), &ret, upToken, key,
|
||
|
bytes.NewReader(resp), dataLen, &putExtra)
|
||
|
if err != nil {
|
||
|
if err.Error() == "file exists" {
|
||
|
return common.QiniuFileUrlHome + key, nil
|
||
|
}
|
||
|
return "", err
|
||
|
}
|
||
|
return common.QiniuFileUrlHome + ret.Key, nil
|
||
|
}
|
||
|
return "", errors.New(fmt.Sprintf("%d%s", res.Errcode, res.Errmsg))
|
||
|
}
|
||
|
|
||
|
func GenDrawShareCode(Did string, appId, secret string, userID ...uint32) (string, error) {
|
||
|
//aT := GetWxAccessToken(appId, secret)
|
||
|
aT, err := model.GetAccessToken()
|
||
|
if aT == "" {
|
||
|
return "", errors.New("get AccessToken Err")
|
||
|
}
|
||
|
var uid uint32
|
||
|
if len(userID) > 0 {
|
||
|
uid = userID[0]
|
||
|
}
|
||
|
scene := fmt.Sprintf("%d/0/%s", uid, Did)
|
||
|
resp, err := requests.Post(fmt.Sprintf("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s",
|
||
|
aT), map[string]interface{}{
|
||
|
"scene": scene,
|
||
|
"page": "pages/index/awardDetail/awardDetail",
|
||
|
"width": 330,
|
||
|
"auto_color": false,
|
||
|
"is_hyaline": true,
|
||
|
}, requests.JSON)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
res := struct {
|
||
|
Errcode uint32 `json:"errcode"`
|
||
|
Errmsg string `json:"errmsg"`
|
||
|
}{}
|
||
|
err = json.Unmarshal(resp, &res)
|
||
|
if err != nil {
|
||
|
// 证明是文件
|
||
|
putPolicy := storage.PutPolicy{
|
||
|
Scope: common.Bucket,
|
||
|
}
|
||
|
mac := qbox.NewMac(common.AccessKey, common.SecretKey)
|
||
|
upToken := putPolicy.UploadToken(mac)
|
||
|
cfg := storage.Config{
|
||
|
Zone: &storage.ZoneHuanan,
|
||
|
UseHTTPS: true,
|
||
|
UseCdnDomains: true,
|
||
|
}
|
||
|
formUploader := storage.NewFormUploader(&cfg)
|
||
|
ret := storage.PutRet{}
|
||
|
dataLen := int64(len(resp))
|
||
|
putExtra := storage.PutExtra{}
|
||
|
var key string
|
||
|
key = fmt.Sprintf("zouzou-server/qrcode/draw/%s_%d.png", Did, uid)
|
||
|
err := formUploader.Put(context.Background(), &ret, upToken, key,
|
||
|
bytes.NewReader(resp), dataLen, &putExtra)
|
||
|
if err != nil {
|
||
|
if err.Error() == "file exists" {
|
||
|
return common.QiniuFileUrlHome + key, nil
|
||
|
}
|
||
|
return "", err
|
||
|
}
|
||
|
return common.QiniuFileUrlHome + ret.Key, nil
|
||
|
}
|
||
|
return "", errors.New(fmt.Sprintf("%d%s", res.Errcode, res.Errmsg))
|
||
|
}
|
||
|
|
||
|
func GenShareCode(appId, secret string) (string, error) {
|
||
|
//aT := GetWxAccessToken(appId, secret)
|
||
|
aT, err := model.GetAccessToken()
|
||
|
if aT == "" {
|
||
|
return "", errors.New("get AccessToken Err")
|
||
|
}
|
||
|
//https://api.weixin.qq.com/wxa/getwxacode?access_token
|
||
|
|
||
|
resp, err := requests.Post(fmt.Sprintf("https://api.weixin.qq.com/wxa/getwxacode?access_token=%s",
|
||
|
aT), map[string]interface{}{
|
||
|
"path": "page/index",
|
||
|
"width": 430,
|
||
|
}, requests.JSON)
|
||
|
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
res := struct {
|
||
|
Errcode uint32 `json:"errcode"`
|
||
|
Errmsg string `json:"errmsg"`
|
||
|
}{}
|
||
|
err = json.Unmarshal(resp, &res)
|
||
|
if err != nil {
|
||
|
// 证明是文件
|
||
|
putPolicy := storage.PutPolicy{
|
||
|
Scope: common.Bucket,
|
||
|
}
|
||
|
mac := qbox.NewMac(common.AccessKey, common.SecretKey)
|
||
|
upToken := putPolicy.UploadToken(mac)
|
||
|
cfg := storage.Config{
|
||
|
Zone: &storage.ZoneHuanan,
|
||
|
UseHTTPS: true,
|
||
|
UseCdnDomains: true,
|
||
|
}
|
||
|
formUploader := storage.NewFormUploader(&cfg)
|
||
|
ret := storage.PutRet{}
|
||
|
dataLen := int64(len(resp))
|
||
|
putExtra := storage.PutExtra{}
|
||
|
var key string
|
||
|
key = fmt.Sprintf("zouzou-server/pack/%d.png", time.Now().Unix())
|
||
|
err := formUploader.Put(context.Background(), &ret, upToken, key,
|
||
|
bytes.NewReader(resp), dataLen, &putExtra)
|
||
|
if err != nil {
|
||
|
if err.Error() == "file exists" {
|
||
|
return common.QiniuFileUrlHome + key, nil
|
||
|
}
|
||
|
return "", err
|
||
|
}
|
||
|
return common.QiniuFileUrlHome + ret.Key, nil
|
||
|
}
|
||
|
|
||
|
return "", errors.New(fmt.Sprintf("%d%s", res.Errcode, res.Errmsg))
|
||
|
}
|
||
|
|
||
|
func Md5(data string) string {
|
||
|
digest := md5.New()
|
||
|
digest.Write([]byte(data))
|
||
|
return fmt.Sprintf("%x", digest.Sum(nil))
|
||
|
}
|