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

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