|
- package push
-
- import (
- "applet/app/cfg"
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/md"
- "applet/app/utils"
- "applet/app/utils/logx"
- "encoding/json"
- "errors"
- "fmt"
- "strings"
- "time"
- "xorm.io/xorm"
-
- "github.com/tidwall/gjson"
- )
-
- //公共处理推送数据
- func CommAddPush(eg *xorm.Engine, args md.PushParams) {
- // 后台mob推送
- pushArgs := make(map[string]string)
- if args.DomainWapBase == "" {
- args.DomainWapBase = "http://h5." + args.MasterId + ".izhyin.com"
- if cfg.Prd {
- args.DomainWapBase = "http://h5." + args.MasterId + ".zhiyingos.com"
- }
- }
- if args.PushType != "" {
- temp, err := db.SysPushTemplateByType(eg, args.PushType)
- if err != nil {
- //e.OutErr(c, e.ERR_DB_ORM, err)
- return
- }
- if temp != nil { //处理有模板的数据
- if temp.IsAppPush != 1 {
- return
- }
- args.Title = temp.Title
- args.Content = temp.Content
- args.Skip = temp.Skip
- //替换链接的一些参数
- if strings.Contains(args.Skip, "[replace_APP_URL]") {
- args.Skip = strings.Replace(args.Skip, "[replace_APP_URL]", args.DomainWapBase, -1)
- }
- if strings.Contains(args.Skip, "[replace_masterId]") {
- args.Skip = strings.Replace(args.Skip, "[replace_masterId]", args.MasterId, -1)
- }
- if strings.Contains(args.Skip, "[replace_uid]") {
- args.Skip = strings.Replace(args.Skip, "[replace_uid]", utils.IntToStr(args.Uid), -1)
- }
- if strings.Contains(args.Skip, "skipIdentifier") {
- args.Skip = strings.Replace(args.Skip, "skipIdentifier", "skip_identifier", -1)
- }
- if strings.Contains(args.Skip, "requiredLogin") {
- args.Skip = strings.Replace(args.Skip, "requiredLogin", "required_login", -1)
- }
- if strings.Contains(args.Skip, "requiredTaobaoAuth") {
- args.Skip = strings.Replace(args.Skip, "requiredTaobaoAuth", "required_taobao_auth", -1)
- }
- if strings.Contains(args.Skip, "activityId") {
- args.Skip = strings.Replace(args.Skip, "activityId", "activity_id", -1)
- }
- if strings.Contains(args.Skip, "sourceType") {
- args.Skip = strings.Replace(args.Skip, "sourceType", "source_type", -1)
- }
- if strings.Contains(args.Skip, "brandId") {
- args.Skip = strings.Replace(args.Skip, "brandId", "brand_id", -1)
- }
- args.Content = strings.Replace(args.Content, "[APP名称]", args.AppName, -1)
- args.Content = strings.Replace(args.Content, "[会员昵称]", args.Nickname, -1)
- args.Content = strings.Replace(args.Content, "[付款金额]", args.Payment, -1)
- args.Content = strings.Replace(args.Content, "[下单人昵称]", args.OrderNickname, -1)
- args.Content = strings.Replace(args.Content, "[订单号]", args.OrderNo, -1)
- args.Content = strings.Replace(args.Content, "[下单时间]", args.OrderTime, -1)
- args.Content = strings.Replace(args.Content, "[预估佣金]", args.Fcommission, -1)
- args.Content = strings.Replace(args.Content, "[注册人昵称]", args.RegNickname, -1)
- args.Content = strings.Replace(args.Content, "[注册人手机号]", args.RegPhone, -1)
- args.Content = strings.Replace(args.Content, "[升级等级名称]", args.LvName, -1)
- args.Content = strings.Replace(args.Content, "[提现金额]", args.WithdrawMoney, -1)
- args.Content = strings.Replace(args.Content, "[原因]", args.Reason, -1)
- args.Content = strings.Replace(args.Content, "[金额]", args.Money, -1)
- args.Content = strings.Replace(args.Content, "[时间]", args.Times, -1)
- }
- }
- pushArgs["push_title"] = args.Title
- pushArgs["push_content"] = args.Content
- pushArgs["push_user"] = fmt.Sprintf("%s_%d", args.MasterId, args.Uid)
- pushArgs["push_receive_user_id"] = utils.IntToStr(args.Uid)
- pushArgs["push_type"] = args.PushType
- pushArgs["masterId"] = args.MasterId
- pushArgs["push_skip"] = args.Skip
- if err := AdminMobPush(eg, pushArgs); err != nil {
- //e.OutErr(c, e.ERR_ADMIN_PUSH, err)
- return
- }
- return
- }
-
- // AdminMobPush mob is 推送 t 有下面几种类型模板类型 | 推送类型;public;:普通推送;activity:活动通知;order_self:新订单提醒(导购自购新订单),order_team:新订单提醒(团队新订单),order_share:新订单提醒(导购分享新订单),member_register:团队成员注册成功,level_upgrade:团队成员等级升级成功,withdraw_fail:提现失败提醒,withdraw_success:提现成功提醒,comission_settle_success:佣金结算提醒(平台结算)
- func AdminMobPush(eg *xorm.Engine, pushArgs map[string]string) error {
- var (
- host string
- port string
- )
-
- // 开始准备推送
- mdb, err := db.GetDatabaseByMasterID(db.Db, pushArgs["masterId"])
- fmt.Println("============================")
- fmt.Println(pushArgs)
- fmt.Println(mdb)
- if err != nil {
- return err
- }
- if mdb.DbHost == "" {
- mdb.DbHost = cfg.DB.Host
- }
- //fmt.Println(mdb.DbHost)
- dbc := strings.Split(mdb.DbHost, ":")
- if len(dbc) != 2 && mdb.DbHost != "" {
- return errors.New("db mapping db_host is formt error")
- }
-
- host = dbc[0]
- port = dbc[1]
-
- args := struct {
- Content string `json:"content"`
- Db struct {
- DbHost string `json:"db_host"`
- DbName string `json:"db_name"`
- DbPassword string `json:"db_password"`
- DbPort string `json:"db_port"`
- DbUsername string `json:"db_username"`
- } `json:"db"`
- Plats int64 `json:"plats"`
- Skip string `json:"skip"`
- Title string `json:"title"`
- User string `json:"user"`
- }{
- Content: pushArgs["push_content"],
- Db: struct {
- DbHost string "json:\"db_host\""
- DbName string "json:\"db_name\""
- DbPassword string "json:\"db_password\""
- DbPort string "json:\"db_port\""
- DbUsername string "json:\"db_username\""
- }{
- DbHost: host,
- DbPort: port,
- DbName: mdb.DbName,
- DbUsername: mdb.DbUsername,
- DbPassword: mdb.DbPassword,
- },
- Plats: 1,
- Skip: pushArgs["push_skip"],
- Title: pushArgs["push_title"],
- User: pushArgs["push_user"],
- }
-
- // 插入推送记录
- log := &model.SysPushApp{
- Title: pushArgs["push_title"],
- Content: pushArgs["push_content"],
- Provider: "mob",
- Type: pushArgs["push_type"],
- State: 1,
- SendAt: 0,
- DeviceProvider: 1,
- Target: 2,
- TargetCondition: fmt.Sprintf(`{"type":"user","val":["%s"]}`, pushArgs["push_receive_user_id"]),
- Skip: pushArgs["push_skip"],
- CreateAt: time.Now(),
- UpdateAt: time.Now(),
- }
- if _, err := db.InertSysPushAppOne(eg, log); err != nil {
- // e.OutErr(c, e.ERR_DB_ORM, err)
- // return
- logx.Warn(err)
- }
- var url string
- var domain = "http://login.izhyin.com"
- if cfg.Prd {
- domain = "http://zhios-admin"
- }
- url = domain + "/appapi/Push/mobPush"
- aesData, err := json.Marshal(&args)
- if err != nil {
- // e.OutErr(c, e.ERR_ADMIN_API, err)
- return err
- }
- //fmt.Println(string(aesData))
- fmt.Println(url)
- fmt.Println(aesData)
- if aesData == nil {
- return nil
- }
- // 推送
- resp, err := utils.AesAdminCurlPOST(string(aesData), url)
- fmt.Println("==========推送============")
- fmt.Println(resp)
- fmt.Println(gjson.Get(string(resp), "message").String())
- fmt.Println(err)
- if err != nil {
- // e.OutErr(c, e.ERR_ADMIN_API, err)
- return err
- }
- if resp == nil {
- return err
- }
- //fmt.Println(string(resp))
- // 推送成功更新推送状态
- if gjson.GetBytes(resp, "code").Int() == 0 {
- // 发送成功处理
- log.State = 2
- if _, err := db.UpdateSysPushApp(eg, log); err != nil {
- logx.Warn(err)
- }
- }
-
- sendData := struct {
- Alias string `json:"alias"`
- Content string `json:"content"`
- Error string `json:"error"`
- Title string `json:"title"`
- }{
- Alias: pushArgs["push_user"],
- Content: pushArgs["push_content"],
- Error: gjson.Get(string(resp), "message").String(),
- Title: pushArgs["push_content"],
- }
- fmt.Println(sendData.Error)
- bendData, _ := json.Marshal(sendData)
- fmt.Println("插入到通知记录")
- // 插入到通知记录
- _, err = db.SysPushUserInsertOne(eg, &model.SysPushUser{
- PushId: int(log.Id),
- Uid: utils.StrToInt(pushArgs["push_receive_user_id"]),
- State: func() int {
- if gjson.GetBytes(resp, "code").Int() == 0 {
- return 1
- }
- return 0
- }(),
- Time: time.Now(),
- SendData: string(bendData),
- Provider: "mob",
- Type: pushArgs["push_type"],
- })
- if err != nil {
- logx.Warn(err)
- }
- return nil
- }
|