第三方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 6.3 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
6 months 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
6 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
4 months 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 SmsSendZy(engine *xorm.Engine, args map[string]interface{}) error {
  135. where := make(map[string]interface{})
  136. param := []string{
  137. "content", //短信内容
  138. "mobile", //号码多个以英文”,”号分割,最大 2000 个
  139. "code", //扩展号 0-9 数字,超过 8 位自动截取前 8 位
  140. "ext", //自定义信息,状态报告时返回,可以为空
  141. }
  142. for _, v := range param {
  143. if args[v] != "" && args[v] != nil {
  144. where[v] = args[v].(string)
  145. }
  146. }
  147. send := ""
  148. var err error
  149. if args["type"] == "mob" { //mob
  150. if args["templateCode"] == "" {
  151. args["templateCode"] = "normal"
  152. }
  153. where = map[string]interface{}{
  154. "appkey": args["smsmsg_key"],
  155. "zone": args["zone"],
  156. "phone": args["mobile"],
  157. "templateCode": args["templateCode"],
  158. }
  159. send, err = SmsMobApiSend(where)
  160. if err != nil {
  161. return err
  162. }
  163. status := gjson.Get(send, "status").Int()
  164. msg := gjson.Get(send, "error").String()
  165. if status == 471 {
  166. return errors.New("发送的ip不在白名单中")
  167. }
  168. if status == 406 {
  169. return errors.New("appkey不存在")
  170. }
  171. if status != 200 {
  172. return errors.New(msg)
  173. }
  174. } else {
  175. send, err = SmsApiSend(engine, "api.v1.sms", where)
  176. if err != nil {
  177. return err
  178. }
  179. success := gjson.Get(send, "code").Int()
  180. if success != 20000 {
  181. return errors.New("发送失败")
  182. }
  183. }
  184. return nil
  185. }
  186. func SmsApiSend(engine *xorm.Engine, method string, args map[string]interface{}) (string, error) {
  187. smsmsgKey := offical.SysCfgByKey(engine, "smsmsg_key")
  188. smsmsgAccount := offical.SysCfgByKey(engine, "smsmsg_account")
  189. smsWebHost := offical.SysCfgByKey(engine, "sms_web_host")
  190. salesSmsmsgKey := offical.SysCfgByKey(engine, "sales_smsmsg_key")
  191. salesSmsmsgAccount := offical.SysCfgByKey(engine, "sales_smsmsg_account")
  192. if args["sms_type"] == "yingxiao" {
  193. smsmsgKey = salesSmsmsgKey
  194. smsmsgAccount = salesSmsmsgAccount
  195. }
  196. thisUrl := smsWebHost
  197. if thisUrl == "" {
  198. thisUrl = "http://api.ljioe.cn/"
  199. }
  200. thisUrl += strings.ReplaceAll(method, ".", "/")
  201. args["timestamp"] = zhios_third_party_utils.Int64ToStr(time.Now().Unix())
  202. args["account"] = smsmsgAccount
  203. args["key"] = php2go.Md5(smsmsgKey + php2go.Md5(args["timestamp"].(string)))
  204. post, err := zhios_third_party_utils.CurlPost(thisUrl, zhios_third_party_utils.SerializeStr(args), nil)
  205. fmt.Println(string(post))
  206. fmt.Println(err)
  207. return string(post), err
  208. }
  209. func SmsMobApiSend(args map[string]interface{}) (string, error) {
  210. thisUrl := "https://webapi.sms.mob.com/sms/sendmsg"
  211. post, err := zhios_third_party_utils.CurlPost(thisUrl, args, nil)
  212. fmt.Println(string(post))
  213. fmt.Println(err)
  214. return string(post), err
  215. }