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

533 lines
18 KiB

  1. package hdl
  2. import (
  3. "applet/app/cfg"
  4. implement2 "applet/app/db/implement"
  5. "applet/app/e"
  6. "applet/app/enum"
  7. "applet/app/lib/validate"
  8. "applet/app/lib/wechat"
  9. "applet/app/md"
  10. "applet/app/svc"
  11. "applet/app/utils"
  12. db "code.fnuoos.com/zhimeng/model.git/src"
  13. "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  14. "code.fnuoos.com/zhimeng/model.git/src/super/model"
  15. "github.com/gin-gonic/gin"
  16. "time"
  17. )
  18. // SetOss
  19. // @Summary oss设置
  20. // @Tags 设置中心-基础设置
  21. // @Description 基础设置-oss设置
  22. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  23. // @Accept json
  24. // @Produce json
  25. // @Param args body md.SetOssReq true "请求参数"
  26. // @Success 200 {string} "success"
  27. // @Failure 400 {object} md.Response "具体错误"
  28. // @Router /api/setCenter/basic/setOss [POST]
  29. func SetOss(c *gin.Context) {
  30. var req md.SetOssReq
  31. err := c.ShouldBindJSON(&req)
  32. if err != nil {
  33. err = validate.HandleValidateErr(err)
  34. err1 := err.(e.E)
  35. e.OutErr(c, err1.Code, err1.Error())
  36. return
  37. }
  38. masterId := svc.GetMasterId(c)
  39. engine := db.DBs[masterId]
  40. sysCfgDb := implement2.NewSysCfgDb(engine, masterId)
  41. sysCfgDb.SysCfgUpdate(enum.FileAccessKey, req.FileAccessKey)
  42. sysCfgDb.SysCfgUpdate(enum.FileSecretKey, req.FileSecretKey)
  43. sysCfgDb.SysCfgUpdate(enum.FileBucketHost, req.FileBucketHost)
  44. sysCfgDb.SysCfgUpdate(enum.FileBucketRegion, req.FileBucketRegion)
  45. sysCfgDb.SysCfgUpdate(enum.FileBucket, req.FileBucket)
  46. svc.ClearRedis(c, enum.FileAccessKey)
  47. svc.ClearRedis(c, enum.FileSecretKey)
  48. svc.ClearRedis(c, enum.FileBucketHost)
  49. svc.ClearRedis(c, enum.FileBucketRegion)
  50. svc.ClearRedis(c, enum.FileBucket)
  51. e.OutSuc(c, "success", nil)
  52. return
  53. }
  54. // GetOss
  55. // @Summary oss获取
  56. // @Tags 设置中心-基础设置
  57. // @Description 基础设置-oss获取
  58. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  59. // @Accept json
  60. // @Produce json
  61. // @Success 200 {object} md.SetOssResp
  62. // @Failure 400 {object} md.Response "具体错误"
  63. // @Router /api/setCenter/basic/getOss [GET]
  64. func GetOss(c *gin.Context) {
  65. masterId := svc.GetMasterId(c)
  66. engine := db.DBs[masterId]
  67. sysCfgDb := implement2.NewSysCfgDb(engine, masterId)
  68. res := sysCfgDb.SysCfgFindWithDb(enum.FileAccessKey, enum.FileSecretKey, enum.FileBucketHost, enum.FileBucketRegion, enum.FileBucket)
  69. e.OutSuc(c, md.SetOssResp{
  70. Data: md.SetOssReq{
  71. FileAccessKey: res[enum.FileAccessKey],
  72. FileSecretKey: res[enum.FileSecretKey],
  73. FileBucketHost: res[enum.FileBucketHost],
  74. FileBucketRegion: res[enum.FileBucketRegion],
  75. FileBucket: res[enum.FileBucket],
  76. },
  77. QiNiuBucketRegionList: md.QiNiuBucketRegionList,
  78. }, nil)
  79. return
  80. }
  81. // WxOpenSet
  82. // @Summary 微信三方应用设置
  83. // @Tags 设置中心-基础设置
  84. // @Description 基础设置-微信三方应用设置
  85. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  86. // @Accept json
  87. // @Produce json
  88. // @Param args body md.WxOpenSetReq true "请求参数"
  89. // @Success 200 {string} "success"
  90. // @Failure 400 {object} md.Response "具体错误"
  91. // @Router /api/setCenter/basic/wxOpenSet [POST]
  92. func WxOpenSet(c *gin.Context) {
  93. var req md.WxOpenSetReq
  94. err := c.ShouldBindJSON(&req)
  95. if err != nil {
  96. err = validate.HandleValidateErr(err)
  97. err1 := err.(e.E)
  98. e.OutErr(c, err1.Code, err1.Error())
  99. return
  100. }
  101. masterId := svc.GetMasterId(c)
  102. //1、查找对应 wx_open_third_party_app_list 记录
  103. wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
  104. UserWxAppletList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppList(utils.StrToInt(masterId))
  105. if err != nil {
  106. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  107. return
  108. }
  109. if UserWxAppletList == nil {
  110. UserWxAppletList = &model.WxOpenThirdPartyAppList{
  111. Uuid: utils.StrToInt(masterId),
  112. Token: req.Token,
  113. AesKey: req.AesKey,
  114. Appid: req.Appid,
  115. AppSecret: req.AppSecret,
  116. ComponentVerifyTicket: "",
  117. ComponentAccessToken: "",
  118. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  119. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  120. }
  121. _, err = wxOpenThirdPartyAppListDb.WxOpenThirdPartyAppListInsert(UserWxAppletList)
  122. if err != nil {
  123. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  124. return
  125. }
  126. } else {
  127. UserWxAppletList.Token = req.Token
  128. UserWxAppletList.AesKey = req.AesKey
  129. UserWxAppletList.Appid = req.Appid
  130. UserWxAppletList.AppSecret = req.AppSecret
  131. _, err = wxOpenThirdPartyAppListDb.UpdateWxOpenThirdPartyAppList(UserWxAppletList,
  132. "token", "aes_key", "appid", "app_secret")
  133. if err != nil {
  134. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  135. return
  136. }
  137. }
  138. e.OutSuc(c, "success", nil)
  139. return
  140. }
  141. // WxOpenGet
  142. // @Summary 微信三方应用获取
  143. // @Tags 设置中心-基础设置
  144. // @Description 基础设置-微信三方应用获取
  145. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  146. // @Accept json
  147. // @Produce json
  148. // @Success 200 {object} md.WxOpenGetResp
  149. // @Failure 400 {object} md.Response "具体错误"
  150. // @Router /api/setCenter/basic/wxOpenGet [GET]
  151. func WxOpenGet(c *gin.Context) {
  152. masterId := svc.GetMasterId(c)
  153. //1、查找对应 wx_open_third_party_app_list 记录
  154. wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
  155. UserWxAppletList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppList(utils.StrToInt(masterId))
  156. if err != nil {
  157. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  158. return
  159. }
  160. if UserWxAppletList == nil {
  161. UserWxAppletList = &model.WxOpenThirdPartyAppList{
  162. Uuid: utils.StrToInt(masterId),
  163. Token: "",
  164. AesKey: "",
  165. Appid: "",
  166. AppSecret: "",
  167. ComponentVerifyTicket: "",
  168. ComponentAccessToken: "",
  169. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  170. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  171. }
  172. _, err = wxOpenThirdPartyAppListDb.WxOpenThirdPartyAppListInsert(UserWxAppletList)
  173. if err != nil {
  174. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  175. return
  176. }
  177. }
  178. sysCfgDb := implement2.NewSysCfgDb(db.Db, "")
  179. res := sysCfgDb.SysCfgFindWithDb(enum.WxOpenAppletServerDomain, enum.WxOpenAuthorizationEventReceivingConfiguration,
  180. enum.WxOpenDomainOfTheInitiatingPageForLoginAuthorization, enum.WxOpenMessageAndEventReceptionConfiguration, enum.WxOpenWhiteListIp)
  181. e.OutSuc(c, md.WxOpenGetResp{
  182. Token: UserWxAppletList.Token,
  183. AesKey: UserWxAppletList.AesKey,
  184. Appid: UserWxAppletList.Appid,
  185. AppSecret: UserWxAppletList.AppSecret,
  186. WxOpenAppletServerDomain: res[enum.WxOpenAppletServerDomain],
  187. WxOpenAuthorizationEventReceivingConfiguration: res[enum.WxOpenAuthorizationEventReceivingConfiguration],
  188. WxOpenDomainOfTheInitiatingPageForLoginAuthorization: res[enum.WxOpenDomainOfTheInitiatingPageForLoginAuthorization],
  189. WxOpenMessageAndEventReceptionConfiguration: res[enum.WxOpenMessageAndEventReceptionConfiguration],
  190. WxOpenWhiteListIp: res[enum.WxOpenWhiteListIp],
  191. }, nil)
  192. return
  193. }
  194. type WxOpenThirdPartyAppList struct {
  195. Id int `json:"id" xorm:"not null pk autoincr unique(IDX_UUID_TYPE) INT(11)"`
  196. Uuid int `json:"uuid" xorm:"not null comment('站长id') index unique(IDX_UUID_TYPE) INT(10)"`
  197. Token string `json:"token" xorm:"not null default '' comment('消息校验Token') VARCHAR(255)"`
  198. AesKey string `json:"aes_key" xorm:"not null default '' comment('消息加解密Key') VARCHAR(255)"`
  199. Appid string `json:"appid" xorm:"not null default '' comment('appid') CHAR(50)"`
  200. AppSecret string `json:"app_secret" xorm:"not null default '' comment('appSecret') VARCHAR(255)"`
  201. ComponentVerifyTicket string `json:"component_verify_ticket" xorm:"not null default '' comment('验证票据') VARCHAR(255)"`
  202. ComponentAccessToken string `json:"component_access_token" xorm:"not null default '' comment('接口令牌') VARCHAR(255)"`
  203. CreateAt string `json:"create_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
  204. UpdateAt string `json:"update_at" xorm:"not null default 'CURRENT_TIMESTAMP' DATETIME"`
  205. }
  206. // AppletList
  207. // @Summary 列表
  208. // @Tags 设置中心-小程序设置
  209. // @Description 小程序设置-列表
  210. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  211. // @Accept json
  212. // @Produce json
  213. // @Success 200 {object} WxOpenThirdPartyAppList
  214. // @Failure 400 {object} md.Response "具体错误"
  215. // @Router /api/setCenter/applet/list [GET]
  216. func AppletList(c *gin.Context) {
  217. masterId := svc.GetMasterId(c)
  218. //1、查找对应 user_wx_applet_list 记录
  219. userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
  220. UserWxAppletList, err := userWxAppletListDb.GetUserWxAppletList(masterId)
  221. if err != nil {
  222. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  223. return
  224. }
  225. e.OutSuc(c, UserWxAppletList, nil)
  226. return
  227. }
  228. // AppletAdd
  229. // @Summary 新增
  230. // @Tags 设置中心-小程序设置
  231. // @Description 小程序设置-新增
  232. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  233. // @Accept json
  234. // @Produce json
  235. // @Param args body md.AppletAddReq true "请求参数"
  236. // @Success 200 {string} "success"
  237. // @Failure 400 {object} md.Response "具体错误"
  238. // @Router /api/setCenter/applet/add [POST]
  239. func AppletAdd(c *gin.Context) {
  240. var req md.AppletAddReq
  241. err := c.ShouldBindJSON(&req)
  242. if err != nil {
  243. err = validate.HandleValidateErr(err)
  244. err1 := err.(e.E)
  245. e.OutErr(c, err1.Code, err1.Error())
  246. return
  247. }
  248. masterId := svc.GetMasterId(c)
  249. //1、查找对应 user_wx_applet_list 记录
  250. userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
  251. UserWxAppletList, err := userWxAppletListDb.GetUserWxAppletList(masterId)
  252. if err != nil {
  253. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  254. return
  255. }
  256. if UserWxAppletList != nil {
  257. e.OutErr(c, e.ERR, "当前仅支持单个小程序")
  258. return
  259. }
  260. _, err = userWxAppletListDb.UserWxAppletListInsert(&model.UserWxAppletList{
  261. Name: req.Name,
  262. Logo: req.Logo,
  263. Appid: req.Appid,
  264. OriginalId: req.OriginalId,
  265. AuthorizerRefreshToken: "",
  266. IsAuth: 0,
  267. Uuid: masterId,
  268. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  269. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  270. })
  271. if err != nil {
  272. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  273. return
  274. }
  275. e.OutSuc(c, "success", nil)
  276. return
  277. }
  278. // AppletUpdate
  279. // @Summary 更新
  280. // @Tags 设置中心-小程序设置
  281. // @Description 小程序设置-更新
  282. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  283. // @Accept json
  284. // @Produce json
  285. // @Param args body md.AppletUpdateReq true "请求参数"
  286. // @Success 200 {string} "success"
  287. // @Failure 400 {object} md.Response "具体错误"
  288. // @Router /api/setCenter/applet/update [POST]
  289. func AppletUpdate(c *gin.Context) {
  290. var req md.AppletUpdateReq
  291. err := c.ShouldBindJSON(&req)
  292. if err != nil {
  293. err = validate.HandleValidateErr(err)
  294. err1 := err.(e.E)
  295. e.OutErr(c, err1.Code, err1.Error())
  296. return
  297. }
  298. masterId := svc.GetMasterId(c)
  299. //1、查找对应 user_wx_applet_list 记录
  300. userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
  301. UserWxAppletList, err := userWxAppletListDb.GetUserWxAppletList(masterId)
  302. if err != nil {
  303. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  304. return
  305. }
  306. if UserWxAppletList == nil {
  307. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  308. return
  309. }
  310. UserWxAppletList.Logo = req.Logo
  311. UserWxAppletList.Name = req.Name
  312. _, err = userWxAppletListDb.UpdateUserWxAppletList(UserWxAppletList, "logo", "name")
  313. if err != nil {
  314. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  315. return
  316. }
  317. e.OutSuc(c, "success", nil)
  318. return
  319. }
  320. // AppletDelete
  321. // @Summary 删除
  322. // @Tags 设置中心-小程序设置
  323. // @Description 小程序设置-删除
  324. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  325. // @Accept json
  326. // @Produce json
  327. // @Success 200 {string} "success"
  328. // @Failure 400 {object} md.Response "具体错误"
  329. // @Router /v1/banner/delete/{$id} [DELETE]
  330. func AppletDelete(c *gin.Context) {
  331. id := c.Param("id")
  332. masterId := svc.GetMasterId(c)
  333. //1、查找对应 user_wx_applet_list 记录
  334. userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
  335. UserWxAppletList, err := userWxAppletListDb.GetUserWxAppletList(masterId)
  336. if err != nil {
  337. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  338. return
  339. }
  340. if UserWxAppletList == nil {
  341. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  342. return
  343. }
  344. _, err = userWxAppletListDb.UserWxAppletListDelete(id)
  345. if err != nil {
  346. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  347. return
  348. }
  349. e.OutSuc(c, "success", nil)
  350. return
  351. }
  352. // AppletAuthorize
  353. // @Summary 设置中心-基础设置-微信三方应用获取
  354. // @Tags 设置中心
  355. // @Description 设置中心-基础设置
  356. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  357. // @Accept json
  358. // @Produce json
  359. // @Success 200 {string} "微信授权界面url"
  360. // @Failure 400 {object} md.Response "具体错误"
  361. // @Router /api/setCenter/applet/authorize [GET]
  362. func AppletAuthorize(c *gin.Context) {
  363. masterId := svc.GetMasterId(c)
  364. url := "http://super.advertisement.dengbiao.top/api/wxOpen/getPreAuthCode?master_id=" + masterId
  365. if cfg.Prd {
  366. url = "http://super.advertisement.dengbiao.top/api/wxOpen/getPreAuthCode?master_id=" + masterId
  367. }
  368. e.OutSuc(c, map[string]string{
  369. "url": url,
  370. }, nil)
  371. return
  372. }
  373. // AppletUnauthorized
  374. // @Summary 设置中心-基础设置-微信三方应用获取
  375. // @Tags 设置中心
  376. // @Description 设置中心-基础设置
  377. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  378. // @Accept json
  379. // @Produce json
  380. // @Success 200 {string} "success"
  381. // @Failure 400 {object} md.Response "具体错误"
  382. // @Router /api/setCenter/applet/unauthorized [GET]
  383. func AppletUnauthorized(c *gin.Context) {
  384. appId := c.DefaultQuery("app_id", "")
  385. masterId := svc.GetMasterId(c)
  386. //1、查找对应 user_wx_applet_list 记录
  387. userWxAppletListDb := implement.NewUserWxAppletListDb(db.Db)
  388. UserWxAppletList, err := userWxAppletListDb.GetUserWxAppletList(masterId)
  389. if err != nil {
  390. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  391. return
  392. }
  393. if UserWxAppletList == nil {
  394. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
  395. return
  396. }
  397. wxOpenThirdPartyAppListDb := implement.NewWxOpenThirdPartyAppListDb(db.Db)
  398. wxOpenThirdPartyAppList, err := wxOpenThirdPartyAppListDb.GetWxOpenThirdPartyAppList(utils.StrToInt(masterId))
  399. if err != nil {
  400. e.OutErr(c, e.ERR, err.Error())
  401. return
  402. }
  403. if wxOpenThirdPartyAppList == nil {
  404. e.OutErr(c, e.ERR_NOT_FAN, "未查询到对应三方应用记录")
  405. return
  406. }
  407. wxApiService, err := wechat.NewWxApiService(masterId, wxOpenThirdPartyAppList.Appid, wxOpenThirdPartyAppList.AppSecret)
  408. if err != nil {
  409. e.OutErr(c, e.ERR, err.Error())
  410. return
  411. }
  412. err = wxApiService.DelAuthorize(appId)
  413. if err != nil {
  414. e.OutErr(c, e.ERR, err.Error())
  415. return
  416. }
  417. e.OutSuc(c, "success", nil)
  418. return
  419. }
  420. // ShareIndex
  421. // @Summary 邀请链接
  422. // @Tags 设置中心-邀请链接
  423. // @Description 邀请链接界面接口
  424. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  425. // @Accept json
  426. // @Produce json
  427. // @Success 200 {object} md.ShareIndexResp
  428. // @Failure 400 {object} md.Response "具体错误"
  429. // @Router /api/setCenter/share/index [GET]
  430. func ShareIndex(c *gin.Context) {
  431. masterId := svc.GetMasterId(c)
  432. userAppDomainDb := implement.NewUserAppDomainDb(db.Db)
  433. agent, err := userAppDomainDb.GetAppDomainByType("agent", masterId)
  434. if err != nil {
  435. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  436. return
  437. }
  438. medium, err := userAppDomainDb.GetAppDomainByType("medium", masterId)
  439. if err != nil {
  440. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  441. return
  442. }
  443. agentDomain := ""
  444. if agent != nil {
  445. agentDomain = agent.Domain
  446. }
  447. mediumDomain := ""
  448. if medium != nil {
  449. mediumDomain = medium.Domain
  450. }
  451. e.OutSuc(c, md.ShareIndexResp{
  452. MasterId: masterId,
  453. AgentDomain: agentDomain,
  454. MediumDomain: mediumDomain,
  455. }, nil)
  456. return
  457. }
  458. // SetMob
  459. // @Summary mob设置
  460. // @Tags 设置中心-基础设置
  461. // @Description 基础设置-mob设置
  462. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  463. // @Accept json
  464. // @Produce json
  465. // @Param args body md.SetMobReq true "请求参数"
  466. // @Success 200 {string} "success"
  467. // @Failure 400 {object} md.Response "具体错误"
  468. // @Router /api/setCenter/basic/setMob [POST]
  469. func SetMob(c *gin.Context) {
  470. var req md.SetMobReq
  471. err := c.ShouldBindJSON(&req)
  472. if err != nil {
  473. err = validate.HandleValidateErr(err)
  474. err1 := err.(e.E)
  475. e.OutErr(c, err1.Code, err1.Error())
  476. return
  477. }
  478. masterId := svc.GetMasterId(c)
  479. engine := db.DBs[masterId]
  480. sysCfgDb := implement2.NewSysCfgDb(engine, masterId)
  481. sysCfgDb.SysCfgUpdate("mob_app_key", req.MobAppKey)
  482. sysCfgDb.SysCfgUpdate("mob_app_secret", req.MobAppSecret)
  483. svc.ClearRedis(c, "mob_app_key")
  484. svc.ClearRedis(c, "mob_app_secret")
  485. e.OutSuc(c, "success", nil)
  486. return
  487. }
  488. // GetMob
  489. // @Summary mob获取
  490. // @Tags 设置中心-基础设置
  491. // @Description 基础设置-mob获取
  492. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  493. // @Accept json
  494. // @Produce json
  495. // @Success 200 {object} md.SetOssResp
  496. // @Failure 400 {object} md.Response "具体错误"
  497. // @Router /api/setCenter/basic/getMob [GET]
  498. func GetMob(c *gin.Context) {
  499. masterId := svc.GetMasterId(c)
  500. engine := db.DBs[masterId]
  501. sysCfgDb := implement2.NewSysCfgDb(engine, masterId)
  502. res := sysCfgDb.SysCfgFindWithDb("mob_app_key", "mob_app_secret")
  503. e.OutSuc(c, md.SetMobResp{
  504. Data: md.SetMobReq{
  505. MobAppSecret: res["mob_app_secret"],
  506. MobAppKey: res["mob_app_key"],
  507. },
  508. }, nil)
  509. return
  510. }