初始化
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
// Package aliyun 短信
|
||||
package aliyun
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||||
dysmsapi20170525 "github.com/alibabacloud-go/dysmsapi-20170525/v3/client"
|
||||
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
||||
"github.com/alibabacloud-go/tea/tea"
|
||||
"vote-admin-api/config"
|
||||
)
|
||||
|
||||
func createClient() (_result *dysmsapi20170525.Client, _err error) {
|
||||
accessKeyId := config.C.Dysms.DysmsAccessKey
|
||||
accessKeySecret := config.C.Dysms.DysmsAccessSecret
|
||||
|
||||
openapiConfig := &openapi.Config{
|
||||
// 必填,您的 AccessKey ID
|
||||
AccessKeyId: &accessKeyId,
|
||||
// 必填,您的 AccessKey Secret
|
||||
AccessKeySecret: &accessKeySecret,
|
||||
}
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
|
||||
openapiConfig.Endpoint = tea.String("dysmsapi.aliyuncs.com")
|
||||
_result = &dysmsapi20170525.Client{}
|
||||
_result, _err = dysmsapi20170525.NewClient(openapiConfig)
|
||||
return _result, _err
|
||||
}
|
||||
|
||||
// SendSms 发送短信
|
||||
func SendSms(phoneNumber, templateCode, sceneDesc string, templateParam map[string]interface{}) error {
|
||||
client, err := createClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
params, err := json.Marshal(templateParam)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sendSmsRequest := &dysmsapi20170525.SendSmsRequest{
|
||||
PhoneNumbers: tea.String(phoneNumber),
|
||||
SignName: tea.String("肝胆相照"),
|
||||
TemplateCode: tea.String(templateCode),
|
||||
TemplateParam: tea.String(string(params)),
|
||||
}
|
||||
|
||||
tryErr := func() (e error) {
|
||||
defer func() {
|
||||
if r := tea.Recover(recover()); r != nil {
|
||||
e = r
|
||||
}
|
||||
}()
|
||||
|
||||
// 初始化运行时配置。
|
||||
runtime := &util.RuntimeOptions{}
|
||||
// 读取超时
|
||||
runtime.SetReadTimeout(10000)
|
||||
// 连接超时
|
||||
runtime.SetConnectTimeout(5000)
|
||||
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
response, err := client.SendSms(sendSmsRequest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if response.Body == nil {
|
||||
return errors.New("短信发送失败")
|
||||
}
|
||||
|
||||
if response.Body.Code != nil && *response.Body.Code != "OK" {
|
||||
if response.Body.Message != nil {
|
||||
return errors.New(*response.Body.Message)
|
||||
}
|
||||
}
|
||||
|
||||
// 检测唯一值返回
|
||||
if response.Body.RequestId == nil {
|
||||
if response.Body.Message != nil {
|
||||
return errors.New(*response.Body.Message)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}()
|
||||
|
||||
if tryErr != nil {
|
||||
var sdkError = &tea.SDKError{}
|
||||
if t, ok := tryErr.(*tea.SDKError); ok {
|
||||
sdkError = t
|
||||
} else {
|
||||
sdkError.Message = tea.String(tryErr.Error())
|
||||
}
|
||||
// 如有需要,请打印 error
|
||||
_, err = util.AssertAsString(sdkError.Message)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package aliyun
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/hmac"
|
||||
"crypto/sha1"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/aliyun/aliyun-oss-go-sdk/oss"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
"vote-admin-api/config"
|
||||
)
|
||||
|
||||
// GetOssSignResponse 获取oss签名返回值
|
||||
type GetOssSignResponse struct {
|
||||
AccessId string `json:"access_id"` // 主键id
|
||||
Host string `json:"host"`
|
||||
Policy string `json:"policy"`
|
||||
Signature string `json:"signature"`
|
||||
Expire int64 `json:"expire"`
|
||||
Callback string `json:"callback"`
|
||||
Dir string `json:"dir"`
|
||||
}
|
||||
|
||||
// GetOssSign 获取oss签名
|
||||
func GetOssSign(dir string) (*GetOssSignResponse, error) {
|
||||
now := time.Now()
|
||||
expire := 30 // 设置该policy超时时间是30s,即这个policy过了这个有效时间,将不能访问。
|
||||
end := now.Add(time.Second * time.Duration(expire))
|
||||
expiration := strings.Replace(end.Format("2006-01-02T15:04:05.000Z"), "+00:00", ".000Z", 1)
|
||||
|
||||
start := []interface{}{"starts-with", "$key", dir}
|
||||
conditions := [][]interface{}{start}
|
||||
|
||||
arr := map[string]interface{}{
|
||||
"expiration": expiration,
|
||||
"conditions": conditions,
|
||||
}
|
||||
policy, _ := json.Marshal(arr)
|
||||
base64Policy := base64.StdEncoding.EncodeToString(policy)
|
||||
stringToSign := base64Policy
|
||||
h := hmac.New(sha1.New, []byte(config.C.Oss.OssAccessKeySecret))
|
||||
h.Write([]byte(stringToSign))
|
||||
signature := base64.StdEncoding.EncodeToString(h.Sum(nil))
|
||||
|
||||
response := &GetOssSignResponse{
|
||||
AccessId: config.C.Oss.OssAccessKey,
|
||||
Host: config.C.Oss.OssCustomDomainName,
|
||||
Policy: base64Policy,
|
||||
Signature: signature,
|
||||
Expire: end.Unix(),
|
||||
Callback: "",
|
||||
Dir: dir,
|
||||
}
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// CreateClient 创建客户端
|
||||
func CreateClient() (*oss.Client, error) {
|
||||
// 创建OSSClient实例。
|
||||
client, err := oss.New(config.C.Oss.OssEndpoint, config.C.Oss.OssAccessKey, config.C.Oss.OssAccessKeySecret)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// GetCusTomObjectToRAM 下载自定义风格文件到内存
|
||||
func GetCusTomObjectToRAM(filename string, style string) (string, error) {
|
||||
if style == "" {
|
||||
style = "image/resize"
|
||||
}
|
||||
|
||||
ossClient, err := CreateClient()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// yourBucketName填写存储空间名称。
|
||||
bucket, err := ossClient.Bucket(config.C.Oss.OssBucket)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 下载文件到流。
|
||||
body, err := bucket.GetObject(filename, oss.Process(style))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
|
||||
defer func(body io.ReadCloser) {
|
||||
_ = body.Close()
|
||||
}(body)
|
||||
|
||||
data, err := io.ReadAll(body)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// GetObjectToRAM 下载文件到内存
|
||||
func GetObjectToRAM(filename string) (string, error) {
|
||||
ossClient, err := CreateClient()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// yourBucketName填写存储空间名称。
|
||||
bucket, err := ossClient.Bucket(config.C.Oss.OssBucket)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 下载文件到流。
|
||||
body, err := bucket.GetObject(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
|
||||
defer func(body io.ReadCloser) {
|
||||
_ = body.Close()
|
||||
}(body)
|
||||
|
||||
data, err := io.ReadAll(body)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
os.Exit(-1)
|
||||
}
|
||||
|
||||
return string(data), nil
|
||||
}
|
||||
|
||||
// GetObjectToLocal 下载文件到本地
|
||||
func GetObjectToLocal(filename, local string) (bool, error) {
|
||||
ossClient, err := CreateClient()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// yourBucketName填写存储空间名称。
|
||||
bucket, err := ossClient.Bucket(config.C.Oss.OssBucket)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// 下载文件到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。
|
||||
// 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。
|
||||
// 依次填写Object完整路径(例如exampledir/exampleobject.txt)和本地文件的完整路径(例如D:\\localpath\\examplefile.txt)。Object完整路径中不能包含Bucket名称。
|
||||
err = bucket.GetObjectToFile(filename, local)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// PutObjectByte 上传文件
|
||||
func PutObjectByte(filename string, content []byte) (bool, error) {
|
||||
ossClient, err := CreateClient()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
// yourBucketName填写存储空间名称。
|
||||
bucket, err := ossClient.Bucket(config.C.Oss.OssBucket)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
err = bucket.PutObject(filename, bytes.NewReader(content))
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"strings"
|
||||
"vote-admin-api/config"
|
||||
"vote-admin-api/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
apiUrl = "https://dev-wx.igandan.com" // 接口地址
|
||||
secretKey = "RY8pcn04#TSdzHVX6YgWnyCue9!T&QP^" // 产品私有密钥,服务端生成签名信息使用,请严格保管,避免泄露
|
||||
platform = "suanyisuan" // 所属平台
|
||||
devImagePrefix = "https://dev-doc.igandan.com/app" // 测试环境图片地址前缀
|
||||
prodImagePrefix = "https://dev-doc.igandan.com/app" // 正式环境图片地址前缀
|
||||
)
|
||||
|
||||
// GenSignature 生成签名信息
|
||||
func GenSignature(params map[string]interface{}) (string, error) {
|
||||
// 对map的key进行排序,包括多层嵌套的情况
|
||||
data := sortMapRecursively(params)
|
||||
|
||||
// 转换为JSON
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
sing := utils.HmacSHA256(string(jsonData), secretKey)
|
||||
return sing, nil
|
||||
}
|
||||
|
||||
// sortMapRecursively 对map的key进行排序,包括多层嵌套的情况
|
||||
func sortMapRecursively(data map[string]interface{}) map[string]interface{} {
|
||||
sortedMap := make(map[string]interface{})
|
||||
keys := make([]string, 0, len(data))
|
||||
|
||||
// 收集所有的key
|
||||
for key := range data {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
// 对key进行排序
|
||||
sort.Strings(keys)
|
||||
|
||||
// 通过排序后的key插入新map中
|
||||
for _, key := range keys {
|
||||
value := data[key]
|
||||
switch valueTyped := value.(type) {
|
||||
case map[string]interface{}:
|
||||
// 如果是嵌套的map,递归调用
|
||||
sortedMap[key] = sortMapRecursively(valueTyped)
|
||||
case []interface{}:
|
||||
// 如果是嵌套的slice,对其中的map进行递归调用
|
||||
for i, v := range valueTyped {
|
||||
if vMap, ok := v.(map[string]interface{}); ok {
|
||||
valueTyped[i] = sortMapRecursively(vMap)
|
||||
}
|
||||
}
|
||||
sortedMap[key] = valueTyped
|
||||
default:
|
||||
// 否则直接插入
|
||||
sortedMap[key] = value
|
||||
}
|
||||
}
|
||||
|
||||
return sortedMap
|
||||
}
|
||||
|
||||
// HandleImagePrefix 处理app图片前缀
|
||||
func HandleImagePrefix(u string) (string, error) {
|
||||
if u == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
// 去除oss前缀
|
||||
u = utils.RemoveOssDomain(u)
|
||||
|
||||
var imgPath string
|
||||
if config.C.Env == "prod" {
|
||||
imgPath = strings.Replace(u, devImagePrefix, "", 1)
|
||||
} else {
|
||||
imgPath = strings.Replace(u, prodImagePrefix, "", 1)
|
||||
}
|
||||
|
||||
if imgPath == "/null" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
return imgPath, nil
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// PayOrderRequest 获取订单支付请求数据
|
||||
type PayOrderRequest struct {
|
||||
OrderId string `json:"orderId" label:"订单编号" validate:"required"` // 订单id
|
||||
}
|
||||
|
||||
// PayOrderDataResponse 获取订单支付返回数据-data
|
||||
type PayOrderDataResponse struct {
|
||||
AppId string `json:"appid"` // 公众号id
|
||||
Total int `json:"total"` // 订单总金额(精确到分)
|
||||
Description string `json:"description"` // 订单描述
|
||||
OpenId string `json:"openid"` // 下单用户
|
||||
OutTradeNo string `json:"out_trade_no"` // 商户订单
|
||||
Attach string `json:"attach"` // 附加信息
|
||||
NotifyUrl string `json:"notify_url"` // 异步接收微信支付结果通知的回调地址
|
||||
GoodName string `json:"goodName"` // 商品名称
|
||||
}
|
||||
|
||||
// VerifySignature 验证签名
|
||||
func VerifySignature(req PayOrderRequest, requestSign string) error {
|
||||
// 将 JSON 数据编码为字节数组
|
||||
jsonData, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
err = json.Unmarshal(jsonData, &maps)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 生成签名
|
||||
sign, err := GenSignature(maps)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(sign)
|
||||
// 对比签名
|
||||
if sign != requestSign {
|
||||
return errors.New("签名错误")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,242 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"vote-admin-api/utils"
|
||||
)
|
||||
|
||||
// GetUserCaseByAppIdenRequest 根据app唯一标识获取用户病例信息-请求数据
|
||||
type GetUserCaseByAppIdenRequest struct {
|
||||
PatientUuid string `json:"patientUuid"` // 患者 uuid
|
||||
Platform string `json:"platform"` // 所属平台
|
||||
Timestamp string `json:"timestamp"` // 当前时间戳(10位)
|
||||
}
|
||||
|
||||
// UpdateUserCaseRequest 修改疾病信息-请求数据
|
||||
type UpdateUserCaseRequest struct {
|
||||
IsAllergy *int `json:"isAllergy"` // 是否过敏史 0否 1是
|
||||
AllergyInfo string `json:"allergyInfo"` // 过敏史详情
|
||||
IsHospital *int `json:"isHospital"` // 是否去医院 0否 1是
|
||||
IsMedication *int `json:"isMedication"` // 是否服药 0否 1是
|
||||
MedicationInfo string `json:"medicationInfo"` // 正在服用的药物
|
||||
LiverStatus string `json:"liverStatus"` // 目前肝脏状态
|
||||
OtherDisease string `json:"otherDisease"` // 合并其他慢性疾病 (多个英文逗号分隔拼接)
|
||||
DiseasesList []*DiseasesListRequest `json:"diseasesList"` // 所患疾病列表
|
||||
PatientUuid string `json:"patientUuid"` // 患者 uuid
|
||||
Platform string `json:"platform"` // 所属平台
|
||||
Timestamp string `json:"timestamp"` // 当前时间戳(10位)
|
||||
}
|
||||
|
||||
type DiseasesListRequest struct {
|
||||
Uuid string `json:"uuid"` // 疾病 uuid
|
||||
Year *int `json:"year"` // 患病时长
|
||||
Info string `json:"info"` // 丙肝基因型(仅针对丙肝)
|
||||
Name string `json:"name"` // 疾病名称
|
||||
}
|
||||
|
||||
// GetUserCaseByAppIdenResponse 根据app唯一标识获取用户病例信息-返回数据
|
||||
type GetUserCaseByAppIdenResponse struct {
|
||||
Code int `json:"code"` // 接口调用状态。200:正常;其它值:调用出错
|
||||
Msg string `json:"msg"` // 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok
|
||||
Data *GetUserCaseByAppIdenData `json:"data"` // 接口返回结果,各个接口自定义,数据结构参考具体文档说明
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// UpdateUserCaseResponse 修改用户病例-返回数据
|
||||
type UpdateUserCaseResponse struct {
|
||||
Code int `json:"code"` // 接口调用状态。200:正常;其它值:调用出错
|
||||
Msg string `json:"msg"` // 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok
|
||||
Data string `json:"data"` // 接口返回结果,各个接口自定义,数据结构参考具体文档说明
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// GetUserCaseByAppIdenData 根据app唯一标识获取用户病例信息-data详细数据
|
||||
type GetUserCaseByAppIdenData struct {
|
||||
AllergyInfo string `json:"allergyInfo" description:"过敏史详情"`
|
||||
PatientUUID string `json:"patientUuid" description:"患者 uuid"`
|
||||
DiseasesList []*DiseasesListData `json:"diseasesList" description:"所患疾病列表"`
|
||||
MedicationInfo string `json:"medicationInfo" description:"正在服用的药物"`
|
||||
OtherDisease string `json:"otherDisease" description:"合并其他慢性疾病 (多个英文逗号分隔拼接)"`
|
||||
IsMedication *int `json:"isMedication" description:"是否服药 0否 1是"`
|
||||
IsHospital *int `json:"isHospital" description:"是否去医院 0否 1是"`
|
||||
IsAllergy *int `json:"isAllergy" description:"是否过敏史 0否 1是"`
|
||||
LiverStatus string `json:"liverStatus" description:"目前肝脏状态"`
|
||||
}
|
||||
|
||||
// DiseasesListData 根据app唯一标识获取用户病例信息-data详细数据-所患疾病数据
|
||||
type DiseasesListData struct {
|
||||
UUID string `json:"uuid" description:"疾病 uuid"`
|
||||
Year *int `json:"year" description:"患病时长"`
|
||||
Info string `json:"info" description:"丙肝基因型(仅针对丙肝)"`
|
||||
Name string `json:"name" description:"疾病名称"`
|
||||
}
|
||||
|
||||
// GetUserCaseByAppIden 根据app唯一标识获取用户病例信息
|
||||
func GetUserCaseByAppIden(appIden string) (g *GetUserCaseByAppIdenResponse, err error) {
|
||||
// 准备要发送的 JSON 数据
|
||||
requestData := GetUserCaseByAppIdenRequest{
|
||||
PatientUuid: appIden,
|
||||
Platform: platform,
|
||||
Timestamp: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
}
|
||||
|
||||
// 将 JSON 数据编码为字节数组
|
||||
jsonData, err := json.Marshal(requestData)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
err = json.Unmarshal(jsonData, &maps)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 获取请求签名
|
||||
sign, err := GenSignature(maps)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 准备请求体
|
||||
requestBody := bytes.NewBuffer(jsonData)
|
||||
|
||||
// 设置请求 URL
|
||||
url := apiUrl + "/patient-api/getDiseaseInfo"
|
||||
|
||||
// 创建 POST 请求
|
||||
req, err := http.NewRequest("POST", url, requestBody)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("sign", sign)
|
||||
|
||||
// 发送请求
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 检查响应状态码
|
||||
if resp.StatusCode != 200 {
|
||||
return g, errors.New("失败")
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &g)
|
||||
if err != nil {
|
||||
// json解析失败
|
||||
return g, err
|
||||
}
|
||||
|
||||
utils.LogJsonInfo("获取app数据返回", g)
|
||||
|
||||
if g.Code != 200 {
|
||||
if g.Msg != "" {
|
||||
return g, errors.New(g.Msg)
|
||||
} else {
|
||||
return g, errors.New("失败")
|
||||
}
|
||||
}
|
||||
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// UpdateUserCase 修改用户病例
|
||||
func UpdateUserCase(reqData UpdateUserCaseRequest) (g *UpdateUserCaseResponse, err error) {
|
||||
reqData.Platform = platform
|
||||
reqData.Timestamp = strconv.FormatInt(time.Now().Unix(), 10)
|
||||
|
||||
// 将 JSON 数据编码为字节数组
|
||||
jsonData, err := json.Marshal(reqData)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
err = json.Unmarshal(jsonData, &maps)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 获取请求签名
|
||||
sign, err := GenSignature(maps)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 准备请求体
|
||||
requestBody := bytes.NewBuffer(jsonData)
|
||||
|
||||
// 设置请求 URL
|
||||
url := apiUrl + "/patient-api/upDiseaseInfo"
|
||||
|
||||
// 创建 POST 请求
|
||||
req, err := http.NewRequest("POST", url, requestBody)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("sign", sign)
|
||||
|
||||
// 发送请求
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 检查响应状态码
|
||||
if resp.StatusCode != 200 {
|
||||
return g, errors.New("失败")
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &g)
|
||||
if err != nil {
|
||||
// json解析失败
|
||||
return g, err
|
||||
}
|
||||
|
||||
utils.LogJsonInfo("修改app数据返回", g)
|
||||
|
||||
if g.Code != 200 {
|
||||
if g.Msg != "" {
|
||||
return g, errors.New(g.Msg)
|
||||
} else {
|
||||
return g, errors.New("失败")
|
||||
}
|
||||
}
|
||||
|
||||
return g, nil
|
||||
}
|
||||
@@ -0,0 +1,239 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"vote-admin-api/utils"
|
||||
)
|
||||
|
||||
// GetInfoByMobileRequest 根据手机号获取用户信息-请求数据
|
||||
type GetInfoByMobileRequest struct {
|
||||
Mobile string `json:"mobile"` // 手机号
|
||||
Platform string `json:"platform"` // 所属平台
|
||||
Timestamp string `json:"timestamp"` // 当前时间戳(10位)
|
||||
}
|
||||
|
||||
// UpdateInfoRequest 修改用户信息-请求数据
|
||||
type UpdateInfoRequest struct {
|
||||
Birthday string `json:"birthday"` // 出生日期
|
||||
IsPegnant *int `json:"isPegnant"` // 是否怀孕 1无计划 2计划中 3已怀孕 4家有宝宝
|
||||
Sex *int `json:"sex"` // 性别 0男 1女
|
||||
Weight *int `json:"weight"` // 体重 KG
|
||||
ExpectedDateOfChildbirth string `json:"expectedDateOfChildbirth"` // 预产期
|
||||
IsHbv *int `json:"isHbv"` // 市区 id
|
||||
NationUuid string `json:"nationUuid"` // 民族 uuid
|
||||
PatientUuid string `json:"patientUuid"` // 患者 uuid
|
||||
Name string `json:"name"` // 姓名
|
||||
ProvId *int64 `json:"provId"` // 省份 id
|
||||
CityId *int64 `json:"cityId"` // 城市 id
|
||||
CountyId *int64 `json:"countyId"` // 市区 id
|
||||
Height *int `json:"height"` // 身高 cm
|
||||
Platform string `json:"platform"` // 所属平台
|
||||
Timestamp string `json:"timestamp"` // 当前时间戳(10位)
|
||||
}
|
||||
|
||||
// GetInfoByMobileResponse 根据手机号获取用户信息-返回数据
|
||||
type GetInfoByMobileResponse struct {
|
||||
Code int `json:"code"` // 接口调用状态。200:正常;其它值:调用出错
|
||||
Msg string `json:"msg"` // 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok
|
||||
Data GetInfoByMobileData `json:"data"` // 接口返回结果,各个接口自定义,数据结构参考具体文档说明
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// UpdateInfoResponse 修改用户信息-返回数据
|
||||
type UpdateInfoResponse struct {
|
||||
Code int `json:"code"` // 接口调用状态。200:正常;其它值:调用出错
|
||||
Msg string `json:"msg"` // 结果说明。如果接口调用出错,那么返回错误描述。成功则返回 ok
|
||||
Data string `json:"data"` // 接口返回结果,各个接口自定义,数据结构参考具体文档说明
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// GetInfoByMobileData 根据手机号获取用户信息-data详细数据
|
||||
type GetInfoByMobileData struct {
|
||||
Birthday string `json:"birthday" description:"出生日期"`
|
||||
IsPregnant *int `json:"isPregnant" description:"是否怀孕 1无计划 2计划中 3已怀孕 4家有宝宝"`
|
||||
Sex *int `json:"sex" description:"性别 0男 1女"`
|
||||
Mobile string `json:"mobile" description:"手机号"`
|
||||
Photo string `json:"photo" description:"头像地址"`
|
||||
Weight *int `json:"weight" description:"体重 KG"`
|
||||
CityID *int64 `json:"cityId" description:"城市 id"`
|
||||
ExpectedDateOfChildbirth string `json:"expectedDateOfChildbirth" description:"预产期"`
|
||||
CountyID *int64 `json:"countyId" description:"市区 id"`
|
||||
IsHBV *int `json:"isHbv" description:"有无 肝硬化或肝癌家族史 0无1有2未知"`
|
||||
NationUUID string `json:"nationUuid" description:"民族 uuid"`
|
||||
PatientUUID string `json:"patientUuid" description:"患者 uuid"`
|
||||
Name string `json:"name" description:"姓名"`
|
||||
ProvinceID *int64 `json:"provId" description:"省份 id"`
|
||||
Height *int `json:"height" description:"身高 cm"`
|
||||
OpenId string `json:"openid" description:"openid"`
|
||||
UnionId string `json:"unionid" description:"unionid"`
|
||||
}
|
||||
|
||||
// GetInfoByMobile 根据手机号获取用户信息
|
||||
func GetInfoByMobile(mobile string) (g *GetInfoByMobileResponse, err error) {
|
||||
// 准备要发送的 JSON 数据
|
||||
requestData := GetInfoByMobileRequest{
|
||||
Mobile: mobile,
|
||||
Platform: platform,
|
||||
Timestamp: strconv.FormatInt(time.Now().Unix(), 10),
|
||||
}
|
||||
|
||||
// 将 JSON 数据编码为字节数组
|
||||
jsonData, err := json.Marshal(requestData)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
err = json.Unmarshal(jsonData, &maps)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 获取请求签名
|
||||
sign, err := GenSignature(maps)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 准备请求体
|
||||
requestBody := bytes.NewBuffer(jsonData)
|
||||
|
||||
// 设置请求 URL
|
||||
url := apiUrl + "/patient-api/getInfo"
|
||||
|
||||
// 创建 POST 请求
|
||||
req, err := http.NewRequest("POST", url, requestBody)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("sign", sign)
|
||||
|
||||
// 发送请求
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 检查响应状态码
|
||||
if resp.StatusCode != 200 {
|
||||
return g, errors.New("失败")
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &g)
|
||||
if err != nil {
|
||||
// json解析失败
|
||||
return g, err
|
||||
}
|
||||
|
||||
utils.LogJsonInfo("获取app数据返回", g)
|
||||
|
||||
if g.Code != 200 {
|
||||
if g.Msg != "" {
|
||||
return g, errors.New(g.Msg)
|
||||
} else {
|
||||
return g, errors.New("失败")
|
||||
}
|
||||
}
|
||||
|
||||
return g, nil
|
||||
}
|
||||
|
||||
// UpdateInfo 修改用户信息
|
||||
func UpdateInfo(reqData UpdateInfoRequest) (g *UpdateInfoResponse, err error) {
|
||||
reqData.Platform = platform
|
||||
reqData.Timestamp = strconv.FormatInt(time.Now().Unix(), 10)
|
||||
|
||||
// 将 JSON 数据编码为字节数组
|
||||
jsonData, err := json.Marshal(reqData)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
maps := make(map[string]interface{})
|
||||
err = json.Unmarshal(jsonData, &maps)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 获取请求签名
|
||||
sign, err := GenSignature(maps)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 准备请求体
|
||||
requestBody := bytes.NewBuffer(jsonData)
|
||||
|
||||
// 设置请求 URL
|
||||
url := apiUrl + "/patient-api/updateInfo"
|
||||
|
||||
// 创建 POST 请求
|
||||
req, err := http.NewRequest("POST", url, requestBody)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 设置请求头
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("sign", sign)
|
||||
|
||||
// 发送请求
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return g, err
|
||||
}
|
||||
|
||||
// 检查响应状态码
|
||||
if resp.StatusCode != 200 {
|
||||
return g, errors.New("失败")
|
||||
}
|
||||
|
||||
err = json.Unmarshal(body, &g)
|
||||
if err != nil {
|
||||
// json解析失败
|
||||
return g, err
|
||||
}
|
||||
|
||||
utils.LogJsonInfo("修改app数据返回", g)
|
||||
|
||||
if g.Code != 200 {
|
||||
if g.Msg != "" {
|
||||
return g, errors.New(g.Msg)
|
||||
} else {
|
||||
return g, errors.New("失败")
|
||||
}
|
||||
}
|
||||
|
||||
return g, nil
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package weChat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
||||
"vote-admin-api/config"
|
||||
)
|
||||
|
||||
// 创建客户端
|
||||
func createClient() (*core.Client, error) {
|
||||
mchId := config.C.Wechat.Pay1281030301.MchId // 商户号
|
||||
mchCertificateSerialNumber := config.C.Wechat.Pay1281030301.MchCertificateSerialNumber // 商户证书序列号
|
||||
v3ApiSecret := config.C.Wechat.Pay1281030301.V3ApiSecret // 商户APIv3密钥
|
||||
privateKeyPath := config.C.Wechat.Pay1281030301.PrivateKey // 商户私钥文件地址
|
||||
|
||||
if mchId == "" {
|
||||
return nil, errors.New("商户号错误")
|
||||
}
|
||||
|
||||
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(privateKeyPath)
|
||||
if err != nil {
|
||||
return nil, errors.New("微信支付生成失败")
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
|
||||
opts := []core.ClientOption{
|
||||
option.WithWechatPayAutoAuthCipher(mchId, mchCertificateSerialNumber, mchPrivateKey, v3ApiSecret),
|
||||
}
|
||||
|
||||
client, err := core.NewClient(ctx, opts...)
|
||||
if err != nil {
|
||||
return nil, errors.New(err.Error())
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// WxPayResult 支付结果
|
||||
type WxPayResult struct {
|
||||
OrderStatus int `json:"order_status"` // 订单状态(1:待支付 2:已完成 3:已取消)
|
||||
PayStatus int `json:"pay_status"` // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||||
PayTime *string `json:"pay_time"` // 支付时间
|
||||
}
|
||||
|
||||
// WxPayRefundResult 退款结果
|
||||
type WxPayRefundResult struct {
|
||||
RefundStatus int `json:"refundStatus"` // 订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常 7:部分退款)
|
||||
SuccessTime *string `json:"successTime"` // 退款成功时间
|
||||
}
|
||||
|
||||
// HandlePayStatus 处理支付状态
|
||||
func HandlePayStatus(t *payments.Transaction) (w *WxPayResult, err error) {
|
||||
w = &WxPayResult{}
|
||||
|
||||
switch *t.TradeState {
|
||||
case "SUCCESS": // 支付成功
|
||||
w.OrderStatus = 2
|
||||
w.PayStatus = 2
|
||||
w.PayTime = t.SuccessTime
|
||||
case "CLOSED": // 已关闭
|
||||
w.PayStatus = 6
|
||||
case "REVOKED": // 已撤销(付款码支付)
|
||||
w.PayStatus = 7
|
||||
case "USERPAYING": // 用户支付中(付款码支付)
|
||||
w.PayStatus = 3
|
||||
case "PAYERROR": // 支付失败(其他原因,如银行返回失败)
|
||||
w.PayStatus = 4
|
||||
default:
|
||||
return nil, errors.New("未知支付状态")
|
||||
}
|
||||
|
||||
return w, nil
|
||||
}
|
||||
|
||||
// HandlePayRefundStatus 处理退款状态
|
||||
func HandlePayRefundStatus(r *refunddomestic.Refund) (w *WxPayRefundResult, err error) {
|
||||
w = &WxPayRefundResult{}
|
||||
|
||||
switch *r.Status {
|
||||
case "SUCCESS": // 退款成功
|
||||
w.RefundStatus = 3
|
||||
if r.SuccessTime != nil {
|
||||
successTime := r.SuccessTime.Format("2006-01-02 15:04:05")
|
||||
w.SuccessTime = &successTime
|
||||
}
|
||||
case "CLOSED": // 退款关闭
|
||||
w.RefundStatus = 5
|
||||
case "PROCESSING": // 退款处理中
|
||||
w.RefundStatus = 2
|
||||
case "ABNORMAL": // 退款异常
|
||||
return nil, errors.New("退款状态错误")
|
||||
default:
|
||||
return nil, errors.New("退款状态错误")
|
||||
}
|
||||
|
||||
return w, nil
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
-----BEGIN PRIVATE KEY-----
|
||||
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDR4fiYRUjnLQA+
|
||||
TPMslDvQIIyZm4ajWFcSXg6yozhdi/O+vuRhwgyeGXsE4SpvuxkK4HPZaocrsjqe
|
||||
68Y46DyhudzXIBy50IF2UeN5ilj7ydq5TJbBOi1iOF1lgOUNK6DGiOQ+folVNKyN
|
||||
sjrsA3RHUKHF/JdOfcpSGzvV79FnmjSll/KsdxEc9LCiAa5t7GYA9Hx7AIDG4i8b
|
||||
03d5EJXvE8BGUZQItE6VIpHwyySTHtGQkPyI6lqMKt2Jlx0TFHu0GkTEmhuA2Z5C
|
||||
5gOqYWclQO+X2ORgulL9Q3mNcpfy9uBynI/CJDbSvVTkNfwYB0Ny1knu/+in7C6p
|
||||
IPJYfZpPAgMBAAECggEAWb5tJPcjSC5W10ziAiLUPJdeZ2Q4OupQOPtc/4eJV367
|
||||
V8maMC7gZE3y61A4bBQtjhgRkVrat5V7OW8JkFXFb0XhJ1+EyPNeGDDFureseuWC
|
||||
EA+uuqrcsw306a0mw+3uzlXEevByWquuSNx4E2katE/HDLiIHjjtZRReDomAGfLw
|
||||
vLNZ40RgdNSXmwpVRoHOZnvX+C2Hxh4fKsrxs1HRCjFsSpTxKwsK6/Kdudj4hsaO
|
||||
Z5glqf4qopPlQIIkToxOe5p7ukwo7vqaqogixx3jOjruOMpzxZ24wU5AxneLcOYr
|
||||
ZC1659Uwv96TsKpueakWw0lq3TXkv9Qs+wDNI+NC6QKBgQDx4hWXplLYPYwgGgxG
|
||||
L9Pk+If9aDS9wj78JUaYBXKR5atXmf/62ufahW4IEDfmMugC1bm9MoYC7CmHv1nk
|
||||
nxckB84B20NOSiaxbkOaJuMHDtnNeXCmL8Gvtb8fTni1AM/g6XEleEw1r40LNZAq
|
||||
kdyQ1+OUFhRQGOvAe1SRr1rdGwKBgQDeIcauwMPkc3Seh6Fb66FQ4Lgmt1GvvuiG
|
||||
UJiwgwPFbEaKKczBHBL6hlKoAFweFa2Qw2xJ0H8jwb8RnkkNcAKYUyMc2wi85+Ih
|
||||
d568pvXMdYl4DGeGdd/sXKf4dGDjcb2AHzNfDuklbCsAojtO3pLWOs5U/RSKe8ps
|
||||
hhWa8nPO3QKBgQCIxuKU3X1tP+hz4qbcLYFxscQcXIeuYiABrwZrQnFV5Pxtzex9
|
||||
Krn+zIK61oj1iAXATKD6Ro6XKnoVg/POHtQUEMHCNP2rUKzumj5p9eFdBV3OHgTA
|
||||
RLMOrARGLLZ/C9WBBiBwIsVdekaUdxZtrAuAcEQFYjLcVCtDrbnVo8YKzwKBgEsg
|
||||
V0cBMP+RwM5hBsTE45Er/3wwofLzeUb7+Tgxh1P887qEuphRO2X5ifkB7iXKpSIB
|
||||
xh0M5AMe4tU9mG1wBaCo9YYr2j+xmTxCbbBWM2mMEwtD/rtuIGabS7/u9FnYPQQZ
|
||||
CVHMBDRA6iZTuAVLp5PG3cPGuGzBw0uC6cm22E4NAoGAKrbg3nFPKtzBJZ8Z0iNH
|
||||
G2gNotmHXbd4+gs4e3Iz9xsEabm4wCoEibNojNdMkX8zX267ebgfvesGEobROjW9
|
||||
Intvh39xi3aQ2Q5gvXzKdY0lDzgVvQ7udmLQ4dCUyYnpLr7Yac0asRxLge2esCWV
|
||||
x2uW2YTiUW3zsbHN/N/vJ5I=
|
||||
-----END PRIVATE KEY-----
|
||||
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIELjCCAxagAwIBAgIUfewObFfg3HHwd/AvUkBlZq85vrswDQYJKoZIhvcNAQEL
|
||||
BQAwXjELMAkGA1UEBhMCQ04xEzARBgNVBAoTClRlbnBheS5jb20xHTAbBgNVBAsT
|
||||
FFRlbnBheS5jb20gQ0EgQ2VudGV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mNhLml0cnVzLmNvbS5jbi9wdWJsaWMv
|
||||
aXRydXNjcmw/Q0E9MUJENDIyMEU1MERCQzA0QjA2QUQzOTc1NDk4NDZDMDFDM0U4
|
||||
RUJEMiZzZz1IQUNDNDcxQjY1NDIyRTEyQjI3QTlEMzNBODdBRDFDREY1OTI2RTE0
|
||||
MDM3MTANBgkqhkiG9w0BAQsFAAOCAQEAJX6C/QMYF0F3IiK9P8tW2DKN8y9vCU20
|
||||
6Ws8u4bcO3AaiSmsdAJ6I1MyZkUg3dKijtnDbieY2P364IEp48TmI4k6UJwP4+/f
|
||||
i0NseOm3BmAJ3mBoNmuFul+61opKpeV67AYQuhbehVA4cDPqKb/hP0tEcb6nefIO
|
||||
mnys5GReLWLFV2XFR1h9QtsohPOEYlpBl6lmKHNoQZdAPtSHq2iVFLbuD7GMKoj7
|
||||
Br2puJEQgxla1AsNxAYjGttEIO9I30+L5av7SKesSGXvL6G5eOvFrZl0S4EsNVz4
|
||||
QeL4qmtFEKDi2eaxeyLuWe9TMI/IiI3ngekoDyTwdnUzadumbio+dg==
|
||||
-----END CERTIFICATE-----
|
||||
Binary file not shown.
@@ -0,0 +1,25 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIENzCCAx+gAwIBAgIUEvql9hcIt5W7UzeukVSU4twsqHswDQYJKoZIhvcNAQEL
|
||||
BQAwXjELMAkGA1UEBhMCQ04xEzARBgNVBAoTClRlbnBheS5jb20xHTAbBgNVBAsT
|
||||
FFRlbnBheS5jb20gQ0EgQ2VudGVyMRswGQYDVQQDExJUZW5wYXkuY29tIFJvb3Qg
|
||||
Q0EwHhcNMjMxMjE5MDUxNTA4WhcNMjgxMjE3MDUxNTA4WjCBkDETMBEGA1UEAwwK
|
||||
MTY1OTY2MjkzNjEbMBkGA1UECgwS5b6u5L+h5ZWG5oi357O757ufMTwwOgYDVQQL
|
||||
DDPmiJDpg73ph5HniZvmrKPmrKPnm7jnhafkupLogZTnvZHljLvpmaLmnInpmZDl
|
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U02+EWqv
|
||||
gVZMgFwGKFCBzDiCs9QwOTO+32OWP62yapFWNba1g4K7Un7MJRsJIj0u0nwjik68
|
||||
hec5KFYTOYttzk9oOLgXLFM6zm0CgYEA933lgvPITFykd2kGKg1v4oA52PPmq9LT
|
||||
2dvVkDpIxioaUUHPALwFLhHLT+w/RPwCfP9gqZAQQFAfpMiTezm+ULA54rLHF2Tj
|
||||
Lm0q3ko98sFW3Ltk1ehw8CdU0p5bii7a7AbuS6V36iTIrC53rhl7N+WL82uMw2w/
|
||||
a8Gz3fIl3gsCgYA+XsHeZC8iBWddS5YXXX1X4e8bRU0JCzQzWpXLh/qDO5mozBzu
|
||||
eBiuy8HKqwRqKA2HtoRjzbT0cx+7o//9L1IcN3V/s7bmKCBzzjSMknE99OwcaIG6
|
||||
f1aaPoRARIyLAKhSgPWINMhBEcejC1vtQWqttu441cAgIP3ighulC/RI/QKBgQDE
|
||||
lxKqdK8USTqzR4+H8+h+CNDqjsMalXuGwGLiEAoirur8xMODl9adg7D2KXkQeQYY
|
||||
+Fp2FmNyUrLwGAtehL2yJmm0s8IFyTPUew7kSCDxJbaz2377k4mymet86iFYoGNx
|
||||
vQeouyWHrfRhIQAcIU2JVyNWFoZX7TJrjBAuKtz9hQKBgGUFIyhQiPYRWDocvuSi
|
||||
McyzYov2AnNehn24vsXC3SCXT804KPOX8RY4he63zWMGaLfCmSq1fhsJnf2pvffK
|
||||
W+9ZXlIH6dwgQ9QyDNjKRVCGwTASsWOyqgn1Be0VDuuoXlREyGNQSn2RpvL1SMFH
|
||||
PW/ePgdfoFkzU5WFJFPnvlU2
|
||||
-----END PRIVATE KEY-----
|
||||
@@ -0,0 +1,24 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIEFDCCAvygAwIBAgIUW1yKacyG0RJ/a2qgaqrxBTHu/pAwDQYJKoZIhvcNAQEL
|
||||
BQAwXjELMAkGA1UEBhMCQ04xEzARBgNVBAoTClRlbnBheS5jb20xHTAbBgNVBAsT
|
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zXP1dih101zl83iwIdPEHIR2skXcTjHRI2qdOvu7X5JmWOJP+51RGtX
|
||||
Y+aWXKfhRzRQOUlpNyltlgsGKzbIXLLBzQjMzBNv+n/HX4q9F0TV3SW4zTiMlhD8
|
||||
+bGGGwuIhziWpK9qvr3RPU1j+0bggHhIre+cNolnh1FepS4Gt964zhx6THtrS/jI
|
||||
I64UBBh6moBq7zB5QYloBhW464c7GCEEv5/AdcxGhAe+vuL/mkVRNsSRxVPIxPE6
|
||||
+qoIiNBmQvL/mL+4UKfX6b9h4wrUQUdQP3ljRdpL3a5YTMTUJuoJSQ==
|
||||
-----END CERTIFICATE-----
|
||||
@@ -0,0 +1,62 @@
|
||||
package weChat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/app"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
|
||||
"vote-admin-api/config"
|
||||
)
|
||||
|
||||
// CloseJsapiOrder 关闭jsapi支付订单
|
||||
func CloseJsapiOrder(outTradeNo string) error {
|
||||
client, err := createClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
svc := jsapi.JsapiApiService{Client: client}
|
||||
|
||||
req := jsapi.CloseOrderRequest{
|
||||
OutTradeNo: &outTradeNo,
|
||||
Mchid: core.String(config.C.Wechat.Pay1281030301.MchId),
|
||||
}
|
||||
|
||||
result, err := svc.CloseOrder(context.TODO(), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result.Response.StatusCode != 204 {
|
||||
return errors.New("关闭订单失败")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// CloseAppOrder 关闭app支付订单
|
||||
func CloseAppOrder(outTradeNo string) error {
|
||||
client, err := createClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
svc := app.AppApiService{Client: client}
|
||||
|
||||
req := app.CloseOrderRequest{
|
||||
OutTradeNo: &outTradeNo,
|
||||
Mchid: core.String(config.C.Wechat.Pay1281030301.MchId),
|
||||
}
|
||||
|
||||
result, err := svc.CloseOrder(context.TODO(), req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result.Response.StatusCode != 204 {
|
||||
return errors.New("关闭订单失败")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package weChat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/downloader"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/notify"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
||||
"vote-admin-api/config"
|
||||
)
|
||||
|
||||
// PayParseNotify 支付回调通知的验签与解密
|
||||
func PayParseNotify(c *gin.Context) (notifyReq *notify.Request, t *payments.Transaction, err error) {
|
||||
mchId := config.C.Wechat.Pay1281030301.MchId // 商户号
|
||||
mchCertificateSerialNumber := config.C.Wechat.Pay1281030301.MchCertificateSerialNumber // 商户证书序列号
|
||||
v3ApiSecret := config.C.Wechat.Pay1281030301.V3ApiSecret // 商户APIv3密钥
|
||||
privateKeyPath := "extend/weChat/certs/" + config.C.Wechat.Pay1281030301.MchId + "/apiclient_key.pem" // 商户私钥文件地址
|
||||
|
||||
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
|
||||
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(privateKeyPath)
|
||||
if err != nil {
|
||||
return nil, nil, errors.New("微信支付生成失败")
|
||||
}
|
||||
|
||||
// 1. 使用 `RegisterDownloaderWithPrivateKey` 注册下载器
|
||||
err = downloader.MgrInstance().RegisterDownloaderWithPrivateKey(c, mchPrivateKey, mchCertificateSerialNumber, mchId, v3ApiSecret)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// 2. 获取商户号对应的微信支付平台证书访问器
|
||||
certificateVisitor := downloader.MgrInstance().GetCertificateVisitor(mchId)
|
||||
// 3. 使用证书访问器初始化 `notify.Handler`
|
||||
handler, err := notify.NewRSANotifyHandler(v3ApiSecret, verifiers.NewSHA256WithRSAVerifier(certificateVisitor))
|
||||
|
||||
transaction := new(payments.Transaction)
|
||||
notifyReq, err = handler.ParseNotifyRequest(context.Background(), c.Request, transaction)
|
||||
// 如果验签未通过,或者解密失败
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return notifyReq, transaction, nil
|
||||
}
|
||||
|
||||
// RefundParseNotify 退款回调通知的验签与解密
|
||||
func RefundParseNotify(c *gin.Context) (notifyReq *notify.Request, t *refunddomestic.Refund, err error) {
|
||||
mchId := config.C.Wechat.Pay1281030301.MchId // 商户号
|
||||
mchCertificateSerialNumber := config.C.Wechat.Pay1281030301.MchCertificateSerialNumber // 商户证书序列号
|
||||
v3ApiSecret := config.C.Wechat.Pay1281030301.V3ApiSecret // 商户APIv3密钥
|
||||
privateKeyPath := "extend/weChat/certs/" + config.C.Wechat.Pay1281030301.MchId + "/apiclient_key.pem" // 商户私钥文件地址
|
||||
|
||||
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
|
||||
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(privateKeyPath)
|
||||
if err != nil {
|
||||
return nil, nil, errors.New("微信支付生成失败")
|
||||
}
|
||||
|
||||
// 1. 使用 `RegisterDownloaderWithPrivateKey` 注册下载器
|
||||
err = downloader.MgrInstance().RegisterDownloaderWithPrivateKey(c, mchPrivateKey, mchCertificateSerialNumber, mchId, v3ApiSecret)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
// 2. 获取商户号对应的微信支付平台证书访问器
|
||||
certificateVisitor := downloader.MgrInstance().GetCertificateVisitor(mchId)
|
||||
// 3. 使用证书访问器初始化 `notify.Handler`
|
||||
handler, err := notify.NewRSANotifyHandler(v3ApiSecret, verifiers.NewSHA256WithRSAVerifier(certificateVisitor))
|
||||
|
||||
refund := new(refunddomestic.Refund)
|
||||
notifyReq, err = handler.ParseNotifyRequest(context.Background(), c.Request, refund)
|
||||
// 如果验签未通过,或者解密失败
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return notifyReq, refund, nil
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
package weChat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/app"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
|
||||
)
|
||||
|
||||
/**
|
||||
JSAPI下单
|
||||
APP下单
|
||||
*/
|
||||
|
||||
// JsapiRequest 请求数据-JSAPI下单
|
||||
type JsapiRequest struct {
|
||||
AppId string `json:"appid" comment:"公众号ID"`
|
||||
MchId string `json:"mchid" comment:"直连商户号"`
|
||||
Description string `json:"description" comment:"商品描述"`
|
||||
OutTradeNo string `json:"out_trade_no" comment:"商户订单号"`
|
||||
NotifyUrl string `json:"notify_url" comment:"回调地址"`
|
||||
Amount JsapiRequestAmountRequest `json:"amount" comment:"订单金额"`
|
||||
Payer JsapiRequestPayerRequest `json:"payer" comment:"支付者"`
|
||||
}
|
||||
|
||||
// JsapiRequestAmountRequest 订单金额信息
|
||||
type JsapiRequestAmountRequest struct {
|
||||
Total int64 `json:"total" comment:"订单总金额"`
|
||||
Currency string `json:"currency" comment:"货币类型"`
|
||||
}
|
||||
|
||||
// JsapiRequestPayerRequest 支付者信息
|
||||
type JsapiRequestPayerRequest struct {
|
||||
OpenId string `json:"openid" comment:"openid"`
|
||||
}
|
||||
|
||||
// AppRequest 请求数据-APP下单
|
||||
type AppRequest struct {
|
||||
AppId string `json:"appid" comment:"公众号ID"`
|
||||
MchId string `json:"mchid" comment:"直连商户号"`
|
||||
Description string `json:"description" comment:"商品描述"`
|
||||
OutTradeNo string `json:"out_trade_no" comment:"商户订单号"`
|
||||
NotifyUrl string `json:"notify_url" comment:"回调地址"`
|
||||
Amount AppRequestAmountRequest `json:"amount" comment:"订单金额"`
|
||||
}
|
||||
|
||||
// AppRequestAmountRequest 订单金额信息
|
||||
type AppRequestAmountRequest struct {
|
||||
Total int64 `json:"total" comment:"订单总金额"`
|
||||
Currency string `json:"currency" comment:"货币类型"`
|
||||
}
|
||||
|
||||
// GetJsapiPrepay JSAPI下单-获取jsapi预支付交易会话
|
||||
func (r JsapiRequest) GetJsapiPrepay() (prepay *jsapi.PrepayWithRequestPaymentResponse, err error) {
|
||||
client, err := createClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
svc := jsapi.JsapiApiService{Client: client}
|
||||
// 得到prepay_id,以及调起支付所需的参数和签名
|
||||
PrepayRequest := jsapi.PrepayRequest{
|
||||
Appid: core.String(r.AppId),
|
||||
Mchid: core.String(r.MchId),
|
||||
Description: core.String(r.Description),
|
||||
OutTradeNo: core.String(r.OutTradeNo),
|
||||
NotifyUrl: core.String(r.NotifyUrl),
|
||||
Amount: &jsapi.Amount{
|
||||
Total: core.Int64(r.Amount.Total),
|
||||
Currency: core.String("CNY"),
|
||||
},
|
||||
Payer: &jsapi.Payer{
|
||||
Openid: core.String(r.Payer.OpenId),
|
||||
},
|
||||
}
|
||||
|
||||
resp, result, err := svc.PrepayWithRequestPayment(context.Background(),
|
||||
PrepayRequest,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result.Response.StatusCode != 200 {
|
||||
return nil, errors.New("发起支付失败")
|
||||
}
|
||||
|
||||
if resp.PrepayId == nil {
|
||||
return nil, errors.New("发起支付失败")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
// GetAppPrepay APP下单-获取app预支付交易会话
|
||||
func (r AppRequest) GetAppPrepay() (prepay *app.PrepayWithRequestPaymentResponse, err error) {
|
||||
client, err := createClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
svc := app.AppApiService{Client: client}
|
||||
// 得到prepay_id,以及调起支付所需的参数和签名
|
||||
|
||||
resp, result, err := svc.PrepayWithRequestPayment(context.Background(),
|
||||
app.PrepayRequest{
|
||||
Appid: core.String(r.AppId),
|
||||
Mchid: core.String(r.MchId),
|
||||
Description: core.String(r.Description),
|
||||
OutTradeNo: core.String(r.OutTradeNo),
|
||||
NotifyUrl: core.String(r.NotifyUrl),
|
||||
Amount: &app.Amount{
|
||||
Total: core.Int64(r.Amount.Total),
|
||||
Currency: core.String("CNY"),
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if result.Response.StatusCode != 200 {
|
||||
return nil, errors.New("发起支付失败")
|
||||
}
|
||||
|
||||
if resp.PrepayId == nil {
|
||||
return nil, errors.New("发起支付失败")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package weChat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
|
||||
)
|
||||
|
||||
type RefundRequest struct {
|
||||
TransactionId string `json:"transaction_id" comment:"微信订单号"`
|
||||
OutTradeNo string `json:"out_trade_no" comment:"商户订单号"`
|
||||
OutRefundNo string `json:"out_refund_no" comment:"退款订单号"`
|
||||
Reason string `json:"reason" comment:"退款原因"`
|
||||
RefundAmount int64 `json:"refund_amount" comment:"退款金额"`
|
||||
PaymentAmountTotal int64 `json:"payment_amount_total" comment:"支付金额"`
|
||||
NotifyUrl string `json:"notify_url" comment:"回调地址"`
|
||||
}
|
||||
|
||||
// Refund 退款
|
||||
func (r RefundRequest) Refund() (*refunddomestic.Refund, error) {
|
||||
client, err := createClient()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
refundRequest := refunddomestic.CreateRequest{
|
||||
TransactionId: core.String(r.TransactionId),
|
||||
OutTradeNo: core.String(r.OutTradeNo),
|
||||
OutRefundNo: core.String(r.OutRefundNo),
|
||||
Reason: core.String(r.Reason),
|
||||
NotifyUrl: core.String(r.NotifyUrl),
|
||||
Amount: &refunddomestic.AmountReq{
|
||||
Currency: core.String("CNY"),
|
||||
Refund: core.Int64(r.RefundAmount),
|
||||
Total: core.Int64(r.PaymentAmountTotal),
|
||||
},
|
||||
}
|
||||
|
||||
svc := refunddomestic.RefundsApiService{Client: client}
|
||||
resp, _, err := svc.Create(context.Background(), refundRequest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package weChat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// GetUserInfoResponse 网页授权拉取用户信息返回值
|
||||
type GetUserInfoResponse struct {
|
||||
OpenId string `json:"openid" form:"openid" label:"openid"`
|
||||
Nickname string `json:"nickname" form:"nickname" label:"用户昵称"`
|
||||
Sex int `json:"sex" form:"sex" label:"性别"` // 用户的性别,值为1时是男性,值为2时是女性,值为0时是未知
|
||||
Province string `json:"province" form:"province" label:"省份"`
|
||||
City string `json:"city" form:"city" label:"城市"`
|
||||
Country string `json:"country" form:"country" label:"国家"`
|
||||
HeadImgUrl string `json:"headimgurl" form:"headimgurl" label:"头像"`
|
||||
UnionId string `json:"unionid" form:"unionid" label:"unionid"`
|
||||
Errcode *int `json:"errcode" form:"errcode" label:"errcode"`
|
||||
Errmsg string `json:"errmsg" form:"errmsg" label:"errmsg"`
|
||||
}
|
||||
|
||||
// GetUserInfo 网页授权拉取用户信息
|
||||
func GetUserInfo(accessToken, openId string) (r *GetUserInfoResponse, err error) {
|
||||
if accessToken == "" {
|
||||
return nil, errors.New("授权失败")
|
||||
}
|
||||
|
||||
if openId == "" {
|
||||
return nil, errors.New("授权失败")
|
||||
}
|
||||
|
||||
// 拼接请求数据
|
||||
requestUrl := "https://api.weixin.qq.com/sns/userinfo?" +
|
||||
"access_token=" + accessToken +
|
||||
"&openid=" + openId +
|
||||
"&lang=zh_CN"
|
||||
|
||||
// 发送GET请求
|
||||
resp, err := http.Get(requestUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, errors.New("请求失败")
|
||||
}
|
||||
|
||||
// 读取响应体
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var response GetUserInfoResponse
|
||||
err = json.Unmarshal([]byte(respBody), &response)
|
||||
if err != nil {
|
||||
// json解析失败
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if response.Errcode != nil {
|
||||
if response.Errmsg != "" {
|
||||
return nil, errors.New(response.Errmsg)
|
||||
} else {
|
||||
return nil, errors.New("请求失败")
|
||||
}
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package weChat
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"vote-admin-api/config"
|
||||
)
|
||||
|
||||
// GetWebAccessTokenResponse 获取网页授权access_token返回值
|
||||
type GetWebAccessTokenResponse struct {
|
||||
AccessToken string `json:"access_token" form:"access_token" label:"网页授权接口调用凭证"`
|
||||
ExpiresIn int `json:"expires_in" form:"expires_in" label:"access_token接口调用凭证超时时间"`
|
||||
RefreshToken string `json:"refresh_token" form:"refresh_token" label:"用户刷新access_token"`
|
||||
OpenId string `json:"openid" form:"openid" label:"openid"`
|
||||
Scope string `json:"scope" form:"scope" label:"scope"`
|
||||
UnionId string `json:"unionid" form:"unionid" label:"unionid"`
|
||||
Errcode *int `json:"errcode" form:"errcode" label:"errcode"`
|
||||
Errmsg string `json:"errmsg" form:"errmsg" label:"errmsg"`
|
||||
}
|
||||
|
||||
// GetWebAccessToken 获取网页授权access_token
|
||||
func GetWebAccessToken(code string) (r *GetWebAccessTokenResponse, err error) {
|
||||
// 拼接请求数据
|
||||
requestUrl := "https://api.weixin.qq.com/sns/oauth2/access_token?" +
|
||||
"appid=" + config.C.Wechat.AppId +
|
||||
"&secret=" + config.C.Wechat.AppSecret +
|
||||
"&code=" + code +
|
||||
"&grant_type=authorization_code"
|
||||
|
||||
// 发送GET请求
|
||||
resp, err := http.Get(requestUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func(Body io.ReadCloser) {
|
||||
_ = Body.Close()
|
||||
}(resp.Body)
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
return nil, errors.New("请求失败")
|
||||
}
|
||||
|
||||
// 读取响应体
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var response GetWebAccessTokenResponse
|
||||
err = json.Unmarshal([]byte(respBody), &response)
|
||||
if err != nil {
|
||||
// json解析失败
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if response.Errcode != nil {
|
||||
if response.Errmsg != "" {
|
||||
return nil, errors.New(response.Errmsg)
|
||||
} else {
|
||||
return nil, errors.New("请求失败")
|
||||
}
|
||||
}
|
||||
|
||||
return &response, nil
|
||||
}
|
||||
Reference in New Issue
Block a user