第三方api接口
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.

84 lines
2.2 KiB

  1. package card_pack
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/db"
  4. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/db/offical"
  5. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/db/offical/model"
  6. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  7. "errors"
  8. "fmt"
  9. "github.com/tidwall/gjson"
  10. "time"
  11. "xorm.io/xorm"
  12. )
  13. //数量
  14. func CardPackNumGetNum(engine *xorm.Engine, uid interface{}) int {
  15. numData := offical.GetCardPackNum(engine, uid)
  16. num := 0
  17. if numData != nil {
  18. num = numData.Num
  19. }
  20. if num < 0 {
  21. num = 0
  22. }
  23. return num
  24. }
  25. //识别
  26. func CardPackSend(engine *xorm.Engine, args map[string]interface{}) (string, error) {
  27. num := CardPackNumGetNum(engine, args["uid"])
  28. if num < 1 {
  29. return "", errors.New("数量不足")
  30. }
  31. send, err2 := Send(args["method"].(string), args["appcode"].(string), args["body"].(string))
  32. if err2 != nil {
  33. return "", err2
  34. }
  35. if gjson.Get(send, "success").Bool() == false {
  36. return "", errors.New("识别失败")
  37. }
  38. //存入记录
  39. ext := map[string]interface{}{
  40. "send": send,
  41. "post": args,
  42. }
  43. var record = model.CardPackRecord{
  44. OrdId: "",
  45. Uid: int(zhios_third_party_utils.AnyToInt64(args["uid"])),
  46. Amount: "1",
  47. CostPrice: "",
  48. Balance: zhios_third_party_utils.IntToStr(num - 1),
  49. PayWay: 0,
  50. State: 1,
  51. Memo: args["name"].(string),
  52. CreateAt: time.Now(),
  53. UpdateAt: time.Now(),
  54. TradeNo: "",
  55. Type: 1,
  56. OrdType: "buy",
  57. Fee: "",
  58. Ext: zhios_third_party_utils.SerializeStr(ext),
  59. }
  60. engine.InsertOne(&record)
  61. sql := `UPDATE card_pack_num_list set num=num-%d WHERE uid=%s ;`
  62. sql = fmt.Sprintf(sql, 1, args["uid"])
  63. fmt.Println(sql)
  64. nativeString, err := db.QueryNativeString(engine, sql)
  65. fmt.Println(nativeString)
  66. fmt.Println(err)
  67. return send, nil
  68. }
  69. func Send(method, appcode, body string) (string, error) {
  70. host := "https://cardpack.market.alicloudapi.com"
  71. headers := map[string]string{
  72. "Authorization": "APPCODE " + appcode,
  73. "Content-Type": "application/json; charset=UTF-8",
  74. }
  75. url := host + method
  76. post, err := zhios_third_party_utils.CurlPost(url, body, headers)
  77. fmt.Println(string(post))
  78. fmt.Println(err)
  79. return string(post), err
  80. }