From 6029e061dc056f510692595821c4ce62d6098da0 Mon Sep 17 00:00:00 2001 From: Michael Li Date: Fri, 22 Jul 2022 16:06:27 +0800 Subject: [PATCH] support custom PhoneBind/Alipay feature just use config file --- README.md | 9 ++++++--- config.yaml.sample | 2 +- internal/middleware/priv.go | 7 +++---- internal/routers/router.go | 40 +++++++++++++++++++++++++------------ web/.env | 1 + web/src/views/Setting.vue | 17 ++++++++++------ web/src/vite-env.d.ts | 1 + 7 files changed, 50 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 2873073c..cc8c068b 100644 --- a/README.md +++ b/README.md @@ -320,9 +320,12 @@ release/paopao-ce --no-default-features --features sqlite3,localoss,loggerfile,r `LoggerFile` 使用文件写日志(目前状态: 稳定); `LoggerZinc` 使用[Zinc](https://github.com/zinclabs/zinc)写日志(目前状态: 稳定,推荐使用); `LoggerMeili` 使用[Meilisearch](https://github.com/meilisearch/meilisearch)写日志(目前状态: 内测阶段); -* 支付: Alipay -* 短信验证码: SmsJuhe(需要开启sms) - `Sms`功能如果没有开启,任意短信验证码都可以绑定手机; +* 支付: Alipay + `Alipay` 开启基于[支付宝开放平台](https://open.alipay.com/)的钱包功能; +* 短信验证码: SmsJuhe(需要开启sms) + `Sms` 开启短信验证码功能,用于手机绑定验证手机是否注册者的;功能如果没有开启,手机绑定时任意短信验证码都可以绑定手机; +* 其他: PhoneBind + `PhoneBind` 开启手机绑定功能; ### 搭建依赖环境 #### [Zinc](https://github.com/zinclabs/zinc) 搜索引擎: diff --git a/config.yaml.sample b/config.yaml.sample index ad713128..262ced2b 100644 --- a/config.yaml.sample +++ b/config.yaml.sample @@ -15,7 +15,7 @@ Features: Develop: ["Base", "MySQL", "BigCacheIndex", "Meili", "Sms", "AliOSS", "LoggerMeili", "Migration"] Demo: ["Base", "MySQL", "Option", "Zinc", "Sms", "MinIO", "LoggerZinc"] Slim: ["Base", "Sqlite3", "LocalOSS", "LoggerFile"] - Base: ["Redis", "Alipay"] + Base: ["Redis", "Alipay", "PhoneBind"] Option: ["SimpleCacheIndex"] Sms: "SmsJuhe" SmsJuhe: diff --git a/internal/middleware/priv.go b/internal/middleware/priv.go index 6f048e66..add6ecd8 100644 --- a/internal/middleware/priv.go +++ b/internal/middleware/priv.go @@ -2,30 +2,29 @@ package middleware import ( "github.com/gin-gonic/gin" + "github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/model" "github.com/rocboss/paopao-ce/pkg/app" "github.com/rocboss/paopao-ce/pkg/errcode" ) func Priv() gin.HandlerFunc { + enablePhoneBind := conf.CfgIf("PhoneBind") return func(c *gin.Context) { if user, exist := c.Get("USER"); exist { if userModel, ok := user.(*model.User); ok { if userModel.Status == model.UserStatusNormal { - - if userModel.Phone == "" { + if enablePhoneBind && userModel.Phone == "" { response := app.NewResponse(c) response.ToErrorResponse(errcode.AccountNoPhoneBind) c.Abort() return } - c.Next() return } } } - response := app.NewResponse(c) response.ToErrorResponse(errcode.UserHasBeenBanned) c.Abort() diff --git a/internal/routers/router.go b/internal/routers/router.go index 80012733..3dbeee71 100644 --- a/internal/routers/router.go +++ b/internal/routers/router.go @@ -49,9 +49,6 @@ func NewRouter() *gin.Engine { // 发送验证码 r.POST("/captcha", api.PostCaptcha) - // 支付宝回调 - r.POST("/alipay/notify", api.AlipayNotify) - // 无鉴权路由组 noAuthApi := r.Group("/") { @@ -108,7 +105,9 @@ func NewRouter() *gin.Engine { authApi.GET("/user/stars", api.GetUserStars) // 绑定用户手机号 - authApi.POST("/user/phone", api.BindUserPhone) + if conf.CfgIf("PhoneBind") { + authApi.POST("/user/phone", api.BindUserPhone) + } // 修改密码 authApi.POST("/user/password", api.ChangeUserPassword) @@ -125,15 +124,6 @@ func NewRouter() *gin.Engine { // 检索标签 authApi.GET("/suggest/tags", api.GetSuggestTags) - // 用户充值 - authApi.POST("/user/recharge", api.GetUserRechargeLink) - - // 用户充值 - authApi.GET("/user/recharge", api.GetUserRechargeResult) - - // 获取用户账单 - authApi.GET("/user/wallet/bills", api.GetUserWalletBills) - // 上传资源 privApi.POST("/attachment", api.UploadAttachment) @@ -185,6 +175,10 @@ func NewRouter() *gin.Engine { // 管理·禁言/解封用户 adminApi.POST("/admin/user/status", api.ChangeUserStatus) } + + // 支付宝路由注册 + alipayRoute(r, authApi) + // 默认404 e.NoRoute(func(c *gin.Context) { c.JSON(http.StatusNotFound, gin.H{ @@ -192,6 +186,7 @@ func NewRouter() *gin.Engine { "msg": "Not Found", }) }) + // 默认405 e.NoMethod(func(c *gin.Context) { c.JSON(http.StatusMethodNotAllowed, gin.H{ @@ -199,6 +194,7 @@ func NewRouter() *gin.Engine { "msg": "Method Not Allowed", }) }) + return e } @@ -216,3 +212,21 @@ func routeLocalOSS(e *gin.Engine) { logrus.Infof("register LocalOSS route in /oss on save path: %s", savePath) } + +func alipayRoute(public gin.IRoutes, authApi gin.IRoutes) { + if !conf.CfgIf("Alipay") { + return + } + + // 支付宝回调 + public.POST("/alipay/notify", api.AlipayNotify) + + // 用户充值 + authApi.POST("/user/recharge", api.GetUserRechargeLink) + + // 获取钱包余额 + authApi.GET("/user/recharge", api.GetUserRechargeResult) + + // 获取用户账单 + authApi.GET("/user/wallet/bills", api.GetUserWalletBills) +} diff --git a/web/.env b/web/.env index efd7ee10..7645f934 100644 --- a/web/.env +++ b/web/.env @@ -8,6 +8,7 @@ VITE_ALLOW_TWEET_ATTACHMENT=true VITE_ALLOW_TWEET_ATTACHMENT_PRICE=true VITE_ALLOW_TWEET_VIDEO=true VITE_ALLOW_TWEET_VISIBILITY=true +VITE_ALLOW_PHONE_BIND=true # 局部参数 VITE_DEFAULT_TWEET_VISIBILITY=public diff --git a/web/src/views/Setting.vue b/web/src/views/Setting.vue index f18024e2..7bd29580 100644 --- a/web/src/views/Setting.vue +++ b/web/src/views/Setting.vue @@ -9,9 +9,10 @@ :src="store.state.userInfo.avatar" /> 0 + store.state.userInfo.phone.length > 0) " ref="avatarRef" :action="uploadGateway" @@ -48,11 +49,12 @@ round type="success" size="small" - v-if=" - !showNicknameEdit && + v-if="!showNicknameEdit && (!allowPhoneBind || ( + allowPhoneBind && store.state.userInfo.phone && store.state.userInfo.phone.length > 0 && - store.state.userInfo.status == 1 + store.state.userInfo.status == 1) + ) " @click="handleNicknameShow" > @@ -70,7 +72,9 @@ - +