第三方api接口
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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