面包店
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

256 righe
8.1 KiB

  1. package push
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. "applet/app/md"
  7. "applet/app/utils"
  8. "applet/app/utils/logx"
  9. "encoding/json"
  10. "errors"
  11. "fmt"
  12. "strings"
  13. "time"
  14. "xorm.io/xorm"
  15. "github.com/tidwall/gjson"
  16. )
  17. //公共处理推送数据
  18. func CommAddPush(eg *xorm.Engine, args md.PushParams) {
  19. // 后台mob推送
  20. pushArgs := make(map[string]string)
  21. if args.DomainWapBase == "" {
  22. args.DomainWapBase = "http://h5." + args.MasterId + ".izhyin.com"
  23. if cfg.Prd {
  24. args.DomainWapBase = "http://h5." + args.MasterId + ".zhiyingos.com"
  25. }
  26. }
  27. if args.PushType != "" {
  28. temp, err := db.SysPushTemplateByType(eg, args.PushType)
  29. if err != nil {
  30. //e.OutErr(c, e.ERR_DB_ORM, err)
  31. return
  32. }
  33. if temp != nil { //处理有模板的数据
  34. if temp.IsAppPush != 1 {
  35. return
  36. }
  37. args.Title = temp.Title
  38. args.Content = temp.Content
  39. args.Skip = temp.Skip
  40. //替换链接的一些参数
  41. if strings.Contains(args.Skip, "[replace_APP_URL]") {
  42. args.Skip = strings.Replace(args.Skip, "[replace_APP_URL]", args.DomainWapBase, -1)
  43. }
  44. if strings.Contains(args.Skip, "[replace_masterId]") {
  45. args.Skip = strings.Replace(args.Skip, "[replace_masterId]", args.MasterId, -1)
  46. }
  47. if strings.Contains(args.Skip, "[replace_uid]") {
  48. args.Skip = strings.Replace(args.Skip, "[replace_uid]", utils.IntToStr(args.Uid), -1)
  49. }
  50. if strings.Contains(args.Skip, "skipIdentifier") {
  51. args.Skip = strings.Replace(args.Skip, "skipIdentifier", "skip_identifier", -1)
  52. }
  53. if strings.Contains(args.Skip, "requiredLogin") {
  54. args.Skip = strings.Replace(args.Skip, "requiredLogin", "required_login", -1)
  55. }
  56. if strings.Contains(args.Skip, "requiredTaobaoAuth") {
  57. args.Skip = strings.Replace(args.Skip, "requiredTaobaoAuth", "required_taobao_auth", -1)
  58. }
  59. if strings.Contains(args.Skip, "activityId") {
  60. args.Skip = strings.Replace(args.Skip, "activityId", "activity_id", -1)
  61. }
  62. if strings.Contains(args.Skip, "sourceType") {
  63. args.Skip = strings.Replace(args.Skip, "sourceType", "source_type", -1)
  64. }
  65. if strings.Contains(args.Skip, "brandId") {
  66. args.Skip = strings.Replace(args.Skip, "brandId", "brand_id", -1)
  67. }
  68. args.Content = strings.Replace(args.Content, "[APP名称]", args.AppName, -1)
  69. args.Content = strings.Replace(args.Content, "[会员昵称]", args.Nickname, -1)
  70. args.Content = strings.Replace(args.Content, "[付款金额]", args.Payment, -1)
  71. args.Content = strings.Replace(args.Content, "[下单人昵称]", args.OrderNickname, -1)
  72. args.Content = strings.Replace(args.Content, "[订单号]", args.OrderNo, -1)
  73. args.Content = strings.Replace(args.Content, "[下单时间]", args.OrderTime, -1)
  74. args.Content = strings.Replace(args.Content, "[预估佣金]", args.Fcommission, -1)
  75. args.Content = strings.Replace(args.Content, "[注册人昵称]", args.RegNickname, -1)
  76. args.Content = strings.Replace(args.Content, "[注册人手机号]", args.RegPhone, -1)
  77. args.Content = strings.Replace(args.Content, "[升级等级名称]", args.LvName, -1)
  78. args.Content = strings.Replace(args.Content, "[提现金额]", args.WithdrawMoney, -1)
  79. args.Content = strings.Replace(args.Content, "[原因]", args.Reason, -1)
  80. args.Content = strings.Replace(args.Content, "[金额]", args.Money, -1)
  81. args.Content = strings.Replace(args.Content, "[时间]", args.Times, -1)
  82. }
  83. }
  84. pushArgs["push_title"] = args.Title
  85. pushArgs["push_content"] = args.Content
  86. pushArgs["push_user"] = fmt.Sprintf("%s_%d", args.MasterId, args.Uid)
  87. pushArgs["push_receive_user_id"] = utils.IntToStr(args.Uid)
  88. pushArgs["push_type"] = args.PushType
  89. pushArgs["masterId"] = args.MasterId
  90. pushArgs["push_skip"] = args.Skip
  91. if err := AdminMobPush(eg, pushArgs); err != nil {
  92. //e.OutErr(c, e.ERR_ADMIN_PUSH, err)
  93. return
  94. }
  95. return
  96. }
  97. // AdminMobPush mob is 推送 t 有下面几种类型模板类型 | 推送类型;public;:普通推送;activity:活动通知;order_self:新订单提醒(导购自购新订单),order_team:新订单提醒(团队新订单),order_share:新订单提醒(导购分享新订单),member_register:团队成员注册成功,level_upgrade:团队成员等级升级成功,withdraw_fail:提现失败提醒,withdraw_success:提现成功提醒,comission_settle_success:佣金结算提醒(平台结算)
  98. func AdminMobPush(eg *xorm.Engine, pushArgs map[string]string) error {
  99. var (
  100. host string
  101. port string
  102. )
  103. // 开始准备推送
  104. mdb, err := db.GetDatabaseByMasterID(db.Db, pushArgs["masterId"])
  105. fmt.Println("============================")
  106. fmt.Println(pushArgs)
  107. fmt.Println(mdb)
  108. if err != nil {
  109. return err
  110. }
  111. if mdb.DbHost == "" {
  112. mdb.DbHost = cfg.DB.Host
  113. }
  114. //fmt.Println(mdb.DbHost)
  115. dbc := strings.Split(mdb.DbHost, ":")
  116. if len(dbc) != 2 && mdb.DbHost != "" {
  117. return errors.New("db mapping db_host is formt error")
  118. }
  119. host = dbc[0]
  120. port = dbc[1]
  121. args := struct {
  122. Content string `json:"content"`
  123. Db struct {
  124. DbHost string `json:"db_host"`
  125. DbName string `json:"db_name"`
  126. DbPassword string `json:"db_password"`
  127. DbPort string `json:"db_port"`
  128. DbUsername string `json:"db_username"`
  129. } `json:"db"`
  130. Plats int64 `json:"plats"`
  131. Skip string `json:"skip"`
  132. Title string `json:"title"`
  133. User string `json:"user"`
  134. }{
  135. Content: pushArgs["push_content"],
  136. Db: struct {
  137. DbHost string "json:\"db_host\""
  138. DbName string "json:\"db_name\""
  139. DbPassword string "json:\"db_password\""
  140. DbPort string "json:\"db_port\""
  141. DbUsername string "json:\"db_username\""
  142. }{
  143. DbHost: host,
  144. DbPort: port,
  145. DbName: mdb.DbName,
  146. DbUsername: mdb.DbUsername,
  147. DbPassword: mdb.DbPassword,
  148. },
  149. Plats: 1,
  150. Skip: pushArgs["push_skip"],
  151. Title: pushArgs["push_title"],
  152. User: pushArgs["push_user"],
  153. }
  154. // 插入推送记录
  155. log := &model.SysPushApp{
  156. Title: pushArgs["push_title"],
  157. Content: pushArgs["push_content"],
  158. Provider: "mob",
  159. Type: pushArgs["push_type"],
  160. State: 1,
  161. SendAt: 0,
  162. DeviceProvider: 1,
  163. Target: 2,
  164. TargetCondition: fmt.Sprintf(`{"type":"user","val":["%s"]}`, pushArgs["push_receive_user_id"]),
  165. Skip: pushArgs["push_skip"],
  166. CreateAt: time.Now(),
  167. UpdateAt: time.Now(),
  168. }
  169. if _, err := db.InertSysPushAppOne(eg, log); err != nil {
  170. // e.OutErr(c, e.ERR_DB_ORM, err)
  171. // return
  172. logx.Warn(err)
  173. }
  174. var url string
  175. var domain = "http://login.izhyin.com"
  176. if cfg.Prd {
  177. domain = "http://zhios-admin"
  178. }
  179. url = domain + "/appapi/Push/mobPush"
  180. aesData, err := json.Marshal(&args)
  181. if err != nil {
  182. // e.OutErr(c, e.ERR_ADMIN_API, err)
  183. return err
  184. }
  185. //fmt.Println(string(aesData))
  186. fmt.Println(url)
  187. fmt.Println(aesData)
  188. if aesData == nil {
  189. return nil
  190. }
  191. // 推送
  192. resp, err := utils.AesAdminCurlPOST(string(aesData), url)
  193. fmt.Println("==========推送============")
  194. fmt.Println(resp)
  195. fmt.Println(gjson.Get(string(resp), "message").String())
  196. fmt.Println(err)
  197. if err != nil {
  198. // e.OutErr(c, e.ERR_ADMIN_API, err)
  199. return err
  200. }
  201. if resp == nil {
  202. return err
  203. }
  204. //fmt.Println(string(resp))
  205. // 推送成功更新推送状态
  206. if gjson.GetBytes(resp, "code").Int() == 0 {
  207. // 发送成功处理
  208. log.State = 2
  209. if _, err := db.UpdateSysPushApp(eg, log); err != nil {
  210. logx.Warn(err)
  211. }
  212. }
  213. sendData := struct {
  214. Alias string `json:"alias"`
  215. Content string `json:"content"`
  216. Error string `json:"error"`
  217. Title string `json:"title"`
  218. }{
  219. Alias: pushArgs["push_user"],
  220. Content: pushArgs["push_content"],
  221. Error: gjson.Get(string(resp), "message").String(),
  222. Title: pushArgs["push_content"],
  223. }
  224. fmt.Println(sendData.Error)
  225. bendData, _ := json.Marshal(sendData)
  226. fmt.Println("插入到通知记录")
  227. // 插入到通知记录
  228. _, err = db.SysPushUserInsertOne(eg, &model.SysPushUser{
  229. PushId: int(log.Id),
  230. Uid: utils.StrToInt(pushArgs["push_receive_user_id"]),
  231. State: func() int {
  232. if gjson.GetBytes(resp, "code").Int() == 0 {
  233. return 1
  234. }
  235. return 0
  236. }(),
  237. Time: time.Now(),
  238. SendData: string(bendData),
  239. Provider: "mob",
  240. Type: pushArgs["push_type"],
  241. })
  242. if err != nil {
  243. logx.Warn(err)
  244. }
  245. return nil
  246. }