Vestmore_GO/server/app/internal/handlers/actions/auth.go

396 lines
9.9 KiB
Go
Raw Normal View History

2024-04-07 10:12:24 +00:00
package actions
import (
2024-04-15 08:08:17 +00:00
"encoding/json"
2024-04-11 10:04:03 +00:00
"strings"
2024-04-12 02:20:29 +00:00
"time"
2024-04-11 10:04:03 +00:00
"github.com/iapologizewhenimwrong/Vestmore_GO/model"
2024-04-12 09:17:10 +00:00
"github.com/iapologizewhenimwrong/Vestmore_GO/translator"
2024-04-09 10:17:08 +00:00
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/auth"
2024-04-09 05:38:29 +00:00
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/basic"
2024-04-09 10:17:08 +00:00
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/email"
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/encryption_decryption"
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/log"
2024-04-07 10:12:24 +00:00
)
2024-04-09 10:17:08 +00:00
var CompanyKey = "vestmore-bjwl"
2024-04-07 10:12:24 +00:00
// @Action base/getToken
// Base_GetToken
2024-04-15 08:08:17 +00:00
// randstr: string;
// sign: string;
// action: string;
// lang: string;
// app_market: int64;
// timestamp: int64;
// token: string;
2024-04-12 09:17:10 +00:00
func BaseGetToken(ctx *ActionContext[BaseGetTokenParam]) (resp *basic.Response) {
2024-04-15 08:08:17 +00:00
const (
TokenExpiry = 864000 // Token过期时间单位
)
2024-04-12 02:20:29 +00:00
2024-04-15 08:08:17 +00:00
type TokenData struct {
LanguageID uint64 `json:"language_id"`
LanguageCode string `json:"language_code"`
}
modelToken := model.Models.KillaraCustomerTokenModel
modelLanguage := model.Models.KillaraCatalogLanguageModel
lang := ctx.Lang
language, err := modelLanguage.GetLanguageByCodeForBackEnd(lang)
if err != nil || language == nil {
language, _ = modelLanguage.GetLanguageByCodeForBackEnd("en")
}
tokenData := TokenData{
LanguageID: *language.LanguageId,
LanguageCode: *language.Code,
}
isLogin := false
var token string
if ctx.Param.Token != "" {
result, err := modelToken.CheckToken(ctx.Param.Token)
if err == nil && result != nil {
token = ctx.Param.Token
// Token存在并且有效
modelToken.UpdateTokenExpiry(token, time.Now().Unix()+TokenExpiry)
tokenDataJSON, _ := json.Marshal(tokenData)
modelToken.UpdateTokenData(token, map[string]interface{}{
"data": string(tokenDataJSON),
})
if result.CustomerId != nil && *result.CustomerId != 0 {
modelCustomer := model.Models.KillaraCustomerModel
customer, err := modelCustomer.GetCustomer(*result.CustomerId)
if err == nil && customer != nil {
isLogin = true
} else {
modelToken.UpdateTokenCustomerID(token, 0)
}
}
}
}
if token == "" {
// Token不存在或失效重新创建一个
token = auth.GenerateMD5()
tokenDataJSON, err := json.Marshal(tokenData)
if err != nil {
return resp.Error(basic.ErrJSONUnMarshal)
}
tokenItem := &model.KillaraCustomerToken{
Token: &token,
CustomerId: basic.Uint64Ptr(0),
Data: basic.StringPtr(string(tokenDataJSON)),
Expiry: basic.Int64Ptr(time.Now().Unix() + TokenExpiry),
Platform: basic.Int64Ptr(1),
}
err = modelToken.InsertToken(tokenItem)
log.Println(*tokenItem.Token)
2024-04-15 08:08:17 +00:00
if err != nil {
return resp.ErrorErr(1, err)
}
}
return resp.Success(map[string]interface{}{
"token": token,
"is_login": isLogin,
"lang_data": map[string]interface{}{
"language_id": tokenData.LanguageID,
"language_code": tokenData.LanguageCode,
}})
2024-04-07 10:12:24 +00:00
}
// @Action account/loginWithTelephonePassword
// AccountLoginWithTelephonePassword
2024-04-11 10:04:03 +00:00
// randstr: string;
// sign: string;
2024-04-07 10:12:24 +00:00
// telephone: string;
2024-04-11 10:04:03 +00:00
// version: string;.0.14
// token: string;
// country_code: string; 86
// password: string;
// action: string;
// device: string;,12
// app_market: uint64;
// email: string;
// timestamp: int64;
2024-04-12 09:17:10 +00:00
func AccountLoginWithTelephonePassword(ctx *ActionContext[AccountLoginWithTelephonePasswordParam]) (resp *basic.Response) {
2024-04-07 10:12:24 +00:00
// ctx.ShouldBind()
2024-04-08 10:13:01 +00:00
// model.Models.KillaraCustomerModel.Find()
2024-04-12 09:17:10 +00:00
if ctx.Param.CountryCode == "" {
2024-04-12 02:20:29 +00:00
resp.ErrorMsg(1, "country_code 参数缺失")
2024-04-11 10:04:03 +00:00
return
}
2024-04-12 09:17:10 +00:00
if ctx.Param.Telephone == "" {
2024-04-12 02:20:29 +00:00
resp.ErrorMsg(1, "telephone 参数缺失")
2024-04-11 10:04:03 +00:00
return
}
2024-04-12 09:17:10 +00:00
if ctx.Param.Password == "" {
2024-04-12 02:20:29 +00:00
resp.ErrorMsg(1, "password 参数缺失")
2024-04-11 10:04:03 +00:00
return
}
2024-04-12 09:17:10 +00:00
telephone := strings.TrimSpace(ctx.Param.Telephone)
password := strings.TrimSpace(ctx.Param.Password)
countryCode := strings.TrimSpace(ctx.Param.CountryCode)
2024-04-11 10:04:03 +00:00
// 假设 modelCustomer 和 modelCustomerToken 是对应的服务接口
var customer *model.KillaraCustomer
var err error
customer, err = model.Models.KillaraCustomerModel.GetCustomerByTelephoneForBackEnd(telephone)
if err != nil {
2024-04-12 02:20:29 +00:00
resp.ErrorMsg(1, err.Error())
2024-04-11 10:04:03 +00:00
return
}
if customer == nil {
customer, err = model.Models.KillaraCustomerModel.GetCustomerByCode(telephone)
if err != nil {
2024-04-12 02:20:29 +00:00
resp.ErrorMsg(1, err.Error())
2024-04-11 10:04:03 +00:00
return
}
}
if customer == nil {
2024-04-12 09:17:10 +00:00
return resp.ErrorTrCode(ctx, translator.AccountNotRegistered)
2024-04-11 10:04:03 +00:00
}
if *customer.CountryCode != countryCode {
2024-04-12 09:17:10 +00:00
return resp.ErrorTrCode(ctx, translator.PhoneNumberCountryIncorrect)
2024-04-11 10:04:03 +00:00
}
if *customer.Status != 1 {
2024-04-12 09:17:10 +00:00
return resp.ErrorTrCode(ctx, translator.AccountDisabledNotLogin)
2024-04-11 10:04:03 +00:00
}
if !auth.CheckPassword(*customer.Password, *customer.Salt, *customer.RandomPassword, password) {
2024-04-12 09:17:10 +00:00
return resp.ErrorTrCode(ctx, translator.AccountOrPasswordLoginFailed)
2024-04-11 10:04:03 +00:00
}
customerID := *customer.CustomerId
2024-04-12 09:17:10 +00:00
err = model.Models.KillaraCustomerTokenModel.UpdateTokenCustomerID(ctx.Param.Token, customerID)
2024-04-11 10:04:03 +00:00
if err != nil {
2024-04-12 02:20:29 +00:00
return resp.ErrorMsg(1, err.Error())
2024-04-11 10:04:03 +00:00
}
var deviceCode string
2024-04-12 09:17:10 +00:00
if ctx.Param.Device != "" {
deviceCode = ctx.Param.Device
2024-04-11 10:04:03 +00:00
}
var version string
2024-04-12 09:17:10 +00:00
if ctx.Param.Version != "" {
version = ctx.Param.Version
2024-04-11 10:04:03 +00:00
}
var ip string
2024-04-12 09:17:10 +00:00
addr := strings.Split(ctx.GinCtx.Request.RemoteAddr, ":")
2024-04-11 10:04:03 +00:00
if len(addr) > 0 {
ip = addr[0]
}
2024-04-12 02:20:29 +00:00
data := &model.KillaraCustomerDevice{
CustomerId: &customerID,
Device: &deviceCode,
Version: &version,
Ip: &ip,
2024-04-12 09:17:10 +00:00
AppMarket: &ctx.Param.AppMarket,
2024-04-12 02:20:29 +00:00
Date: basic.TimePtr(time.Now()),
}
2024-04-11 10:04:03 +00:00
2024-04-12 02:20:29 +00:00
// 假设 insertCustomerDevice 是对应的服务接口
2024-04-11 10:04:03 +00:00
// insertCustomerDevice(data)
2024-04-12 02:20:29 +00:00
err = model.Models.KillaraCustomerModel.InsertCustomerDevice(data)
if err != nil {
return resp.ErrorErr(1, err)
}
2024-04-11 10:04:03 +00:00
// // 假设 clearDuplicateToken 是对应的服务接口
// clearDuplicateToken(customerID, param.Token)
2024-04-12 09:17:10 +00:00
model.Models.KillaraCustomerTokenModel.ClearDuplicateToken(customerID, ctx.Param.Token, 1)
2024-04-11 10:04:03 +00:00
2024-04-08 10:13:01 +00:00
// log.Println(param)
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}
// @Action account/registerSmsCode
// AccountRegisterSmsCode
// action: string;
// country_code?: string;
// telephone?: string;
// token: string;
2024-04-12 09:17:10 +00:00
func AccountRegisterSmsCode(ctx *ActionContext[AccountRegisterSmsCodeParam]) (resp *basic.Response) {
2024-04-07 10:12:24 +00:00
// ctx.ShouldBind()
log.Println()
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}
// @Action account/forgetSmsCode
// AccountForgetSmsCode
// action: string;
// country_code?: string;
// telephone?: string;
// token: string;
2024-04-12 09:17:10 +00:00
func AccountForgetSmsCode(ctx *ActionContext[AccountForgetSmsCodeParam]) (resp *basic.Response) {
2024-04-07 10:12:24 +00:00
// ctx.ShouldBind()
2024-04-12 09:17:10 +00:00
// ctx *gin.Context, param *AccountForgetSmsCodeParam
2024-04-07 10:12:24 +00:00
log.Println()
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}
2024-04-09 10:17:08 +00:00
type RegisterValidEmailCode struct {
Code string
Email string
AppMarket int
}
2024-04-07 10:12:24 +00:00
// @Action account/registerEmailCode
// AccountRegisterEmailCode
2024-04-09 05:38:29 +00:00
// randstr: string;
// sign: string;
// action: string;
// app_market: int;
// email: string;
// timestamp: int64;
// token: string;
2024-04-12 09:17:10 +00:00
func AccountRegisterEmailCode(ctx *ActionContext[AccountRegisterEmailCodeParam]) (resp *basic.Response) {
2024-04-09 10:17:08 +00:00
2024-04-12 09:17:10 +00:00
if !email.IsEmailValid(ctx.Param.Email) {
2024-04-12 02:20:29 +00:00
return resp.Error(basic.ErrEmailFormat)
2024-04-09 10:17:08 +00:00
}
gcm := encryption_decryption.NewSecretGCM[RegisterValidEmailCode](CompanyKey)
code := auth.GenerateVerificationCode()
codetoken := &RegisterValidEmailCode{
Code: code,
2024-04-12 09:17:10 +00:00
Email: ctx.Param.Email,
AppMarket: ctx.Param.AppMarket,
2024-04-09 10:17:08 +00:00
}
tokenstr, err := gcm.Encrypt(codetoken)
if err != nil {
2024-04-12 02:20:29 +00:00
return resp.Error(basic.ErrEncGcm)
2024-04-09 10:17:08 +00:00
}
2024-04-08 10:13:01 +00:00
2024-04-09 10:17:08 +00:00
resp.Success(map[string]any{
"token": tokenstr,
})
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}
// @Action member/alterPassword
// MemberAlterPassword
// action: string;
// confirm_password: string;
// new_password: string;
// old_password: string;
// token?: string;
2024-04-12 09:17:10 +00:00
func MemberAlterPassword(ctx *ActionContext[MemberAlterPasswordParam]) (resp *basic.Response) {
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}
// @Action account/loginWithEmailPassword
// AccountLoginWithEmailPassword
2024-04-09 05:38:29 +00:00
// password: string;
// randstr: string;
// sign: string;
// action: string;
// device: string;
// version: string;
2024-04-12 09:17:10 +00:00
// app_market: uint64;
2024-04-09 05:38:29 +00:00
// email: string;
// timestamp: int64;
// token: string;
2024-04-12 09:17:10 +00:00
func AccountLoginWithEmailPassword(ctx *ActionContext[AccountLoginWithEmailPasswordParam]) (resp *basic.Response) {
if ctx.Param.Email == "" {
return resp.ErrorMsg(1, "email 参数缺失")
}
if ctx.Param.Password == "" {
return resp.ErrorMsg(1, "password 参数缺失")
}
email := strings.TrimSpace(ctx.Param.Email)
password := strings.TrimSpace(ctx.Param.Password)
var customer *model.KillaraCustomer
var err error
customer, err = model.Models.KillaraCustomerModel.GetCustomerByEmailForBackEnd(email)
if err != nil {
return resp.ErrorErr(1, err)
}
if customer == nil {
return resp.ErrorTrCode(ctx, translator.AccountNotRegistered)
}
if *customer.Status != 1 {
return resp.ErrorTrCode(ctx, translator.AccountDisabledNotLogin)
}
if !auth.CheckPassword(*customer.Password, *customer.Salt, *customer.RandomPassword, password) {
return resp.ErrorTrCode(ctx, translator.AccountOrPasswordLoginFailed)
}
customerID := *customer.CustomerId
err = model.Models.KillaraCustomerTokenModel.UpdateTokenCustomerID(ctx.Param.Token, customerID)
if err != nil {
return resp.ErrorMsg(1, err.Error())
}
var deviceCode string
if ctx.Param.Device != "" {
deviceCode = ctx.Param.Device
}
var version string
if ctx.Param.Version != "" {
version = ctx.Param.Version
}
var ip string
addr := strings.Split(ctx.GinCtx.Request.RemoteAddr, ":")
if len(addr) > 0 {
ip = addr[0]
}
data := &model.KillaraCustomerDevice{
CustomerId: &customerID,
Device: &deviceCode,
Version: &version,
Ip: &ip,
AppMarket: &ctx.Param.AppMarket,
Date: basic.TimePtr(time.Now()),
}
err = model.Models.KillaraCustomerModel.InsertCustomerDevice(data)
if err != nil {
return resp.ErrorErr(1, err)
}
model.Models.KillaraCustomerTokenModel.ClearDuplicateToken(customerID, ctx.Param.Token, 1)
2024-04-09 05:38:29 +00:00
2024-04-12 02:20:29 +00:00
return resp.Success()
2024-04-07 10:12:24 +00:00
}