package lianlian import ( zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils" "crypto/aes" "crypto/cipher" "encoding/base64" "errors" "fmt" "github.com/syyongx/php2go" "github.com/tidwall/gjson" "time" ) type AesEncrypter struct { key []byte iv []byte block cipher.Block } var AesEcpt AesEncrypter var channelId = "1870" var sysSecret = "WDgCblk9EOduT30aHVdeVg==" var Aeckey = "WDgCblk9EOduT30aHVdeVg==" var tbaseUrl = "https://uapid.lianlianlvyou.com/ll/api/" var channelIdProd = "3270" var sysSecretProd = "Qqwsr0tozSaZXh4eRBagcg==" var AeckeyProd = "Qqwsr0tozSaZXh4eRBagcg==" var tbaseUrlProd = "https://uapi.lianlianlvyou.com/ll/api/" //获取渠道账户信息 func GetBalance(prod bool) string { args := map[string]interface{}{ "flag": 1, } tmp := zhios_third_party_utils.SerializeStr(args) send, err := Send("channel/getChannelAccountInfo", tmp, prod) if err != nil || gjson.Get(send, "code").Int() != 200 { fmt.Println(send) return "" } encryptedData := CommEncryptedData(send, prod) return encryptedData } //查询站点列表 func GetLocationList(params map[string]string, prod bool) string { args := map[string]interface{}{} for k, v := range params { if v != "" { args[k] = v } } tmp := zhios_third_party_utils.SerializeStr(args) send, err := Send("location/getLocationList", tmp, prod) if err != nil || gjson.Get(send, "code").Int() != 200 { fmt.Println(send) return "" } encryptedData := CommEncryptedData(send, prod) return encryptedData } //查询产品分类 func ProductCate(params map[string]string, prod bool) string { args := map[string]interface{}{} for k, v := range params { if v != "" { args[k] = v } } tmp := zhios_third_party_utils.SerializeStr(args) send, err := Send("product/queryProductCategory", tmp, prod) if err != nil || gjson.Get(send, "code").Int() != 200 { fmt.Println(send) return "" } encryptedData := CommEncryptedData(send, prod) return encryptedData } //查询产品列表 func ProductList(params map[string]string, prod bool) string { args := map[string]interface{}{} for k, v := range params { if v != "" { args[k] = v } } tmp := zhios_third_party_utils.SerializeStr(args) send, err := Send("product/queryProductList", tmp, prod) if err != nil || gjson.Get(send, "code").Int() != 200 { fmt.Println(send) return "" } encryptedData := CommEncryptedData(send, prod) return encryptedData } //查询产品信息 func ProductDetail(params map[string]string, prod bool) string { args := map[string]interface{}{} for k, v := range params { if v != "" { args[k] = v } } tmp := zhios_third_party_utils.SerializeStr(args) send, err := Send("product/queryProductByCondition", tmp, prod) if err != nil || gjson.Get(send, "code").Int() != 200 { fmt.Println(send) return "" } encryptedData := CommEncryptedData(send, prod) return encryptedData } //查询产品图文详情 func ProductDetailImg(params map[string]string, prod bool) string { args := map[string]interface{}{} for k, v := range params { if v != "" { args[k] = v } } tmp := zhios_third_party_utils.SerializeStr(args) send, err := Send("product/detail/v2/html", tmp, prod) if err != nil || gjson.Get(send, "code").Int() != 200 { fmt.Println(send) return "" } encryptedData := CommEncryptedData(send, prod) return encryptedData } //查询产品图文详情 func ProductStatus(params map[string]string, prod bool) string { args := map[string]interface{}{} for k, v := range params { if v != "" { args[k] = v } } tmp := zhios_third_party_utils.SerializeStr(args) send, err := Send("product/queryProductStatus", tmp, prod) if err != nil || gjson.Get(send, "code").Int() != 200 { fmt.Println(send) return "" } encryptedData := CommEncryptedData(send, prod) return encryptedData } //创建渠道订单 func CreateOrder(params map[string]string, prod bool) (string, error) { args := map[string]interface{}{} for k, v := range params { if v != "" { args[k] = v } } tmp := zhios_third_party_utils.SerializeStr(args) fmt.Println(tmp) send, err := Send("channelOrder/createOrder", tmp, prod) if err != nil || gjson.Get(send, "code").Int() != 200 { fmt.Println(send) if err == nil { err = errors.New(gjson.Get(send, "message").String()) } return "", err } encryptedData := CommEncryptedData(send, prod) return encryptedData, nil } func CheckCreateOrder(params map[string]string, prod bool) (string, error) { args := map[string]interface{}{} for k, v := range params { if v != "" { args[k] = v } } tmp := zhios_third_party_utils.SerializeStr(args) fmt.Println(tmp) send, err := Send("channelOrder/checkCreateOrder", tmp, prod) if err != nil || gjson.Get(send, "code").Int() != 200 { fmt.Println(send) if err == nil { err = errors.New(gjson.Get(send, "message").String()) } return "", err } //encryptedData := CommEncryptedData(send,prod) return send, nil } //订单详情 func OrderDetail(params map[string]string, prod bool) string { args := map[string]interface{}{} for k, v := range params { if v != "" { args[k] = v } } tmp := zhios_third_party_utils.SerializeStr(args) fmt.Println(tmp) send, err := Send("orderDetail/queryOrderDetail", tmp, prod) if err != nil || gjson.Get(send, "code").Int() != 200 { fmt.Println(send) return "" } encryptedData := CommEncryptedData(send, prod) return encryptedData } //物流 func OrderExpress(params map[string]string, prod bool) string { args := map[string]interface{}{} for k, v := range params { if v != "" { args[k] = v } } tmp := zhios_third_party_utils.SerializeStr(args) fmt.Println(tmp) send, err := Send("orderDetail/queryOrderExpress", tmp, prod) if err != nil || gjson.Get(send, "code").Int() != 200 { fmt.Println(send) return "" } encryptedData := CommEncryptedData(send, prod) return encryptedData } //订单退款 func OrderApplyRefund(params map[string]interface{}, prod bool) (string, error) { args := map[string]interface{}{} for k, v := range params { if v != "" { args[k] = v } } tmp := zhios_third_party_utils.SerializeStr(args) fmt.Println(tmp) send, err := Send("order/applyRefund", tmp, prod) if err != nil || gjson.Get(send, "code").Int() != 200 { fmt.Println(send) if err == nil { err = errors.New(gjson.Get(send, "message").String()) } return "", err } encryptedData := CommEncryptedData(send, prod) return encryptedData, nil } func CommEncryptedData(send string, prod bool) string { Aeckey1, _ := php2go.Base64Decode(Aeckey) if prod { Aeckey1, _ = php2go.Base64Decode(AeckeyProd) } encrptedStr := gjson.Get(send, "data.encryptedData").String() encrptedStr, _ = php2go.Base64Decode(encrptedStr) encryptedData := AesDecryptECB([]byte(encrptedStr), []byte(Aeckey1)) fmt.Println(encryptedData) return encryptedData } func Send(method, params string, prod bool) (string, error) { reqUrl := tbaseUrl + method + "?" Aeckey1, _ := php2go.Base64Decode(Aeckey) if prod { reqUrl = tbaseUrlProd + method + "?" Aeckey1, _ = php2go.Base64Decode(AeckeyProd) } encryptedData := AesEncryptECB([]byte(params), []byte(Aeckey1)) nowTime := time.Now().Unix() * 1000 sysParams := map[string]interface{}{ "encryptedData": string(encryptedData), "channelId": channelId, "timestamp": nowTime, } if prod { sysParams["channelId"] = channelIdProd } sysParams["sign"] = getSign(string(encryptedData), nowTime, prod) post, err := zhios_third_party_utils.CurlPost(reqUrl, zhios_third_party_utils.SerializeStr(sysParams), map[string]string{}) if err != nil { return "", err } return string(post), nil } func getSign(encryptedData string, timestamp int64, prod bool) string { if prod { return php2go.Md5(encryptedData + channelIdProd + zhios_third_party_utils.Int64ToStr(timestamp)) } return php2go.Md5(encryptedData + channelId + zhios_third_party_utils.Int64ToStr(timestamp)) } func generateKey(key []byte) (genKey []byte) { genKey = make([]byte, 16) copy(genKey, key) for i := 16; i < len(key); { for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 { genKey[j] ^= key[i] } } return genKey } //http://events.jianshu.io/p/2e0b3ffeaeff func AesEncryptECB(origData []byte, key []byte) string { var encrypted []byte cipher, _ := aes.NewCipher(generateKey(key)) length := (len(origData) + aes.BlockSize) / aes.BlockSize plain := make([]byte, length*aes.BlockSize) copy(plain, origData) pad := byte(len(plain) - len(origData)) for i := len(origData); i < len(plain); i++ { plain[i] = pad } encrypted = make([]byte, len(plain)) // 分组分块加密 for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() { cipher.Encrypt(encrypted[bs:be], plain[bs:be]) } return base64.StdEncoding.EncodeToString(encrypted) } func AesDecryptECB(encrypted []byte, key []byte) string { var decrypted []byte cipher, _ := aes.NewCipher(generateKey(key)) decrypted = make([]byte, len(encrypted)) // for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() { cipher.Decrypt(decrypted[bs:be], encrypted[bs:be]) } trim := 0 if len(decrypted) > 0 { trim = len(decrypted) - int(decrypted[len(decrypted)-1]) } return string(decrypted[:trim]) }