1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-17 11:12:26 +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" "github.com/gomodule/redigo/redis"
) )
//Redis redis cache // Redis .redis cache
type Redis struct { type Redis struct {
conn *redis.Pool conn *redis.Pool
} }

View File

@@ -7,6 +7,7 @@ import (
"gopkg.in/h2non/gock.v1" "gopkg.in/h2non/gock.v1"
) )
// TestGetTicketFromServer .
func TestGetTicketFromServer(t *testing.T) { func TestGetTicketFromServer(t *testing.T) {
defer gock.Off() defer gock.Off()
gock.New(getTicketURL).Reply(200).JSON(&ResTicket{Ticket: "mock-ticket", ExpiresIn: 10}) 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" "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" const getTicketURL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi"
// DefaultJsTicket 默认获取js ticket方法 // DefaultJsTicket 默认获取js ticket方法

2
go.mod
View File

@@ -5,7 +5,7 @@ go 1.14
require ( require (
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/fatih/structs v1.1.0 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/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/sirupsen/logrus v1.8.1 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/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 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= 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.5 h1:nRAxCa+SVsyjSBrtZmG/cqb6VbTmuRzpg/PoTFlpumc=
github.com/gomodule/redigo v1.8.4/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUzlpkBYO0= 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 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI= github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=

View File

@@ -10,6 +10,8 @@ import (
const ( const (
code2SessionURL = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code" 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 登录/用户信息 // Auth 登录/用户信息
@@ -31,16 +33,21 @@ type ResCode2Session struct {
UnionID string `json:"unionid"` // 用户在开放平台的唯一标识符在满足UnionID下发条件的情况下会返回 UnionID string `json:"unionid"` // 用户在开放平台的唯一标识符在满足UnionID下发条件的情况下会返回
} }
// RspCheckEncryptedData .
type RspCheckEncryptedData struct {
util.CommonError
Vaild bool `json:"vaild"` // 是否是合法的数据
CreateTime uint `json:"create_time"` // 加密数据生成的时间戳
}
// Code2Session 登录凭证校验。 // Code2Session 登录凭证校验。
func (auth *Auth) Code2Session(jsCode string) (result ResCode2Session, err error) { func (auth *Auth) Code2Session(jsCode string) (result ResCode2Session, err error) {
urlStr := fmt.Sprintf(code2SessionURL, auth.AppID, auth.AppSecret, jsCode)
var response []byte var response []byte
response, err = util.HTTPGet(urlStr) if response, err = util.HTTPGet(fmt.Sprintf(code2SessionURL, auth.AppID, auth.AppSecret, jsCode)); err != nil {
if err != nil {
return return
} }
err = json.Unmarshal(response, &result) if err = json.Unmarshal(response, &result); err != nil {
if err != nil {
return return
} }
if result.ErrCode != 0 { if result.ErrCode != 0 {
@@ -54,3 +61,21 @@ func (auth *Auth) Code2Session(jsCode string) (result ResCode2Session, err error
func (auth *Auth) GetPaidUnionID() { func (auth *Auth) GetPaidUnionID() {
// TODO // 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" "github.com/silenceper/wechat/v2/cache"
) )
// Config config for 小程序 // Config .config for 小程序
type Config struct { type Config struct {
AppID string `json:"app_id"` // appid AppID string `json:"app_id"` // appid
AppSecret string `json:"app_secret"` // appsecret AppSecret string `json:"app_secret"` // appsecret

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
package config package config
// Config config for pay // Config .config for pay
type Config struct { type Config struct {
AppID string `json:"app_id"` AppID string `json:"app_id"`
MchID string `json:"mch_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 // BridgeAppConfig get app bridge config
func (o *Order) BridgeAppConfig(p *Params) (cfg ConfigForApp, err error) { func (o *Order) BridgeAppConfig(p *Params) (cfg ConfigForApp, err error) {
var ( var (
timestamp string = strconv.FormatInt(time.Now().Unix(), 10) timestamp = strconv.FormatInt(time.Now().Unix(), 10)
noncestr string = util.RandomStr(32) noncestr = util.RandomStr(32)
_package string = "Sign=WXPay" _package = "Sign=WXPay"
) )
order, err := o.PrePayOrder(p) order, err := o.PrePayOrder(p)
if err != nil { if err != nil {

View File

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

View File

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