广告平台(站长下代理使用)
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.
 
 
 
 
 
 

143 lines
3.7 KiB

  1. package hdl
  2. import (
  3. implement2 "applet/app/db/implement"
  4. "applet/app/e"
  5. "applet/app/lib/validate"
  6. "applet/app/md"
  7. "applet/app/svc"
  8. "applet/app/utils"
  9. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/sms"
  10. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  11. db "code.fnuoos.com/zhimeng/model.git/src"
  12. "code.fnuoos.com/zhimeng/model.git/src/implement"
  13. implement3 "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  14. "code.fnuoos.com/zhimeng/model.git/src/super/model"
  15. "fmt"
  16. "github.com/gin-gonic/gin"
  17. "github.com/tidwall/gjson"
  18. "time"
  19. )
  20. // Sms
  21. // @Summary 短信发送
  22. // @Tags 短信
  23. // @Description 短信-短信发送
  24. // @param Authorization header string false "验证参数Bearer和token空格拼接"
  25. // @Accept json
  26. // @Produce json
  27. // @Param args body md.SmsReq true "请求参数"
  28. // @Success 200 {string} "success"
  29. // @Failure 400 {object} md.Response "具体错误"
  30. // @Router /api/sms [POST]
  31. func Sms(c *gin.Context) {
  32. var args md.SmsReq
  33. err := c.ShouldBindJSON(&args)
  34. if err != nil {
  35. err = validate.HandleValidateErr(err)
  36. err1 := err.(e.E)
  37. e.OutErr(c, err1.Code, err1.Error())
  38. return
  39. }
  40. if args.Type == "fast_login" {
  41. NewAdminDb := implement.NewAgentDb(svc.MasterDb(c))
  42. admin, _ := NewAdminDb.GetAgentByUsername(args.Phone)
  43. if admin == nil {
  44. e.OutErr(c, 400, e.NewErr(400, "账号未注册"))
  45. return
  46. }
  47. }
  48. if args.Type == "update_password" {
  49. admin, _ := svc.CheckUser(c)
  50. if admin == nil {
  51. e.OutErr(c, 400, e.NewErr(400, "账号未注册"))
  52. return
  53. }
  54. args.Phone = admin.Username
  55. }
  56. NewSysCfgDb := implement2.NewSysCfgDb(svc.MasterDb(c), c.GetString("mid"))
  57. key := NewSysCfgDb.SysCfgGetWithDb("mob_app_key")
  58. if key == "" {
  59. e.OutErr(c, 400, e.NewErr(400, "appkey不存在"))
  60. return
  61. }
  62. count := SmsNumGetSmsNum(c.GetString("mid"))
  63. if count-1 < 0 {
  64. e.OutErr(c, 400, e.NewErr(400, "短信数量不足"))
  65. return
  66. }
  67. where := map[string]interface{}{
  68. "appkey": key,
  69. "zone": "86",
  70. "phone": args.Phone,
  71. }
  72. templateCode := NewSysCfgDb.SysCfgGetWithDb("sms_code")
  73. if templateCode != "" {
  74. where["templateCode"] = templateCode
  75. }
  76. send, err := sms.SmsMobApiSend(where)
  77. if err != nil {
  78. e.OutErr(c, 400, err.Error())
  79. return
  80. }
  81. status := gjson.Get(send, "status").Int()
  82. msg := gjson.Get(send, "error").String()
  83. if status == 471 {
  84. e.OutErr(c, 400, e.NewErr(400, "发送的ip不在白名单中"))
  85. return
  86. }
  87. if status == 406 {
  88. e.OutErr(c, 400, e.NewErr(400, "appkey不存在"))
  89. return
  90. }
  91. if status != 200 {
  92. e.OutErr(c, 400, e.NewErr(400, msg))
  93. return
  94. }
  95. //存入记录
  96. ext := map[string]interface{}{
  97. "send": send,
  98. "post": where,
  99. }
  100. var record = model.SmsRecord{
  101. OrdId: "",
  102. Uid: utils.StrToInt(c.GetString("mid")),
  103. Amount: "1",
  104. CostPrice: "",
  105. Balance: zhios_third_party_utils.IntToStr(count - 1),
  106. PayWay: 0,
  107. State: 1,
  108. Memo: "发送短信",
  109. CreateAt: time.Now(),
  110. UpdateAt: time.Now(),
  111. TradeNo: "",
  112. Type: 1,
  113. OrdType: "buy",
  114. Fee: "",
  115. Ext: zhios_third_party_utils.SerializeStr(ext),
  116. Phone: args.Phone,
  117. }
  118. db.Db.InsertOne(&record)
  119. sql := `UPDATE sms_num_list set num=num-%d WHERE uid=%s ;`
  120. sql = fmt.Sprintf(sql, 1, c.GetString("mid"))
  121. fmt.Println(sql)
  122. nativeString, err := db.QueryNativeString(db.Db, sql)
  123. fmt.Println(nativeString)
  124. fmt.Println(err)
  125. e.OutSuc(c, "success", nil)
  126. return
  127. }
  128. func SmsNumGetSmsNum(uid interface{}) int {
  129. NewSmsNumListDb := implement3.NewSmsNumListDb(db.Db)
  130. numData := NewSmsNumListDb.GetSmsNum(uid)
  131. num := 0
  132. if numData != nil {
  133. num = numData.Num
  134. }
  135. if num < 0 {
  136. num = 0
  137. }
  138. return num
  139. }