1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 12:52:27 +08:00

[feature] Format the code and improve Mini Program authorization to o… (#473)

* [feature] Format the code and improve Mini Program authorization to obtain openid(miniprogram/auth/auth.go Code2Session)

* [feature] CheckEncryptedData (https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.checkEncryptedData.html)

* upgrade json error

* upgrade json error

Co-authored-by: houseme <houseme@outlook.com>
This commit is contained in:
houseme
2021-09-08 11:03:23 +08:00
committed by GitHub
parent 47adf42208
commit 96c1f98944
90 changed files with 787 additions and 760 deletions

2
cache/redis.go vendored
View File

@@ -7,7 +7,7 @@ import (
"github.com/gomodule/redigo/redis"
)
//Redis redis cache
// Redis .redis cache
type Redis struct {
conn *redis.Pool
}

View File

@@ -7,6 +7,7 @@ import (
"gopkg.in/h2non/gock.v1"
)
// TestGetTicketFromServer .
func TestGetTicketFromServer(t *testing.T) {
defer gock.Off()
gock.New(getTicketURL).Reply(200).JSON(&ResTicket{Ticket: "mock-ticket", ExpiresIn: 10})

View File

@@ -10,7 +10,7 @@ import (
"github.com/silenceper/wechat/v2/util"
)
//获取ticket的url
// getTicketURL 获取ticket的url
const getTicketURL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi"
// DefaultJsTicket 默认获取js ticket方法

2
go.mod
View File

@@ -5,7 +5,7 @@ go 1.14
require (
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/fatih/structs v1.1.0
github.com/gomodule/redigo v1.8.4
github.com/gomodule/redigo v1.8.5
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/sirupsen/logrus v1.8.1

4
go.sum
View File

@@ -6,8 +6,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/gomodule/redigo v1.8.4 h1:Z5JUg94HMTR1XpwBaSH4vq3+PNSIykBLxMdglbw10gg=
github.com/gomodule/redigo v1.8.4/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
github.com/gomodule/redigo v1.8.5 h1:nRAxCa+SVsyjSBrtZmG/cqb6VbTmuRzpg/PoTFlpumc=
github.com/gomodule/redigo v1.8.5/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

View File

@@ -10,6 +10,8 @@ import (
const (
code2SessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"
checkEncryptedDataURL = "https://api.weixin.qq.com/wxa/business/checkencryptedmsg?access_token=%s"
)
// Auth 登录/用户信息
@@ -31,16 +33,21 @@ type ResCode2Session struct {
UnionID string `json:"unionid"` // 用户在开放平台的唯一标识符在满足UnionID下发条件的情况下会返回
}
// RspCheckEncryptedData .
type RspCheckEncryptedData struct {
util.CommonError
Vaild bool `json:"vaild"` // 是否是合法的数据
CreateTime uint `json:"create_time"` // 加密数据生成的时间戳
}
// Code2Session 登录凭证校验。
func (auth *Auth) Code2Session(jsCode string) (result ResCode2Session, err error) {
urlStr := fmt.Sprintf(code2SessionURL, auth.AppID, auth.AppSecret, jsCode)
var response []byte
response, err = util.HTTPGet(urlStr)
if err != nil {
if response, err = util.HTTPGet(fmt.Sprintf(code2SessionURL, auth.AppID, auth.AppSecret, jsCode)); err != nil {
return
}
err = json.Unmarshal(response, &result)
if err != nil {
if err = json.Unmarshal(response, &result); err != nil {
return
}
if result.ErrCode != 0 {
@@ -54,3 +61,21 @@ func (auth *Auth) Code2Session(jsCode string) (result ResCode2Session, err error
func (auth *Auth) GetPaidUnionID() {
// TODO
}
// CheckEncryptedData .检查加密信息是否由微信生成当前只支持手机号加密数据只能检测最近3天生成的加密数据
func (auth *Auth) CheckEncryptedData(encryptedMsgHash string) (result RspCheckEncryptedData, err error) {
var response []byte
var (
at string
)
if at, err = auth.GetAccessToken(); err != nil {
return
}
if response, err = util.HTTPPost(fmt.Sprintf(checkEncryptedDataURL, at), "encrypted_msg_hash="+encryptedMsgHash); err != nil {
return
}
if err = util.DecodeWithError(response, &result, "CheckEncryptedDataAuth"); err != nil {
return
}
return
}

View File

@@ -5,7 +5,7 @@ import (
"github.com/silenceper/wechat/v2/cache"
)
// Config config for 小程序
// Config .config for 小程序
type Config struct {
AppID string `json:"app_id"` // appid
AppSecret string `json:"app_secret"` // appsecret

View File

@@ -51,7 +51,6 @@ type ULResult struct {
// Generate 生成url link
func (u *URLLink) Generate(params *ULParams) (string, error) {
var accessToken string
accessToken, err := u.GetAccessToken()
if err != nil {
return "", err

View File

@@ -4,7 +4,7 @@ import (
"github.com/silenceper/wechat/v2/cache"
)
// Config config for 微信公众号
// Config .config for 微信公众号
type Config struct {
AppID string `json:"app_id"` // appid
AppSecret string `json:"app_secret"` // appsecret

View File

@@ -4,7 +4,7 @@ import (
"github.com/silenceper/wechat/v2/cache"
)
//Config config for 微信开放平台
// Config .config for 微信开放平台
type Config struct {
AppID string `json:"app_id"` // appid
AppSecret string `json:"app_secret"` // appsecret

View File

@@ -1,6 +1,6 @@
package config
// Config config for pay
// Config .config for pay
type Config struct {
AppID string `json:"app_id"`
MchID string `json:"mch_id"`

View File

@@ -172,9 +172,9 @@ func (o *Order) BridgeConfig(p *Params) (cfg Config, err error) {
// BridgeAppConfig get app bridge config
func (o *Order) BridgeAppConfig(p *Params) (cfg ConfigForApp, err error) {
var (
timestamp string = strconv.FormatInt(time.Now().Unix(), 10)
noncestr string = util.RandomStr(32)
_package string = "Sign=WXPay"
timestamp = strconv.FormatInt(time.Now().Unix(), 10)
noncestr = util.RandomStr(32)
_package = "Sign=WXPay"
)
order, err := o.PrePayOrder(p)
if err != nil {

View File

@@ -1,3 +1,4 @@
//go:build linux && cgo && msgaudit
// +build linux,cgo,msgaudit
// Package msgaudit only for linux

View File

@@ -1,3 +1,4 @@
//go:build !linux || !cgo || !msgaudit
// +build !linux !cgo !msgaudit
// Package msgaudit for unsupport platform