396 lines
9.9 KiB
Go
396 lines
9.9 KiB
Go
package actions
|
||
|
||
import (
|
||
"encoding/json"
|
||
"strings"
|
||
"time"
|
||
|
||
"github.com/iapologizewhenimwrong/Vestmore_GO/model"
|
||
"github.com/iapologizewhenimwrong/Vestmore_GO/translator"
|
||
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/auth"
|
||
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/basic"
|
||
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/email"
|
||
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/encryption_decryption"
|
||
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/log"
|
||
)
|
||
|
||
var CompanyKey = "vestmore-bjwl"
|
||
|
||
// @Action base/getToken
|
||
// Base_GetToken
|
||
// randstr: string;
|
||
// sign: string;
|
||
// action: string;
|
||
// lang: string;
|
||
// app_market: int64;
|
||
// timestamp: int64;
|
||
// token: string;
|
||
func BaseGetToken(ctx *ActionContext[BaseGetTokenParam]) (resp *basic.Response) {
|
||
|
||
const (
|
||
TokenExpiry = 864000 // Token过期时间,单位:秒
|
||
)
|
||
|
||
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(nil, *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)
|
||
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,
|
||
}})
|
||
}
|
||
|
||
// @Action account/loginWithTelephonePassword
|
||
// AccountLoginWithTelephonePassword
|
||
// randstr: string;
|
||
// sign: string;
|
||
// telephone: string;
|
||
// version: string;.0.14
|
||
// token: string;
|
||
// country_code: string; 86
|
||
// password: string;
|
||
// action: string;
|
||
// device: string;,12
|
||
// app_market: uint64;
|
||
// email: string;
|
||
// timestamp: int64;
|
||
func AccountLoginWithTelephonePassword(ctx *ActionContext[AccountLoginWithTelephonePasswordParam]) (resp *basic.Response) {
|
||
// ctx.ShouldBind()
|
||
// model.Models.KillaraCustomerModel.Find()
|
||
|
||
if ctx.Param.CountryCode == "" {
|
||
resp.ErrorMsg(1, "country_code 参数缺失")
|
||
return
|
||
|
||
}
|
||
if ctx.Param.Telephone == "" {
|
||
resp.ErrorMsg(1, "telephone 参数缺失")
|
||
return
|
||
}
|
||
|
||
if ctx.Param.Password == "" {
|
||
resp.ErrorMsg(1, "password 参数缺失")
|
||
return
|
||
}
|
||
|
||
telephone := strings.TrimSpace(ctx.Param.Telephone)
|
||
password := strings.TrimSpace(ctx.Param.Password)
|
||
countryCode := strings.TrimSpace(ctx.Param.CountryCode)
|
||
|
||
// 假设 modelCustomer 和 modelCustomerToken 是对应的服务接口
|
||
|
||
var customer *model.KillaraCustomer
|
||
var err error
|
||
|
||
customer, err = model.Models.KillaraCustomerModel.GetCustomerByTelephoneForBackEnd(nil, telephone)
|
||
if err != nil {
|
||
resp.ErrorMsg(1, err.Error())
|
||
return
|
||
}
|
||
if customer == nil {
|
||
customer, err = model.Models.KillaraCustomerModel.GetCustomerByCode(nil, telephone)
|
||
if err != nil {
|
||
resp.ErrorMsg(1, err.Error())
|
||
return
|
||
}
|
||
}
|
||
|
||
if customer == nil {
|
||
return resp.ErrorTrCode(ctx, translator.AccountNotRegistered)
|
||
}
|
||
|
||
if *customer.CountryCode != countryCode {
|
||
return resp.ErrorTrCode(ctx, translator.PhoneNumberCountryIncorrect)
|
||
}
|
||
|
||
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()),
|
||
}
|
||
|
||
// 假设 insertCustomerDevice 是对应的服务接口
|
||
// insertCustomerDevice(data)
|
||
err = model.Models.KillaraCustomerModel.InsertCustomerDevice(data)
|
||
if err != nil {
|
||
return resp.ErrorErr(1, err)
|
||
}
|
||
|
||
// // 假设 clearDuplicateToken 是对应的服务接口
|
||
// clearDuplicateToken(customerID, param.Token)
|
||
model.Models.KillaraCustomerTokenModel.ClearDuplicateToken(customerID, ctx.Param.Token, 1)
|
||
|
||
// log.Println(param)
|
||
return resp.Success()
|
||
}
|
||
|
||
// @Action account/registerSmsCode
|
||
// AccountRegisterSmsCode
|
||
// action: string;
|
||
// country_code?: string;
|
||
// telephone?: string;
|
||
// token: string;
|
||
func AccountRegisterSmsCode(ctx *ActionContext[AccountRegisterSmsCodeParam]) (resp *basic.Response) {
|
||
// ctx.ShouldBind()
|
||
log.Println()
|
||
return resp.Success()
|
||
}
|
||
|
||
// @Action account/forgetSmsCode
|
||
// AccountForgetSmsCode
|
||
// action: string;
|
||
// country_code?: string;
|
||
// telephone?: string;
|
||
// token: string;
|
||
func AccountForgetSmsCode(ctx *ActionContext[AccountForgetSmsCodeParam]) (resp *basic.Response) {
|
||
// ctx.ShouldBind()
|
||
// ctx *gin.Context, param *AccountForgetSmsCodeParam
|
||
log.Println()
|
||
return resp.Success()
|
||
}
|
||
|
||
type RegisterValidEmailCode struct {
|
||
Code string
|
||
Email string
|
||
AppMarket int
|
||
}
|
||
|
||
// @Action account/registerEmailCode
|
||
// AccountRegisterEmailCode
|
||
// randstr: string;
|
||
// sign: string;
|
||
// action: string;
|
||
// app_market: int;
|
||
// email: string;
|
||
// timestamp: int64;
|
||
// token: string;
|
||
func AccountRegisterEmailCode(ctx *ActionContext[AccountRegisterEmailCodeParam]) (resp *basic.Response) {
|
||
|
||
if !email.IsEmailValid(ctx.Param.Email) {
|
||
return resp.Error(basic.ErrEmailFormat)
|
||
}
|
||
|
||
gcm := encryption_decryption.NewSecretGCM[RegisterValidEmailCode](CompanyKey)
|
||
|
||
code := auth.GenerateVerificationCode()
|
||
codetoken := &RegisterValidEmailCode{
|
||
Code: code,
|
||
Email: ctx.Param.Email,
|
||
AppMarket: ctx.Param.AppMarket,
|
||
}
|
||
tokenstr, err := gcm.Encrypt(codetoken)
|
||
if err != nil {
|
||
return resp.Error(basic.ErrEncGcm)
|
||
}
|
||
|
||
resp.Success(map[string]any{
|
||
"token": tokenstr,
|
||
})
|
||
return resp.Success()
|
||
}
|
||
|
||
// @Action member/alterPassword
|
||
// MemberAlterPassword
|
||
// action: string;
|
||
// confirm_password: string;
|
||
// new_password: string;
|
||
// old_password: string;
|
||
// token?: string;
|
||
func MemberAlterPassword(ctx *ActionContext[MemberAlterPasswordParam]) (resp *basic.Response) {
|
||
return resp.Success()
|
||
}
|
||
|
||
// @Action account/loginWithEmailPassword
|
||
// AccountLoginWithEmailPassword
|
||
// password: string;
|
||
// randstr: string;
|
||
// sign: string;
|
||
// action: string;
|
||
// device: string;
|
||
// version: string;
|
||
// app_market: uint64;
|
||
// email: string;
|
||
// timestamp: int64;
|
||
// token: string;
|
||
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(nil, 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)
|
||
|
||
return resp.Success()
|
||
}
|