Merge branch 'zhangzeliang' into dev

dev
RIceWqy 2 years ago
commit 6260a1f013

@ -1 +1 @@
{"pid":28155}
{"pid":33859}

@ -1,7 +1,6 @@
package tencent
import (
"encoding/json"
"fmt"
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
@ -87,7 +86,7 @@ func SendSMSVerificationCode(templateID, signName, mobile, verificationCode stri
request.SenderId = common.StringPtr("")
// 通过client对象调用想要访问的接口需要传入请求对象
response, err := client.SendSms(request)
_, err := client.SendSms(request)
// 处理异常
if _, ok := err.(*errors.TencentCloudSDKError); ok {
fmt.Printf("An API error has returned: %s", err)
@ -97,9 +96,6 @@ func SendSMSVerificationCode(templateID, signName, mobile, verificationCode stri
if err != nil {
return err
}
b, _ := json.Marshal(response.Response)
// 打印返回的json字符串
fmt.Printf("%s", b)
/*
* [FailedOperation.SignatureIncorrectOrUnapproved](https://cloud.tencent.com/document/product/382/9558#.E7.9F.AD.E4.BF.A1.E5.8F.91.E9.80.81.E6.8F.90.E7.A4.BA.EF.BC.9Afailedoperation.signatureincorrectorunapproved-.E5.A6.82.E4.BD.95.E5.A4.84.E7.90.86.EF.BC.9F)

@ -8,26 +8,26 @@ import (
"time"
)
var regSMSVerificationCode sync.Map
var loginOrregSMSVerificationCode sync.Map
// 登记注册用验证码
func StoreRegSMSVerificationCode(mobile string) (string, error) {
codeInterface, ok := regSMSVerificationCode.Load(mobile)
func StoreLoginOrloginOrRegSMSVerificationCode(mobile string) (string, error) {
codeInterface, ok := loginOrregSMSVerificationCode.Load(mobile)
if ok {
return codeInterface.(string), nil
}
verificationCode := randCharNumber(6)
regSMSVerificationCode.Store(mobile, verificationCode)
loginOrregSMSVerificationCode.Store(mobile, verificationCode)
go func(mobile string) {
time.Sleep(time.Second * 5 * 60)
regSMSVerificationCode.Delete(mobile)
loginOrregSMSVerificationCode.Delete(mobile)
}(mobile)
return verificationCode, nil
}
// 验证注册验证码
func RegSMSVerification(mobile, verificationCode string) bool {
codeInterface, ok := regSMSVerificationCode.LoadAndDelete(mobile)
func LoginOrRegSMSVerification(mobile, verificationCode string) bool {
codeInterface, ok := loginOrregSMSVerificationCode.LoadAndDelete(mobile)
if !ok {
return false
}

@ -2,9 +2,11 @@ package usercenter
import (
"encoding/json"
"log"
"regexp"
"src/module/tencent"
"github.com/towgo/towgo/dao/basedboperat"
"github.com/towgo/towgo/towgo"
)
@ -15,7 +17,8 @@ func InitManageApi() {
//注册JSON-RPC服务处理器method路由
//账户登录 F
towgo.SetFunc(_methodHead+"/user/login", userLogin)
towgo.SetFunc(_methodHead+"/user/loginOrRegByMobile", userLoginOrRegByMobile)
//获取自己的账户信息
towgo.SetFunc(_methodHead+"/user/myinfo", userMyinfo)
@ -27,7 +30,7 @@ func InitManageApi() {
towgo.SetFunc(_methodHead+"/user/regByMobile", userRegByMobile)
//获取注册短信验证码
towgo.SetFunc(_methodHead+"/user/getRegSMSVerificationCode", getRegSMSVerificationCode)
towgo.SetFunc(_methodHead+"/user/getLoginOrRegSMSVerificationCode", getLoginOrRegSMSVerificationCode)
//修改密码 F
towgo.SetFunc(_methodHead+"/user/changepassword", userChangepassword)
@ -42,7 +45,7 @@ func isPhoneNumber(input string) bool {
return reg.MatchString(input)
}
func getRegSMSVerificationCode(rpcConn towgo.JsonRpcConnection) {
func getLoginOrRegSMSVerificationCode(rpcConn towgo.JsonRpcConnection) {
var params struct {
Mobile string `json:"mobile"`
}
@ -53,7 +56,11 @@ func getRegSMSVerificationCode(rpcConn towgo.JsonRpcConnection) {
return
}
code, err := StoreRegSMSVerificationCode(params.Mobile)
code, err := StoreLoginOrloginOrRegSMSVerificationCode(params.Mobile)
if err != nil {
rpcConn.WriteError(500, err.Error())
return
}
err = tencent.SendSMSVerificationCode("2030693", "蕊鑫信息科技", params.Mobile, code)
if err != nil {
@ -85,7 +92,7 @@ func userRegByMobile(rpcConn towgo.JsonRpcConnection) {
return
}
if !RegSMSVerification(params.Mobile, params.VerificationCode) {
if !LoginOrRegSMSVerification(params.Mobile, params.VerificationCode) {
rpcConn.WriteError(500, "验证码错误")
return
}
@ -160,6 +167,69 @@ func userLogin(rpcConn towgo.JsonRpcConnection) {
}
// 用户手机验证码登陆
func userLoginOrRegByMobile(rpcConn towgo.JsonRpcConnection) {
var params struct {
Mobile string `json:"mobile"`
VerificationCode string `json:"verification_code"`
}
rpcConn.ReadParams(&params)
if params.Mobile == "" {
rpcConn.WriteError(500, "手机号码不能为空")
return
}
if params.VerificationCode == "" {
rpcConn.WriteError(500, "验证码不能为空")
return
}
if !LoginOrRegSMSVerification(params.Mobile, params.VerificationCode) {
rpcConn.WriteError(500, "验证码错误")
return
}
//验证通过
user := User{}
basedboperat.Get(&user, nil, "username = ?", params.Mobile)
if user.ID > 0 {
loginErr := user.LoginNoAuth(params.Mobile)
if loginErr != nil { //模型层登陆成功
log.Print(loginErr.Error())
//dblog.Write("user:info", fmt.Sprintf("%s@%s 登录失败! 错误信息:%s", user.Username, rpcConn.GetRemoteAddr(), loginErr.Error()))
rpcConn.WriteError(500, "用户名或密码错误")
return
}
user.Password = ""
user.Salt = ""
//dblog.Write("user:info", fmt.Sprintf("%s@%s 登录成功!", user.Username, rpcConn.GetRemoteAddr()))
rpcConn.WriteResult(user)
return
}
user.Username = params.Mobile
user.Password = randCharNumber(8)
Err := user.Reg(user.Username, user.Password)
if Err != nil {
rpcConn.WriteError(500, Err.Error())
return
}
user.Password = ""
user.Salt = ""
rpcConn.WriteResult(user)
}
// token check
func userTokenCheck(rpcConn towgo.JsonRpcConnection) {
result := map[string]interface{}{} //初始化结果参数

@ -30,6 +30,31 @@ type User struct {
UserToken *UserToken `json:"-" gorm:"-" xorm:"-"`
CreatedAt int64 `json:"created_at"` //创建时间
UpdatedAt int64 `json:"updated_at"` //更新时间
GroupID int64 `json:"group_id"` // 用户组ID
Avatar string `json:"avatar"` // 用户头像
Birthday string `json:"birthday"` // 用户生日
Money string `json:"money"` // 用户资金
Score int64 `json:"score"` // 用户积分
//Verification Verification `json:"verification"` // 用户验证信息
ChildUserCount int64 `json:"child_user_count"` // 子用户数量
ChildUserCount1 int64 `json:"child_user_count_1"` // 第一层子用户数量
ChildUserCount2 int64 `json:"child_user_count_2"` // 第二层子用户数量
TotalConsume string `json:"total_consume"` // 总消费金额
UserID int64 `json:"user_id" xorm:"-"` // 用户ID
Createtime int64 `json:"createtime"` // 创建时间
Expiretime int64 `json:"expiretime"` // 过期时间
ExpiresIn int64 `json:"expires_in"` // 过期时长
//Group Group `json:"group"` // 用户组信息
}
type Verification struct {
Email int `json:"email"` // 邮箱验证状态
Mobile int `json:"mobile"` // 手机验证状态
}
type Group struct {
Name string `json:"name"` // 用户组名
Image string `json:"image"` // 用户组图片
}
// 注册
@ -54,6 +79,7 @@ func (a *User) Reg(username, password string) error {
a.NewPassword(password)
a.Username = username
a.Mobile = username
a.CanDelete = true
_, err := basedboperat.Create(a) // 通过数据的指针来创建
@ -147,6 +173,31 @@ func (a *User) Login(username, password string) error {
return nil
}
// 用户登陆
func (a *User) LoginNoAuth(username string) error {
//通过用户名查询用户数据
err := basedboperat.Get(a, nil, "username = ?", username)
if err != nil {
return err
}
//检查用户名是否存在
//判断用户是否存在
if a.Username == "" {
return errors.New("用户名不存在")
}
//验证通过
//生成用户信息
a.UserToken = NewToken(a)
a.Token = a.UserToken.TokenKey
return nil
}
// 用户注销
func (a *User) Logoff() {
DeleteToken(a.UserToken.TokenKey)
@ -276,5 +327,5 @@ func (a *User) AfterQuery() {
if a.ID == 0 {
return
}
a.UserID = a.ID
}

Loading…
Cancel
Save