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

169 lines
4.9 KiB

  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]interface{}{})
  44. if err != nil {
  45. return err
  46. }
  47. platformCount := gjson.Get(send, "smsNum").Int()
  48. if int(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] != "" && args[v] != nil {
  61. where[v] = args[v].(string)
  62. }
  63. }
  64. send := ""
  65. var err error
  66. if args["type"] == "mob" { //mob
  67. if args["templateCode"] == "" {
  68. args["templateCode"] = "normal"
  69. }
  70. where = map[string]interface{}{
  71. "appkey": args["smsmsg_key"],
  72. "zone": args["zone"],
  73. "phone": args["mobile"],
  74. "templateCode": args["templateCode"],
  75. }
  76. send, err = SmsMobApiSend(where)
  77. if err != nil {
  78. return err
  79. }
  80. status := gjson.Get(send, "status").Int()
  81. msg := gjson.Get(send, "error").String()
  82. if status == 471 {
  83. return errors.New("发送的ip不在白名单中")
  84. }
  85. if status == 406 {
  86. return errors.New("appkey不存在")
  87. }
  88. if status != 200 {
  89. return errors.New(msg)
  90. }
  91. } else {
  92. send, err = SmsApiSend(engine, "api.v1.sms", where)
  93. if err != nil {
  94. return err
  95. }
  96. success := gjson.Get(send, "code").Int()
  97. if success != 20000 {
  98. return errors.New("发送失败")
  99. }
  100. }
  101. //存入记录
  102. ext := map[string]interface{}{
  103. "send": send,
  104. "post": where,
  105. }
  106. var record = model.SmsRecord{
  107. OrdId: "",
  108. Uid: int(zhios_third_party_utils.AnyToInt64(args["uid"])),
  109. Amount: zhios_third_party_utils.IntToStr(count * int(contentLen)),
  110. CostPrice: "",
  111. Balance: zhios_third_party_utils.IntToStr(num - count*int(contentLen)),
  112. PayWay: 0,
  113. State: 1,
  114. Memo: "发送短信",
  115. CreateAt: time.Now(),
  116. UpdateAt: time.Now(),
  117. TradeNo: "",
  118. Type: 1,
  119. OrdType: "buy",
  120. Fee: "",
  121. Ext: zhios_third_party_utils.SerializeStr(ext),
  122. SmsType: args["sms_type"].(string),
  123. Phone: args["mobile"].(string),
  124. }
  125. engine.InsertOne(&record)
  126. sql := `UPDATE sms_num_list set num=num-%d WHERE uid=%s and type='%s';`
  127. sql = fmt.Sprintf(sql, count*int(contentLen), args["uid"], args["sms_type"])
  128. fmt.Println(sql)
  129. nativeString, err := db.QueryNativeString(engine, sql)
  130. fmt.Println(nativeString)
  131. fmt.Println(err)
  132. return nil
  133. }
  134. func SmsApiSend(engine *xorm.Engine, method string, args map[string]interface{}) (string, error) {
  135. smsmsgKey := offical.SysCfgByKey(engine, "smsmsg_key")
  136. smsmsgAccount := offical.SysCfgByKey(engine, "smsmsg_account")
  137. smsWebHost := offical.SysCfgByKey(engine, "sms_web_host")
  138. salesSmsmsgKey := offical.SysCfgByKey(engine, "sales_smsmsg_key")
  139. salesSmsmsgAccount := offical.SysCfgByKey(engine, "sales_smsmsg_account")
  140. if args["sms_type"] == "yingxiao" {
  141. smsmsgKey = salesSmsmsgKey
  142. smsmsgAccount = salesSmsmsgAccount
  143. }
  144. thisUrl := smsWebHost
  145. if thisUrl == "" {
  146. thisUrl = "http://api.ljioe.cn/"
  147. }
  148. thisUrl += strings.ReplaceAll(method, ".", "/")
  149. args["timestamp"] = zhios_third_party_utils.Int64ToStr(time.Now().Unix())
  150. args["account"] = smsmsgAccount
  151. args["key"] = php2go.Md5(smsmsgKey + php2go.Md5(args["timestamp"].(string)))
  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. }
  157. func SmsMobApiSend(args map[string]interface{}) (string, error) {
  158. thisUrl := "https://webapi.sms.mob.com/sms/sendmsg"
  159. post, err := zhios_third_party_utils.CurlPost(thisUrl, args, nil)
  160. fmt.Println(string(post))
  161. fmt.Println(err)
  162. return string(post), err
  163. }