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

api.go 4.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package sms
  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/syyongx/php2go"
  10. "github.com/tidwall/gjson"
  11. "strings"
  12. "time"
  13. "xorm.io/xorm"
  14. )
  15. //短信数量
  16. func SmsNumGetSmsNum(engine *xorm.Engine, smsType, uid interface{}) int {
  17. numData := offical.GetSmsNum(engine, smsType, uid)
  18. num := 0
  19. if numData != nil {
  20. num = numData.Num
  21. }
  22. if num < 0 {
  23. num = 0
  24. }
  25. return num
  26. }
  27. //发送短信
  28. func SmsSend(engine *xorm.Engine, args map[string]interface{}) error {
  29. num := SmsNumGetSmsNum(engine, args["sms_type"], args["uid"])
  30. ex := strings.Split(args["mobile"].(string), ",")
  31. if len(ex) == 0 {
  32. return errors.New("请输入手机号")
  33. }
  34. count := len(ex)
  35. contentLen := php2go.Ceil(float64(len(args["content"].(string))) / 4 / 70)
  36. if args["type"] == "mob" {
  37. contentLen = 1
  38. }
  39. if num < count*int(contentLen) {
  40. return errors.New("短信不足")
  41. }
  42. if args["type"] != "mob" { //联江短信数量
  43. send, err := SmsApiSend(engine, "api.v1.accountNum", map[string]string{})
  44. if err != nil {
  45. return err
  46. }
  47. platformCount := gjson.Get(send, "data.smsNum").String()
  48. if zhios_third_party_utils.StrToInt(platformCount) < count*int(contentLen) {
  49. return errors.New("平台短信不足")
  50. }
  51. }
  52. where := make(map[string]interface{})
  53. param := []string{
  54. "content", //短信内容
  55. "mobile", //号码多个以英文”,”号分割,最大 2000 个
  56. "code", //扩展号 0-9 数字,超过 8 位自动截取前 8 位
  57. "ext", //自定义信息,状态报告时返回,可以为空
  58. }
  59. for _, v := range param {
  60. if args[v] != "" {
  61. where[v] = args[v].(string)
  62. }
  63. }
  64. send := ""
  65. var err error
  66. if args["type"] == "mob" { //mob
  67. where = map[string]interface{}{
  68. "appkey": args["smsmsg_key"],
  69. "zone": args["zone"],
  70. "phone": args["phone"],
  71. "templateCode": args["templateCode"],
  72. }
  73. send, err = SmsMobApiSend(where)
  74. if err != nil {
  75. return err
  76. }
  77. status := gjson.Get(send, "status").Int()
  78. msg := gjson.Get(send, "error").String()
  79. if status == 471 {
  80. return errors.New("发送的ip不在白名单中")
  81. }
  82. if status == 406 {
  83. return errors.New("appkey不存在")
  84. }
  85. if status != 200 {
  86. return errors.New(msg)
  87. }
  88. } else {
  89. send, err = SmsApiSend(engine, "api.v1.sms", where)
  90. if err != nil {
  91. return err
  92. }
  93. success := gjson.Get(send, "success").Int()
  94. if success == 0 {
  95. return errors.New("发送失败")
  96. }
  97. }
  98. //存入记录
  99. ext := map[string]interface{}{
  100. "send": send,
  101. "post": where,
  102. }
  103. var record = model.SmsRecord{
  104. OrdId: "",
  105. Uid: int(zhios_third_party_utils.AnyToInt64(args["uid"])),
  106. Amount: zhios_third_party_utils.IntToStr(count * int(contentLen)),
  107. CostPrice: "",
  108. Balance: zhios_third_party_utils.IntToStr(num - count*int(contentLen)),
  109. PayWay: 0,
  110. State: 1,
  111. Memo: "发送短信",
  112. CreateAt: time.Now(),
  113. UpdateAt: time.Now(),
  114. TradeNo: "",
  115. Type: 1,
  116. OrdType: "buy",
  117. Fee: "",
  118. Ext: zhios_third_party_utils.SerializeStr(ext),
  119. SmsType: args["sms_type"].(string),
  120. }
  121. engine.InsertOne(&record)
  122. sql := `UPDATE sms_num_list set num=num-%d WHERE uid=%s and type=%s;`
  123. sql = fmt.Sprintf(sql, count*int(contentLen), args["uid"], args["sms_type"])
  124. db.QueryNativeString(engine, sql)
  125. return nil
  126. }
  127. func SmsApiSend(engine *xorm.Engine, method string, args map[string]string) (string, error) {
  128. smsmsgKey := offical.SysCfgByKey(engine, "smsmsg_key")
  129. smsmsgAccount := offical.SysCfgByKey(engine, "smsmsg_account")
  130. smsWebHost := offical.SysCfgByKey(engine, "sms_web_host")
  131. salesSmsmsgKey := offical.SysCfgByKey(engine, "sales_smsmsg_key")
  132. salesSmsmsgAccount := offical.SysCfgByKey(engine, "sales_smsmsg_account")
  133. if args["sms_type"] == "yingxiao" {
  134. smsmsgKey = salesSmsmsgKey
  135. smsmsgAccount = salesSmsmsgAccount
  136. }
  137. thisUrl := smsWebHost
  138. if thisUrl == "" {
  139. thisUrl = "http://api.ljioe.cn/"
  140. }
  141. thisUrl += strings.ReplaceAll(method, ".", "/")
  142. args["timestamp"] = zhios_third_party_utils.Int64ToStr(time.Now().Unix())
  143. args["account"] = smsmsgAccount
  144. args["key"] = php2go.Md5(smsmsgKey + php2go.Md5(args["timestamp"]))
  145. post, err := zhios_third_party_utils.CurlPost(thisUrl, zhios_third_party_utils.SerializeStr(args), nil)
  146. fmt.Println(string(post))
  147. fmt.Println(err)
  148. return string(post), err
  149. }
  150. func SmsMobApiSend(args map[string]interface{}) (string, error) {
  151. thisUrl := "https://webapi.sms.mob.com/sms/sendmsg"
  152. post, err := zhios_third_party_utils.CurlPost(thisUrl, zhios_third_party_utils.SerializeStr(args), nil)
  153. fmt.Println(string(post))
  154. fmt.Println(err)
  155. return string(post), err
  156. }