2021-06-30 02:12:05 +00:00
|
|
|
package crypt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-11-01 03:32:23 +00:00
|
|
|
"github.com/codinl/go-logger"
|
2021-06-30 02:12:05 +00:00
|
|
|
"github.com/qiniu/api.v7/auth/qbox"
|
|
|
|
"github.com/qiniu/api.v7/storage"
|
|
|
|
"mh-server/common"
|
|
|
|
"mh-server/lib/requests"
|
|
|
|
"mh-server/model"
|
2021-11-01 03:32:23 +00:00
|
|
|
"os"
|
2021-06-30 02:12:05 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-11-01 03:32:23 +00:00
|
|
|
const AppletCodeBaseUrl = "http://switch.deovo.com:8000/img/applet_code/"
|
|
|
|
|
2021-06-30 02:12:05 +00:00
|
|
|
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]
|
|
|
|
}
|
2021-11-01 03:32:23 +00:00
|
|
|
scene := fmt.Sprintf("uid=%d", uid)
|
2021-06-30 02:12:05 +00:00
|
|
|
resp, err := requests.Post(fmt.Sprintf("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=%s",
|
|
|
|
aT), map[string]interface{}{
|
|
|
|
"scene": scene,
|
2021-11-01 03:32:23 +00:00
|
|
|
"page": "pages/index/index",
|
|
|
|
"width": 430,
|
2021-06-30 02:12:05 +00:00
|
|
|
"auto_color": false,
|
2021-11-01 03:32:23 +00:00
|
|
|
"is_hyaline": false,
|
2021-06-30 02:12:05 +00:00
|
|
|
}, requests.JSON)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
res := struct {
|
|
|
|
Errcode uint32 `json:"errcode"`
|
|
|
|
Errmsg string `json:"errmsg"`
|
|
|
|
}{}
|
2021-11-01 03:32:23 +00:00
|
|
|
fileName := fmt.Sprintf("%d_%d.png", uid, time.Now().Unix())
|
2021-06-30 02:12:05 +00:00
|
|
|
err = json.Unmarshal(resp, &res)
|
|
|
|
if err != nil {
|
2021-11-01 03:32:23 +00:00
|
|
|
//// 证明是文件
|
|
|
|
//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
|
|
|
|
// /www/server/images/applet_code
|
|
|
|
|
|
|
|
//f, _ := os.Create(fmt.Sprintf("./" + fileName)) //创建文件
|
|
|
|
f, _ := os.Create(fmt.Sprintf("/www/server/images/applet_code/" + fileName)) //创建文件
|
|
|
|
defer f.Close() //关闭文件
|
|
|
|
_, err = f.Write(resp)
|
2021-06-30 02:12:05 +00:00
|
|
|
if err != nil {
|
2021-11-01 03:32:23 +00:00
|
|
|
logger.Errorf("err:%#v", err)
|
|
|
|
logger.Error("err:", err)
|
2021-06-30 02:12:05 +00:00
|
|
|
}
|
2021-11-01 03:32:23 +00:00
|
|
|
//jpeg.Encode(f, resp, nil) //写入文件
|
|
|
|
return AppletCodeBaseUrl + fileName, nil
|
|
|
|
|
2021-06-30 02:12:05 +00:00
|
|
|
}
|
|
|
|
return "", errors.New(fmt.Sprintf("%d%s", res.Errcode, res.Errmsg))
|
|
|
|
}
|
|
|
|
|
2021-11-01 03:32:23 +00:00
|
|
|
// 分享二维码
|
|
|
|
func GenShareCode(uid uint32) (string, error) {
|
2021-06-30 02:12:05 +00:00
|
|
|
//aT := GetWxAccessToken(appId, secret)
|
|
|
|
aT, err := model.GetAccessToken()
|
|
|
|
if aT == "" {
|
2021-11-01 03:32:23 +00:00
|
|
|
logger.Error("GetAccessToken err:", err)
|
2021-06-30 02:12:05 +00:00
|
|
|
return "", errors.New("get AccessToken Err")
|
|
|
|
}
|
|
|
|
//https://api.weixin.qq.com/wxa/getwxacode?access_token
|
2021-11-01 03:32:23 +00:00
|
|
|
fmt.Println("aT:", aT)
|
|
|
|
//pages/index/index?uid=123456
|
2021-06-30 02:12:05 +00:00
|
|
|
resp, err := requests.Post(fmt.Sprintf("https://api.weixin.qq.com/wxa/getwxacode?access_token=%s",
|
|
|
|
aT), map[string]interface{}{
|
2021-11-01 03:32:23 +00:00
|
|
|
"path": "pages/index/index" + fmt.Sprintf("?uid=%d", uid),
|
2021-06-30 02:12:05 +00:00
|
|
|
"width": 430,
|
|
|
|
}, requests.JSON)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
res := struct {
|
2021-11-01 03:32:23 +00:00
|
|
|
Errcode uint32 `json:"errcode"`
|
|
|
|
Errmsg string `json:"errmsg"`
|
|
|
|
ContentType string `json:"contentType"`
|
|
|
|
Buffer []byte `json:"buffer"`
|
|
|
|
//"errcode": 0,
|
|
|
|
//"errmsg": "ok",
|
|
|
|
//"contentType": "image/jpeg",
|
|
|
|
//"buffer": Buffer
|
2021-06-30 02:12:05 +00:00
|
|
|
}{}
|
2021-11-01 03:32:23 +00:00
|
|
|
fmt.Println("resp:", string(resp))
|
|
|
|
fileName := fmt.Sprintf("%d_%d.png", uid, time.Now().Unix())
|
2021-06-30 02:12:05 +00:00
|
|
|
err = json.Unmarshal(resp, &res)
|
|
|
|
if err != nil {
|
2021-11-01 03:32:23 +00:00
|
|
|
//// 证明是文件
|
|
|
|
//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
|
|
|
|
//f, _ := os.Create(fmt.Sprintf("/Users/li/mh/mh_server/" + fileName)) //创建文件
|
|
|
|
f, _ := os.Create(fmt.Sprintf("/www/server/images/applet_code/" + fileName)) //创建文件
|
|
|
|
defer f.Close() //关闭文件
|
|
|
|
_, err = f.Write(resp)
|
2021-06-30 02:12:05 +00:00
|
|
|
if err != nil {
|
2021-11-01 03:32:23 +00:00
|
|
|
logger.Errorf("err:%#v", err)
|
|
|
|
logger.Error("err:", err)
|
2021-06-30 02:12:05 +00:00
|
|
|
}
|
2021-11-01 03:32:23 +00:00
|
|
|
//jpeg.Encode(f, resp, nil) //写入文件
|
|
|
|
return AppletCodeBaseUrl + fileName, nil
|
2021-06-30 02:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|