This commit is contained in:
xxjwxc
2020-08-11 21:47:01 +08:00
parent 31f3bd0c2a
commit bc5ce66b55
5 changed files with 93 additions and 11 deletions

View File

@@ -18,6 +18,11 @@ type MessageBody struct {
Data interface{} `json:"data,omitempty"`
}
func init() {
_tryRegisteryCode(NormalMessageID)
_tryRegisteryCode(NormalMessageID)
}
//GetErrorMsg 获取错误消息 参数(int,string)
func GetErrorMsg(errorCode ...interface{}) (msg MessageBody) {
if len(errorCode) == 0 {

View File

@@ -54,3 +54,21 @@ type wxTools struct {
keyFile string
rootcaFile string
}
// QrcodeRet ...
type QrcodeRet struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
}
//
type wxPostdata struct {
Scene string `json:"scene"`
Page string `json:"page"`
}
//
type wxQrcodedata struct {
Path string `json:"path"` //路径
Width int `json:"width"` //二维码宽度
}

View File

@@ -9,12 +9,16 @@ import (
wxpay "gopkg.in/go-with/wxpay.v1"
)
const (
// 微信支付商户平台证书路径
certFileLoc = "/conf/cert/apiclient_cert.pem"
keyFileLoc = "/conf/cert/apiclient_key.pem"
rootcaFileLoc = "/conf/cert/rootca.pem"
)
// 微信支付商户平台证书路径
// CertFileLoc cert.pem
var CertFileLoc = "/conf/cert/apiclient_cert.pem"
// KeyFileLoc key.pem
var KeyFileLoc = "/conf/cert/apiclient_key.pem"
// RootcaFileLoc rootca.pem
var RootcaFileLoc = "/conf/cert/rootca.pem"
// WxTools 微信操作类型
type WxTools interface {
@@ -27,15 +31,17 @@ type WxTools interface {
SelectOrder(openID, orderID string) (int, message.MessageBody) // 统一查询接口
RefundPay(openID, orderID, refundNO string, totalFee, refundFee int) (bool, message.MessageBody) // 申请退款
WxEnterprisePay(openID, tradeNO, desc, ipAddr string, amount int) bool // 企业付款
GetShareQrcode(path string, scene, page string) (ret QrcodeRet) // 获取小程序码
GetWxQrcode(path, page string, width int) (ret QrcodeRet) // 获取小程序二维码 (有限个)
}
// New 新建及 初始化配置信息
func New(info WxInfo) (WxTools, error) {
t := &wxTools{
wxInfo: info,
certFile: tools.GetCurrentDirectory() + certFileLoc,
keyFile: tools.GetCurrentDirectory() + keyFileLoc,
rootcaFile: tools.GetCurrentDirectory() + rootcaFileLoc,
certFile: tools.GetCurrentDirectory() + CertFileLoc,
keyFile: tools.GetCurrentDirectory() + KeyFileLoc,
rootcaFile: tools.GetCurrentDirectory() + RootcaFileLoc,
client: wxpay.NewClient(info.AppID, info.MchID, info.APIKey),
}
err := t.client.WithCert(t.certFile, t.keyFile, t.rootcaFile)

View File

@@ -63,7 +63,7 @@ func (_wx *wxTools) SmallAppUnifiedorder(openID string, price int64, priceBody,
ret, err := _wx.client.Post(unifiedOrderURL, params, true)
if err != nil {
mylog.Error(err)
msg := message.GetErrorMsg(message.UnknownError)
msg := message.GetErrorMsg(err)
return msg
}
//-----------------------end
@@ -196,7 +196,7 @@ func (_wx *wxTools) RefundPay(openID, orderID, refundNO string, totalFee, refund
ret, err := _wx.client.Post(refundURL, params, true)
if err != nil {
mylog.Error(err)
msg := message.GetErrorMsg(message.UnknownError)
msg := message.GetErrorMsg(err)
return code, msg
}
//-----------------------end

53
weixin/qrcode.go Normal file
View File

@@ -0,0 +1,53 @@
package weixin
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/xxjwxc/public/myhttp"
"github.com/xxjwxc/public/tools"
)
const (
GETSHAREURL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" //小程序码
GETQRCODEURL = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=" //小程序二维码
)
// GetShareQrcode 获取小程序码
// path图片保存路径
// scene: 附带参数
// page:小程序页面头部
func (_wx *wxTools) GetShareQrcode(path string, scene, page string) (ret QrcodeRet) {
accessToken, _ := _wx.GetAccessToken() // 获取access_token
data := wxPostdata{Scene: scene, Page: page}
bo, _ := json.Marshal(data)
resb := myhttp.OnPostJSON(GETSHAREURL+accessToken, string(bo))
tools.JSONEncode(string(resb), &ret) //错误码45029 最大限制
if ret.Errcode == 0 {
ioutil.WriteFile(path, resb, 0666) //写入文件(字节数组)
}
return
}
// GetWxQrcode 获取小程序二维码 (有限个)
// path图片保存路径
// page: 小程序页面pages/index?query=1
// width: 二维码宽度
func (_wx *wxTools) GetWxQrcode(path, page string, width int) (ret QrcodeRet) {
fmt.Println(path)
//获取access_token
accessToken, _ := _wx.GetAccessToken()
data := wxQrcodedata{Path: page, Width: width}
bo, _ := json.Marshal(data)
resb := myhttp.OnPostJSON(GETQRCODEURL+accessToken, string(bo))
tools.JSONEncode(string(resb), &ret) //错误码45029 最大限制
if ret.Errcode == 0 {
ioutil.WriteFile(path, resb, 0666) //写入文件(字节数组)
}
return
}