第三方api接口
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

299 linhas
7.6 KiB

  1. package lianlian
  2. import (
  3. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "encoding/base64"
  7. "fmt"
  8. "github.com/syyongx/php2go"
  9. "github.com/tidwall/gjson"
  10. "time"
  11. )
  12. type AesEncrypter struct {
  13. key []byte
  14. iv []byte
  15. block cipher.Block
  16. }
  17. var AesEcpt AesEncrypter
  18. var channelId = "1870"
  19. var sysSecret = "WDgCblk9EOduT30aHVdeVg=="
  20. var Aeckey = "WDgCblk9EOduT30aHVdeVg=="
  21. var tbaseUrl = "https://uapid.lianlianlvyou.com/ll/api/"
  22. //获取渠道账户信息
  23. func GetBalance() string {
  24. args := map[string]interface{}{
  25. "flag": 1,
  26. }
  27. tmp := zhios_third_party_utils.SerializeStr(args)
  28. send, err := Send("channel/getChannelAccountInfo", tmp)
  29. if err != nil || gjson.Get(send, "code").Int() != 200 {
  30. fmt.Println(send)
  31. return ""
  32. }
  33. encryptedData := CommEncryptedData(send)
  34. return encryptedData
  35. }
  36. //查询站点列表
  37. func GetLocationList(params map[string]string) string {
  38. args := map[string]interface{}{}
  39. for k, v := range params {
  40. if v != "" {
  41. args[k] = v
  42. }
  43. }
  44. tmp := zhios_third_party_utils.SerializeStr(args)
  45. send, err := Send("location/getLocationList", tmp)
  46. if err != nil || gjson.Get(send, "code").Int() != 200 {
  47. fmt.Println(send)
  48. return ""
  49. }
  50. encryptedData := CommEncryptedData(send)
  51. return encryptedData
  52. }
  53. //查询产品分类
  54. func ProductCate(params map[string]string) string {
  55. args := map[string]interface{}{}
  56. for k, v := range params {
  57. if v != "" {
  58. args[k] = v
  59. }
  60. }
  61. tmp := zhios_third_party_utils.SerializeStr(args)
  62. send, err := Send("product/queryProductCategory", tmp)
  63. if err != nil || gjson.Get(send, "code").Int() != 200 {
  64. fmt.Println(send)
  65. return ""
  66. }
  67. encryptedData := CommEncryptedData(send)
  68. return encryptedData
  69. }
  70. //查询产品列表
  71. func ProductList(params map[string]string) string {
  72. args := map[string]interface{}{}
  73. for k, v := range params {
  74. if v != "" {
  75. args[k] = v
  76. }
  77. }
  78. tmp := zhios_third_party_utils.SerializeStr(args)
  79. send, err := Send("product/queryProductList", tmp)
  80. if err != nil || gjson.Get(send, "code").Int() != 200 {
  81. fmt.Println(send)
  82. return ""
  83. }
  84. encryptedData := CommEncryptedData(send)
  85. return encryptedData
  86. }
  87. //查询产品信息
  88. func ProductDetail(params map[string]string) string {
  89. args := map[string]interface{}{}
  90. for k, v := range params {
  91. if v != "" {
  92. args[k] = v
  93. }
  94. }
  95. tmp := zhios_third_party_utils.SerializeStr(args)
  96. send, err := Send("product/queryProductByCondition", tmp)
  97. if err != nil || gjson.Get(send, "code").Int() != 200 {
  98. fmt.Println(send)
  99. return ""
  100. }
  101. encryptedData := CommEncryptedData(send)
  102. return encryptedData
  103. }
  104. //查询产品图文详情
  105. func ProductDetailImg(params map[string]string) string {
  106. args := map[string]interface{}{}
  107. for k, v := range params {
  108. if v != "" {
  109. args[k] = v
  110. }
  111. }
  112. tmp := zhios_third_party_utils.SerializeStr(args)
  113. send, err := Send("product/detail/v2/html", tmp)
  114. if err != nil || gjson.Get(send, "code").Int() != 200 {
  115. fmt.Println(send)
  116. return ""
  117. }
  118. encryptedData := CommEncryptedData(send)
  119. return encryptedData
  120. }
  121. //查询产品图文详情
  122. func ProductStatus(params map[string]string) string {
  123. args := map[string]interface{}{}
  124. for k, v := range params {
  125. if v != "" {
  126. args[k] = v
  127. }
  128. }
  129. tmp := zhios_third_party_utils.SerializeStr(args)
  130. send, err := Send("product/queryProductStatus", tmp)
  131. if err != nil || gjson.Get(send, "code").Int() != 200 {
  132. fmt.Println(send)
  133. return ""
  134. }
  135. encryptedData := CommEncryptedData(send)
  136. return encryptedData
  137. }
  138. //创建渠道订单
  139. func CreateOrder(params map[string]string) string {
  140. args := map[string]interface{}{}
  141. for k, v := range params {
  142. if v != "" {
  143. args[k] = v
  144. }
  145. }
  146. tmp := zhios_third_party_utils.SerializeStr(args)
  147. fmt.Println(tmp)
  148. send, err := Send("channelOrder/createOrder", tmp)
  149. if err != nil || gjson.Get(send, "code").Int() != 200 {
  150. fmt.Println(send)
  151. return ""
  152. }
  153. encryptedData := CommEncryptedData(send)
  154. return encryptedData
  155. }
  156. //订单详情
  157. func OrderDetail(params map[string]string) string {
  158. args := map[string]interface{}{}
  159. for k, v := range params {
  160. if v != "" {
  161. args[k] = v
  162. }
  163. }
  164. tmp := zhios_third_party_utils.SerializeStr(args)
  165. fmt.Println(tmp)
  166. send, err := Send("orderDetail/queryOrderDetail", tmp)
  167. if err != nil || gjson.Get(send, "code").Int() != 200 {
  168. fmt.Println(send)
  169. return ""
  170. }
  171. encryptedData := CommEncryptedData(send)
  172. return encryptedData
  173. }
  174. //物流
  175. func OrderExpress(params map[string]string) string {
  176. args := map[string]interface{}{}
  177. for k, v := range params {
  178. if v != "" {
  179. args[k] = v
  180. }
  181. }
  182. tmp := zhios_third_party_utils.SerializeStr(args)
  183. fmt.Println(tmp)
  184. send, err := Send("orderDetail/queryOrderExpress", tmp)
  185. if err != nil || gjson.Get(send, "code").Int() != 200 {
  186. fmt.Println(send)
  187. return ""
  188. }
  189. encryptedData := CommEncryptedData(send)
  190. return encryptedData
  191. }
  192. //订单退款
  193. func OrderApplyRefund(params map[string]interface{}) string {
  194. args := map[string]interface{}{}
  195. for k, v := range params {
  196. if v != "" {
  197. args[k] = v
  198. }
  199. }
  200. tmp := zhios_third_party_utils.SerializeStr(args)
  201. fmt.Println(tmp)
  202. send, err := Send("order/applyRefund", tmp)
  203. if err != nil || gjson.Get(send, "code").Int() != 200 {
  204. fmt.Println(send)
  205. return ""
  206. }
  207. encryptedData := CommEncryptedData(send)
  208. return encryptedData
  209. }
  210. func CommEncryptedData(send string) string {
  211. encrptedStr := gjson.Get(send, "data.encryptedData").String()
  212. encrptedStr, _ = php2go.Base64Decode(encrptedStr)
  213. encryptedData := AesDecryptECB([]byte(encrptedStr), []byte(Aeckey))
  214. fmt.Println(encryptedData)
  215. return encryptedData
  216. }
  217. func Send(method, params string) (string, error) {
  218. reqUrl := tbaseUrl + method + "?"
  219. Aeckey, _ = php2go.Base64Decode(Aeckey)
  220. encryptedData := AesEncryptECB([]byte(params), []byte(Aeckey))
  221. nowTime := time.Now().Unix() * 1000
  222. sysParams := map[string]interface{}{
  223. "encryptedData": string(encryptedData),
  224. "channelId": channelId,
  225. "timestamp": nowTime,
  226. }
  227. sysParams["sign"] = getSign(string(encryptedData), nowTime)
  228. post, err := zhios_third_party_utils.CurlPost(reqUrl, zhios_third_party_utils.SerializeStr(sysParams), map[string]string{})
  229. if err != nil {
  230. return "", err
  231. }
  232. return string(post), nil
  233. }
  234. func getSign(encryptedData string, timestamp int64) string {
  235. return php2go.Md5(encryptedData + channelId + zhios_third_party_utils.Int64ToStr(timestamp))
  236. }
  237. func generateKey(key []byte) (genKey []byte) {
  238. genKey = make([]byte, 16)
  239. copy(genKey, key)
  240. for i := 16; i < len(key); {
  241. for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 {
  242. genKey[j] ^= key[i]
  243. }
  244. }
  245. return genKey
  246. }
  247. //http://events.jianshu.io/p/2e0b3ffeaeff
  248. func AesEncryptECB(origData []byte, key []byte) string {
  249. var encrypted []byte
  250. cipher, _ := aes.NewCipher(generateKey(key))
  251. length := (len(origData) + aes.BlockSize) / aes.BlockSize
  252. plain := make([]byte, length*aes.BlockSize)
  253. copy(plain, origData)
  254. pad := byte(len(plain) - len(origData))
  255. for i := len(origData); i < len(plain); i++ {
  256. plain[i] = pad
  257. }
  258. encrypted = make([]byte, len(plain))
  259. // 分组分块加密
  260. for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
  261. cipher.Encrypt(encrypted[bs:be], plain[bs:be])
  262. }
  263. return base64.StdEncoding.EncodeToString(encrypted)
  264. }
  265. func AesDecryptECB(encrypted []byte, key []byte) string {
  266. var decrypted []byte
  267. cipher, _ := aes.NewCipher(generateKey(key))
  268. decrypted = make([]byte, len(encrypted))
  269. //
  270. for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
  271. cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
  272. }
  273. trim := 0
  274. if len(decrypted) > 0 {
  275. trim = len(decrypted) - int(decrypted[len(decrypted)-1])
  276. }
  277. return string(decrypted[:trim])
  278. }