Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support custom PhoneBind/Alipay feature just use config file #160

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) 搜索引擎:
Expand Down
2 changes: 1 addition & 1 deletion config.yaml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 3 additions & 4 deletions internal/middleware/priv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
40 changes: 27 additions & 13 deletions internal/routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ func NewRouter() *gin.Engine {
// 发送验证码
r.POST("/captcha", api.PostCaptcha)

// 支付宝回调
r.POST("/alipay/notify", api.AlipayNotify)

// 无鉴权路由组
noAuthApi := r.Group("/")
{
Expand Down Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -185,20 +175,26 @@ 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{
"code": 404,
"msg": "Not Found",
})
})

// 默认405
e.NoMethod(func(c *gin.Context) {
c.JSON(http.StatusMethodNotAllowed, gin.H{
"code": 405,
"msg": "Method Not Allowed",
})
})

return e
}

Expand All @@ -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)
}
1 change: 1 addition & 0 deletions web/.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 11 additions & 6 deletions web/src/views/Setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
:src="store.state.userInfo.avatar"
/>
<n-upload
v-if="
v-if="!allowPhoneBind || (
allowPhoneBind &&
store.state.userInfo.phone &&
store.state.userInfo.phone.length > 0
store.state.userInfo.phone.length > 0)
"
ref="avatarRef"
:action="uploadGateway"
Expand Down Expand Up @@ -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"
>
Expand All @@ -70,7 +72,9 @@
</div>
</n-card>

<n-card title="手机号" size="small" class="setting-card">
<n-card
v-if="allowPhoneBind"
title="手机号" size="small" class="setting-card">
<div
v-if="
store.state.userInfo.phone &&
Expand Down Expand Up @@ -270,6 +274,7 @@ import type {
InputInst,
} from 'naive-ui';

const allowPhoneBind= (import.meta.env.VITE_ALLOW_PHONE_BIND.toLocaleLowerCase() === 'true')
const uploadGateway = import.meta.env.VITE_HOST + '/v1/attachment';
const uploadToken = 'Bearer ' + localStorage.getItem('PAOPAO_TOKEN');
const uploadType = ref('public/avatar');
Expand Down
1 change: 1 addition & 0 deletions web/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface ImportMetaEnv {
readonly VITE_ALLOW_TWEET_ATTACHMENT_PRICE: string
readonly VITE_ALLOW_TWEET_VIDEO: string
readonly VITE_ALLOW_TWEET_VISIBILITY: string
readonly VITE_ALLOW_PHONE_BINDING: string
readonly VITE_DEFAULT_TWEET_VISIBILITY: string
}

Expand Down