package hdl import ( "applet/app/cfg" implement2 "applet/app/db/implement" "applet/app/e" "applet/app/enum" "applet/app/lib/validate" "applet/app/lib/wechat" "applet/app/md" "applet/app/svc" "applet/app/utils" db "code.fnuoos.com/zhimeng/model.git/src" "code.fnuoos.com/zhimeng/model.git/src/super/implement" "code.fnuoos.com/zhimeng/model.git/src/super/model" "github.com/gin-gonic/gin" "time" ) // SetOss // @Summary oss设置 // @Tags 设置中心-基础设置 // @Description 基础设置-oss设置 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Param args body md.SetOssReq true "请求参数" // @Success 200 {string} "success" // @Failure 400 {object} md.Response "具体错误" // @Router /api/setCenter/basic/setOss [POST] func SetOss(c *gin.Context) { var req md.SetOssReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } masterId := svc.GetMasterId(c) engine := db.DBs[masterId] sysCfgDb := implement2.NewSysCfgDb(engine, masterId) sysCfgDb.SysCfgUpdate(enum.FileAccessKey, req.FileAccessKey) sysCfgDb.SysCfgUpdate(enum.FileSecretKey, req.FileSecretKey) sysCfgDb.SysCfgUpdate(enum.FileBucketHost, req.FileBucketHost) sysCfgDb.SysCfgUpdate(enum.FileBucketRegion, req.FileBucketRegion) sysCfgDb.SysCfgUpdate(enum.FileBucket, req.FileBucket) svc.ClearRedis(c, enum.FileAccessKey) svc.ClearRedis(c, enum.FileSecretKey) svc.ClearRedis(c, enum.FileBucketHost) svc.ClearRedis(c, enum.FileBucketRegion) svc.ClearRedis(c, enum.FileBucket) e.OutSuc(c, "success", nil) return } // GetOss // @Summary oss获取 // @Tags 设置中心-基础设置 // @Description 基础设置-oss获取 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Success 200 {object} md.SetOssResp // @Failure 400 {object} md.Response "具体错误" // @Router /api/setCenter/basic/getOss [GET] func GetOss(c *gin.Context) { masterId := svc.GetMasterId(c) engine := db.DBs[masterId] sysCfgDb := implement2.NewSysCfgDb(engine, masterId) res := sysCfgDb.SysCfgFindWithDb(enum.FileAccessKey, enum.FileSecretKey, enum.FileBucketHost, enum.FileBucketRegion, enum.FileBucket) e.OutSuc(c, md.SetOssResp{ Data: md.SetOssReq{ FileAccessKey: res[enum.FileAccessKey], FileSecretKey: res[enum.FileSecretKey], FileBucketHost: res[enum.FileBucketHost], FileBucketRegion: res[enum.FileBucketRegion], FileBucket: res[enum.FileBucket], }, QiNiuBucketRegionList: md.QiNiuBucketRegionList, }, nil) return } // WxOpenSet // @Summary 微信三方应用设置 // @Tags 设置中心-基础设置 // @Description 基础设置-微信三方应用设置 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Param args body md.WxOpenSetReq true "请求参数" // @Success 200 {string} "success" // @Failure 400 {object} md.Response "具体错误" // @Router /api/setCenter/basic/wxOpenSet [POST] func WxOpenSet(c *gin.Context) { var req md.WxOpenSetReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } masterId := svc.GetMasterId(c) //1、查找对应 wx_open_third_party_app_list 记录 wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db) UserWxAppletList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppList(utils.StrToInt(masterId)) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if UserWxAppletList == nil { UserWxAppletList = &model.WxOpenThirdPartyAppList{ Uuid: utils.StrToInt(masterId), Token: req.Token, AesKey: req.AesKey, Appid: req.Appid, AppSecret: req.AppSecret, ComponentVerifyTicket: "", ComponentAccessToken: "", CreateAt: time.Now().Format("2006-01-02 15:04:05"), UpdateAt: time.Now().Format("2006-01-02 15:04:05"), } _, err = wxOpenThirdPartyAppListDb.WxOpenThirdPartyAppListInsert(UserWxAppletList) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } } else { UserWxAppletList.Token = req.Token UserWxAppletList.AesKey = req.AesKey UserWxAppletList.Appid = req.Appid UserWxAppletList.AppSecret = req.AppSecret _, err = wxOpenThirdPartyAppListDb.UpdateWxOpenThirdPartyAppList(UserWxAppletList, "token", "aes_key", "appid", "app_secret") if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } } e.OutSuc(c, "success", nil) return } // WxOpenGet // @Summary 微信三方应用获取 // @Tags 设置中心-基础设置 // @Description 基础设置-微信三方应用获取 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Success 200 {object} md.WxOpenGetResp // @Failure 400 {object} md.Response "具体错误" // @Router /api/setCenter/basic/wxOpenGet [GET] func WxOpenGet(c *gin.Context) { masterId := svc.GetMasterId(c) //1、查找对应 wx_open_third_party_app_list 记录 wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db) UserWxAppletList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppList(utils.StrToInt(masterId)) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if UserWxAppletList == nil { UserWxAppletList = &model.WxOpenThirdPartyAppList{ Uuid: utils.StrToInt(masterId), Token: "", AesKey: "", Appid: "", AppSecret: "", ComponentVerifyTicket: "", ComponentAccessToken: "", CreateAt: time.Now().Format("2006-01-02 15:04:05"), UpdateAt: time.Now().Format("2006-01-02 15:04:05"), } _, err = wxOpenThirdPartyAppListDb.WxOpenThirdPartyAppListInsert(UserWxAppletList) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } } sysCfgDb := implement2.NewSysCfgDb(db.Db, "") res := sysCfgDb.SysCfgFindWithDb(enum.WxOpenAppletServerDomain, enum.WxOpenAuthorizationEventReceivingConfiguration, enum.WxOpenDomainOfTheInitiatingPageForLoginAuthorization, enum.WxOpenMessageAndEventReceptionConfiguration, enum.WxOpenWhiteListIp) e.OutSuc(c, md.WxOpenGetResp{ Token: UserWxAppletList.Token, AesKey: UserWxAppletList.AesKey, Appid: UserWxAppletList.Appid, AppSecret: UserWxAppletList.AppSecret, WxOpenAppletServerDomain: res[enum.WxOpenAppletServerDomain], WxOpenAuthorizationEventReceivingConfiguration: res[enum.WxOpenAuthorizationEventReceivingConfiguration], WxOpenDomainOfTheInitiatingPageForLoginAuthorization: res[enum.WxOpenDomainOfTheInitiatingPageForLoginAuthorization], WxOpenMessageAndEventReceptionConfiguration: res[enum.WxOpenMessageAndEventReceptionConfiguration], WxOpenWhiteListIp: res[enum.WxOpenWhiteListIp], }, nil) return } type WxOpenThirdPartyAppList struct { Id int `json:"id" xorm:"not null pk autoincr unique(IDX_UUID_TYPE) INT(11)"` Uuid int `json:"uuid" xorm:"not null comment('站长id') index unique(IDX_UUID_TYPE) INT(10)"` Token string `json:"token" xorm:"not null default '' comment('消息校验Token') VARCHAR(255)"` AesKey string `json:"aes_key" xorm:"not null default '' comment('消息加解密Key') VARCHAR(255)"` Appid string `json:"appid" xorm:"not null default '' comment('appid') CHAR(50)"` AppSecret string `json:"app_secret" xorm:"not null default '' comment('appSecret') VARCHAR(255)"` ComponentVerifyTicket string `json:"component_verify_ticket" xorm:"not null default '' comment('验证票据') VARCHAR(255)"` ComponentAccessToken string `json:"component_access_token" xorm:"not null default '' comment('接口令牌') VARCHAR(255)"` CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"` } // AppletList // @Summary 列表 // @Tags 设置中心-小程序设置 // @Description 小程序设置-列表 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Success 200 {object} WxOpenThirdPartyAppList // @Failure 400 {object} md.Response "具体错误" // @Router /api/setCenter/applet/list [GET] func AppletList(c *gin.Context) { masterId := svc.GetMasterId(c) //1、查找对应 user_wx_applet_list 记录 userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db) UserWxAppletList, err := userWxAppletListDb.GetUserWxAppletList(masterId) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, UserWxAppletList, nil) return } // AppletAdd // @Summary 新增 // @Tags 设置中心-小程序设置 // @Description 小程序设置-新增 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Param args body md.AppletAddReq true "请求参数" // @Success 200 {string} "success" // @Failure 400 {object} md.Response "具体错误" // @Router /api/setCenter/applet/add [POST] func AppletAdd(c *gin.Context) { var req md.AppletAddReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } masterId := svc.GetMasterId(c) //1、查找对应 user_wx_applet_list 记录 userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db) UserWxAppletList, err := userWxAppletListDb.GetUserWxAppletList(masterId) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if UserWxAppletList != nil { e.OutErr(c, e.ERR, "当前仅支持单个小程序") return } _, err = userWxAppletListDb.UserWxAppletListInsert(&model.UserWxAppletList{ Name: req.Name, Logo: req.Logo, Appid: req.Appid, OriginalId: req.OriginalId, AuthorizerRefreshToken: "", IsAuth: 0, Uuid: masterId, CreateAt: time.Now().Format("2006-01-02 15:04:05"), UpdateAt: time.Now().Format("2006-01-02 15:04:05"), }) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, "success", nil) return } // AppletUpdate // @Summary 更新 // @Tags 设置中心-小程序设置 // @Description 小程序设置-更新 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Param args body md.AppletUpdateReq true "请求参数" // @Success 200 {string} "success" // @Failure 400 {object} md.Response "具体错误" // @Router /api/setCenter/applet/update [POST] func AppletUpdate(c *gin.Context) { var req md.AppletUpdateReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } masterId := svc.GetMasterId(c) //1、查找对应 user_wx_applet_list 记录 userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db) UserWxAppletList, err := userWxAppletListDb.GetUserWxAppletList(masterId) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if UserWxAppletList == nil { e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录") return } UserWxAppletList.Logo = req.Logo UserWxAppletList.Name = req.Name _, err = userWxAppletListDb.UpdateUserWxAppletList(UserWxAppletList, "logo", "name") if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, "success", nil) return } // AppletDelete // @Summary 删除 // @Tags 设置中心-小程序设置 // @Description 小程序设置-删除 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Success 200 {string} "success" // @Failure 400 {object} md.Response "具体错误" // @Router /v1/banner/delete/{$id} [DELETE] func AppletDelete(c *gin.Context) { id := c.Param("id") masterId := svc.GetMasterId(c) //1、查找对应 user_wx_applet_list 记录 userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db) UserWxAppletList, err := userWxAppletListDb.GetUserWxAppletList(masterId) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if UserWxAppletList == nil { e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录") return } _, err = userWxAppletListDb.UserWxAppletListDelete(id) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } e.OutSuc(c, "success", nil) return } // AppletAuthorize // @Summary 设置中心-基础设置-微信三方应用获取 // @Tags 设置中心 // @Description 设置中心-基础设置 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Success 200 {string} "微信授权界面url" // @Failure 400 {object} md.Response "具体错误" // @Router /api/setCenter/applet/authorize [GET] func AppletAuthorize(c *gin.Context) { masterId := svc.GetMasterId(c) url := "http://super.advertisement.dengbiao.top/api/wxOpen/getPreAuthCode?master_id=" + masterId if cfg.Prd { url = "http://super.advertisement.dengbiao.top/api/wxOpen/getPreAuthCode?master_id=" + masterId } e.OutSuc(c, map[string]string{ "url": url, }, nil) return } // AppletUnauthorized // @Summary 设置中心-基础设置-微信三方应用获取 // @Tags 设置中心 // @Description 设置中心-基础设置 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Success 200 {string} "success" // @Failure 400 {object} md.Response "具体错误" // @Router /api/setCenter/applet/unauthorized [GET] func AppletUnauthorized(c *gin.Context) { appId := c.DefaultQuery("app_id", "") masterId := svc.GetMasterId(c) //1、查找对应 user_wx_applet_list 记录 userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db) UserWxAppletList, err := userWxAppletListDb.GetUserWxAppletList(masterId) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } if UserWxAppletList == nil { e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录") return } wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db) wxOpenThirdPartyAppList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppList(utils.StrToInt(masterId)) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } if wxOpenThirdPartyAppList == nil { e.OutErr(c, e.ERR_NOT_FAN, "未查询到对应三方应用记录") return } wxApiService, err := wechat.NewWxApiService(masterId, wxOpenThirdPartyAppList.Appid, wxOpenThirdPartyAppList.AppSecret) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } err = wxApiService.DelAuthorize(appId) if err != nil { e.OutErr(c, e.ERR, err.Error()) return } e.OutSuc(c, "success", nil) return } // ShareIndex // @Summary 邀请链接 // @Tags 设置中心-邀请链接 // @Description 邀请链接界面接口 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Success 200 {object} md.ShareIndexResp // @Failure 400 {object} md.Response "具体错误" // @Router /api/setCenter/share/index [GET] func ShareIndex(c *gin.Context) { masterId := svc.GetMasterId(c) userAppDomainDb := implement.NewUserAppDomainDb(db.Db) agent, err := userAppDomainDb.GetAppDomainByType("agent", masterId) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } medium, err := userAppDomainDb.GetAppDomainByType("medium", masterId) if err != nil { e.OutErr(c, e.ERR_DB_ORM, err.Error()) return } agentDomain := "" if agent != nil { agentDomain = agent.Domain } mediumDomain := "" if medium != nil { mediumDomain = medium.Domain } e.OutSuc(c, md.ShareIndexResp{ MasterId: masterId, AgentDomain: agentDomain, MediumDomain: mediumDomain, }, nil) return } // SetMob // @Summary mob设置 // @Tags 设置中心-基础设置 // @Description 基础设置-mob设置 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Param args body md.SetMobReq true "请求参数" // @Success 200 {string} "success" // @Failure 400 {object} md.Response "具体错误" // @Router /api/setCenter/basic/setMob [POST] func SetMob(c *gin.Context) { var req md.SetMobReq err := c.ShouldBindJSON(&req) if err != nil { err = validate.HandleValidateErr(err) err1 := err.(e.E) e.OutErr(c, err1.Code, err1.Error()) return } masterId := svc.GetMasterId(c) engine := db.DBs[masterId] sysCfgDb := implement2.NewSysCfgDb(engine, masterId) sysCfgDb.SysCfgUpdate("mob_app_key", req.MobAppKey) sysCfgDb.SysCfgUpdate("mob_app_secret", req.MobAppSecret) svc.ClearRedis(c, "mob_app_key") svc.ClearRedis(c, "mob_app_secret") e.OutSuc(c, "success", nil) return } // GetMob // @Summary mob获取 // @Tags 设置中心-基础设置 // @Description 基础设置-mob获取 // @param Authorization header string true "验证参数Bearer和token空格拼接" // @Accept json // @Produce json // @Success 200 {object} md.SetOssResp // @Failure 400 {object} md.Response "具体错误" // @Router /api/setCenter/basic/getMob [GET] func GetMob(c *gin.Context) { masterId := svc.GetMasterId(c) engine := db.DBs[masterId] sysCfgDb := implement2.NewSysCfgDb(engine, masterId) res := sysCfgDb.SysCfgFindWithDb("mob_app_key", "mob_app_secret") e.OutSuc(c, md.SetMobResp{ Data: md.SetMobReq{ MobAppSecret: res["mob_app_secret"], MobAppKey: res["mob_app_key"], }, }, nil) return }