You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
340 lines
8.7 KiB
340 lines
8.7 KiB
package lgyf
|
|
|
|
import (
|
|
rsa2 "crypto/rsa"
|
|
"encoding/json"
|
|
"errors"
|
|
"git.oa00.com/go/rsa"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
PayStatusIng = 0 // 发佣处理中
|
|
PayStatusSuc = 1 // 发佣成功
|
|
PayStatusFail = 2 // 发佣失败
|
|
)
|
|
|
|
var Lgyf = &lgyf{}
|
|
|
|
type lgyf struct {
|
|
config Config
|
|
LgyfPublicKey *rsa2.PublicKey // 平台公钥
|
|
CustomerPrivateKey *rsa2.PrivateKey // 客户私钥
|
|
}
|
|
type Config struct {
|
|
LgyfPublicKey string // 平台公钥
|
|
CustomerPrivateKey string // 客户私钥
|
|
CustomerCode string // 客户编号
|
|
CustomerChannel string // 客户渠道
|
|
Tax string // 税源通道号
|
|
Url string // 服务器地址
|
|
Version string // 版本
|
|
CallbackUrl string // 回调地址
|
|
}
|
|
|
|
// InitLgyf @Title 初始化配置
|
|
func InitLgyf(config Config) (err error) {
|
|
Lgyf.config = config
|
|
Lgyf.LgyfPublicKey, err = rsa.ParsePublicKey(config.LgyfPublicKey)
|
|
if err != nil {
|
|
return errors.New("灵工云服公钥配置错误")
|
|
}
|
|
Lgyf.CustomerPrivateKey, err = rsa.ParsePrivateKey(config.CustomerPrivateKey, rsa.PKCS8)
|
|
if err != nil {
|
|
return errors.New("灵工云服公钥配置错误")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Demand struct {
|
|
TaskNo string `json:"task_no"`
|
|
ClientTaskNo string `json:"client_task_no"`
|
|
}
|
|
|
|
// Demand @Title 发布任务
|
|
func (l *lgyf) Demand(name, describe, orderSn string) (result Demand, err error) {
|
|
_, res, err := l.exec("makeDemand", map[string]string{
|
|
"task_name": name,
|
|
"task_rim": describe,
|
|
"task_type": "1",
|
|
"task_condition": describe,
|
|
"client_task_no": orderSn,
|
|
"pay_channel": l.config.Tax,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
json.Unmarshal([]byte(res), &result)
|
|
return
|
|
}
|
|
|
|
type QueryDemand struct {
|
|
TaskName string `json:"task_name"`
|
|
TaskCondition string `json:"task_condition"`
|
|
TaskNo string `json:"task_no"`
|
|
ClientTaskNo string `json:"client_task_no"`
|
|
PayChannel string `json:"pay_channel"`
|
|
TaskType string `json:"task_type"`
|
|
TaskRim string `json:"task_rim"`
|
|
}
|
|
|
|
// QueryDemandByTaskNo @Title 查询任务
|
|
func (l *lgyf) QueryDemandByTaskNo(taskNo string) (result QueryDemand, err error) {
|
|
_, res, err := l.exec("queryDemand", map[string]string{
|
|
"task_no": taskNo,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
json.Unmarshal([]byte(res), &result)
|
|
return
|
|
}
|
|
|
|
// QueryDemandByOrderSn @Title 查询任务
|
|
func (l *lgyf) QueryDemandByOrderSn(orderSn string) (result QueryDemand, err error) {
|
|
_, res, err := l.exec("queryDemand", map[string]string{
|
|
"client_task_no": orderSn,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
json.Unmarshal([]byte(res), &result)
|
|
return
|
|
}
|
|
|
|
type Register struct {
|
|
WorkerNo string `json:"worker_no"`
|
|
CertNo string `json:"cert_no"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// Register @Title 签约
|
|
func (l *lgyf) Register(name, idCard, bankName, cardNo, phone, idCardFrontHex, idCardReverseHex string) (result Register, err error) {
|
|
_, res, err := l.exec("enterpriseRegister", map[string]string{
|
|
"name": name,
|
|
"cert_no": idCard,
|
|
"bank_name": bankName,
|
|
"card_no": cardNo,
|
|
"phone": phone,
|
|
}, []param{
|
|
{"cert_pic_front", idCardFrontHex},
|
|
{"cert_pic_reverse", idCardReverseHex},
|
|
}...)
|
|
if err != nil {
|
|
return
|
|
}
|
|
json.Unmarshal([]byte(res), &result)
|
|
return
|
|
}
|
|
|
|
type QueryRegister struct {
|
|
WorkerNo string `json:"worker_no"`
|
|
CertNo string `json:"cert_no"`
|
|
Name string `json:"name"`
|
|
RegeditDate string `json:"regedit_date"`
|
|
}
|
|
|
|
// QueryRegister @Title 签约查询
|
|
func (l *lgyf) QueryRegister(idCard string) (result QueryRegister, err error) {
|
|
_, res, err := l.exec("queryEnterprise", map[string]string{
|
|
"cert_no": idCard,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
json.Unmarshal([]byte(res), &result)
|
|
return
|
|
}
|
|
|
|
// AddBankCard @Title 添加银行卡
|
|
func (l *lgyf) AddBankCard(name, idCard, bankName, cardNo, phone string) (result Register, err error) {
|
|
_, res, err := l.exec("enterpriseRegister", map[string]string{
|
|
"name": name,
|
|
"cert_no": idCard,
|
|
"bank_name": bankName,
|
|
"card_no": cardNo,
|
|
"phone": phone,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
json.Unmarshal([]byte(res), &result)
|
|
return
|
|
}
|
|
|
|
type QueryBalance struct {
|
|
UsableMoney string `json:"usable_money"`
|
|
FreezeMoney string `json:"freeze_money"`
|
|
PayChannel string `json:"pay_channel"`
|
|
TotalMoney string `json:"total_money"`
|
|
}
|
|
|
|
// QueryBalance @Title 账户余额查询
|
|
func (l *lgyf) QueryBalance() (result QueryBalance, err error) {
|
|
_, res, err := l.exec("queryBalance", map[string]string{
|
|
"pay_channel": l.config.Tax,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
json.Unmarshal([]byte(res), &result)
|
|
return
|
|
}
|
|
|
|
type QueryUserQuota struct {
|
|
Amount string `json:"amount"`
|
|
CertNo string `json:"cert_no"`
|
|
Month string `json:"month"`
|
|
Name string `json:"name"`
|
|
PayChannel string `json:"pay_channel"`
|
|
}
|
|
|
|
// QueryUserQuota @Title 单人可发额度查询
|
|
func (l *lgyf) QueryUserQuota(idCard, yearMonth string) (result QueryUserQuota, err error) {
|
|
_, res, err := l.exec("queryCertAmount", map[string]string{
|
|
"cert_no": idCard,
|
|
"month": yearMonth,
|
|
"pay_channel": l.config.Tax,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
json.Unmarshal([]byte(res), &result)
|
|
return
|
|
}
|
|
|
|
type Pay struct {
|
|
Amount string `json:"amount"`
|
|
OutTradeNo string `json:"out_trade_no"`
|
|
ClientOrderNo string `json:"client_order_no"`
|
|
TaskNo string `json:"task_no"`
|
|
CardNo string `json:"card_no"`
|
|
Name string `json:"name"`
|
|
PayChannel string `json:"pay_channel"`
|
|
ServiceAmt string `json:"service_amt"`
|
|
OrderRim string `json:"order_rim"`
|
|
}
|
|
|
|
// Pay @Title 单笔发佣
|
|
func (l *lgyf) Pay(name, idCard, cardNo, orderSn, amount, taskNo, remark string) (result Pay, err error) {
|
|
_, res, err := l.exec("pay", map[string]string{
|
|
"name": name,
|
|
"cert_no": idCard,
|
|
"card_no": cardNo,
|
|
"asy_url": l.config.CallbackUrl,
|
|
"client_order_no": orderSn,
|
|
"amount": amount,
|
|
"task_no": taskNo,
|
|
"pay_channel": l.config.Tax,
|
|
"order_rim": remark,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
json.Unmarshal([]byte(res), &result)
|
|
return
|
|
}
|
|
|
|
type Query struct {
|
|
Amount string `json:"amount"`
|
|
OutTradeNo string `json:"out_trade_no"`
|
|
CertNo string `json:"cert_no"`
|
|
ClientOrderNo string `json:"client_order_no"`
|
|
TaskNo string `json:"task_no"`
|
|
CardNo string `json:"card_no"`
|
|
Name string `json:"name"`
|
|
PayChannel string `json:"pay_channel"`
|
|
ServiceAmt string `json:"service_amt"`
|
|
OrderRim string `json:"order_rim"`
|
|
Status string `json:"status"`
|
|
Msg string `json:"msg"`
|
|
}
|
|
|
|
// Query @Title 单笔发佣查询
|
|
func (l *lgyf) Query(tradeNo string) (result Query, err error) {
|
|
req, res, err := l.exec("query", map[string]string{
|
|
"out_trade_no": tradeNo,
|
|
})
|
|
if err != nil {
|
|
return
|
|
}
|
|
json.Unmarshal([]byte(res), &result)
|
|
result.Msg = req.Msg
|
|
return
|
|
}
|
|
|
|
// PayCallback @Title 发佣回调
|
|
func (l *lgyf) PayCallback(requestStr string) (res Query, err error) {
|
|
req := result{}
|
|
json.Unmarshal([]byte(requestStr), &req)
|
|
if req.Code == "000" {
|
|
body, _ := rsa.DecryptWithSha1Base64(req.Data, l.CustomerPrivateKey)
|
|
json.Unmarshal([]byte(body), &res)
|
|
return
|
|
}
|
|
return res, errors.New(req.Msg)
|
|
}
|
|
|
|
// CallbackSuc @Title 回调处理成功
|
|
func (l *lgyf) CallbackSuc() string {
|
|
return ":sucess"
|
|
}
|
|
|
|
type result struct {
|
|
Msg string `json:"msg"`
|
|
Code string `json:"code"`
|
|
ClientNo string `json:"client_no"`
|
|
Data string `json:"data"`
|
|
Uuid string `json:"uuid"`
|
|
}
|
|
|
|
type param struct {
|
|
Key string
|
|
Value string
|
|
}
|
|
|
|
// @Title 调用接口
|
|
func (l *lgyf) exec(action string, data map[string]string, params ...param) (req result, body string, err error) {
|
|
urlData := map[string]string{
|
|
"version": l.config.Version,
|
|
"client_no": l.config.CustomerCode,
|
|
}
|
|
for _, param := range params {
|
|
urlData[param.Key] = param.Value
|
|
}
|
|
data["sign"] = l.sign(data)
|
|
marshal, _ := json.Marshal(&data)
|
|
enData, _ := rsa.EncryptWithSha1Base64(string(marshal), l.LgyfPublicKey)
|
|
urlData["data"] = enData
|
|
jsonData, _ := json.Marshal(&urlData)
|
|
bytes, err := request(post, l.config.Url+action, string(jsonData), map[string]string{
|
|
"Content-Type": "application/json",
|
|
})
|
|
if err != nil {
|
|
return req, body, errors.New("网络错误")
|
|
}
|
|
json.Unmarshal(bytes, &req)
|
|
if req.Code == "000" {
|
|
body, err = rsa.DecryptWithSha1Base64(req.Data, l.CustomerPrivateKey)
|
|
return
|
|
} else {
|
|
return req, body, errors.New(req.Msg)
|
|
}
|
|
}
|
|
|
|
// @Title 签名
|
|
func (l *lgyf) sign(data map[string]string) string {
|
|
var keys []string
|
|
for key, _ := range data {
|
|
keys = append(keys, key)
|
|
}
|
|
sort.Strings(keys)
|
|
var arrData []string
|
|
for _, key := range keys {
|
|
arrData = append(arrData, key+"="+data[key])
|
|
}
|
|
sign, _ := rsa.SignWithSha1Base64(strings.Join(arrData, "&"), l.CustomerPrivateKey)
|
|
return sign
|
|
}
|